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