1 // SPDX-License-Identifier: MIT
3 * Copyright © 2020 Intel Corporation
6 #include <linux/agp_backend.h>
7 #include <linux/stop_machine.h>
9 #include <asm/set_memory.h>
12 #include <drm/i915_drm.h>
13 #include <drm/intel-gtt.h>
15 #include "gem/i915_gem_lmem.h"
19 #include "i915_scatterlist.h"
20 #include "i915_vgpu.h"
22 #include "intel_gtt.h"
23 #include "gen8_ppgtt.h"
26 i915_get_ggtt_vma_pages(struct i915_vma *vma);
28 static void i915_ggtt_color_adjust(const struct drm_mm_node *node,
33 if (i915_node_color_differs(node, color))
34 *start += I915_GTT_PAGE_SIZE;
37 * Also leave a space between the unallocated reserved node after the
38 * GTT and any objects within the GTT, i.e. we use the color adjustment
39 * to insert a guard page to prevent prefetches crossing over the
42 node = list_next_entry(node, node_list);
43 if (node->color != color)
44 *end -= I915_GTT_PAGE_SIZE;
47 static int ggtt_init_hw(struct i915_ggtt *ggtt)
49 struct drm_i915_private *i915 = ggtt->vm.i915;
51 i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
53 ggtt->vm.is_ggtt = true;
55 /* Only VLV supports read-only GGTT mappings */
56 ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
58 if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
59 ggtt->vm.mm.color_adjust = i915_ggtt_color_adjust;
61 if (ggtt->mappable_end) {
62 if (!io_mapping_init_wc(&ggtt->iomap,
64 ggtt->mappable_end)) {
65 ggtt->vm.cleanup(&ggtt->vm);
69 ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start,
73 intel_ggtt_init_fences(ggtt);
79 * i915_ggtt_init_hw - Initialize GGTT hardware
82 int i915_ggtt_init_hw(struct drm_i915_private *i915)
87 * Note that we use page colouring to enforce a guard page at the
88 * end of the address space. This is required as the CS may prefetch
89 * beyond the end of the batch buffer, across the page boundary,
90 * and beyond the end of the GTT if we do not provide a guard.
92 ret = ggtt_init_hw(&i915->ggtt);
100 * Certain Gen5 chipsets require idling the GPU before
101 * unmapping anything from the GTT when VT-d is enabled.
103 static bool needs_idle_maps(struct drm_i915_private *i915)
106 * Query intel_iommu to see if we need the workaround. Presumably that
109 if (!intel_vtd_active())
112 if (GRAPHICS_VER(i915) == 5 && IS_MOBILE(i915))
115 if (GRAPHICS_VER(i915) == 12)
116 return true; /* XXX DMAR fault reason 7 */
122 * i915_ggtt_suspend_vm - Suspend the memory mappings for a GGTT or DPT VM
123 * @vm: The VM to suspend the mappings for
125 * Suspend the memory mappings for all objects mapped to HW via the GGTT or a
128 void i915_ggtt_suspend_vm(struct i915_address_space *vm)
130 struct i915_vma *vma, *vn;
133 drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
135 mutex_lock(&vm->mutex);
137 /* Skip rewriting PTE on VMA unbind. */
138 open = atomic_xchg(&vm->open, 0);
140 list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) {
141 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
142 i915_vma_wait_for_bind(vma);
144 if (i915_vma_is_pinned(vma))
147 if (!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) {
148 __i915_vma_evict(vma);
149 drm_mm_remove_node(&vma->node);
153 vm->clear_range(vm, 0, vm->total);
155 atomic_set(&vm->open, open);
157 mutex_unlock(&vm->mutex);
160 void i915_ggtt_suspend(struct i915_ggtt *ggtt)
162 i915_ggtt_suspend_vm(&ggtt->vm);
163 ggtt->invalidate(ggtt);
165 intel_gt_check_and_clear_faults(ggtt->vm.gt);
168 void gen6_ggtt_invalidate(struct i915_ggtt *ggtt)
170 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
172 spin_lock_irq(&uncore->lock);
173 intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
174 intel_uncore_read_fw(uncore, GFX_FLSH_CNTL_GEN6);
175 spin_unlock_irq(&uncore->lock);
178 static void gen8_ggtt_invalidate(struct i915_ggtt *ggtt)
180 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
183 * Note that as an uncached mmio write, this will flush the
184 * WCB of the writes into the GGTT before it triggers the invalidate.
186 intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
189 static void guc_ggtt_invalidate(struct i915_ggtt *ggtt)
191 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
192 struct drm_i915_private *i915 = ggtt->vm.i915;
194 gen8_ggtt_invalidate(ggtt);
196 if (GRAPHICS_VER(i915) >= 12)
197 intel_uncore_write_fw(uncore, GEN12_GUC_TLB_INV_CR,
198 GEN12_GUC_TLB_INV_CR_INVALIDATE);
200 intel_uncore_write_fw(uncore, GEN8_GTCR, GEN8_GTCR_INVALIDATE);
203 static void gmch_ggtt_invalidate(struct i915_ggtt *ggtt)
205 intel_gtt_chipset_flush();
208 u64 gen8_ggtt_pte_encode(dma_addr_t addr,
209 enum i915_cache_level level,
212 gen8_pte_t pte = addr | _PAGE_PRESENT;
215 pte |= GEN12_GGTT_PTE_LM;
220 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
225 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
228 enum i915_cache_level level,
231 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
232 gen8_pte_t __iomem *pte =
233 (gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
235 gen8_set_pte(pte, gen8_ggtt_pte_encode(addr, level, flags));
237 ggtt->invalidate(ggtt);
240 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
241 struct i915_vma *vma,
242 enum i915_cache_level level,
245 const gen8_pte_t pte_encode = gen8_ggtt_pte_encode(0, level, flags);
246 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
247 gen8_pte_t __iomem *gte;
248 gen8_pte_t __iomem *end;
249 struct sgt_iter iter;
253 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
254 * not to allow the user to override access to a read only page.
257 gte = (gen8_pte_t __iomem *)ggtt->gsm;
258 gte += vma->node.start / I915_GTT_PAGE_SIZE;
259 end = gte + vma->node.size / I915_GTT_PAGE_SIZE;
261 for_each_sgt_daddr(addr, iter, vma->pages)
262 gen8_set_pte(gte++, pte_encode | addr);
263 GEM_BUG_ON(gte > end);
265 /* Fill the allocated but "unused" space beyond the end of the buffer */
267 gen8_set_pte(gte++, vm->scratch[0]->encode);
270 * We want to flush the TLBs only after we're certain all the PTE
271 * updates have finished.
273 ggtt->invalidate(ggtt);
276 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
279 enum i915_cache_level level,
282 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
283 gen6_pte_t __iomem *pte =
284 (gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
286 iowrite32(vm->pte_encode(addr, level, flags), pte);
288 ggtt->invalidate(ggtt);
292 * Binds an object into the global gtt with the specified cache level.
293 * The object will be accessible to the GPU via commands whose operands
294 * reference offsets within the global GTT as well as accessible by the GPU
295 * through the GMADR mapped BAR (i915->mm.gtt->gtt).
297 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
298 struct i915_vma *vma,
299 enum i915_cache_level level,
302 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
303 gen6_pte_t __iomem *gte;
304 gen6_pte_t __iomem *end;
305 struct sgt_iter iter;
308 gte = (gen6_pte_t __iomem *)ggtt->gsm;
309 gte += vma->node.start / I915_GTT_PAGE_SIZE;
310 end = gte + vma->node.size / I915_GTT_PAGE_SIZE;
312 for_each_sgt_daddr(addr, iter, vma->pages)
313 iowrite32(vm->pte_encode(addr, level, flags), gte++);
314 GEM_BUG_ON(gte > end);
316 /* Fill the allocated but "unused" space beyond the end of the buffer */
318 iowrite32(vm->scratch[0]->encode, gte++);
321 * We want to flush the TLBs only after we're certain all the PTE
322 * updates have finished.
324 ggtt->invalidate(ggtt);
327 static void nop_clear_range(struct i915_address_space *vm,
328 u64 start, u64 length)
332 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
333 u64 start, u64 length)
335 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
336 unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
337 unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
338 const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
339 gen8_pte_t __iomem *gtt_base =
340 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
341 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
344 if (WARN(num_entries > max_entries,
345 "First entry = %d; Num entries = %d (max=%d)\n",
346 first_entry, num_entries, max_entries))
347 num_entries = max_entries;
349 for (i = 0; i < num_entries; i++)
350 gen8_set_pte(>t_base[i], scratch_pte);
353 static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
356 * Make sure the internal GAM fifo has been cleared of all GTT
357 * writes before exiting stop_machine(). This guarantees that
358 * any aperture accesses waiting to start in another process
359 * cannot back up behind the GTT writes causing a hang.
360 * The register can be any arbitrary GAM register.
362 intel_uncore_posting_read_fw(vm->gt->uncore, GFX_FLSH_CNTL_GEN6);
366 struct i915_address_space *vm;
369 enum i915_cache_level level;
372 static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
374 struct insert_page *arg = _arg;
376 gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
377 bxt_vtd_ggtt_wa(arg->vm);
382 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
385 enum i915_cache_level level,
388 struct insert_page arg = { vm, addr, offset, level };
390 stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
393 struct insert_entries {
394 struct i915_address_space *vm;
395 struct i915_vma *vma;
396 enum i915_cache_level level;
400 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
402 struct insert_entries *arg = _arg;
404 gen8_ggtt_insert_entries(arg->vm, arg->vma, arg->level, arg->flags);
405 bxt_vtd_ggtt_wa(arg->vm);
410 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
411 struct i915_vma *vma,
412 enum i915_cache_level level,
415 struct insert_entries arg = { vm, vma, level, flags };
417 stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
420 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
421 u64 start, u64 length)
423 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
424 unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
425 unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
426 gen6_pte_t scratch_pte, __iomem *gtt_base =
427 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
428 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
431 if (WARN(num_entries > max_entries,
432 "First entry = %d; Num entries = %d (max=%d)\n",
433 first_entry, num_entries, max_entries))
434 num_entries = max_entries;
436 scratch_pte = vm->scratch[0]->encode;
437 for (i = 0; i < num_entries; i++)
438 iowrite32(scratch_pte, >t_base[i]);
441 static void i915_ggtt_insert_page(struct i915_address_space *vm,
444 enum i915_cache_level cache_level,
447 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
448 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
450 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
453 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
454 struct i915_vma *vma,
455 enum i915_cache_level cache_level,
458 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
459 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
461 intel_gtt_insert_sg_entries(vma->pages, vma->node.start >> PAGE_SHIFT,
465 static void i915_ggtt_clear_range(struct i915_address_space *vm,
466 u64 start, u64 length)
468 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
471 static void ggtt_bind_vma(struct i915_address_space *vm,
472 struct i915_vm_pt_stash *stash,
473 struct i915_vma *vma,
474 enum i915_cache_level cache_level,
477 struct drm_i915_gem_object *obj = vma->obj;
480 if (i915_vma_is_bound(vma, ~flags & I915_VMA_BIND_MASK))
483 /* Applicable to VLV (gen8+ do not support RO in the GGTT) */
485 if (i915_gem_object_is_readonly(obj))
486 pte_flags |= PTE_READ_ONLY;
487 if (i915_gem_object_is_lmem(obj))
490 vm->insert_entries(vm, vma, cache_level, pte_flags);
491 vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
494 static void ggtt_unbind_vma(struct i915_address_space *vm, struct i915_vma *vma)
496 vm->clear_range(vm, vma->node.start, vma->size);
499 static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
504 if (!intel_uc_uses_guc(&ggtt->vm.gt->uc))
507 GEM_BUG_ON(ggtt->vm.total <= GUC_GGTT_TOP);
508 size = ggtt->vm.total - GUC_GGTT_TOP;
510 ret = i915_gem_gtt_reserve(&ggtt->vm, &ggtt->uc_fw, size,
511 GUC_GGTT_TOP, I915_COLOR_UNEVICTABLE,
514 drm_dbg(&ggtt->vm.i915->drm,
515 "Failed to reserve top of GGTT for GuC\n");
520 static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
522 if (drm_mm_node_allocated(&ggtt->uc_fw))
523 drm_mm_remove_node(&ggtt->uc_fw);
526 static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
528 ggtt_release_guc_top(ggtt);
529 if (drm_mm_node_allocated(&ggtt->error_capture))
530 drm_mm_remove_node(&ggtt->error_capture);
531 mutex_destroy(&ggtt->error_mutex);
534 static int init_ggtt(struct i915_ggtt *ggtt)
537 * Let GEM Manage all of the aperture.
539 * However, leave one page at the end still bound to the scratch page.
540 * There are a number of places where the hardware apparently prefetches
541 * past the end of the object, and we've seen multiple hangs with the
542 * GPU head pointer stuck in a batchbuffer bound at the last page of the
543 * aperture. One page should be enough to keep any prefetching inside
546 unsigned long hole_start, hole_end;
547 struct drm_mm_node *entry;
551 * GuC requires all resources that we're sharing with it to be placed in
552 * non-WOPCM memory. If GuC is not present or not in use we still need a
553 * small bias as ring wraparound at offset 0 sometimes hangs. No idea
556 ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
557 intel_wopcm_guc_size(&ggtt->vm.i915->wopcm));
559 ret = intel_vgt_balloon(ggtt);
563 mutex_init(&ggtt->error_mutex);
564 if (ggtt->mappable_end) {
566 * Reserve a mappable slot for our lockless error capture.
568 * We strongly prefer taking address 0x0 in order to protect
569 * other critical buffers against accidental overwrites,
570 * as writing to address 0 is a very common mistake.
572 * Since 0 may already be in use by the system (e.g. the BIOS
573 * framebuffer), we let the reservation fail quietly and hope
574 * 0 remains reserved always.
576 * If we fail to reserve 0, and then fail to find any space
577 * for an error-capture, remain silent. We can afford not
578 * to reserve an error_capture node as we have fallback
579 * paths, and we trust that 0 will remain reserved. However,
580 * the only likely reason for failure to insert is a driver
581 * bug, which we expect to cause other failures...
583 ggtt->error_capture.size = I915_GTT_PAGE_SIZE;
584 ggtt->error_capture.color = I915_COLOR_UNEVICTABLE;
585 if (drm_mm_reserve_node(&ggtt->vm.mm, &ggtt->error_capture))
586 drm_mm_insert_node_in_range(&ggtt->vm.mm,
587 &ggtt->error_capture,
588 ggtt->error_capture.size, 0,
589 ggtt->error_capture.color,
590 0, ggtt->mappable_end,
593 if (drm_mm_node_allocated(&ggtt->error_capture))
594 drm_dbg(&ggtt->vm.i915->drm,
595 "Reserved GGTT:[%llx, %llx] for use by error capture\n",
596 ggtt->error_capture.start,
597 ggtt->error_capture.start + ggtt->error_capture.size);
600 * The upper portion of the GuC address space has a sizeable hole
601 * (several MB) that is inaccessible by GuC. Reserve this range within
602 * GGTT as it can comfortably hold GuC/HuC firmware images.
604 ret = ggtt_reserve_guc_top(ggtt);
608 /* Clear any non-preallocated blocks */
609 drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
610 drm_dbg(&ggtt->vm.i915->drm,
611 "clearing unused GTT space: [%lx, %lx]\n",
612 hole_start, hole_end);
613 ggtt->vm.clear_range(&ggtt->vm, hole_start,
614 hole_end - hole_start);
617 /* And finally clear the reserved guard page */
618 ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
623 cleanup_init_ggtt(ggtt);
627 static void aliasing_gtt_bind_vma(struct i915_address_space *vm,
628 struct i915_vm_pt_stash *stash,
629 struct i915_vma *vma,
630 enum i915_cache_level cache_level,
635 /* Currently applicable only to VLV */
637 if (i915_gem_object_is_readonly(vma->obj))
638 pte_flags |= PTE_READ_ONLY;
640 if (flags & I915_VMA_LOCAL_BIND)
641 ppgtt_bind_vma(&i915_vm_to_ggtt(vm)->alias->vm,
642 stash, vma, cache_level, flags);
644 if (flags & I915_VMA_GLOBAL_BIND)
645 vm->insert_entries(vm, vma, cache_level, pte_flags);
648 static void aliasing_gtt_unbind_vma(struct i915_address_space *vm,
649 struct i915_vma *vma)
651 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
652 vm->clear_range(vm, vma->node.start, vma->size);
654 if (i915_vma_is_bound(vma, I915_VMA_LOCAL_BIND))
655 ppgtt_unbind_vma(&i915_vm_to_ggtt(vm)->alias->vm, vma);
658 static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
660 struct i915_vm_pt_stash stash = {};
661 struct i915_ppgtt *ppgtt;
664 ppgtt = i915_ppgtt_create(ggtt->vm.gt, 0);
666 return PTR_ERR(ppgtt);
668 if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
673 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, ggtt->vm.total);
677 i915_gem_object_lock(ppgtt->vm.scratch[0], NULL);
678 err = i915_vm_map_pt_stash(&ppgtt->vm, &stash);
679 i915_gem_object_unlock(ppgtt->vm.scratch[0]);
684 * Note we only pre-allocate as far as the end of the global
685 * GTT. On 48b / 4-level page-tables, the difference is very,
686 * very significant! We have to preallocate as GVT/vgpu does
687 * not like the page directory disappearing.
689 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, ggtt->vm.total);
692 ggtt->vm.bind_async_flags |= ppgtt->vm.bind_async_flags;
694 GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != ggtt_bind_vma);
695 ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
697 GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != ggtt_unbind_vma);
698 ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
700 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
704 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
706 i915_vm_put(&ppgtt->vm);
710 static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
712 struct i915_ppgtt *ppgtt;
714 ppgtt = fetch_and_zero(&ggtt->alias);
718 i915_vm_put(&ppgtt->vm);
720 ggtt->vm.vma_ops.bind_vma = ggtt_bind_vma;
721 ggtt->vm.vma_ops.unbind_vma = ggtt_unbind_vma;
724 int i915_init_ggtt(struct drm_i915_private *i915)
728 ret = init_ggtt(&i915->ggtt);
732 if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
733 ret = init_aliasing_ppgtt(&i915->ggtt);
735 cleanup_init_ggtt(&i915->ggtt);
741 static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
743 struct i915_vma *vma, *vn;
745 atomic_set(&ggtt->vm.open, 0);
747 flush_workqueue(ggtt->vm.i915->wq);
749 mutex_lock(&ggtt->vm.mutex);
751 list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link)
752 WARN_ON(__i915_vma_unbind(vma));
754 if (drm_mm_node_allocated(&ggtt->error_capture))
755 drm_mm_remove_node(&ggtt->error_capture);
756 mutex_destroy(&ggtt->error_mutex);
758 ggtt_release_guc_top(ggtt);
759 intel_vgt_deballoon(ggtt);
761 ggtt->vm.cleanup(&ggtt->vm);
763 mutex_unlock(&ggtt->vm.mutex);
764 i915_address_space_fini(&ggtt->vm);
766 arch_phys_wc_del(ggtt->mtrr);
768 if (ggtt->iomap.size)
769 io_mapping_fini(&ggtt->iomap);
773 * i915_ggtt_driver_release - Clean up GGTT hardware initialization
776 void i915_ggtt_driver_release(struct drm_i915_private *i915)
778 struct i915_ggtt *ggtt = &i915->ggtt;
780 fini_aliasing_ppgtt(ggtt);
782 intel_ggtt_fini_fences(ggtt);
783 ggtt_cleanup_hw(ggtt);
787 * i915_ggtt_driver_late_release - Cleanup of GGTT that needs to be done after
788 * all free objects have been drained.
791 void i915_ggtt_driver_late_release(struct drm_i915_private *i915)
793 struct i915_ggtt *ggtt = &i915->ggtt;
795 GEM_WARN_ON(kref_read(&ggtt->vm.resv_ref) != 1);
796 dma_resv_fini(&ggtt->vm._resv);
799 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
801 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
802 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
803 return snb_gmch_ctl << 20;
806 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
808 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
809 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
811 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
814 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
815 if (bdw_gmch_ctl > 4)
819 return bdw_gmch_ctl << 20;
822 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
824 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
825 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
828 return 1 << (20 + gmch_ctrl);
833 static unsigned int gen6_gttmmadr_size(struct drm_i915_private *i915)
836 * GEN6: GTTMMADR size is 4MB and GTTADR starts at 2MB offset
837 * GEN8: GTTMMADR size is 16MB and GTTADR starts at 8MB offset
839 GEM_BUG_ON(GRAPHICS_VER(i915) < 6);
840 return (GRAPHICS_VER(i915) < 8) ? SZ_4M : SZ_16M;
843 static unsigned int gen6_gttadr_offset(struct drm_i915_private *i915)
845 return gen6_gttmmadr_size(i915) / 2;
848 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
850 struct drm_i915_private *i915 = ggtt->vm.i915;
851 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
852 phys_addr_t phys_addr;
856 GEM_WARN_ON(pci_resource_len(pdev, 0) != gen6_gttmmadr_size(i915));
857 phys_addr = pci_resource_start(pdev, 0) + gen6_gttadr_offset(i915);
860 * On BXT+/ICL+ writes larger than 64 bit to the GTT pagetable range
861 * will be dropped. For WC mappings in general we have 64 byte burst
862 * writes when the WC buffer is flushed, so we can't use it, but have to
863 * resort to an uncached mapping. The WC issue is easily caught by the
864 * readback check when writing GTT PTE entries.
866 if (IS_GEN9_LP(i915) || GRAPHICS_VER(i915) >= 11)
867 ggtt->gsm = ioremap(phys_addr, size);
869 ggtt->gsm = ioremap_wc(phys_addr, size);
871 drm_err(&i915->drm, "Failed to map the ggtt page table\n");
875 kref_init(&ggtt->vm.resv_ref);
876 ret = setup_scratch_page(&ggtt->vm);
878 drm_err(&i915->drm, "Scratch setup failed\n");
879 /* iounmap will also get called at remove, but meh */
885 if (i915_gem_object_is_lmem(ggtt->vm.scratch[0]))
888 ggtt->vm.scratch[0]->encode =
889 ggtt->vm.pte_encode(px_dma(ggtt->vm.scratch[0]),
890 I915_CACHE_NONE, pte_flags);
895 int ggtt_set_pages(struct i915_vma *vma)
899 GEM_BUG_ON(vma->pages);
901 ret = i915_get_ggtt_vma_pages(vma);
905 vma->page_sizes = vma->obj->mm.page_sizes;
910 static void gen6_gmch_remove(struct i915_address_space *vm)
912 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
918 static struct resource pci_resource(struct pci_dev *pdev, int bar)
920 return (struct resource)DEFINE_RES_MEM(pci_resource_start(pdev, bar),
921 pci_resource_len(pdev, bar));
924 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
926 struct drm_i915_private *i915 = ggtt->vm.i915;
927 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
931 /* TODO: We're not aware of mappable constraints on gen8 yet */
932 if (!HAS_LMEM(i915)) {
933 ggtt->gmadr = pci_resource(pdev, 2);
934 ggtt->mappable_end = resource_size(&ggtt->gmadr);
937 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
938 if (IS_CHERRYVIEW(i915))
939 size = chv_get_total_gtt_size(snb_gmch_ctl);
941 size = gen8_get_total_gtt_size(snb_gmch_ctl);
943 ggtt->vm.alloc_pt_dma = alloc_pt_dma;
944 ggtt->vm.lmem_pt_obj_flags = I915_BO_ALLOC_PM_EARLY;
946 ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
947 ggtt->vm.cleanup = gen6_gmch_remove;
948 ggtt->vm.insert_page = gen8_ggtt_insert_page;
949 ggtt->vm.clear_range = nop_clear_range;
950 if (intel_scanout_needs_vtd_wa(i915))
951 ggtt->vm.clear_range = gen8_ggtt_clear_range;
953 ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
956 * Serialize GTT updates with aperture access on BXT if VT-d is on,
959 if (intel_vm_no_concurrent_access_wa(i915)) {
960 ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
961 ggtt->vm.insert_page = bxt_vtd_ggtt_insert_page__BKL;
962 ggtt->vm.bind_async_flags =
963 I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
966 ggtt->invalidate = gen8_ggtt_invalidate;
968 ggtt->vm.vma_ops.bind_vma = ggtt_bind_vma;
969 ggtt->vm.vma_ops.unbind_vma = ggtt_unbind_vma;
970 ggtt->vm.vma_ops.set_pages = ggtt_set_pages;
971 ggtt->vm.vma_ops.clear_pages = clear_pages;
973 ggtt->vm.pte_encode = gen8_ggtt_pte_encode;
975 setup_private_pat(ggtt->vm.gt->uncore);
977 return ggtt_probe_common(ggtt, size);
980 static u64 snb_pte_encode(dma_addr_t addr,
981 enum i915_cache_level level,
984 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
987 case I915_CACHE_L3_LLC:
989 pte |= GEN6_PTE_CACHE_LLC;
991 case I915_CACHE_NONE:
992 pte |= GEN6_PTE_UNCACHED;
1001 static u64 ivb_pte_encode(dma_addr_t addr,
1002 enum i915_cache_level level,
1005 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1008 case I915_CACHE_L3_LLC:
1009 pte |= GEN7_PTE_CACHE_L3_LLC;
1011 case I915_CACHE_LLC:
1012 pte |= GEN6_PTE_CACHE_LLC;
1014 case I915_CACHE_NONE:
1015 pte |= GEN6_PTE_UNCACHED;
1018 MISSING_CASE(level);
1024 static u64 byt_pte_encode(dma_addr_t addr,
1025 enum i915_cache_level level,
1028 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1030 if (!(flags & PTE_READ_ONLY))
1031 pte |= BYT_PTE_WRITEABLE;
1033 if (level != I915_CACHE_NONE)
1034 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
1039 static u64 hsw_pte_encode(dma_addr_t addr,
1040 enum i915_cache_level level,
1043 gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1045 if (level != I915_CACHE_NONE)
1046 pte |= HSW_WB_LLC_AGE3;
1051 static u64 iris_pte_encode(dma_addr_t addr,
1052 enum i915_cache_level level,
1055 gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1058 case I915_CACHE_NONE:
1061 pte |= HSW_WT_ELLC_LLC_AGE3;
1064 pte |= HSW_WB_ELLC_LLC_AGE3;
1071 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
1073 struct drm_i915_private *i915 = ggtt->vm.i915;
1074 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1078 ggtt->gmadr = pci_resource(pdev, 2);
1079 ggtt->mappable_end = resource_size(&ggtt->gmadr);
1082 * 64/512MB is the current min/max we actually know of, but this is
1083 * just a coarse sanity check.
1085 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
1086 drm_err(&i915->drm, "Unknown GMADR size (%pa)\n",
1087 &ggtt->mappable_end);
1091 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1093 size = gen6_get_total_gtt_size(snb_gmch_ctl);
1094 ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
1096 ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1098 ggtt->vm.clear_range = nop_clear_range;
1099 if (!HAS_FULL_PPGTT(i915) || intel_scanout_needs_vtd_wa(i915))
1100 ggtt->vm.clear_range = gen6_ggtt_clear_range;
1101 ggtt->vm.insert_page = gen6_ggtt_insert_page;
1102 ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
1103 ggtt->vm.cleanup = gen6_gmch_remove;
1105 ggtt->invalidate = gen6_ggtt_invalidate;
1107 if (HAS_EDRAM(i915))
1108 ggtt->vm.pte_encode = iris_pte_encode;
1109 else if (IS_HASWELL(i915))
1110 ggtt->vm.pte_encode = hsw_pte_encode;
1111 else if (IS_VALLEYVIEW(i915))
1112 ggtt->vm.pte_encode = byt_pte_encode;
1113 else if (GRAPHICS_VER(i915) >= 7)
1114 ggtt->vm.pte_encode = ivb_pte_encode;
1116 ggtt->vm.pte_encode = snb_pte_encode;
1118 ggtt->vm.vma_ops.bind_vma = ggtt_bind_vma;
1119 ggtt->vm.vma_ops.unbind_vma = ggtt_unbind_vma;
1120 ggtt->vm.vma_ops.set_pages = ggtt_set_pages;
1121 ggtt->vm.vma_ops.clear_pages = clear_pages;
1123 return ggtt_probe_common(ggtt, size);
1126 static void i915_gmch_remove(struct i915_address_space *vm)
1128 intel_gmch_remove();
1131 static int i915_gmch_probe(struct i915_ggtt *ggtt)
1133 struct drm_i915_private *i915 = ggtt->vm.i915;
1134 phys_addr_t gmadr_base;
1137 ret = intel_gmch_probe(i915->bridge_dev, to_pci_dev(i915->drm.dev), NULL);
1139 drm_err(&i915->drm, "failed to set up gmch\n");
1143 intel_gtt_get(&ggtt->vm.total, &gmadr_base, &ggtt->mappable_end);
1146 (struct resource)DEFINE_RES_MEM(gmadr_base, ggtt->mappable_end);
1148 ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1150 if (needs_idle_maps(i915)) {
1151 drm_notice(&i915->drm,
1152 "Flushing DMA requests before IOMMU unmaps; performance may be degraded\n");
1153 ggtt->do_idle_maps = true;
1156 ggtt->vm.insert_page = i915_ggtt_insert_page;
1157 ggtt->vm.insert_entries = i915_ggtt_insert_entries;
1158 ggtt->vm.clear_range = i915_ggtt_clear_range;
1159 ggtt->vm.cleanup = i915_gmch_remove;
1161 ggtt->invalidate = gmch_ggtt_invalidate;
1163 ggtt->vm.vma_ops.bind_vma = ggtt_bind_vma;
1164 ggtt->vm.vma_ops.unbind_vma = ggtt_unbind_vma;
1165 ggtt->vm.vma_ops.set_pages = ggtt_set_pages;
1166 ggtt->vm.vma_ops.clear_pages = clear_pages;
1168 if (unlikely(ggtt->do_idle_maps))
1169 drm_notice(&i915->drm,
1170 "Applying Ironlake quirks for intel_iommu\n");
1175 static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
1177 struct drm_i915_private *i915 = gt->i915;
1181 ggtt->vm.i915 = i915;
1182 ggtt->vm.dma = i915->drm.dev;
1183 dma_resv_init(&ggtt->vm._resv);
1185 if (GRAPHICS_VER(i915) <= 5)
1186 ret = i915_gmch_probe(ggtt);
1187 else if (GRAPHICS_VER(i915) < 8)
1188 ret = gen6_gmch_probe(ggtt);
1190 ret = gen8_gmch_probe(ggtt);
1192 dma_resv_fini(&ggtt->vm._resv);
1196 if ((ggtt->vm.total - 1) >> 32) {
1198 "We never expected a Global GTT with more than 32bits"
1199 " of address space! Found %lldM!\n",
1200 ggtt->vm.total >> 20);
1201 ggtt->vm.total = 1ULL << 32;
1202 ggtt->mappable_end =
1203 min_t(u64, ggtt->mappable_end, ggtt->vm.total);
1206 if (ggtt->mappable_end > ggtt->vm.total) {
1208 "mappable aperture extends past end of GGTT,"
1209 " aperture=%pa, total=%llx\n",
1210 &ggtt->mappable_end, ggtt->vm.total);
1211 ggtt->mappable_end = ggtt->vm.total;
1214 /* GMADR is the PCI mmio aperture into the global GTT. */
1215 drm_dbg(&i915->drm, "GGTT size = %lluM\n", ggtt->vm.total >> 20);
1216 drm_dbg(&i915->drm, "GMADR size = %lluM\n",
1217 (u64)ggtt->mappable_end >> 20);
1218 drm_dbg(&i915->drm, "DSM size = %lluM\n",
1219 (u64)resource_size(&intel_graphics_stolen_res) >> 20);
1225 * i915_ggtt_probe_hw - Probe GGTT hardware location
1226 * @i915: i915 device
1228 int i915_ggtt_probe_hw(struct drm_i915_private *i915)
1232 ret = ggtt_probe_hw(&i915->ggtt, &i915->gt);
1236 if (intel_vtd_active())
1237 drm_info(&i915->drm, "VT-d active for gfx access\n");
1242 int i915_ggtt_enable_hw(struct drm_i915_private *i915)
1244 if (GRAPHICS_VER(i915) < 6 && !intel_enable_gtt())
1250 void i915_ggtt_enable_guc(struct i915_ggtt *ggtt)
1252 GEM_BUG_ON(ggtt->invalidate != gen8_ggtt_invalidate);
1254 ggtt->invalidate = guc_ggtt_invalidate;
1256 ggtt->invalidate(ggtt);
1259 void i915_ggtt_disable_guc(struct i915_ggtt *ggtt)
1261 /* XXX Temporary pardon for error unload */
1262 if (ggtt->invalidate == gen8_ggtt_invalidate)
1265 /* We should only be called after i915_ggtt_enable_guc() */
1266 GEM_BUG_ON(ggtt->invalidate != guc_ggtt_invalidate);
1268 ggtt->invalidate = gen8_ggtt_invalidate;
1270 ggtt->invalidate(ggtt);
1274 * i915_ggtt_resume_vm - Restore the memory mappings for a GGTT or DPT VM
1275 * @vm: The VM to restore the mappings for
1277 * Restore the memory mappings for all objects mapped to HW via the GGTT or a
1280 * Returns %true if restoring the mapping for any object that was in a write
1281 * domain before suspend.
1283 bool i915_ggtt_resume_vm(struct i915_address_space *vm)
1285 struct i915_vma *vma;
1286 bool write_domain_objs = false;
1289 drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
1291 /* First fill our portion of the GTT with scratch pages */
1292 vm->clear_range(vm, 0, vm->total);
1294 /* Skip rewriting PTE on VMA unbind. */
1295 open = atomic_xchg(&vm->open, 0);
1297 /* clflush objects bound into the GGTT and rebind them. */
1298 list_for_each_entry(vma, &vm->bound_list, vm_link) {
1299 struct drm_i915_gem_object *obj = vma->obj;
1300 unsigned int was_bound =
1301 atomic_read(&vma->flags) & I915_VMA_BIND_MASK;
1303 GEM_BUG_ON(!was_bound);
1304 vma->ops->bind_vma(vm, NULL, vma,
1305 obj ? obj->cache_level : 0,
1307 if (obj) { /* only used during resume => exclusive access */
1308 write_domain_objs |= fetch_and_zero(&obj->write_domain);
1309 obj->read_domains |= I915_GEM_DOMAIN_GTT;
1313 atomic_set(&vm->open, open);
1315 return write_domain_objs;
1318 void i915_ggtt_resume(struct i915_ggtt *ggtt)
1322 intel_gt_check_and_clear_faults(ggtt->vm.gt);
1324 flush = i915_ggtt_resume_vm(&ggtt->vm);
1326 ggtt->invalidate(ggtt);
1329 wbinvd_on_all_cpus();
1331 if (GRAPHICS_VER(ggtt->vm.i915) >= 8)
1332 setup_private_pat(ggtt->vm.gt->uncore);
1334 intel_ggtt_restore_fences(ggtt);
1337 static struct scatterlist *
1338 rotate_pages(struct drm_i915_gem_object *obj, unsigned int offset,
1339 unsigned int width, unsigned int height,
1340 unsigned int src_stride, unsigned int dst_stride,
1341 struct sg_table *st, struct scatterlist *sg)
1343 unsigned int column, row;
1344 unsigned int src_idx;
1346 for (column = 0; column < width; column++) {
1349 src_idx = src_stride * (height - 1) + column + offset;
1350 for (row = 0; row < height; row++) {
1353 * We don't need the pages, but need to initialize
1354 * the entries so the sg list can be happily traversed.
1355 * The only thing we need are DMA addresses.
1357 sg_set_page(sg, NULL, I915_GTT_PAGE_SIZE, 0);
1358 sg_dma_address(sg) =
1359 i915_gem_object_get_dma_address(obj, src_idx);
1360 sg_dma_len(sg) = I915_GTT_PAGE_SIZE;
1362 src_idx -= src_stride;
1365 left = (dst_stride - height) * I915_GTT_PAGE_SIZE;
1373 * The DE ignores the PTEs for the padding tiles, the sg entry
1374 * here is just a conenience to indicate how many padding PTEs
1375 * to insert at this spot.
1377 sg_set_page(sg, NULL, left, 0);
1378 sg_dma_address(sg) = 0;
1379 sg_dma_len(sg) = left;
1386 static noinline struct sg_table *
1387 intel_rotate_pages(struct intel_rotation_info *rot_info,
1388 struct drm_i915_gem_object *obj)
1390 unsigned int size = intel_rotation_info_size(rot_info);
1391 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1392 struct sg_table *st;
1393 struct scatterlist *sg;
1397 /* Allocate target SG list. */
1398 st = kmalloc(sizeof(*st), GFP_KERNEL);
1402 ret = sg_alloc_table(st, size, GFP_KERNEL);
1409 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
1410 sg = rotate_pages(obj, rot_info->plane[i].offset,
1411 rot_info->plane[i].width, rot_info->plane[i].height,
1412 rot_info->plane[i].src_stride,
1413 rot_info->plane[i].dst_stride,
1422 drm_dbg(&i915->drm, "Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
1423 obj->base.size, rot_info->plane[0].width,
1424 rot_info->plane[0].height, size);
1426 return ERR_PTR(ret);
1429 static struct scatterlist *
1430 add_padding_pages(unsigned int count,
1431 struct sg_table *st, struct scatterlist *sg)
1436 * The DE ignores the PTEs for the padding tiles, the sg entry
1437 * here is just a convenience to indicate how many padding PTEs
1438 * to insert at this spot.
1440 sg_set_page(sg, NULL, count * I915_GTT_PAGE_SIZE, 0);
1441 sg_dma_address(sg) = 0;
1442 sg_dma_len(sg) = count * I915_GTT_PAGE_SIZE;
1448 static struct scatterlist *
1449 remap_tiled_color_plane_pages(struct drm_i915_gem_object *obj,
1450 unsigned int offset, unsigned int alignment_pad,
1451 unsigned int width, unsigned int height,
1452 unsigned int src_stride, unsigned int dst_stride,
1453 struct sg_table *st, struct scatterlist *sg,
1454 unsigned int *gtt_offset)
1458 if (!width || !height)
1462 sg = add_padding_pages(alignment_pad, st, sg);
1464 for (row = 0; row < height; row++) {
1465 unsigned int left = width * I915_GTT_PAGE_SIZE;
1469 unsigned int length;
1472 * We don't need the pages, but need to initialize
1473 * the entries so the sg list can be happily traversed.
1474 * The only thing we need are DMA addresses.
1477 addr = i915_gem_object_get_dma_address_len(obj, offset, &length);
1479 length = min(left, length);
1483 sg_set_page(sg, NULL, length, 0);
1484 sg_dma_address(sg) = addr;
1485 sg_dma_len(sg) = length;
1488 offset += length / I915_GTT_PAGE_SIZE;
1492 offset += src_stride - width;
1494 left = (dst_stride - width) * I915_GTT_PAGE_SIZE;
1499 sg = add_padding_pages(left >> PAGE_SHIFT, st, sg);
1502 *gtt_offset += alignment_pad + dst_stride * height;
1507 static struct scatterlist *
1508 remap_contiguous_pages(struct drm_i915_gem_object *obj,
1509 unsigned int obj_offset,
1511 struct sg_table *st, struct scatterlist *sg)
1513 struct scatterlist *iter;
1514 unsigned int offset;
1516 iter = i915_gem_object_get_sg_dma(obj, obj_offset, &offset);
1522 len = min(sg_dma_len(iter) - (offset << PAGE_SHIFT),
1523 count << PAGE_SHIFT);
1524 sg_set_page(sg, NULL, len, 0);
1525 sg_dma_address(sg) =
1526 sg_dma_address(iter) + (offset << PAGE_SHIFT);
1527 sg_dma_len(sg) = len;
1530 count -= len >> PAGE_SHIFT;
1535 iter = __sg_next(iter);
1540 static struct scatterlist *
1541 remap_linear_color_plane_pages(struct drm_i915_gem_object *obj,
1542 unsigned int obj_offset, unsigned int alignment_pad,
1544 struct sg_table *st, struct scatterlist *sg,
1545 unsigned int *gtt_offset)
1551 sg = add_padding_pages(alignment_pad, st, sg);
1553 sg = remap_contiguous_pages(obj, obj_offset, size, st, sg);
1556 *gtt_offset += alignment_pad + size;
1561 static struct scatterlist *
1562 remap_color_plane_pages(const struct intel_remapped_info *rem_info,
1563 struct drm_i915_gem_object *obj,
1565 struct sg_table *st, struct scatterlist *sg,
1566 unsigned int *gtt_offset)
1568 unsigned int alignment_pad = 0;
1570 if (rem_info->plane_alignment)
1571 alignment_pad = ALIGN(*gtt_offset, rem_info->plane_alignment) - *gtt_offset;
1573 if (rem_info->plane[color_plane].linear)
1574 sg = remap_linear_color_plane_pages(obj,
1575 rem_info->plane[color_plane].offset,
1577 rem_info->plane[color_plane].size,
1582 sg = remap_tiled_color_plane_pages(obj,
1583 rem_info->plane[color_plane].offset,
1585 rem_info->plane[color_plane].width,
1586 rem_info->plane[color_plane].height,
1587 rem_info->plane[color_plane].src_stride,
1588 rem_info->plane[color_plane].dst_stride,
1595 static noinline struct sg_table *
1596 intel_remap_pages(struct intel_remapped_info *rem_info,
1597 struct drm_i915_gem_object *obj)
1599 unsigned int size = intel_remapped_info_size(rem_info);
1600 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1601 struct sg_table *st;
1602 struct scatterlist *sg;
1603 unsigned int gtt_offset = 0;
1607 /* Allocate target SG list. */
1608 st = kmalloc(sizeof(*st), GFP_KERNEL);
1612 ret = sg_alloc_table(st, size, GFP_KERNEL);
1619 for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
1620 sg = remap_color_plane_pages(rem_info, obj, i, st, sg, >t_offset);
1630 drm_dbg(&i915->drm, "Failed to create remapped mapping for object size %zu! (%ux%u tiles, %u pages)\n",
1631 obj->base.size, rem_info->plane[0].width,
1632 rem_info->plane[0].height, size);
1634 return ERR_PTR(ret);
1637 static noinline struct sg_table *
1638 intel_partial_pages(const struct i915_ggtt_view *view,
1639 struct drm_i915_gem_object *obj)
1641 struct sg_table *st;
1642 struct scatterlist *sg;
1643 unsigned int count = view->partial.size;
1646 st = kmalloc(sizeof(*st), GFP_KERNEL);
1650 ret = sg_alloc_table(st, count, GFP_KERNEL);
1656 sg = remap_contiguous_pages(obj, view->partial.offset, count, st, st->sgl);
1659 i915_sg_trim(st); /* Drop any unused tail entries. */
1666 return ERR_PTR(ret);
1670 i915_get_ggtt_vma_pages(struct i915_vma *vma)
1675 * The vma->pages are only valid within the lifespan of the borrowed
1676 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
1677 * must be the vma->pages. A simple rule is that vma->pages must only
1678 * be accessed when the obj->mm.pages are pinned.
1680 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
1682 switch (vma->ggtt_view.type) {
1684 GEM_BUG_ON(vma->ggtt_view.type);
1686 case I915_GGTT_VIEW_NORMAL:
1687 vma->pages = vma->obj->mm.pages;
1690 case I915_GGTT_VIEW_ROTATED:
1692 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
1695 case I915_GGTT_VIEW_REMAPPED:
1697 intel_remap_pages(&vma->ggtt_view.remapped, vma->obj);
1700 case I915_GGTT_VIEW_PARTIAL:
1701 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
1706 if (IS_ERR(vma->pages)) {
1707 ret = PTR_ERR(vma->pages);
1709 drm_err(&vma->vm->i915->drm,
1710 "Failed to get pages for VMA view type %u (%d)!\n",
1711 vma->ggtt_view.type, ret);