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