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