blob: 6f410cfb051000fb062d1578a5077f7557cbff6e [file] [log] [blame]
Daniel Vetter76aaf222010-11-05 22:23:30 +01001/*
2 * Copyright © 2010 Daniel Vetter
Ben Widawskyc4ac5242014-02-19 22:05:47 -08003 * Copyright © 2011-2014 Intel Corporation
Daniel Vetter76aaf222010-11-05 22:23:30 +01004 *
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
Daniel Vetter0e46ce22014-01-08 16:10:27 +010026#include <linux/seq_file.h>
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
28#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010029#include "i915_drv.h"
30#include "i915_trace.h"
31#include "intel_drv.h"
32
Ville Syrjäläee0ce472014-04-09 13:28:01 +030033static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
34static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
Ben Widawskya2319c02014-03-18 16:09:37 -070035
Daniel Vettercfa7c862014-04-29 11:53:58 +020036static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
37{
38 if (enable_ppgtt == 0 || !HAS_ALIASING_PPGTT(dev))
39 return 0;
40
41 if (enable_ppgtt == 1)
42 return 1;
43
44 if (enable_ppgtt == 2 && HAS_PPGTT(dev))
45 return 2;
46
Daniel Vetter93a25a92014-03-06 09:40:43 +010047#ifdef CONFIG_INTEL_IOMMU
48 /* Disable ppgtt on SNB if VT-d is on. */
49 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
50 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +020051 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +010052 }
53#endif
54
Jesse Barnes62942ed2014-06-13 09:28:33 -070055 /* Early VLV doesn't have this */
Ville Syrjäläca2aed6c2014-06-28 02:03:56 +030056 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
57 dev->pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -070058 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
59 return 0;
60 }
61
Daniel Vettercfa7c862014-04-29 11:53:58 +020062 return HAS_ALIASING_PPGTT(dev) ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +010063}
64
Ben Widawskyfbe5d362013-11-04 19:56:49 -080065
Ben Widawsky6f65e292013-12-06 14:10:56 -080066static void ppgtt_bind_vma(struct i915_vma *vma,
67 enum i915_cache_level cache_level,
68 u32 flags);
69static void ppgtt_unbind_vma(struct i915_vma *vma);
70
Ben Widawsky94ec8f62013-11-02 21:07:18 -070071static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
72 enum i915_cache_level level,
73 bool valid)
74{
75 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
76 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -030077
78 switch (level) {
79 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -080080 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -030081 break;
82 case I915_CACHE_WT:
83 pte |= PPAT_DISPLAY_ELLC_INDEX;
84 break;
85 default:
86 pte |= PPAT_CACHED_INDEX;
87 break;
88 }
89
Ben Widawsky94ec8f62013-11-02 21:07:18 -070090 return pte;
91}
92
Ben Widawskyb1fe6672013-11-04 21:20:14 -080093static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
94 dma_addr_t addr,
95 enum i915_cache_level level)
96{
97 gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
98 pde |= addr;
99 if (level != I915_CACHE_NONE)
100 pde |= PPAT_CACHED_PDE_INDEX;
101 else
102 pde |= PPAT_UNCACHED_INDEX;
103 return pde;
104}
105
Chris Wilson350ec882013-08-06 13:17:02 +0100106static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700107 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530108 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700109{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700110 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700111 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700112
113 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100114 case I915_CACHE_L3_LLC:
115 case I915_CACHE_LLC:
116 pte |= GEN6_PTE_CACHE_LLC;
117 break;
118 case I915_CACHE_NONE:
119 pte |= GEN6_PTE_UNCACHED;
120 break;
121 default:
122 WARN_ON(1);
123 }
124
125 return pte;
126}
127
128static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700129 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530130 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100131{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700132 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100133 pte |= GEN6_PTE_ADDR_ENCODE(addr);
134
135 switch (level) {
136 case I915_CACHE_L3_LLC:
137 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700138 break;
139 case I915_CACHE_LLC:
140 pte |= GEN6_PTE_CACHE_LLC;
141 break;
142 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700143 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700144 break;
145 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100146 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700147 }
148
Ben Widawsky54d12522012-09-24 16:44:32 -0700149 return pte;
150}
151
Ben Widawsky80a74f72013-06-27 16:30:19 -0700152static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700153 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530154 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700155{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700156 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700157 pte |= GEN6_PTE_ADDR_ENCODE(addr);
158
159 /* Mark the page as writeable. Other platforms don't have a
160 * setting for read-only/writable, so this matches that behavior.
161 */
Akash Goel24f3a8c2014-06-17 10:59:42 +0530162 if (!(flags & PTE_READ_ONLY))
163 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700164
165 if (level != I915_CACHE_NONE)
166 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
167
168 return pte;
169}
170
Ben Widawsky80a74f72013-06-27 16:30:19 -0700171static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700172 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530173 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700174{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700175 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700176 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700177
178 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700179 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700180
181 return pte;
182}
183
Ben Widawsky4d15c142013-07-04 11:02:06 -0700184static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700185 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530186 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700187{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700188 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700189 pte |= HSW_PTE_ADDR_ENCODE(addr);
190
Chris Wilson651d7942013-08-08 14:41:10 +0100191 switch (level) {
192 case I915_CACHE_NONE:
193 break;
194 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000195 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100196 break;
197 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000198 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100199 break;
200 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700201
202 return pte;
203}
204
Ben Widawsky94e409c2013-11-04 22:29:36 -0800205/* Broadwell Page Directory Pointer Descriptors */
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100206static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100207 uint64_t val)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800208{
209 int ret;
210
211 BUG_ON(entry >= 4);
212
213 ret = intel_ring_begin(ring, 6);
214 if (ret)
215 return ret;
216
217 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
218 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
219 intel_ring_emit(ring, (u32)(val >> 32));
220 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
221 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
222 intel_ring_emit(ring, (u32)(val));
223 intel_ring_advance(ring);
224
225 return 0;
226}
227
Ben Widawskyeeb94882013-12-06 14:11:10 -0800228static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100229 struct intel_engine_cs *ring)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800230{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800231 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800232
233 /* bit of a hack to find the actual last used pd */
234 int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
235
Ben Widawsky94e409c2013-11-04 22:29:36 -0800236 for (i = used_pd - 1; i >= 0; i--) {
237 dma_addr_t addr = ppgtt->pd_dma_addr[i];
McAulay, Alistair6689c162014-08-15 18:51:35 +0100238 ret = gen8_write_pdp(ring, i, addr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800239 if (ret)
240 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800241 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800242
Ben Widawskyeeb94882013-12-06 14:11:10 -0800243 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800244}
245
Ben Widawsky459108b2013-11-02 21:07:23 -0700246static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800247 uint64_t start,
248 uint64_t length,
Ben Widawsky459108b2013-11-02 21:07:23 -0700249 bool use_scratch)
250{
251 struct i915_hw_ppgtt *ppgtt =
252 container_of(vm, struct i915_hw_ppgtt, base);
253 gen8_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800254 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
255 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
256 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800257 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700258 unsigned last_pte, i;
259
260 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
261 I915_CACHE_LLC, use_scratch);
262
263 while (num_entries) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800264 struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde];
Ben Widawsky459108b2013-11-02 21:07:23 -0700265
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800266 last_pte = pte + num_entries;
Ben Widawsky459108b2013-11-02 21:07:23 -0700267 if (last_pte > GEN8_PTES_PER_PAGE)
268 last_pte = GEN8_PTES_PER_PAGE;
269
270 pt_vaddr = kmap_atomic(page_table);
271
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800272 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700273 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800274 num_entries--;
275 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700276
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300277 if (!HAS_LLC(ppgtt->base.dev))
278 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky459108b2013-11-02 21:07:23 -0700279 kunmap_atomic(pt_vaddr);
280
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800281 pte = 0;
282 if (++pde == GEN8_PDES_PER_PAGE) {
283 pdpe++;
284 pde = 0;
285 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700286 }
287}
288
Ben Widawsky9df15b42013-11-02 21:07:24 -0700289static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
290 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800291 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530292 enum i915_cache_level cache_level, u32 unused)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700293{
294 struct i915_hw_ppgtt *ppgtt =
295 container_of(vm, struct i915_hw_ppgtt, base);
296 gen8_gtt_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800297 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
298 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
299 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700300 struct sg_page_iter sg_iter;
301
Chris Wilson6f1cc992013-12-31 15:50:31 +0000302 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700303
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800304 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
305 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS))
306 break;
307
308 if (pt_vaddr == NULL)
309 pt_vaddr = kmap_atomic(ppgtt->gen8_pt_pages[pdpe][pde]);
310
311 pt_vaddr[pte] =
Chris Wilson6f1cc992013-12-31 15:50:31 +0000312 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
313 cache_level, true);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800314 if (++pte == GEN8_PTES_PER_PAGE) {
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300315 if (!HAS_LLC(ppgtt->base.dev))
316 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700317 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000318 pt_vaddr = NULL;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800319 if (++pde == GEN8_PDES_PER_PAGE) {
320 pdpe++;
321 pde = 0;
322 }
323 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700324 }
325 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300326 if (pt_vaddr) {
327 if (!HAS_LLC(ppgtt->base.dev))
328 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000329 kunmap_atomic(pt_vaddr);
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300330 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700331}
332
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800333static void gen8_free_page_tables(struct page **pt_pages)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800334{
335 int i;
336
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800337 if (pt_pages == NULL)
338 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800339
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800340 for (i = 0; i < GEN8_PDES_PER_PAGE; i++)
341 if (pt_pages[i])
342 __free_pages(pt_pages[i], 0);
343}
344
345static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt)
346{
347 int i;
348
349 for (i = 0; i < ppgtt->num_pd_pages; i++) {
350 gen8_free_page_tables(ppgtt->gen8_pt_pages[i]);
351 kfree(ppgtt->gen8_pt_pages[i]);
352 kfree(ppgtt->gen8_pt_dma_addr[i]);
353 }
354
Ben Widawskyb45a6712014-02-12 14:28:44 -0800355 __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
356}
357
358static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
359{
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800360 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800361 int i, j;
362
363 for (i = 0; i < ppgtt->num_pd_pages; i++) {
364 /* TODO: In the future we'll support sparse mappings, so this
365 * will have to change. */
366 if (!ppgtt->pd_dma_addr[i])
367 continue;
368
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800369 pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
370 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800371
372 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
373 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
374 if (addr)
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800375 pci_unmap_page(hwdev, addr, PAGE_SIZE,
376 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800377 }
378 }
379}
380
Ben Widawsky37aca442013-11-04 20:47:32 -0800381static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
382{
383 struct i915_hw_ppgtt *ppgtt =
384 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800385
Ben Widawskyb45a6712014-02-12 14:28:44 -0800386 gen8_ppgtt_unmap_pages(ppgtt);
387 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800388}
389
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800390static struct page **__gen8_alloc_page_tables(void)
391{
392 struct page **pt_pages;
393 int i;
394
395 pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL);
396 if (!pt_pages)
397 return ERR_PTR(-ENOMEM);
398
399 for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
400 pt_pages[i] = alloc_page(GFP_KERNEL);
401 if (!pt_pages[i])
402 goto bail;
403 }
404
405 return pt_pages;
406
407bail:
408 gen8_free_page_tables(pt_pages);
409 kfree(pt_pages);
410 return ERR_PTR(-ENOMEM);
411}
412
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800413static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
414 const int max_pdp)
415{
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800416 struct page **pt_pages[GEN8_LEGACY_PDPS];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800417 int i, ret;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800418
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800419 for (i = 0; i < max_pdp; i++) {
420 pt_pages[i] = __gen8_alloc_page_tables();
421 if (IS_ERR(pt_pages[i])) {
422 ret = PTR_ERR(pt_pages[i]);
423 goto unwind_out;
424 }
425 }
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800426
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800427 /* NB: Avoid touching gen8_pt_pages until last to keep the allocation,
428 * "atomic" - for cleanup purposes.
429 */
430 for (i = 0; i < max_pdp; i++)
431 ppgtt->gen8_pt_pages[i] = pt_pages[i];
432
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800433 return 0;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800434
435unwind_out:
436 while (i--) {
437 gen8_free_page_tables(pt_pages[i]);
438 kfree(pt_pages[i]);
439 }
440
441 return ret;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800442}
443
444static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
445{
446 int i;
447
448 for (i = 0; i < ppgtt->num_pd_pages; i++) {
449 ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
450 sizeof(dma_addr_t),
451 GFP_KERNEL);
452 if (!ppgtt->gen8_pt_dma_addr[i])
453 return -ENOMEM;
454 }
455
456 return 0;
457}
458
459static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
460 const int max_pdp)
461{
462 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
463 if (!ppgtt->pd_pages)
464 return -ENOMEM;
465
466 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
467 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
468
469 return 0;
470}
471
472static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
473 const int max_pdp)
474{
475 int ret;
476
477 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
478 if (ret)
479 return ret;
480
481 ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
482 if (ret) {
483 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
484 return ret;
485 }
486
487 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
488
489 ret = gen8_ppgtt_allocate_dma(ppgtt);
490 if (ret)
491 gen8_ppgtt_free(ppgtt);
492
493 return ret;
494}
495
496static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
497 const int pd)
498{
499 dma_addr_t pd_addr;
500 int ret;
501
502 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
503 &ppgtt->pd_pages[pd], 0,
504 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
505
506 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
507 if (ret)
508 return ret;
509
510 ppgtt->pd_dma_addr[pd] = pd_addr;
511
512 return 0;
513}
514
515static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
516 const int pd,
517 const int pt)
518{
519 dma_addr_t pt_addr;
520 struct page *p;
521 int ret;
522
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800523 p = ppgtt->gen8_pt_pages[pd][pt];
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800524 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
525 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
526 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
527 if (ret)
528 return ret;
529
530 ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
531
532 return 0;
533}
534
Ben Widawsky37aca442013-11-04 20:47:32 -0800535/**
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800536 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
537 * with a net effect resembling a 2-level page table in normal x86 terms. Each
538 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
539 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800540 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800541 * FIXME: split allocation into smaller pieces. For now we only ever do this
542 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
Ben Widawsky37aca442013-11-04 20:47:32 -0800543 * TODO: Do something with the size parameter
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800544 */
Ben Widawsky37aca442013-11-04 20:47:32 -0800545static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
546{
Ben Widawsky37aca442013-11-04 20:47:32 -0800547 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800548 const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800549 int i, j, ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800550
551 if (size % (1<<30))
552 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
553
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800554 /* 1. Do all our allocations for page directories and page tables. */
555 ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
556 if (ret)
557 return ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800558
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800559 /*
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800560 * 2. Create DMA mappings for the page directories and page tables.
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800561 */
562 for (i = 0; i < max_pdp; i++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800563 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800564 if (ret)
565 goto bail;
566
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800567 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800568 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800569 if (ret)
570 goto bail;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800571 }
572 }
573
574 /*
575 * 3. Map all the page directory entires to point to the page tables
576 * we've allocated.
577 *
578 * For now, the PPGTT helper functions all require that the PDEs are
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800579 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800580 * will never need to touch the PDEs again.
581 */
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800582 for (i = 0; i < max_pdp; i++) {
583 gen8_ppgtt_pde_t *pd_vaddr;
584 pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
585 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
586 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
587 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
588 I915_CACHE_LLC);
589 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300590 if (!HAS_LLC(ppgtt->base.dev))
591 drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800592 kunmap_atomic(pd_vaddr);
593 }
594
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800595 ppgtt->switch_mm = gen8_mm_switch;
596 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
597 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
598 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
599 ppgtt->base.start = 0;
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800600 ppgtt->base.total = ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800601
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800602 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Ben Widawsky459108b2013-11-02 21:07:23 -0700603
Ben Widawsky37aca442013-11-04 20:47:32 -0800604 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
605 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
606 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800607 ppgtt->num_pd_entries,
608 (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700609 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800610
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800611bail:
612 gen8_ppgtt_unmap_pages(ppgtt);
613 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800614 return ret;
615}
616
Ben Widawsky87d60b62013-12-06 14:11:29 -0800617static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
618{
619 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
620 struct i915_address_space *vm = &ppgtt->base;
621 gen6_gtt_pte_t __iomem *pd_addr;
622 gen6_gtt_pte_t scratch_pte;
623 uint32_t pd_entry;
624 int pte, pde;
625
Akash Goel24f3a8c2014-06-17 10:59:42 +0530626 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800627
628 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
629 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
630
631 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
632 ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
633 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
634 u32 expected;
635 gen6_gtt_pte_t *pt_vaddr;
636 dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
637 pd_entry = readl(pd_addr + pde);
638 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
639
640 if (pd_entry != expected)
641 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
642 pde,
643 pd_entry,
644 expected);
645 seq_printf(m, "\tPDE: %x\n", pd_entry);
646
647 pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
648 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
649 unsigned long va =
650 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
651 (pte * PAGE_SIZE);
652 int i;
653 bool found = false;
654 for (i = 0; i < 4; i++)
655 if (pt_vaddr[pte + i] != scratch_pte)
656 found = true;
657 if (!found)
658 continue;
659
660 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
661 for (i = 0; i < 4; i++) {
662 if (pt_vaddr[pte + i] != scratch_pte)
663 seq_printf(m, " %08x", pt_vaddr[pte + i]);
664 else
665 seq_puts(m, " SCRATCH ");
666 }
667 seq_puts(m, "\n");
668 }
669 kunmap_atomic(pt_vaddr);
670 }
671}
672
Ben Widawsky3e302542013-04-23 23:15:32 -0700673static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700674{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700675 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700676 gen6_gtt_pte_t __iomem *pd_addr;
677 uint32_t pd_entry;
678 int i;
679
Ben Widawsky0a732872013-04-23 23:15:30 -0700680 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700681 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
682 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
683 for (i = 0; i < ppgtt->num_pd_entries; i++) {
684 dma_addr_t pt_addr;
685
686 pt_addr = ppgtt->pt_dma_addr[i];
687 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
688 pd_entry |= GEN6_PDE_VALID;
689
690 writel(pd_entry, pd_addr + i);
691 }
692 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700693}
694
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800695static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700696{
Ben Widawsky3e302542013-04-23 23:15:32 -0700697 BUG_ON(ppgtt->pd_offset & 0x3f);
698
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800699 return (ppgtt->pd_offset / 64) << 16;
700}
Ben Widawsky61973492013-04-08 18:43:54 -0700701
Ben Widawsky90252e52013-12-06 14:11:12 -0800702static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100703 struct intel_engine_cs *ring)
Ben Widawsky90252e52013-12-06 14:11:12 -0800704{
Ben Widawsky90252e52013-12-06 14:11:12 -0800705 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700706
Ben Widawsky90252e52013-12-06 14:11:12 -0800707 /* NB: TLBs must be flushed and invalidated before a switch */
708 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
709 if (ret)
710 return ret;
711
712 ret = intel_ring_begin(ring, 6);
713 if (ret)
714 return ret;
715
716 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
717 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
718 intel_ring_emit(ring, PP_DIR_DCLV_2G);
719 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
720 intel_ring_emit(ring, get_pd_offset(ppgtt));
721 intel_ring_emit(ring, MI_NOOP);
722 intel_ring_advance(ring);
723
724 return 0;
725}
726
Ben Widawsky48a10382013-12-06 14:11:11 -0800727static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100728 struct intel_engine_cs *ring)
Ben Widawsky48a10382013-12-06 14:11:11 -0800729{
Ben Widawsky48a10382013-12-06 14:11:11 -0800730 int ret;
731
Ben Widawsky48a10382013-12-06 14:11:11 -0800732 /* NB: TLBs must be flushed and invalidated before a switch */
733 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
734 if (ret)
735 return ret;
736
737 ret = intel_ring_begin(ring, 6);
738 if (ret)
739 return ret;
740
741 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
742 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
743 intel_ring_emit(ring, PP_DIR_DCLV_2G);
744 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
745 intel_ring_emit(ring, get_pd_offset(ppgtt));
746 intel_ring_emit(ring, MI_NOOP);
747 intel_ring_advance(ring);
748
Ben Widawsky90252e52013-12-06 14:11:12 -0800749 /* XXX: RCS is the only one to auto invalidate the TLBs? */
750 if (ring->id != RCS) {
751 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
752 if (ret)
753 return ret;
754 }
755
Ben Widawsky48a10382013-12-06 14:11:11 -0800756 return 0;
757}
758
Ben Widawskyeeb94882013-12-06 14:11:10 -0800759static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100760 struct intel_engine_cs *ring)
Ben Widawskyeeb94882013-12-06 14:11:10 -0800761{
762 struct drm_device *dev = ppgtt->base.dev;
763 struct drm_i915_private *dev_priv = dev->dev_private;
764
Ben Widawsky48a10382013-12-06 14:11:11 -0800765
Ben Widawskyeeb94882013-12-06 14:11:10 -0800766 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
767 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
768
769 POSTING_READ(RING_PP_DIR_DCLV(ring));
770
771 return 0;
772}
773
Daniel Vetter82460d92014-08-06 20:19:53 +0200774static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -0800775{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800776 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100777 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +0200778 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -0800779
780 for_each_ring(ring, dev_priv, j) {
781 I915_WRITE(RING_MODE_GEN7(ring),
782 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -0800783 }
Ben Widawskyeeb94882013-12-06 14:11:10 -0800784}
785
Daniel Vetter82460d92014-08-06 20:19:53 +0200786static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800787{
Jani Nikula50227e12014-03-31 14:27:21 +0300788 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100789 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800790 uint32_t ecochk, ecobits;
791 int i;
792
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800793 ecobits = I915_READ(GAC_ECO_BITS);
794 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
795
796 ecochk = I915_READ(GAM_ECOCHK);
797 if (IS_HASWELL(dev)) {
798 ecochk |= ECOCHK_PPGTT_WB_HSW;
799 } else {
800 ecochk |= ECOCHK_PPGTT_LLC_IVB;
801 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
802 }
803 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800804
Ben Widawsky61973492013-04-08 18:43:54 -0700805 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800806 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800807 I915_WRITE(RING_MODE_GEN7(ring),
808 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700809 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800810}
811
Daniel Vetter82460d92014-08-06 20:19:53 +0200812static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -0700813{
Jani Nikula50227e12014-03-31 14:27:21 +0300814 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800815 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -0700816
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800817 ecobits = I915_READ(GAC_ECO_BITS);
818 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
819 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700820
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800821 gab_ctl = I915_READ(GAB_CTL);
822 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700823
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800824 ecochk = I915_READ(GAM_ECOCHK);
825 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700826
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800827 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700828}
829
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100830/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700831static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800832 uint64_t start,
833 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -0700834 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100835{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700836 struct i915_hw_ppgtt *ppgtt =
837 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700838 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -0800839 unsigned first_entry = start >> PAGE_SHIFT;
840 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100841 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100842 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
843 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100844
Akash Goel24f3a8c2014-06-17 10:59:42 +0530845 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100846
Daniel Vetter7bddb012012-02-09 17:15:47 +0100847 while (num_entries) {
848 last_pte = first_pte + num_entries;
849 if (last_pte > I915_PPGTT_PT_ENTRIES)
850 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100851
Daniel Vettera15326a2013-03-19 23:48:39 +0100852 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100853
854 for (i = first_pte; i < last_pte; i++)
855 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100856
857 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100858
Daniel Vetter7bddb012012-02-09 17:15:47 +0100859 num_entries -= last_pte - first_pte;
860 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100861 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100862 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100863}
864
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700865static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800866 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800867 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530868 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -0800869{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700870 struct i915_hw_ppgtt *ppgtt =
871 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700872 gen6_gtt_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -0800873 unsigned first_entry = start >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100874 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200875 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
876 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800877
Chris Wilsoncc797142013-12-31 15:50:30 +0000878 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +0200879 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +0000880 if (pt_vaddr == NULL)
881 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800882
Chris Wilsoncc797142013-12-31 15:50:30 +0000883 pt_vaddr[act_pte] =
884 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +0530885 cache_level, true, flags);
886
Imre Deak6e995e22013-02-18 19:28:04 +0200887 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
888 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +0000889 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +0100890 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +0200891 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800892 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800893 }
Chris Wilsoncc797142013-12-31 15:50:30 +0000894 if (pt_vaddr)
895 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800896}
897
Ben Widawskya00d8252014-02-19 22:05:48 -0800898static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100899{
Daniel Vetter3440d262013-01-24 13:49:56 -0800900 int i;
901
902 if (ppgtt->pt_dma_addr) {
903 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700904 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800905 ppgtt->pt_dma_addr[i],
906 4096, PCI_DMA_BIDIRECTIONAL);
907 }
Ben Widawskya00d8252014-02-19 22:05:48 -0800908}
909
910static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
911{
912 int i;
Daniel Vetter3440d262013-01-24 13:49:56 -0800913
914 kfree(ppgtt->pt_dma_addr);
915 for (i = 0; i < ppgtt->num_pd_entries; i++)
916 __free_page(ppgtt->pt_pages[i]);
917 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800918}
919
Ben Widawskya00d8252014-02-19 22:05:48 -0800920static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
921{
922 struct i915_hw_ppgtt *ppgtt =
923 container_of(vm, struct i915_hw_ppgtt, base);
924
Ben Widawskya00d8252014-02-19 22:05:48 -0800925 drm_mm_remove_node(&ppgtt->node);
926
927 gen6_ppgtt_unmap_pages(ppgtt);
928 gen6_ppgtt_free(ppgtt);
929}
930
Ben Widawskyb1465202014-02-19 22:05:49 -0800931static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -0800932{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700933 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100934 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -0800935 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -0800936 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100937
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800938 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
939 * allocator works in address space sizes, so it's multiplied by page
940 * size. We allocate at the top of the GTT to avoid fragmentation.
941 */
942 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -0800943alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800944 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
945 &ppgtt->node, GEN6_PD_SIZE,
946 GEN6_PD_ALIGN, 0,
947 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -0700948 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800949 if (ret == -ENOSPC && !retried) {
950 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
951 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +0200952 I915_CACHE_NONE,
953 0, dev_priv->gtt.base.total,
954 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800955 if (ret)
956 return ret;
957
958 retried = true;
959 goto alloc;
960 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800961
962 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
963 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100964
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700965 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawskyb1465202014-02-19 22:05:49 -0800966 return ret;
967}
968
969static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
970{
971 int i;
972
973 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
974 GFP_KERNEL);
975
976 if (!ppgtt->pt_pages)
977 return -ENOMEM;
978
979 for (i = 0; i < ppgtt->num_pd_entries; i++) {
980 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
981 if (!ppgtt->pt_pages[i]) {
982 gen6_ppgtt_free(ppgtt);
983 return -ENOMEM;
984 }
985 }
986
987 return 0;
988}
989
990static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
991{
992 int ret;
993
994 ret = gen6_ppgtt_allocate_page_directories(ppgtt);
995 if (ret)
996 return ret;
997
998 ret = gen6_ppgtt_allocate_page_tables(ppgtt);
999 if (ret) {
1000 drm_mm_remove_node(&ppgtt->node);
1001 return ret;
1002 }
1003
1004 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
1005 GFP_KERNEL);
1006 if (!ppgtt->pt_dma_addr) {
1007 drm_mm_remove_node(&ppgtt->node);
1008 gen6_ppgtt_free(ppgtt);
1009 return -ENOMEM;
1010 }
1011
1012 return 0;
1013}
1014
1015static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
1016{
1017 struct drm_device *dev = ppgtt->base.dev;
1018 int i;
1019
1020 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1021 dma_addr_t pt_addr;
1022
1023 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
1024 PCI_DMA_BIDIRECTIONAL);
1025
1026 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1027 gen6_ppgtt_unmap_pages(ppgtt);
1028 return -EIO;
1029 }
1030
1031 ppgtt->pt_dma_addr[i] = pt_addr;
1032 }
1033
1034 return 0;
1035}
1036
1037static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1038{
1039 struct drm_device *dev = ppgtt->base.dev;
1040 struct drm_i915_private *dev_priv = dev->dev_private;
1041 int ret;
1042
1043 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001044 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001045 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001046 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001047 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001048 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001049 ppgtt->switch_mm = gen7_mm_switch;
1050 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001051 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001052
1053 ret = gen6_ppgtt_alloc(ppgtt);
1054 if (ret)
1055 return ret;
1056
1057 ret = gen6_ppgtt_setup_page_tables(ppgtt);
1058 if (ret) {
1059 gen6_ppgtt_free(ppgtt);
1060 return ret;
1061 }
1062
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001063 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1064 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1065 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001066 ppgtt->base.start = 0;
Ben Widawsky5a6c93f2014-03-08 11:58:17 -08001067 ppgtt->base.total = ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001068 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001069
Ben Widawskyb1465202014-02-19 22:05:49 -08001070 ppgtt->pd_offset =
1071 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001072
Ben Widawsky782f1492014-02-20 11:50:33 -08001073 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001074
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001075 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1076 ppgtt->node.size >> 20,
1077 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001078
Daniel Vetterfa76da32014-08-06 20:19:54 +02001079 gen6_write_pdes(ppgtt);
1080 DRM_DEBUG("Adding PPGTT at offset %x\n",
1081 ppgtt->pd_offset << 10);
1082
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001083 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001084}
1085
Daniel Vetterfa76da32014-08-06 20:19:54 +02001086static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001087{
1088 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter3440d262013-01-24 13:49:56 -08001089
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001090 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001091 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001092
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001093 if (INTEL_INFO(dev)->gen < 8)
Daniel Vetterfa76da32014-08-06 20:19:54 +02001094 return gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -07001095 else if (IS_GEN8(dev))
Daniel Vetterfa76da32014-08-06 20:19:54 +02001096 return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001097 else
1098 BUG();
Daniel Vetterfa76da32014-08-06 20:19:54 +02001099}
1100int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1101{
1102 struct drm_i915_private *dev_priv = dev->dev_private;
1103 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001104
Daniel Vetterfa76da32014-08-06 20:19:54 +02001105 ret = __hw_ppgtt_init(dev, ppgtt);
1106 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001107 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001108 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1109 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001110 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001111 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001112
1113 return ret;
1114}
1115
Daniel Vetter82460d92014-08-06 20:19:53 +02001116int i915_ppgtt_init_hw(struct drm_device *dev)
1117{
1118 struct drm_i915_private *dev_priv = dev->dev_private;
1119 struct intel_engine_cs *ring;
1120 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1121 int i, ret = 0;
1122
Thomas Daniel671b50132014-08-20 16:24:50 +01001123 /* In the case of execlists, PPGTT is enabled by the context descriptor
1124 * and the PDPs are contained within the context itself. We don't
1125 * need to do anything here. */
1126 if (i915.enable_execlists)
1127 return 0;
1128
Daniel Vetter82460d92014-08-06 20:19:53 +02001129 if (!USES_PPGTT(dev))
1130 return 0;
1131
1132 if (IS_GEN6(dev))
1133 gen6_ppgtt_enable(dev);
1134 else if (IS_GEN7(dev))
1135 gen7_ppgtt_enable(dev);
1136 else if (INTEL_INFO(dev)->gen >= 8)
1137 gen8_ppgtt_enable(dev);
1138 else
1139 WARN_ON(1);
1140
1141 if (ppgtt) {
1142 for_each_ring(ring, dev_priv, i) {
McAulay, Alistair6689c162014-08-15 18:51:35 +01001143 ret = ppgtt->switch_mm(ppgtt, ring);
Daniel Vetter82460d92014-08-06 20:19:53 +02001144 if (ret != 0)
1145 return ret;
1146 }
1147 }
1148
1149 return ret;
1150}
Daniel Vetter4d884702014-08-06 15:04:47 +02001151struct i915_hw_ppgtt *
1152i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1153{
1154 struct i915_hw_ppgtt *ppgtt;
1155 int ret;
1156
1157 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1158 if (!ppgtt)
1159 return ERR_PTR(-ENOMEM);
1160
1161 ret = i915_ppgtt_init(dev, ppgtt);
1162 if (ret) {
1163 kfree(ppgtt);
1164 return ERR_PTR(ret);
1165 }
1166
1167 ppgtt->file_priv = fpriv;
1168
1169 return ppgtt;
1170}
1171
Daniel Vetteree960be2014-08-06 15:04:45 +02001172void i915_ppgtt_release(struct kref *kref)
1173{
1174 struct i915_hw_ppgtt *ppgtt =
1175 container_of(kref, struct i915_hw_ppgtt, ref);
1176
1177 /* vmas should already be unbound */
1178 WARN_ON(!list_empty(&ppgtt->base.active_list));
1179 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1180
Daniel Vetter19dd1202014-08-06 15:04:55 +02001181 list_del(&ppgtt->base.global_link);
1182 drm_mm_takedown(&ppgtt->base.mm);
1183
Daniel Vetteree960be2014-08-06 15:04:45 +02001184 ppgtt->base.cleanup(&ppgtt->base);
1185 kfree(ppgtt);
1186}
1187
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001188static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001189ppgtt_bind_vma(struct i915_vma *vma,
1190 enum i915_cache_level cache_level,
1191 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001192{
Akash Goel24f3a8c2014-06-17 10:59:42 +05301193 /* Currently applicable only to VLV */
1194 if (vma->obj->gt_ro)
1195 flags |= PTE_READ_ONLY;
1196
Ben Widawsky782f1492014-02-20 11:50:33 -08001197 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301198 cache_level, flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001199}
1200
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001201static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001202{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001203 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001204 vma->node.start,
1205 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001206 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001207}
1208
Ben Widawskya81cc002013-01-18 12:30:31 -08001209extern int intel_iommu_gfx_mapped;
1210/* Certain Gen5 chipsets require require idling the GPU before
1211 * unmapping anything from the GTT when VT-d is enabled.
1212 */
1213static inline bool needs_idle_maps(struct drm_device *dev)
1214{
1215#ifdef CONFIG_INTEL_IOMMU
1216 /* Query intel_iommu to see if we need the workaround. Presumably that
1217 * was loaded first.
1218 */
1219 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1220 return true;
1221#endif
1222 return false;
1223}
1224
Ben Widawsky5c042282011-10-17 15:51:55 -07001225static bool do_idling(struct drm_i915_private *dev_priv)
1226{
1227 bool ret = dev_priv->mm.interruptible;
1228
Ben Widawskya81cc002013-01-18 12:30:31 -08001229 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001230 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001231 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001232 DRM_ERROR("Couldn't idle GPU\n");
1233 /* Wait a bit, in hopes it avoids the hang */
1234 udelay(10);
1235 }
1236 }
1237
1238 return ret;
1239}
1240
1241static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1242{
Ben Widawskya81cc002013-01-18 12:30:31 -08001243 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001244 dev_priv->mm.interruptible = interruptible;
1245}
1246
Ben Widawsky828c7902013-10-16 09:21:30 -07001247void i915_check_and_clear_faults(struct drm_device *dev)
1248{
1249 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001250 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07001251 int i;
1252
1253 if (INTEL_INFO(dev)->gen < 6)
1254 return;
1255
1256 for_each_ring(ring, dev_priv, i) {
1257 u32 fault_reg;
1258 fault_reg = I915_READ(RING_FAULT_REG(ring));
1259 if (fault_reg & RING_FAULT_VALID) {
1260 DRM_DEBUG_DRIVER("Unexpected fault\n"
1261 "\tAddr: 0x%08lx\\n"
1262 "\tAddress space: %s\n"
1263 "\tSource ID: %d\n"
1264 "\tType: %d\n",
1265 fault_reg & PAGE_MASK,
1266 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1267 RING_FAULT_SRCID(fault_reg),
1268 RING_FAULT_FAULT_TYPE(fault_reg));
1269 I915_WRITE(RING_FAULT_REG(ring),
1270 fault_reg & ~RING_FAULT_VALID);
1271 }
1272 }
1273 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1274}
1275
1276void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1277{
1278 struct drm_i915_private *dev_priv = dev->dev_private;
1279
1280 /* Don't bother messing with faults pre GEN6 as we have little
1281 * documentation supporting that it's a good idea.
1282 */
1283 if (INTEL_INFO(dev)->gen < 6)
1284 return;
1285
1286 i915_check_and_clear_faults(dev);
1287
1288 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001289 dev_priv->gtt.base.start,
1290 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001291 true);
Ben Widawsky828c7902013-10-16 09:21:30 -07001292}
1293
Daniel Vetter76aaf222010-11-05 22:23:30 +01001294void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1295{
1296 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001297 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001298 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001299
Ben Widawsky828c7902013-10-16 09:21:30 -07001300 i915_check_and_clear_faults(dev);
1301
Chris Wilsonbee4a182011-01-21 10:54:32 +00001302 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001303 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001304 dev_priv->gtt.base.start,
1305 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001306 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001307
Ben Widawsky35c20a62013-05-31 11:28:48 -07001308 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001309 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1310 &dev_priv->gtt.base);
1311 if (!vma)
1312 continue;
1313
Chris Wilson2c225692013-08-09 12:26:45 +01001314 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001315 /* The bind_vma code tries to be smart about tracking mappings.
1316 * Unfortunately above, we've just wiped out the mappings
1317 * without telling our object about it. So we need to fake it.
1318 */
1319 obj->has_global_gtt_mapping = 0;
1320 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001321 }
1322
Ben Widawsky80da2162013-12-06 14:11:17 -08001323
Ben Widawskya2319c02014-03-18 16:09:37 -07001324 if (INTEL_INFO(dev)->gen >= 8) {
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001325 if (IS_CHERRYVIEW(dev))
1326 chv_setup_private_ppat(dev_priv);
1327 else
1328 bdw_setup_private_ppat(dev_priv);
1329
Ben Widawsky80da2162013-12-06 14:11:17 -08001330 return;
Ben Widawskya2319c02014-03-18 16:09:37 -07001331 }
Ben Widawsky80da2162013-12-06 14:11:17 -08001332
1333 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1334 /* TODO: Perhaps it shouldn't be gen6 specific */
1335 if (i915_is_ggtt(vm)) {
1336 if (dev_priv->mm.aliasing_ppgtt)
1337 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1338 continue;
1339 }
1340
1341 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001342 }
1343
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001344 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001345}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001346
Daniel Vetter74163902012-02-15 23:50:21 +01001347int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001348{
Chris Wilson9da3da62012-06-01 15:20:22 +01001349 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001350 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001351
1352 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1353 obj->pages->sgl, obj->pages->nents,
1354 PCI_DMA_BIDIRECTIONAL))
1355 return -ENOSPC;
1356
1357 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001358}
1359
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001360static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1361{
1362#ifdef writeq
1363 writeq(pte, addr);
1364#else
1365 iowrite32((u32)pte, addr);
1366 iowrite32(pte >> 32, addr + 4);
1367#endif
1368}
1369
1370static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1371 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001372 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301373 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001374{
1375 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001376 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001377 gen8_gtt_pte_t __iomem *gtt_entries =
1378 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1379 int i = 0;
1380 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001381 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001382
1383 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1384 addr = sg_dma_address(sg_iter.sg) +
1385 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1386 gen8_set_pte(&gtt_entries[i],
1387 gen8_pte_encode(addr, level, true));
1388 i++;
1389 }
1390
1391 /*
1392 * XXX: This serves as a posting read to make sure that the PTE has
1393 * actually been updated. There is some concern that even though
1394 * registers and PTEs are within the same BAR that they are potentially
1395 * of NUMA access patterns. Therefore, even with the way we assume
1396 * hardware should work, we must keep this posting read for paranoia.
1397 */
1398 if (i != 0)
1399 WARN_ON(readq(&gtt_entries[i-1])
1400 != gen8_pte_encode(addr, level, true));
1401
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001402 /* This next bit makes the above posting read even more important. We
1403 * want to flush the TLBs only after we're certain all the PTE updates
1404 * have finished.
1405 */
1406 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1407 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001408}
1409
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001410/*
1411 * Binds an object into the global gtt with the specified cache level. The object
1412 * will be accessible to the GPU via commands whose operands reference offsets
1413 * within the global GTT as well as accessible by the GPU through the GMADR
1414 * mapped BAR (dev_priv->mm.gtt->gtt).
1415 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001416static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001417 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001418 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301419 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001420{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001421 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001422 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001423 gen6_gtt_pte_t __iomem *gtt_entries =
1424 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001425 int i = 0;
1426 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001427 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001428
Imre Deak6e995e22013-02-18 19:28:04 +02001429 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001430 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301431 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001432 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001433 }
1434
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001435 /* XXX: This serves as a posting read to make sure that the PTE has
1436 * actually been updated. There is some concern that even though
1437 * registers and PTEs are within the same BAR that they are potentially
1438 * of NUMA access patterns. Therefore, even with the way we assume
1439 * hardware should work, we must keep this posting read for paranoia.
1440 */
Pavel Machek57007df2014-07-28 13:20:58 +02001441 if (i != 0) {
1442 unsigned long gtt = readl(&gtt_entries[i-1]);
1443 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1444 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001445
1446 /* This next bit makes the above posting read even more important. We
1447 * want to flush the TLBs only after we're certain all the PTE updates
1448 * have finished.
1449 */
1450 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1451 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001452}
1453
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001454static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001455 uint64_t start,
1456 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001457 bool use_scratch)
1458{
1459 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001460 unsigned first_entry = start >> PAGE_SHIFT;
1461 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001462 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1463 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1464 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1465 int i;
1466
1467 if (WARN(num_entries > max_entries,
1468 "First entry = %d; Num entries = %d (max=%d)\n",
1469 first_entry, num_entries, max_entries))
1470 num_entries = max_entries;
1471
1472 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1473 I915_CACHE_LLC,
1474 use_scratch);
1475 for (i = 0; i < num_entries; i++)
1476 gen8_set_pte(&gtt_base[i], scratch_pte);
1477 readl(gtt_base);
1478}
1479
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001480static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001481 uint64_t start,
1482 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001483 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001484{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001485 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001486 unsigned first_entry = start >> PAGE_SHIFT;
1487 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001488 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1489 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001490 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001491 int i;
1492
1493 if (WARN(num_entries > max_entries,
1494 "First entry = %d; Num entries = %d (max=%d)\n",
1495 first_entry, num_entries, max_entries))
1496 num_entries = max_entries;
1497
Akash Goel24f3a8c2014-06-17 10:59:42 +05301498 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07001499
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001500 for (i = 0; i < num_entries; i++)
1501 iowrite32(scratch_pte, &gtt_base[i]);
1502 readl(gtt_base);
1503}
1504
Ben Widawsky6f65e292013-12-06 14:10:56 -08001505
1506static void i915_ggtt_bind_vma(struct i915_vma *vma,
1507 enum i915_cache_level cache_level,
1508 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001509{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001510 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001511 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1512 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1513
Ben Widawsky6f65e292013-12-06 14:10:56 -08001514 BUG_ON(!i915_is_ggtt(vma->vm));
1515 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1516 vma->obj->has_global_gtt_mapping = 1;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001517}
1518
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001519static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001520 uint64_t start,
1521 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001522 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001523{
Ben Widawsky782f1492014-02-20 11:50:33 -08001524 unsigned first_entry = start >> PAGE_SHIFT;
1525 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001526 intel_gtt_clear_range(first_entry, num_entries);
1527}
1528
Ben Widawsky6f65e292013-12-06 14:10:56 -08001529static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001530{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001531 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1532 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001533
Ben Widawsky6f65e292013-12-06 14:10:56 -08001534 BUG_ON(!i915_is_ggtt(vma->vm));
1535 vma->obj->has_global_gtt_mapping = 0;
1536 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001537}
1538
Ben Widawsky6f65e292013-12-06 14:10:56 -08001539static void ggtt_bind_vma(struct i915_vma *vma,
1540 enum i915_cache_level cache_level,
1541 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001542{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001543 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001544 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001545 struct drm_i915_gem_object *obj = vma->obj;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001546
Akash Goel24f3a8c2014-06-17 10:59:42 +05301547 /* Currently applicable only to VLV */
1548 if (obj->gt_ro)
1549 flags |= PTE_READ_ONLY;
1550
Ben Widawsky6f65e292013-12-06 14:10:56 -08001551 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1552 * or we have a global mapping already but the cacheability flags have
1553 * changed, set the global PTEs.
1554 *
1555 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1556 * instead if none of the above hold true.
1557 *
1558 * NB: A global mapping should only be needed for special regions like
1559 * "gtt mappable", SNB errata, or if specified via special execbuf
1560 * flags. At all other times, the GPU will use the aliasing PPGTT.
1561 */
1562 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1563 if (!obj->has_global_gtt_mapping ||
1564 (cache_level != obj->cache_level)) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001565 vma->vm->insert_entries(vma->vm, obj->pages,
1566 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301567 cache_level, flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001568 obj->has_global_gtt_mapping = 1;
1569 }
1570 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001571
Ben Widawsky6f65e292013-12-06 14:10:56 -08001572 if (dev_priv->mm.aliasing_ppgtt &&
1573 (!obj->has_aliasing_ppgtt_mapping ||
1574 (cache_level != obj->cache_level))) {
1575 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1576 appgtt->base.insert_entries(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001577 vma->obj->pages,
1578 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301579 cache_level, flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001580 vma->obj->has_aliasing_ppgtt_mapping = 1;
1581 }
1582}
1583
1584static void ggtt_unbind_vma(struct i915_vma *vma)
1585{
1586 struct drm_device *dev = vma->vm->dev;
1587 struct drm_i915_private *dev_priv = dev->dev_private;
1588 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001589
1590 if (obj->has_global_gtt_mapping) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001591 vma->vm->clear_range(vma->vm,
1592 vma->node.start,
1593 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001594 true);
1595 obj->has_global_gtt_mapping = 0;
1596 }
1597
1598 if (obj->has_aliasing_ppgtt_mapping) {
1599 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1600 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001601 vma->node.start,
1602 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001603 true);
1604 obj->has_aliasing_ppgtt_mapping = 0;
1605 }
Daniel Vetter74163902012-02-15 23:50:21 +01001606}
1607
1608void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1609{
Ben Widawsky5c042282011-10-17 15:51:55 -07001610 struct drm_device *dev = obj->base.dev;
1611 struct drm_i915_private *dev_priv = dev->dev_private;
1612 bool interruptible;
1613
1614 interruptible = do_idling(dev_priv);
1615
Chris Wilson9da3da62012-06-01 15:20:22 +01001616 if (!obj->has_dma_mapping)
1617 dma_unmap_sg(&dev->pdev->dev,
1618 obj->pages->sgl, obj->pages->nents,
1619 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001620
1621 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001622}
Daniel Vetter644ec022012-03-26 09:45:40 +02001623
Chris Wilson42d6ab42012-07-26 11:49:32 +01001624static void i915_gtt_color_adjust(struct drm_mm_node *node,
1625 unsigned long color,
1626 unsigned long *start,
1627 unsigned long *end)
1628{
1629 if (node->color != color)
1630 *start += 4096;
1631
1632 if (!list_empty(&node->node_list)) {
1633 node = list_entry(node->node_list.next,
1634 struct drm_mm_node,
1635 node_list);
1636 if (node->allocated && node->color != color)
1637 *end -= 4096;
1638 }
1639}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001640
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001641int i915_gem_setup_global_gtt(struct drm_device *dev,
1642 unsigned long start,
1643 unsigned long mappable_end,
1644 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001645{
Ben Widawskye78891c2013-01-25 16:41:04 -08001646 /* Let GEM Manage all of the aperture.
1647 *
1648 * However, leave one page at the end still bound to the scratch page.
1649 * There are a number of places where the hardware apparently prefetches
1650 * past the end of the object, and we've seen multiple hangs with the
1651 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1652 * aperture. One page should be enough to keep any prefetching inside
1653 * of the aperture.
1654 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001655 struct drm_i915_private *dev_priv = dev->dev_private;
1656 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001657 struct drm_mm_node *entry;
1658 struct drm_i915_gem_object *obj;
1659 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02001660 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02001661
Ben Widawsky35451cb2013-01-17 12:45:13 -08001662 BUG_ON(mappable_end > end);
1663
Chris Wilsoned2f3452012-11-15 11:32:19 +00001664 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001665 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001666 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001667 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001668
Chris Wilsoned2f3452012-11-15 11:32:19 +00001669 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001670 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001671 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001672
Ben Widawskyedd41a82013-07-05 14:41:05 -07001673 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001674 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001675
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001676 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001677 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001678 if (ret) {
1679 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
1680 return ret;
1681 }
Chris Wilsoned2f3452012-11-15 11:32:19 +00001682 obj->has_global_gtt_mapping = 1;
1683 }
1684
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001685 dev_priv->gtt.base.start = start;
1686 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001687
Chris Wilsoned2f3452012-11-15 11:32:19 +00001688 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001689 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00001690 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1691 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08001692 ggtt_vm->clear_range(ggtt_vm, hole_start,
1693 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001694 }
1695
1696 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08001697 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001698
Daniel Vetterfa76da32014-08-06 20:19:54 +02001699 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
1700 struct i915_hw_ppgtt *ppgtt;
1701
1702 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1703 if (!ppgtt)
1704 return -ENOMEM;
1705
1706 ret = __hw_ppgtt_init(dev, ppgtt);
1707 if (ret != 0)
1708 return ret;
1709
1710 dev_priv->mm.aliasing_ppgtt = ppgtt;
1711 }
1712
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001713 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001714}
1715
Ben Widawskyd7e50082012-12-18 10:31:25 -08001716void i915_gem_init_global_gtt(struct drm_device *dev)
1717{
1718 struct drm_i915_private *dev_priv = dev->dev_private;
1719 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001720
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001721 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001722 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001723
Ben Widawskye78891c2013-01-25 16:41:04 -08001724 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001725}
1726
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001727void i915_global_gtt_cleanup(struct drm_device *dev)
1728{
1729 struct drm_i915_private *dev_priv = dev->dev_private;
1730 struct i915_address_space *vm = &dev_priv->gtt.base;
1731
Daniel Vetter70e32542014-08-06 15:04:57 +02001732 if (dev_priv->mm.aliasing_ppgtt) {
1733 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1734
1735 ppgtt->base.cleanup(&ppgtt->base);
1736 }
1737
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001738 if (drm_mm_initialized(&vm->mm)) {
1739 drm_mm_takedown(&vm->mm);
1740 list_del(&vm->global_link);
1741 }
1742
1743 vm->cleanup(vm);
1744}
Daniel Vetter70e32542014-08-06 15:04:57 +02001745
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001746static int setup_scratch_page(struct drm_device *dev)
1747{
1748 struct drm_i915_private *dev_priv = dev->dev_private;
1749 struct page *page;
1750 dma_addr_t dma_addr;
1751
1752 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1753 if (page == NULL)
1754 return -ENOMEM;
1755 get_page(page);
1756 set_pages_uc(page, 1);
1757
1758#ifdef CONFIG_INTEL_IOMMU
1759 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1760 PCI_DMA_BIDIRECTIONAL);
1761 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1762 return -EINVAL;
1763#else
1764 dma_addr = page_to_phys(page);
1765#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001766 dev_priv->gtt.base.scratch.page = page;
1767 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001768
1769 return 0;
1770}
1771
1772static void teardown_scratch_page(struct drm_device *dev)
1773{
1774 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001775 struct page *page = dev_priv->gtt.base.scratch.page;
1776
1777 set_pages_wb(page, 1);
1778 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001779 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001780 put_page(page);
1781 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001782}
1783
1784static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1785{
1786 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1787 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1788 return snb_gmch_ctl << 20;
1789}
1790
Ben Widawsky9459d252013-11-03 16:53:55 -08001791static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1792{
1793 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1794 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1795 if (bdw_gmch_ctl)
1796 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07001797
1798#ifdef CONFIG_X86_32
1799 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
1800 if (bdw_gmch_ctl > 4)
1801 bdw_gmch_ctl = 4;
1802#endif
1803
Ben Widawsky9459d252013-11-03 16:53:55 -08001804 return bdw_gmch_ctl << 20;
1805}
1806
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001807static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
1808{
1809 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
1810 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
1811
1812 if (gmch_ctrl)
1813 return 1 << (20 + gmch_ctrl);
1814
1815 return 0;
1816}
1817
Ben Widawskybaa09f52013-01-24 13:49:57 -08001818static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001819{
1820 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1821 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1822 return snb_gmch_ctl << 25; /* 32 MB units */
1823}
1824
Ben Widawsky9459d252013-11-03 16:53:55 -08001825static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1826{
1827 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1828 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1829 return bdw_gmch_ctl << 25; /* 32 MB units */
1830}
1831
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001832static size_t chv_get_stolen_size(u16 gmch_ctrl)
1833{
1834 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
1835 gmch_ctrl &= SNB_GMCH_GMS_MASK;
1836
1837 /*
1838 * 0x0 to 0x10: 32MB increments starting at 0MB
1839 * 0x11 to 0x16: 4MB increments starting at 8MB
1840 * 0x17 to 0x1d: 4MB increments start at 36MB
1841 */
1842 if (gmch_ctrl < 0x11)
1843 return gmch_ctrl << 25;
1844 else if (gmch_ctrl < 0x17)
1845 return (gmch_ctrl - 0x11 + 2) << 22;
1846 else
1847 return (gmch_ctrl - 0x17 + 9) << 22;
1848}
1849
Ben Widawsky63340132013-11-04 19:32:22 -08001850static int ggtt_probe_common(struct drm_device *dev,
1851 size_t gtt_size)
1852{
1853 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001854 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08001855 int ret;
1856
1857 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001858 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08001859 (pci_resource_len(dev->pdev, 0) / 2);
1860
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001861 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08001862 if (!dev_priv->gtt.gsm) {
1863 DRM_ERROR("Failed to map the gtt page table\n");
1864 return -ENOMEM;
1865 }
1866
1867 ret = setup_scratch_page(dev);
1868 if (ret) {
1869 DRM_ERROR("Scratch setup failed\n");
1870 /* iounmap will also get called at remove, but meh */
1871 iounmap(dev_priv->gtt.gsm);
1872 }
1873
1874 return ret;
1875}
1876
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001877/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1878 * bits. When using advanced contexts each context stores its own PAT, but
1879 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001880static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001881{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001882 uint64_t pat;
1883
1884 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1885 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1886 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1887 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1888 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1889 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1890 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1891 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1892
1893 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1894 * write would work. */
1895 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1896 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1897}
1898
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001899static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
1900{
1901 uint64_t pat;
1902
1903 /*
1904 * Map WB on BDW to snooped on CHV.
1905 *
1906 * Only the snoop bit has meaning for CHV, the rest is
1907 * ignored.
1908 *
1909 * Note that the harware enforces snooping for all page
1910 * table accesses. The snoop bit is actually ignored for
1911 * PDEs.
1912 */
1913 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
1914 GEN8_PPAT(1, 0) |
1915 GEN8_PPAT(2, 0) |
1916 GEN8_PPAT(3, 0) |
1917 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
1918 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
1919 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
1920 GEN8_PPAT(7, CHV_PPAT_SNOOP);
1921
1922 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1923 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1924}
1925
Ben Widawsky63340132013-11-04 19:32:22 -08001926static int gen8_gmch_probe(struct drm_device *dev,
1927 size_t *gtt_total,
1928 size_t *stolen,
1929 phys_addr_t *mappable_base,
1930 unsigned long *mappable_end)
1931{
1932 struct drm_i915_private *dev_priv = dev->dev_private;
1933 unsigned int gtt_size;
1934 u16 snb_gmch_ctl;
1935 int ret;
1936
1937 /* TODO: We're not aware of mappable constraints on gen8 yet */
1938 *mappable_base = pci_resource_start(dev->pdev, 2);
1939 *mappable_end = pci_resource_len(dev->pdev, 2);
1940
1941 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1942 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1943
1944 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1945
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001946 if (IS_CHERRYVIEW(dev)) {
1947 *stolen = chv_get_stolen_size(snb_gmch_ctl);
1948 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
1949 } else {
1950 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1951 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
1952 }
Ben Widawsky63340132013-11-04 19:32:22 -08001953
Ben Widawskyd31eb102013-11-02 21:07:17 -07001954 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001955
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001956 if (IS_CHERRYVIEW(dev))
1957 chv_setup_private_ppat(dev_priv);
1958 else
1959 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001960
Ben Widawsky63340132013-11-04 19:32:22 -08001961 ret = ggtt_probe_common(dev, gtt_size);
1962
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001963 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1964 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001965
1966 return ret;
1967}
1968
Ben Widawskybaa09f52013-01-24 13:49:57 -08001969static int gen6_gmch_probe(struct drm_device *dev,
1970 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001971 size_t *stolen,
1972 phys_addr_t *mappable_base,
1973 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001974{
1975 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001976 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001977 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001978 int ret;
1979
Ben Widawsky41907dd2013-02-08 11:32:47 -08001980 *mappable_base = pci_resource_start(dev->pdev, 2);
1981 *mappable_end = pci_resource_len(dev->pdev, 2);
1982
Ben Widawskybaa09f52013-01-24 13:49:57 -08001983 /* 64/512MB is the current min/max we actually know of, but this is just
1984 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001985 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001986 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001987 DRM_ERROR("Unknown GMADR size (%lx)\n",
1988 dev_priv->gtt.mappable_end);
1989 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001990 }
1991
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001992 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1993 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001994 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001995
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07001996 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001997
Ben Widawsky63340132013-11-04 19:32:22 -08001998 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001999 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
2000
Ben Widawsky63340132013-11-04 19:32:22 -08002001 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002002
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002003 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2004 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002005
2006 return ret;
2007}
2008
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002009static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002010{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002011
2012 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002013
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002014 iounmap(gtt->gsm);
2015 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002016}
2017
2018static int i915_gmch_probe(struct drm_device *dev,
2019 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002020 size_t *stolen,
2021 phys_addr_t *mappable_base,
2022 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002023{
2024 struct drm_i915_private *dev_priv = dev->dev_private;
2025 int ret;
2026
Ben Widawskybaa09f52013-01-24 13:49:57 -08002027 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2028 if (!ret) {
2029 DRM_ERROR("failed to set up gmch\n");
2030 return -EIO;
2031 }
2032
Ben Widawsky41907dd2013-02-08 11:32:47 -08002033 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002034
2035 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002036 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002037
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002038 if (unlikely(dev_priv->gtt.do_idle_maps))
2039 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2040
Ben Widawskybaa09f52013-01-24 13:49:57 -08002041 return 0;
2042}
2043
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002044static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002045{
2046 intel_gmch_remove();
2047}
2048
2049int i915_gem_gtt_init(struct drm_device *dev)
2050{
2051 struct drm_i915_private *dev_priv = dev->dev_private;
2052 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002053 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002054
Ben Widawskybaa09f52013-01-24 13:49:57 -08002055 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002056 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002057 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002058 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002059 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002060 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002061 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002062 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002063 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002064 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002065 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002066 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002067 else if (INTEL_INFO(dev)->gen >= 7)
2068 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002069 else
Chris Wilson350ec882013-08-06 13:17:02 +01002070 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002071 } else {
2072 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2073 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002074 }
2075
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002076 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002077 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002078 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002079 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002080
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002081 gtt->base.dev = dev;
2082
Ben Widawskybaa09f52013-01-24 13:49:57 -08002083 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002084 DRM_INFO("Memory usable by graphics device = %zdM\n",
2085 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002086 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2087 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002088#ifdef CONFIG_INTEL_IOMMU
2089 if (intel_iommu_gfx_mapped)
2090 DRM_INFO("VT-d active for gfx access\n");
2091#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002092 /*
2093 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2094 * user's requested state against the hardware/driver capabilities. We
2095 * do this now so that we can print out any log messages once rather
2096 * than every time we check intel_enable_ppgtt().
2097 */
2098 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2099 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002100
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002101 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002102}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002103
2104static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2105 struct i915_address_space *vm)
2106{
2107 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2108 if (vma == NULL)
2109 return ERR_PTR(-ENOMEM);
2110
2111 INIT_LIST_HEAD(&vma->vma_link);
2112 INIT_LIST_HEAD(&vma->mm_list);
2113 INIT_LIST_HEAD(&vma->exec_list);
2114 vma->vm = vm;
2115 vma->obj = obj;
2116
2117 switch (INTEL_INFO(vm->dev)->gen) {
2118 case 8:
2119 case 7:
2120 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002121 if (i915_is_ggtt(vm)) {
2122 vma->unbind_vma = ggtt_unbind_vma;
2123 vma->bind_vma = ggtt_bind_vma;
2124 } else {
2125 vma->unbind_vma = ppgtt_unbind_vma;
2126 vma->bind_vma = ppgtt_bind_vma;
2127 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002128 break;
2129 case 5:
2130 case 4:
2131 case 3:
2132 case 2:
2133 BUG_ON(!i915_is_ggtt(vm));
2134 vma->unbind_vma = i915_ggtt_unbind_vma;
2135 vma->bind_vma = i915_ggtt_bind_vma;
2136 break;
2137 default:
2138 BUG();
2139 }
2140
2141 /* Keep GGTT vmas first to make debug easier */
2142 if (i915_is_ggtt(vm))
2143 list_add(&vma->vma_link, &obj->vma_list);
Michel Thierrye07f0552014-08-19 15:49:41 +01002144 else {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002145 list_add_tail(&vma->vma_link, &obj->vma_list);
Michel Thierrye07f0552014-08-19 15:49:41 +01002146 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
2147 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002148
2149 return vma;
2150}
2151
2152struct i915_vma *
2153i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2154 struct i915_address_space *vm)
2155{
2156 struct i915_vma *vma;
2157
2158 vma = i915_gem_obj_to_vma(obj, vm);
2159 if (!vma)
2160 vma = __i915_gem_vma_create(obj, vm);
2161
2162 return vma;
2163}