]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/i915_gem_gtt.c
drm/i915: Convert remaining users of 32bit power domain masks
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / i915_gem_gtt.c
CommitLineData
76aaf220
DV
1/*
2 * Copyright © 2010 Daniel Vetter
c4ac524c 3 * Copyright © 2011-2014 Intel Corporation
76aaf220
DV
4 *
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
aae4a3d8
CW
26#include <linux/slab.h> /* fault-inject.h is not standalone! */
27
28#include <linux/fault-inject.h>
e007b19d 29#include <linux/log2.h>
606fec95 30#include <linux/random.h>
0e46ce2e 31#include <linux/seq_file.h>
5bab6f60 32#include <linux/stop_machine.h>
e007b19d 33
760285e7
DH
34#include <drm/drmP.h>
35#include <drm/i915_drm.h>
e007b19d 36
76aaf220 37#include "i915_drv.h"
5dda8fa3 38#include "i915_vgpu.h"
76aaf220
DV
39#include "i915_trace.h"
40#include "intel_drv.h"
d07f0e59 41#include "intel_frontbuffer.h"
76aaf220 42
bb8f9cff
CW
43#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
44
45f8f69a
TU
45/**
46 * DOC: Global GTT views
47 *
48 * Background and previous state
49 *
50 * Historically objects could exists (be bound) in global GTT space only as
51 * singular instances with a view representing all of the object's backing pages
52 * in a linear fashion. This view will be called a normal view.
53 *
54 * To support multiple views of the same object, where the number of mapped
55 * pages is not equal to the backing store, or where the layout of the pages
56 * is not linear, concept of a GGTT view was added.
57 *
58 * One example of an alternative view is a stereo display driven by a single
59 * image. In this case we would have a framebuffer looking like this
60 * (2x2 pages):
61 *
62 * 12
63 * 34
64 *
65 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
66 * rendering. In contrast, fed to the display engine would be an alternative
67 * view which could look something like this:
68 *
69 * 1212
70 * 3434
71 *
72 * In this example both the size and layout of pages in the alternative view is
73 * different from the normal view.
74 *
75 * Implementation and usage
76 *
77 * GGTT views are implemented using VMAs and are distinguished via enum
78 * i915_ggtt_view_type and struct i915_ggtt_view.
79 *
80 * A new flavour of core GEM functions which work with GGTT bound objects were
ec7adb6e
JL
81 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
82 * renaming in large amounts of code. They take the struct i915_ggtt_view
83 * parameter encapsulating all metadata required to implement a view.
45f8f69a
TU
84 *
85 * As a helper for callers which are only interested in the normal view,
86 * globally const i915_ggtt_view_normal singleton instance exists. All old core
87 * GEM API functions, the ones not taking the view parameter, are operating on,
88 * or with the normal GGTT view.
89 *
90 * Code wanting to add or use a new GGTT view needs to:
91 *
92 * 1. Add a new enum with a suitable name.
93 * 2. Extend the metadata in the i915_ggtt_view structure if required.
94 * 3. Add support to i915_get_vma_pages().
95 *
96 * New views are required to build a scatter-gather table from within the
97 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
98 * exists for the lifetime of an VMA.
99 *
100 * Core API is designed to have copy semantics which means that passed in
101 * struct i915_ggtt_view does not need to be persistent (left around after
102 * calling the core API functions).
103 *
104 */
105
70b9f6f8
DV
106static int
107i915_get_ggtt_vma_pages(struct i915_vma *vma);
108
7c3f86b6
CW
109static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
110{
111 /* Note that as an uncached mmio write, this should flush the
112 * WCB of the writes into the GGTT before it triggers the invalidate.
113 */
114 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
115}
116
117static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
118{
119 gen6_ggtt_invalidate(dev_priv);
120 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
121}
122
123static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
124{
125 intel_gtt_chipset_flush();
126}
127
128static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
129{
130 i915->ggtt.invalidate(i915);
131}
132
c033666a
CW
133int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
134 int enable_ppgtt)
cfa7c862 135{
1893a71b
CW
136 bool has_aliasing_ppgtt;
137 bool has_full_ppgtt;
1f9a99e0 138 bool has_full_48bit_ppgtt;
1893a71b 139
9e1d0e60
MT
140 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
141 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
142 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
1893a71b 143
e320d400
ZW
144 if (intel_vgpu_active(dev_priv)) {
145 /* emulation is too hard */
146 has_full_ppgtt = false;
147 has_full_48bit_ppgtt = false;
148 }
71ba2d64 149
0e4ca100
CW
150 if (!has_aliasing_ppgtt)
151 return 0;
152
70ee45e1
DL
153 /*
154 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
155 * execlists, the sole mechanism available to submit work.
156 */
c033666a 157 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
cfa7c862
DV
158 return 0;
159
160 if (enable_ppgtt == 1)
161 return 1;
162
1893a71b 163 if (enable_ppgtt == 2 && has_full_ppgtt)
cfa7c862
DV
164 return 2;
165
1f9a99e0
MT
166 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
167 return 3;
168
93a25a9e
DV
169#ifdef CONFIG_INTEL_IOMMU
170 /* Disable ppgtt on SNB if VT-d is on. */
c033666a 171 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
93a25a9e 172 DRM_INFO("Disabling PPGTT because VT-d is on\n");
cfa7c862 173 return 0;
93a25a9e
DV
174 }
175#endif
176
62942ed7 177 /* Early VLV doesn't have this */
91c8a326 178 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
62942ed7
JB
179 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
180 return 0;
181 }
182
e320d400 183 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
1f9a99e0 184 return has_full_48bit_ppgtt ? 3 : 2;
2f82bbdf
MT
185 else
186 return has_aliasing_ppgtt ? 1 : 0;
93a25a9e
DV
187}
188
70b9f6f8
DV
189static int ppgtt_bind_vma(struct i915_vma *vma,
190 enum i915_cache_level cache_level,
191 u32 unused)
47552659
DV
192{
193 u32 pte_flags = 0;
194
a4f5ea64 195 vma->pages = vma->obj->mm.pages;
247177dd 196
47552659
DV
197 /* Currently applicable only to VLV */
198 if (vma->obj->gt_ro)
199 pte_flags |= PTE_READ_ONLY;
200
247177dd 201 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
47552659 202 cache_level, pte_flags);
70b9f6f8
DV
203
204 return 0;
47552659
DV
205}
206
207static void ppgtt_unbind_vma(struct i915_vma *vma)
208{
209 vma->vm->clear_range(vma->vm,
210 vma->node.start,
4fb84d99 211 vma->size);
47552659 212}
6f65e29a 213
2c642b07 214static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
4fb84d99 215 enum i915_cache_level level)
94ec8f61 216{
4fb84d99 217 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
94ec8f61 218 pte |= addr;
63c42e56
BW
219
220 switch (level) {
221 case I915_CACHE_NONE:
fbe5d36e 222 pte |= PPAT_UNCACHED_INDEX;
63c42e56
BW
223 break;
224 case I915_CACHE_WT:
225 pte |= PPAT_DISPLAY_ELLC_INDEX;
226 break;
227 default:
228 pte |= PPAT_CACHED_INDEX;
229 break;
230 }
231
94ec8f61
BW
232 return pte;
233}
234
fe36f55d
MK
235static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
236 const enum i915_cache_level level)
b1fe6673 237{
07749ef3 238 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
b1fe6673
BW
239 pde |= addr;
240 if (level != I915_CACHE_NONE)
241 pde |= PPAT_CACHED_PDE_INDEX;
242 else
243 pde |= PPAT_UNCACHED_INDEX;
244 return pde;
245}
246
762d9936
MT
247#define gen8_pdpe_encode gen8_pde_encode
248#define gen8_pml4e_encode gen8_pde_encode
249
07749ef3
MT
250static gen6_pte_t snb_pte_encode(dma_addr_t addr,
251 enum i915_cache_level level,
4fb84d99 252 u32 unused)
54d12527 253{
4fb84d99 254 gen6_pte_t pte = GEN6_PTE_VALID;
54d12527 255 pte |= GEN6_PTE_ADDR_ENCODE(addr);
e7210c3c
BW
256
257 switch (level) {
350ec881
CW
258 case I915_CACHE_L3_LLC:
259 case I915_CACHE_LLC:
260 pte |= GEN6_PTE_CACHE_LLC;
261 break;
262 case I915_CACHE_NONE:
263 pte |= GEN6_PTE_UNCACHED;
264 break;
265 default:
5f77eeb0 266 MISSING_CASE(level);
350ec881
CW
267 }
268
269 return pte;
270}
271
07749ef3
MT
272static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
273 enum i915_cache_level level,
4fb84d99 274 u32 unused)
350ec881 275{
4fb84d99 276 gen6_pte_t pte = GEN6_PTE_VALID;
350ec881
CW
277 pte |= GEN6_PTE_ADDR_ENCODE(addr);
278
279 switch (level) {
280 case I915_CACHE_L3_LLC:
281 pte |= GEN7_PTE_CACHE_L3_LLC;
e7210c3c
BW
282 break;
283 case I915_CACHE_LLC:
284 pte |= GEN6_PTE_CACHE_LLC;
285 break;
286 case I915_CACHE_NONE:
9119708c 287 pte |= GEN6_PTE_UNCACHED;
e7210c3c
BW
288 break;
289 default:
5f77eeb0 290 MISSING_CASE(level);
e7210c3c
BW
291 }
292
54d12527
BW
293 return pte;
294}
295
07749ef3
MT
296static gen6_pte_t byt_pte_encode(dma_addr_t addr,
297 enum i915_cache_level level,
4fb84d99 298 u32 flags)
93c34e70 299{
4fb84d99 300 gen6_pte_t pte = GEN6_PTE_VALID;
93c34e70
KG
301 pte |= GEN6_PTE_ADDR_ENCODE(addr);
302
24f3a8cf
AG
303 if (!(flags & PTE_READ_ONLY))
304 pte |= BYT_PTE_WRITEABLE;
93c34e70
KG
305
306 if (level != I915_CACHE_NONE)
307 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
308
309 return pte;
310}
311
07749ef3
MT
312static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
313 enum i915_cache_level level,
4fb84d99 314 u32 unused)
9119708c 315{
4fb84d99 316 gen6_pte_t pte = GEN6_PTE_VALID;
0d8ff15e 317 pte |= HSW_PTE_ADDR_ENCODE(addr);
9119708c
KG
318
319 if (level != I915_CACHE_NONE)
87a6b688 320 pte |= HSW_WB_LLC_AGE3;
9119708c
KG
321
322 return pte;
323}
324
07749ef3
MT
325static gen6_pte_t iris_pte_encode(dma_addr_t addr,
326 enum i915_cache_level level,
4fb84d99 327 u32 unused)
4d15c145 328{
4fb84d99 329 gen6_pte_t pte = GEN6_PTE_VALID;
4d15c145
BW
330 pte |= HSW_PTE_ADDR_ENCODE(addr);
331
651d794f
CW
332 switch (level) {
333 case I915_CACHE_NONE:
334 break;
335 case I915_CACHE_WT:
c51e9701 336 pte |= HSW_WT_ELLC_LLC_AGE3;
651d794f
CW
337 break;
338 default:
c51e9701 339 pte |= HSW_WB_ELLC_LLC_AGE3;
651d794f
CW
340 break;
341 }
4d15c145
BW
342
343 return pte;
344}
345
275a991c 346static int __setup_page_dma(struct drm_i915_private *dev_priv,
c114f76a 347 struct i915_page_dma *p, gfp_t flags)
678d96fb 348{
275a991c 349 struct device *kdev = &dev_priv->drm.pdev->dev;
678d96fb 350
aae4a3d8
CW
351 if (I915_SELFTEST_ONLY(should_fail(&dev_priv->vm_fault, 1)))
352 i915_gem_shrink_all(dev_priv);
353
c114f76a 354 p->page = alloc_page(flags);
44159ddb
MK
355 if (!p->page)
356 return -ENOMEM;
678d96fb 357
c49d13ee 358 p->daddr = dma_map_page(kdev,
f51455d4 359 p->page, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
678d96fb 360
c49d13ee 361 if (dma_mapping_error(kdev, p->daddr)) {
44159ddb
MK
362 __free_page(p->page);
363 return -EINVAL;
364 }
1266cdb1
MT
365
366 return 0;
678d96fb
BW
367}
368
275a991c
TU
369static int setup_page_dma(struct drm_i915_private *dev_priv,
370 struct i915_page_dma *p)
c114f76a 371{
275a991c 372 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
c114f76a
MK
373}
374
275a991c
TU
375static void cleanup_page_dma(struct drm_i915_private *dev_priv,
376 struct i915_page_dma *p)
06fda602 377{
275a991c 378 struct pci_dev *pdev = dev_priv->drm.pdev;
52a05c30 379
44159ddb 380 if (WARN_ON(!p->page))
06fda602 381 return;
678d96fb 382
f51455d4 383 dma_unmap_page(&pdev->dev, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
44159ddb
MK
384 __free_page(p->page);
385 memset(p, 0, sizeof(*p));
386}
387
d1c54acd 388static void *kmap_page_dma(struct i915_page_dma *p)
73eeea53 389{
d1c54acd
MK
390 return kmap_atomic(p->page);
391}
73eeea53 392
d1c54acd
MK
393/* We use the flushing unmap only with ppgtt structures:
394 * page directories, page tables and scratch pages.
395 */
e2d214ae 396static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
d1c54acd 397{
73eeea53
MK
398 /* There are only few exceptions for gen >=6. chv and bxt.
399 * And we are not sure about the latter so play safe for now.
400 */
cc3f90f0 401 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
73eeea53
MK
402 drm_clflush_virt_range(vaddr, PAGE_SIZE);
403
404 kunmap_atomic(vaddr);
405}
406
567047be 407#define kmap_px(px) kmap_page_dma(px_base(px))
e2d214ae 408#define kunmap_px(ppgtt, vaddr) \
49d73912 409 kunmap_page_dma((ppgtt)->base.i915, (vaddr))
d1c54acd 410
275a991c
TU
411#define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
412#define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
e2d214ae
TU
413#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
414#define fill32_px(dev_priv, px, v) \
415 fill_page_dma_32((dev_priv), px_base(px), (v))
567047be 416
e2d214ae
TU
417static void fill_page_dma(struct drm_i915_private *dev_priv,
418 struct i915_page_dma *p, const uint64_t val)
d1c54acd
MK
419{
420 int i;
421 uint64_t * const vaddr = kmap_page_dma(p);
422
423 for (i = 0; i < 512; i++)
424 vaddr[i] = val;
425
e2d214ae 426 kunmap_page_dma(dev_priv, vaddr);
d1c54acd
MK
427}
428
e2d214ae
TU
429static void fill_page_dma_32(struct drm_i915_private *dev_priv,
430 struct i915_page_dma *p, const uint32_t val32)
73eeea53
MK
431{
432 uint64_t v = val32;
433
434 v = v << 32 | val32;
435
e2d214ae 436 fill_page_dma(dev_priv, p, v);
73eeea53
MK
437}
438
8bcdd0f7 439static int
275a991c 440setup_scratch_page(struct drm_i915_private *dev_priv,
bb8f9cff
CW
441 struct i915_page_dma *scratch,
442 gfp_t gfp)
4ad2af1e 443{
275a991c 444 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
4ad2af1e
MK
445}
446
275a991c 447static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
8bcdd0f7 448 struct i915_page_dma *scratch)
4ad2af1e 449{
275a991c 450 cleanup_page_dma(dev_priv, scratch);
4ad2af1e
MK
451}
452
275a991c 453static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
06fda602 454{
ec565b3c 455 struct i915_page_table *pt;
275a991c 456 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
678d96fb 457 int ret = -ENOMEM;
06fda602
BW
458
459 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
460 if (!pt)
461 return ERR_PTR(-ENOMEM);
462
678d96fb
BW
463 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
464 GFP_KERNEL);
465
466 if (!pt->used_ptes)
467 goto fail_bitmap;
468
275a991c 469 ret = setup_px(dev_priv, pt);
678d96fb 470 if (ret)
44159ddb 471 goto fail_page_m;
06fda602
BW
472
473 return pt;
678d96fb 474
44159ddb 475fail_page_m:
678d96fb
BW
476 kfree(pt->used_ptes);
477fail_bitmap:
478 kfree(pt);
479
480 return ERR_PTR(ret);
06fda602
BW
481}
482
275a991c
TU
483static void free_pt(struct drm_i915_private *dev_priv,
484 struct i915_page_table *pt)
06fda602 485{
275a991c 486 cleanup_px(dev_priv, pt);
2e906bea
MK
487 kfree(pt->used_ptes);
488 kfree(pt);
489}
490
491static void gen8_initialize_pt(struct i915_address_space *vm,
492 struct i915_page_table *pt)
493{
494 gen8_pte_t scratch_pte;
495
8bcdd0f7 496 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
4fb84d99 497 I915_CACHE_LLC);
2e906bea 498
49d73912 499 fill_px(vm->i915, pt, scratch_pte);
2e906bea
MK
500}
501
502static void gen6_initialize_pt(struct i915_address_space *vm,
503 struct i915_page_table *pt)
504{
505 gen6_pte_t scratch_pte;
506
8bcdd0f7 507 WARN_ON(vm->scratch_page.daddr == 0);
2e906bea 508
8bcdd0f7 509 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
4fb84d99 510 I915_CACHE_LLC, 0);
2e906bea 511
49d73912 512 fill32_px(vm->i915, pt, scratch_pte);
06fda602
BW
513}
514
275a991c 515static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
06fda602 516{
ec565b3c 517 struct i915_page_directory *pd;
33c8819f 518 int ret = -ENOMEM;
06fda602
BW
519
520 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
521 if (!pd)
522 return ERR_PTR(-ENOMEM);
523
33c8819f
MT
524 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
525 sizeof(*pd->used_pdes), GFP_KERNEL);
526 if (!pd->used_pdes)
a08e111a 527 goto fail_bitmap;
33c8819f 528
275a991c 529 ret = setup_px(dev_priv, pd);
33c8819f 530 if (ret)
a08e111a 531 goto fail_page_m;
e5815a2e 532
06fda602 533 return pd;
33c8819f 534
a08e111a 535fail_page_m:
33c8819f 536 kfree(pd->used_pdes);
a08e111a 537fail_bitmap:
33c8819f
MT
538 kfree(pd);
539
540 return ERR_PTR(ret);
06fda602
BW
541}
542
275a991c
TU
543static void free_pd(struct drm_i915_private *dev_priv,
544 struct i915_page_directory *pd)
2e906bea
MK
545{
546 if (px_page(pd)) {
275a991c 547 cleanup_px(dev_priv, pd);
2e906bea
MK
548 kfree(pd->used_pdes);
549 kfree(pd);
550 }
551}
552
553static void gen8_initialize_pd(struct i915_address_space *vm,
554 struct i915_page_directory *pd)
555{
556 gen8_pde_t scratch_pde;
557
558 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
559
49d73912 560 fill_px(vm->i915, pd, scratch_pde);
2e906bea
MK
561}
562
275a991c 563static int __pdp_init(struct drm_i915_private *dev_priv,
6ac18502
MT
564 struct i915_page_directory_pointer *pdp)
565{
275a991c 566 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
6ac18502
MT
567
568 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
569 sizeof(unsigned long),
570 GFP_KERNEL);
571 if (!pdp->used_pdpes)
572 return -ENOMEM;
573
574 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
575 GFP_KERNEL);
576 if (!pdp->page_directory) {
577 kfree(pdp->used_pdpes);
578 /* the PDP might be the statically allocated top level. Keep it
579 * as clean as possible */
580 pdp->used_pdpes = NULL;
581 return -ENOMEM;
582 }
583
584 return 0;
585}
586
587static void __pdp_fini(struct i915_page_directory_pointer *pdp)
588{
589 kfree(pdp->used_pdpes);
590 kfree(pdp->page_directory);
591 pdp->page_directory = NULL;
592}
593
762d9936 594static struct
275a991c 595i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
762d9936
MT
596{
597 struct i915_page_directory_pointer *pdp;
598 int ret = -ENOMEM;
599
275a991c 600 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
762d9936
MT
601
602 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
603 if (!pdp)
604 return ERR_PTR(-ENOMEM);
605
275a991c 606 ret = __pdp_init(dev_priv, pdp);
762d9936
MT
607 if (ret)
608 goto fail_bitmap;
609
275a991c 610 ret = setup_px(dev_priv, pdp);
762d9936
MT
611 if (ret)
612 goto fail_page_m;
613
614 return pdp;
615
616fail_page_m:
617 __pdp_fini(pdp);
618fail_bitmap:
619 kfree(pdp);
620
621 return ERR_PTR(ret);
622}
623
275a991c 624static void free_pdp(struct drm_i915_private *dev_priv,
6ac18502
MT
625 struct i915_page_directory_pointer *pdp)
626{
627 __pdp_fini(pdp);
275a991c
TU
628 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
629 cleanup_px(dev_priv, pdp);
762d9936
MT
630 kfree(pdp);
631 }
632}
633
69ab76fd
MT
634static void gen8_initialize_pdp(struct i915_address_space *vm,
635 struct i915_page_directory_pointer *pdp)
636{
637 gen8_ppgtt_pdpe_t scratch_pdpe;
638
639 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
640
49d73912 641 fill_px(vm->i915, pdp, scratch_pdpe);
69ab76fd
MT
642}
643
644static void gen8_initialize_pml4(struct i915_address_space *vm,
645 struct i915_pml4 *pml4)
646{
647 gen8_ppgtt_pml4e_t scratch_pml4e;
648
649 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
650 I915_CACHE_LLC);
651
49d73912 652 fill_px(vm->i915, pml4, scratch_pml4e);
69ab76fd
MT
653}
654
762d9936 655static void
5c693b2b
MA
656gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
657 struct i915_page_directory_pointer *pdp,
658 struct i915_page_directory *pd,
659 int index)
762d9936
MT
660{
661 gen8_ppgtt_pdpe_t *page_directorypo;
662
275a991c 663 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
762d9936
MT
664 return;
665
666 page_directorypo = kmap_px(pdp);
667 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
668 kunmap_px(ppgtt, page_directorypo);
669}
670
671static void
56843107
MA
672gen8_setup_pml4e(struct i915_hw_ppgtt *ppgtt,
673 struct i915_pml4 *pml4,
674 struct i915_page_directory_pointer *pdp,
675 int index)
762d9936
MT
676{
677 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
678
275a991c 679 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
762d9936
MT
680 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
681 kunmap_px(ppgtt, pagemap);
6ac18502
MT
682}
683
94e409c1 684/* Broadwell Page Directory Pointer Descriptors */
e85b26dc 685static int gen8_write_pdp(struct drm_i915_gem_request *req,
7cb6d7ac
MT
686 unsigned entry,
687 dma_addr_t addr)
94e409c1 688{
7e37f889 689 struct intel_ring *ring = req->ring;
4a570db5 690 struct intel_engine_cs *engine = req->engine;
94e409c1
BW
691 int ret;
692
693 BUG_ON(entry >= 4);
694
5fb9de1a 695 ret = intel_ring_begin(req, 6);
94e409c1
BW
696 if (ret)
697 return ret;
698
b5321f30
CW
699 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
700 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
701 intel_ring_emit(ring, upper_32_bits(addr));
702 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
703 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
704 intel_ring_emit(ring, lower_32_bits(addr));
705 intel_ring_advance(ring);
94e409c1
BW
706
707 return 0;
708}
709
2dba3239
MT
710static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
711 struct drm_i915_gem_request *req)
94e409c1 712{
eeb9488e 713 int i, ret;
94e409c1 714
7cb6d7ac 715 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
d852c7bf
MK
716 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
717
e85b26dc 718 ret = gen8_write_pdp(req, i, pd_daddr);
eeb9488e
BW
719 if (ret)
720 return ret;
94e409c1 721 }
d595bd4b 722
eeb9488e 723 return 0;
94e409c1
BW
724}
725
2dba3239
MT
726static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
727 struct drm_i915_gem_request *req)
728{
729 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
730}
731
fce93755
MK
732/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
733 * the page table structures, we mark them dirty so that
734 * context switching/execlist queuing code takes extra steps
735 * to ensure that tlbs are flushed.
736 */
737static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
738{
49d73912 739 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
fce93755
MK
740}
741
2ce5179f
MW
742/* Removes entries from a single page table, releasing it if it's empty.
743 * Caller can use the return value to update higher-level entries.
744 */
745static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
d209b9c3
MW
746 struct i915_page_table *pt,
747 uint64_t start,
748 uint64_t length)
459108b8 749{
e5716f55 750 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
d209b9c3 751 unsigned int num_entries = gen8_pte_count(start, length);
37c63934
MK
752 unsigned int pte = gen8_pte_index(start);
753 unsigned int pte_end = pte + num_entries;
f9b5b782 754 gen8_pte_t *pt_vaddr;
d209b9c3
MW
755 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
756 I915_CACHE_LLC);
459108b8 757
d209b9c3 758 if (WARN_ON(!px_page(pt)))
2ce5179f 759 return false;
459108b8 760
37c63934
MK
761 GEM_BUG_ON(pte_end > GEN8_PTES);
762
763 bitmap_clear(pt->used_ptes, pte, num_entries);
e81ecb5e
ZW
764 if (USES_FULL_PPGTT(vm->i915)) {
765 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
766 return true;
767 }
2ce5179f 768
d209b9c3
MW
769 pt_vaddr = kmap_px(pt);
770
37c63934
MK
771 while (pte < pte_end)
772 pt_vaddr[pte++] = scratch_pte;
06fda602 773
d209b9c3 774 kunmap_px(ppgtt, pt_vaddr);
2ce5179f
MW
775
776 return false;
d209b9c3 777}
06fda602 778
2ce5179f
MW
779/* Removes entries from a single page dir, releasing it if it's empty.
780 * Caller can use the return value to update higher-level entries
781 */
782static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
d209b9c3
MW
783 struct i915_page_directory *pd,
784 uint64_t start,
785 uint64_t length)
786{
2ce5179f 787 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
d209b9c3
MW
788 struct i915_page_table *pt;
789 uint64_t pde;
2ce5179f
MW
790 gen8_pde_t *pde_vaddr;
791 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
792 I915_CACHE_LLC);
d209b9c3
MW
793
794 gen8_for_each_pde(pt, pd, start, length, pde) {
06fda602 795 if (WARN_ON(!pd->page_table[pde]))
00245266 796 break;
06fda602 797
2ce5179f
MW
798 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
799 __clear_bit(pde, pd->used_pdes);
800 pde_vaddr = kmap_px(pd);
801 pde_vaddr[pde] = scratch_pde;
802 kunmap_px(ppgtt, pde_vaddr);
49d73912 803 free_pt(vm->i915, pt);
2ce5179f
MW
804 }
805 }
806
a18dbba8 807 if (bitmap_empty(pd->used_pdes, I915_PDES))
2ce5179f 808 return true;
2ce5179f
MW
809
810 return false;
d209b9c3 811}
06fda602 812
2ce5179f
MW
813/* Removes entries from a single page dir pointer, releasing it if it's empty.
814 * Caller can use the return value to update higher-level entries
815 */
816static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
d209b9c3
MW
817 struct i915_page_directory_pointer *pdp,
818 uint64_t start,
819 uint64_t length)
820{
2ce5179f 821 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
d209b9c3
MW
822 struct i915_page_directory *pd;
823 uint64_t pdpe;
06fda602 824
d209b9c3
MW
825 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
826 if (WARN_ON(!pdp->page_directory[pdpe]))
827 break;
459108b8 828
2ce5179f
MW
829 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
830 __clear_bit(pdpe, pdp->used_pdpes);
9e65a378 831 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
49d73912 832 free_pd(vm->i915, pd);
2ce5179f
MW
833 }
834 }
835
fce93755
MK
836 mark_tlbs_dirty(ppgtt);
837
a18dbba8 838 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
2ce5179f 839 return true;
2ce5179f
MW
840
841 return false;
d209b9c3 842}
459108b8 843
2ce5179f
MW
844/* Removes entries from a single pml4.
845 * This is the top-level structure in 4-level page tables used on gen8+.
846 * Empty entries are always scratch pml4e.
847 */
d209b9c3
MW
848static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
849 struct i915_pml4 *pml4,
850 uint64_t start,
851 uint64_t length)
852{
2ce5179f 853 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
d209b9c3
MW
854 struct i915_page_directory_pointer *pdp;
855 uint64_t pml4e;
2ce5179f 856
49d73912 857 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
459108b8 858
d209b9c3
MW
859 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
860 if (WARN_ON(!pml4->pdps[pml4e]))
861 break;
459108b8 862
2ce5179f
MW
863 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
864 __clear_bit(pml4e, pml4->used_pml4es);
9e65a378 865 gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
49d73912 866 free_pdp(vm->i915, pdp);
2ce5179f 867 }
459108b8
BW
868 }
869}
870
f9b5b782 871static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
4fb84d99 872 uint64_t start, uint64_t length)
9df15b49 873{
e5716f55 874 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
f9b5b782 875
c6385c94 876 if (USES_FULL_48BIT_PPGTT(vm->i915))
d209b9c3
MW
877 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
878 else
879 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
f9b5b782
MT
880}
881
882static void
883gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
884 struct i915_page_directory_pointer *pdp,
3387d433 885 struct sg_page_iter *sg_iter,
f9b5b782
MT
886 uint64_t start,
887 enum i915_cache_level cache_level)
888{
e5716f55 889 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
07749ef3 890 gen8_pte_t *pt_vaddr;
de5ba8eb
MT
891 unsigned pdpe = gen8_pdpe_index(start);
892 unsigned pde = gen8_pde_index(start);
893 unsigned pte = gen8_pte_index(start);
9df15b49 894
6f1cc993 895 pt_vaddr = NULL;
7ad47cf2 896
3387d433 897 while (__sg_page_iter_next(sg_iter)) {
d7b3de91 898 if (pt_vaddr == NULL) {
d4ec9da0 899 struct i915_page_directory *pd = pdp->page_directory[pdpe];
ec565b3c 900 struct i915_page_table *pt = pd->page_table[pde];
d1c54acd 901 pt_vaddr = kmap_px(pt);
d7b3de91 902 }
9df15b49 903
7ad47cf2 904 pt_vaddr[pte] =
3387d433 905 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
4fb84d99 906 cache_level);
07749ef3 907 if (++pte == GEN8_PTES) {
d1c54acd 908 kunmap_px(ppgtt, pt_vaddr);
6f1cc993 909 pt_vaddr = NULL;
07749ef3 910 if (++pde == I915_PDES) {
c6385c94 911 if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
de5ba8eb 912 break;
7ad47cf2
BW
913 pde = 0;
914 }
915 pte = 0;
9df15b49
BW
916 }
917 }
d1c54acd
MK
918
919 if (pt_vaddr)
920 kunmap_px(ppgtt, pt_vaddr);
9df15b49
BW
921}
922
f9b5b782
MT
923static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
924 struct sg_table *pages,
925 uint64_t start,
926 enum i915_cache_level cache_level,
927 u32 unused)
928{
e5716f55 929 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
3387d433 930 struct sg_page_iter sg_iter;
f9b5b782 931
3387d433 932 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
de5ba8eb 933
c6385c94 934 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
de5ba8eb
MT
935 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
936 cache_level);
937 } else {
938 struct i915_page_directory_pointer *pdp;
e8ebd8e2 939 uint64_t pml4e;
de5ba8eb
MT
940 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
941
e8ebd8e2 942 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
de5ba8eb
MT
943 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
944 start, cache_level);
945 }
946 }
f9b5b782
MT
947}
948
275a991c 949static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
f37c0505 950 struct i915_page_directory *pd)
7ad47cf2
BW
951{
952 int i;
953
567047be 954 if (!px_page(pd))
7ad47cf2
BW
955 return;
956
33c8819f 957 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
06fda602
BW
958 if (WARN_ON(!pd->page_table[i]))
959 continue;
7ad47cf2 960
275a991c 961 free_pt(dev_priv, pd->page_table[i]);
06fda602
BW
962 pd->page_table[i] = NULL;
963 }
d7b3de91
BW
964}
965
8776f02b
MK
966static int gen8_init_scratch(struct i915_address_space *vm)
967{
49d73912 968 struct drm_i915_private *dev_priv = vm->i915;
64c050db 969 int ret;
8776f02b 970
275a991c 971 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
8bcdd0f7
CW
972 if (ret)
973 return ret;
8776f02b 974
275a991c 975 vm->scratch_pt = alloc_pt(dev_priv);
8776f02b 976 if (IS_ERR(vm->scratch_pt)) {
64c050db
MA
977 ret = PTR_ERR(vm->scratch_pt);
978 goto free_scratch_page;
8776f02b
MK
979 }
980
275a991c 981 vm->scratch_pd = alloc_pd(dev_priv);
8776f02b 982 if (IS_ERR(vm->scratch_pd)) {
64c050db
MA
983 ret = PTR_ERR(vm->scratch_pd);
984 goto free_pt;
8776f02b
MK
985 }
986
275a991c
TU
987 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
988 vm->scratch_pdp = alloc_pdp(dev_priv);
69ab76fd 989 if (IS_ERR(vm->scratch_pdp)) {
64c050db
MA
990 ret = PTR_ERR(vm->scratch_pdp);
991 goto free_pd;
69ab76fd
MT
992 }
993 }
994
8776f02b
MK
995 gen8_initialize_pt(vm, vm->scratch_pt);
996 gen8_initialize_pd(vm, vm->scratch_pd);
275a991c 997 if (USES_FULL_48BIT_PPGTT(dev_priv))
69ab76fd 998 gen8_initialize_pdp(vm, vm->scratch_pdp);
8776f02b
MK
999
1000 return 0;
64c050db
MA
1001
1002free_pd:
275a991c 1003 free_pd(dev_priv, vm->scratch_pd);
64c050db 1004free_pt:
275a991c 1005 free_pt(dev_priv, vm->scratch_pt);
64c050db 1006free_scratch_page:
275a991c 1007 cleanup_scratch_page(dev_priv, &vm->scratch_page);
64c050db
MA
1008
1009 return ret;
8776f02b
MK
1010}
1011
650da34c
ZL
1012static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1013{
1014 enum vgt_g2v_type msg;
49d73912 1015 struct drm_i915_private *dev_priv = ppgtt->base.i915;
650da34c
ZL
1016 int i;
1017
df28564d 1018 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
650da34c
ZL
1019 u64 daddr = px_dma(&ppgtt->pml4);
1020
ab75bb5d
VS
1021 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1022 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
650da34c
ZL
1023
1024 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1025 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1026 } else {
1027 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1028 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1029
ab75bb5d
VS
1030 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1031 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
650da34c
ZL
1032 }
1033
1034 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1035 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1036 }
1037
1038 I915_WRITE(vgtif_reg(g2v_notify), msg);
1039
1040 return 0;
1041}
1042
8776f02b
MK
1043static void gen8_free_scratch(struct i915_address_space *vm)
1044{
49d73912 1045 struct drm_i915_private *dev_priv = vm->i915;
8776f02b 1046
275a991c
TU
1047 if (USES_FULL_48BIT_PPGTT(dev_priv))
1048 free_pdp(dev_priv, vm->scratch_pdp);
1049 free_pd(dev_priv, vm->scratch_pd);
1050 free_pt(dev_priv, vm->scratch_pt);
1051 cleanup_scratch_page(dev_priv, &vm->scratch_page);
8776f02b
MK
1052}
1053
275a991c 1054static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
762d9936 1055 struct i915_page_directory_pointer *pdp)
b45a6715
BW
1056{
1057 int i;
1058
275a991c 1059 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
d4ec9da0 1060 if (WARN_ON(!pdp->page_directory[i]))
06fda602
BW
1061 continue;
1062
275a991c
TU
1063 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1064 free_pd(dev_priv, pdp->page_directory[i]);
7ad47cf2 1065 }
69876bed 1066
275a991c 1067 free_pdp(dev_priv, pdp);
762d9936
MT
1068}
1069
1070static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1071{
49d73912 1072 struct drm_i915_private *dev_priv = ppgtt->base.i915;
762d9936
MT
1073 int i;
1074
1075 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1076 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1077 continue;
1078
275a991c 1079 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
762d9936
MT
1080 }
1081
275a991c 1082 cleanup_px(dev_priv, &ppgtt->pml4);
762d9936
MT
1083}
1084
1085static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1086{
49d73912 1087 struct drm_i915_private *dev_priv = vm->i915;
e5716f55 1088 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
762d9936 1089
275a991c 1090 if (intel_vgpu_active(dev_priv))
650da34c
ZL
1091 gen8_ppgtt_notify_vgt(ppgtt, false);
1092
275a991c
TU
1093 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1094 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
762d9936
MT
1095 else
1096 gen8_ppgtt_cleanup_4lvl(ppgtt);
d4ec9da0 1097
8776f02b 1098 gen8_free_scratch(vm);
b45a6715
BW
1099}
1100
d7b2633d
MT
1101/**
1102 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
d4ec9da0
MT
1103 * @vm: Master vm structure.
1104 * @pd: Page directory for this address range.
d7b2633d 1105 * @start: Starting virtual address to begin allocations.
d4ec9da0 1106 * @length: Size of the allocations.
d7b2633d
MT
1107 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1108 * caller to free on error.
1109 *
1110 * Allocate the required number of page tables. Extremely similar to
1111 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1112 * the page directory boundary (instead of the page directory pointer). That
1113 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1114 * possible, and likely that the caller will need to use multiple calls of this
1115 * function to achieve the appropriate allocation.
1116 *
1117 * Return: 0 if success; negative error code otherwise.
1118 */
d4ec9da0 1119static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
e5815a2e 1120 struct i915_page_directory *pd,
5441f0cb 1121 uint64_t start,
d7b2633d
MT
1122 uint64_t length,
1123 unsigned long *new_pts)
bf2b4ed2 1124{
49d73912 1125 struct drm_i915_private *dev_priv = vm->i915;
d7b2633d 1126 struct i915_page_table *pt;
5441f0cb 1127 uint32_t pde;
bf2b4ed2 1128
e8ebd8e2 1129 gen8_for_each_pde(pt, pd, start, length, pde) {
d7b2633d 1130 /* Don't reallocate page tables */
6ac18502 1131 if (test_bit(pde, pd->used_pdes)) {
d7b2633d 1132 /* Scratch is never allocated this way */
d4ec9da0 1133 WARN_ON(pt == vm->scratch_pt);
d7b2633d
MT
1134 continue;
1135 }
1136
275a991c 1137 pt = alloc_pt(dev_priv);
d7b2633d 1138 if (IS_ERR(pt))
5441f0cb
MT
1139 goto unwind_out;
1140
d4ec9da0 1141 gen8_initialize_pt(vm, pt);
d7b2633d 1142 pd->page_table[pde] = pt;
966082c9 1143 __set_bit(pde, new_pts);
4c06ec8d 1144 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
7ad47cf2
BW
1145 }
1146
bf2b4ed2 1147 return 0;
7ad47cf2
BW
1148
1149unwind_out:
d7b2633d 1150 for_each_set_bit(pde, new_pts, I915_PDES)
275a991c 1151 free_pt(dev_priv, pd->page_table[pde]);
7ad47cf2 1152
d7b3de91 1153 return -ENOMEM;
bf2b4ed2
BW
1154}
1155
d7b2633d
MT
1156/**
1157 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
d4ec9da0 1158 * @vm: Master vm structure.
d7b2633d
MT
1159 * @pdp: Page directory pointer for this address range.
1160 * @start: Starting virtual address to begin allocations.
d4ec9da0
MT
1161 * @length: Size of the allocations.
1162 * @new_pds: Bitmap set by function with new allocations. Likely used by the
d7b2633d
MT
1163 * caller to free on error.
1164 *
1165 * Allocate the required number of page directories starting at the pde index of
1166 * @start, and ending at the pde index @start + @length. This function will skip
1167 * over already allocated page directories within the range, and only allocate
1168 * new ones, setting the appropriate pointer within the pdp as well as the
1169 * correct position in the bitmap @new_pds.
1170 *
1171 * The function will only allocate the pages within the range for a give page
1172 * directory pointer. In other words, if @start + @length straddles a virtually
1173 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1174 * required by the caller, This is not currently possible, and the BUG in the
1175 * code will prevent it.
1176 *
1177 * Return: 0 if success; negative error code otherwise.
1178 */
d4ec9da0
MT
1179static int
1180gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1181 struct i915_page_directory_pointer *pdp,
1182 uint64_t start,
1183 uint64_t length,
1184 unsigned long *new_pds)
bf2b4ed2 1185{
49d73912 1186 struct drm_i915_private *dev_priv = vm->i915;
d7b2633d 1187 struct i915_page_directory *pd;
69876bed 1188 uint32_t pdpe;
275a991c 1189 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
69876bed 1190
6ac18502 1191 WARN_ON(!bitmap_empty(new_pds, pdpes));
d7b2633d 1192
e8ebd8e2 1193 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
6ac18502 1194 if (test_bit(pdpe, pdp->used_pdpes))
d7b2633d 1195 continue;
33c8819f 1196
275a991c 1197 pd = alloc_pd(dev_priv);
d7b2633d 1198 if (IS_ERR(pd))
d7b3de91 1199 goto unwind_out;
69876bed 1200
d4ec9da0 1201 gen8_initialize_pd(vm, pd);
d7b2633d 1202 pdp->page_directory[pdpe] = pd;
966082c9 1203 __set_bit(pdpe, new_pds);
4c06ec8d 1204 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
d7b3de91
BW
1205 }
1206
bf2b4ed2 1207 return 0;
d7b3de91
BW
1208
1209unwind_out:
6ac18502 1210 for_each_set_bit(pdpe, new_pds, pdpes)
275a991c 1211 free_pd(dev_priv, pdp->page_directory[pdpe]);
d7b3de91
BW
1212
1213 return -ENOMEM;
bf2b4ed2
BW
1214}
1215
762d9936
MT
1216/**
1217 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1218 * @vm: Master vm structure.
1219 * @pml4: Page map level 4 for this address range.
1220 * @start: Starting virtual address to begin allocations.
1221 * @length: Size of the allocations.
1222 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1223 * caller to free on error.
1224 *
1225 * Allocate the required number of page directory pointers. Extremely similar to
1226 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1227 * The main difference is here we are limited by the pml4 boundary (instead of
1228 * the page directory pointer).
1229 *
1230 * Return: 0 if success; negative error code otherwise.
1231 */
1232static int
1233gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1234 struct i915_pml4 *pml4,
1235 uint64_t start,
1236 uint64_t length,
1237 unsigned long *new_pdps)
1238{
49d73912 1239 struct drm_i915_private *dev_priv = vm->i915;
762d9936 1240 struct i915_page_directory_pointer *pdp;
762d9936
MT
1241 uint32_t pml4e;
1242
1243 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1244
e8ebd8e2 1245 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
762d9936 1246 if (!test_bit(pml4e, pml4->used_pml4es)) {
275a991c 1247 pdp = alloc_pdp(dev_priv);
762d9936
MT
1248 if (IS_ERR(pdp))
1249 goto unwind_out;
1250
69ab76fd 1251 gen8_initialize_pdp(vm, pdp);
762d9936
MT
1252 pml4->pdps[pml4e] = pdp;
1253 __set_bit(pml4e, new_pdps);
1254 trace_i915_page_directory_pointer_entry_alloc(vm,
1255 pml4e,
1256 start,
1257 GEN8_PML4E_SHIFT);
1258 }
1259 }
1260
1261 return 0;
1262
1263unwind_out:
1264 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
275a991c 1265 free_pdp(dev_priv, pml4->pdps[pml4e]);
762d9936
MT
1266
1267 return -ENOMEM;
1268}
1269
d7b2633d 1270static void
3a41a05d 1271free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
d7b2633d 1272{
d7b2633d
MT
1273 kfree(new_pts);
1274 kfree(new_pds);
1275}
1276
1277/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1278 * of these are based on the number of PDPEs in the system.
1279 */
1280static
1281int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
3a41a05d 1282 unsigned long **new_pts,
6ac18502 1283 uint32_t pdpes)
d7b2633d 1284{
d7b2633d 1285 unsigned long *pds;
3a41a05d 1286 unsigned long *pts;
d7b2633d 1287
3a41a05d 1288 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
d7b2633d
MT
1289 if (!pds)
1290 return -ENOMEM;
1291
3a41a05d
MW
1292 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1293 GFP_TEMPORARY);
1294 if (!pts)
1295 goto err_out;
d7b2633d
MT
1296
1297 *new_pds = pds;
1298 *new_pts = pts;
1299
1300 return 0;
1301
1302err_out:
3a41a05d 1303 free_gen8_temp_bitmaps(pds, pts);
d7b2633d
MT
1304 return -ENOMEM;
1305}
1306
762d9936
MT
1307static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1308 struct i915_page_directory_pointer *pdp,
1309 uint64_t start,
1310 uint64_t length)
bf2b4ed2 1311{
e5716f55 1312 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
3a41a05d 1313 unsigned long *new_page_dirs, *new_page_tables;
49d73912 1314 struct drm_i915_private *dev_priv = vm->i915;
5441f0cb 1315 struct i915_page_directory *pd;
33c8819f
MT
1316 const uint64_t orig_start = start;
1317 const uint64_t orig_length = length;
5441f0cb 1318 uint32_t pdpe;
275a991c 1319 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
bf2b4ed2
BW
1320 int ret;
1321
6ac18502 1322 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
bf2b4ed2
BW
1323 if (ret)
1324 return ret;
1325
d7b2633d 1326 /* Do the allocations first so we can easily bail out */
d4ec9da0
MT
1327 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1328 new_page_dirs);
d7b2633d 1329 if (ret) {
3a41a05d 1330 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
d7b2633d
MT
1331 return ret;
1332 }
1333
1334 /* For every page directory referenced, allocate page tables */
e8ebd8e2 1335 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
d4ec9da0 1336 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
3a41a05d 1337 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
5441f0cb
MT
1338 if (ret)
1339 goto err_out;
5441f0cb
MT
1340 }
1341
33c8819f
MT
1342 start = orig_start;
1343 length = orig_length;
1344
d7b2633d
MT
1345 /* Allocations have completed successfully, so set the bitmaps, and do
1346 * the mappings. */
e8ebd8e2 1347 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
d1c54acd 1348 gen8_pde_t *const page_directory = kmap_px(pd);
33c8819f 1349 struct i915_page_table *pt;
09120d4e 1350 uint64_t pd_len = length;
33c8819f
MT
1351 uint64_t pd_start = start;
1352 uint32_t pde;
1353
d7b2633d
MT
1354 /* Every pd should be allocated, we just did that above. */
1355 WARN_ON(!pd);
1356
e8ebd8e2 1357 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
d7b2633d
MT
1358 /* Same reasoning as pd */
1359 WARN_ON(!pt);
1360 WARN_ON(!pd_len);
1361 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1362
1363 /* Set our used ptes within the page table */
1364 bitmap_set(pt->used_ptes,
1365 gen8_pte_index(pd_start),
1366 gen8_pte_count(pd_start, pd_len));
1367
1368 /* Our pde is now pointing to the pagetable, pt */
966082c9 1369 __set_bit(pde, pd->used_pdes);
d7b2633d
MT
1370
1371 /* Map the PDE to the page table */
fe36f55d
MK
1372 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1373 I915_CACHE_LLC);
4c06ec8d
MT
1374 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1375 gen8_pte_index(start),
1376 gen8_pte_count(start, length),
1377 GEN8_PTES);
d7b2633d
MT
1378
1379 /* NB: We haven't yet mapped ptes to pages. At this
1380 * point we're still relying on insert_entries() */
33c8819f 1381 }
d7b2633d 1382
d1c54acd 1383 kunmap_px(ppgtt, page_directory);
d4ec9da0 1384 __set_bit(pdpe, pdp->used_pdpes);
5c693b2b 1385 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
33c8819f
MT
1386 }
1387
3a41a05d 1388 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
5b7e4c9c 1389 mark_tlbs_dirty(ppgtt);
d7b3de91 1390 return 0;
bf2b4ed2 1391
d7b3de91 1392err_out:
d7b2633d 1393 while (pdpe--) {
e8ebd8e2
DG
1394 unsigned long temp;
1395
3a41a05d
MW
1396 for_each_set_bit(temp, new_page_tables + pdpe *
1397 BITS_TO_LONGS(I915_PDES), I915_PDES)
275a991c
TU
1398 free_pt(dev_priv,
1399 pdp->page_directory[pdpe]->page_table[temp]);
d7b2633d
MT
1400 }
1401
6ac18502 1402 for_each_set_bit(pdpe, new_page_dirs, pdpes)
275a991c 1403 free_pd(dev_priv, pdp->page_directory[pdpe]);
d7b2633d 1404
3a41a05d 1405 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
5b7e4c9c 1406 mark_tlbs_dirty(ppgtt);
bf2b4ed2
BW
1407 return ret;
1408}
1409
762d9936
MT
1410static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1411 struct i915_pml4 *pml4,
1412 uint64_t start,
1413 uint64_t length)
1414{
1415 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
e5716f55 1416 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
762d9936 1417 struct i915_page_directory_pointer *pdp;
e8ebd8e2 1418 uint64_t pml4e;
762d9936
MT
1419 int ret = 0;
1420
1421 /* Do the pml4 allocations first, so we don't need to track the newly
1422 * allocated tables below the pdp */
1423 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1424
1425 /* The pagedirectory and pagetable allocations are done in the shared 3
1426 * and 4 level code. Just allocate the pdps.
1427 */
1428 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1429 new_pdps);
1430 if (ret)
1431 return ret;
1432
e8ebd8e2 1433 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
762d9936
MT
1434 WARN_ON(!pdp);
1435
1436 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1437 if (ret)
1438 goto err_out;
1439
56843107 1440 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
762d9936
MT
1441 }
1442
1443 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1444 GEN8_PML4ES_PER_PML4);
1445
1446 return 0;
1447
1448err_out:
1449 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
49d73912 1450 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
762d9936
MT
1451
1452 return ret;
1453}
1454
1455static int gen8_alloc_va_range(struct i915_address_space *vm,
1456 uint64_t start, uint64_t length)
1457{
e5716f55 1458 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
762d9936 1459
c6385c94 1460 if (USES_FULL_48BIT_PPGTT(vm->i915))
762d9936
MT
1461 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1462 else
1463 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1464}
1465
ea91e401
MT
1466static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1467 uint64_t start, uint64_t length,
1468 gen8_pte_t scratch_pte,
1469 struct seq_file *m)
1470{
1471 struct i915_page_directory *pd;
ea91e401
MT
1472 uint32_t pdpe;
1473
e8ebd8e2 1474 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
ea91e401
MT
1475 struct i915_page_table *pt;
1476 uint64_t pd_len = length;
1477 uint64_t pd_start = start;
1478 uint32_t pde;
1479
1480 if (!test_bit(pdpe, pdp->used_pdpes))
1481 continue;
1482
1483 seq_printf(m, "\tPDPE #%d\n", pdpe);
e8ebd8e2 1484 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
ea91e401
MT
1485 uint32_t pte;
1486 gen8_pte_t *pt_vaddr;
1487
1488 if (!test_bit(pde, pd->used_pdes))
1489 continue;
1490
1491 pt_vaddr = kmap_px(pt);
1492 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1493 uint64_t va =
1494 (pdpe << GEN8_PDPE_SHIFT) |
1495 (pde << GEN8_PDE_SHIFT) |
1496 (pte << GEN8_PTE_SHIFT);
1497 int i;
1498 bool found = false;
1499
1500 for (i = 0; i < 4; i++)
1501 if (pt_vaddr[pte + i] != scratch_pte)
1502 found = true;
1503 if (!found)
1504 continue;
1505
1506 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1507 for (i = 0; i < 4; i++) {
1508 if (pt_vaddr[pte + i] != scratch_pte)
1509 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1510 else
1511 seq_puts(m, " SCRATCH ");
1512 }
1513 seq_puts(m, "\n");
1514 }
1515 /* don't use kunmap_px, it could trigger
1516 * an unnecessary flush.
1517 */
1518 kunmap_atomic(pt_vaddr);
1519 }
1520 }
1521}
1522
1523static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1524{
1525 struct i915_address_space *vm = &ppgtt->base;
1526 uint64_t start = ppgtt->base.start;
1527 uint64_t length = ppgtt->base.total;
8bcdd0f7 1528 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
4fb84d99 1529 I915_CACHE_LLC);
ea91e401 1530
c6385c94 1531 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
ea91e401
MT
1532 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1533 } else {
e8ebd8e2 1534 uint64_t pml4e;
ea91e401
MT
1535 struct i915_pml4 *pml4 = &ppgtt->pml4;
1536 struct i915_page_directory_pointer *pdp;
1537
e8ebd8e2 1538 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
ea91e401
MT
1539 if (!test_bit(pml4e, pml4->used_pml4es))
1540 continue;
1541
1542 seq_printf(m, " PML4E #%llu\n", pml4e);
1543 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1544 }
1545 }
1546}
1547
331f38e7
ZL
1548static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1549{
3a41a05d 1550 unsigned long *new_page_dirs, *new_page_tables;
275a991c 1551 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
331f38e7
ZL
1552 int ret;
1553
1554 /* We allocate temp bitmap for page tables for no gain
1555 * but as this is for init only, lets keep the things simple
1556 */
1557 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1558 if (ret)
1559 return ret;
1560
1561 /* Allocate for all pdps regardless of how the ppgtt
1562 * was defined.
1563 */
1564 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1565 0, 1ULL << 32,
1566 new_page_dirs);
1567 if (!ret)
1568 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1569
3a41a05d 1570 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
331f38e7
ZL
1571
1572 return ret;
1573}
1574
eb0b44ad 1575/*
f3a964b9
BW
1576 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1577 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1578 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1579 * space.
37aca44a 1580 *
f3a964b9 1581 */
5c5f6457 1582static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
37aca44a 1583{
49d73912 1584 struct drm_i915_private *dev_priv = ppgtt->base.i915;
8776f02b 1585 int ret;
7cb6d7ac 1586
8776f02b
MK
1587 ret = gen8_init_scratch(&ppgtt->base);
1588 if (ret)
1589 return ret;
69876bed 1590
d7b2633d 1591 ppgtt->base.start = 0;
d7b2633d 1592 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
5c5f6457 1593 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
d7b2633d 1594 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
c7e16f22 1595 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
777dc5bb
DV
1596 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1597 ppgtt->base.bind_vma = ppgtt_bind_vma;
ea91e401 1598 ppgtt->debug_dump = gen8_dump_ppgtt;
d7b2633d 1599
275a991c
TU
1600 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1601 ret = setup_px(dev_priv, &ppgtt->pml4);
762d9936
MT
1602 if (ret)
1603 goto free_scratch;
6ac18502 1604
69ab76fd
MT
1605 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1606
762d9936 1607 ppgtt->base.total = 1ULL << 48;
2dba3239 1608 ppgtt->switch_mm = gen8_48b_mm_switch;
762d9936 1609 } else {
275a991c 1610 ret = __pdp_init(dev_priv, &ppgtt->pdp);
81ba8aef
MT
1611 if (ret)
1612 goto free_scratch;
1613
1614 ppgtt->base.total = 1ULL << 32;
2dba3239 1615 ppgtt->switch_mm = gen8_legacy_mm_switch;
762d9936
MT
1616 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1617 0, 0,
1618 GEN8_PML4E_SHIFT);
331f38e7 1619
275a991c 1620 if (intel_vgpu_active(dev_priv)) {
331f38e7
ZL
1621 ret = gen8_preallocate_top_level_pdps(ppgtt);
1622 if (ret)
1623 goto free_scratch;
1624 }
81ba8aef 1625 }
6ac18502 1626
275a991c 1627 if (intel_vgpu_active(dev_priv))
650da34c
ZL
1628 gen8_ppgtt_notify_vgt(ppgtt, true);
1629
d7b2633d 1630 return 0;
6ac18502
MT
1631
1632free_scratch:
1633 gen8_free_scratch(&ppgtt->base);
1634 return ret;
d7b2633d
MT
1635}
1636
87d60b63
BW
1637static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1638{
87d60b63 1639 struct i915_address_space *vm = &ppgtt->base;
09942c65 1640 struct i915_page_table *unused;
07749ef3 1641 gen6_pte_t scratch_pte;
87d60b63 1642 uint32_t pd_entry;
731f74c5 1643 uint32_t pte, pde;
09942c65 1644 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
87d60b63 1645
8bcdd0f7 1646 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
4fb84d99 1647 I915_CACHE_LLC, 0);
87d60b63 1648
731f74c5 1649 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
87d60b63 1650 u32 expected;
07749ef3 1651 gen6_pte_t *pt_vaddr;
567047be 1652 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
09942c65 1653 pd_entry = readl(ppgtt->pd_addr + pde);
87d60b63
BW
1654 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1655
1656 if (pd_entry != expected)
1657 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1658 pde,
1659 pd_entry,
1660 expected);
1661 seq_printf(m, "\tPDE: %x\n", pd_entry);
1662
d1c54acd
MK
1663 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1664
07749ef3 1665 for (pte = 0; pte < GEN6_PTES; pte+=4) {
87d60b63 1666 unsigned long va =
07749ef3 1667 (pde * PAGE_SIZE * GEN6_PTES) +
87d60b63
BW
1668 (pte * PAGE_SIZE);
1669 int i;
1670 bool found = false;
1671 for (i = 0; i < 4; i++)
1672 if (pt_vaddr[pte + i] != scratch_pte)
1673 found = true;
1674 if (!found)
1675 continue;
1676
1677 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1678 for (i = 0; i < 4; i++) {
1679 if (pt_vaddr[pte + i] != scratch_pte)
1680 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1681 else
1682 seq_puts(m, " SCRATCH ");
1683 }
1684 seq_puts(m, "\n");
1685 }
d1c54acd 1686 kunmap_px(ppgtt, pt_vaddr);
87d60b63
BW
1687 }
1688}
1689
678d96fb 1690/* Write pde (index) from the page directory @pd to the page table @pt */
ec565b3c
MT
1691static void gen6_write_pde(struct i915_page_directory *pd,
1692 const int pde, struct i915_page_table *pt)
6197349b 1693{
678d96fb
BW
1694 /* Caller needs to make sure the write completes if necessary */
1695 struct i915_hw_ppgtt *ppgtt =
1696 container_of(pd, struct i915_hw_ppgtt, pd);
1697 u32 pd_entry;
6197349b 1698
567047be 1699 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
678d96fb 1700 pd_entry |= GEN6_PDE_VALID;
6197349b 1701
678d96fb
BW
1702 writel(pd_entry, ppgtt->pd_addr + pde);
1703}
6197349b 1704
678d96fb
BW
1705/* Write all the page tables found in the ppgtt structure to incrementing page
1706 * directories. */
1707static void gen6_write_page_range(struct drm_i915_private *dev_priv,
ec565b3c 1708 struct i915_page_directory *pd,
678d96fb
BW
1709 uint32_t start, uint32_t length)
1710{
72e96d64 1711 struct i915_ggtt *ggtt = &dev_priv->ggtt;
ec565b3c 1712 struct i915_page_table *pt;
731f74c5 1713 uint32_t pde;
678d96fb 1714
731f74c5 1715 gen6_for_each_pde(pt, pd, start, length, pde)
678d96fb
BW
1716 gen6_write_pde(pd, pde, pt);
1717
1718 /* Make sure write is complete before other code can use this page
1719 * table. Also require for WC mapped PTEs */
72e96d64 1720 readl(ggtt->gsm);
3e302542
BW
1721}
1722
b4a74e3a 1723static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
3e302542 1724{
44159ddb 1725 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
b4a74e3a 1726
44159ddb 1727 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
b4a74e3a
BW
1728}
1729
90252e5c 1730static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
e85b26dc 1731 struct drm_i915_gem_request *req)
90252e5c 1732{
7e37f889 1733 struct intel_ring *ring = req->ring;
4a570db5 1734 struct intel_engine_cs *engine = req->engine;
90252e5c
BW
1735 int ret;
1736
90252e5c 1737 /* NB: TLBs must be flushed and invalidated before a switch */
7c9cf4e3 1738 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
90252e5c
BW
1739 if (ret)
1740 return ret;
1741
5fb9de1a 1742 ret = intel_ring_begin(req, 6);
90252e5c
BW
1743 if (ret)
1744 return ret;
1745
b5321f30
CW
1746 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1747 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1748 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1749 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1750 intel_ring_emit(ring, get_pd_offset(ppgtt));
1751 intel_ring_emit(ring, MI_NOOP);
1752 intel_ring_advance(ring);
90252e5c
BW
1753
1754 return 0;
1755}
1756
48a10389 1757static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
e85b26dc 1758 struct drm_i915_gem_request *req)
48a10389 1759{
7e37f889 1760 struct intel_ring *ring = req->ring;
4a570db5 1761 struct intel_engine_cs *engine = req->engine;
48a10389
BW
1762 int ret;
1763
48a10389 1764 /* NB: TLBs must be flushed and invalidated before a switch */
7c9cf4e3 1765 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
48a10389
BW
1766 if (ret)
1767 return ret;
1768
5fb9de1a 1769 ret = intel_ring_begin(req, 6);
48a10389
BW
1770 if (ret)
1771 return ret;
1772
b5321f30
CW
1773 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1774 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1775 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1776 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1777 intel_ring_emit(ring, get_pd_offset(ppgtt));
1778 intel_ring_emit(ring, MI_NOOP);
1779 intel_ring_advance(ring);
48a10389 1780
90252e5c 1781 /* XXX: RCS is the only one to auto invalidate the TLBs? */
e2f80391 1782 if (engine->id != RCS) {
7c9cf4e3 1783 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
90252e5c
BW
1784 if (ret)
1785 return ret;
1786 }
1787
48a10389
BW
1788 return 0;
1789}
1790
eeb9488e 1791static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
e85b26dc 1792 struct drm_i915_gem_request *req)
eeb9488e 1793{
4a570db5 1794 struct intel_engine_cs *engine = req->engine;
8eb95204 1795 struct drm_i915_private *dev_priv = req->i915;
48a10389 1796
e2f80391
TU
1797 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1798 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
eeb9488e
BW
1799 return 0;
1800}
1801
c6be607a 1802static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
eeb9488e 1803{
e2f80391 1804 struct intel_engine_cs *engine;
3b3f1650 1805 enum intel_engine_id id;
3e302542 1806
3b3f1650 1807 for_each_engine(engine, dev_priv, id) {
c6be607a
TU
1808 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1809 GEN8_GFX_PPGTT_48B : 0;
e2f80391 1810 I915_WRITE(RING_MODE_GEN7(engine),
2dba3239 1811 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
eeb9488e 1812 }
eeb9488e 1813}
6197349b 1814
c6be607a 1815static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
3e302542 1816{
e2f80391 1817 struct intel_engine_cs *engine;
b4a74e3a 1818 uint32_t ecochk, ecobits;
3b3f1650 1819 enum intel_engine_id id;
6197349b 1820
b4a74e3a
BW
1821 ecobits = I915_READ(GAC_ECO_BITS);
1822 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
a65c2fcd 1823
b4a74e3a 1824 ecochk = I915_READ(GAM_ECOCHK);
772c2a51 1825 if (IS_HASWELL(dev_priv)) {
b4a74e3a
BW
1826 ecochk |= ECOCHK_PPGTT_WB_HSW;
1827 } else {
1828 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1829 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1830 }
1831 I915_WRITE(GAM_ECOCHK, ecochk);
a65c2fcd 1832
3b3f1650 1833 for_each_engine(engine, dev_priv, id) {
6197349b 1834 /* GFX_MODE is per-ring on gen7+ */
e2f80391 1835 I915_WRITE(RING_MODE_GEN7(engine),
b4a74e3a 1836 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
6197349b 1837 }
b4a74e3a 1838}
6197349b 1839
c6be607a 1840static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
b4a74e3a 1841{
b4a74e3a 1842 uint32_t ecochk, gab_ctl, ecobits;
a65c2fcd 1843
b4a74e3a
BW
1844 ecobits = I915_READ(GAC_ECO_BITS);
1845 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1846 ECOBITS_PPGTT_CACHE64B);
6197349b 1847
b4a74e3a
BW
1848 gab_ctl = I915_READ(GAB_CTL);
1849 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1850
1851 ecochk = I915_READ(GAM_ECOCHK);
1852 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1853
1854 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
6197349b
BW
1855}
1856
1d2a314c 1857/* PPGTT support for Sandybdrige/Gen6 and later */
853ba5d2 1858static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
782f1495 1859 uint64_t start,
4fb84d99 1860 uint64_t length)
1d2a314c 1861{
e5716f55 1862 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
07749ef3 1863 gen6_pte_t *pt_vaddr, scratch_pte;
782f1495
BW
1864 unsigned first_entry = start >> PAGE_SHIFT;
1865 unsigned num_entries = length >> PAGE_SHIFT;
07749ef3
MT
1866 unsigned act_pt = first_entry / GEN6_PTES;
1867 unsigned first_pte = first_entry % GEN6_PTES;
7bddb01f 1868 unsigned last_pte, i;
1d2a314c 1869
8bcdd0f7 1870 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
4fb84d99 1871 I915_CACHE_LLC, 0);
1d2a314c 1872
7bddb01f
DV
1873 while (num_entries) {
1874 last_pte = first_pte + num_entries;
07749ef3
MT
1875 if (last_pte > GEN6_PTES)
1876 last_pte = GEN6_PTES;
7bddb01f 1877
d1c54acd 1878 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1d2a314c 1879
7bddb01f
DV
1880 for (i = first_pte; i < last_pte; i++)
1881 pt_vaddr[i] = scratch_pte;
1d2a314c 1882
d1c54acd 1883 kunmap_px(ppgtt, pt_vaddr);
1d2a314c 1884
7bddb01f
DV
1885 num_entries -= last_pte - first_pte;
1886 first_pte = 0;
a15326a5 1887 act_pt++;
7bddb01f 1888 }
1d2a314c
DV
1889}
1890
853ba5d2 1891static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
def886c3 1892 struct sg_table *pages,
782f1495 1893 uint64_t start,
24f3a8cf 1894 enum i915_cache_level cache_level, u32 flags)
def886c3 1895{
e5716f55 1896 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
782f1495 1897 unsigned first_entry = start >> PAGE_SHIFT;
07749ef3
MT
1898 unsigned act_pt = first_entry / GEN6_PTES;
1899 unsigned act_pte = first_entry % GEN6_PTES;
85d1225e
DG
1900 gen6_pte_t *pt_vaddr = NULL;
1901 struct sgt_iter sgt_iter;
1902 dma_addr_t addr;
6e995e23 1903
85d1225e 1904 for_each_sgt_dma(addr, sgt_iter, pages) {
cc79714f 1905 if (pt_vaddr == NULL)
d1c54acd 1906 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
6e995e23 1907
cc79714f 1908 pt_vaddr[act_pte] =
4fb84d99 1909 vm->pte_encode(addr, cache_level, flags);
24f3a8cf 1910
07749ef3 1911 if (++act_pte == GEN6_PTES) {
d1c54acd 1912 kunmap_px(ppgtt, pt_vaddr);
cc79714f 1913 pt_vaddr = NULL;
a15326a5 1914 act_pt++;
6e995e23 1915 act_pte = 0;
def886c3 1916 }
def886c3 1917 }
85d1225e 1918
cc79714f 1919 if (pt_vaddr)
d1c54acd 1920 kunmap_px(ppgtt, pt_vaddr);
def886c3
DV
1921}
1922
678d96fb 1923static int gen6_alloc_va_range(struct i915_address_space *vm,
a05d80ee 1924 uint64_t start_in, uint64_t length_in)
678d96fb 1925{
4933d519 1926 DECLARE_BITMAP(new_page_tables, I915_PDES);
49d73912 1927 struct drm_i915_private *dev_priv = vm->i915;
72e96d64 1928 struct i915_ggtt *ggtt = &dev_priv->ggtt;
e5716f55 1929 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
ec565b3c 1930 struct i915_page_table *pt;
a05d80ee 1931 uint32_t start, length, start_save, length_save;
731f74c5 1932 uint32_t pde;
4933d519
MT
1933 int ret;
1934
a05d80ee
MK
1935 start = start_save = start_in;
1936 length = length_save = length_in;
4933d519
MT
1937
1938 bitmap_zero(new_page_tables, I915_PDES);
1939
1940 /* The allocation is done in two stages so that we can bail out with
1941 * minimal amount of pain. The first stage finds new page tables that
1942 * need allocation. The second stage marks use ptes within the page
1943 * tables.
1944 */
731f74c5 1945 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
79ab9370 1946 if (pt != vm->scratch_pt) {
4933d519
MT
1947 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1948 continue;
1949 }
1950
1951 /* We've already allocated a page table */
1952 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1953
275a991c 1954 pt = alloc_pt(dev_priv);
4933d519
MT
1955 if (IS_ERR(pt)) {
1956 ret = PTR_ERR(pt);
1957 goto unwind_out;
1958 }
1959
1960 gen6_initialize_pt(vm, pt);
1961
1962 ppgtt->pd.page_table[pde] = pt;
966082c9 1963 __set_bit(pde, new_page_tables);
72744cb1 1964 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
4933d519
MT
1965 }
1966
1967 start = start_save;
1968 length = length_save;
678d96fb 1969
731f74c5 1970 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
678d96fb
BW
1971 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1972
1973 bitmap_zero(tmp_bitmap, GEN6_PTES);
1974 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1975 gen6_pte_count(start, length));
1976
966082c9 1977 if (__test_and_clear_bit(pde, new_page_tables))
4933d519
MT
1978 gen6_write_pde(&ppgtt->pd, pde, pt);
1979
72744cb1
MT
1980 trace_i915_page_table_entry_map(vm, pde, pt,
1981 gen6_pte_index(start),
1982 gen6_pte_count(start, length),
1983 GEN6_PTES);
4933d519 1984 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
678d96fb
BW
1985 GEN6_PTES);
1986 }
1987
4933d519
MT
1988 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1989
1990 /* Make sure write is complete before other code can use this page
1991 * table. Also require for WC mapped PTEs */
72e96d64 1992 readl(ggtt->gsm);
4933d519 1993
563222a7 1994 mark_tlbs_dirty(ppgtt);
678d96fb 1995 return 0;
4933d519
MT
1996
1997unwind_out:
1998 for_each_set_bit(pde, new_page_tables, I915_PDES) {
ec565b3c 1999 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
4933d519 2000
79ab9370 2001 ppgtt->pd.page_table[pde] = vm->scratch_pt;
275a991c 2002 free_pt(dev_priv, pt);
4933d519
MT
2003 }
2004
2005 mark_tlbs_dirty(ppgtt);
2006 return ret;
678d96fb
BW
2007}
2008
8776f02b
MK
2009static int gen6_init_scratch(struct i915_address_space *vm)
2010{
49d73912 2011 struct drm_i915_private *dev_priv = vm->i915;
8bcdd0f7 2012 int ret;
8776f02b 2013
275a991c 2014 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
8bcdd0f7
CW
2015 if (ret)
2016 return ret;
8776f02b 2017
275a991c 2018 vm->scratch_pt = alloc_pt(dev_priv);
8776f02b 2019 if (IS_ERR(vm->scratch_pt)) {
275a991c 2020 cleanup_scratch_page(dev_priv, &vm->scratch_page);
8776f02b
MK
2021 return PTR_ERR(vm->scratch_pt);
2022 }
2023
2024 gen6_initialize_pt(vm, vm->scratch_pt);
2025
2026 return 0;
2027}
2028
2029static void gen6_free_scratch(struct i915_address_space *vm)
2030{
49d73912 2031 struct drm_i915_private *dev_priv = vm->i915;
8776f02b 2032
275a991c
TU
2033 free_pt(dev_priv, vm->scratch_pt);
2034 cleanup_scratch_page(dev_priv, &vm->scratch_page);
8776f02b
MK
2035}
2036
061dd493 2037static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
a00d825d 2038{
e5716f55 2039 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
731f74c5 2040 struct i915_page_directory *pd = &ppgtt->pd;
49d73912 2041 struct drm_i915_private *dev_priv = vm->i915;
09942c65
MT
2042 struct i915_page_table *pt;
2043 uint32_t pde;
4933d519 2044
061dd493
DV
2045 drm_mm_remove_node(&ppgtt->node);
2046
731f74c5 2047 gen6_for_all_pdes(pt, pd, pde)
79ab9370 2048 if (pt != vm->scratch_pt)
275a991c 2049 free_pt(dev_priv, pt);
06fda602 2050
8776f02b 2051 gen6_free_scratch(vm);
3440d265
DV
2052}
2053
b146520f 2054static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
3440d265 2055{
8776f02b 2056 struct i915_address_space *vm = &ppgtt->base;
49d73912 2057 struct drm_i915_private *dev_priv = ppgtt->base.i915;
72e96d64 2058 struct i915_ggtt *ggtt = &dev_priv->ggtt;
b146520f 2059 int ret;
1d2a314c 2060
c8d4c0d6
BW
2061 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2062 * allocator works in address space sizes, so it's multiplied by page
2063 * size. We allocate at the top of the GTT to avoid fragmentation.
2064 */
72e96d64 2065 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
4933d519 2066
8776f02b
MK
2067 ret = gen6_init_scratch(vm);
2068 if (ret)
2069 return ret;
4933d519 2070
e007b19d
CW
2071 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2072 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2073 I915_COLOR_UNEVICTABLE,
2074 0, ggtt->base.total,
2075 PIN_HIGH);
c8c26622 2076 if (ret)
678d96fb
BW
2077 goto err_out;
2078
72e96d64 2079 if (ppgtt->node.start < ggtt->mappable_end)
c8d4c0d6 2080 DRM_DEBUG("Forced to use aperture for PDEs\n");
1d2a314c 2081
c8c26622 2082 return 0;
678d96fb
BW
2083
2084err_out:
8776f02b 2085 gen6_free_scratch(vm);
678d96fb 2086 return ret;
b146520f
BW
2087}
2088
b146520f
BW
2089static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2090{
2f2cf682 2091 return gen6_ppgtt_allocate_page_directories(ppgtt);
4933d519 2092}
06dc68d6 2093
4933d519
MT
2094static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2095 uint64_t start, uint64_t length)
2096{
ec565b3c 2097 struct i915_page_table *unused;
731f74c5 2098 uint32_t pde;
1d2a314c 2099
731f74c5 2100 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
79ab9370 2101 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
b146520f
BW
2102}
2103
5c5f6457 2104static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
b146520f 2105{
49d73912 2106 struct drm_i915_private *dev_priv = ppgtt->base.i915;
72e96d64 2107 struct i915_ggtt *ggtt = &dev_priv->ggtt;
b146520f
BW
2108 int ret;
2109
72e96d64 2110 ppgtt->base.pte_encode = ggtt->base.pte_encode;
5db94019 2111 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
b146520f 2112 ppgtt->switch_mm = gen6_mm_switch;
772c2a51 2113 else if (IS_HASWELL(dev_priv))
b146520f 2114 ppgtt->switch_mm = hsw_mm_switch;
5db94019 2115 else if (IS_GEN7(dev_priv))
b146520f 2116 ppgtt->switch_mm = gen7_mm_switch;
8eb95204 2117 else
b146520f
BW
2118 BUG();
2119
2120 ret = gen6_ppgtt_alloc(ppgtt);
2121 if (ret)
2122 return ret;
2123
5c5f6457 2124 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
b146520f
BW
2125 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2126 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
777dc5bb
DV
2127 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2128 ppgtt->base.bind_vma = ppgtt_bind_vma;
b146520f 2129 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
b146520f 2130 ppgtt->base.start = 0;
09942c65 2131 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
87d60b63 2132 ppgtt->debug_dump = gen6_dump_ppgtt;
1d2a314c 2133
44159ddb 2134 ppgtt->pd.base.ggtt_offset =
07749ef3 2135 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1d2a314c 2136
72e96d64 2137 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
44159ddb 2138 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
678d96fb 2139
5c5f6457 2140 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
1d2a314c 2141
678d96fb
BW
2142 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2143
440fd528 2144 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
b146520f
BW
2145 ppgtt->node.size >> 20,
2146 ppgtt->node.start / PAGE_SIZE);
3440d265 2147
fa76da34 2148 DRM_DEBUG("Adding PPGTT at offset %x\n",
44159ddb 2149 ppgtt->pd.base.ggtt_offset << 10);
fa76da34 2150
b146520f 2151 return 0;
3440d265
DV
2152}
2153
2bfa996e
CW
2154static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2155 struct drm_i915_private *dev_priv)
3440d265 2156{
49d73912 2157 ppgtt->base.i915 = dev_priv;
3440d265 2158
2bfa996e 2159 if (INTEL_INFO(dev_priv)->gen < 8)
5c5f6457 2160 return gen6_ppgtt_init(ppgtt);
3ed124b2 2161 else
d7b2633d 2162 return gen8_ppgtt_init(ppgtt);
fa76da34 2163}
c114f76a 2164
a2cad9df 2165static void i915_address_space_init(struct i915_address_space *vm,
80b204bc
CW
2166 struct drm_i915_private *dev_priv,
2167 const char *name)
a2cad9df 2168{
80b204bc 2169 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
47db922f 2170
a2cad9df 2171 drm_mm_init(&vm->mm, vm->start, vm->total);
47db922f
CW
2172 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2173
a2cad9df
MW
2174 INIT_LIST_HEAD(&vm->active_list);
2175 INIT_LIST_HEAD(&vm->inactive_list);
50e046b6 2176 INIT_LIST_HEAD(&vm->unbound_list);
47db922f 2177
a2cad9df
MW
2178 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2179}
2180
ed9724dd
MA
2181static void i915_address_space_fini(struct i915_address_space *vm)
2182{
2183 i915_gem_timeline_fini(&vm->timeline);
2184 drm_mm_takedown(&vm->mm);
2185 list_del(&vm->global_link);
2186}
2187
c6be607a 2188static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
d5165ebd 2189{
d5165ebd
TG
2190 /* This function is for gtt related workarounds. This function is
2191 * called on driver load and after a GPU reset, so you can place
2192 * workarounds here even if they get overwritten by GPU reset.
2193 */
9fb5026f 2194 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
8652744b 2195 if (IS_BROADWELL(dev_priv))
d5165ebd 2196 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
920a14b2 2197 else if (IS_CHERRYVIEW(dev_priv))
d5165ebd 2198 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
b976dc53 2199 else if (IS_GEN9_BC(dev_priv))
d5165ebd 2200 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
9fb5026f 2201 else if (IS_GEN9_LP(dev_priv))
d5165ebd
TG
2202 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2203}
2204
2bfa996e
CW
2205static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2206 struct drm_i915_private *dev_priv,
80b204bc
CW
2207 struct drm_i915_file_private *file_priv,
2208 const char *name)
fa76da34 2209{
2bfa996e 2210 int ret;
3ed124b2 2211
2bfa996e 2212 ret = __hw_ppgtt_init(ppgtt, dev_priv);
fa76da34 2213 if (ret == 0) {
c7c48dfd 2214 kref_init(&ppgtt->ref);
80b204bc 2215 i915_address_space_init(&ppgtt->base, dev_priv, name);
2bfa996e 2216 ppgtt->base.file = file_priv;
93bd8649 2217 }
1d2a314c
DV
2218
2219 return ret;
2220}
2221
c6be607a 2222int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
82460d97 2223{
c6be607a 2224 gtt_write_workarounds(dev_priv);
d5165ebd 2225
671b5013
TD
2226 /* In the case of execlists, PPGTT is enabled by the context descriptor
2227 * and the PDPs are contained within the context itself. We don't
2228 * need to do anything here. */
2229 if (i915.enable_execlists)
2230 return 0;
2231
c6be607a 2232 if (!USES_PPGTT(dev_priv))
82460d97
DV
2233 return 0;
2234
5db94019 2235 if (IS_GEN6(dev_priv))
c6be607a 2236 gen6_ppgtt_enable(dev_priv);
5db94019 2237 else if (IS_GEN7(dev_priv))
c6be607a
TU
2238 gen7_ppgtt_enable(dev_priv);
2239 else if (INTEL_GEN(dev_priv) >= 8)
2240 gen8_ppgtt_enable(dev_priv);
82460d97 2241 else
c6be607a 2242 MISSING_CASE(INTEL_GEN(dev_priv));
82460d97 2243
4ad2fd88
JH
2244 return 0;
2245}
1d2a314c 2246
4d884705 2247struct i915_hw_ppgtt *
2bfa996e 2248i915_ppgtt_create(struct drm_i915_private *dev_priv,
80b204bc
CW
2249 struct drm_i915_file_private *fpriv,
2250 const char *name)
4d884705
DV
2251{
2252 struct i915_hw_ppgtt *ppgtt;
2253 int ret;
2254
2255 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2256 if (!ppgtt)
2257 return ERR_PTR(-ENOMEM);
2258
80b204bc 2259 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
4d884705
DV
2260 if (ret) {
2261 kfree(ppgtt);
2262 return ERR_PTR(ret);
2263 }
2264
198c974d
DCS
2265 trace_i915_ppgtt_create(&ppgtt->base);
2266
4d884705
DV
2267 return ppgtt;
2268}
2269
0c7eeda1
CW
2270void i915_ppgtt_close(struct i915_address_space *vm)
2271{
2272 struct list_head *phases[] = {
2273 &vm->active_list,
2274 &vm->inactive_list,
2275 &vm->unbound_list,
2276 NULL,
2277 }, **phase;
2278
2279 GEM_BUG_ON(vm->closed);
2280 vm->closed = true;
2281
2282 for (phase = phases; *phase; phase++) {
2283 struct i915_vma *vma, *vn;
2284
2285 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2286 if (!i915_vma_is_closed(vma))
2287 i915_vma_close(vma);
2288 }
2289}
2290
ed9724dd 2291void i915_ppgtt_release(struct kref *kref)
ee960be7
DV
2292{
2293 struct i915_hw_ppgtt *ppgtt =
2294 container_of(kref, struct i915_hw_ppgtt, ref);
2295
198c974d
DCS
2296 trace_i915_ppgtt_release(&ppgtt->base);
2297
50e046b6 2298 /* vmas should already be unbound and destroyed */
ee960be7
DV
2299 WARN_ON(!list_empty(&ppgtt->base.active_list));
2300 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
50e046b6 2301 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
ee960be7 2302
ed9724dd 2303 i915_address_space_fini(&ppgtt->base);
19dd120c 2304
ee960be7
DV
2305 ppgtt->base.cleanup(&ppgtt->base);
2306 kfree(ppgtt);
2307}
1d2a314c 2308
a81cc00c
BW
2309/* Certain Gen5 chipsets require require idling the GPU before
2310 * unmapping anything from the GTT when VT-d is enabled.
2311 */
97d6d7ab 2312static bool needs_idle_maps(struct drm_i915_private *dev_priv)
a81cc00c
BW
2313{
2314#ifdef CONFIG_INTEL_IOMMU
2315 /* Query intel_iommu to see if we need the workaround. Presumably that
2316 * was loaded first.
2317 */
97d6d7ab 2318 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
a81cc00c
BW
2319 return true;
2320#endif
2321 return false;
2322}
2323
dc97997a 2324void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
828c7908 2325{
e2f80391 2326 struct intel_engine_cs *engine;
3b3f1650 2327 enum intel_engine_id id;
828c7908 2328
dc97997a 2329 if (INTEL_INFO(dev_priv)->gen < 6)
828c7908
BW
2330 return;
2331
3b3f1650 2332 for_each_engine(engine, dev_priv, id) {
828c7908 2333 u32 fault_reg;
e2f80391 2334 fault_reg = I915_READ(RING_FAULT_REG(engine));
828c7908
BW
2335 if (fault_reg & RING_FAULT_VALID) {
2336 DRM_DEBUG_DRIVER("Unexpected fault\n"
59a5d290 2337 "\tAddr: 0x%08lx\n"
828c7908
BW
2338 "\tAddress space: %s\n"
2339 "\tSource ID: %d\n"
2340 "\tType: %d\n",
2341 fault_reg & PAGE_MASK,
2342 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2343 RING_FAULT_SRCID(fault_reg),
2344 RING_FAULT_FAULT_TYPE(fault_reg));
e2f80391 2345 I915_WRITE(RING_FAULT_REG(engine),
828c7908
BW
2346 fault_reg & ~RING_FAULT_VALID);
2347 }
2348 }
3b3f1650
AG
2349
2350 /* Engine specific init may not have been done till this point. */
2351 if (dev_priv->engine[RCS])
2352 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
828c7908
BW
2353}
2354
275a991c 2355void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
828c7908 2356{
72e96d64 2357 struct i915_ggtt *ggtt = &dev_priv->ggtt;
828c7908
BW
2358
2359 /* Don't bother messing with faults pre GEN6 as we have little
2360 * documentation supporting that it's a good idea.
2361 */
275a991c 2362 if (INTEL_GEN(dev_priv) < 6)
828c7908
BW
2363 return;
2364
dc97997a 2365 i915_check_and_clear_faults(dev_priv);
828c7908 2366
4fb84d99 2367 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
91e56499 2368
7c3f86b6 2369 i915_ggtt_invalidate(dev_priv);
828c7908
BW
2370}
2371
03ac84f1
CW
2372int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2373 struct sg_table *pages)
7c2e6fdf 2374{
1a292fa5
CW
2375 do {
2376 if (dma_map_sg(&obj->base.dev->pdev->dev,
2377 pages->sgl, pages->nents,
2378 PCI_DMA_BIDIRECTIONAL))
2379 return 0;
2380
2381 /* If the DMA remap fails, one cause can be that we have
2382 * too many objects pinned in a small remapping table,
2383 * such as swiotlb. Incrementally purge all other objects and
2384 * try again - if there are no more pages to remove from
2385 * the DMA remapper, i915_gem_shrink will return 0.
2386 */
2387 GEM_BUG_ON(obj->mm.pages == pages);
2388 } while (i915_gem_shrink(to_i915(obj->base.dev),
2389 obj->base.size >> PAGE_SHIFT,
2390 I915_SHRINK_BOUND |
2391 I915_SHRINK_UNBOUND |
2392 I915_SHRINK_ACTIVE));
9da3da66 2393
03ac84f1 2394 return -ENOSPC;
7c2e6fdf
DV
2395}
2396
2c642b07 2397static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
94ec8f61 2398{
94ec8f61 2399 writeq(pte, addr);
94ec8f61
BW
2400}
2401
d6473f56
CW
2402static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2403 dma_addr_t addr,
2404 uint64_t offset,
2405 enum i915_cache_level level,
2406 u32 unused)
2407{
7c3f86b6 2408 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
d6473f56 2409 gen8_pte_t __iomem *pte =
7c3f86b6 2410 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
d6473f56 2411
4fb84d99 2412 gen8_set_pte(pte, gen8_pte_encode(addr, level));
d6473f56 2413
7c3f86b6 2414 ggtt->invalidate(vm->i915);
d6473f56
CW
2415}
2416
94ec8f61
BW
2417static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2418 struct sg_table *st,
782f1495 2419 uint64_t start,
24f3a8cf 2420 enum i915_cache_level level, u32 unused)
94ec8f61 2421{
ce7fda2e 2422 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
85d1225e
DG
2423 struct sgt_iter sgt_iter;
2424 gen8_pte_t __iomem *gtt_entries;
2425 gen8_pte_t gtt_entry;
2426 dma_addr_t addr;
85d1225e 2427 int i = 0;
be69459a 2428
85d1225e
DG
2429 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2430
2431 for_each_sgt_dma(addr, sgt_iter, st) {
4fb84d99 2432 gtt_entry = gen8_pte_encode(addr, level);
85d1225e 2433 gen8_set_pte(&gtt_entries[i++], gtt_entry);
94ec8f61
BW
2434 }
2435
2436 /*
2437 * XXX: This serves as a posting read to make sure that the PTE has
2438 * actually been updated. There is some concern that even though
2439 * registers and PTEs are within the same BAR that they are potentially
2440 * of NUMA access patterns. Therefore, even with the way we assume
2441 * hardware should work, we must keep this posting read for paranoia.
2442 */
2443 if (i != 0)
85d1225e 2444 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
94ec8f61 2445
94ec8f61
BW
2446 /* This next bit makes the above posting read even more important. We
2447 * want to flush the TLBs only after we're certain all the PTE updates
2448 * have finished.
2449 */
7c3f86b6 2450 ggtt->invalidate(vm->i915);
94ec8f61
BW
2451}
2452
c140330b
CW
2453struct insert_entries {
2454 struct i915_address_space *vm;
2455 struct sg_table *st;
2456 uint64_t start;
2457 enum i915_cache_level level;
2458 u32 flags;
2459};
2460
2461static int gen8_ggtt_insert_entries__cb(void *_arg)
2462{
2463 struct insert_entries *arg = _arg;
2464 gen8_ggtt_insert_entries(arg->vm, arg->st,
2465 arg->start, arg->level, arg->flags);
2466 return 0;
2467}
2468
2469static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2470 struct sg_table *st,
2471 uint64_t start,
2472 enum i915_cache_level level,
2473 u32 flags)
2474{
2475 struct insert_entries arg = { vm, st, start, level, flags };
2476 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2477}
2478
d6473f56
CW
2479static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2480 dma_addr_t addr,
2481 uint64_t offset,
2482 enum i915_cache_level level,
2483 u32 flags)
2484{
7c3f86b6 2485 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
d6473f56 2486 gen6_pte_t __iomem *pte =
7c3f86b6 2487 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
d6473f56 2488
4fb84d99 2489 iowrite32(vm->pte_encode(addr, level, flags), pte);
d6473f56 2490
7c3f86b6 2491 ggtt->invalidate(vm->i915);
d6473f56
CW
2492}
2493
e76e9aeb
BW
2494/*
2495 * Binds an object into the global gtt with the specified cache level. The object
2496 * will be accessible to the GPU via commands whose operands reference offsets
2497 * within the global GTT as well as accessible by the GPU through the GMADR
2498 * mapped BAR (dev_priv->mm.gtt->gtt).
2499 */
853ba5d2 2500static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
7faf1ab2 2501 struct sg_table *st,
782f1495 2502 uint64_t start,
24f3a8cf 2503 enum i915_cache_level level, u32 flags)
e76e9aeb 2504{
ce7fda2e 2505 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
85d1225e
DG
2506 struct sgt_iter sgt_iter;
2507 gen6_pte_t __iomem *gtt_entries;
2508 gen6_pte_t gtt_entry;
2509 dma_addr_t addr;
85d1225e 2510 int i = 0;
be69459a 2511
85d1225e
DG
2512 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2513
2514 for_each_sgt_dma(addr, sgt_iter, st) {
4fb84d99 2515 gtt_entry = vm->pte_encode(addr, level, flags);
85d1225e 2516 iowrite32(gtt_entry, &gtt_entries[i++]);
e76e9aeb
BW
2517 }
2518
e76e9aeb
BW
2519 /* XXX: This serves as a posting read to make sure that the PTE has
2520 * actually been updated. There is some concern that even though
2521 * registers and PTEs are within the same BAR that they are potentially
2522 * of NUMA access patterns. Therefore, even with the way we assume
2523 * hardware should work, we must keep this posting read for paranoia.
2524 */
85d1225e
DG
2525 if (i != 0)
2526 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
0f9b91c7
BW
2527
2528 /* This next bit makes the above posting read even more important. We
2529 * want to flush the TLBs only after we're certain all the PTE updates
2530 * have finished.
2531 */
7c3f86b6 2532 ggtt->invalidate(vm->i915);
e76e9aeb
BW
2533}
2534
f7770bfd 2535static void nop_clear_range(struct i915_address_space *vm,
4fb84d99 2536 uint64_t start, uint64_t length)
f7770bfd
CW
2537{
2538}
2539
94ec8f61 2540static void gen8_ggtt_clear_range(struct i915_address_space *vm,
4fb84d99 2541 uint64_t start, uint64_t length)
94ec8f61 2542{
ce7fda2e 2543 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
782f1495
BW
2544 unsigned first_entry = start >> PAGE_SHIFT;
2545 unsigned num_entries = length >> PAGE_SHIFT;
07749ef3 2546 gen8_pte_t scratch_pte, __iomem *gtt_base =
72e96d64
JL
2547 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2548 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
94ec8f61
BW
2549 int i;
2550
2551 if (WARN(num_entries > max_entries,
2552 "First entry = %d; Num entries = %d (max=%d)\n",
2553 first_entry, num_entries, max_entries))
2554 num_entries = max_entries;
2555
8bcdd0f7 2556 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
4fb84d99 2557 I915_CACHE_LLC);
94ec8f61
BW
2558 for (i = 0; i < num_entries; i++)
2559 gen8_set_pte(&gtt_base[i], scratch_pte);
2560 readl(gtt_base);
2561}
2562
853ba5d2 2563static void gen6_ggtt_clear_range(struct i915_address_space *vm,
782f1495 2564 uint64_t start,
4fb84d99 2565 uint64_t length)
7faf1ab2 2566{
ce7fda2e 2567 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
782f1495
BW
2568 unsigned first_entry = start >> PAGE_SHIFT;
2569 unsigned num_entries = length >> PAGE_SHIFT;
07749ef3 2570 gen6_pte_t scratch_pte, __iomem *gtt_base =
72e96d64
JL
2571 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2572 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
7faf1ab2
DV
2573 int i;
2574
2575 if (WARN(num_entries > max_entries,
2576 "First entry = %d; Num entries = %d (max=%d)\n",
2577 first_entry, num_entries, max_entries))
2578 num_entries = max_entries;
2579
8bcdd0f7 2580 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
4fb84d99 2581 I915_CACHE_LLC, 0);
828c7908 2582
7faf1ab2
DV
2583 for (i = 0; i < num_entries; i++)
2584 iowrite32(scratch_pte, &gtt_base[i]);
2585 readl(gtt_base);
2586}
2587
d6473f56
CW
2588static void i915_ggtt_insert_page(struct i915_address_space *vm,
2589 dma_addr_t addr,
2590 uint64_t offset,
2591 enum i915_cache_level cache_level,
2592 u32 unused)
2593{
d6473f56
CW
2594 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2595 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
d6473f56
CW
2596
2597 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
d6473f56
CW
2598}
2599
d369d2d9
DV
2600static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2601 struct sg_table *pages,
2602 uint64_t start,
2603 enum i915_cache_level cache_level, u32 unused)
7faf1ab2
DV
2604{
2605 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2606 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2607
d369d2d9 2608 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
0875546c 2609
7faf1ab2
DV
2610}
2611
853ba5d2 2612static void i915_ggtt_clear_range(struct i915_address_space *vm,
782f1495 2613 uint64_t start,
4fb84d99 2614 uint64_t length)
7faf1ab2 2615{
2eedfc7d 2616 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
7faf1ab2
DV
2617}
2618
70b9f6f8
DV
2619static int ggtt_bind_vma(struct i915_vma *vma,
2620 enum i915_cache_level cache_level,
2621 u32 flags)
0a878716 2622{
49d73912 2623 struct drm_i915_private *i915 = vma->vm->i915;
0a878716
DV
2624 struct drm_i915_gem_object *obj = vma->obj;
2625 u32 pte_flags = 0;
2626 int ret;
2627
2628 ret = i915_get_ggtt_vma_pages(vma);
2629 if (ret)
2630 return ret;
2631
2632 /* Currently applicable only to VLV */
2633 if (obj->gt_ro)
2634 pte_flags |= PTE_READ_ONLY;
2635
9c870d03 2636 intel_runtime_pm_get(i915);
247177dd 2637 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
0a878716 2638 cache_level, pte_flags);
9c870d03 2639 intel_runtime_pm_put(i915);
0a878716
DV
2640
2641 /*
2642 * Without aliasing PPGTT there's no difference between
2643 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2644 * upgrade to both bound if we bind either to avoid double-binding.
2645 */
3272db53 2646 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
0a878716
DV
2647
2648 return 0;
2649}
2650
2651static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2652 enum i915_cache_level cache_level,
2653 u32 flags)
d5bd1449 2654{
49d73912 2655 struct drm_i915_private *i915 = vma->vm->i915;
321d178e 2656 u32 pte_flags;
70b9f6f8
DV
2657 int ret;
2658
2659 ret = i915_get_ggtt_vma_pages(vma);
2660 if (ret)
2661 return ret;
7faf1ab2 2662
24f3a8cf 2663 /* Currently applicable only to VLV */
321d178e
CW
2664 pte_flags = 0;
2665 if (vma->obj->gt_ro)
f329f5f6 2666 pte_flags |= PTE_READ_ONLY;
24f3a8cf 2667
ec7adb6e 2668
3272db53 2669 if (flags & I915_VMA_GLOBAL_BIND) {
9c870d03 2670 intel_runtime_pm_get(i915);
321d178e 2671 vma->vm->insert_entries(vma->vm,
247177dd 2672 vma->pages, vma->node.start,
0875546c 2673 cache_level, pte_flags);
9c870d03 2674 intel_runtime_pm_put(i915);
6f65e29a 2675 }
d5bd1449 2676
3272db53 2677 if (flags & I915_VMA_LOCAL_BIND) {
9c870d03 2678 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
321d178e 2679 appgtt->base.insert_entries(&appgtt->base,
247177dd 2680 vma->pages, vma->node.start,
f329f5f6 2681 cache_level, pte_flags);
6f65e29a 2682 }
70b9f6f8
DV
2683
2684 return 0;
d5bd1449
CW
2685}
2686
6f65e29a 2687static void ggtt_unbind_vma(struct i915_vma *vma)
74163907 2688{
49d73912 2689 struct drm_i915_private *i915 = vma->vm->i915;
9c870d03 2690 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
de180033 2691 const u64 size = min(vma->size, vma->node.size);
6f65e29a 2692
9c870d03
CW
2693 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2694 intel_runtime_pm_get(i915);
782f1495 2695 vma->vm->clear_range(vma->vm,
4fb84d99 2696 vma->node.start, size);
9c870d03
CW
2697 intel_runtime_pm_put(i915);
2698 }
06615ee5 2699
3272db53 2700 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
6f65e29a 2701 appgtt->base.clear_range(&appgtt->base,
4fb84d99 2702 vma->node.start, size);
74163907
DV
2703}
2704
03ac84f1
CW
2705void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2706 struct sg_table *pages)
7c2e6fdf 2707{
52a05c30
DW
2708 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2709 struct device *kdev = &dev_priv->drm.pdev->dev;
307dc25b 2710 struct i915_ggtt *ggtt = &dev_priv->ggtt;
5c042287 2711
307dc25b 2712 if (unlikely(ggtt->do_idle_maps)) {
22dd3bb9 2713 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
307dc25b
CW
2714 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2715 /* Wait a bit, in hopes it avoids the hang */
2716 udelay(10);
2717 }
2718 }
5c042287 2719
03ac84f1 2720 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
7c2e6fdf 2721}
644ec02b 2722
45b186f1 2723static void i915_gtt_color_adjust(const struct drm_mm_node *node,
42d6ab48 2724 unsigned long color,
440fd528
TR
2725 u64 *start,
2726 u64 *end)
42d6ab48 2727{
a6508ded 2728 if (node->allocated && node->color != color)
f51455d4 2729 *start += I915_GTT_PAGE_SIZE;
42d6ab48 2730
a6508ded
CW
2731 /* Also leave a space between the unallocated reserved node after the
2732 * GTT and any objects within the GTT, i.e. we use the color adjustment
2733 * to insert a guard page to prevent prefetches crossing over the
2734 * GTT boundary.
2735 */
b44f97fd 2736 node = list_next_entry(node, node_list);
a6508ded 2737 if (node->color != color)
f51455d4 2738 *end -= I915_GTT_PAGE_SIZE;
42d6ab48 2739}
fbe5d36e 2740
6cde9a02
CW
2741int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2742{
2743 struct i915_ggtt *ggtt = &i915->ggtt;
2744 struct i915_hw_ppgtt *ppgtt;
2745 int err;
2746
2747 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2748 if (!ppgtt)
2749 return -ENOMEM;
2750
2751 err = __hw_ppgtt_init(ppgtt, i915);
2752 if (err)
2753 goto err_ppgtt;
2754
2755 if (ppgtt->base.allocate_va_range) {
2756 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2757 0, ppgtt->base.total);
2758 if (err)
2759 goto err_ppgtt_cleanup;
2760 }
2761
2762 ppgtt->base.clear_range(&ppgtt->base,
2763 ppgtt->base.start,
2764 ppgtt->base.total);
2765
2766 i915->mm.aliasing_ppgtt = ppgtt;
2767 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2768 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2769
2770 return 0;
2771
2772err_ppgtt_cleanup:
2773 ppgtt->base.cleanup(&ppgtt->base);
2774err_ppgtt:
2775 kfree(ppgtt);
2776 return err;
2777}
2778
2779void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2780{
2781 struct i915_ggtt *ggtt = &i915->ggtt;
2782 struct i915_hw_ppgtt *ppgtt;
2783
2784 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2785 if (!ppgtt)
2786 return;
2787
2788 ppgtt->base.cleanup(&ppgtt->base);
2789 kfree(ppgtt);
2790
2791 ggtt->base.bind_vma = ggtt_bind_vma;
2792}
2793
f6b9d5ca 2794int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
644ec02b 2795{
e78891ca
BW
2796 /* Let GEM Manage all of the aperture.
2797 *
2798 * However, leave one page at the end still bound to the scratch page.
2799 * There are a number of places where the hardware apparently prefetches
2800 * past the end of the object, and we've seen multiple hangs with the
2801 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2802 * aperture. One page should be enough to keep any prefetching inside
2803 * of the aperture.
2804 */
72e96d64 2805 struct i915_ggtt *ggtt = &dev_priv->ggtt;
ed2f3452 2806 unsigned long hole_start, hole_end;
f6b9d5ca 2807 struct drm_mm_node *entry;
fa76da34 2808 int ret;
644ec02b 2809
b02d22a3
ZW
2810 ret = intel_vgt_balloon(dev_priv);
2811 if (ret)
2812 return ret;
5dda8fa3 2813
95374d75 2814 /* Reserve a mappable slot for our lockless error capture */
4e64e553
CW
2815 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2816 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2817 0, ggtt->mappable_end,
2818 DRM_MM_INSERT_LOW);
95374d75
CW
2819 if (ret)
2820 return ret;
2821
ed2f3452 2822 /* Clear any non-preallocated blocks */
72e96d64 2823 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
ed2f3452
CW
2824 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2825 hole_start, hole_end);
72e96d64 2826 ggtt->base.clear_range(&ggtt->base, hole_start,
4fb84d99 2827 hole_end - hole_start);
ed2f3452
CW
2828 }
2829
2830 /* And finally clear the reserved guard page */
f6b9d5ca 2831 ggtt->base.clear_range(&ggtt->base,
4fb84d99 2832 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
6c5566a8 2833
97d6d7ab 2834 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
6cde9a02 2835 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
95374d75 2836 if (ret)
6cde9a02 2837 goto err;
fa76da34
DV
2838 }
2839
6c5566a8 2840 return 0;
95374d75 2841
95374d75
CW
2842err:
2843 drm_mm_remove_node(&ggtt->error_capture);
2844 return ret;
e76e9aeb
BW
2845}
2846
d85489d3
JL
2847/**
2848 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
97d6d7ab 2849 * @dev_priv: i915 device
d85489d3 2850 */
97d6d7ab 2851void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
90d0a0e8 2852{
72e96d64 2853 struct i915_ggtt *ggtt = &dev_priv->ggtt;
94d4a2a9
CW
2854 struct i915_vma *vma, *vn;
2855
2856 ggtt->base.closed = true;
2857
2858 mutex_lock(&dev_priv->drm.struct_mutex);
2859 WARN_ON(!list_empty(&ggtt->base.active_list));
2860 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2861 WARN_ON(i915_vma_unbind(vma));
2862 mutex_unlock(&dev_priv->drm.struct_mutex);
90d0a0e8 2863
6cde9a02 2864 i915_gem_fini_aliasing_ppgtt(dev_priv);
97d6d7ab 2865 i915_gem_cleanup_stolen(&dev_priv->drm);
a4eba47b 2866
95374d75
CW
2867 if (drm_mm_node_allocated(&ggtt->error_capture))
2868 drm_mm_remove_node(&ggtt->error_capture);
2869
72e96d64 2870 if (drm_mm_initialized(&ggtt->base.mm)) {
b02d22a3 2871 intel_vgt_deballoon(dev_priv);
5dda8fa3 2872
ed9724dd
MA
2873 mutex_lock(&dev_priv->drm.struct_mutex);
2874 i915_address_space_fini(&ggtt->base);
2875 mutex_unlock(&dev_priv->drm.struct_mutex);
90d0a0e8
DV
2876 }
2877
72e96d64 2878 ggtt->base.cleanup(&ggtt->base);
f6b9d5ca
CW
2879
2880 arch_phys_wc_del(ggtt->mtrr);
f7bbe788 2881 io_mapping_fini(&ggtt->mappable);
90d0a0e8 2882}
70e32544 2883
2c642b07 2884static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
e76e9aeb
BW
2885{
2886 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2887 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2888 return snb_gmch_ctl << 20;
2889}
2890
2c642b07 2891static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
9459d252
BW
2892{
2893 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2894 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2895 if (bdw_gmch_ctl)
2896 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
562d55d9
BW
2897
2898#ifdef CONFIG_X86_32
2899 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2900 if (bdw_gmch_ctl > 4)
2901 bdw_gmch_ctl = 4;
2902#endif
2903
9459d252
BW
2904 return bdw_gmch_ctl << 20;
2905}
2906
2c642b07 2907static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
d7f25f23
DL
2908{
2909 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2910 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2911
2912 if (gmch_ctrl)
2913 return 1 << (20 + gmch_ctrl);
2914
2915 return 0;
2916}
2917
2c642b07 2918static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
e76e9aeb
BW
2919{
2920 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2921 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2922 return snb_gmch_ctl << 25; /* 32 MB units */
2923}
2924
2c642b07 2925static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
9459d252
BW
2926{
2927 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2928 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2929 return bdw_gmch_ctl << 25; /* 32 MB units */
2930}
2931
d7f25f23
DL
2932static size_t chv_get_stolen_size(u16 gmch_ctrl)
2933{
2934 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2935 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2936
2937 /*
2938 * 0x0 to 0x10: 32MB increments starting at 0MB
2939 * 0x11 to 0x16: 4MB increments starting at 8MB
2940 * 0x17 to 0x1d: 4MB increments start at 36MB
2941 */
2942 if (gmch_ctrl < 0x11)
2943 return gmch_ctrl << 25;
2944 else if (gmch_ctrl < 0x17)
2945 return (gmch_ctrl - 0x11 + 2) << 22;
2946 else
2947 return (gmch_ctrl - 0x17 + 9) << 22;
2948}
2949
66375014
DL
2950static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2951{
2952 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2953 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2954
2955 if (gen9_gmch_ctl < 0xf0)
2956 return gen9_gmch_ctl << 25; /* 32 MB units */
2957 else
2958 /* 4MB increments starting at 0xf0 for 4MB */
2959 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2960}
2961
34c998b4 2962static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
63340133 2963{
49d73912
CW
2964 struct drm_i915_private *dev_priv = ggtt->base.i915;
2965 struct pci_dev *pdev = dev_priv->drm.pdev;
34c998b4 2966 phys_addr_t phys_addr;
8bcdd0f7 2967 int ret;
63340133
BW
2968
2969 /* For Modern GENs the PTEs and register space are split in the BAR */
34c998b4 2970 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
63340133 2971
2a073f89
ID
2972 /*
2973 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2974 * dropped. For WC mappings in general we have 64 byte burst writes
2975 * when the WC buffer is flushed, so we can't use it, but have to
2976 * resort to an uncached mapping. The WC issue is easily caught by the
2977 * readback check when writing GTT PTE entries.
2978 */
cc3f90f0 2979 if (IS_GEN9_LP(dev_priv))
34c998b4 2980 ggtt->gsm = ioremap_nocache(phys_addr, size);
2a073f89 2981 else
34c998b4 2982 ggtt->gsm = ioremap_wc(phys_addr, size);
72e96d64 2983 if (!ggtt->gsm) {
34c998b4 2984 DRM_ERROR("Failed to map the ggtt page table\n");
63340133
BW
2985 return -ENOMEM;
2986 }
2987
275a991c 2988 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
8bcdd0f7 2989 if (ret) {
63340133
BW
2990 DRM_ERROR("Scratch setup failed\n");
2991 /* iounmap will also get called at remove, but meh */
72e96d64 2992 iounmap(ggtt->gsm);
8bcdd0f7 2993 return ret;
63340133
BW
2994 }
2995
4ad2af1e 2996 return 0;
63340133
BW
2997}
2998
fbe5d36e
BW
2999/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3000 * bits. When using advanced contexts each context stores its own PAT, but
3001 * writing this data shouldn't be harmful even in those cases. */
ee0ce478 3002static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
fbe5d36e 3003{
fbe5d36e
BW
3004 uint64_t pat;
3005
3006 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3007 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3008 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3009 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3010 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3011 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3012 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3013 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3014
2d1fe073 3015 if (!USES_PPGTT(dev_priv))
d6a8b72e
RV
3016 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3017 * so RTL will always use the value corresponding to
3018 * pat_sel = 000".
3019 * So let's disable cache for GGTT to avoid screen corruptions.
3020 * MOCS still can be used though.
3021 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3022 * before this patch, i.e. the same uncached + snooping access
3023 * like on gen6/7 seems to be in effect.
3024 * - So this just fixes blitter/render access. Again it looks
3025 * like it's not just uncached access, but uncached + snooping.
3026 * So we can still hold onto all our assumptions wrt cpu
3027 * clflushing on LLC machines.
3028 */
3029 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3030
fbe5d36e
BW
3031 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3032 * write would work. */
7e435ad2
VS
3033 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3034 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
fbe5d36e
BW
3035}
3036
ee0ce478
VS
3037static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3038{
3039 uint64_t pat;
3040
3041 /*
3042 * Map WB on BDW to snooped on CHV.
3043 *
3044 * Only the snoop bit has meaning for CHV, the rest is
3045 * ignored.
3046 *
cf3d262e
VS
3047 * The hardware will never snoop for certain types of accesses:
3048 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3049 * - PPGTT page tables
3050 * - some other special cycles
3051 *
3052 * As with BDW, we also need to consider the following for GT accesses:
3053 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3054 * so RTL will always use the value corresponding to
3055 * pat_sel = 000".
3056 * Which means we must set the snoop bit in PAT entry 0
3057 * in order to keep the global status page working.
ee0ce478
VS
3058 */
3059 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3060 GEN8_PPAT(1, 0) |
3061 GEN8_PPAT(2, 0) |
3062 GEN8_PPAT(3, 0) |
3063 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3064 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3065 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3066 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3067
7e435ad2
VS
3068 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3069 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
ee0ce478
VS
3070}
3071
34c998b4
CW
3072static void gen6_gmch_remove(struct i915_address_space *vm)
3073{
3074 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3075
3076 iounmap(ggtt->gsm);
49d73912 3077 cleanup_scratch_page(vm->i915, &vm->scratch_page);
34c998b4
CW
3078}
3079
d507d735 3080static int gen8_gmch_probe(struct i915_ggtt *ggtt)
63340133 3081{
49d73912 3082 struct drm_i915_private *dev_priv = ggtt->base.i915;
97d6d7ab 3083 struct pci_dev *pdev = dev_priv->drm.pdev;
34c998b4 3084 unsigned int size;
63340133 3085 u16 snb_gmch_ctl;
63340133
BW
3086
3087 /* TODO: We're not aware of mappable constraints on gen8 yet */
97d6d7ab
CW
3088 ggtt->mappable_base = pci_resource_start(pdev, 2);
3089 ggtt->mappable_end = pci_resource_len(pdev, 2);
63340133 3090
97d6d7ab
CW
3091 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3092 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
63340133 3093
97d6d7ab 3094 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
63340133 3095
97d6d7ab 3096 if (INTEL_GEN(dev_priv) >= 9) {
d507d735 3097 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
34c998b4 3098 size = gen8_get_total_gtt_size(snb_gmch_ctl);
97d6d7ab 3099 } else if (IS_CHERRYVIEW(dev_priv)) {
d507d735 3100 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
34c998b4 3101 size = chv_get_total_gtt_size(snb_gmch_ctl);
d7f25f23 3102 } else {
d507d735 3103 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
34c998b4 3104 size = gen8_get_total_gtt_size(snb_gmch_ctl);
d7f25f23 3105 }
63340133 3106
34c998b4 3107 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
63340133 3108
cc3f90f0 3109 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
ee0ce478
VS
3110 chv_setup_private_ppat(dev_priv);
3111 else
3112 bdw_setup_private_ppat(dev_priv);
fbe5d36e 3113
34c998b4 3114 ggtt->base.cleanup = gen6_gmch_remove;
d507d735
JL
3115 ggtt->base.bind_vma = ggtt_bind_vma;
3116 ggtt->base.unbind_vma = ggtt_unbind_vma;
d6473f56 3117 ggtt->base.insert_page = gen8_ggtt_insert_page;
f7770bfd 3118 ggtt->base.clear_range = nop_clear_range;
48f112fe 3119 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
f7770bfd
CW
3120 ggtt->base.clear_range = gen8_ggtt_clear_range;
3121
3122 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3123 if (IS_CHERRYVIEW(dev_priv))
3124 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3125
7c3f86b6
CW
3126 ggtt->invalidate = gen6_ggtt_invalidate;
3127
34c998b4 3128 return ggtt_probe_common(ggtt, size);
63340133
BW
3129}
3130
d507d735 3131static int gen6_gmch_probe(struct i915_ggtt *ggtt)
e76e9aeb 3132{
49d73912 3133 struct drm_i915_private *dev_priv = ggtt->base.i915;
97d6d7ab 3134 struct pci_dev *pdev = dev_priv->drm.pdev;
34c998b4 3135 unsigned int size;
e76e9aeb 3136 u16 snb_gmch_ctl;
e76e9aeb 3137
97d6d7ab
CW
3138 ggtt->mappable_base = pci_resource_start(pdev, 2);
3139 ggtt->mappable_end = pci_resource_len(pdev, 2);
41907ddc 3140
baa09f5f
BW
3141 /* 64/512MB is the current min/max we actually know of, but this is just
3142 * a coarse sanity check.
e76e9aeb 3143 */
34c998b4 3144 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
d507d735 3145 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
baa09f5f 3146 return -ENXIO;
e76e9aeb
BW
3147 }
3148
97d6d7ab
CW
3149 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3150 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3151 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
e76e9aeb 3152
d507d735 3153 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
e76e9aeb 3154
34c998b4
CW
3155 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3156 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
e76e9aeb 3157
d507d735 3158 ggtt->base.clear_range = gen6_ggtt_clear_range;
d6473f56 3159 ggtt->base.insert_page = gen6_ggtt_insert_page;
d507d735
JL
3160 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3161 ggtt->base.bind_vma = ggtt_bind_vma;
3162 ggtt->base.unbind_vma = ggtt_unbind_vma;
34c998b4
CW
3163 ggtt->base.cleanup = gen6_gmch_remove;
3164
7c3f86b6
CW
3165 ggtt->invalidate = gen6_ggtt_invalidate;
3166
34c998b4
CW
3167 if (HAS_EDRAM(dev_priv))
3168 ggtt->base.pte_encode = iris_pte_encode;
3169 else if (IS_HASWELL(dev_priv))
3170 ggtt->base.pte_encode = hsw_pte_encode;
3171 else if (IS_VALLEYVIEW(dev_priv))
3172 ggtt->base.pte_encode = byt_pte_encode;
3173 else if (INTEL_GEN(dev_priv) >= 7)
3174 ggtt->base.pte_encode = ivb_pte_encode;
3175 else
3176 ggtt->base.pte_encode = snb_pte_encode;
7faf1ab2 3177
34c998b4 3178 return ggtt_probe_common(ggtt, size);
e76e9aeb
BW
3179}
3180
34c998b4 3181static void i915_gmch_remove(struct i915_address_space *vm)
e76e9aeb 3182{
34c998b4 3183 intel_gmch_remove();
644ec02b 3184}
baa09f5f 3185
d507d735 3186static int i915_gmch_probe(struct i915_ggtt *ggtt)
baa09f5f 3187{
49d73912 3188 struct drm_i915_private *dev_priv = ggtt->base.i915;
baa09f5f
BW
3189 int ret;
3190
91c8a326 3191 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
baa09f5f
BW
3192 if (!ret) {
3193 DRM_ERROR("failed to set up gmch\n");
3194 return -EIO;
3195 }
3196
edd1f2fe
CW
3197 intel_gtt_get(&ggtt->base.total,
3198 &ggtt->stolen_size,
3199 &ggtt->mappable_base,
3200 &ggtt->mappable_end);
baa09f5f 3201
97d6d7ab 3202 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
d6473f56 3203 ggtt->base.insert_page = i915_ggtt_insert_page;
d507d735
JL
3204 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3205 ggtt->base.clear_range = i915_ggtt_clear_range;
3206 ggtt->base.bind_vma = ggtt_bind_vma;
3207 ggtt->base.unbind_vma = ggtt_unbind_vma;
34c998b4 3208 ggtt->base.cleanup = i915_gmch_remove;
baa09f5f 3209
7c3f86b6
CW
3210 ggtt->invalidate = gmch_ggtt_invalidate;
3211
d507d735 3212 if (unlikely(ggtt->do_idle_maps))
c0a7f818
CW
3213 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3214
baa09f5f
BW
3215 return 0;
3216}
3217
d85489d3 3218/**
0088e522 3219 * i915_ggtt_probe_hw - Probe GGTT hardware location
97d6d7ab 3220 * @dev_priv: i915 device
d85489d3 3221 */
97d6d7ab 3222int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
baa09f5f 3223{
62106b4f 3224 struct i915_ggtt *ggtt = &dev_priv->ggtt;
baa09f5f
BW
3225 int ret;
3226
49d73912 3227 ggtt->base.i915 = dev_priv;
c114f76a 3228
34c998b4
CW
3229 if (INTEL_GEN(dev_priv) <= 5)
3230 ret = i915_gmch_probe(ggtt);
3231 else if (INTEL_GEN(dev_priv) < 8)
3232 ret = gen6_gmch_probe(ggtt);
3233 else
3234 ret = gen8_gmch_probe(ggtt);
a54c0c27 3235 if (ret)
baa09f5f 3236 return ret;
baa09f5f 3237
db9309a5
CW
3238 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3239 * This is easier than doing range restriction on the fly, as we
3240 * currently don't have any bits spare to pass in this upper
3241 * restriction!
3242 */
3243 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3244 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3245 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3246 }
3247
c890e2d5
CW
3248 if ((ggtt->base.total - 1) >> 32) {
3249 DRM_ERROR("We never expected a Global GTT with more than 32bits"
f6b9d5ca 3250 " of address space! Found %lldM!\n",
c890e2d5
CW
3251 ggtt->base.total >> 20);
3252 ggtt->base.total = 1ULL << 32;
3253 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3254 }
3255
f6b9d5ca
CW
3256 if (ggtt->mappable_end > ggtt->base.total) {
3257 DRM_ERROR("mappable aperture extends past end of GGTT,"
3258 " aperture=%llx, total=%llx\n",
3259 ggtt->mappable_end, ggtt->base.total);
3260 ggtt->mappable_end = ggtt->base.total;
3261 }
3262
baa09f5f 3263 /* GMADR is the PCI mmio aperture into the global GTT. */
c44ef60e 3264 DRM_INFO("Memory usable by graphics device = %lluM\n",
62106b4f
JL
3265 ggtt->base.total >> 20);
3266 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
edd1f2fe 3267 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
5db6c735
DV
3268#ifdef CONFIG_INTEL_IOMMU
3269 if (intel_iommu_gfx_mapped)
3270 DRM_INFO("VT-d active for gfx access\n");
3271#endif
baa09f5f
BW
3272
3273 return 0;
0088e522
CW
3274}
3275
3276/**
3277 * i915_ggtt_init_hw - Initialize GGTT hardware
97d6d7ab 3278 * @dev_priv: i915 device
0088e522 3279 */
97d6d7ab 3280int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
0088e522 3281{
0088e522
CW
3282 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3283 int ret;
3284
f6b9d5ca
CW
3285 INIT_LIST_HEAD(&dev_priv->vm_list);
3286
a6508ded
CW
3287 /* Note that we use page colouring to enforce a guard page at the
3288 * end of the address space. This is required as the CS may prefetch
3289 * beyond the end of the batch buffer, across the page boundary,
3290 * and beyond the end of the GTT if we do not provide a guard.
f6b9d5ca 3291 */
80b204bc 3292 mutex_lock(&dev_priv->drm.struct_mutex);
80b204bc 3293 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
a6508ded 3294 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
f6b9d5ca 3295 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
80b204bc 3296 mutex_unlock(&dev_priv->drm.struct_mutex);
f6b9d5ca 3297
f7bbe788
CW
3298 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3299 dev_priv->ggtt.mappable_base,
3300 dev_priv->ggtt.mappable_end)) {
f6b9d5ca
CW
3301 ret = -EIO;
3302 goto out_gtt_cleanup;
3303 }
3304
3305 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3306
0088e522
CW
3307 /*
3308 * Initialise stolen early so that we may reserve preallocated
3309 * objects for the BIOS to KMS transition.
3310 */
7ace3d30 3311 ret = i915_gem_init_stolen(dev_priv);
0088e522
CW
3312 if (ret)
3313 goto out_gtt_cleanup;
3314
3315 return 0;
a4eba47b
ID
3316
3317out_gtt_cleanup:
72e96d64 3318 ggtt->base.cleanup(&ggtt->base);
a4eba47b 3319 return ret;
baa09f5f 3320}
6f65e29a 3321
97d6d7ab 3322int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
ac840ae5 3323{
97d6d7ab 3324 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
ac840ae5
VS
3325 return -EIO;
3326
3327 return 0;
3328}
3329
7c3f86b6
CW
3330void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3331{
3332 i915->ggtt.invalidate = guc_ggtt_invalidate;
3333}
3334
3335void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3336{
3337 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3338}
3339
275a991c 3340void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
fa42331b 3341{
72e96d64 3342 struct i915_ggtt *ggtt = &dev_priv->ggtt;
fbb30a5c 3343 struct drm_i915_gem_object *obj, *on;
fa42331b 3344
dc97997a 3345 i915_check_and_clear_faults(dev_priv);
fa42331b
DV
3346
3347 /* First fill our portion of the GTT with scratch pages */
4fb84d99 3348 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
fa42331b 3349
fbb30a5c
CW
3350 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3351
3352 /* clflush objects bound into the GGTT and rebind them. */
3353 list_for_each_entry_safe(obj, on,
56cea323 3354 &dev_priv->mm.bound_list, global_link) {
fbb30a5c
CW
3355 bool ggtt_bound = false;
3356 struct i915_vma *vma;
3357
1c7f4bca 3358 list_for_each_entry(vma, &obj->vma_list, obj_link) {
72e96d64 3359 if (vma->vm != &ggtt->base)
2c3d9984 3360 continue;
fa42331b 3361
fbb30a5c
CW
3362 if (!i915_vma_unbind(vma))
3363 continue;
3364
2c3d9984
TU
3365 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3366 PIN_UPDATE));
fbb30a5c 3367 ggtt_bound = true;
2c3d9984
TU
3368 }
3369
fbb30a5c 3370 if (ggtt_bound)
975f7ff4 3371 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
2c3d9984 3372 }
fa42331b 3373
fbb30a5c
CW
3374 ggtt->base.closed = false;
3375
275a991c 3376 if (INTEL_GEN(dev_priv) >= 8) {
cc3f90f0 3377 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
fa42331b
DV
3378 chv_setup_private_ppat(dev_priv);
3379 else
3380 bdw_setup_private_ppat(dev_priv);
3381
3382 return;
3383 }
3384
275a991c 3385 if (USES_PPGTT(dev_priv)) {
72e96d64
JL
3386 struct i915_address_space *vm;
3387
fa42331b
DV
3388 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3389 /* TODO: Perhaps it shouldn't be gen6 specific */
3390
e5716f55 3391 struct i915_hw_ppgtt *ppgtt;
fa42331b 3392
2bfa996e 3393 if (i915_is_ggtt(vm))
fa42331b 3394 ppgtt = dev_priv->mm.aliasing_ppgtt;
e5716f55
JL
3395 else
3396 ppgtt = i915_vm_to_ppgtt(vm);
fa42331b
DV
3397
3398 gen6_write_page_range(dev_priv, &ppgtt->pd,
3399 0, ppgtt->base.total);
3400 }
3401 }
3402
7c3f86b6 3403 i915_ggtt_invalidate(dev_priv);
fa42331b
DV
3404}
3405
804beb4b 3406static struct scatterlist *
2d7f3bdb 3407rotate_pages(const dma_addr_t *in, unsigned int offset,
804beb4b 3408 unsigned int width, unsigned int height,
87130255 3409 unsigned int stride,
804beb4b 3410 struct sg_table *st, struct scatterlist *sg)
50470bb0
TU
3411{
3412 unsigned int column, row;
3413 unsigned int src_idx;
50470bb0 3414
50470bb0 3415 for (column = 0; column < width; column++) {
87130255 3416 src_idx = stride * (height - 1) + column;
50470bb0
TU
3417 for (row = 0; row < height; row++) {
3418 st->nents++;
3419 /* We don't need the pages, but need to initialize
3420 * the entries so the sg list can be happily traversed.
3421 * The only thing we need are DMA addresses.
3422 */
3423 sg_set_page(sg, NULL, PAGE_SIZE, 0);
804beb4b 3424 sg_dma_address(sg) = in[offset + src_idx];
50470bb0
TU
3425 sg_dma_len(sg) = PAGE_SIZE;
3426 sg = sg_next(sg);
87130255 3427 src_idx -= stride;
50470bb0
TU
3428 }
3429 }
804beb4b
TU
3430
3431 return sg;
50470bb0
TU
3432}
3433
3434static struct sg_table *
6687c906 3435intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
50470bb0
TU
3436 struct drm_i915_gem_object *obj)
3437{
85d1225e 3438 const size_t n_pages = obj->base.size / PAGE_SIZE;
6687c906 3439 unsigned int size = intel_rotation_info_size(rot_info);
85d1225e
DG
3440 struct sgt_iter sgt_iter;
3441 dma_addr_t dma_addr;
50470bb0
TU
3442 unsigned long i;
3443 dma_addr_t *page_addr_list;
3444 struct sg_table *st;
89e3e142 3445 struct scatterlist *sg;
1d00dad5 3446 int ret = -ENOMEM;
50470bb0 3447
50470bb0 3448 /* Allocate a temporary list of source pages for random access. */
85d1225e 3449 page_addr_list = drm_malloc_gfp(n_pages,
f2a85e19
CW
3450 sizeof(dma_addr_t),
3451 GFP_TEMPORARY);
50470bb0
TU
3452 if (!page_addr_list)
3453 return ERR_PTR(ret);
3454
3455 /* Allocate target SG list. */
3456 st = kmalloc(sizeof(*st), GFP_KERNEL);
3457 if (!st)
3458 goto err_st_alloc;
3459
6687c906 3460 ret = sg_alloc_table(st, size, GFP_KERNEL);
50470bb0
TU
3461 if (ret)
3462 goto err_sg_alloc;
3463
3464 /* Populate source page list from the object. */
3465 i = 0;
a4f5ea64 3466 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
85d1225e 3467 page_addr_list[i++] = dma_addr;
50470bb0 3468
85d1225e 3469 GEM_BUG_ON(i != n_pages);
11f20322
VS
3470 st->nents = 0;
3471 sg = st->sgl;
3472
6687c906
VS
3473 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3474 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3475 rot_info->plane[i].width, rot_info->plane[i].height,
3476 rot_info->plane[i].stride, st, sg);
89e3e142
TU
3477 }
3478
6687c906
VS
3479 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3480 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
50470bb0
TU
3481
3482 drm_free_large(page_addr_list);
3483
3484 return st;
3485
3486err_sg_alloc:
3487 kfree(st);
3488err_st_alloc:
3489 drm_free_large(page_addr_list);
3490
6687c906
VS
3491 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3492 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3493
50470bb0
TU
3494 return ERR_PTR(ret);
3495}
ec7adb6e 3496
8bd7ef16
JL
3497static struct sg_table *
3498intel_partial_pages(const struct i915_ggtt_view *view,
3499 struct drm_i915_gem_object *obj)
3500{
3501 struct sg_table *st;
d2a84a76 3502 struct scatterlist *sg, *iter;
8bab1193 3503 unsigned int count = view->partial.size;
d2a84a76 3504 unsigned int offset;
8bd7ef16
JL
3505 int ret = -ENOMEM;
3506
3507 st = kmalloc(sizeof(*st), GFP_KERNEL);
3508 if (!st)
3509 goto err_st_alloc;
3510
d2a84a76 3511 ret = sg_alloc_table(st, count, GFP_KERNEL);
8bd7ef16
JL
3512 if (ret)
3513 goto err_sg_alloc;
3514
8bab1193 3515 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
d2a84a76
CW
3516 GEM_BUG_ON(!iter);
3517
8bd7ef16
JL
3518 sg = st->sgl;
3519 st->nents = 0;
d2a84a76
CW
3520 do {
3521 unsigned int len;
8bd7ef16 3522
d2a84a76
CW
3523 len = min(iter->length - (offset << PAGE_SHIFT),
3524 count << PAGE_SHIFT);
3525 sg_set_page(sg, NULL, len, 0);
3526 sg_dma_address(sg) =
3527 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3528 sg_dma_len(sg) = len;
8bd7ef16 3529
8bd7ef16 3530 st->nents++;
d2a84a76
CW
3531 count -= len >> PAGE_SHIFT;
3532 if (count == 0) {
3533 sg_mark_end(sg);
3534 return st;
3535 }
8bd7ef16 3536
d2a84a76
CW
3537 sg = __sg_next(sg);
3538 iter = __sg_next(iter);
3539 offset = 0;
3540 } while (1);
8bd7ef16
JL
3541
3542err_sg_alloc:
3543 kfree(st);
3544err_st_alloc:
3545 return ERR_PTR(ret);
3546}
3547
70b9f6f8 3548static int
50470bb0 3549i915_get_ggtt_vma_pages(struct i915_vma *vma)
fe14d5f4 3550{
50470bb0
TU
3551 int ret = 0;
3552
2c3a3f44
CW
3553 /* The vma->pages are only valid within the lifespan of the borrowed
3554 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3555 * must be the vma->pages. A simple rule is that vma->pages must only
3556 * be accessed when the obj->mm.pages are pinned.
3557 */
3558 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3559
247177dd 3560 if (vma->pages)
fe14d5f4
TU
3561 return 0;
3562
3563 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
a4f5ea64 3564 vma->pages = vma->obj->mm.pages;
50470bb0 3565 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
247177dd 3566 vma->pages =
8bab1193
CW
3567 intel_rotate_fb_obj_pages(&vma->ggtt_view.rotated,
3568 vma->obj);
8bd7ef16 3569 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
247177dd 3570 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
fe14d5f4
TU
3571 else
3572 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3573 vma->ggtt_view.type);
3574
247177dd 3575 if (!vma->pages) {
ec7adb6e 3576 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
fe14d5f4 3577 vma->ggtt_view.type);
50470bb0 3578 ret = -EINVAL;
247177dd
CW
3579 } else if (IS_ERR(vma->pages)) {
3580 ret = PTR_ERR(vma->pages);
3581 vma->pages = NULL;
50470bb0
TU
3582 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3583 vma->ggtt_view.type, ret);
fe14d5f4
TU
3584 }
3585
50470bb0 3586 return ret;
fe14d5f4
TU
3587}
3588
625d988a
CW
3589/**
3590 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
a4dbf7cf
CW
3591 * @vm: the &struct i915_address_space
3592 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3593 * @size: how much space to allocate inside the GTT,
3594 * must be #I915_GTT_PAGE_SIZE aligned
3595 * @offset: where to insert inside the GTT,
3596 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3597 * (@offset + @size) must fit within the address space
3598 * @color: color to apply to node, if this node is not from a VMA,
3599 * color must be #I915_COLOR_UNEVICTABLE
3600 * @flags: control search and eviction behaviour
625d988a
CW
3601 *
3602 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3603 * the address space (using @size and @color). If the @node does not fit, it
3604 * tries to evict any overlapping nodes from the GTT, including any
3605 * neighbouring nodes if the colors do not match (to ensure guard pages between
3606 * differing domains). See i915_gem_evict_for_node() for the gory details
3607 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3608 * evicting active overlapping objects, and any overlapping node that is pinned
3609 * or marked as unevictable will also result in failure.
3610 *
3611 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3612 * asked to wait for eviction and interrupted.
3613 */
3614int i915_gem_gtt_reserve(struct i915_address_space *vm,
3615 struct drm_mm_node *node,
3616 u64 size, u64 offset, unsigned long color,
3617 unsigned int flags)
3618{
3619 int err;
3620
3621 GEM_BUG_ON(!size);
3622 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3623 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3624 GEM_BUG_ON(range_overflows(offset, size, vm->total));
3fec7ec4 3625 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
9734ad13 3626 GEM_BUG_ON(drm_mm_node_allocated(node));
625d988a
CW
3627
3628 node->size = size;
3629 node->start = offset;
3630 node->color = color;
3631
3632 err = drm_mm_reserve_node(&vm->mm, node);
3633 if (err != -ENOSPC)
3634 return err;
3635
3636 err = i915_gem_evict_for_node(vm, node, flags);
3637 if (err == 0)
3638 err = drm_mm_reserve_node(&vm->mm, node);
3639
3640 return err;
3641}
3642
606fec95
CW
3643static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3644{
3645 u64 range, addr;
3646
3647 GEM_BUG_ON(range_overflows(start, len, end));
3648 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3649
3650 range = round_down(end - len, align) - round_up(start, align);
3651 if (range) {
3652 if (sizeof(unsigned long) == sizeof(u64)) {
3653 addr = get_random_long();
3654 } else {
3655 addr = get_random_int();
3656 if (range > U32_MAX) {
3657 addr <<= 32;
3658 addr |= get_random_int();
3659 }
3660 }
3661 div64_u64_rem(addr, range, &addr);
3662 start += addr;
3663 }
3664
3665 return round_up(start, align);
3666}
3667
e007b19d
CW
3668/**
3669 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
a4dbf7cf
CW
3670 * @vm: the &struct i915_address_space
3671 * @node: the &struct drm_mm_node (typically i915_vma.node)
3672 * @size: how much space to allocate inside the GTT,
3673 * must be #I915_GTT_PAGE_SIZE aligned
3674 * @alignment: required alignment of starting offset, may be 0 but
3675 * if specified, this must be a power-of-two and at least
3676 * #I915_GTT_MIN_ALIGNMENT
3677 * @color: color to apply to node
3678 * @start: start of any range restriction inside GTT (0 for all),
e007b19d 3679 * must be #I915_GTT_PAGE_SIZE aligned
a4dbf7cf
CW
3680 * @end: end of any range restriction inside GTT (U64_MAX for all),
3681 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3682 * @flags: control search and eviction behaviour
e007b19d
CW
3683 *
3684 * i915_gem_gtt_insert() first searches for an available hole into which
3685 * is can insert the node. The hole address is aligned to @alignment and
3686 * its @size must then fit entirely within the [@start, @end] bounds. The
3687 * nodes on either side of the hole must match @color, or else a guard page
3688 * will be inserted between the two nodes (or the node evicted). If no
606fec95
CW
3689 * suitable hole is found, first a victim is randomly selected and tested
3690 * for eviction, otherwise then the LRU list of objects within the GTT
e007b19d
CW
3691 * is scanned to find the first set of replacement nodes to create the hole.
3692 * Those old overlapping nodes are evicted from the GTT (and so must be
3693 * rebound before any future use). Any node that is currently pinned cannot
3694 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3695 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3696 * searching for an eviction candidate. See i915_gem_evict_something() for
3697 * the gory details on the eviction algorithm.
3698 *
3699 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3700 * asked to wait for eviction and interrupted.
3701 */
3702int i915_gem_gtt_insert(struct i915_address_space *vm,
3703 struct drm_mm_node *node,
3704 u64 size, u64 alignment, unsigned long color,
3705 u64 start, u64 end, unsigned int flags)
3706{
4e64e553 3707 enum drm_mm_insert_mode mode;
606fec95 3708 u64 offset;
e007b19d
CW
3709 int err;
3710
3711 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3712 GEM_BUG_ON(!size);
3713 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3714 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3715 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3716 GEM_BUG_ON(start >= end);
3717 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3718 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
3fec7ec4 3719 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
9734ad13 3720 GEM_BUG_ON(drm_mm_node_allocated(node));
e007b19d
CW
3721
3722 if (unlikely(range_overflows(start, size, end)))
3723 return -ENOSPC;
3724
3725 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3726 return -ENOSPC;
3727
4e64e553
CW
3728 mode = DRM_MM_INSERT_BEST;
3729 if (flags & PIN_HIGH)
3730 mode = DRM_MM_INSERT_HIGH;
3731 if (flags & PIN_MAPPABLE)
3732 mode = DRM_MM_INSERT_LOW;
e007b19d
CW
3733
3734 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3735 * so we know that we always have a minimum alignment of 4096.
3736 * The drm_mm range manager is optimised to return results
3737 * with zero alignment, so where possible use the optimal
3738 * path.
3739 */
3740 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3741 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3742 alignment = 0;
3743
4e64e553
CW
3744 err = drm_mm_insert_node_in_range(&vm->mm, node,
3745 size, alignment, color,
3746 start, end, mode);
e007b19d
CW
3747 if (err != -ENOSPC)
3748 return err;
3749
606fec95
CW
3750 /* No free space, pick a slot at random.
3751 *
3752 * There is a pathological case here using a GTT shared between
3753 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3754 *
3755 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3756 * (64k objects) (448k objects)
3757 *
3758 * Now imagine that the eviction LRU is ordered top-down (just because
3759 * pathology meets real life), and that we need to evict an object to
3760 * make room inside the aperture. The eviction scan then has to walk
3761 * the 448k list before it finds one within range. And now imagine that
3762 * it has to search for a new hole between every byte inside the memcpy,
3763 * for several simultaneous clients.
3764 *
3765 * On a full-ppgtt system, if we have run out of available space, there
3766 * will be lots and lots of objects in the eviction list! Again,
3767 * searching that LRU list may be slow if we are also applying any
3768 * range restrictions (e.g. restriction to low 4GiB) and so, for
3769 * simplicity and similarilty between different GTT, try the single
3770 * random replacement first.
3771 */
3772 offset = random_offset(start, end,
3773 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3774 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3775 if (err != -ENOSPC)
3776 return err;
3777
3778 /* Randomly selected placement is pinned, do a search */
e007b19d
CW
3779 err = i915_gem_evict_something(vm, size, alignment, color,
3780 start, end, flags);
3781 if (err)
3782 return err;
3783
4e64e553
CW
3784 return drm_mm_insert_node_in_range(&vm->mm, node,
3785 size, alignment, color,
3786 start, end, DRM_MM_INSERT_EVICT);
e007b19d 3787}
3b5bb0a3
CW
3788
3789#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3790#include "selftests/mock_gtt.c"
1c42819a 3791#include "selftests/i915_gem_gtt.c"
3b5bb0a3 3792#endif