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