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