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