1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2020 Intel Corporation
4 */
5
6 #include <asm/set_memory.h>
7 #include <asm/smp.h>
8 #include <linux/types.h>
9 #include <linux/stop_machine.h>
10
11 #include <drm/i915_drm.h>
12 #include <drm/intel-gtt.h>
13
14 #include "gem/i915_gem_lmem.h"
15
16 #include "intel_ggtt_gmch.h"
17 #include "intel_gt.h"
18 #include "intel_gt_regs.h"
19 #include "intel_pci_config.h"
20 #include "i915_drv.h"
21 #include "i915_pci.h"
22 #include "i915_scatterlist.h"
23 #include "i915_utils.h"
24 #include "i915_vgpu.h"
25
26 #include "intel_gtt.h"
27 #include "gen8_ppgtt.h"
28
suspend_retains_ptes(struct i915_address_space * vm)29 static inline bool suspend_retains_ptes(struct i915_address_space *vm)
30 {
31 return GRAPHICS_VER(vm->i915) >= 8 &&
32 !HAS_LMEM(vm->i915) &&
33 vm->is_ggtt;
34 }
35
i915_ggtt_color_adjust(const struct drm_mm_node * node,unsigned long color,u64 * start,u64 * end)36 static void i915_ggtt_color_adjust(const struct drm_mm_node *node,
37 unsigned long color,
38 u64 *start,
39 u64 *end)
40 {
41 if (i915_node_color_differs(node, color))
42 *start += I915_GTT_PAGE_SIZE;
43
44 /*
45 * Also leave a space between the unallocated reserved node after the
46 * GTT and any objects within the GTT, i.e. we use the color adjustment
47 * to insert a guard page to prevent prefetches crossing over the
48 * GTT boundary.
49 */
50 node = list_next_entry(node, node_list);
51 if (node->color != color)
52 *end -= I915_GTT_PAGE_SIZE;
53 }
54
ggtt_init_hw(struct i915_ggtt * ggtt)55 static int ggtt_init_hw(struct i915_ggtt *ggtt)
56 {
57 struct drm_i915_private *i915 = ggtt->vm.i915;
58
59 i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
60
61 ggtt->vm.is_ggtt = true;
62
63 /* Only VLV supports read-only GGTT mappings */
64 ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
65
66 if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
67 ggtt->vm.mm.color_adjust = i915_ggtt_color_adjust;
68
69 if (ggtt->mappable_end) {
70 if (!io_mapping_init_wc(&ggtt->iomap,
71 ggtt->gmadr.start,
72 ggtt->mappable_end)) {
73 ggtt->vm.cleanup(&ggtt->vm);
74 return -EIO;
75 }
76
77 ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start,
78 ggtt->mappable_end);
79 }
80
81 intel_ggtt_init_fences(ggtt);
82
83 return 0;
84 }
85
86 /**
87 * i915_ggtt_init_hw - Initialize GGTT hardware
88 * @i915: i915 device
89 */
i915_ggtt_init_hw(struct drm_i915_private * i915)90 int i915_ggtt_init_hw(struct drm_i915_private *i915)
91 {
92 int ret;
93
94 /*
95 * Note that we use page colouring to enforce a guard page at the
96 * end of the address space. This is required as the CS may prefetch
97 * beyond the end of the batch buffer, across the page boundary,
98 * and beyond the end of the GTT if we do not provide a guard.
99 */
100 ret = ggtt_init_hw(to_gt(i915)->ggtt);
101 if (ret)
102 return ret;
103
104 return 0;
105 }
106
107 /*
108 * Return the value of the last GGTT pte cast to an u64, if
109 * the system is supposed to retain ptes across resume. 0 otherwise.
110 */
read_last_pte(struct i915_address_space * vm)111 static u64 read_last_pte(struct i915_address_space *vm)
112 {
113 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
114 gen8_pte_t __iomem *ptep;
115
116 if (!suspend_retains_ptes(vm))
117 return 0;
118
119 GEM_BUG_ON(GRAPHICS_VER(vm->i915) < 8);
120 ptep = (typeof(ptep))ggtt->gsm + (ggtt_total_entries(ggtt) - 1);
121 return readq(ptep);
122 }
123
124 /**
125 * i915_ggtt_suspend_vm - Suspend the memory mappings for a GGTT or DPT VM
126 * @vm: The VM to suspend the mappings for
127 *
128 * Suspend the memory mappings for all objects mapped to HW via the GGTT or a
129 * DPT page table.
130 */
i915_ggtt_suspend_vm(struct i915_address_space * vm)131 void i915_ggtt_suspend_vm(struct i915_address_space *vm)
132 {
133 struct i915_vma *vma, *vn;
134 int save_skip_rewrite;
135
136 drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
137
138 retry:
139 i915_gem_drain_freed_objects(vm->i915);
140
141 mutex_lock(&vm->mutex);
142
143 /*
144 * Skip rewriting PTE on VMA unbind.
145 * FIXME: Use an argument to i915_vma_unbind() instead?
146 */
147 save_skip_rewrite = vm->skip_pte_rewrite;
148 vm->skip_pte_rewrite = true;
149
150 list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) {
151 struct drm_i915_gem_object *obj = vma->obj;
152
153 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
154
155 if (i915_vma_is_pinned(vma) || !i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
156 continue;
157
158 /* unlikely to race when GPU is idle, so no worry about slowpath.. */
159 if (WARN_ON(!i915_gem_object_trylock(obj, NULL))) {
160 /*
161 * No dead objects should appear here, GPU should be
162 * completely idle, and userspace suspended
163 */
164 i915_gem_object_get(obj);
165
166 mutex_unlock(&vm->mutex);
167
168 i915_gem_object_lock(obj, NULL);
169 GEM_WARN_ON(i915_vma_unbind(vma));
170 i915_gem_object_unlock(obj);
171 i915_gem_object_put(obj);
172
173 vm->skip_pte_rewrite = save_skip_rewrite;
174 goto retry;
175 }
176
177 if (!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) {
178 i915_vma_wait_for_bind(vma);
179
180 __i915_vma_evict(vma, false);
181 drm_mm_remove_node(&vma->node);
182 }
183
184 i915_gem_object_unlock(obj);
185 }
186
187 if (!suspend_retains_ptes(vm))
188 vm->clear_range(vm, 0, vm->total);
189 else
190 i915_vm_to_ggtt(vm)->probed_pte = read_last_pte(vm);
191
192 vm->skip_pte_rewrite = save_skip_rewrite;
193
194 mutex_unlock(&vm->mutex);
195 }
196
i915_ggtt_suspend(struct i915_ggtt * ggtt)197 void i915_ggtt_suspend(struct i915_ggtt *ggtt)
198 {
199 i915_ggtt_suspend_vm(&ggtt->vm);
200 ggtt->invalidate(ggtt);
201
202 intel_gt_check_and_clear_faults(ggtt->vm.gt);
203 }
204
gen6_ggtt_invalidate(struct i915_ggtt * ggtt)205 void gen6_ggtt_invalidate(struct i915_ggtt *ggtt)
206 {
207 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
208
209 spin_lock_irq(&uncore->lock);
210 intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
211 intel_uncore_read_fw(uncore, GFX_FLSH_CNTL_GEN6);
212 spin_unlock_irq(&uncore->lock);
213 }
214
gen8_ggtt_invalidate(struct i915_ggtt * ggtt)215 static void gen8_ggtt_invalidate(struct i915_ggtt *ggtt)
216 {
217 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
218
219 /*
220 * Note that as an uncached mmio write, this will flush the
221 * WCB of the writes into the GGTT before it triggers the invalidate.
222 */
223 intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
224 }
225
guc_ggtt_invalidate(struct i915_ggtt * ggtt)226 static void guc_ggtt_invalidate(struct i915_ggtt *ggtt)
227 {
228 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
229 struct drm_i915_private *i915 = ggtt->vm.i915;
230
231 gen8_ggtt_invalidate(ggtt);
232
233 if (GRAPHICS_VER(i915) >= 12)
234 intel_uncore_write_fw(uncore, GEN12_GUC_TLB_INV_CR,
235 GEN12_GUC_TLB_INV_CR_INVALIDATE);
236 else
237 intel_uncore_write_fw(uncore, GEN8_GTCR, GEN8_GTCR_INVALIDATE);
238 }
239
gen8_ggtt_pte_encode(dma_addr_t addr,enum i915_cache_level level,u32 flags)240 u64 gen8_ggtt_pte_encode(dma_addr_t addr,
241 enum i915_cache_level level,
242 u32 flags)
243 {
244 gen8_pte_t pte = addr | GEN8_PAGE_PRESENT;
245
246 if (flags & PTE_LM)
247 pte |= GEN12_GGTT_PTE_LM;
248
249 return pte;
250 }
251
gen8_set_pte(void __iomem * addr,gen8_pte_t pte)252 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
253 {
254 writeq(pte, addr);
255 }
256
gen8_ggtt_insert_page(struct i915_address_space * vm,dma_addr_t addr,u64 offset,enum i915_cache_level level,u32 flags)257 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
258 dma_addr_t addr,
259 u64 offset,
260 enum i915_cache_level level,
261 u32 flags)
262 {
263 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
264 gen8_pte_t __iomem *pte =
265 (gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
266
267 gen8_set_pte(pte, gen8_ggtt_pte_encode(addr, level, flags));
268
269 ggtt->invalidate(ggtt);
270 }
271
gen8_ggtt_insert_entries(struct i915_address_space * vm,struct i915_vma_resource * vma_res,enum i915_cache_level level,u32 flags)272 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
273 struct i915_vma_resource *vma_res,
274 enum i915_cache_level level,
275 u32 flags)
276 {
277 const gen8_pte_t pte_encode = gen8_ggtt_pte_encode(0, level, flags);
278 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
279 gen8_pte_t __iomem *gte;
280 gen8_pte_t __iomem *end;
281 struct sgt_iter iter;
282 dma_addr_t addr;
283
284 /*
285 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
286 * not to allow the user to override access to a read only page.
287 */
288
289 gte = (gen8_pte_t __iomem *)ggtt->gsm;
290 gte += vma_res->start / I915_GTT_PAGE_SIZE;
291 end = gte + vma_res->node_size / I915_GTT_PAGE_SIZE;
292
293 for_each_sgt_daddr(addr, iter, vma_res->bi.pages)
294 gen8_set_pte(gte++, pte_encode | addr);
295 GEM_BUG_ON(gte > end);
296
297 /* Fill the allocated but "unused" space beyond the end of the buffer */
298 while (gte < end)
299 gen8_set_pte(gte++, vm->scratch[0]->encode);
300
301 /*
302 * We want to flush the TLBs only after we're certain all the PTE
303 * updates have finished.
304 */
305 ggtt->invalidate(ggtt);
306 }
307
gen6_ggtt_insert_page(struct i915_address_space * vm,dma_addr_t addr,u64 offset,enum i915_cache_level level,u32 flags)308 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
309 dma_addr_t addr,
310 u64 offset,
311 enum i915_cache_level level,
312 u32 flags)
313 {
314 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
315 gen6_pte_t __iomem *pte =
316 (gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
317
318 iowrite32(vm->pte_encode(addr, level, flags), pte);
319
320 ggtt->invalidate(ggtt);
321 }
322
323 /*
324 * Binds an object into the global gtt with the specified cache level.
325 * The object will be accessible to the GPU via commands whose operands
326 * reference offsets within the global GTT as well as accessible by the GPU
327 * through the GMADR mapped BAR (i915->mm.gtt->gtt).
328 */
gen6_ggtt_insert_entries(struct i915_address_space * vm,struct i915_vma_resource * vma_res,enum i915_cache_level level,u32 flags)329 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
330 struct i915_vma_resource *vma_res,
331 enum i915_cache_level level,
332 u32 flags)
333 {
334 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
335 gen6_pte_t __iomem *gte;
336 gen6_pte_t __iomem *end;
337 struct sgt_iter iter;
338 dma_addr_t addr;
339
340 gte = (gen6_pte_t __iomem *)ggtt->gsm;
341 gte += vma_res->start / I915_GTT_PAGE_SIZE;
342 end = gte + vma_res->node_size / I915_GTT_PAGE_SIZE;
343
344 for_each_sgt_daddr(addr, iter, vma_res->bi.pages)
345 iowrite32(vm->pte_encode(addr, level, flags), gte++);
346 GEM_BUG_ON(gte > end);
347
348 /* Fill the allocated but "unused" space beyond the end of the buffer */
349 while (gte < end)
350 iowrite32(vm->scratch[0]->encode, gte++);
351
352 /*
353 * We want to flush the TLBs only after we're certain all the PTE
354 * updates have finished.
355 */
356 ggtt->invalidate(ggtt);
357 }
358
nop_clear_range(struct i915_address_space * vm,u64 start,u64 length)359 static void nop_clear_range(struct i915_address_space *vm,
360 u64 start, u64 length)
361 {
362 }
363
gen8_ggtt_clear_range(struct i915_address_space * vm,u64 start,u64 length)364 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
365 u64 start, u64 length)
366 {
367 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
368 unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
369 unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
370 const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
371 gen8_pte_t __iomem *gtt_base =
372 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
373 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
374 int i;
375
376 if (WARN(num_entries > max_entries,
377 "First entry = %d; Num entries = %d (max=%d)\n",
378 first_entry, num_entries, max_entries))
379 num_entries = max_entries;
380
381 for (i = 0; i < num_entries; i++)
382 gen8_set_pte(>t_base[i], scratch_pte);
383 }
384
bxt_vtd_ggtt_wa(struct i915_address_space * vm)385 static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
386 {
387 /*
388 * Make sure the internal GAM fifo has been cleared of all GTT
389 * writes before exiting stop_machine(). This guarantees that
390 * any aperture accesses waiting to start in another process
391 * cannot back up behind the GTT writes causing a hang.
392 * The register can be any arbitrary GAM register.
393 */
394 intel_uncore_posting_read_fw(vm->gt->uncore, GFX_FLSH_CNTL_GEN6);
395 }
396
397 struct insert_page {
398 struct i915_address_space *vm;
399 dma_addr_t addr;
400 u64 offset;
401 enum i915_cache_level level;
402 };
403
bxt_vtd_ggtt_insert_page__cb(void * _arg)404 static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
405 {
406 struct insert_page *arg = _arg;
407
408 gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
409 bxt_vtd_ggtt_wa(arg->vm);
410
411 return 0;
412 }
413
bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space * vm,dma_addr_t addr,u64 offset,enum i915_cache_level level,u32 unused)414 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
415 dma_addr_t addr,
416 u64 offset,
417 enum i915_cache_level level,
418 u32 unused)
419 {
420 struct insert_page arg = { vm, addr, offset, level };
421
422 stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
423 }
424
425 struct insert_entries {
426 struct i915_address_space *vm;
427 struct i915_vma_resource *vma_res;
428 enum i915_cache_level level;
429 u32 flags;
430 };
431
bxt_vtd_ggtt_insert_entries__cb(void * _arg)432 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
433 {
434 struct insert_entries *arg = _arg;
435
436 gen8_ggtt_insert_entries(arg->vm, arg->vma_res, arg->level, arg->flags);
437 bxt_vtd_ggtt_wa(arg->vm);
438
439 return 0;
440 }
441
bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space * vm,struct i915_vma_resource * vma_res,enum i915_cache_level level,u32 flags)442 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
443 struct i915_vma_resource *vma_res,
444 enum i915_cache_level level,
445 u32 flags)
446 {
447 struct insert_entries arg = { vm, vma_res, level, flags };
448
449 stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
450 }
451
gen6_ggtt_clear_range(struct i915_address_space * vm,u64 start,u64 length)452 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
453 u64 start, u64 length)
454 {
455 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
456 unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
457 unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
458 gen6_pte_t scratch_pte, __iomem *gtt_base =
459 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
460 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
461 int i;
462
463 if (WARN(num_entries > max_entries,
464 "First entry = %d; Num entries = %d (max=%d)\n",
465 first_entry, num_entries, max_entries))
466 num_entries = max_entries;
467
468 scratch_pte = vm->scratch[0]->encode;
469 for (i = 0; i < num_entries; i++)
470 iowrite32(scratch_pte, >t_base[i]);
471 }
472
intel_ggtt_bind_vma(struct i915_address_space * vm,struct i915_vm_pt_stash * stash,struct i915_vma_resource * vma_res,enum i915_cache_level cache_level,u32 flags)473 void intel_ggtt_bind_vma(struct i915_address_space *vm,
474 struct i915_vm_pt_stash *stash,
475 struct i915_vma_resource *vma_res,
476 enum i915_cache_level cache_level,
477 u32 flags)
478 {
479 u32 pte_flags;
480
481 if (vma_res->bound_flags & (~flags & I915_VMA_BIND_MASK))
482 return;
483
484 vma_res->bound_flags |= flags;
485
486 /* Applicable to VLV (gen8+ do not support RO in the GGTT) */
487 pte_flags = 0;
488 if (vma_res->bi.readonly)
489 pte_flags |= PTE_READ_ONLY;
490 if (vma_res->bi.lmem)
491 pte_flags |= PTE_LM;
492
493 vm->insert_entries(vm, vma_res, cache_level, pte_flags);
494 vma_res->page_sizes_gtt = I915_GTT_PAGE_SIZE;
495 }
496
intel_ggtt_unbind_vma(struct i915_address_space * vm,struct i915_vma_resource * vma_res)497 void intel_ggtt_unbind_vma(struct i915_address_space *vm,
498 struct i915_vma_resource *vma_res)
499 {
500 vm->clear_range(vm, vma_res->start, vma_res->vma_size);
501 }
502
503 /*
504 * Reserve the top of the GuC address space for firmware images. Addresses
505 * beyond GUC_GGTT_TOP in the GuC address space are inaccessible by GuC,
506 * which makes for a suitable range to hold GuC/HuC firmware images if the
507 * size of the GGTT is 4G. However, on a 32-bit platform the size of the GGTT
508 * is limited to 2G, which is less than GUC_GGTT_TOP, but we reserve a chunk
509 * of the same size anyway, which is far more than needed, to keep the logic
510 * in uc_fw_ggtt_offset() simple.
511 */
512 #define GUC_TOP_RESERVE_SIZE (SZ_4G - GUC_GGTT_TOP)
513
ggtt_reserve_guc_top(struct i915_ggtt * ggtt)514 static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
515 {
516 u64 offset;
517 int ret;
518
519 if (!intel_uc_uses_guc(&ggtt->vm.gt->uc))
520 return 0;
521
522 GEM_BUG_ON(ggtt->vm.total <= GUC_TOP_RESERVE_SIZE);
523 offset = ggtt->vm.total - GUC_TOP_RESERVE_SIZE;
524
525 ret = i915_gem_gtt_reserve(&ggtt->vm, NULL, &ggtt->uc_fw,
526 GUC_TOP_RESERVE_SIZE, offset,
527 I915_COLOR_UNEVICTABLE, PIN_NOEVICT);
528 if (ret)
529 drm_dbg(&ggtt->vm.i915->drm,
530 "Failed to reserve top of GGTT for GuC\n");
531
532 return ret;
533 }
534
ggtt_release_guc_top(struct i915_ggtt * ggtt)535 static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
536 {
537 if (drm_mm_node_allocated(&ggtt->uc_fw))
538 drm_mm_remove_node(&ggtt->uc_fw);
539 }
540
cleanup_init_ggtt(struct i915_ggtt * ggtt)541 static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
542 {
543 ggtt_release_guc_top(ggtt);
544 if (drm_mm_node_allocated(&ggtt->error_capture))
545 drm_mm_remove_node(&ggtt->error_capture);
546 mutex_destroy(&ggtt->error_mutex);
547 }
548
init_ggtt(struct i915_ggtt * ggtt)549 static int init_ggtt(struct i915_ggtt *ggtt)
550 {
551 /*
552 * Let GEM Manage all of the aperture.
553 *
554 * However, leave one page at the end still bound to the scratch page.
555 * There are a number of places where the hardware apparently prefetches
556 * past the end of the object, and we've seen multiple hangs with the
557 * GPU head pointer stuck in a batchbuffer bound at the last page of the
558 * aperture. One page should be enough to keep any prefetching inside
559 * of the aperture.
560 */
561 unsigned long hole_start, hole_end;
562 struct drm_mm_node *entry;
563 int ret;
564
565 ggtt->pte_lost = true;
566
567 /*
568 * GuC requires all resources that we're sharing with it to be placed in
569 * non-WOPCM memory. If GuC is not present or not in use we still need a
570 * small bias as ring wraparound at offset 0 sometimes hangs. No idea
571 * why.
572 */
573 ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
574 intel_wopcm_guc_size(&ggtt->vm.i915->wopcm));
575
576 ret = intel_vgt_balloon(ggtt);
577 if (ret)
578 return ret;
579
580 mutex_init(&ggtt->error_mutex);
581 if (ggtt->mappable_end) {
582 /*
583 * Reserve a mappable slot for our lockless error capture.
584 *
585 * We strongly prefer taking address 0x0 in order to protect
586 * other critical buffers against accidental overwrites,
587 * as writing to address 0 is a very common mistake.
588 *
589 * Since 0 may already be in use by the system (e.g. the BIOS
590 * framebuffer), we let the reservation fail quietly and hope
591 * 0 remains reserved always.
592 *
593 * If we fail to reserve 0, and then fail to find any space
594 * for an error-capture, remain silent. We can afford not
595 * to reserve an error_capture node as we have fallback
596 * paths, and we trust that 0 will remain reserved. However,
597 * the only likely reason for failure to insert is a driver
598 * bug, which we expect to cause other failures...
599 */
600 ggtt->error_capture.size = I915_GTT_PAGE_SIZE;
601 ggtt->error_capture.color = I915_COLOR_UNEVICTABLE;
602 if (drm_mm_reserve_node(&ggtt->vm.mm, &ggtt->error_capture))
603 drm_mm_insert_node_in_range(&ggtt->vm.mm,
604 &ggtt->error_capture,
605 ggtt->error_capture.size, 0,
606 ggtt->error_capture.color,
607 0, ggtt->mappable_end,
608 DRM_MM_INSERT_LOW);
609 }
610 if (drm_mm_node_allocated(&ggtt->error_capture))
611 drm_dbg(&ggtt->vm.i915->drm,
612 "Reserved GGTT:[%llx, %llx] for use by error capture\n",
613 ggtt->error_capture.start,
614 ggtt->error_capture.start + ggtt->error_capture.size);
615
616 /*
617 * The upper portion of the GuC address space has a sizeable hole
618 * (several MB) that is inaccessible by GuC. Reserve this range within
619 * GGTT as it can comfortably hold GuC/HuC firmware images.
620 */
621 ret = ggtt_reserve_guc_top(ggtt);
622 if (ret)
623 goto err;
624
625 /* Clear any non-preallocated blocks */
626 drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
627 drm_dbg(&ggtt->vm.i915->drm,
628 "clearing unused GTT space: [%lx, %lx]\n",
629 hole_start, hole_end);
630 ggtt->vm.clear_range(&ggtt->vm, hole_start,
631 hole_end - hole_start);
632 }
633
634 /* And finally clear the reserved guard page */
635 ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
636
637 return 0;
638
639 err:
640 cleanup_init_ggtt(ggtt);
641 return ret;
642 }
643
aliasing_gtt_bind_vma(struct i915_address_space * vm,struct i915_vm_pt_stash * stash,struct i915_vma_resource * vma_res,enum i915_cache_level cache_level,u32 flags)644 static void aliasing_gtt_bind_vma(struct i915_address_space *vm,
645 struct i915_vm_pt_stash *stash,
646 struct i915_vma_resource *vma_res,
647 enum i915_cache_level cache_level,
648 u32 flags)
649 {
650 u32 pte_flags;
651
652 /* Currently applicable only to VLV */
653 pte_flags = 0;
654 if (vma_res->bi.readonly)
655 pte_flags |= PTE_READ_ONLY;
656
657 if (flags & I915_VMA_LOCAL_BIND)
658 ppgtt_bind_vma(&i915_vm_to_ggtt(vm)->alias->vm,
659 stash, vma_res, cache_level, flags);
660
661 if (flags & I915_VMA_GLOBAL_BIND)
662 vm->insert_entries(vm, vma_res, cache_level, pte_flags);
663
664 vma_res->bound_flags |= flags;
665 }
666
aliasing_gtt_unbind_vma(struct i915_address_space * vm,struct i915_vma_resource * vma_res)667 static void aliasing_gtt_unbind_vma(struct i915_address_space *vm,
668 struct i915_vma_resource *vma_res)
669 {
670 if (vma_res->bound_flags & I915_VMA_GLOBAL_BIND)
671 vm->clear_range(vm, vma_res->start, vma_res->vma_size);
672
673 if (vma_res->bound_flags & I915_VMA_LOCAL_BIND)
674 ppgtt_unbind_vma(&i915_vm_to_ggtt(vm)->alias->vm, vma_res);
675 }
676
init_aliasing_ppgtt(struct i915_ggtt * ggtt)677 static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
678 {
679 struct i915_vm_pt_stash stash = {};
680 struct i915_ppgtt *ppgtt;
681 int err;
682
683 ppgtt = i915_ppgtt_create(ggtt->vm.gt, 0);
684 if (IS_ERR(ppgtt))
685 return PTR_ERR(ppgtt);
686
687 if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
688 err = -ENODEV;
689 goto err_ppgtt;
690 }
691
692 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, ggtt->vm.total);
693 if (err)
694 goto err_ppgtt;
695
696 i915_gem_object_lock(ppgtt->vm.scratch[0], NULL);
697 err = i915_vm_map_pt_stash(&ppgtt->vm, &stash);
698 i915_gem_object_unlock(ppgtt->vm.scratch[0]);
699 if (err)
700 goto err_stash;
701
702 /*
703 * Note we only pre-allocate as far as the end of the global
704 * GTT. On 48b / 4-level page-tables, the difference is very,
705 * very significant! We have to preallocate as GVT/vgpu does
706 * not like the page directory disappearing.
707 */
708 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, ggtt->vm.total);
709
710 ggtt->alias = ppgtt;
711 ggtt->vm.bind_async_flags |= ppgtt->vm.bind_async_flags;
712
713 GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != intel_ggtt_bind_vma);
714 ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
715
716 GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != intel_ggtt_unbind_vma);
717 ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
718
719 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
720 return 0;
721
722 err_stash:
723 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
724 err_ppgtt:
725 i915_vm_put(&ppgtt->vm);
726 return err;
727 }
728
fini_aliasing_ppgtt(struct i915_ggtt * ggtt)729 static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
730 {
731 struct i915_ppgtt *ppgtt;
732
733 ppgtt = fetch_and_zero(&ggtt->alias);
734 if (!ppgtt)
735 return;
736
737 i915_vm_put(&ppgtt->vm);
738
739 ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
740 ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
741 }
742
i915_init_ggtt(struct drm_i915_private * i915)743 int i915_init_ggtt(struct drm_i915_private *i915)
744 {
745 int ret;
746
747 ret = init_ggtt(to_gt(i915)->ggtt);
748 if (ret)
749 return ret;
750
751 if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
752 ret = init_aliasing_ppgtt(to_gt(i915)->ggtt);
753 if (ret)
754 cleanup_init_ggtt(to_gt(i915)->ggtt);
755 }
756
757 return 0;
758 }
759
ggtt_cleanup_hw(struct i915_ggtt * ggtt)760 static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
761 {
762 struct i915_vma *vma, *vn;
763
764 flush_workqueue(ggtt->vm.i915->wq);
765 i915_gem_drain_freed_objects(ggtt->vm.i915);
766
767 mutex_lock(&ggtt->vm.mutex);
768
769 ggtt->vm.skip_pte_rewrite = true;
770
771 list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link) {
772 struct drm_i915_gem_object *obj = vma->obj;
773 bool trylock;
774
775 trylock = i915_gem_object_trylock(obj, NULL);
776 WARN_ON(!trylock);
777
778 WARN_ON(__i915_vma_unbind(vma));
779 if (trylock)
780 i915_gem_object_unlock(obj);
781 }
782
783 if (drm_mm_node_allocated(&ggtt->error_capture))
784 drm_mm_remove_node(&ggtt->error_capture);
785 mutex_destroy(&ggtt->error_mutex);
786
787 ggtt_release_guc_top(ggtt);
788 intel_vgt_deballoon(ggtt);
789
790 ggtt->vm.cleanup(&ggtt->vm);
791
792 mutex_unlock(&ggtt->vm.mutex);
793 i915_address_space_fini(&ggtt->vm);
794
795 arch_phys_wc_del(ggtt->mtrr);
796
797 if (ggtt->iomap.size)
798 io_mapping_fini(&ggtt->iomap);
799 }
800
801 /**
802 * i915_ggtt_driver_release - Clean up GGTT hardware initialization
803 * @i915: i915 device
804 */
i915_ggtt_driver_release(struct drm_i915_private * i915)805 void i915_ggtt_driver_release(struct drm_i915_private *i915)
806 {
807 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
808
809 fini_aliasing_ppgtt(ggtt);
810
811 intel_ggtt_fini_fences(ggtt);
812 ggtt_cleanup_hw(ggtt);
813 }
814
815 /**
816 * i915_ggtt_driver_late_release - Cleanup of GGTT that needs to be done after
817 * all free objects have been drained.
818 * @i915: i915 device
819 */
i915_ggtt_driver_late_release(struct drm_i915_private * i915)820 void i915_ggtt_driver_late_release(struct drm_i915_private *i915)
821 {
822 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
823
824 GEM_WARN_ON(kref_read(&ggtt->vm.resv_ref) != 1);
825 dma_resv_fini(&ggtt->vm._resv);
826 }
827
gen6_get_total_gtt_size(u16 snb_gmch_ctl)828 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
829 {
830 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
831 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
832 return snb_gmch_ctl << 20;
833 }
834
gen8_get_total_gtt_size(u16 bdw_gmch_ctl)835 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
836 {
837 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
838 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
839 if (bdw_gmch_ctl)
840 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
841
842 #ifdef CONFIG_X86_32
843 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
844 if (bdw_gmch_ctl > 4)
845 bdw_gmch_ctl = 4;
846 #endif
847
848 return bdw_gmch_ctl << 20;
849 }
850
chv_get_total_gtt_size(u16 gmch_ctrl)851 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
852 {
853 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
854 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
855
856 if (gmch_ctrl)
857 return 1 << (20 + gmch_ctrl);
858
859 return 0;
860 }
861
gen6_gttmmadr_size(struct drm_i915_private * i915)862 static unsigned int gen6_gttmmadr_size(struct drm_i915_private *i915)
863 {
864 /*
865 * GEN6: GTTMMADR size is 4MB and GTTADR starts at 2MB offset
866 * GEN8: GTTMMADR size is 16MB and GTTADR starts at 8MB offset
867 */
868 GEM_BUG_ON(GRAPHICS_VER(i915) < 6);
869 return (GRAPHICS_VER(i915) < 8) ? SZ_4M : SZ_16M;
870 }
871
gen6_gttadr_offset(struct drm_i915_private * i915)872 static unsigned int gen6_gttadr_offset(struct drm_i915_private *i915)
873 {
874 return gen6_gttmmadr_size(i915) / 2;
875 }
876
ggtt_probe_common(struct i915_ggtt * ggtt,u64 size)877 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
878 {
879 struct drm_i915_private *i915 = ggtt->vm.i915;
880 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
881 phys_addr_t phys_addr;
882 u32 pte_flags;
883 int ret;
884
885 GEM_WARN_ON(pci_resource_len(pdev, GTTMMADR_BAR) != gen6_gttmmadr_size(i915));
886 phys_addr = pci_resource_start(pdev, GTTMMADR_BAR) + gen6_gttadr_offset(i915);
887
888 /*
889 * On BXT+/ICL+ writes larger than 64 bit to the GTT pagetable range
890 * will be dropped. For WC mappings in general we have 64 byte burst
891 * writes when the WC buffer is flushed, so we can't use it, but have to
892 * resort to an uncached mapping. The WC issue is easily caught by the
893 * readback check when writing GTT PTE entries.
894 */
895 if (IS_GEN9_LP(i915) || GRAPHICS_VER(i915) >= 11)
896 ggtt->gsm = ioremap(phys_addr, size);
897 else
898 ggtt->gsm = ioremap_wc(phys_addr, size);
899 if (!ggtt->gsm) {
900 drm_err(&i915->drm, "Failed to map the ggtt page table\n");
901 return -ENOMEM;
902 }
903
904 kref_init(&ggtt->vm.resv_ref);
905 ret = setup_scratch_page(&ggtt->vm);
906 if (ret) {
907 drm_err(&i915->drm, "Scratch setup failed\n");
908 /* iounmap will also get called at remove, but meh */
909 iounmap(ggtt->gsm);
910 return ret;
911 }
912
913 pte_flags = 0;
914 if (i915_gem_object_is_lmem(ggtt->vm.scratch[0]))
915 pte_flags |= PTE_LM;
916
917 ggtt->vm.scratch[0]->encode =
918 ggtt->vm.pte_encode(px_dma(ggtt->vm.scratch[0]),
919 I915_CACHE_NONE, pte_flags);
920
921 return 0;
922 }
923
gen6_gmch_remove(struct i915_address_space * vm)924 static void gen6_gmch_remove(struct i915_address_space *vm)
925 {
926 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
927
928 iounmap(ggtt->gsm);
929 free_scratch(vm);
930 }
931
pci_resource(struct pci_dev * pdev,int bar)932 static struct resource pci_resource(struct pci_dev *pdev, int bar)
933 {
934 return (struct resource)DEFINE_RES_MEM(pci_resource_start(pdev, bar),
935 pci_resource_len(pdev, bar));
936 }
937
gen8_gmch_probe(struct i915_ggtt * ggtt)938 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
939 {
940 struct drm_i915_private *i915 = ggtt->vm.i915;
941 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
942 unsigned int size;
943 u16 snb_gmch_ctl;
944
945 if (!HAS_LMEM(i915)) {
946 if (!i915_pci_resource_valid(pdev, GTT_APERTURE_BAR))
947 return -ENXIO;
948
949 ggtt->gmadr = pci_resource(pdev, GTT_APERTURE_BAR);
950 ggtt->mappable_end = resource_size(&ggtt->gmadr);
951 }
952
953 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
954 if (IS_CHERRYVIEW(i915))
955 size = chv_get_total_gtt_size(snb_gmch_ctl);
956 else
957 size = gen8_get_total_gtt_size(snb_gmch_ctl);
958
959 ggtt->vm.alloc_pt_dma = alloc_pt_dma;
960 ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
961 ggtt->vm.lmem_pt_obj_flags = I915_BO_ALLOC_PM_EARLY;
962
963 ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
964 ggtt->vm.cleanup = gen6_gmch_remove;
965 ggtt->vm.insert_page = gen8_ggtt_insert_page;
966 ggtt->vm.clear_range = nop_clear_range;
967 if (intel_scanout_needs_vtd_wa(i915))
968 ggtt->vm.clear_range = gen8_ggtt_clear_range;
969
970 ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
971
972 /*
973 * Serialize GTT updates with aperture access on BXT if VT-d is on,
974 * and always on CHV.
975 */
976 if (intel_vm_no_concurrent_access_wa(i915)) {
977 ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
978 ggtt->vm.insert_page = bxt_vtd_ggtt_insert_page__BKL;
979
980 /*
981 * Calling stop_machine() version of GGTT update function
982 * at error capture/reset path will raise lockdep warning.
983 * Allow calling gen8_ggtt_insert_* directly at reset path
984 * which is safe from parallel GGTT updates.
985 */
986 ggtt->vm.raw_insert_page = gen8_ggtt_insert_page;
987 ggtt->vm.raw_insert_entries = gen8_ggtt_insert_entries;
988
989 ggtt->vm.bind_async_flags =
990 I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
991 }
992
993 ggtt->invalidate = gen8_ggtt_invalidate;
994
995 ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
996 ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
997
998 ggtt->vm.pte_encode = gen8_ggtt_pte_encode;
999
1000 setup_private_pat(ggtt->vm.gt->uncore);
1001
1002 return ggtt_probe_common(ggtt, size);
1003 }
1004
snb_pte_encode(dma_addr_t addr,enum i915_cache_level level,u32 flags)1005 static u64 snb_pte_encode(dma_addr_t addr,
1006 enum i915_cache_level level,
1007 u32 flags)
1008 {
1009 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1010
1011 switch (level) {
1012 case I915_CACHE_L3_LLC:
1013 case I915_CACHE_LLC:
1014 pte |= GEN6_PTE_CACHE_LLC;
1015 break;
1016 case I915_CACHE_NONE:
1017 pte |= GEN6_PTE_UNCACHED;
1018 break;
1019 default:
1020 MISSING_CASE(level);
1021 }
1022
1023 return pte;
1024 }
1025
ivb_pte_encode(dma_addr_t addr,enum i915_cache_level level,u32 flags)1026 static u64 ivb_pte_encode(dma_addr_t addr,
1027 enum i915_cache_level level,
1028 u32 flags)
1029 {
1030 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1031
1032 switch (level) {
1033 case I915_CACHE_L3_LLC:
1034 pte |= GEN7_PTE_CACHE_L3_LLC;
1035 break;
1036 case I915_CACHE_LLC:
1037 pte |= GEN6_PTE_CACHE_LLC;
1038 break;
1039 case I915_CACHE_NONE:
1040 pte |= GEN6_PTE_UNCACHED;
1041 break;
1042 default:
1043 MISSING_CASE(level);
1044 }
1045
1046 return pte;
1047 }
1048
byt_pte_encode(dma_addr_t addr,enum i915_cache_level level,u32 flags)1049 static u64 byt_pte_encode(dma_addr_t addr,
1050 enum i915_cache_level level,
1051 u32 flags)
1052 {
1053 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1054
1055 if (!(flags & PTE_READ_ONLY))
1056 pte |= BYT_PTE_WRITEABLE;
1057
1058 if (level != I915_CACHE_NONE)
1059 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
1060
1061 return pte;
1062 }
1063
hsw_pte_encode(dma_addr_t addr,enum i915_cache_level level,u32 flags)1064 static u64 hsw_pte_encode(dma_addr_t addr,
1065 enum i915_cache_level level,
1066 u32 flags)
1067 {
1068 gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1069
1070 if (level != I915_CACHE_NONE)
1071 pte |= HSW_WB_LLC_AGE3;
1072
1073 return pte;
1074 }
1075
iris_pte_encode(dma_addr_t addr,enum i915_cache_level level,u32 flags)1076 static u64 iris_pte_encode(dma_addr_t addr,
1077 enum i915_cache_level level,
1078 u32 flags)
1079 {
1080 gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1081
1082 switch (level) {
1083 case I915_CACHE_NONE:
1084 break;
1085 case I915_CACHE_WT:
1086 pte |= HSW_WT_ELLC_LLC_AGE3;
1087 break;
1088 default:
1089 pte |= HSW_WB_ELLC_LLC_AGE3;
1090 break;
1091 }
1092
1093 return pte;
1094 }
1095
gen6_gmch_probe(struct i915_ggtt * ggtt)1096 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
1097 {
1098 struct drm_i915_private *i915 = ggtt->vm.i915;
1099 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1100 unsigned int size;
1101 u16 snb_gmch_ctl;
1102
1103 if (!i915_pci_resource_valid(pdev, GTT_APERTURE_BAR))
1104 return -ENXIO;
1105
1106 ggtt->gmadr = pci_resource(pdev, GTT_APERTURE_BAR);
1107 ggtt->mappable_end = resource_size(&ggtt->gmadr);
1108
1109 /*
1110 * 64/512MB is the current min/max we actually know of, but this is
1111 * just a coarse sanity check.
1112 */
1113 if (ggtt->mappable_end < (64 << 20) ||
1114 ggtt->mappable_end > (512 << 20)) {
1115 drm_err(&i915->drm, "Unknown GMADR size (%pa)\n",
1116 &ggtt->mappable_end);
1117 return -ENXIO;
1118 }
1119
1120 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1121
1122 size = gen6_get_total_gtt_size(snb_gmch_ctl);
1123 ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
1124
1125 ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1126 ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
1127
1128 ggtt->vm.clear_range = nop_clear_range;
1129 if (!HAS_FULL_PPGTT(i915) || intel_scanout_needs_vtd_wa(i915))
1130 ggtt->vm.clear_range = gen6_ggtt_clear_range;
1131 ggtt->vm.insert_page = gen6_ggtt_insert_page;
1132 ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
1133 ggtt->vm.cleanup = gen6_gmch_remove;
1134
1135 ggtt->invalidate = gen6_ggtt_invalidate;
1136
1137 if (HAS_EDRAM(i915))
1138 ggtt->vm.pte_encode = iris_pte_encode;
1139 else if (IS_HASWELL(i915))
1140 ggtt->vm.pte_encode = hsw_pte_encode;
1141 else if (IS_VALLEYVIEW(i915))
1142 ggtt->vm.pte_encode = byt_pte_encode;
1143 else if (GRAPHICS_VER(i915) >= 7)
1144 ggtt->vm.pte_encode = ivb_pte_encode;
1145 else
1146 ggtt->vm.pte_encode = snb_pte_encode;
1147
1148 ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
1149 ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
1150
1151 return ggtt_probe_common(ggtt, size);
1152 }
1153
ggtt_probe_hw(struct i915_ggtt * ggtt,struct intel_gt * gt)1154 static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
1155 {
1156 struct drm_i915_private *i915 = gt->i915;
1157 int ret;
1158
1159 ggtt->vm.gt = gt;
1160 ggtt->vm.i915 = i915;
1161 ggtt->vm.dma = i915->drm.dev;
1162 dma_resv_init(&ggtt->vm._resv);
1163
1164 if (GRAPHICS_VER(i915) >= 8)
1165 ret = gen8_gmch_probe(ggtt);
1166 else if (GRAPHICS_VER(i915) >= 6)
1167 ret = gen6_gmch_probe(ggtt);
1168 else
1169 ret = intel_ggtt_gmch_probe(ggtt);
1170
1171 if (ret) {
1172 dma_resv_fini(&ggtt->vm._resv);
1173 return ret;
1174 }
1175
1176 if ((ggtt->vm.total - 1) >> 32) {
1177 drm_err(&i915->drm,
1178 "We never expected a Global GTT with more than 32bits"
1179 " of address space! Found %lldM!\n",
1180 ggtt->vm.total >> 20);
1181 ggtt->vm.total = 1ULL << 32;
1182 ggtt->mappable_end =
1183 min_t(u64, ggtt->mappable_end, ggtt->vm.total);
1184 }
1185
1186 if (ggtt->mappable_end > ggtt->vm.total) {
1187 drm_err(&i915->drm,
1188 "mappable aperture extends past end of GGTT,"
1189 " aperture=%pa, total=%llx\n",
1190 &ggtt->mappable_end, ggtt->vm.total);
1191 ggtt->mappable_end = ggtt->vm.total;
1192 }
1193
1194 /* GMADR is the PCI mmio aperture into the global GTT. */
1195 drm_dbg(&i915->drm, "GGTT size = %lluM\n", ggtt->vm.total >> 20);
1196 drm_dbg(&i915->drm, "GMADR size = %lluM\n",
1197 (u64)ggtt->mappable_end >> 20);
1198 drm_dbg(&i915->drm, "DSM size = %lluM\n",
1199 (u64)resource_size(&intel_graphics_stolen_res) >> 20);
1200
1201 return 0;
1202 }
1203
1204 /**
1205 * i915_ggtt_probe_hw - Probe GGTT hardware location
1206 * @i915: i915 device
1207 */
i915_ggtt_probe_hw(struct drm_i915_private * i915)1208 int i915_ggtt_probe_hw(struct drm_i915_private *i915)
1209 {
1210 int ret;
1211
1212 ret = ggtt_probe_hw(to_gt(i915)->ggtt, to_gt(i915));
1213 if (ret)
1214 return ret;
1215
1216 if (i915_vtd_active(i915))
1217 drm_info(&i915->drm, "VT-d active for gfx access\n");
1218
1219 return 0;
1220 }
1221
i915_ggtt_enable_hw(struct drm_i915_private * i915)1222 int i915_ggtt_enable_hw(struct drm_i915_private *i915)
1223 {
1224 if (GRAPHICS_VER(i915) < 6)
1225 return intel_ggtt_gmch_enable_hw(i915);
1226
1227 return 0;
1228 }
1229
i915_ggtt_enable_guc(struct i915_ggtt * ggtt)1230 void i915_ggtt_enable_guc(struct i915_ggtt *ggtt)
1231 {
1232 GEM_BUG_ON(ggtt->invalidate != gen8_ggtt_invalidate);
1233
1234 ggtt->invalidate = guc_ggtt_invalidate;
1235
1236 ggtt->invalidate(ggtt);
1237 }
1238
i915_ggtt_disable_guc(struct i915_ggtt * ggtt)1239 void i915_ggtt_disable_guc(struct i915_ggtt *ggtt)
1240 {
1241 /* XXX Temporary pardon for error unload */
1242 if (ggtt->invalidate == gen8_ggtt_invalidate)
1243 return;
1244
1245 /* We should only be called after i915_ggtt_enable_guc() */
1246 GEM_BUG_ON(ggtt->invalidate != guc_ggtt_invalidate);
1247
1248 ggtt->invalidate = gen8_ggtt_invalidate;
1249
1250 ggtt->invalidate(ggtt);
1251 }
1252
1253 /**
1254 * i915_ggtt_resume_vm - Restore the memory mappings for a GGTT or DPT VM
1255 * @vm: The VM to restore the mappings for
1256 *
1257 * Restore the memory mappings for all objects mapped to HW via the GGTT or a
1258 * DPT page table.
1259 *
1260 * Returns %true if restoring the mapping for any object that was in a write
1261 * domain before suspend.
1262 */
i915_ggtt_resume_vm(struct i915_address_space * vm)1263 bool i915_ggtt_resume_vm(struct i915_address_space *vm)
1264 {
1265 struct i915_vma *vma;
1266 bool write_domain_objs = false;
1267 bool retained_ptes;
1268
1269 drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
1270
1271 /*
1272 * First fill our portion of the GTT with scratch pages if
1273 * they were not retained across suspend.
1274 */
1275 retained_ptes = suspend_retains_ptes(vm) &&
1276 !i915_vm_to_ggtt(vm)->pte_lost &&
1277 !GEM_WARN_ON(i915_vm_to_ggtt(vm)->probed_pte != read_last_pte(vm));
1278
1279 if (!retained_ptes)
1280 vm->clear_range(vm, 0, vm->total);
1281
1282 /* clflush objects bound into the GGTT and rebind them. */
1283 list_for_each_entry(vma, &vm->bound_list, vm_link) {
1284 struct drm_i915_gem_object *obj = vma->obj;
1285 unsigned int was_bound =
1286 atomic_read(&vma->flags) & I915_VMA_BIND_MASK;
1287
1288 GEM_BUG_ON(!was_bound);
1289 if (!retained_ptes) {
1290 /*
1291 * Clear the bound flags of the vma resource to allow
1292 * ptes to be repopulated.
1293 */
1294 vma->resource->bound_flags = 0;
1295 vma->ops->bind_vma(vm, NULL, vma->resource,
1296 obj ? obj->cache_level : 0,
1297 was_bound);
1298 }
1299 if (obj) { /* only used during resume => exclusive access */
1300 write_domain_objs |= fetch_and_zero(&obj->write_domain);
1301 obj->read_domains |= I915_GEM_DOMAIN_GTT;
1302 }
1303 }
1304
1305 return write_domain_objs;
1306 }
1307
i915_ggtt_resume(struct i915_ggtt * ggtt)1308 void i915_ggtt_resume(struct i915_ggtt *ggtt)
1309 {
1310 bool flush;
1311
1312 intel_gt_check_and_clear_faults(ggtt->vm.gt);
1313
1314 flush = i915_ggtt_resume_vm(&ggtt->vm);
1315
1316 ggtt->invalidate(ggtt);
1317
1318 if (flush)
1319 wbinvd_on_all_cpus();
1320
1321 if (GRAPHICS_VER(ggtt->vm.i915) >= 8)
1322 setup_private_pat(ggtt->vm.gt->uncore);
1323
1324 intel_ggtt_restore_fences(ggtt);
1325 }
1326
i915_ggtt_mark_pte_lost(struct drm_i915_private * i915,bool val)1327 void i915_ggtt_mark_pte_lost(struct drm_i915_private *i915, bool val)
1328 {
1329 to_gt(i915)->ggtt->pte_lost = val;
1330 }
1331