]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/i915_gem_gtt.c
drm/i915: Fix up the vma aliasing ppgtt binding
[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
0e46ce2e 26#include <linux/seq_file.h>
760285e7
DH
27#include <drm/drmP.h>
28#include <drm/i915_drm.h>
76aaf220 29#include "i915_drv.h"
5dda8fa3 30#include "i915_vgpu.h"
76aaf220
DV
31#include "i915_trace.h"
32#include "intel_drv.h"
33
45f8f69a
TU
34/**
35 * DOC: Global GTT views
36 *
37 * Background and previous state
38 *
39 * Historically objects could exists (be bound) in global GTT space only as
40 * singular instances with a view representing all of the object's backing pages
41 * in a linear fashion. This view will be called a normal view.
42 *
43 * To support multiple views of the same object, where the number of mapped
44 * pages is not equal to the backing store, or where the layout of the pages
45 * is not linear, concept of a GGTT view was added.
46 *
47 * One example of an alternative view is a stereo display driven by a single
48 * image. In this case we would have a framebuffer looking like this
49 * (2x2 pages):
50 *
51 * 12
52 * 34
53 *
54 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
55 * rendering. In contrast, fed to the display engine would be an alternative
56 * view which could look something like this:
57 *
58 * 1212
59 * 3434
60 *
61 * In this example both the size and layout of pages in the alternative view is
62 * different from the normal view.
63 *
64 * Implementation and usage
65 *
66 * GGTT views are implemented using VMAs and are distinguished via enum
67 * i915_ggtt_view_type and struct i915_ggtt_view.
68 *
69 * A new flavour of core GEM functions which work with GGTT bound objects were
ec7adb6e
JL
70 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
71 * renaming in large amounts of code. They take the struct i915_ggtt_view
72 * parameter encapsulating all metadata required to implement a view.
45f8f69a
TU
73 *
74 * As a helper for callers which are only interested in the normal view,
75 * globally const i915_ggtt_view_normal singleton instance exists. All old core
76 * GEM API functions, the ones not taking the view parameter, are operating on,
77 * or with the normal GGTT view.
78 *
79 * Code wanting to add or use a new GGTT view needs to:
80 *
81 * 1. Add a new enum with a suitable name.
82 * 2. Extend the metadata in the i915_ggtt_view structure if required.
83 * 3. Add support to i915_get_vma_pages().
84 *
85 * New views are required to build a scatter-gather table from within the
86 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
87 * exists for the lifetime of an VMA.
88 *
89 * Core API is designed to have copy semantics which means that passed in
90 * struct i915_ggtt_view does not need to be persistent (left around after
91 * calling the core API functions).
92 *
93 */
94
fe14d5f4 95const struct i915_ggtt_view i915_ggtt_view_normal;
9abc4648
JL
96const struct i915_ggtt_view i915_ggtt_view_rotated = {
97 .type = I915_GGTT_VIEW_ROTATED
98};
fe14d5f4 99
ee0ce478
VS
100static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
101static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
a2319c08 102
cfa7c862
DV
103static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
104{
1893a71b
CW
105 bool has_aliasing_ppgtt;
106 bool has_full_ppgtt;
107
108 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
109 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
1893a71b 110
71ba2d64
YZ
111 if (intel_vgpu_active(dev))
112 has_full_ppgtt = false; /* emulation is too hard */
113
70ee45e1
DL
114 /*
115 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
116 * execlists, the sole mechanism available to submit work.
117 */
118 if (INTEL_INFO(dev)->gen < 9 &&
119 (enable_ppgtt == 0 || !has_aliasing_ppgtt))
cfa7c862
DV
120 return 0;
121
122 if (enable_ppgtt == 1)
123 return 1;
124
1893a71b 125 if (enable_ppgtt == 2 && has_full_ppgtt)
cfa7c862
DV
126 return 2;
127
93a25a9e
DV
128#ifdef CONFIG_INTEL_IOMMU
129 /* Disable ppgtt on SNB if VT-d is on. */
130 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
131 DRM_INFO("Disabling PPGTT because VT-d is on\n");
cfa7c862 132 return 0;
93a25a9e
DV
133 }
134#endif
135
62942ed7 136 /* Early VLV doesn't have this */
ca2aed6c
VS
137 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
138 dev->pdev->revision < 0xb) {
62942ed7
JB
139 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
140 return 0;
141 }
142
2f82bbdf
MT
143 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
144 return 2;
145 else
146 return has_aliasing_ppgtt ? 1 : 0;
93a25a9e
DV
147}
148
6f65e29a
BW
149static void ppgtt_bind_vma(struct i915_vma *vma,
150 enum i915_cache_level cache_level,
151 u32 flags);
152static void ppgtt_unbind_vma(struct i915_vma *vma);
153
07749ef3
MT
154static inline gen8_pte_t gen8_pte_encode(dma_addr_t addr,
155 enum i915_cache_level level,
156 bool valid)
94ec8f61 157{
07749ef3 158 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
94ec8f61 159 pte |= addr;
63c42e56
BW
160
161 switch (level) {
162 case I915_CACHE_NONE:
fbe5d36e 163 pte |= PPAT_UNCACHED_INDEX;
63c42e56
BW
164 break;
165 case I915_CACHE_WT:
166 pte |= PPAT_DISPLAY_ELLC_INDEX;
167 break;
168 default:
169 pte |= PPAT_CACHED_INDEX;
170 break;
171 }
172
94ec8f61
BW
173 return pte;
174}
175
07749ef3
MT
176static inline gen8_pde_t gen8_pde_encode(struct drm_device *dev,
177 dma_addr_t addr,
178 enum i915_cache_level level)
b1fe6673 179{
07749ef3 180 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
b1fe6673
BW
181 pde |= addr;
182 if (level != I915_CACHE_NONE)
183 pde |= PPAT_CACHED_PDE_INDEX;
184 else
185 pde |= PPAT_UNCACHED_INDEX;
186 return pde;
187}
188
07749ef3
MT
189static gen6_pte_t snb_pte_encode(dma_addr_t addr,
190 enum i915_cache_level level,
191 bool valid, u32 unused)
54d12527 192{
07749ef3 193 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
54d12527 194 pte |= GEN6_PTE_ADDR_ENCODE(addr);
e7210c3c
BW
195
196 switch (level) {
350ec881
CW
197 case I915_CACHE_L3_LLC:
198 case I915_CACHE_LLC:
199 pte |= GEN6_PTE_CACHE_LLC;
200 break;
201 case I915_CACHE_NONE:
202 pte |= GEN6_PTE_UNCACHED;
203 break;
204 default:
5f77eeb0 205 MISSING_CASE(level);
350ec881
CW
206 }
207
208 return pte;
209}
210
07749ef3
MT
211static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
212 enum i915_cache_level level,
213 bool valid, u32 unused)
350ec881 214{
07749ef3 215 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
350ec881
CW
216 pte |= GEN6_PTE_ADDR_ENCODE(addr);
217
218 switch (level) {
219 case I915_CACHE_L3_LLC:
220 pte |= GEN7_PTE_CACHE_L3_LLC;
e7210c3c
BW
221 break;
222 case I915_CACHE_LLC:
223 pte |= GEN6_PTE_CACHE_LLC;
224 break;
225 case I915_CACHE_NONE:
9119708c 226 pte |= GEN6_PTE_UNCACHED;
e7210c3c
BW
227 break;
228 default:
5f77eeb0 229 MISSING_CASE(level);
e7210c3c
BW
230 }
231
54d12527
BW
232 return pte;
233}
234
07749ef3
MT
235static gen6_pte_t byt_pte_encode(dma_addr_t addr,
236 enum i915_cache_level level,
237 bool valid, u32 flags)
93c34e70 238{
07749ef3 239 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
93c34e70
KG
240 pte |= GEN6_PTE_ADDR_ENCODE(addr);
241
24f3a8cf
AG
242 if (!(flags & PTE_READ_ONLY))
243 pte |= BYT_PTE_WRITEABLE;
93c34e70
KG
244
245 if (level != I915_CACHE_NONE)
246 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
247
248 return pte;
249}
250
07749ef3
MT
251static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
252 enum i915_cache_level level,
253 bool valid, u32 unused)
9119708c 254{
07749ef3 255 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
0d8ff15e 256 pte |= HSW_PTE_ADDR_ENCODE(addr);
9119708c
KG
257
258 if (level != I915_CACHE_NONE)
87a6b688 259 pte |= HSW_WB_LLC_AGE3;
9119708c
KG
260
261 return pte;
262}
263
07749ef3
MT
264static gen6_pte_t iris_pte_encode(dma_addr_t addr,
265 enum i915_cache_level level,
266 bool valid, u32 unused)
4d15c145 267{
07749ef3 268 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
4d15c145
BW
269 pte |= HSW_PTE_ADDR_ENCODE(addr);
270
651d794f
CW
271 switch (level) {
272 case I915_CACHE_NONE:
273 break;
274 case I915_CACHE_WT:
c51e9701 275 pte |= HSW_WT_ELLC_LLC_AGE3;
651d794f
CW
276 break;
277 default:
c51e9701 278 pte |= HSW_WB_ELLC_LLC_AGE3;
651d794f
CW
279 break;
280 }
4d15c145
BW
281
282 return pte;
283}
284
678d96fb
BW
285#define i915_dma_unmap_single(px, dev) \
286 __i915_dma_unmap_single((px)->daddr, dev)
287
288static inline void __i915_dma_unmap_single(dma_addr_t daddr,
289 struct drm_device *dev)
290{
291 struct device *device = &dev->pdev->dev;
292
293 dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
294}
295
296/**
297 * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc.
298 * @px: Page table/dir/etc to get a DMA map for
299 * @dev: drm device
300 *
301 * Page table allocations are unified across all gens. They always require a
302 * single 4k allocation, as well as a DMA mapping. If we keep the structs
303 * symmetric here, the simple macro covers us for every page table type.
304 *
305 * Return: 0 if success.
306 */
307#define i915_dma_map_single(px, dev) \
308 i915_dma_map_page_single((px)->page, (dev), &(px)->daddr)
309
310static inline int i915_dma_map_page_single(struct page *page,
311 struct drm_device *dev,
312 dma_addr_t *daddr)
313{
314 struct device *device = &dev->pdev->dev;
315
316 *daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
1266cdb1
MT
317 if (dma_mapping_error(device, *daddr))
318 return -ENOMEM;
319
320 return 0;
678d96fb
BW
321}
322
ec565b3c 323static void unmap_and_free_pt(struct i915_page_table *pt,
678d96fb 324 struct drm_device *dev)
06fda602
BW
325{
326 if (WARN_ON(!pt->page))
327 return;
678d96fb
BW
328
329 i915_dma_unmap_single(pt, dev);
06fda602 330 __free_page(pt->page);
678d96fb 331 kfree(pt->used_ptes);
06fda602
BW
332 kfree(pt);
333}
334
5a8e9943 335static void gen8_initialize_pt(struct i915_address_space *vm,
e5815a2e 336 struct i915_page_table *pt)
5a8e9943
MT
337{
338 gen8_pte_t *pt_vaddr, scratch_pte;
339 int i;
340
341 pt_vaddr = kmap_atomic(pt->page);
342 scratch_pte = gen8_pte_encode(vm->scratch.addr,
343 I915_CACHE_LLC, true);
344
345 for (i = 0; i < GEN8_PTES; i++)
346 pt_vaddr[i] = scratch_pte;
347
348 if (!HAS_LLC(vm->dev))
349 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
350 kunmap_atomic(pt_vaddr);
351}
352
ec565b3c 353static struct i915_page_table *alloc_pt_single(struct drm_device *dev)
06fda602 354{
ec565b3c 355 struct i915_page_table *pt;
678d96fb
BW
356 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
357 GEN8_PTES : GEN6_PTES;
358 int ret = -ENOMEM;
06fda602
BW
359
360 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
361 if (!pt)
362 return ERR_PTR(-ENOMEM);
363
678d96fb
BW
364 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
365 GFP_KERNEL);
366
367 if (!pt->used_ptes)
368 goto fail_bitmap;
369
4933d519 370 pt->page = alloc_page(GFP_KERNEL);
678d96fb
BW
371 if (!pt->page)
372 goto fail_page;
373
374 ret = i915_dma_map_single(pt, dev);
375 if (ret)
376 goto fail_dma;
06fda602
BW
377
378 return pt;
678d96fb
BW
379
380fail_dma:
381 __free_page(pt->page);
382fail_page:
383 kfree(pt->used_ptes);
384fail_bitmap:
385 kfree(pt);
386
387 return ERR_PTR(ret);
06fda602
BW
388}
389
e5815a2e
MT
390static void unmap_and_free_pd(struct i915_page_directory *pd,
391 struct drm_device *dev)
06fda602
BW
392{
393 if (pd->page) {
e5815a2e 394 i915_dma_unmap_single(pd, dev);
06fda602 395 __free_page(pd->page);
33c8819f 396 kfree(pd->used_pdes);
06fda602
BW
397 kfree(pd);
398 }
399}
400
e5815a2e 401static struct i915_page_directory *alloc_pd_single(struct drm_device *dev)
06fda602 402{
ec565b3c 403 struct i915_page_directory *pd;
33c8819f 404 int ret = -ENOMEM;
06fda602
BW
405
406 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
407 if (!pd)
408 return ERR_PTR(-ENOMEM);
409
33c8819f
MT
410 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
411 sizeof(*pd->used_pdes), GFP_KERNEL);
412 if (!pd->used_pdes)
413 goto free_pd;
414
5a8e9943 415 pd->page = alloc_page(GFP_KERNEL);
33c8819f
MT
416 if (!pd->page)
417 goto free_bitmap;
06fda602 418
e5815a2e 419 ret = i915_dma_map_single(pd, dev);
33c8819f
MT
420 if (ret)
421 goto free_page;
e5815a2e 422
06fda602 423 return pd;
33c8819f
MT
424
425free_page:
426 __free_page(pd->page);
427free_bitmap:
428 kfree(pd->used_pdes);
429free_pd:
430 kfree(pd);
431
432 return ERR_PTR(ret);
06fda602
BW
433}
434
94e409c1 435/* Broadwell Page Directory Pointer Descriptors */
7cb6d7ac
MT
436static int gen8_write_pdp(struct intel_engine_cs *ring,
437 unsigned entry,
438 dma_addr_t addr)
94e409c1
BW
439{
440 int ret;
441
442 BUG_ON(entry >= 4);
443
444 ret = intel_ring_begin(ring, 6);
445 if (ret)
446 return ret;
447
448 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
449 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
7cb6d7ac 450 intel_ring_emit(ring, upper_32_bits(addr));
94e409c1
BW
451 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
452 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
7cb6d7ac 453 intel_ring_emit(ring, lower_32_bits(addr));
94e409c1
BW
454 intel_ring_advance(ring);
455
456 return 0;
457}
458
eeb9488e 459static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
6689c167 460 struct intel_engine_cs *ring)
94e409c1 461{
eeb9488e 462 int i, ret;
94e409c1 463
7cb6d7ac
MT
464 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
465 struct i915_page_directory *pd = ppgtt->pdp.page_directory[i];
466 dma_addr_t pd_daddr = pd ? pd->daddr : ppgtt->scratch_pd->daddr;
467 /* The page directory might be NULL, but we need to clear out
468 * whatever the previous context might have used. */
469 ret = gen8_write_pdp(ring, i, pd_daddr);
eeb9488e
BW
470 if (ret)
471 return ret;
94e409c1 472 }
d595bd4b 473
eeb9488e 474 return 0;
94e409c1
BW
475}
476
459108b8 477static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
782f1495
BW
478 uint64_t start,
479 uint64_t length,
459108b8
BW
480 bool use_scratch)
481{
482 struct i915_hw_ppgtt *ppgtt =
483 container_of(vm, struct i915_hw_ppgtt, base);
07749ef3 484 gen8_pte_t *pt_vaddr, scratch_pte;
7ad47cf2
BW
485 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
486 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
487 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
782f1495 488 unsigned num_entries = length >> PAGE_SHIFT;
459108b8
BW
489 unsigned last_pte, i;
490
491 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
492 I915_CACHE_LLC, use_scratch);
493
494 while (num_entries) {
ec565b3c
MT
495 struct i915_page_directory *pd;
496 struct i915_page_table *pt;
06fda602
BW
497 struct page *page_table;
498
499 if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
500 continue;
501
502 pd = ppgtt->pdp.page_directory[pdpe];
503
504 if (WARN_ON(!pd->page_table[pde]))
505 continue;
506
507 pt = pd->page_table[pde];
508
509 if (WARN_ON(!pt->page))
510 continue;
511
512 page_table = pt->page;
459108b8 513
7ad47cf2 514 last_pte = pte + num_entries;
07749ef3
MT
515 if (last_pte > GEN8_PTES)
516 last_pte = GEN8_PTES;
459108b8
BW
517
518 pt_vaddr = kmap_atomic(page_table);
519
7ad47cf2 520 for (i = pte; i < last_pte; i++) {
459108b8 521 pt_vaddr[i] = scratch_pte;
7ad47cf2
BW
522 num_entries--;
523 }
459108b8 524
fd1ab8f4
RB
525 if (!HAS_LLC(ppgtt->base.dev))
526 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
459108b8
BW
527 kunmap_atomic(pt_vaddr);
528
7ad47cf2 529 pte = 0;
07749ef3 530 if (++pde == I915_PDES) {
7ad47cf2
BW
531 pdpe++;
532 pde = 0;
533 }
459108b8
BW
534 }
535}
536
9df15b49
BW
537static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
538 struct sg_table *pages,
782f1495 539 uint64_t start,
24f3a8cf 540 enum i915_cache_level cache_level, u32 unused)
9df15b49
BW
541{
542 struct i915_hw_ppgtt *ppgtt =
543 container_of(vm, struct i915_hw_ppgtt, base);
07749ef3 544 gen8_pte_t *pt_vaddr;
7ad47cf2
BW
545 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
546 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
547 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
9df15b49
BW
548 struct sg_page_iter sg_iter;
549
6f1cc993 550 pt_vaddr = NULL;
7ad47cf2 551
9df15b49 552 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
76643600 553 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
7ad47cf2
BW
554 break;
555
d7b3de91 556 if (pt_vaddr == NULL) {
ec565b3c
MT
557 struct i915_page_directory *pd = ppgtt->pdp.page_directory[pdpe];
558 struct i915_page_table *pt = pd->page_table[pde];
06fda602 559 struct page *page_table = pt->page;
d7b3de91
BW
560
561 pt_vaddr = kmap_atomic(page_table);
562 }
9df15b49 563
7ad47cf2 564 pt_vaddr[pte] =
6f1cc993
CW
565 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
566 cache_level, true);
07749ef3 567 if (++pte == GEN8_PTES) {
fd1ab8f4
RB
568 if (!HAS_LLC(ppgtt->base.dev))
569 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
9df15b49 570 kunmap_atomic(pt_vaddr);
6f1cc993 571 pt_vaddr = NULL;
07749ef3 572 if (++pde == I915_PDES) {
7ad47cf2
BW
573 pdpe++;
574 pde = 0;
575 }
576 pte = 0;
9df15b49
BW
577 }
578 }
fd1ab8f4
RB
579 if (pt_vaddr) {
580 if (!HAS_LLC(ppgtt->base.dev))
581 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
6f1cc993 582 kunmap_atomic(pt_vaddr);
fd1ab8f4 583 }
9df15b49
BW
584}
585
69876bed
MT
586static void __gen8_do_map_pt(gen8_pde_t * const pde,
587 struct i915_page_table *pt,
588 struct drm_device *dev)
589{
590 gen8_pde_t entry =
591 gen8_pde_encode(dev, pt->daddr, I915_CACHE_LLC);
592 *pde = entry;
593}
594
595static void gen8_initialize_pd(struct i915_address_space *vm,
596 struct i915_page_directory *pd)
597{
598 struct i915_hw_ppgtt *ppgtt =
599 container_of(vm, struct i915_hw_ppgtt, base);
600 gen8_pde_t *page_directory;
601 struct i915_page_table *pt;
602 int i;
603
604 page_directory = kmap_atomic(pd->page);
605 pt = ppgtt->scratch_pt;
606 for (i = 0; i < I915_PDES; i++)
607 /* Map the PDE to the page table */
608 __gen8_do_map_pt(page_directory + i, pt, vm->dev);
609
610 if (!HAS_LLC(vm->dev))
611 drm_clflush_virt_range(page_directory, PAGE_SIZE);
e5815a2e
MT
612 kunmap_atomic(page_directory);
613}
614
ec565b3c 615static void gen8_free_page_tables(struct i915_page_directory *pd, struct drm_device *dev)
7ad47cf2
BW
616{
617 int i;
618
06fda602 619 if (!pd->page)
7ad47cf2
BW
620 return;
621
33c8819f 622 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
06fda602
BW
623 if (WARN_ON(!pd->page_table[i]))
624 continue;
7ad47cf2 625
06dc68d6 626 unmap_and_free_pt(pd->page_table[i], dev);
06fda602
BW
627 pd->page_table[i] = NULL;
628 }
d7b3de91
BW
629}
630
061dd493 631static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
b45a6715 632{
061dd493
DV
633 struct i915_hw_ppgtt *ppgtt =
634 container_of(vm, struct i915_hw_ppgtt, base);
b45a6715
BW
635 int i;
636
33c8819f 637 for_each_set_bit(i, ppgtt->pdp.used_pdpes, GEN8_LEGACY_PDPES) {
06fda602
BW
638 if (WARN_ON(!ppgtt->pdp.page_directory[i]))
639 continue;
640
06dc68d6 641 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
e5815a2e 642 unmap_and_free_pd(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
7ad47cf2 643 }
69876bed 644
e5815a2e 645 unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev);
69876bed 646 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
b45a6715
BW
647}
648
d7b2633d
MT
649/**
650 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
651 * @ppgtt: Master ppgtt structure.
652 * @pd: Page directory for this address range.
653 * @start: Starting virtual address to begin allocations.
654 * @length Size of the allocations.
655 * @new_pts: Bitmap set by function with new allocations. Likely used by the
656 * caller to free on error.
657 *
658 * Allocate the required number of page tables. Extremely similar to
659 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
660 * the page directory boundary (instead of the page directory pointer). That
661 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
662 * possible, and likely that the caller will need to use multiple calls of this
663 * function to achieve the appropriate allocation.
664 *
665 * Return: 0 if success; negative error code otherwise.
666 */
e5815a2e
MT
667static int gen8_ppgtt_alloc_pagetabs(struct i915_hw_ppgtt *ppgtt,
668 struct i915_page_directory *pd,
5441f0cb 669 uint64_t start,
d7b2633d
MT
670 uint64_t length,
671 unsigned long *new_pts)
bf2b4ed2 672{
e5815a2e 673 struct drm_device *dev = ppgtt->base.dev;
d7b2633d 674 struct i915_page_table *pt;
5441f0cb
MT
675 uint64_t temp;
676 uint32_t pde;
bf2b4ed2 677
d7b2633d
MT
678 gen8_for_each_pde(pt, pd, start, length, temp, pde) {
679 /* Don't reallocate page tables */
680 if (pt) {
681 /* Scratch is never allocated this way */
682 WARN_ON(pt == ppgtt->scratch_pt);
683 continue;
684 }
685
686 pt = alloc_pt_single(dev);
687 if (IS_ERR(pt))
5441f0cb
MT
688 goto unwind_out;
689
d7b2633d
MT
690 gen8_initialize_pt(&ppgtt->base, pt);
691 pd->page_table[pde] = pt;
692 set_bit(pde, new_pts);
7ad47cf2
BW
693 }
694
bf2b4ed2 695 return 0;
7ad47cf2
BW
696
697unwind_out:
d7b2633d 698 for_each_set_bit(pde, new_pts, I915_PDES)
e5815a2e 699 unmap_and_free_pt(pd->page_table[pde], dev);
7ad47cf2 700
d7b3de91 701 return -ENOMEM;
bf2b4ed2
BW
702}
703
d7b2633d
MT
704/**
705 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
706 * @ppgtt: Master ppgtt structure.
707 * @pdp: Page directory pointer for this address range.
708 * @start: Starting virtual address to begin allocations.
709 * @length Size of the allocations.
710 * @new_pds Bitmap set by function with new allocations. Likely used by the
711 * caller to free on error.
712 *
713 * Allocate the required number of page directories starting at the pde index of
714 * @start, and ending at the pde index @start + @length. This function will skip
715 * over already allocated page directories within the range, and only allocate
716 * new ones, setting the appropriate pointer within the pdp as well as the
717 * correct position in the bitmap @new_pds.
718 *
719 * The function will only allocate the pages within the range for a give page
720 * directory pointer. In other words, if @start + @length straddles a virtually
721 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
722 * required by the caller, This is not currently possible, and the BUG in the
723 * code will prevent it.
724 *
725 * Return: 0 if success; negative error code otherwise.
726 */
c488dbba
MT
727static int gen8_ppgtt_alloc_page_directories(struct i915_hw_ppgtt *ppgtt,
728 struct i915_page_directory_pointer *pdp,
69876bed 729 uint64_t start,
d7b2633d
MT
730 uint64_t length,
731 unsigned long *new_pds)
bf2b4ed2 732{
e5815a2e 733 struct drm_device *dev = ppgtt->base.dev;
d7b2633d 734 struct i915_page_directory *pd;
69876bed
MT
735 uint64_t temp;
736 uint32_t pdpe;
737
d7b2633d
MT
738 WARN_ON(!bitmap_empty(new_pds, GEN8_LEGACY_PDPES));
739
69876bed
MT
740 /* FIXME: PPGTT container_of won't work for 64b */
741 WARN_ON((start + length) > 0x800000000ULL);
742
d7b2633d
MT
743 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
744 if (pd)
745 continue;
33c8819f 746
d7b2633d
MT
747 pd = alloc_pd_single(dev);
748 if (IS_ERR(pd))
d7b3de91 749 goto unwind_out;
69876bed 750
d7b2633d
MT
751 gen8_initialize_pd(&ppgtt->base, pd);
752 pdp->page_directory[pdpe] = pd;
753 set_bit(pdpe, new_pds);
d7b3de91
BW
754 }
755
bf2b4ed2 756 return 0;
d7b3de91
BW
757
758unwind_out:
d7b2633d 759 for_each_set_bit(pdpe, new_pds, GEN8_LEGACY_PDPES)
e5815a2e 760 unmap_and_free_pd(pdp->page_directory[pdpe], dev);
d7b3de91
BW
761
762 return -ENOMEM;
bf2b4ed2
BW
763}
764
d7b2633d
MT
765static void
766free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long **new_pts)
767{
768 int i;
769
770 for (i = 0; i < GEN8_LEGACY_PDPES; i++)
771 kfree(new_pts[i]);
772 kfree(new_pts);
773 kfree(new_pds);
774}
775
776/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
777 * of these are based on the number of PDPEs in the system.
778 */
779static
780int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
781 unsigned long ***new_pts)
782{
783 int i;
784 unsigned long *pds;
785 unsigned long **pts;
786
787 pds = kcalloc(BITS_TO_LONGS(GEN8_LEGACY_PDPES), sizeof(unsigned long), GFP_KERNEL);
788 if (!pds)
789 return -ENOMEM;
790
791 pts = kcalloc(GEN8_LEGACY_PDPES, sizeof(unsigned long *), GFP_KERNEL);
792 if (!pts) {
793 kfree(pds);
794 return -ENOMEM;
795 }
796
797 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
798 pts[i] = kcalloc(BITS_TO_LONGS(I915_PDES),
799 sizeof(unsigned long), GFP_KERNEL);
800 if (!pts[i])
801 goto err_out;
802 }
803
804 *new_pds = pds;
805 *new_pts = pts;
806
807 return 0;
808
809err_out:
810 free_gen8_temp_bitmaps(pds, pts);
811 return -ENOMEM;
812}
813
e5815a2e
MT
814static int gen8_alloc_va_range(struct i915_address_space *vm,
815 uint64_t start,
816 uint64_t length)
bf2b4ed2 817{
e5815a2e
MT
818 struct i915_hw_ppgtt *ppgtt =
819 container_of(vm, struct i915_hw_ppgtt, base);
d7b2633d 820 unsigned long *new_page_dirs, **new_page_tables;
5441f0cb 821 struct i915_page_directory *pd;
33c8819f
MT
822 const uint64_t orig_start = start;
823 const uint64_t orig_length = length;
5441f0cb
MT
824 uint64_t temp;
825 uint32_t pdpe;
bf2b4ed2
BW
826 int ret;
827
d7b2633d
MT
828#ifndef CONFIG_64BIT
829 /* Disallow 64b address on 32b platforms. Nothing is wrong with doing
830 * this in hardware, but a lot of the drm code is not prepared to handle
831 * 64b offset on 32b platforms.
832 * This will be addressed when 48b PPGTT is added */
833 if (start + length > 0x100000000ULL)
834 return -E2BIG;
835#endif
836
837 /* Wrap is never okay since we can only represent 48b, and we don't
838 * actually use the other side of the canonical address space.
839 */
840 if (WARN_ON(start + length < start))
841 return -ERANGE;
842
843 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables);
bf2b4ed2
BW
844 if (ret)
845 return ret;
846
d7b2633d
MT
847 /* Do the allocations first so we can easily bail out */
848 ret = gen8_ppgtt_alloc_page_directories(ppgtt, &ppgtt->pdp, start, length,
849 new_page_dirs);
850 if (ret) {
851 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
852 return ret;
853 }
854
855 /* For every page directory referenced, allocate page tables */
5441f0cb 856 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
d7b2633d
MT
857 ret = gen8_ppgtt_alloc_pagetabs(ppgtt, pd, start, length,
858 new_page_tables[pdpe]);
5441f0cb
MT
859 if (ret)
860 goto err_out;
5441f0cb
MT
861 }
862
33c8819f
MT
863 start = orig_start;
864 length = orig_length;
865
d7b2633d
MT
866 /* Allocations have completed successfully, so set the bitmaps, and do
867 * the mappings. */
33c8819f 868 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
d7b2633d 869 gen8_pde_t *const page_directory = kmap_atomic(pd->page);
33c8819f
MT
870 struct i915_page_table *pt;
871 uint64_t pd_len = gen8_clamp_pd(start, length);
872 uint64_t pd_start = start;
873 uint32_t pde;
874
d7b2633d
MT
875 /* Every pd should be allocated, we just did that above. */
876 WARN_ON(!pd);
877
878 gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
879 /* Same reasoning as pd */
880 WARN_ON(!pt);
881 WARN_ON(!pd_len);
882 WARN_ON(!gen8_pte_count(pd_start, pd_len));
883
884 /* Set our used ptes within the page table */
885 bitmap_set(pt->used_ptes,
886 gen8_pte_index(pd_start),
887 gen8_pte_count(pd_start, pd_len));
888
889 /* Our pde is now pointing to the pagetable, pt */
33c8819f 890 set_bit(pde, pd->used_pdes);
d7b2633d
MT
891
892 /* Map the PDE to the page table */
893 __gen8_do_map_pt(page_directory + pde, pt, vm->dev);
894
895 /* NB: We haven't yet mapped ptes to pages. At this
896 * point we're still relying on insert_entries() */
33c8819f 897 }
d7b2633d
MT
898
899 if (!HAS_LLC(vm->dev))
900 drm_clflush_virt_range(page_directory, PAGE_SIZE);
901
902 kunmap_atomic(page_directory);
903
33c8819f
MT
904 set_bit(pdpe, ppgtt->pdp.used_pdpes);
905 }
906
d7b2633d 907 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
d7b3de91 908 return 0;
bf2b4ed2 909
d7b3de91 910err_out:
d7b2633d
MT
911 while (pdpe--) {
912 for_each_set_bit(temp, new_page_tables[pdpe], I915_PDES)
913 unmap_and_free_pt(ppgtt->pdp.page_directory[pdpe]->page_table[temp], vm->dev);
914 }
915
916 for_each_set_bit(pdpe, new_page_dirs, GEN8_LEGACY_PDPES)
917 unmap_and_free_pd(ppgtt->pdp.page_directory[pdpe], vm->dev);
918
919 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
bf2b4ed2
BW
920 return ret;
921}
922
eb0b44ad 923/*
f3a964b9
BW
924 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
925 * with a net effect resembling a 2-level page table in normal x86 terms. Each
926 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
927 * space.
37aca44a 928 *
f3a964b9 929 */
5c5f6457 930static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
37aca44a 931{
69876bed
MT
932 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
933 if (IS_ERR(ppgtt->scratch_pt))
934 return PTR_ERR(ppgtt->scratch_pt);
935
e5815a2e 936 ppgtt->scratch_pd = alloc_pd_single(ppgtt->base.dev);
7cb6d7ac
MT
937 if (IS_ERR(ppgtt->scratch_pd))
938 return PTR_ERR(ppgtt->scratch_pd);
939
69876bed 940 gen8_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
7cb6d7ac 941 gen8_initialize_pd(&ppgtt->base, ppgtt->scratch_pd);
69876bed 942
d7b2633d 943 ppgtt->base.start = 0;
5c5f6457 944 ppgtt->base.total = 1ULL << 32;
d7b2633d 945 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
5c5f6457 946 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
d7b2633d 947 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
c7e16f22 948 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
777dc5bb
DV
949 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
950 ppgtt->base.bind_vma = ppgtt_bind_vma;
d7b2633d
MT
951
952 ppgtt->switch_mm = gen8_mm_switch;
953
954 return 0;
955}
956
87d60b63
BW
957static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
958{
87d60b63 959 struct i915_address_space *vm = &ppgtt->base;
09942c65 960 struct i915_page_table *unused;
07749ef3 961 gen6_pte_t scratch_pte;
87d60b63 962 uint32_t pd_entry;
09942c65
MT
963 uint32_t pte, pde, temp;
964 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
87d60b63 965
24f3a8cf 966 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
87d60b63 967
09942c65 968 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
87d60b63 969 u32 expected;
07749ef3 970 gen6_pte_t *pt_vaddr;
06fda602 971 dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
09942c65 972 pd_entry = readl(ppgtt->pd_addr + pde);
87d60b63
BW
973 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
974
975 if (pd_entry != expected)
976 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
977 pde,
978 pd_entry,
979 expected);
980 seq_printf(m, "\tPDE: %x\n", pd_entry);
981
06fda602 982 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
07749ef3 983 for (pte = 0; pte < GEN6_PTES; pte+=4) {
87d60b63 984 unsigned long va =
07749ef3 985 (pde * PAGE_SIZE * GEN6_PTES) +
87d60b63
BW
986 (pte * PAGE_SIZE);
987 int i;
988 bool found = false;
989 for (i = 0; i < 4; i++)
990 if (pt_vaddr[pte + i] != scratch_pte)
991 found = true;
992 if (!found)
993 continue;
994
995 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
996 for (i = 0; i < 4; i++) {
997 if (pt_vaddr[pte + i] != scratch_pte)
998 seq_printf(m, " %08x", pt_vaddr[pte + i]);
999 else
1000 seq_puts(m, " SCRATCH ");
1001 }
1002 seq_puts(m, "\n");
1003 }
1004 kunmap_atomic(pt_vaddr);
1005 }
1006}
1007
678d96fb 1008/* Write pde (index) from the page directory @pd to the page table @pt */
ec565b3c
MT
1009static void gen6_write_pde(struct i915_page_directory *pd,
1010 const int pde, struct i915_page_table *pt)
6197349b 1011{
678d96fb
BW
1012 /* Caller needs to make sure the write completes if necessary */
1013 struct i915_hw_ppgtt *ppgtt =
1014 container_of(pd, struct i915_hw_ppgtt, pd);
1015 u32 pd_entry;
6197349b 1016
678d96fb
BW
1017 pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr);
1018 pd_entry |= GEN6_PDE_VALID;
6197349b 1019
678d96fb
BW
1020 writel(pd_entry, ppgtt->pd_addr + pde);
1021}
6197349b 1022
678d96fb
BW
1023/* Write all the page tables found in the ppgtt structure to incrementing page
1024 * directories. */
1025static void gen6_write_page_range(struct drm_i915_private *dev_priv,
ec565b3c 1026 struct i915_page_directory *pd,
678d96fb
BW
1027 uint32_t start, uint32_t length)
1028{
ec565b3c 1029 struct i915_page_table *pt;
678d96fb
BW
1030 uint32_t pde, temp;
1031
1032 gen6_for_each_pde(pt, pd, start, length, temp, pde)
1033 gen6_write_pde(pd, pde, pt);
1034
1035 /* Make sure write is complete before other code can use this page
1036 * table. Also require for WC mapped PTEs */
1037 readl(dev_priv->gtt.gsm);
3e302542
BW
1038}
1039
b4a74e3a 1040static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
3e302542 1041{
7324cc04 1042 BUG_ON(ppgtt->pd.pd_offset & 0x3f);
b4a74e3a 1043
7324cc04 1044 return (ppgtt->pd.pd_offset / 64) << 16;
b4a74e3a
BW
1045}
1046
90252e5c 1047static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
6689c167 1048 struct intel_engine_cs *ring)
90252e5c 1049{
90252e5c
BW
1050 int ret;
1051
90252e5c
BW
1052 /* NB: TLBs must be flushed and invalidated before a switch */
1053 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1054 if (ret)
1055 return ret;
1056
1057 ret = intel_ring_begin(ring, 6);
1058 if (ret)
1059 return ret;
1060
1061 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1062 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1063 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1064 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1065 intel_ring_emit(ring, get_pd_offset(ppgtt));
1066 intel_ring_emit(ring, MI_NOOP);
1067 intel_ring_advance(ring);
1068
1069 return 0;
1070}
1071
71ba2d64
YZ
1072static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
1073 struct intel_engine_cs *ring)
1074{
1075 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1076
1077 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1078 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1079 return 0;
1080}
1081
48a10389 1082static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
6689c167 1083 struct intel_engine_cs *ring)
48a10389 1084{
48a10389
BW
1085 int ret;
1086
48a10389
BW
1087 /* NB: TLBs must be flushed and invalidated before a switch */
1088 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1089 if (ret)
1090 return ret;
1091
1092 ret = intel_ring_begin(ring, 6);
1093 if (ret)
1094 return ret;
1095
1096 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1097 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1098 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1099 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1100 intel_ring_emit(ring, get_pd_offset(ppgtt));
1101 intel_ring_emit(ring, MI_NOOP);
1102 intel_ring_advance(ring);
1103
90252e5c
BW
1104 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1105 if (ring->id != RCS) {
1106 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1107 if (ret)
1108 return ret;
1109 }
1110
48a10389
BW
1111 return 0;
1112}
1113
eeb9488e 1114static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
6689c167 1115 struct intel_engine_cs *ring)
eeb9488e
BW
1116{
1117 struct drm_device *dev = ppgtt->base.dev;
1118 struct drm_i915_private *dev_priv = dev->dev_private;
1119
48a10389 1120
eeb9488e
BW
1121 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1122 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1123
1124 POSTING_READ(RING_PP_DIR_DCLV(ring));
1125
1126 return 0;
1127}
1128
82460d97 1129static void gen8_ppgtt_enable(struct drm_device *dev)
eeb9488e 1130{
eeb9488e 1131 struct drm_i915_private *dev_priv = dev->dev_private;
a4872ba6 1132 struct intel_engine_cs *ring;
82460d97 1133 int j;
3e302542 1134
eeb9488e
BW
1135 for_each_ring(ring, dev_priv, j) {
1136 I915_WRITE(RING_MODE_GEN7(ring),
1137 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
eeb9488e 1138 }
eeb9488e 1139}
6197349b 1140
82460d97 1141static void gen7_ppgtt_enable(struct drm_device *dev)
3e302542 1142{
50227e1c 1143 struct drm_i915_private *dev_priv = dev->dev_private;
a4872ba6 1144 struct intel_engine_cs *ring;
b4a74e3a 1145 uint32_t ecochk, ecobits;
3e302542 1146 int i;
6197349b 1147
b4a74e3a
BW
1148 ecobits = I915_READ(GAC_ECO_BITS);
1149 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
a65c2fcd 1150
b4a74e3a
BW
1151 ecochk = I915_READ(GAM_ECOCHK);
1152 if (IS_HASWELL(dev)) {
1153 ecochk |= ECOCHK_PPGTT_WB_HSW;
1154 } else {
1155 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1156 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1157 }
1158 I915_WRITE(GAM_ECOCHK, ecochk);
a65c2fcd 1159
b4a74e3a 1160 for_each_ring(ring, dev_priv, i) {
6197349b 1161 /* GFX_MODE is per-ring on gen7+ */
b4a74e3a
BW
1162 I915_WRITE(RING_MODE_GEN7(ring),
1163 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
6197349b 1164 }
b4a74e3a 1165}
6197349b 1166
82460d97 1167static void gen6_ppgtt_enable(struct drm_device *dev)
b4a74e3a 1168{
50227e1c 1169 struct drm_i915_private *dev_priv = dev->dev_private;
b4a74e3a 1170 uint32_t ecochk, gab_ctl, ecobits;
a65c2fcd 1171
b4a74e3a
BW
1172 ecobits = I915_READ(GAC_ECO_BITS);
1173 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1174 ECOBITS_PPGTT_CACHE64B);
6197349b 1175
b4a74e3a
BW
1176 gab_ctl = I915_READ(GAB_CTL);
1177 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1178
1179 ecochk = I915_READ(GAM_ECOCHK);
1180 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1181
1182 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
6197349b
BW
1183}
1184
1d2a314c 1185/* PPGTT support for Sandybdrige/Gen6 and later */
853ba5d2 1186static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
782f1495
BW
1187 uint64_t start,
1188 uint64_t length,
828c7908 1189 bool use_scratch)
1d2a314c 1190{
853ba5d2
BW
1191 struct i915_hw_ppgtt *ppgtt =
1192 container_of(vm, struct i915_hw_ppgtt, base);
07749ef3 1193 gen6_pte_t *pt_vaddr, scratch_pte;
782f1495
BW
1194 unsigned first_entry = start >> PAGE_SHIFT;
1195 unsigned num_entries = length >> PAGE_SHIFT;
07749ef3
MT
1196 unsigned act_pt = first_entry / GEN6_PTES;
1197 unsigned first_pte = first_entry % GEN6_PTES;
7bddb01f 1198 unsigned last_pte, i;
1d2a314c 1199
24f3a8cf 1200 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
1d2a314c 1201
7bddb01f
DV
1202 while (num_entries) {
1203 last_pte = first_pte + num_entries;
07749ef3
MT
1204 if (last_pte > GEN6_PTES)
1205 last_pte = GEN6_PTES;
7bddb01f 1206
06fda602 1207 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
1d2a314c 1208
7bddb01f
DV
1209 for (i = first_pte; i < last_pte; i++)
1210 pt_vaddr[i] = scratch_pte;
1d2a314c
DV
1211
1212 kunmap_atomic(pt_vaddr);
1d2a314c 1213
7bddb01f
DV
1214 num_entries -= last_pte - first_pte;
1215 first_pte = 0;
a15326a5 1216 act_pt++;
7bddb01f 1217 }
1d2a314c
DV
1218}
1219
853ba5d2 1220static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
def886c3 1221 struct sg_table *pages,
782f1495 1222 uint64_t start,
24f3a8cf 1223 enum i915_cache_level cache_level, u32 flags)
def886c3 1224{
853ba5d2
BW
1225 struct i915_hw_ppgtt *ppgtt =
1226 container_of(vm, struct i915_hw_ppgtt, base);
07749ef3 1227 gen6_pte_t *pt_vaddr;
782f1495 1228 unsigned first_entry = start >> PAGE_SHIFT;
07749ef3
MT
1229 unsigned act_pt = first_entry / GEN6_PTES;
1230 unsigned act_pte = first_entry % GEN6_PTES;
6e995e23
ID
1231 struct sg_page_iter sg_iter;
1232
cc79714f 1233 pt_vaddr = NULL;
6e995e23 1234 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
cc79714f 1235 if (pt_vaddr == NULL)
06fda602 1236 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
6e995e23 1237
cc79714f
CW
1238 pt_vaddr[act_pte] =
1239 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
24f3a8cf
AG
1240 cache_level, true, flags);
1241
07749ef3 1242 if (++act_pte == GEN6_PTES) {
6e995e23 1243 kunmap_atomic(pt_vaddr);
cc79714f 1244 pt_vaddr = NULL;
a15326a5 1245 act_pt++;
6e995e23 1246 act_pte = 0;
def886c3 1247 }
def886c3 1248 }
cc79714f
CW
1249 if (pt_vaddr)
1250 kunmap_atomic(pt_vaddr);
def886c3
DV
1251}
1252
563222a7
BW
1253/* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we
1254 * are switching between contexts with the same LRCA, we also must do a force
1255 * restore.
1256 */
1257static inline void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1258{
1259 /* If current vm != vm, */
1260 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1261}
1262
4933d519 1263static void gen6_initialize_pt(struct i915_address_space *vm,
ec565b3c 1264 struct i915_page_table *pt)
4933d519
MT
1265{
1266 gen6_pte_t *pt_vaddr, scratch_pte;
1267 int i;
1268
1269 WARN_ON(vm->scratch.addr == 0);
1270
1271 scratch_pte = vm->pte_encode(vm->scratch.addr,
1272 I915_CACHE_LLC, true, 0);
1273
1274 pt_vaddr = kmap_atomic(pt->page);
1275
1276 for (i = 0; i < GEN6_PTES; i++)
1277 pt_vaddr[i] = scratch_pte;
1278
1279 kunmap_atomic(pt_vaddr);
1280}
1281
678d96fb
BW
1282static int gen6_alloc_va_range(struct i915_address_space *vm,
1283 uint64_t start, uint64_t length)
1284{
4933d519
MT
1285 DECLARE_BITMAP(new_page_tables, I915_PDES);
1286 struct drm_device *dev = vm->dev;
1287 struct drm_i915_private *dev_priv = dev->dev_private;
678d96fb
BW
1288 struct i915_hw_ppgtt *ppgtt =
1289 container_of(vm, struct i915_hw_ppgtt, base);
ec565b3c 1290 struct i915_page_table *pt;
4933d519 1291 const uint32_t start_save = start, length_save = length;
678d96fb 1292 uint32_t pde, temp;
4933d519
MT
1293 int ret;
1294
1295 WARN_ON(upper_32_bits(start));
1296
1297 bitmap_zero(new_page_tables, I915_PDES);
1298
1299 /* The allocation is done in two stages so that we can bail out with
1300 * minimal amount of pain. The first stage finds new page tables that
1301 * need allocation. The second stage marks use ptes within the page
1302 * tables.
1303 */
1304 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1305 if (pt != ppgtt->scratch_pt) {
1306 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1307 continue;
1308 }
1309
1310 /* We've already allocated a page table */
1311 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1312
1313 pt = alloc_pt_single(dev);
1314 if (IS_ERR(pt)) {
1315 ret = PTR_ERR(pt);
1316 goto unwind_out;
1317 }
1318
1319 gen6_initialize_pt(vm, pt);
1320
1321 ppgtt->pd.page_table[pde] = pt;
1322 set_bit(pde, new_page_tables);
72744cb1 1323 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
4933d519
MT
1324 }
1325
1326 start = start_save;
1327 length = length_save;
678d96fb
BW
1328
1329 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1330 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1331
1332 bitmap_zero(tmp_bitmap, GEN6_PTES);
1333 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1334 gen6_pte_count(start, length));
1335
4933d519
MT
1336 if (test_and_clear_bit(pde, new_page_tables))
1337 gen6_write_pde(&ppgtt->pd, pde, pt);
1338
72744cb1
MT
1339 trace_i915_page_table_entry_map(vm, pde, pt,
1340 gen6_pte_index(start),
1341 gen6_pte_count(start, length),
1342 GEN6_PTES);
4933d519 1343 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
678d96fb
BW
1344 GEN6_PTES);
1345 }
1346
4933d519
MT
1347 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1348
1349 /* Make sure write is complete before other code can use this page
1350 * table. Also require for WC mapped PTEs */
1351 readl(dev_priv->gtt.gsm);
1352
563222a7 1353 mark_tlbs_dirty(ppgtt);
678d96fb 1354 return 0;
4933d519
MT
1355
1356unwind_out:
1357 for_each_set_bit(pde, new_page_tables, I915_PDES) {
ec565b3c 1358 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
4933d519
MT
1359
1360 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1361 unmap_and_free_pt(pt, vm->dev);
1362 }
1363
1364 mark_tlbs_dirty(ppgtt);
1365 return ret;
678d96fb
BW
1366}
1367
061dd493 1368static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
a00d825d 1369{
061dd493
DV
1370 struct i915_hw_ppgtt *ppgtt =
1371 container_of(vm, struct i915_hw_ppgtt, base);
09942c65
MT
1372 struct i915_page_table *pt;
1373 uint32_t pde;
4933d519 1374
061dd493
DV
1375
1376 drm_mm_remove_node(&ppgtt->node);
1377
09942c65 1378 gen6_for_all_pdes(pt, ppgtt, pde) {
4933d519 1379 if (pt != ppgtt->scratch_pt)
09942c65 1380 unmap_and_free_pt(pt, ppgtt->base.dev);
4933d519 1381 }
06fda602 1382
4933d519 1383 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
e5815a2e 1384 unmap_and_free_pd(&ppgtt->pd, ppgtt->base.dev);
3440d265
DV
1385}
1386
b146520f 1387static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
3440d265 1388{
853ba5d2 1389 struct drm_device *dev = ppgtt->base.dev;
1d2a314c 1390 struct drm_i915_private *dev_priv = dev->dev_private;
e3cc1995 1391 bool retried = false;
b146520f 1392 int ret;
1d2a314c 1393
c8d4c0d6
BW
1394 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1395 * allocator works in address space sizes, so it's multiplied by page
1396 * size. We allocate at the top of the GTT to avoid fragmentation.
1397 */
1398 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
4933d519
MT
1399 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
1400 if (IS_ERR(ppgtt->scratch_pt))
1401 return PTR_ERR(ppgtt->scratch_pt);
1402
1403 gen6_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
1404
e3cc1995 1405alloc:
c8d4c0d6
BW
1406 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1407 &ppgtt->node, GEN6_PD_SIZE,
1408 GEN6_PD_ALIGN, 0,
1409 0, dev_priv->gtt.base.total,
3e8b5ae9 1410 DRM_MM_TOPDOWN);
e3cc1995
BW
1411 if (ret == -ENOSPC && !retried) {
1412 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1413 GEN6_PD_SIZE, GEN6_PD_ALIGN,
d23db88c
CW
1414 I915_CACHE_NONE,
1415 0, dev_priv->gtt.base.total,
1416 0);
e3cc1995 1417 if (ret)
678d96fb 1418 goto err_out;
e3cc1995
BW
1419
1420 retried = true;
1421 goto alloc;
1422 }
c8d4c0d6 1423
c8c26622 1424 if (ret)
678d96fb
BW
1425 goto err_out;
1426
c8c26622 1427
c8d4c0d6
BW
1428 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1429 DRM_DEBUG("Forced to use aperture for PDEs\n");
1d2a314c 1430
c8c26622 1431 return 0;
678d96fb
BW
1432
1433err_out:
4933d519 1434 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
678d96fb 1435 return ret;
b146520f
BW
1436}
1437
b146520f
BW
1438static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1439{
2f2cf682 1440 return gen6_ppgtt_allocate_page_directories(ppgtt);
4933d519 1441}
06dc68d6 1442
4933d519
MT
1443static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1444 uint64_t start, uint64_t length)
1445{
ec565b3c 1446 struct i915_page_table *unused;
4933d519 1447 uint32_t pde, temp;
1d2a314c 1448
4933d519
MT
1449 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
1450 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
b146520f
BW
1451}
1452
5c5f6457 1453static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
b146520f
BW
1454{
1455 struct drm_device *dev = ppgtt->base.dev;
1456 struct drm_i915_private *dev_priv = dev->dev_private;
1457 int ret;
1458
1459 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
1460 if (IS_GEN6(dev)) {
b146520f
BW
1461 ppgtt->switch_mm = gen6_mm_switch;
1462 } else if (IS_HASWELL(dev)) {
b146520f
BW
1463 ppgtt->switch_mm = hsw_mm_switch;
1464 } else if (IS_GEN7(dev)) {
b146520f
BW
1465 ppgtt->switch_mm = gen7_mm_switch;
1466 } else
1467 BUG();
1468
71ba2d64
YZ
1469 if (intel_vgpu_active(dev))
1470 ppgtt->switch_mm = vgpu_mm_switch;
1471
b146520f
BW
1472 ret = gen6_ppgtt_alloc(ppgtt);
1473 if (ret)
1474 return ret;
1475
5c5f6457 1476 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
b146520f
BW
1477 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1478 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
777dc5bb
DV
1479 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1480 ppgtt->base.bind_vma = ppgtt_bind_vma;
b146520f 1481 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
b146520f 1482 ppgtt->base.start = 0;
09942c65 1483 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
87d60b63 1484 ppgtt->debug_dump = gen6_dump_ppgtt;
1d2a314c 1485
7324cc04 1486 ppgtt->pd.pd_offset =
07749ef3 1487 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1d2a314c 1488
678d96fb
BW
1489 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
1490 ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
1491
5c5f6457 1492 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
1d2a314c 1493
678d96fb
BW
1494 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
1495
440fd528 1496 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
b146520f
BW
1497 ppgtt->node.size >> 20,
1498 ppgtt->node.start / PAGE_SIZE);
3440d265 1499
fa76da34 1500 DRM_DEBUG("Adding PPGTT at offset %x\n",
7324cc04 1501 ppgtt->pd.pd_offset << 10);
fa76da34 1502
b146520f 1503 return 0;
3440d265
DV
1504}
1505
5c5f6457 1506static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
3440d265
DV
1507{
1508 struct drm_i915_private *dev_priv = dev->dev_private;
3440d265 1509
853ba5d2 1510 ppgtt->base.dev = dev;
8407bb91 1511 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
3440d265 1512
3ed124b2 1513 if (INTEL_INFO(dev)->gen < 8)
5c5f6457 1514 return gen6_ppgtt_init(ppgtt);
3ed124b2 1515 else
d7b2633d 1516 return gen8_ppgtt_init(ppgtt);
fa76da34
DV
1517}
1518int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1519{
1520 struct drm_i915_private *dev_priv = dev->dev_private;
1521 int ret = 0;
3ed124b2 1522
5c5f6457 1523 ret = __hw_ppgtt_init(dev, ppgtt);
fa76da34 1524 if (ret == 0) {
c7c48dfd 1525 kref_init(&ppgtt->ref);
93bd8649
BW
1526 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1527 ppgtt->base.total);
7e0d96bc 1528 i915_init_vm(dev_priv, &ppgtt->base);
93bd8649 1529 }
1d2a314c
DV
1530
1531 return ret;
1532}
1533
82460d97
DV
1534int i915_ppgtt_init_hw(struct drm_device *dev)
1535{
1536 struct drm_i915_private *dev_priv = dev->dev_private;
1537 struct intel_engine_cs *ring;
1538 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1539 int i, ret = 0;
1540
671b5013
TD
1541 /* In the case of execlists, PPGTT is enabled by the context descriptor
1542 * and the PDPs are contained within the context itself. We don't
1543 * need to do anything here. */
1544 if (i915.enable_execlists)
1545 return 0;
1546
82460d97
DV
1547 if (!USES_PPGTT(dev))
1548 return 0;
1549
1550 if (IS_GEN6(dev))
1551 gen6_ppgtt_enable(dev);
1552 else if (IS_GEN7(dev))
1553 gen7_ppgtt_enable(dev);
1554 else if (INTEL_INFO(dev)->gen >= 8)
1555 gen8_ppgtt_enable(dev);
1556 else
5f77eeb0 1557 MISSING_CASE(INTEL_INFO(dev)->gen);
82460d97
DV
1558
1559 if (ppgtt) {
1560 for_each_ring(ring, dev_priv, i) {
6689c167 1561 ret = ppgtt->switch_mm(ppgtt, ring);
82460d97
DV
1562 if (ret != 0)
1563 return ret;
7e0d96bc 1564 }
93bd8649 1565 }
1d2a314c
DV
1566
1567 return ret;
1568}
4d884705
DV
1569struct i915_hw_ppgtt *
1570i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1571{
1572 struct i915_hw_ppgtt *ppgtt;
1573 int ret;
1574
1575 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1576 if (!ppgtt)
1577 return ERR_PTR(-ENOMEM);
1578
1579 ret = i915_ppgtt_init(dev, ppgtt);
1580 if (ret) {
1581 kfree(ppgtt);
1582 return ERR_PTR(ret);
1583 }
1584
1585 ppgtt->file_priv = fpriv;
1586
198c974d
DCS
1587 trace_i915_ppgtt_create(&ppgtt->base);
1588
4d884705
DV
1589 return ppgtt;
1590}
1591
ee960be7
DV
1592void i915_ppgtt_release(struct kref *kref)
1593{
1594 struct i915_hw_ppgtt *ppgtt =
1595 container_of(kref, struct i915_hw_ppgtt, ref);
1596
198c974d
DCS
1597 trace_i915_ppgtt_release(&ppgtt->base);
1598
ee960be7
DV
1599 /* vmas should already be unbound */
1600 WARN_ON(!list_empty(&ppgtt->base.active_list));
1601 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1602
19dd120c
DV
1603 list_del(&ppgtt->base.global_link);
1604 drm_mm_takedown(&ppgtt->base.mm);
1605
ee960be7
DV
1606 ppgtt->base.cleanup(&ppgtt->base);
1607 kfree(ppgtt);
1608}
1d2a314c 1609
7e0d96bc 1610static void
6f65e29a
BW
1611ppgtt_bind_vma(struct i915_vma *vma,
1612 enum i915_cache_level cache_level,
f329f5f6 1613 u32 unused)
1d2a314c 1614{
f329f5f6
DV
1615 u32 pte_flags = 0;
1616
24f3a8cf
AG
1617 /* Currently applicable only to VLV */
1618 if (vma->obj->gt_ro)
f329f5f6 1619 pte_flags |= PTE_READ_ONLY;
24f3a8cf 1620
782f1495 1621 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
f329f5f6 1622 cache_level, pte_flags);
1d2a314c
DV
1623}
1624
7e0d96bc 1625static void ppgtt_unbind_vma(struct i915_vma *vma)
7bddb01f 1626{
6f65e29a 1627 vma->vm->clear_range(vma->vm,
782f1495
BW
1628 vma->node.start,
1629 vma->obj->base.size,
6f65e29a 1630 true);
7bddb01f
DV
1631}
1632
a81cc00c
BW
1633extern int intel_iommu_gfx_mapped;
1634/* Certain Gen5 chipsets require require idling the GPU before
1635 * unmapping anything from the GTT when VT-d is enabled.
1636 */
1637static inline bool needs_idle_maps(struct drm_device *dev)
1638{
1639#ifdef CONFIG_INTEL_IOMMU
1640 /* Query intel_iommu to see if we need the workaround. Presumably that
1641 * was loaded first.
1642 */
1643 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1644 return true;
1645#endif
1646 return false;
1647}
1648
5c042287
BW
1649static bool do_idling(struct drm_i915_private *dev_priv)
1650{
1651 bool ret = dev_priv->mm.interruptible;
1652
a81cc00c 1653 if (unlikely(dev_priv->gtt.do_idle_maps)) {
5c042287 1654 dev_priv->mm.interruptible = false;
b2da9fe5 1655 if (i915_gpu_idle(dev_priv->dev)) {
5c042287
BW
1656 DRM_ERROR("Couldn't idle GPU\n");
1657 /* Wait a bit, in hopes it avoids the hang */
1658 udelay(10);
1659 }
1660 }
1661
1662 return ret;
1663}
1664
1665static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1666{
a81cc00c 1667 if (unlikely(dev_priv->gtt.do_idle_maps))
5c042287
BW
1668 dev_priv->mm.interruptible = interruptible;
1669}
1670
828c7908
BW
1671void i915_check_and_clear_faults(struct drm_device *dev)
1672{
1673 struct drm_i915_private *dev_priv = dev->dev_private;
a4872ba6 1674 struct intel_engine_cs *ring;
828c7908
BW
1675 int i;
1676
1677 if (INTEL_INFO(dev)->gen < 6)
1678 return;
1679
1680 for_each_ring(ring, dev_priv, i) {
1681 u32 fault_reg;
1682 fault_reg = I915_READ(RING_FAULT_REG(ring));
1683 if (fault_reg & RING_FAULT_VALID) {
1684 DRM_DEBUG_DRIVER("Unexpected fault\n"
59a5d290 1685 "\tAddr: 0x%08lx\n"
828c7908
BW
1686 "\tAddress space: %s\n"
1687 "\tSource ID: %d\n"
1688 "\tType: %d\n",
1689 fault_reg & PAGE_MASK,
1690 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1691 RING_FAULT_SRCID(fault_reg),
1692 RING_FAULT_FAULT_TYPE(fault_reg));
1693 I915_WRITE(RING_FAULT_REG(ring),
1694 fault_reg & ~RING_FAULT_VALID);
1695 }
1696 }
1697 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1698}
1699
91e56499
CW
1700static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
1701{
1702 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
1703 intel_gtt_chipset_flush();
1704 } else {
1705 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1706 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1707 }
1708}
1709
828c7908
BW
1710void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1711{
1712 struct drm_i915_private *dev_priv = dev->dev_private;
1713
1714 /* Don't bother messing with faults pre GEN6 as we have little
1715 * documentation supporting that it's a good idea.
1716 */
1717 if (INTEL_INFO(dev)->gen < 6)
1718 return;
1719
1720 i915_check_and_clear_faults(dev);
1721
1722 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
782f1495
BW
1723 dev_priv->gtt.base.start,
1724 dev_priv->gtt.base.total,
e568af1c 1725 true);
91e56499
CW
1726
1727 i915_ggtt_flush(dev_priv);
828c7908
BW
1728}
1729
76aaf220
DV
1730void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1731{
1732 struct drm_i915_private *dev_priv = dev->dev_private;
05394f39 1733 struct drm_i915_gem_object *obj;
80da2161 1734 struct i915_address_space *vm;
76aaf220 1735
828c7908
BW
1736 i915_check_and_clear_faults(dev);
1737
bee4a186 1738 /* First fill our portion of the GTT with scratch pages */
853ba5d2 1739 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
782f1495
BW
1740 dev_priv->gtt.base.start,
1741 dev_priv->gtt.base.total,
828c7908 1742 true);
bee4a186 1743
35c20a60 1744 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
6f65e29a
BW
1745 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1746 &dev_priv->gtt.base);
1747 if (!vma)
1748 continue;
1749
2c22569b 1750 i915_gem_clflush_object(obj, obj->pin_display);
0875546c 1751 WARN_ON(i915_vma_bind(vma, obj->cache_level, PIN_UPDATE));
76aaf220
DV
1752 }
1753
80da2161 1754
a2319c08 1755 if (INTEL_INFO(dev)->gen >= 8) {
5a4e33a3 1756 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
ee0ce478
VS
1757 chv_setup_private_ppat(dev_priv);
1758 else
1759 bdw_setup_private_ppat(dev_priv);
1760
80da2161 1761 return;
a2319c08 1762 }
80da2161 1763
678d96fb
BW
1764 if (USES_PPGTT(dev)) {
1765 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1766 /* TODO: Perhaps it shouldn't be gen6 specific */
1767
1768 struct i915_hw_ppgtt *ppgtt =
1769 container_of(vm, struct i915_hw_ppgtt,
1770 base);
80da2161 1771
678d96fb
BW
1772 if (i915_is_ggtt(vm))
1773 ppgtt = dev_priv->mm.aliasing_ppgtt;
1774
1775 gen6_write_page_range(dev_priv, &ppgtt->pd,
1776 0, ppgtt->base.total);
1777 }
76aaf220
DV
1778 }
1779
91e56499 1780 i915_ggtt_flush(dev_priv);
76aaf220 1781}
7c2e6fdf 1782
74163907 1783int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
7c2e6fdf 1784{
9da3da66 1785 if (obj->has_dma_mapping)
74163907 1786 return 0;
9da3da66
CW
1787
1788 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1789 obj->pages->sgl, obj->pages->nents,
1790 PCI_DMA_BIDIRECTIONAL))
1791 return -ENOSPC;
1792
1793 return 0;
7c2e6fdf
DV
1794}
1795
07749ef3 1796static inline void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
94ec8f61
BW
1797{
1798#ifdef writeq
1799 writeq(pte, addr);
1800#else
1801 iowrite32((u32)pte, addr);
1802 iowrite32(pte >> 32, addr + 4);
1803#endif
1804}
1805
1806static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1807 struct sg_table *st,
782f1495 1808 uint64_t start,
24f3a8cf 1809 enum i915_cache_level level, u32 unused)
94ec8f61
BW
1810{
1811 struct drm_i915_private *dev_priv = vm->dev->dev_private;
782f1495 1812 unsigned first_entry = start >> PAGE_SHIFT;
07749ef3
MT
1813 gen8_pte_t __iomem *gtt_entries =
1814 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
94ec8f61
BW
1815 int i = 0;
1816 struct sg_page_iter sg_iter;
57007df7 1817 dma_addr_t addr = 0; /* shut up gcc */
94ec8f61
BW
1818
1819 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1820 addr = sg_dma_address(sg_iter.sg) +
1821 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1822 gen8_set_pte(&gtt_entries[i],
1823 gen8_pte_encode(addr, level, true));
1824 i++;
1825 }
1826
1827 /*
1828 * XXX: This serves as a posting read to make sure that the PTE has
1829 * actually been updated. There is some concern that even though
1830 * registers and PTEs are within the same BAR that they are potentially
1831 * of NUMA access patterns. Therefore, even with the way we assume
1832 * hardware should work, we must keep this posting read for paranoia.
1833 */
1834 if (i != 0)
1835 WARN_ON(readq(&gtt_entries[i-1])
1836 != gen8_pte_encode(addr, level, true));
1837
94ec8f61
BW
1838 /* This next bit makes the above posting read even more important. We
1839 * want to flush the TLBs only after we're certain all the PTE updates
1840 * have finished.
1841 */
1842 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1843 POSTING_READ(GFX_FLSH_CNTL_GEN6);
94ec8f61
BW
1844}
1845
e76e9aeb
BW
1846/*
1847 * Binds an object into the global gtt with the specified cache level. The object
1848 * will be accessible to the GPU via commands whose operands reference offsets
1849 * within the global GTT as well as accessible by the GPU through the GMADR
1850 * mapped BAR (dev_priv->mm.gtt->gtt).
1851 */
853ba5d2 1852static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
7faf1ab2 1853 struct sg_table *st,
782f1495 1854 uint64_t start,
24f3a8cf 1855 enum i915_cache_level level, u32 flags)
e76e9aeb 1856{
853ba5d2 1857 struct drm_i915_private *dev_priv = vm->dev->dev_private;
782f1495 1858 unsigned first_entry = start >> PAGE_SHIFT;
07749ef3
MT
1859 gen6_pte_t __iomem *gtt_entries =
1860 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
6e995e23
ID
1861 int i = 0;
1862 struct sg_page_iter sg_iter;
57007df7 1863 dma_addr_t addr = 0;
e76e9aeb 1864
6e995e23 1865 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
2db76d7c 1866 addr = sg_page_iter_dma_address(&sg_iter);
24f3a8cf 1867 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
6e995e23 1868 i++;
e76e9aeb
BW
1869 }
1870
e76e9aeb
BW
1871 /* XXX: This serves as a posting read to make sure that the PTE has
1872 * actually been updated. There is some concern that even though
1873 * registers and PTEs are within the same BAR that they are potentially
1874 * of NUMA access patterns. Therefore, even with the way we assume
1875 * hardware should work, we must keep this posting read for paranoia.
1876 */
57007df7
PM
1877 if (i != 0) {
1878 unsigned long gtt = readl(&gtt_entries[i-1]);
1879 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1880 }
0f9b91c7
BW
1881
1882 /* This next bit makes the above posting read even more important. We
1883 * want to flush the TLBs only after we're certain all the PTE updates
1884 * have finished.
1885 */
1886 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1887 POSTING_READ(GFX_FLSH_CNTL_GEN6);
e76e9aeb
BW
1888}
1889
94ec8f61 1890static void gen8_ggtt_clear_range(struct i915_address_space *vm,
782f1495
BW
1891 uint64_t start,
1892 uint64_t length,
94ec8f61
BW
1893 bool use_scratch)
1894{
1895 struct drm_i915_private *dev_priv = vm->dev->dev_private;
782f1495
BW
1896 unsigned first_entry = start >> PAGE_SHIFT;
1897 unsigned num_entries = length >> PAGE_SHIFT;
07749ef3
MT
1898 gen8_pte_t scratch_pte, __iomem *gtt_base =
1899 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
94ec8f61
BW
1900 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1901 int i;
1902
1903 if (WARN(num_entries > max_entries,
1904 "First entry = %d; Num entries = %d (max=%d)\n",
1905 first_entry, num_entries, max_entries))
1906 num_entries = max_entries;
1907
1908 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1909 I915_CACHE_LLC,
1910 use_scratch);
1911 for (i = 0; i < num_entries; i++)
1912 gen8_set_pte(&gtt_base[i], scratch_pte);
1913 readl(gtt_base);
1914}
1915
853ba5d2 1916static void gen6_ggtt_clear_range(struct i915_address_space *vm,
782f1495
BW
1917 uint64_t start,
1918 uint64_t length,
828c7908 1919 bool use_scratch)
7faf1ab2 1920{
853ba5d2 1921 struct drm_i915_private *dev_priv = vm->dev->dev_private;
782f1495
BW
1922 unsigned first_entry = start >> PAGE_SHIFT;
1923 unsigned num_entries = length >> PAGE_SHIFT;
07749ef3
MT
1924 gen6_pte_t scratch_pte, __iomem *gtt_base =
1925 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
a54c0c27 1926 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
7faf1ab2
DV
1927 int i;
1928
1929 if (WARN(num_entries > max_entries,
1930 "First entry = %d; Num entries = %d (max=%d)\n",
1931 first_entry, num_entries, max_entries))
1932 num_entries = max_entries;
1933
24f3a8cf 1934 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
828c7908 1935
7faf1ab2
DV
1936 for (i = 0; i < num_entries; i++)
1937 iowrite32(scratch_pte, &gtt_base[i]);
1938 readl(gtt_base);
1939}
1940
6f65e29a
BW
1941
1942static void i915_ggtt_bind_vma(struct i915_vma *vma,
1943 enum i915_cache_level cache_level,
1944 u32 unused)
7faf1ab2 1945{
6f65e29a 1946 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
7faf1ab2
DV
1947 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1948 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1949
6f65e29a 1950 BUG_ON(!i915_is_ggtt(vma->vm));
fe14d5f4 1951 intel_gtt_insert_sg_entries(vma->ggtt_view.pages, entry, flags);
0875546c
DV
1952
1953 vma->bound |= GLOBAL_BIND;
7faf1ab2
DV
1954}
1955
853ba5d2 1956static void i915_ggtt_clear_range(struct i915_address_space *vm,
782f1495
BW
1957 uint64_t start,
1958 uint64_t length,
828c7908 1959 bool unused)
7faf1ab2 1960{
782f1495
BW
1961 unsigned first_entry = start >> PAGE_SHIFT;
1962 unsigned num_entries = length >> PAGE_SHIFT;
7faf1ab2
DV
1963 intel_gtt_clear_range(first_entry, num_entries);
1964}
1965
6f65e29a
BW
1966static void i915_ggtt_unbind_vma(struct i915_vma *vma)
1967{
1968 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1969 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
7faf1ab2 1970
6f65e29a 1971 BUG_ON(!i915_is_ggtt(vma->vm));
6f65e29a
BW
1972 intel_gtt_clear_range(first, size);
1973}
7faf1ab2 1974
6f65e29a
BW
1975static void ggtt_bind_vma(struct i915_vma *vma,
1976 enum i915_cache_level cache_level,
1977 u32 flags)
d5bd1449 1978{
6f65e29a 1979 struct drm_device *dev = vma->vm->dev;
7faf1ab2 1980 struct drm_i915_private *dev_priv = dev->dev_private;
6f65e29a 1981 struct drm_i915_gem_object *obj = vma->obj;
ec7adb6e 1982 struct sg_table *pages = obj->pages;
f329f5f6 1983 u32 pte_flags = 0;
7faf1ab2 1984
24f3a8cf
AG
1985 /* Currently applicable only to VLV */
1986 if (obj->gt_ro)
f329f5f6 1987 pte_flags |= PTE_READ_ONLY;
24f3a8cf 1988
ec7adb6e
JL
1989 if (i915_is_ggtt(vma->vm))
1990 pages = vma->ggtt_view.pages;
1991
6f65e29a 1992 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
0875546c
DV
1993 vma->vm->insert_entries(vma->vm, pages,
1994 vma->node.start,
1995 cache_level, pte_flags);
1996
1997 vma->bound |= GLOBAL_BIND;
6f65e29a 1998 }
d5bd1449 1999
0875546c 2000 if (dev_priv->mm.aliasing_ppgtt && flags & LOCAL_BIND) {
6f65e29a 2001 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
ec7adb6e 2002 appgtt->base.insert_entries(&appgtt->base, pages,
782f1495 2003 vma->node.start,
f329f5f6 2004 cache_level, pte_flags);
6f65e29a 2005 }
d5bd1449
CW
2006}
2007
6f65e29a 2008static void ggtt_unbind_vma(struct i915_vma *vma)
74163907 2009{
6f65e29a 2010 struct drm_device *dev = vma->vm->dev;
7faf1ab2 2011 struct drm_i915_private *dev_priv = dev->dev_private;
6f65e29a 2012 struct drm_i915_gem_object *obj = vma->obj;
6f65e29a 2013
aff43766 2014 if (vma->bound & GLOBAL_BIND) {
782f1495
BW
2015 vma->vm->clear_range(vma->vm,
2016 vma->node.start,
2017 obj->base.size,
6f65e29a 2018 true);
6f65e29a 2019 }
74898d7e 2020
0875546c 2021 if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
6f65e29a
BW
2022 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2023 appgtt->base.clear_range(&appgtt->base,
782f1495
BW
2024 vma->node.start,
2025 obj->base.size,
6f65e29a 2026 true);
6f65e29a 2027 }
74163907
DV
2028}
2029
2030void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
7c2e6fdf 2031{
5c042287
BW
2032 struct drm_device *dev = obj->base.dev;
2033 struct drm_i915_private *dev_priv = dev->dev_private;
2034 bool interruptible;
2035
2036 interruptible = do_idling(dev_priv);
2037
9da3da66
CW
2038 if (!obj->has_dma_mapping)
2039 dma_unmap_sg(&dev->pdev->dev,
2040 obj->pages->sgl, obj->pages->nents,
2041 PCI_DMA_BIDIRECTIONAL);
5c042287
BW
2042
2043 undo_idling(dev_priv, interruptible);
7c2e6fdf 2044}
644ec02b 2045
42d6ab48
CW
2046static void i915_gtt_color_adjust(struct drm_mm_node *node,
2047 unsigned long color,
440fd528
TR
2048 u64 *start,
2049 u64 *end)
42d6ab48
CW
2050{
2051 if (node->color != color)
2052 *start += 4096;
2053
2054 if (!list_empty(&node->node_list)) {
2055 node = list_entry(node->node_list.next,
2056 struct drm_mm_node,
2057 node_list);
2058 if (node->allocated && node->color != color)
2059 *end -= 4096;
2060 }
2061}
fbe5d36e 2062
f548c0e9
DV
2063static int i915_gem_setup_global_gtt(struct drm_device *dev,
2064 unsigned long start,
2065 unsigned long mappable_end,
2066 unsigned long end)
644ec02b 2067{
e78891ca
BW
2068 /* Let GEM Manage all of the aperture.
2069 *
2070 * However, leave one page at the end still bound to the scratch page.
2071 * There are a number of places where the hardware apparently prefetches
2072 * past the end of the object, and we've seen multiple hangs with the
2073 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2074 * aperture. One page should be enough to keep any prefetching inside
2075 * of the aperture.
2076 */
40d74980
BW
2077 struct drm_i915_private *dev_priv = dev->dev_private;
2078 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
ed2f3452
CW
2079 struct drm_mm_node *entry;
2080 struct drm_i915_gem_object *obj;
2081 unsigned long hole_start, hole_end;
fa76da34 2082 int ret;
644ec02b 2083
35451cb6
BW
2084 BUG_ON(mappable_end > end);
2085
ed2f3452 2086 /* Subtract the guard page ... */
40d74980 2087 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
5dda8fa3
YZ
2088
2089 dev_priv->gtt.base.start = start;
2090 dev_priv->gtt.base.total = end - start;
2091
2092 if (intel_vgpu_active(dev)) {
2093 ret = intel_vgt_balloon(dev);
2094 if (ret)
2095 return ret;
2096 }
2097
42d6ab48 2098 if (!HAS_LLC(dev))
93bd8649 2099 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
644ec02b 2100
ed2f3452 2101 /* Mark any preallocated objects as occupied */
35c20a60 2102 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
40d74980 2103 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
fa76da34 2104
edd41a87 2105 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
c6cfb325
BW
2106 i915_gem_obj_ggtt_offset(obj), obj->base.size);
2107
2108 WARN_ON(i915_gem_obj_ggtt_bound(obj));
40d74980 2109 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
6c5566a8
DV
2110 if (ret) {
2111 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2112 return ret;
2113 }
aff43766 2114 vma->bound |= GLOBAL_BIND;
ed2f3452
CW
2115 }
2116
ed2f3452 2117 /* Clear any non-preallocated blocks */
40d74980 2118 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
ed2f3452
CW
2119 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2120 hole_start, hole_end);
782f1495
BW
2121 ggtt_vm->clear_range(ggtt_vm, hole_start,
2122 hole_end - hole_start, true);
ed2f3452
CW
2123 }
2124
2125 /* And finally clear the reserved guard page */
782f1495 2126 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
6c5566a8 2127
fa76da34
DV
2128 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2129 struct i915_hw_ppgtt *ppgtt;
2130
2131 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2132 if (!ppgtt)
2133 return -ENOMEM;
2134
5c5f6457
DV
2135 ret = __hw_ppgtt_init(dev, ppgtt);
2136 if (ret) {
2137 ppgtt->base.cleanup(&ppgtt->base);
2138 kfree(ppgtt);
2139 return ret;
2140 }
2141
2142 if (ppgtt->base.allocate_va_range)
2143 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2144 ppgtt->base.total);
4933d519 2145 if (ret) {
061dd493 2146 ppgtt->base.cleanup(&ppgtt->base);
4933d519 2147 kfree(ppgtt);
fa76da34 2148 return ret;
4933d519 2149 }
fa76da34 2150
5c5f6457
DV
2151 ppgtt->base.clear_range(&ppgtt->base,
2152 ppgtt->base.start,
2153 ppgtt->base.total,
2154 true);
2155
fa76da34
DV
2156 dev_priv->mm.aliasing_ppgtt = ppgtt;
2157 }
2158
6c5566a8 2159 return 0;
e76e9aeb
BW
2160}
2161
d7e5008f
BW
2162void i915_gem_init_global_gtt(struct drm_device *dev)
2163{
2164 struct drm_i915_private *dev_priv = dev->dev_private;
2165 unsigned long gtt_size, mappable_size;
d7e5008f 2166
853ba5d2 2167 gtt_size = dev_priv->gtt.base.total;
93d18799 2168 mappable_size = dev_priv->gtt.mappable_end;
d7e5008f 2169
e78891ca 2170 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
e76e9aeb
BW
2171}
2172
90d0a0e8
DV
2173void i915_global_gtt_cleanup(struct drm_device *dev)
2174{
2175 struct drm_i915_private *dev_priv = dev->dev_private;
2176 struct i915_address_space *vm = &dev_priv->gtt.base;
2177
70e32544
DV
2178 if (dev_priv->mm.aliasing_ppgtt) {
2179 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2180
2181 ppgtt->base.cleanup(&ppgtt->base);
2182 }
2183
90d0a0e8 2184 if (drm_mm_initialized(&vm->mm)) {
5dda8fa3
YZ
2185 if (intel_vgpu_active(dev))
2186 intel_vgt_deballoon();
2187
90d0a0e8
DV
2188 drm_mm_takedown(&vm->mm);
2189 list_del(&vm->global_link);
2190 }
2191
2192 vm->cleanup(vm);
2193}
70e32544 2194
e76e9aeb
BW
2195static int setup_scratch_page(struct drm_device *dev)
2196{
2197 struct drm_i915_private *dev_priv = dev->dev_private;
2198 struct page *page;
2199 dma_addr_t dma_addr;
2200
2201 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
2202 if (page == NULL)
2203 return -ENOMEM;
e76e9aeb
BW
2204 set_pages_uc(page, 1);
2205
2206#ifdef CONFIG_INTEL_IOMMU
2207 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
2208 PCI_DMA_BIDIRECTIONAL);
2209 if (pci_dma_mapping_error(dev->pdev, dma_addr))
2210 return -EINVAL;
2211#else
2212 dma_addr = page_to_phys(page);
2213#endif
853ba5d2
BW
2214 dev_priv->gtt.base.scratch.page = page;
2215 dev_priv->gtt.base.scratch.addr = dma_addr;
e76e9aeb
BW
2216
2217 return 0;
2218}
2219
2220static void teardown_scratch_page(struct drm_device *dev)
2221{
2222 struct drm_i915_private *dev_priv = dev->dev_private;
853ba5d2
BW
2223 struct page *page = dev_priv->gtt.base.scratch.page;
2224
2225 set_pages_wb(page, 1);
2226 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
e76e9aeb 2227 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
853ba5d2 2228 __free_page(page);
e76e9aeb
BW
2229}
2230
2231static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2232{
2233 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2234 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2235 return snb_gmch_ctl << 20;
2236}
2237
9459d252
BW
2238static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2239{
2240 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2241 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2242 if (bdw_gmch_ctl)
2243 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
562d55d9
BW
2244
2245#ifdef CONFIG_X86_32
2246 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2247 if (bdw_gmch_ctl > 4)
2248 bdw_gmch_ctl = 4;
2249#endif
2250
9459d252
BW
2251 return bdw_gmch_ctl << 20;
2252}
2253
d7f25f23
DL
2254static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2255{
2256 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2257 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2258
2259 if (gmch_ctrl)
2260 return 1 << (20 + gmch_ctrl);
2261
2262 return 0;
2263}
2264
baa09f5f 2265static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
e76e9aeb
BW
2266{
2267 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2268 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2269 return snb_gmch_ctl << 25; /* 32 MB units */
2270}
2271
9459d252
BW
2272static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2273{
2274 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2275 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2276 return bdw_gmch_ctl << 25; /* 32 MB units */
2277}
2278
d7f25f23
DL
2279static size_t chv_get_stolen_size(u16 gmch_ctrl)
2280{
2281 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2282 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2283
2284 /*
2285 * 0x0 to 0x10: 32MB increments starting at 0MB
2286 * 0x11 to 0x16: 4MB increments starting at 8MB
2287 * 0x17 to 0x1d: 4MB increments start at 36MB
2288 */
2289 if (gmch_ctrl < 0x11)
2290 return gmch_ctrl << 25;
2291 else if (gmch_ctrl < 0x17)
2292 return (gmch_ctrl - 0x11 + 2) << 22;
2293 else
2294 return (gmch_ctrl - 0x17 + 9) << 22;
2295}
2296
66375014
DL
2297static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2298{
2299 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2300 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2301
2302 if (gen9_gmch_ctl < 0xf0)
2303 return gen9_gmch_ctl << 25; /* 32 MB units */
2304 else
2305 /* 4MB increments starting at 0xf0 for 4MB */
2306 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2307}
2308
63340133
BW
2309static int ggtt_probe_common(struct drm_device *dev,
2310 size_t gtt_size)
2311{
2312 struct drm_i915_private *dev_priv = dev->dev_private;
21c34607 2313 phys_addr_t gtt_phys_addr;
63340133
BW
2314 int ret;
2315
2316 /* For Modern GENs the PTEs and register space are split in the BAR */
21c34607 2317 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
63340133
BW
2318 (pci_resource_len(dev->pdev, 0) / 2);
2319
2a073f89
ID
2320 /*
2321 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2322 * dropped. For WC mappings in general we have 64 byte burst writes
2323 * when the WC buffer is flushed, so we can't use it, but have to
2324 * resort to an uncached mapping. The WC issue is easily caught by the
2325 * readback check when writing GTT PTE entries.
2326 */
2327 if (IS_BROXTON(dev))
2328 dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size);
2329 else
2330 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
63340133
BW
2331 if (!dev_priv->gtt.gsm) {
2332 DRM_ERROR("Failed to map the gtt page table\n");
2333 return -ENOMEM;
2334 }
2335
2336 ret = setup_scratch_page(dev);
2337 if (ret) {
2338 DRM_ERROR("Scratch setup failed\n");
2339 /* iounmap will also get called at remove, but meh */
2340 iounmap(dev_priv->gtt.gsm);
2341 }
2342
2343 return ret;
2344}
2345
fbe5d36e
BW
2346/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2347 * bits. When using advanced contexts each context stores its own PAT, but
2348 * writing this data shouldn't be harmful even in those cases. */
ee0ce478 2349static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
fbe5d36e 2350{
fbe5d36e
BW
2351 uint64_t pat;
2352
2353 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2354 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2355 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2356 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2357 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2358 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2359 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2360 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2361
d6a8b72e
RV
2362 if (!USES_PPGTT(dev_priv->dev))
2363 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2364 * so RTL will always use the value corresponding to
2365 * pat_sel = 000".
2366 * So let's disable cache for GGTT to avoid screen corruptions.
2367 * MOCS still can be used though.
2368 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2369 * before this patch, i.e. the same uncached + snooping access
2370 * like on gen6/7 seems to be in effect.
2371 * - So this just fixes blitter/render access. Again it looks
2372 * like it's not just uncached access, but uncached + snooping.
2373 * So we can still hold onto all our assumptions wrt cpu
2374 * clflushing on LLC machines.
2375 */
2376 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2377
fbe5d36e
BW
2378 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2379 * write would work. */
2380 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2381 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2382}
2383
ee0ce478
VS
2384static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2385{
2386 uint64_t pat;
2387
2388 /*
2389 * Map WB on BDW to snooped on CHV.
2390 *
2391 * Only the snoop bit has meaning for CHV, the rest is
2392 * ignored.
2393 *
cf3d262e
VS
2394 * The hardware will never snoop for certain types of accesses:
2395 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2396 * - PPGTT page tables
2397 * - some other special cycles
2398 *
2399 * As with BDW, we also need to consider the following for GT accesses:
2400 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2401 * so RTL will always use the value corresponding to
2402 * pat_sel = 000".
2403 * Which means we must set the snoop bit in PAT entry 0
2404 * in order to keep the global status page working.
ee0ce478
VS
2405 */
2406 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2407 GEN8_PPAT(1, 0) |
2408 GEN8_PPAT(2, 0) |
2409 GEN8_PPAT(3, 0) |
2410 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2411 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2412 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2413 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2414
2415 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2416 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2417}
2418
63340133
BW
2419static int gen8_gmch_probe(struct drm_device *dev,
2420 size_t *gtt_total,
2421 size_t *stolen,
2422 phys_addr_t *mappable_base,
2423 unsigned long *mappable_end)
2424{
2425 struct drm_i915_private *dev_priv = dev->dev_private;
2426 unsigned int gtt_size;
2427 u16 snb_gmch_ctl;
2428 int ret;
2429
2430 /* TODO: We're not aware of mappable constraints on gen8 yet */
2431 *mappable_base = pci_resource_start(dev->pdev, 2);
2432 *mappable_end = pci_resource_len(dev->pdev, 2);
2433
2434 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
2435 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
2436
2437 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2438
66375014
DL
2439 if (INTEL_INFO(dev)->gen >= 9) {
2440 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
2441 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2442 } else if (IS_CHERRYVIEW(dev)) {
d7f25f23
DL
2443 *stolen = chv_get_stolen_size(snb_gmch_ctl);
2444 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2445 } else {
2446 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
2447 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2448 }
63340133 2449
07749ef3 2450 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
63340133 2451
5a4e33a3 2452 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
ee0ce478
VS
2453 chv_setup_private_ppat(dev_priv);
2454 else
2455 bdw_setup_private_ppat(dev_priv);
fbe5d36e 2456
63340133
BW
2457 ret = ggtt_probe_common(dev, gtt_size);
2458
94ec8f61
BW
2459 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2460 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
777dc5bb
DV
2461 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2462 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
63340133
BW
2463
2464 return ret;
2465}
2466
baa09f5f
BW
2467static int gen6_gmch_probe(struct drm_device *dev,
2468 size_t *gtt_total,
41907ddc
BW
2469 size_t *stolen,
2470 phys_addr_t *mappable_base,
2471 unsigned long *mappable_end)
e76e9aeb
BW
2472{
2473 struct drm_i915_private *dev_priv = dev->dev_private;
baa09f5f 2474 unsigned int gtt_size;
e76e9aeb 2475 u16 snb_gmch_ctl;
e76e9aeb
BW
2476 int ret;
2477
41907ddc
BW
2478 *mappable_base = pci_resource_start(dev->pdev, 2);
2479 *mappable_end = pci_resource_len(dev->pdev, 2);
2480
baa09f5f
BW
2481 /* 64/512MB is the current min/max we actually know of, but this is just
2482 * a coarse sanity check.
e76e9aeb 2483 */
41907ddc 2484 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
baa09f5f
BW
2485 DRM_ERROR("Unknown GMADR size (%lx)\n",
2486 dev_priv->gtt.mappable_end);
2487 return -ENXIO;
e76e9aeb
BW
2488 }
2489
e76e9aeb
BW
2490 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2491 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
e76e9aeb 2492 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
e76e9aeb 2493
c4ae25ec 2494 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
a93e4161 2495
63340133 2496 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
07749ef3 2497 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
e76e9aeb 2498
63340133 2499 ret = ggtt_probe_common(dev, gtt_size);
e76e9aeb 2500
853ba5d2
BW
2501 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2502 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
777dc5bb
DV
2503 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2504 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
7faf1ab2 2505
e76e9aeb
BW
2506 return ret;
2507}
2508
853ba5d2 2509static void gen6_gmch_remove(struct i915_address_space *vm)
e76e9aeb 2510{
853ba5d2
BW
2511
2512 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
5ed16782 2513
853ba5d2
BW
2514 iounmap(gtt->gsm);
2515 teardown_scratch_page(vm->dev);
644ec02b 2516}
baa09f5f
BW
2517
2518static int i915_gmch_probe(struct drm_device *dev,
2519 size_t *gtt_total,
41907ddc
BW
2520 size_t *stolen,
2521 phys_addr_t *mappable_base,
2522 unsigned long *mappable_end)
baa09f5f
BW
2523{
2524 struct drm_i915_private *dev_priv = dev->dev_private;
2525 int ret;
2526
baa09f5f
BW
2527 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2528 if (!ret) {
2529 DRM_ERROR("failed to set up gmch\n");
2530 return -EIO;
2531 }
2532
41907ddc 2533 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
baa09f5f
BW
2534
2535 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
853ba5d2 2536 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
777dc5bb
DV
2537 dev_priv->gtt.base.bind_vma = i915_ggtt_bind_vma;
2538 dev_priv->gtt.base.unbind_vma = i915_ggtt_unbind_vma;
baa09f5f 2539
c0a7f818
CW
2540 if (unlikely(dev_priv->gtt.do_idle_maps))
2541 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2542
baa09f5f
BW
2543 return 0;
2544}
2545
853ba5d2 2546static void i915_gmch_remove(struct i915_address_space *vm)
baa09f5f
BW
2547{
2548 intel_gmch_remove();
2549}
2550
2551int i915_gem_gtt_init(struct drm_device *dev)
2552{
2553 struct drm_i915_private *dev_priv = dev->dev_private;
2554 struct i915_gtt *gtt = &dev_priv->gtt;
baa09f5f
BW
2555 int ret;
2556
baa09f5f 2557 if (INTEL_INFO(dev)->gen <= 5) {
b2f21b4d 2558 gtt->gtt_probe = i915_gmch_probe;
853ba5d2 2559 gtt->base.cleanup = i915_gmch_remove;
63340133 2560 } else if (INTEL_INFO(dev)->gen < 8) {
b2f21b4d 2561 gtt->gtt_probe = gen6_gmch_probe;
853ba5d2 2562 gtt->base.cleanup = gen6_gmch_remove;
4d15c145 2563 if (IS_HASWELL(dev) && dev_priv->ellc_size)
853ba5d2 2564 gtt->base.pte_encode = iris_pte_encode;
4d15c145 2565 else if (IS_HASWELL(dev))
853ba5d2 2566 gtt->base.pte_encode = hsw_pte_encode;
b2f21b4d 2567 else if (IS_VALLEYVIEW(dev))
853ba5d2 2568 gtt->base.pte_encode = byt_pte_encode;
350ec881
CW
2569 else if (INTEL_INFO(dev)->gen >= 7)
2570 gtt->base.pte_encode = ivb_pte_encode;
b2f21b4d 2571 else
350ec881 2572 gtt->base.pte_encode = snb_pte_encode;
63340133
BW
2573 } else {
2574 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2575 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
baa09f5f
BW
2576 }
2577
853ba5d2 2578 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
b2f21b4d 2579 &gtt->mappable_base, &gtt->mappable_end);
a54c0c27 2580 if (ret)
baa09f5f 2581 return ret;
baa09f5f 2582
853ba5d2
BW
2583 gtt->base.dev = dev;
2584
baa09f5f 2585 /* GMADR is the PCI mmio aperture into the global GTT. */
853ba5d2
BW
2586 DRM_INFO("Memory usable by graphics device = %zdM\n",
2587 gtt->base.total >> 20);
b2f21b4d
BW
2588 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2589 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
5db6c735
DV
2590#ifdef CONFIG_INTEL_IOMMU
2591 if (intel_iommu_gfx_mapped)
2592 DRM_INFO("VT-d active for gfx access\n");
2593#endif
cfa7c862
DV
2594 /*
2595 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2596 * user's requested state against the hardware/driver capabilities. We
2597 * do this now so that we can print out any log messages once rather
2598 * than every time we check intel_enable_ppgtt().
2599 */
2600 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2601 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
baa09f5f
BW
2602
2603 return 0;
2604}
6f65e29a 2605
ec7adb6e
JL
2606static struct i915_vma *
2607__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2608 struct i915_address_space *vm,
2609 const struct i915_ggtt_view *ggtt_view)
6f65e29a 2610{
dabde5c7 2611 struct i915_vma *vma;
6f65e29a 2612
ec7adb6e
JL
2613 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2614 return ERR_PTR(-EINVAL);
e20d2ab7
CW
2615
2616 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
dabde5c7
DC
2617 if (vma == NULL)
2618 return ERR_PTR(-ENOMEM);
ec7adb6e 2619
6f65e29a
BW
2620 INIT_LIST_HEAD(&vma->vma_link);
2621 INIT_LIST_HEAD(&vma->mm_list);
2622 INIT_LIST_HEAD(&vma->exec_list);
2623 vma->vm = vm;
2624 vma->obj = obj;
2625
777dc5bb 2626 if (i915_is_ggtt(vm))
ec7adb6e 2627 vma->ggtt_view = *ggtt_view;
6f65e29a 2628
f7635669
TU
2629 list_add_tail(&vma->vma_link, &obj->vma_list);
2630 if (!i915_is_ggtt(vm))
e07f0552 2631 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
6f65e29a
BW
2632
2633 return vma;
2634}
2635
2636struct i915_vma *
ec7adb6e
JL
2637i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2638 struct i915_address_space *vm)
2639{
2640 struct i915_vma *vma;
2641
2642 vma = i915_gem_obj_to_vma(obj, vm);
2643 if (!vma)
2644 vma = __i915_gem_vma_create(obj, vm,
2645 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
2646
2647 return vma;
2648}
2649
2650struct i915_vma *
2651i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
fe14d5f4 2652 const struct i915_ggtt_view *view)
6f65e29a 2653{
ec7adb6e 2654 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
6f65e29a
BW
2655 struct i915_vma *vma;
2656
ec7adb6e
JL
2657 if (WARN_ON(!view))
2658 return ERR_PTR(-EINVAL);
2659
2660 vma = i915_gem_obj_to_ggtt_view(obj, view);
2661
2662 if (IS_ERR(vma))
2663 return vma;
2664
6f65e29a 2665 if (!vma)
ec7adb6e 2666 vma = __i915_gem_vma_create(obj, ggtt, view);
6f65e29a
BW
2667
2668 return vma;
ec7adb6e 2669
6f65e29a 2670}
fe14d5f4 2671
50470bb0
TU
2672static void
2673rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
2674 struct sg_table *st)
2675{
2676 unsigned int column, row;
2677 unsigned int src_idx;
2678 struct scatterlist *sg = st->sgl;
2679
2680 st->nents = 0;
2681
2682 for (column = 0; column < width; column++) {
2683 src_idx = width * (height - 1) + column;
2684 for (row = 0; row < height; row++) {
2685 st->nents++;
2686 /* We don't need the pages, but need to initialize
2687 * the entries so the sg list can be happily traversed.
2688 * The only thing we need are DMA addresses.
2689 */
2690 sg_set_page(sg, NULL, PAGE_SIZE, 0);
2691 sg_dma_address(sg) = in[src_idx];
2692 sg_dma_len(sg) = PAGE_SIZE;
2693 sg = sg_next(sg);
2694 src_idx -= width;
2695 }
2696 }
2697}
2698
2699static struct sg_table *
2700intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
2701 struct drm_i915_gem_object *obj)
2702{
2703 struct drm_device *dev = obj->base.dev;
2704 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
2705 unsigned long size, pages, rot_pages;
2706 struct sg_page_iter sg_iter;
2707 unsigned long i;
2708 dma_addr_t *page_addr_list;
2709 struct sg_table *st;
2710 unsigned int tile_pitch, tile_height;
2711 unsigned int width_pages, height_pages;
1d00dad5 2712 int ret = -ENOMEM;
50470bb0
TU
2713
2714 pages = obj->base.size / PAGE_SIZE;
2715
2716 /* Calculate tiling geometry. */
2717 tile_height = intel_tile_height(dev, rot_info->pixel_format,
2718 rot_info->fb_modifier);
2719 tile_pitch = PAGE_SIZE / tile_height;
2720 width_pages = DIV_ROUND_UP(rot_info->pitch, tile_pitch);
2721 height_pages = DIV_ROUND_UP(rot_info->height, tile_height);
2722 rot_pages = width_pages * height_pages;
2723 size = rot_pages * PAGE_SIZE;
2724
2725 /* Allocate a temporary list of source pages for random access. */
2726 page_addr_list = drm_malloc_ab(pages, sizeof(dma_addr_t));
2727 if (!page_addr_list)
2728 return ERR_PTR(ret);
2729
2730 /* Allocate target SG list. */
2731 st = kmalloc(sizeof(*st), GFP_KERNEL);
2732 if (!st)
2733 goto err_st_alloc;
2734
2735 ret = sg_alloc_table(st, rot_pages, GFP_KERNEL);
2736 if (ret)
2737 goto err_sg_alloc;
2738
2739 /* Populate source page list from the object. */
2740 i = 0;
2741 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
2742 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
2743 i++;
2744 }
2745
2746 /* Rotate the pages. */
2747 rotate_pages(page_addr_list, width_pages, height_pages, st);
2748
2749 DRM_DEBUG_KMS(
2750 "Created rotated page mapping for object size %lu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages).\n",
2751 size, rot_info->pitch, rot_info->height,
2752 rot_info->pixel_format, width_pages, height_pages,
2753 rot_pages);
2754
2755 drm_free_large(page_addr_list);
2756
2757 return st;
2758
2759err_sg_alloc:
2760 kfree(st);
2761err_st_alloc:
2762 drm_free_large(page_addr_list);
2763
2764 DRM_DEBUG_KMS(
2765 "Failed to create rotated mapping for object size %lu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages)\n",
2766 size, ret, rot_info->pitch, rot_info->height,
2767 rot_info->pixel_format, width_pages, height_pages,
2768 rot_pages);
2769 return ERR_PTR(ret);
2770}
ec7adb6e 2771
50470bb0
TU
2772static inline int
2773i915_get_ggtt_vma_pages(struct i915_vma *vma)
fe14d5f4 2774{
50470bb0
TU
2775 int ret = 0;
2776
fe14d5f4
TU
2777 if (vma->ggtt_view.pages)
2778 return 0;
2779
2780 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
2781 vma->ggtt_view.pages = vma->obj->pages;
50470bb0
TU
2782 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
2783 vma->ggtt_view.pages =
2784 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
fe14d5f4
TU
2785 else
2786 WARN_ONCE(1, "GGTT view %u not implemented!\n",
2787 vma->ggtt_view.type);
2788
2789 if (!vma->ggtt_view.pages) {
ec7adb6e 2790 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
fe14d5f4 2791 vma->ggtt_view.type);
50470bb0
TU
2792 ret = -EINVAL;
2793 } else if (IS_ERR(vma->ggtt_view.pages)) {
2794 ret = PTR_ERR(vma->ggtt_view.pages);
2795 vma->ggtt_view.pages = NULL;
2796 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
2797 vma->ggtt_view.type, ret);
fe14d5f4
TU
2798 }
2799
50470bb0 2800 return ret;
fe14d5f4
TU
2801}
2802
2803/**
2804 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
2805 * @vma: VMA to map
2806 * @cache_level: mapping cache level
2807 * @flags: flags like global or local mapping
2808 *
2809 * DMA addresses are taken from the scatter-gather table of this object (or of
2810 * this VMA in case of non-default GGTT views) and PTE entries set up.
2811 * Note that DMA addresses are also the only part of the SG table we care about.
2812 */
2813int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2814 u32 flags)
2815{
0875546c 2816 u32 bind_flags = 0;
1d335d1b
MK
2817 int ret;
2818
2819 if (vma->vm->allocate_va_range) {
2820 trace_i915_va_alloc(vma->vm, vma->node.start,
2821 vma->node.size,
2822 VM_TO_TRACE_NAME(vma->vm));
2823
2824 ret = vma->vm->allocate_va_range(vma->vm,
2825 vma->node.start,
2826 vma->node.size);
2827 if (ret)
2828 return ret;
2829 }
2830
ec7adb6e 2831 if (i915_is_ggtt(vma->vm)) {
1d335d1b 2832 ret = i915_get_ggtt_vma_pages(vma);
ec7adb6e 2833 if (ret)
0875546c 2834 return 0;
ec7adb6e 2835 }
fe14d5f4 2836
0875546c
DV
2837 if (flags & PIN_GLOBAL)
2838 bind_flags |= GLOBAL_BIND;
2839 if (flags & PIN_USER)
2840 bind_flags |= LOCAL_BIND;
2841
2842 if (flags & PIN_UPDATE)
2843 bind_flags |= vma->bound;
2844 else
2845 bind_flags &= ~vma->bound;
2846
2847 if (bind_flags)
2848 vma->vm->bind_vma(vma, cache_level, bind_flags);
2849
2850 vma->bound |= bind_flags;
fe14d5f4
TU
2851
2852 return 0;
2853}