i915_gem_gtt.c revision 1.7 1 /* $NetBSD: i915_gem_gtt.c,v 1.7 2018/08/27 06:33:21 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.7 2018/08/27 06:33:21 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 /* Write all the page tables found in the ppgtt structure to incrementing page
1692 * directories. */
1693 static void gen6_write_page_range(struct drm_i915_private *dev_priv,
1694 struct i915_page_directory *pd,
1695 uint32_t start, uint32_t length)
1696 {
1697 struct i915_page_table *pt;
1698 uint32_t pde, temp;
1699
1700 gen6_for_each_pde(pt, pd, start, length, temp, pde)
1701 gen6_write_pde(pd, pde, pt);
1702
1703 /* Make sure write is complete before other code can use this page
1704 * table. Also require for WC mapped PTEs */
1705 #ifdef __NetBSD__
1706 bus_space_read_4(dev_priv->gtt.bst, dev_priv->gtt.bsh, 0);
1707 #else
1708 readl(dev_priv->gtt.gsm);
1709 #endif
1710 }
1711
1712 static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
1713 {
1714 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1715
1716 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
1717 }
1718
1719 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
1720 struct drm_i915_gem_request *req)
1721 {
1722 struct intel_engine_cs *ring = req->ring;
1723 int ret;
1724
1725 /* NB: TLBs must be flushed and invalidated before a switch */
1726 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1727 if (ret)
1728 return ret;
1729
1730 ret = intel_ring_begin(req, 6);
1731 if (ret)
1732 return ret;
1733
1734 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1735 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1736 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1737 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1738 intel_ring_emit(ring, get_pd_offset(ppgtt));
1739 intel_ring_emit(ring, MI_NOOP);
1740 intel_ring_advance(ring);
1741
1742 return 0;
1743 }
1744
1745 static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
1746 struct drm_i915_gem_request *req)
1747 {
1748 struct intel_engine_cs *ring = req->ring;
1749 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1750
1751 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1752 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1753 return 0;
1754 }
1755
1756 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
1757 struct drm_i915_gem_request *req)
1758 {
1759 struct intel_engine_cs *ring = req->ring;
1760 int ret;
1761
1762 /* NB: TLBs must be flushed and invalidated before a switch */
1763 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1764 if (ret)
1765 return ret;
1766
1767 ret = intel_ring_begin(req, 6);
1768 if (ret)
1769 return ret;
1770
1771 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1772 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1773 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1774 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1775 intel_ring_emit(ring, get_pd_offset(ppgtt));
1776 intel_ring_emit(ring, MI_NOOP);
1777 intel_ring_advance(ring);
1778
1779 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1780 if (ring->id != RCS) {
1781 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1782 if (ret)
1783 return ret;
1784 }
1785
1786 return 0;
1787 }
1788
1789 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
1790 struct drm_i915_gem_request *req)
1791 {
1792 struct intel_engine_cs *ring = req->ring;
1793 struct drm_device *dev = ppgtt->base.dev;
1794 struct drm_i915_private *dev_priv = dev->dev_private;
1795
1796
1797 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1798 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1799
1800 POSTING_READ(RING_PP_DIR_DCLV(ring));
1801
1802 return 0;
1803 }
1804
1805 static void gen8_ppgtt_enable(struct drm_device *dev)
1806 {
1807 struct drm_i915_private *dev_priv = dev->dev_private;
1808 struct intel_engine_cs *ring;
1809 int j;
1810
1811 for_each_ring(ring, dev_priv, j) {
1812 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
1813 I915_WRITE(RING_MODE_GEN7(ring),
1814 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
1815 }
1816 }
1817
1818 static void gen7_ppgtt_enable(struct drm_device *dev)
1819 {
1820 struct drm_i915_private *dev_priv = dev->dev_private;
1821 struct intel_engine_cs *ring;
1822 uint32_t ecochk, ecobits;
1823 int i;
1824
1825 ecobits = I915_READ(GAC_ECO_BITS);
1826 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1827
1828 ecochk = I915_READ(GAM_ECOCHK);
1829 if (IS_HASWELL(dev)) {
1830 ecochk |= ECOCHK_PPGTT_WB_HSW;
1831 } else {
1832 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1833 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1834 }
1835 I915_WRITE(GAM_ECOCHK, ecochk);
1836
1837 for_each_ring(ring, dev_priv, i) {
1838 /* GFX_MODE is per-ring on gen7+ */
1839 I915_WRITE(RING_MODE_GEN7(ring),
1840 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1841 }
1842 }
1843
1844 static void gen6_ppgtt_enable(struct drm_device *dev)
1845 {
1846 struct drm_i915_private *dev_priv = dev->dev_private;
1847 uint32_t ecochk, gab_ctl, ecobits;
1848
1849 ecobits = I915_READ(GAC_ECO_BITS);
1850 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1851 ECOBITS_PPGTT_CACHE64B);
1852
1853 gab_ctl = I915_READ(GAB_CTL);
1854 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1855
1856 ecochk = I915_READ(GAM_ECOCHK);
1857 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1858
1859 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1860 }
1861
1862 /* PPGTT support for Sandybdrige/Gen6 and later */
1863 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1864 uint64_t start,
1865 uint64_t length,
1866 bool use_scratch)
1867 {
1868 struct i915_hw_ppgtt *ppgtt =
1869 container_of(vm, struct i915_hw_ppgtt, base);
1870 gen6_pte_t *pt_vaddr, scratch_pte;
1871 unsigned first_entry = start >> PAGE_SHIFT;
1872 unsigned num_entries = length >> PAGE_SHIFT;
1873 unsigned act_pt = first_entry / GEN6_PTES;
1874 unsigned first_pte = first_entry % GEN6_PTES;
1875 unsigned last_pte, i;
1876
1877 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1878 I915_CACHE_LLC, true, 0);
1879
1880 while (num_entries) {
1881 last_pte = first_pte + num_entries;
1882 if (last_pte > GEN6_PTES)
1883 last_pte = GEN6_PTES;
1884
1885 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1886
1887 for (i = first_pte; i < last_pte; i++)
1888 pt_vaddr[i] = scratch_pte;
1889
1890 kunmap_px(ppgtt, pt_vaddr);
1891
1892 num_entries -= last_pte - first_pte;
1893 first_pte = 0;
1894 act_pt++;
1895 }
1896 }
1897
1898 #ifdef __NetBSD__
1899 static void
1900 gen6_ppgtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
1901 uint64_t start, enum i915_cache_level cache_level)
1902 {
1903 struct i915_hw_ppgtt *ppgtt =
1904 container_of(vm, struct i915_hw_ppgtt, base);
1905 gen6_gtt_pte_t *pt_vaddr;
1906 unsigned first_entry = start >> PAGE_SHIFT;
1907 unsigned act_pt = first_entry / GEN6_PTES;
1908 unsigned act_pte = first_entry % GEN6_PTES;
1909 unsigned seg;
1910 int ret;
1911
1912 pt_vaddr = NULL;
1913 KASSERT(0 < dmamap->dm_nsegs);
1914 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
1915 KASSERT(dmamap->dm_segs[seg].ds_len == PAGE_SIZE);
1916 if (pt_vaddr == NULL)
1917 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1918
1919 pt_vaddr[act_pte] =
1920 vm->pte_encode(dmamap->dm_segs[seg].ds_addr,
1921 cache_level, true, flags);
1922
1923 if (++act_pte == GEN6_PTES) {
1924 kunmap_px(ppgtt, pt_vaddr);
1925 pt_vaddr = NULL;
1926 act_pt++;
1927 act_pte = 0;
1928 }
1929 }
1930 if (pt_vaddr)
1931 kunmap_px(ppgtt, pt_vaddr);
1932 }
1933 #else
1934 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1935 struct sg_table *pages,
1936 uint64_t start,
1937 enum i915_cache_level cache_level, u32 flags)
1938 {
1939 struct i915_hw_ppgtt *ppgtt =
1940 container_of(vm, struct i915_hw_ppgtt, base);
1941 gen6_pte_t *pt_vaddr;
1942 unsigned first_entry = start >> PAGE_SHIFT;
1943 unsigned act_pt = first_entry / GEN6_PTES;
1944 unsigned act_pte = first_entry % GEN6_PTES;
1945 struct sg_page_iter sg_iter;
1946
1947 pt_vaddr = NULL;
1948 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
1949 if (pt_vaddr == NULL)
1950 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1951
1952 pt_vaddr[act_pte] =
1953 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
1954 cache_level, true, flags);
1955
1956 if (++act_pte == GEN6_PTES) {
1957 kunmap_px(ppgtt, pt_vaddr);
1958 pt_vaddr = NULL;
1959 act_pt++;
1960 act_pte = 0;
1961 }
1962 }
1963 if (pt_vaddr)
1964 kunmap_px(ppgtt, pt_vaddr);
1965 }
1966 #endif
1967
1968 static int gen6_alloc_va_range(struct i915_address_space *vm,
1969 uint64_t start_in, uint64_t length_in)
1970 {
1971 DECLARE_BITMAP(new_page_tables, I915_PDES);
1972 struct drm_device *dev = vm->dev;
1973 struct drm_i915_private *dev_priv = dev->dev_private;
1974 struct i915_hw_ppgtt *ppgtt =
1975 container_of(vm, struct i915_hw_ppgtt, base);
1976 struct i915_page_table *pt;
1977 uint32_t start, length, start_save, length_save;
1978 uint32_t pde, temp;
1979 int ret;
1980
1981 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1982 return -ENODEV;
1983
1984 start = start_save = start_in;
1985 length = length_save = length_in;
1986
1987 bitmap_zero(new_page_tables, I915_PDES);
1988
1989 /* The allocation is done in two stages so that we can bail out with
1990 * minimal amount of pain. The first stage finds new page tables that
1991 * need allocation. The second stage marks use ptes within the page
1992 * tables.
1993 */
1994 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1995 if (pt != vm->scratch_pt) {
1996 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1997 continue;
1998 }
1999
2000 /* We've already allocated a page table */
2001 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
2002
2003 pt = alloc_pt(dev);
2004 if (IS_ERR(pt)) {
2005 ret = PTR_ERR(pt);
2006 goto unwind_out;
2007 }
2008
2009 gen6_initialize_pt(vm, pt);
2010
2011 ppgtt->pd.page_table[pde] = pt;
2012 __set_bit(pde, new_page_tables);
2013 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
2014 }
2015
2016 start = start_save;
2017 length = length_save;
2018
2019 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
2020 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
2021
2022 bitmap_zero(tmp_bitmap, GEN6_PTES);
2023 bitmap_set(tmp_bitmap, gen6_pte_index(start),
2024 gen6_pte_count(start, length));
2025
2026 if (__test_and_clear_bit(pde, new_page_tables))
2027 gen6_write_pde(&ppgtt->pd, pde, pt);
2028
2029 trace_i915_page_table_entry_map(vm, pde, pt,
2030 gen6_pte_index(start),
2031 gen6_pte_count(start, length),
2032 GEN6_PTES);
2033 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
2034 GEN6_PTES);
2035 }
2036
2037 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
2038
2039 /* Make sure write is complete before other code can use this page
2040 * table. Also require for WC mapped PTEs */
2041 #ifdef __NetBSD__
2042 bus_space_read_4(dev_priv->gtt.bst, dev_priv->gtt.bsh, 0);
2043 #else
2044 readl(dev_priv->gtt.gsm);
2045 #endif
2046
2047 mark_tlbs_dirty(ppgtt);
2048 return 0;
2049
2050 unwind_out:
2051 for_each_set_bit(pde, new_page_tables, I915_PDES) {
2052 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
2053
2054 ppgtt->pd.page_table[pde] = vm->scratch_pt;
2055 free_pt(vm->dev, pt);
2056 }
2057
2058 mark_tlbs_dirty(ppgtt);
2059 return ret;
2060 }
2061
2062 static int gen6_init_scratch(struct i915_address_space *vm)
2063 {
2064 struct drm_device *dev = vm->dev;
2065
2066 vm->scratch_page = alloc_scratch_page(dev);
2067 if (IS_ERR(vm->scratch_page))
2068 return PTR_ERR(vm->scratch_page);
2069
2070 vm->scratch_pt = alloc_pt(dev);
2071 if (IS_ERR(vm->scratch_pt)) {
2072 free_scratch_page(dev, vm->scratch_page);
2073 return PTR_ERR(vm->scratch_pt);
2074 }
2075
2076 gen6_initialize_pt(vm, vm->scratch_pt);
2077
2078 return 0;
2079 }
2080
2081 static void gen6_free_scratch(struct i915_address_space *vm)
2082 {
2083 struct drm_device *dev = vm->dev;
2084
2085 free_pt(dev, vm->scratch_pt);
2086 free_scratch_page(dev, vm->scratch_page);
2087 }
2088
2089 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
2090 {
2091 struct i915_hw_ppgtt *ppgtt =
2092 container_of(vm, struct i915_hw_ppgtt, base);
2093 struct i915_page_table *pt;
2094 uint32_t pde;
2095
2096 drm_mm_remove_node(&ppgtt->node);
2097
2098 gen6_for_all_pdes(pt, ppgtt, pde) {
2099 if (pt != vm->scratch_pt)
2100 free_pt(ppgtt->base.dev, pt);
2101 }
2102
2103 gen6_free_scratch(vm);
2104 }
2105
2106 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
2107 {
2108 struct i915_address_space *vm = &ppgtt->base;
2109 struct drm_device *dev = ppgtt->base.dev;
2110 struct drm_i915_private *dev_priv = dev->dev_private;
2111 bool retried = false;
2112 int ret;
2113
2114 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2115 * allocator works in address space sizes, so it's multiplied by page
2116 * size. We allocate at the top of the GTT to avoid fragmentation.
2117 */
2118 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
2119
2120 ret = gen6_init_scratch(vm);
2121 if (ret)
2122 return ret;
2123
2124 alloc:
2125 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
2126 &ppgtt->node, GEN6_PD_SIZE,
2127 GEN6_PD_ALIGN, 0,
2128 0, dev_priv->gtt.base.total,
2129 DRM_MM_TOPDOWN);
2130 if (ret == -ENOSPC && !retried) {
2131 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
2132 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2133 I915_CACHE_NONE,
2134 0, dev_priv->gtt.base.total,
2135 0);
2136 if (ret)
2137 goto err_out;
2138
2139 retried = true;
2140 goto alloc;
2141 }
2142
2143 if (ret)
2144 goto err_out;
2145
2146
2147 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
2148 DRM_DEBUG("Forced to use aperture for PDEs\n");
2149
2150 return 0;
2151
2152 err_out:
2153 gen6_free_scratch(vm);
2154 return ret;
2155 }
2156
2157 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2158 {
2159 return gen6_ppgtt_allocate_page_directories(ppgtt);
2160 }
2161
2162 static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2163 uint64_t start, uint64_t length)
2164 {
2165 struct i915_page_table *unused;
2166 uint32_t pde, temp;
2167
2168 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
2169 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
2170 }
2171
2172 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
2173 {
2174 struct drm_device *dev = ppgtt->base.dev;
2175 struct drm_i915_private *dev_priv = dev->dev_private;
2176 int ret;
2177
2178 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
2179 if (IS_GEN6(dev)) {
2180 ppgtt->switch_mm = gen6_mm_switch;
2181 } else if (IS_HASWELL(dev)) {
2182 ppgtt->switch_mm = hsw_mm_switch;
2183 } else if (IS_GEN7(dev)) {
2184 ppgtt->switch_mm = gen7_mm_switch;
2185 } else
2186 BUG();
2187
2188 if (intel_vgpu_active(dev))
2189 ppgtt->switch_mm = vgpu_mm_switch;
2190
2191 ret = gen6_ppgtt_alloc(ppgtt);
2192 if (ret)
2193 return ret;
2194
2195 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
2196 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2197 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
2198 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2199 ppgtt->base.bind_vma = ppgtt_bind_vma;
2200 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
2201 ppgtt->base.start = 0;
2202 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
2203 #ifndef __NetBSD__
2204 ppgtt->debug_dump = gen6_dump_ppgtt;
2205 #endif
2206
2207 ppgtt->pd.base.ggtt_offset =
2208 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
2209
2210 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
2211 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
2212
2213 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
2214
2215 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2216
2217 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
2218 ppgtt->node.size >> 20,
2219 ppgtt->node.start / PAGE_SIZE);
2220
2221 DRM_DEBUG("Adding PPGTT at offset %x\n",
2222 ppgtt->pd.base.ggtt_offset << 10);
2223
2224 return 0;
2225 }
2226
2227 static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
2228 {
2229 ppgtt->base.dev = dev;
2230
2231 if (INTEL_INFO(dev)->gen < 8)
2232 return gen6_ppgtt_init(ppgtt);
2233 else
2234 return gen8_ppgtt_init(ppgtt);
2235 }
2236
2237 static void i915_address_space_init(struct i915_address_space *vm,
2238 struct drm_i915_private *dev_priv)
2239 {
2240 drm_mm_init(&vm->mm, vm->start, vm->total);
2241 vm->dev = dev_priv->dev;
2242 INIT_LIST_HEAD(&vm->active_list);
2243 INIT_LIST_HEAD(&vm->inactive_list);
2244 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2245 }
2246
2247 int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
2248 {
2249 struct drm_i915_private *dev_priv = dev->dev_private;
2250 int ret = 0;
2251
2252 ret = __hw_ppgtt_init(dev, ppgtt);
2253 if (ret == 0) {
2254 kref_init(&ppgtt->ref);
2255 i915_address_space_init(&ppgtt->base, dev_priv);
2256 }
2257
2258 return ret;
2259 }
2260
2261 int i915_ppgtt_init_hw(struct drm_device *dev)
2262 {
2263 /* In the case of execlists, PPGTT is enabled by the context descriptor
2264 * and the PDPs are contained within the context itself. We don't
2265 * need to do anything here. */
2266 if (i915.enable_execlists)
2267 return 0;
2268
2269 if (!USES_PPGTT(dev))
2270 return 0;
2271
2272 if (IS_GEN6(dev))
2273 gen6_ppgtt_enable(dev);
2274 else if (IS_GEN7(dev))
2275 gen7_ppgtt_enable(dev);
2276 else if (INTEL_INFO(dev)->gen >= 8)
2277 gen8_ppgtt_enable(dev);
2278 else
2279 MISSING_CASE(INTEL_INFO(dev)->gen);
2280
2281 return 0;
2282 }
2283
2284 int i915_ppgtt_init_ring(struct drm_i915_gem_request *req)
2285 {
2286 struct drm_i915_private *dev_priv = req->ring->dev->dev_private;
2287 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2288
2289 if (i915.enable_execlists)
2290 return 0;
2291
2292 if (!ppgtt)
2293 return 0;
2294
2295 return ppgtt->switch_mm(ppgtt, req);
2296 }
2297
2298 struct i915_hw_ppgtt *
2299 i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
2300 {
2301 struct i915_hw_ppgtt *ppgtt;
2302 int ret;
2303
2304 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2305 if (!ppgtt)
2306 return ERR_PTR(-ENOMEM);
2307
2308 ret = i915_ppgtt_init(dev, ppgtt);
2309 if (ret) {
2310 kfree(ppgtt);
2311 return ERR_PTR(ret);
2312 }
2313
2314 ppgtt->file_priv = fpriv;
2315
2316 trace_i915_ppgtt_create(&ppgtt->base);
2317
2318 return ppgtt;
2319 }
2320
2321 void i915_ppgtt_release(struct kref *kref)
2322 {
2323 struct i915_hw_ppgtt *ppgtt =
2324 container_of(kref, struct i915_hw_ppgtt, ref);
2325
2326 trace_i915_ppgtt_release(&ppgtt->base);
2327
2328 /* vmas should already be unbound */
2329 WARN_ON(!list_empty(&ppgtt->base.active_list));
2330 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
2331
2332 list_del(&ppgtt->base.global_link);
2333 drm_mm_takedown(&ppgtt->base.mm);
2334
2335 ppgtt->base.cleanup(&ppgtt->base);
2336 kfree(ppgtt);
2337 }
2338
2339 extern int intel_iommu_gfx_mapped;
2340 /* Certain Gen5 chipsets require require idling the GPU before
2341 * unmapping anything from the GTT when VT-d is enabled.
2342 */
2343 static bool needs_idle_maps(struct drm_device *dev)
2344 {
2345 #ifdef CONFIG_INTEL_IOMMU
2346 /* Query intel_iommu to see if we need the workaround. Presumably that
2347 * was loaded first.
2348 */
2349 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
2350 return true;
2351 #endif
2352 return false;
2353 }
2354
2355 static bool do_idling(struct drm_i915_private *dev_priv)
2356 {
2357 bool ret = dev_priv->mm.interruptible;
2358
2359 if (unlikely(dev_priv->gtt.do_idle_maps)) {
2360 dev_priv->mm.interruptible = false;
2361 if (i915_gpu_idle(dev_priv->dev)) {
2362 DRM_ERROR("Couldn't idle GPU\n");
2363 /* Wait a bit, in hopes it avoids the hang */
2364 udelay(10);
2365 }
2366 }
2367
2368 return ret;
2369 }
2370
2371 static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
2372 {
2373 if (unlikely(dev_priv->gtt.do_idle_maps))
2374 dev_priv->mm.interruptible = interruptible;
2375 }
2376
2377 void i915_check_and_clear_faults(struct drm_device *dev)
2378 {
2379 struct drm_i915_private *dev_priv = dev->dev_private;
2380 struct intel_engine_cs *ring;
2381 int i;
2382
2383 if (INTEL_INFO(dev)->gen < 6)
2384 return;
2385
2386 for_each_ring(ring, dev_priv, i) {
2387 u32 fault_reg;
2388 fault_reg = I915_READ(RING_FAULT_REG(ring));
2389 if (fault_reg & RING_FAULT_VALID) {
2390 DRM_DEBUG_DRIVER("Unexpected fault\n"
2391 "\tAddr: 0x%08"PRIx32"\n"
2392 "\tAddress space: %s\n"
2393 "\tSource ID: %d\n"
2394 "\tType: %d\n",
2395 fault_reg & PAGE_MASK,
2396 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2397 RING_FAULT_SRCID(fault_reg),
2398 RING_FAULT_FAULT_TYPE(fault_reg));
2399 I915_WRITE(RING_FAULT_REG(ring),
2400 fault_reg & ~RING_FAULT_VALID);
2401 }
2402 }
2403 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
2404 }
2405
2406 static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2407 {
2408 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
2409 intel_gtt_chipset_flush();
2410 } else {
2411 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2412 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2413 }
2414 }
2415
2416 void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2417 {
2418 struct drm_i915_private *dev_priv = dev->dev_private;
2419
2420 /* Don't bother messing with faults pre GEN6 as we have little
2421 * documentation supporting that it's a good idea.
2422 */
2423 if (INTEL_INFO(dev)->gen < 6)
2424 return;
2425
2426 i915_check_and_clear_faults(dev);
2427
2428 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
2429 dev_priv->gtt.base.start,
2430 dev_priv->gtt.base.total,
2431 true);
2432
2433 i915_ggtt_flush(dev_priv);
2434 }
2435
2436 int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
2437 {
2438 #ifdef __NetBSD__
2439 KASSERT(0 < obj->base.size);
2440 /* XXX errno NetBSD->Linux */
2441 return -bus_dmamap_load_raw(obj->base.dev->dmat, obj->igo_dmamap,
2442 obj->pages, obj->igo_nsegs, obj->base.size, BUS_DMA_NOWAIT);
2443 #else
2444 if (!dma_map_sg(&obj->base.dev->pdev->dev,
2445 obj->pages->sgl, obj->pages->nents,
2446 PCI_DMA_BIDIRECTIONAL))
2447 return -ENOSPC;
2448
2449 return 0;
2450 #endif
2451 }
2452
2453 #ifdef __NetBSD__
2454 static uint64_t
2455 gen8_get_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i)
2456 {
2457 CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */
2458 CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
2459 #ifdef _LP64 /* XXX How to detect bus_space_read_8? */
2460 return bus_space_read_8(bst, bsh, 8*i);
2461 #else
2462 /*
2463 * XXX I'm not sure this case can actually happen in practice:
2464 * 32-bit gen8 chipsets?
2465 */
2466 return bus_space_read_4(bst, bsh, 8*i) |
2467 ((uint64_t)bus_space_read_4(bst, bsh, 8*i + 4) << 32);
2468 #endif
2469 }
2470
2471 static inline void
2472 gen8_set_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i,
2473 gen8_gtt_pte_t pte)
2474 {
2475 CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */
2476 CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
2477 #ifdef _LP64 /* XXX How to detect bus_space_write_8? */
2478 bus_space_write_8(bst, bsh, 8*i, pte);
2479 #else
2480 bus_space_write_4(bst, bsh, 8*i, (uint32_t)pte);
2481 bus_space_write_4(bst, bsh, 8*i + 4, (uint32_t)(pte >> 32));
2482 #endif
2483 }
2484 #else
2485 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2486 {
2487 #ifdef writeq
2488 writeq(pte, addr);
2489 #else
2490 iowrite32((u32)pte, addr);
2491 iowrite32(pte >> 32, addr + 4);
2492 #endif
2493 }
2494 #endif
2495
2496 #ifdef __NetBSD__
2497 static void
2498 gen8_ggtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
2499 uint64_t start, enum i915_cache_level level)
2500 {
2501 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2502 unsigned first_entry = start >> PAGE_SHIFT;
2503 const bus_space_tag_t bst = dev_priv->gtt.bst;
2504 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
2505 unsigned i;
2506
2507 KASSERT(0 < dmamap->dm_nsegs);
2508 for (i = 0; i < dmamap->dm_nsegs; i++) {
2509 KASSERT(dmamap->dm_segs[i].ds_len == PAGE_SIZE);
2510 gen8_set_pte(bst, bsh, first_entry + i,
2511 gen8_pte_encode(dmamap->dm_segs[i].ds_addr, level, true));
2512 }
2513 if (0 < i) {
2514 /* Posting read. */
2515 WARN_ON(gen8_get_pte(bst, bsh, (first_entry + i - 1))
2516 != gen8_pte_encode(dmamap->dm_segs[i - 1].ds_addr, level,
2517 true));
2518 }
2519 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2520 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2521 }
2522 #else
2523 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2524 struct sg_table *st,
2525 uint64_t start,
2526 enum i915_cache_level level, u32 unused)
2527 {
2528 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2529 unsigned first_entry = start >> PAGE_SHIFT;
2530 gen8_pte_t __iomem *gtt_entries =
2531 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
2532 int i = 0;
2533 struct sg_page_iter sg_iter;
2534 dma_addr_t addr = 0; /* shut up gcc */
2535
2536 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
2537 addr = sg_dma_address(sg_iter.sg) +
2538 (sg_iter.sg_pgoffset << PAGE_SHIFT);
2539 gen8_set_pte(>t_entries[i],
2540 gen8_pte_encode(addr, level, true));
2541 i++;
2542 }
2543
2544 /*
2545 * XXX: This serves as a posting read to make sure that the PTE has
2546 * actually been updated. There is some concern that even though
2547 * registers and PTEs are within the same BAR that they are potentially
2548 * of NUMA access patterns. Therefore, even with the way we assume
2549 * hardware should work, we must keep this posting read for paranoia.
2550 */
2551 if (i != 0)
2552 WARN_ON(readq(>t_entries[i-1])
2553 != gen8_pte_encode(addr, level, true));
2554
2555 /* This next bit makes the above posting read even more important. We
2556 * want to flush the TLBs only after we're certain all the PTE updates
2557 * have finished.
2558 */
2559 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2560 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2561 }
2562 #endif
2563
2564 /*
2565 * Binds an object into the global gtt with the specified cache level. The object
2566 * will be accessible to the GPU via commands whose operands reference offsets
2567 * within the global GTT as well as accessible by the GPU through the GMADR
2568 * mapped BAR (dev_priv->mm.gtt->gtt).
2569 */
2570 #ifdef __NetBSD__
2571 static void
2572 gen6_ggtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
2573 uint64_t start, enum i915_cache_level level)
2574 {
2575 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2576 unsigned first_entry = start >> PAGE_SHIFT;
2577 const bus_space_tag_t bst = dev_priv->gtt.bst;
2578 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
2579 unsigned i;
2580
2581 KASSERT(0 < dmamap->dm_nsegs);
2582 for (i = 0; i < dmamap->dm_nsegs; i++) {
2583 KASSERT(dmamap->dm_segs[i].ds_len == PAGE_SIZE);
2584 CTASSERT(sizeof(gen6_gtt_pte_t) == 4);
2585 bus_space_write_4(bst, bsh, 4*(first_entry + i),
2586 vm->pte_encode(dmamap->dm_segs[i].ds_addr, level, true));
2587 }
2588 if (0 < i) {
2589 /* Posting read. */
2590 WARN_ON(bus_space_read_4(bst, bsh, 4*(first_entry + i - 1))
2591 != vm->pte_encode(dmamap->dm_segs[i - 1].ds_addr, level,
2592 true));
2593 }
2594 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2595 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2596 }
2597 #else
2598 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2599 struct sg_table *st,
2600 uint64_t start,
2601 enum i915_cache_level level, u32 flags)
2602 {
2603 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2604 unsigned first_entry = start >> PAGE_SHIFT;
2605 gen6_pte_t __iomem *gtt_entries =
2606 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
2607 int i = 0;
2608 struct sg_page_iter sg_iter;
2609 dma_addr_t addr = 0;
2610
2611 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
2612 addr = sg_page_iter_dma_address(&sg_iter);
2613 iowrite32(vm->pte_encode(addr, level, true, flags), >t_entries[i]);
2614 i++;
2615 }
2616
2617 /* XXX: This serves as a posting read to make sure that the PTE has
2618 * actually been updated. There is some concern that even though
2619 * registers and PTEs are within the same BAR that they are potentially
2620 * of NUMA access patterns. Therefore, even with the way we assume
2621 * hardware should work, we must keep this posting read for paranoia.
2622 */
2623 if (i != 0) {
2624 unsigned long gtt = readl(>t_entries[i-1]);
2625 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
2626 }
2627
2628 /* This next bit makes the above posting read even more important. We
2629 * want to flush the TLBs only after we're certain all the PTE updates
2630 * have finished.
2631 */
2632 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2633 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2634 }
2635 #endif
2636
2637 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2638 uint64_t start,
2639 uint64_t length,
2640 bool use_scratch)
2641 {
2642 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2643 unsigned first_entry = start >> PAGE_SHIFT;
2644 unsigned num_entries = length >> PAGE_SHIFT;
2645 #ifdef __NetBSD__
2646 const bus_space_tag_t bst = dev_priv->gtt.bst;
2647 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
2648 gen8_pte_t scratch_pte;
2649 #else
2650 gen8_pte_t scratch_pte, __iomem *gtt_base =
2651 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
2652 #endif
2653 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
2654 int i;
2655
2656 if (WARN(num_entries > max_entries,
2657 "First entry = %d; Num entries = %d (max=%d)\n",
2658 first_entry, num_entries, max_entries))
2659 num_entries = max_entries;
2660
2661 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
2662 I915_CACHE_LLC,
2663 use_scratch);
2664 #ifdef __NetBSD__
2665 CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
2666 for (i = 0; i < num_entries; i++)
2667 gen8_set_pte(bst, bsh, first_entry + i, scratch_pte);
2668 (void)gen8_get_pte(bst, bsh, first_entry);
2669 #else
2670 for (i = 0; i < num_entries; i++)
2671 gen8_set_pte(>t_base[i], scratch_pte);
2672 readl(gtt_base);
2673 #endif
2674 }
2675
2676 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2677 uint64_t start,
2678 uint64_t length,
2679 bool use_scratch)
2680 {
2681 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2682 unsigned first_entry = start >> PAGE_SHIFT;
2683 unsigned num_entries = length >> PAGE_SHIFT;
2684 #ifdef __NetBSD__
2685 const bus_space_tag_t bst = dev_priv->gtt.bst;
2686 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
2687 gen8_pte_t scratch_pte;
2688 #else
2689 gen6_pte_t scratch_pte, __iomem *gtt_base =
2690 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
2691 #endif
2692 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
2693 int i;
2694
2695 if (WARN(num_entries > max_entries,
2696 "First entry = %d; Num entries = %d (max=%d)\n",
2697 first_entry, num_entries, max_entries))
2698 num_entries = max_entries;
2699
2700 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
2701 I915_CACHE_LLC, use_scratch, 0);
2702
2703 #ifdef __NetBSD__
2704 CTASSERT(sizeof(gen6_gtt_pte_t) == 4);
2705 for (i = 0; i < num_entries; i++)
2706 bus_space_write_4(bst, bsh, 4*(first_entry + i), scratch_pte);
2707 (void)bus_space_read_4(bst, bsh, 4*first_entry);
2708 #else
2709 for (i = 0; i < num_entries; i++)
2710 iowrite32(scratch_pte, >t_base[i]);
2711 readl(gtt_base);
2712 #endif
2713 }
2714
2715 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2716 #ifdef __NetBSD__
2717 bus_dmamap_t pages,
2718 #else
2719 struct sg_table *pages,
2720 #endif
2721 uint64_t start,
2722 enum i915_cache_level cache_level, u32 unused)
2723 {
2724 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2725 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2726
2727 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
2728 }
2729
2730 static void i915_ggtt_clear_range(struct i915_address_space *vm,
2731 uint64_t start,
2732 uint64_t length,
2733 bool unused)
2734 {
2735 unsigned first_entry = start >> PAGE_SHIFT;
2736 unsigned num_entries = length >> PAGE_SHIFT;
2737 intel_gtt_clear_range(first_entry, num_entries);
2738 }
2739
2740 static int ggtt_bind_vma(struct i915_vma *vma,
2741 enum i915_cache_level cache_level,
2742 u32 flags)
2743 {
2744 struct drm_i915_gem_object *obj = vma->obj;
2745 u32 pte_flags = 0;
2746 int ret;
2747
2748 ret = i915_get_ggtt_vma_pages(vma);
2749 if (ret)
2750 return ret;
2751
2752 /* Currently applicable only to VLV */
2753 if (obj->gt_ro)
2754 pte_flags |= PTE_READ_ONLY;
2755
2756 vma->vm->insert_entries(vma->vm, vma->ggtt_view.pages,
2757 vma->node.start,
2758 cache_level, pte_flags);
2759
2760 /*
2761 * Without aliasing PPGTT there's no difference between
2762 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2763 * upgrade to both bound if we bind either to avoid double-binding.
2764 */
2765 vma->bound |= GLOBAL_BIND | LOCAL_BIND;
2766
2767 return 0;
2768 }
2769
2770 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2771 enum i915_cache_level cache_level,
2772 u32 flags)
2773 {
2774 struct drm_device *dev = vma->vm->dev;
2775 struct drm_i915_private *dev_priv = dev->dev_private;
2776 struct drm_i915_gem_object *obj = vma->obj;
2777 #ifdef __NetBSD__
2778 bus_dmamap_t pages = obj->pages;
2779 #else
2780 struct sg_table *pages = obj->pages;
2781 #endif
2782 u32 pte_flags = 0;
2783 int ret;
2784
2785 ret = i915_get_ggtt_vma_pages(vma);
2786 if (ret)
2787 return ret;
2788 pages = vma->ggtt_view.pages;
2789
2790 /* Currently applicable only to VLV */
2791 if (obj->gt_ro)
2792 pte_flags |= PTE_READ_ONLY;
2793
2794
2795 if (flags & GLOBAL_BIND) {
2796 vma->vm->insert_entries(vma->vm, pages,
2797 vma->node.start,
2798 cache_level, pte_flags);
2799 }
2800
2801 if (flags & LOCAL_BIND) {
2802 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2803 appgtt->base.insert_entries(&appgtt->base, pages,
2804 vma->node.start,
2805 cache_level, pte_flags);
2806 }
2807
2808 return 0;
2809 }
2810
2811 static void ggtt_unbind_vma(struct i915_vma *vma)
2812 {
2813 struct drm_device *dev = vma->vm->dev;
2814 struct drm_i915_private *dev_priv = dev->dev_private;
2815 struct drm_i915_gem_object *obj = vma->obj;
2816 const uint64_t size = min_t(uint64_t,
2817 obj->base.size,
2818 vma->node.size);
2819
2820 if (vma->bound & GLOBAL_BIND) {
2821 vma->vm->clear_range(vma->vm,
2822 vma->node.start,
2823 size,
2824 true);
2825 }
2826
2827 if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
2828 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2829
2830 appgtt->base.clear_range(&appgtt->base,
2831 vma->node.start,
2832 size,
2833 true);
2834 }
2835 }
2836
2837 void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2838 {
2839 struct drm_device *dev = obj->base.dev;
2840 struct drm_i915_private *dev_priv = dev->dev_private;
2841 bool interruptible;
2842
2843 interruptible = do_idling(dev_priv);
2844
2845 #ifdef __NetBSD__
2846 bus_dmamap_unload(dev->dmat, obj->igo_dmamap);
2847 #else
2848 dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
2849 PCI_DMA_BIDIRECTIONAL);
2850 #endif
2851
2852 undo_idling(dev_priv, interruptible);
2853 }
2854
2855 static void i915_gtt_color_adjust(struct drm_mm_node *node,
2856 unsigned long color,
2857 u64 *start,
2858 u64 *end)
2859 {
2860 if (node->color != color)
2861 *start += 4096;
2862
2863 if (!list_empty(&node->node_list)) {
2864 node = list_entry(node->node_list.next,
2865 struct drm_mm_node,
2866 node_list);
2867 if (node->allocated && node->color != color)
2868 *end -= 4096;
2869 }
2870 }
2871
2872 static int i915_gem_setup_global_gtt(struct drm_device *dev,
2873 u64 start,
2874 u64 mappable_end,
2875 u64 end)
2876 {
2877 /* Let GEM Manage all of the aperture.
2878 *
2879 * However, leave one page at the end still bound to the scratch page.
2880 * There are a number of places where the hardware apparently prefetches
2881 * past the end of the object, and we've seen multiple hangs with the
2882 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2883 * aperture. One page should be enough to keep any prefetching inside
2884 * of the aperture.
2885 */
2886 struct drm_i915_private *dev_priv = dev->dev_private;
2887 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
2888 struct drm_mm_node *entry;
2889 struct drm_i915_gem_object *obj;
2890 unsigned long hole_start, hole_end;
2891 int ret;
2892
2893 BUG_ON(mappable_end > end);
2894
2895 ggtt_vm->start = start;
2896
2897 /* Subtract the guard page before address space initialization to
2898 * shrink the range used by drm_mm */
2899 ggtt_vm->total = end - start - PAGE_SIZE;
2900 i915_address_space_init(ggtt_vm, dev_priv);
2901 ggtt_vm->total += PAGE_SIZE;
2902
2903 if (intel_vgpu_active(dev)) {
2904 ret = intel_vgt_balloon(dev);
2905 if (ret)
2906 return ret;
2907 }
2908
2909 if (!HAS_LLC(dev))
2910 ggtt_vm->mm.color_adjust = i915_gtt_color_adjust;
2911
2912 /* Mark any preallocated objects as occupied */
2913 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
2914 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
2915
2916 DRM_DEBUG_KMS("reserving preallocated space: %llx + %zx\n",
2917 i915_gem_obj_ggtt_offset(obj), obj->base.size);
2918
2919 WARN_ON(i915_gem_obj_ggtt_bound(obj));
2920 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
2921 if (ret) {
2922 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2923 return ret;
2924 }
2925 vma->bound |= GLOBAL_BIND;
2926 __i915_vma_set_map_and_fenceable(vma);
2927 list_add_tail(&vma->mm_list, &ggtt_vm->inactive_list);
2928 }
2929
2930 /* Clear any non-preallocated blocks */
2931 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
2932 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2933 hole_start, hole_end);
2934 ggtt_vm->clear_range(ggtt_vm, hole_start,
2935 hole_end - hole_start, true);
2936 }
2937
2938 /* And finally clear the reserved guard page */
2939 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
2940
2941 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2942 struct i915_hw_ppgtt *ppgtt;
2943
2944 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2945 if (!ppgtt)
2946 return -ENOMEM;
2947
2948 ret = __hw_ppgtt_init(dev, ppgtt);
2949 if (ret) {
2950 ppgtt->base.cleanup(&ppgtt->base);
2951 kfree(ppgtt);
2952 return ret;
2953 }
2954
2955 if (ppgtt->base.allocate_va_range)
2956 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2957 ppgtt->base.total);
2958 if (ret) {
2959 ppgtt->base.cleanup(&ppgtt->base);
2960 kfree(ppgtt);
2961 return ret;
2962 }
2963
2964 ppgtt->base.clear_range(&ppgtt->base,
2965 ppgtt->base.start,
2966 ppgtt->base.total,
2967 true);
2968
2969 dev_priv->mm.aliasing_ppgtt = ppgtt;
2970 WARN_ON(dev_priv->gtt.base.bind_vma != ggtt_bind_vma);
2971 dev_priv->gtt.base.bind_vma = aliasing_gtt_bind_vma;
2972 }
2973
2974 return 0;
2975 }
2976
2977 void i915_gem_init_global_gtt(struct drm_device *dev)
2978 {
2979 struct drm_i915_private *dev_priv = dev->dev_private;
2980 u64 gtt_size, mappable_size;
2981
2982 gtt_size = dev_priv->gtt.base.total;
2983 mappable_size = dev_priv->gtt.mappable_end;
2984
2985 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
2986 }
2987
2988 void i915_global_gtt_cleanup(struct drm_device *dev)
2989 {
2990 struct drm_i915_private *dev_priv = dev->dev_private;
2991 struct i915_address_space *vm = &dev_priv->gtt.base;
2992
2993 if (dev_priv->mm.aliasing_ppgtt) {
2994 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2995
2996 ppgtt->base.cleanup(&ppgtt->base);
2997 kfree(ppgtt);
2998 }
2999
3000 if (drm_mm_initialized(&vm->mm)) {
3001 if (intel_vgpu_active(dev))
3002 intel_vgt_deballoon();
3003
3004 drm_mm_takedown(&vm->mm);
3005 list_del(&vm->global_link);
3006 }
3007
3008 vm->cleanup(vm);
3009 }
3010
3011 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
3012 {
3013 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
3014 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
3015 return snb_gmch_ctl << 20;
3016 }
3017
3018 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
3019 {
3020 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
3021 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
3022 if (bdw_gmch_ctl)
3023 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
3024
3025 #ifdef CONFIG_X86_32
3026 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
3027 if (bdw_gmch_ctl > 4)
3028 bdw_gmch_ctl = 4;
3029 #endif
3030
3031 return bdw_gmch_ctl << 20;
3032 }
3033
3034 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
3035 {
3036 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
3037 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
3038
3039 if (gmch_ctrl)
3040 return 1 << (20 + gmch_ctrl);
3041
3042 return 0;
3043 }
3044
3045 static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
3046 {
3047 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
3048 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
3049 return snb_gmch_ctl << 25; /* 32 MB units */
3050 }
3051
3052 static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
3053 {
3054 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
3055 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
3056 return bdw_gmch_ctl << 25; /* 32 MB units */
3057 }
3058
3059 static size_t chv_get_stolen_size(u16 gmch_ctrl)
3060 {
3061 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
3062 gmch_ctrl &= SNB_GMCH_GMS_MASK;
3063
3064 /*
3065 * 0x0 to 0x10: 32MB increments starting at 0MB
3066 * 0x11 to 0x16: 4MB increments starting at 8MB
3067 * 0x17 to 0x1d: 4MB increments start at 36MB
3068 */
3069 if (gmch_ctrl < 0x11)
3070 return gmch_ctrl << 25;
3071 else if (gmch_ctrl < 0x17)
3072 return (gmch_ctrl - 0x11 + 2) << 22;
3073 else
3074 return (gmch_ctrl - 0x17 + 9) << 22;
3075 }
3076
3077 static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
3078 {
3079 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
3080 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
3081
3082 if (gen9_gmch_ctl < 0xf0)
3083 return gen9_gmch_ctl << 25; /* 32 MB units */
3084 else
3085 /* 4MB increments starting at 0xf0 for 4MB */
3086 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
3087 }
3088
3089 static int ggtt_probe_common(struct drm_device *dev,
3090 size_t gtt_size)
3091 {
3092 struct drm_i915_private *dev_priv = dev->dev_private;
3093 struct i915_page_scratch *scratch_page;
3094 phys_addr_t gtt_phys_addr;
3095
3096 /* For Modern GENs the PTEs and register space are split in the BAR */
3097 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
3098 (pci_resource_len(dev->pdev, 0) / 2);
3099
3100 #ifdef __NetBSD__
3101 dev_priv->gtt.bst = dev->pdev->pd_pa.pa_memt;
3102 /* XXX errno NetBSD->Linux */
3103 ret = -bus_space_map(dev_priv->gtt.bst, gtt_phys_addr, gtt_size,
3104 IS_PROXTON(dev) ? 0 : BUS_SPACE_MAP_PREFETCHABLE,
3105 &dev_priv->gtt.bsh);
3106 if (ret) {
3107 DRM_ERROR("Failed to map the graphics translation table: %d\n",
3108 ret);
3109 return ret;
3110 }
3111 dev_priv->gtt.size = gtt_size;
3112 #else
3113 /*
3114 * On BXT writes larger than 64 bit to the GTT pagetable range will be
3115 * dropped. For WC mappings in general we have 64 byte burst writes
3116 * when the WC buffer is flushed, so we can't use it, but have to
3117 * resort to an uncached mapping. The WC issue is easily caught by the
3118 * readback check when writing GTT PTE entries.
3119 */
3120 if (IS_BROXTON(dev))
3121 dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size);
3122 else
3123 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
3124 if (!dev_priv->gtt.gsm) {
3125 DRM_ERROR("Failed to map the gtt page table\n");
3126 return -ENOMEM;
3127 }
3128 #endif
3129
3130 scratch_page = alloc_scratch_page(dev);
3131 if (IS_ERR(scratch_page)) {
3132 DRM_ERROR("Scratch setup failed\n");
3133 /* iounmap will also get called at remove, but meh */
3134 #ifdef __NetBSD__
3135 bus_space_unmap(dev_priv->gtt.bst, dev_priv->gtt.bsh,
3136 dev_priv->gtt.size);
3137 #else
3138 iounmap(dev_priv->gtt.gsm);
3139 #endif
3140 return PTR_ERR(scratch_page);
3141 }
3142
3143 dev_priv->gtt.base.scratch_page = scratch_page;
3144
3145 return 0;
3146 }
3147
3148 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3149 * bits. When using advanced contexts each context stores its own PAT, but
3150 * writing this data shouldn't be harmful even in those cases. */
3151 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
3152 {
3153 uint64_t pat;
3154
3155 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3156 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3157 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3158 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3159 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3160 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3161 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3162 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3163
3164 if (!USES_PPGTT(dev_priv->dev))
3165 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3166 * so RTL will always use the value corresponding to
3167 * pat_sel = 000".
3168 * So let's disable cache for GGTT to avoid screen corruptions.
3169 * MOCS still can be used though.
3170 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3171 * before this patch, i.e. the same uncached + snooping access
3172 * like on gen6/7 seems to be in effect.
3173 * - So this just fixes blitter/render access. Again it looks
3174 * like it's not just uncached access, but uncached + snooping.
3175 * So we can still hold onto all our assumptions wrt cpu
3176 * clflushing on LLC machines.
3177 */
3178 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3179
3180 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3181 * write would work. */
3182 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3183 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3184 }
3185
3186 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3187 {
3188 uint64_t pat;
3189
3190 /*
3191 * Map WB on BDW to snooped on CHV.
3192 *
3193 * Only the snoop bit has meaning for CHV, the rest is
3194 * ignored.
3195 *
3196 * The hardware will never snoop for certain types of accesses:
3197 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3198 * - PPGTT page tables
3199 * - some other special cycles
3200 *
3201 * As with BDW, we also need to consider the following for GT accesses:
3202 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3203 * so RTL will always use the value corresponding to
3204 * pat_sel = 000".
3205 * Which means we must set the snoop bit in PAT entry 0
3206 * in order to keep the global status page working.
3207 */
3208 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3209 GEN8_PPAT(1, 0) |
3210 GEN8_PPAT(2, 0) |
3211 GEN8_PPAT(3, 0) |
3212 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3213 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3214 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3215 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3216
3217 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3218 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3219 }
3220
3221 static int gen8_gmch_probe(struct drm_device *dev,
3222 u64 *gtt_total,
3223 size_t *stolen,
3224 phys_addr_t *mappable_base,
3225 u64 *mappable_end)
3226 {
3227 struct drm_i915_private *dev_priv = dev->dev_private;
3228 u64 gtt_size;
3229 u16 snb_gmch_ctl;
3230 int ret;
3231
3232 /* TODO: We're not aware of mappable constraints on gen8 yet */
3233 *mappable_base = pci_resource_start(dev->pdev, 2);
3234 *mappable_end = pci_resource_len(dev->pdev, 2);
3235
3236 #ifndef __NetBSD__
3237 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
3238 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
3239 #endif
3240
3241 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3242
3243 if (INTEL_INFO(dev)->gen >= 9) {
3244 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
3245 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
3246 } else if (IS_CHERRYVIEW(dev)) {
3247 *stolen = chv_get_stolen_size(snb_gmch_ctl);
3248 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
3249 } else {
3250 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
3251 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
3252 }
3253
3254 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
3255
3256 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3257 chv_setup_private_ppat(dev_priv);
3258 else
3259 bdw_setup_private_ppat(dev_priv);
3260
3261 ret = ggtt_probe_common(dev, gtt_size);
3262
3263 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
3264 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
3265 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
3266 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
3267
3268 /* XXX 39-bit addresses? Really? See pci_set_dma_mask above... */
3269 dev_priv->gtt.max_paddr = __BITS(38, 0);
3270
3271 return ret;
3272 }
3273
3274 static int gen6_gmch_probe(struct drm_device *dev,
3275 u64 *gtt_total,
3276 size_t *stolen,
3277 phys_addr_t *mappable_base,
3278 u64 *mappable_end)
3279 {
3280 struct drm_i915_private *dev_priv = dev->dev_private;
3281 unsigned int gtt_size;
3282 u16 snb_gmch_ctl;
3283 int ret;
3284
3285 *mappable_base = pci_resource_start(dev->pdev, 2);
3286 *mappable_end = pci_resource_len(dev->pdev, 2);
3287
3288 /* 64/512MB is the current min/max we actually know of, but this is just
3289 * a coarse sanity check.
3290 */
3291 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
3292 DRM_ERROR("Unknown GMADR size (%llx)\n",
3293 dev_priv->gtt.mappable_end);
3294 return -ENXIO;
3295 }
3296
3297 #ifndef __NetBSD__
3298 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
3299 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
3300 #endif
3301 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3302
3303 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
3304
3305 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
3306 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
3307
3308 ret = ggtt_probe_common(dev, gtt_size);
3309
3310 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
3311 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
3312 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
3313 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
3314
3315 dev_priv->gtt.max_paddr = __BITS(39, 0);
3316
3317 return ret;
3318 }
3319
3320 static void gen6_gmch_remove(struct i915_address_space *vm)
3321 {
3322 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
3323
3324 #ifdef __NetBSD__
3325 bus_space_unmap(gtt->bst, gtt->bsh, gtt->size);
3326 #else
3327 iounmap(gtt->gsm);
3328 #endif
3329 free_scratch_page(vm->dev, vm->scratch_page);
3330 }
3331
3332 static int i915_gmch_probe(struct drm_device *dev,
3333 u64 *gtt_total,
3334 size_t *stolen,
3335 phys_addr_t *mappable_base,
3336 u64 *mappable_end)
3337 {
3338 struct drm_i915_private *dev_priv = dev->dev_private;
3339 int ret;
3340
3341 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
3342 if (!ret) {
3343 DRM_ERROR("failed to set up gmch\n");
3344 return -EIO;
3345 }
3346
3347 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
3348
3349 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
3350 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
3351 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
3352 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
3353 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
3354
3355 if (unlikely(dev_priv->gtt.do_idle_maps))
3356 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3357
3358 if (INTEL_INFO(dev)->gen <= 2)
3359 dev_priv->gtt.max_paddr = __BITS(29, 0);
3360 else if ((INTEL_INFO(dev)->gen <= 3) ||
3361 IS_BROADWATER(dev) || IS_CRESTLINE(dev))
3362 dev_priv->gtt.max_paddr = __BITS(31, 0);
3363 else if (INTEL_INFO(dev)->gen <= 5)
3364 dev_priv->gtt.max_paddr = __BITS(35, 0);
3365 else
3366 dev_priv->gtt.max_paddr = __BITS(39, 0);
3367
3368 return 0;
3369 }
3370
3371 static void i915_gmch_remove(struct i915_address_space *vm)
3372 {
3373 intel_gmch_remove();
3374 }
3375
3376 int i915_gem_gtt_init(struct drm_device *dev)
3377 {
3378 struct drm_i915_private *dev_priv = dev->dev_private;
3379 struct i915_gtt *gtt = &dev_priv->gtt;
3380 int ret;
3381
3382 if (INTEL_INFO(dev)->gen <= 5) {
3383 gtt->gtt_probe = i915_gmch_probe;
3384 gtt->base.cleanup = i915_gmch_remove;
3385 } else if (INTEL_INFO(dev)->gen < 8) {
3386 gtt->gtt_probe = gen6_gmch_probe;
3387 gtt->base.cleanup = gen6_gmch_remove;
3388 if (IS_HASWELL(dev) && dev_priv->ellc_size)
3389 gtt->base.pte_encode = iris_pte_encode;
3390 else if (IS_HASWELL(dev))
3391 gtt->base.pte_encode = hsw_pte_encode;
3392 else if (IS_VALLEYVIEW(dev))
3393 gtt->base.pte_encode = byt_pte_encode;
3394 else if (INTEL_INFO(dev)->gen >= 7)
3395 gtt->base.pte_encode = ivb_pte_encode;
3396 else
3397 gtt->base.pte_encode = snb_pte_encode;
3398 } else {
3399 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
3400 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
3401 }
3402
3403 gtt->base.dev = dev;
3404
3405 ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size,
3406 >t->mappable_base, >t->mappable_end);
3407 if (ret)
3408 return ret;
3409
3410 #ifdef __NetBSD__
3411 dev_priv->gtt.pgfl = x86_select_freelist(dev_priv->gtt.max_paddr);
3412 ret = drm_limit_dma_space(dev, 0, dev_priv->gtt.max_paddr);
3413 if (ret) {
3414 DRM_ERROR("Unable to limit DMA paddr allocations: %d!\n", ret);
3415 gtt->base.cleanup(>t->base);
3416 return ret;
3417 }
3418 #endif
3419
3420 /* GMADR is the PCI mmio aperture into the global GTT. */
3421 DRM_INFO("Memory usable by graphics device = %lluM\n",
3422 gtt->base.total >> 20);
3423 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", gtt->mappable_end >> 20);
3424 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
3425 #ifdef CONFIG_INTEL_IOMMU
3426 if (intel_iommu_gfx_mapped)
3427 DRM_INFO("VT-d active for gfx access\n");
3428 #endif
3429 /*
3430 * i915.enable_ppgtt is read-only, so do an early pass to validate the
3431 * user's requested state against the hardware/driver capabilities. We
3432 * do this now so that we can print out any log messages once rather
3433 * than every time we check intel_enable_ppgtt().
3434 */
3435 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
3436 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
3437
3438 return 0;
3439 }
3440
3441 void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3442 {
3443 struct drm_i915_private *dev_priv = dev->dev_private;
3444 struct drm_i915_gem_object *obj;
3445 struct i915_address_space *vm;
3446 struct i915_vma *vma;
3447 bool flush;
3448
3449 i915_check_and_clear_faults(dev);
3450
3451 /* First fill our portion of the GTT with scratch pages */
3452 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
3453 dev_priv->gtt.base.start,
3454 dev_priv->gtt.base.total,
3455 true);
3456
3457 /* Cache flush objects bound into GGTT and rebind them. */
3458 vm = &dev_priv->gtt.base;
3459 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
3460 flush = false;
3461 list_for_each_entry(vma, &obj->vma_list, vma_link) {
3462 if (vma->vm != vm)
3463 continue;
3464
3465 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3466 PIN_UPDATE));
3467
3468 flush = true;
3469 }
3470
3471 if (flush)
3472 i915_gem_clflush_object(obj, obj->pin_display);
3473 }
3474
3475 if (INTEL_INFO(dev)->gen >= 8) {
3476 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3477 chv_setup_private_ppat(dev_priv);
3478 else
3479 bdw_setup_private_ppat(dev_priv);
3480
3481 return;
3482 }
3483
3484 if (USES_PPGTT(dev)) {
3485 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3486 /* TODO: Perhaps it shouldn't be gen6 specific */
3487
3488 struct i915_hw_ppgtt *ppgtt =
3489 container_of(vm, struct i915_hw_ppgtt,
3490 base);
3491
3492 if (i915_is_ggtt(vm))
3493 ppgtt = dev_priv->mm.aliasing_ppgtt;
3494
3495 gen6_write_page_range(dev_priv, &ppgtt->pd,
3496 0, ppgtt->base.total);
3497 }
3498 }
3499
3500 i915_ggtt_flush(dev_priv);
3501 }
3502
3503 static struct i915_vma *
3504 __i915_gem_vma_create(struct drm_i915_gem_object *obj,
3505 struct i915_address_space *vm,
3506 const struct i915_ggtt_view *ggtt_view)
3507 {
3508 struct i915_vma *vma;
3509
3510 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
3511 return ERR_PTR(-EINVAL);
3512
3513 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
3514 if (vma == NULL)
3515 return ERR_PTR(-ENOMEM);
3516
3517 INIT_LIST_HEAD(&vma->vma_link);
3518 INIT_LIST_HEAD(&vma->mm_list);
3519 INIT_LIST_HEAD(&vma->exec_list);
3520 vma->vm = vm;
3521 vma->obj = obj;
3522
3523 if (i915_is_ggtt(vm))
3524 vma->ggtt_view = *ggtt_view;
3525
3526 list_add_tail(&vma->vma_link, &obj->vma_list);
3527 if (!i915_is_ggtt(vm))
3528 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
3529
3530 return vma;
3531 }
3532
3533 struct i915_vma *
3534 i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
3535 struct i915_address_space *vm)
3536 {
3537 struct i915_vma *vma;
3538
3539 vma = i915_gem_obj_to_vma(obj, vm);
3540 if (!vma)
3541 vma = __i915_gem_vma_create(obj, vm,
3542 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
3543
3544 return vma;
3545 }
3546
3547 struct i915_vma *
3548 i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
3549 const struct i915_ggtt_view *view)
3550 {
3551 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
3552 struct i915_vma *vma;
3553
3554 if (WARN_ON(!view))
3555 return ERR_PTR(-EINVAL);
3556
3557 vma = i915_gem_obj_to_ggtt_view(obj, view);
3558
3559 if (IS_ERR(vma))
3560 return vma;
3561
3562 if (!vma)
3563 vma = __i915_gem_vma_create(obj, ggtt, view);
3564
3565 return vma;
3566
3567 }
3568
3569 static struct scatterlist *
3570 rotate_pages(dma_addr_t *in, unsigned int offset,
3571 unsigned int width, unsigned int height,
3572 struct sg_table *st, struct scatterlist *sg)
3573 {
3574 #ifdef __NetBSD__
3575 panic("XXX");
3576 #else
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 #endif
3603 }
3604
3605 static struct sg_table *
3606 intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
3607 struct drm_i915_gem_object *obj)
3608 {
3609 #ifdef __NetBSD__
3610 panic("XXX");
3611 #else
3612 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
3613 unsigned int size_pages = rot_info->size >> PAGE_SHIFT;
3614 unsigned int size_pages_uv;
3615 struct sg_page_iter sg_iter;
3616 unsigned long i;
3617 dma_addr_t *page_addr_list;
3618 struct sg_table *st;
3619 unsigned int uv_start_page;
3620 struct scatterlist *sg;
3621 int ret = -ENOMEM;
3622
3623 /* Allocate a temporary list of source pages for random access. */
3624 page_addr_list = drm_malloc_ab(obj->base.size / PAGE_SIZE,
3625 sizeof(dma_addr_t));
3626 if (!page_addr_list)
3627 return ERR_PTR(ret);
3628
3629 /* Account for UV plane with NV12. */
3630 if (rot_info->pixel_format == DRM_FORMAT_NV12)
3631 size_pages_uv = rot_info->size_uv >> PAGE_SHIFT;
3632 else
3633 size_pages_uv = 0;
3634
3635 /* Allocate target SG list. */
3636 st = kmalloc(sizeof(*st), GFP_KERNEL);
3637 if (!st)
3638 goto err_st_alloc;
3639
3640 ret = sg_alloc_table(st, size_pages + size_pages_uv, GFP_KERNEL);
3641 if (ret)
3642 goto err_sg_alloc;
3643
3644 /* Populate source page list from the object. */
3645 i = 0;
3646 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
3647 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
3648 i++;
3649 }
3650
3651 /* Rotate the pages. */
3652 sg = rotate_pages(page_addr_list, 0,
3653 rot_info->width_pages, rot_info->height_pages,
3654 st, NULL);
3655
3656 /* Append the UV plane if NV12. */
3657 if (rot_info->pixel_format == DRM_FORMAT_NV12) {
3658 uv_start_page = size_pages;
3659
3660 /* Check for tile-row un-alignment. */
3661 if (offset_in_page(rot_info->uv_offset))
3662 uv_start_page--;
3663
3664 rot_info->uv_start_page = uv_start_page;
3665
3666 rotate_pages(page_addr_list, uv_start_page,
3667 rot_info->width_pages_uv,
3668 rot_info->height_pages_uv,
3669 st, sg);
3670 }
3671
3672 DRM_DEBUG_KMS(
3673 "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",
3674 obj->base.size, rot_info->pitch, rot_info->height,
3675 rot_info->pixel_format, rot_info->width_pages,
3676 rot_info->height_pages, size_pages + size_pages_uv,
3677 size_pages);
3678
3679 drm_free_large(page_addr_list);
3680
3681 return st;
3682
3683 err_sg_alloc:
3684 kfree(st);
3685 err_st_alloc:
3686 drm_free_large(page_addr_list);
3687
3688 DRM_DEBUG_KMS(
3689 "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",
3690 obj->base.size, ret, rot_info->pitch, rot_info->height,
3691 rot_info->pixel_format, rot_info->width_pages,
3692 rot_info->height_pages, size_pages + size_pages_uv,
3693 size_pages);
3694 return ERR_PTR(ret);
3695 #endif
3696 }
3697
3698 static struct sg_table *
3699 intel_partial_pages(const struct i915_ggtt_view *view,
3700 struct drm_i915_gem_object *obj)
3701 {
3702 #ifdef __NetBSD__
3703 panic("XXX");
3704 #else
3705 struct sg_table *st;
3706 struct scatterlist *sg;
3707 struct sg_page_iter obj_sg_iter;
3708 int ret = -ENOMEM;
3709
3710 st = kmalloc(sizeof(*st), GFP_KERNEL);
3711 if (!st)
3712 goto err_st_alloc;
3713
3714 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
3715 if (ret)
3716 goto err_sg_alloc;
3717
3718 sg = st->sgl;
3719 st->nents = 0;
3720 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
3721 view->params.partial.offset)
3722 {
3723 if (st->nents >= view->params.partial.size)
3724 break;
3725
3726 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3727 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
3728 sg_dma_len(sg) = PAGE_SIZE;
3729
3730 sg = sg_next(sg);
3731 st->nents++;
3732 }
3733
3734 return st;
3735
3736 err_sg_alloc:
3737 kfree(st);
3738 err_st_alloc:
3739 return ERR_PTR(ret);
3740 #endif
3741 }
3742
3743 static int
3744 i915_get_ggtt_vma_pages(struct i915_vma *vma)
3745 {
3746 int ret = 0;
3747
3748 if (vma->ggtt_view.pages)
3749 return 0;
3750
3751 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
3752 vma->ggtt_view.pages = vma->obj->pages;
3753 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
3754 vma->ggtt_view.pages =
3755 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
3756 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
3757 vma->ggtt_view.pages =
3758 intel_partial_pages(&vma->ggtt_view, vma->obj);
3759 else
3760 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3761 vma->ggtt_view.type);
3762
3763 if (!vma->ggtt_view.pages) {
3764 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
3765 vma->ggtt_view.type);
3766 ret = -EINVAL;
3767 } else if (IS_ERR(vma->ggtt_view.pages)) {
3768 ret = PTR_ERR(vma->ggtt_view.pages);
3769 vma->ggtt_view.pages = NULL;
3770 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3771 vma->ggtt_view.type, ret);
3772 }
3773
3774 return ret;
3775 }
3776
3777 /**
3778 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3779 * @vma: VMA to map
3780 * @cache_level: mapping cache level
3781 * @flags: flags like global or local mapping
3782 *
3783 * DMA addresses are taken from the scatter-gather table of this object (or of
3784 * this VMA in case of non-default GGTT views) and PTE entries set up.
3785 * Note that DMA addresses are also the only part of the SG table we care about.
3786 */
3787 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3788 u32 flags)
3789 {
3790 int ret;
3791 u32 bind_flags;
3792
3793 if (WARN_ON(flags == 0))
3794 return -EINVAL;
3795
3796 bind_flags = 0;
3797 if (flags & PIN_GLOBAL)
3798 bind_flags |= GLOBAL_BIND;
3799 if (flags & PIN_USER)
3800 bind_flags |= LOCAL_BIND;
3801
3802 if (flags & PIN_UPDATE)
3803 bind_flags |= vma->bound;
3804 else
3805 bind_flags &= ~vma->bound;
3806
3807 if (bind_flags == 0)
3808 return 0;
3809
3810 if (vma->bound == 0 && vma->vm->allocate_va_range) {
3811 trace_i915_va_alloc(vma->vm,
3812 vma->node.start,
3813 vma->node.size,
3814 VM_TO_TRACE_NAME(vma->vm));
3815
3816 /* XXX: i915_vma_pin() will fix this +- hack */
3817 vma->pin_count++;
3818 ret = vma->vm->allocate_va_range(vma->vm,
3819 vma->node.start,
3820 vma->node.size);
3821 vma->pin_count--;
3822 if (ret)
3823 return ret;
3824 }
3825
3826 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
3827 if (ret)
3828 return ret;
3829
3830 vma->bound |= bind_flags;
3831
3832 return 0;
3833 }
3834
3835 /**
3836 * i915_ggtt_view_size - Get the size of a GGTT view.
3837 * @obj: Object the view is of.
3838 * @view: The view in question.
3839 *
3840 * @return The size of the GGTT view in bytes.
3841 */
3842 size_t
3843 i915_ggtt_view_size(struct drm_i915_gem_object *obj,
3844 const struct i915_ggtt_view *view)
3845 {
3846 if (view->type == I915_GGTT_VIEW_NORMAL) {
3847 return obj->base.size;
3848 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3849 return view->rotation_info.size;
3850 } else if (view->type == I915_GGTT_VIEW_PARTIAL) {
3851 return view->params.partial.size << PAGE_SHIFT;
3852 } else {
3853 WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type);
3854 return obj->base.size;
3855 }
3856 }
3857