blob: ee38fafc6917053e2f6193a18fff5f2c279c0a09 [file] [log] [blame]
Daniel Vetter76aaf222010-11-05 22:23:30 +01001/*
2 * Copyright © 2010 Daniel Vetter
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
Daniel Vetter0e46ce22014-01-08 16:10:27 +010025#include <linux/seq_file.h>
David Howells760285e2012-10-02 18:01:07 +010026#include <drm/drmP.h>
27#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010028#include "i915_drv.h"
29#include "i915_trace.h"
30#include "intel_drv.h"
31
Ben Widawsky6670a5a2013-06-27 16:30:04 -070032#define GEN6_PPGTT_PD_ENTRIES 512
33#define I915_PPGTT_PT_ENTRIES (PAGE_SIZE / sizeof(gen6_gtt_pte_t))
Ben Widawskyd31eb102013-11-02 21:07:17 -070034typedef uint64_t gen8_gtt_pte_t;
Ben Widawsky37aca442013-11-04 20:47:32 -080035typedef gen8_gtt_pte_t gen8_ppgtt_pde_t;
Ben Widawsky6670a5a2013-06-27 16:30:04 -070036
Ben Widawsky26b1ff32012-11-04 09:21:31 -080037/* PPGTT stuff */
38#define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0))
Ben Widawsky0d8ff152013-07-04 11:02:03 -070039#define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0))
Ben Widawsky26b1ff32012-11-04 09:21:31 -080040
41#define GEN6_PDE_VALID (1 << 0)
42/* gen6+ has bit 11-4 for physical addr bit 39-32 */
43#define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
44
45#define GEN6_PTE_VALID (1 << 0)
46#define GEN6_PTE_UNCACHED (1 << 1)
47#define HSW_PTE_UNCACHED (0)
48#define GEN6_PTE_CACHE_LLC (2 << 1)
Chris Wilson350ec882013-08-06 13:17:02 +010049#define GEN7_PTE_CACHE_L3_LLC (3 << 1)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080050#define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
Ben Widawsky0d8ff152013-07-04 11:02:03 -070051#define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr)
52
53/* Cacheability Control is a 4-bit value. The low three bits are stored in *
54 * bits 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
55 */
56#define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \
57 (((bits) & 0x8) << (11 - 3)))
Ben Widawsky87a6b682013-08-04 23:47:29 -070058#define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2)
Ben Widawsky0d8ff152013-07-04 11:02:03 -070059#define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3)
Ben Widawsky4d15c142013-07-04 11:02:06 -070060#define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb)
Chris Wilsonc51e9702013-11-22 10:37:53 +000061#define HSW_WB_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x8)
Chris Wilson651d7942013-08-08 14:41:10 +010062#define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6)
Chris Wilsonc51e9702013-11-22 10:37:53 +000063#define HSW_WT_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x7)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080064
Ben Widawsky459108b2013-11-02 21:07:23 -070065#define GEN8_PTES_PER_PAGE (PAGE_SIZE / sizeof(gen8_gtt_pte_t))
Ben Widawsky37aca442013-11-04 20:47:32 -080066#define GEN8_PDES_PER_PAGE (PAGE_SIZE / sizeof(gen8_ppgtt_pde_t))
67#define GEN8_LEGACY_PDPS 4
68
Ben Widawskyfbe5d362013-11-04 19:56:49 -080069#define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD)
70#define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */
71#define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */
72#define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */
73
Ben Widawsky6f65e292013-12-06 14:10:56 -080074static void ppgtt_bind_vma(struct i915_vma *vma,
75 enum i915_cache_level cache_level,
76 u32 flags);
77static void ppgtt_unbind_vma(struct i915_vma *vma);
Ben Widawskyeeb94882013-12-06 14:11:10 -080078static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt);
Ben Widawsky6f65e292013-12-06 14:10:56 -080079
Ben Widawsky94ec8f62013-11-02 21:07:18 -070080static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
81 enum i915_cache_level level,
82 bool valid)
83{
84 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
85 pte |= addr;
Ben Widawskyfbe5d362013-11-04 19:56:49 -080086 if (level != I915_CACHE_NONE)
87 pte |= PPAT_CACHED_INDEX;
88 else
89 pte |= PPAT_UNCACHED_INDEX;
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,
108 bool valid)
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,
130 bool valid)
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
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700152#define BYT_PTE_WRITEABLE (1 << 1)
153#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
154
Ben Widawsky80a74f72013-06-27 16:30:19 -0700155static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700156 enum i915_cache_level level,
157 bool valid)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700158{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700159 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700160 pte |= GEN6_PTE_ADDR_ENCODE(addr);
161
162 /* Mark the page as writeable. Other platforms don't have a
163 * setting for read-only/writable, so this matches that behavior.
164 */
165 pte |= BYT_PTE_WRITEABLE;
166
167 if (level != I915_CACHE_NONE)
168 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
169
170 return pte;
171}
172
Ben Widawsky80a74f72013-06-27 16:30:19 -0700173static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700174 enum i915_cache_level level,
175 bool valid)
Kenneth Graunke91197082013-04-22 00:53:51 -0700176{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700177 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700178 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700179
180 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700181 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700182
183 return pte;
184}
185
Ben Widawsky4d15c142013-07-04 11:02:06 -0700186static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700187 enum i915_cache_level level,
188 bool valid)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700189{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700190 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700191 pte |= HSW_PTE_ADDR_ENCODE(addr);
192
Chris Wilson651d7942013-08-08 14:41:10 +0100193 switch (level) {
194 case I915_CACHE_NONE:
195 break;
196 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000197 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100198 break;
199 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000200 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100201 break;
202 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700203
204 return pte;
205}
206
Ben Widawsky94e409c2013-11-04 22:29:36 -0800207/* Broadwell Page Directory Pointer Descriptors */
208static int gen8_write_pdp(struct intel_ring_buffer *ring, unsigned entry,
Ben Widawskye178f702013-12-06 14:10:47 -0800209 uint64_t val, bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800210{
Ben Widawskye178f702013-12-06 14:10:47 -0800211 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800212 int ret;
213
214 BUG_ON(entry >= 4);
215
Ben Widawskye178f702013-12-06 14:10:47 -0800216 if (synchronous) {
217 I915_WRITE(GEN8_RING_PDP_UDW(ring, entry), val >> 32);
218 I915_WRITE(GEN8_RING_PDP_LDW(ring, entry), (u32)val);
219 return 0;
220 }
221
Ben Widawsky94e409c2013-11-04 22:29:36 -0800222 ret = intel_ring_begin(ring, 6);
223 if (ret)
224 return ret;
225
226 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
227 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
228 intel_ring_emit(ring, (u32)(val >> 32));
229 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
230 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
231 intel_ring_emit(ring, (u32)(val));
232 intel_ring_advance(ring);
233
234 return 0;
235}
236
Ben Widawskyeeb94882013-12-06 14:11:10 -0800237static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
238 struct intel_ring_buffer *ring,
239 bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800240{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800241 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800242
243 /* bit of a hack to find the actual last used pd */
244 int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
245
Ben Widawsky94e409c2013-11-04 22:29:36 -0800246 for (i = used_pd - 1; i >= 0; i--) {
247 dma_addr_t addr = ppgtt->pd_dma_addr[i];
Ben Widawskyeeb94882013-12-06 14:11:10 -0800248 ret = gen8_write_pdp(ring, i, addr, synchronous);
249 if (ret)
250 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800251 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800252
Ben Widawskyeeb94882013-12-06 14:11:10 -0800253 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800254}
255
Ben Widawsky459108b2013-11-02 21:07:23 -0700256static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
257 unsigned first_entry,
258 unsigned num_entries,
259 bool use_scratch)
260{
261 struct i915_hw_ppgtt *ppgtt =
262 container_of(vm, struct i915_hw_ppgtt, base);
263 gen8_gtt_pte_t *pt_vaddr, scratch_pte;
264 unsigned act_pt = first_entry / GEN8_PTES_PER_PAGE;
265 unsigned first_pte = first_entry % GEN8_PTES_PER_PAGE;
266 unsigned last_pte, i;
267
268 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
269 I915_CACHE_LLC, use_scratch);
270
271 while (num_entries) {
272 struct page *page_table = &ppgtt->gen8_pt_pages[act_pt];
273
274 last_pte = first_pte + num_entries;
275 if (last_pte > GEN8_PTES_PER_PAGE)
276 last_pte = GEN8_PTES_PER_PAGE;
277
278 pt_vaddr = kmap_atomic(page_table);
279
280 for (i = first_pte; i < last_pte; i++)
281 pt_vaddr[i] = scratch_pte;
282
283 kunmap_atomic(pt_vaddr);
284
285 num_entries -= last_pte - first_pte;
286 first_pte = 0;
287 act_pt++;
288 }
289}
290
Ben Widawsky9df15b42013-11-02 21:07:24 -0700291static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
292 struct sg_table *pages,
293 unsigned first_entry,
294 enum i915_cache_level cache_level)
295{
296 struct i915_hw_ppgtt *ppgtt =
297 container_of(vm, struct i915_hw_ppgtt, base);
298 gen8_gtt_pte_t *pt_vaddr;
299 unsigned act_pt = first_entry / GEN8_PTES_PER_PAGE;
300 unsigned act_pte = first_entry % GEN8_PTES_PER_PAGE;
301 struct sg_page_iter sg_iter;
302
Chris Wilson6f1cc992013-12-31 15:50:31 +0000303 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700304 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilson6f1cc992013-12-31 15:50:31 +0000305 if (pt_vaddr == NULL)
306 pt_vaddr = kmap_atomic(&ppgtt->gen8_pt_pages[act_pt]);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700307
Chris Wilson6f1cc992013-12-31 15:50:31 +0000308 pt_vaddr[act_pte] =
309 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
310 cache_level, true);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700311 if (++act_pte == GEN8_PTES_PER_PAGE) {
312 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000313 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700314 act_pt++;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700315 act_pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700316 }
317 }
Chris Wilson6f1cc992013-12-31 15:50:31 +0000318 if (pt_vaddr)
319 kunmap_atomic(pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700320}
321
Ben Widawskyb45a6712014-02-12 14:28:44 -0800322static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
323{
324 int i;
325
326 for (i = 0; i < ppgtt->num_pd_pages ; i++)
327 kfree(ppgtt->gen8_pt_dma_addr[i]);
328
329 __free_pages(ppgtt->gen8_pt_pages, get_order(ppgtt->num_pt_pages << PAGE_SHIFT));
330 __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
331}
332
333static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
334{
335 int i, j;
336
337 for (i = 0; i < ppgtt->num_pd_pages; i++) {
338 /* TODO: In the future we'll support sparse mappings, so this
339 * will have to change. */
340 if (!ppgtt->pd_dma_addr[i])
341 continue;
342
343 pci_unmap_page(ppgtt->base.dev->pdev,
344 ppgtt->pd_dma_addr[i],
345 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
346
347 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
348 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
349 if (addr)
350 pci_unmap_page(ppgtt->base.dev->pdev,
351 addr,
352 PAGE_SIZE,
353 PCI_DMA_BIDIRECTIONAL);
354
355 }
356 }
357}
358
Ben Widawsky37aca442013-11-04 20:47:32 -0800359static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
360{
361 struct i915_hw_ppgtt *ppgtt =
362 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800363
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800364 list_del(&vm->global_link);
Ben Widawsky686e1f62013-11-25 09:54:34 -0800365 drm_mm_takedown(&vm->mm);
366
Ben Widawskyb45a6712014-02-12 14:28:44 -0800367 gen8_ppgtt_unmap_pages(ppgtt);
368 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800369}
370
371/**
372 * GEN8 legacy ppgtt programming is accomplished through 4 PDP registers with a
373 * net effect resembling a 2-level page table in normal x86 terms. Each PDP
374 * represents 1GB of memory
375 * 4 * 512 * 512 * 4096 = 4GB legacy 32b address space.
376 *
377 * TODO: Do something with the size parameter
378 **/
379static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
380{
381 struct page *pt_pages;
382 int i, j, ret = -ENOMEM;
383 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
384 const int num_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
385
386 if (size % (1<<30))
387 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
388
389 /* FIXME: split allocation into smaller pieces. For now we only ever do
390 * this once, but with full PPGTT, the multiple contiguous allocations
391 * will be bad.
392 */
393 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
394 if (!ppgtt->pd_pages)
395 return -ENOMEM;
396
397 pt_pages = alloc_pages(GFP_KERNEL, get_order(num_pt_pages << PAGE_SHIFT));
398 if (!pt_pages) {
399 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
400 return -ENOMEM;
401 }
402
403 ppgtt->gen8_pt_pages = pt_pages;
404 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
405 ppgtt->num_pt_pages = 1 << get_order(num_pt_pages << PAGE_SHIFT);
406 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800407 ppgtt->enable = gen8_ppgtt_enable;
Ben Widawskyeeb94882013-12-06 14:11:10 -0800408 ppgtt->switch_mm = gen8_mm_switch;
Ben Widawsky459108b2013-11-02 21:07:23 -0700409 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700410 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Ben Widawsky37aca442013-11-04 20:47:32 -0800411 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -0800412 ppgtt->base.start = 0;
413 ppgtt->base.total = ppgtt->num_pt_pages * GEN8_PTES_PER_PAGE * PAGE_SIZE;
Ben Widawsky37aca442013-11-04 20:47:32 -0800414
415 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
416
417 /*
418 * - Create a mapping for the page directories.
419 * - For each page directory:
420 * allocate space for page table mappings.
421 * map each page table
422 */
423 for (i = 0; i < max_pdp; i++) {
424 dma_addr_t temp;
425 temp = pci_map_page(ppgtt->base.dev->pdev,
426 &ppgtt->pd_pages[i], 0,
427 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
428 if (pci_dma_mapping_error(ppgtt->base.dev->pdev, temp))
429 goto err_out;
430
431 ppgtt->pd_dma_addr[i] = temp;
432
433 ppgtt->gen8_pt_dma_addr[i] = kmalloc(sizeof(dma_addr_t) * GEN8_PDES_PER_PAGE, GFP_KERNEL);
434 if (!ppgtt->gen8_pt_dma_addr[i])
435 goto err_out;
436
437 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
438 struct page *p = &pt_pages[i * GEN8_PDES_PER_PAGE + j];
439 temp = pci_map_page(ppgtt->base.dev->pdev,
440 p, 0, PAGE_SIZE,
441 PCI_DMA_BIDIRECTIONAL);
442
443 if (pci_dma_mapping_error(ppgtt->base.dev->pdev, temp))
444 goto err_out;
445
446 ppgtt->gen8_pt_dma_addr[i][j] = temp;
447 }
448 }
449
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800450 /* For now, the PPGTT helper functions all require that the PDEs are
451 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
452 * will never need to touch the PDEs again */
453 for (i = 0; i < max_pdp; i++) {
454 gen8_ppgtt_pde_t *pd_vaddr;
455 pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
456 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
457 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
458 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
459 I915_CACHE_LLC);
460 }
461 kunmap_atomic(pd_vaddr);
462 }
463
Ben Widawsky459108b2013-11-02 21:07:23 -0700464 ppgtt->base.clear_range(&ppgtt->base, 0,
465 ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE,
466 true);
467
Ben Widawsky37aca442013-11-04 20:47:32 -0800468 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
469 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
470 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
471 ppgtt->num_pt_pages,
472 (ppgtt->num_pt_pages - num_pt_pages) +
473 size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700474 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800475
476err_out:
477 ppgtt->base.cleanup(&ppgtt->base);
478 return ret;
479}
480
Ben Widawsky87d60b62013-12-06 14:11:29 -0800481static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
482{
483 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
484 struct i915_address_space *vm = &ppgtt->base;
485 gen6_gtt_pte_t __iomem *pd_addr;
486 gen6_gtt_pte_t scratch_pte;
487 uint32_t pd_entry;
488 int pte, pde;
489
490 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
491
492 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
493 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
494
495 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
496 ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
497 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
498 u32 expected;
499 gen6_gtt_pte_t *pt_vaddr;
500 dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
501 pd_entry = readl(pd_addr + pde);
502 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
503
504 if (pd_entry != expected)
505 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
506 pde,
507 pd_entry,
508 expected);
509 seq_printf(m, "\tPDE: %x\n", pd_entry);
510
511 pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
512 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
513 unsigned long va =
514 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
515 (pte * PAGE_SIZE);
516 int i;
517 bool found = false;
518 for (i = 0; i < 4; i++)
519 if (pt_vaddr[pte + i] != scratch_pte)
520 found = true;
521 if (!found)
522 continue;
523
524 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
525 for (i = 0; i < 4; i++) {
526 if (pt_vaddr[pte + i] != scratch_pte)
527 seq_printf(m, " %08x", pt_vaddr[pte + i]);
528 else
529 seq_puts(m, " SCRATCH ");
530 }
531 seq_puts(m, "\n");
532 }
533 kunmap_atomic(pt_vaddr);
534 }
535}
536
Ben Widawsky3e302542013-04-23 23:15:32 -0700537static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700538{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700539 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700540 gen6_gtt_pte_t __iomem *pd_addr;
541 uint32_t pd_entry;
542 int i;
543
Ben Widawsky0a732872013-04-23 23:15:30 -0700544 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700545 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
546 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
547 for (i = 0; i < ppgtt->num_pd_entries; i++) {
548 dma_addr_t pt_addr;
549
550 pt_addr = ppgtt->pt_dma_addr[i];
551 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
552 pd_entry |= GEN6_PDE_VALID;
553
554 writel(pd_entry, pd_addr + i);
555 }
556 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700557}
558
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800559static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700560{
Ben Widawsky3e302542013-04-23 23:15:32 -0700561 BUG_ON(ppgtt->pd_offset & 0x3f);
562
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800563 return (ppgtt->pd_offset / 64) << 16;
564}
Ben Widawsky61973492013-04-08 18:43:54 -0700565
Ben Widawsky90252e52013-12-06 14:11:12 -0800566static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
567 struct intel_ring_buffer *ring,
568 bool synchronous)
569{
570 struct drm_device *dev = ppgtt->base.dev;
571 struct drm_i915_private *dev_priv = dev->dev_private;
572 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700573
Ben Widawsky90252e52013-12-06 14:11:12 -0800574 /* If we're in reset, we can assume the GPU is sufficiently idle to
575 * manually frob these bits. Ideally we could use the ring functions,
576 * except our error handling makes it quite difficult (can't use
577 * intel_ring_begin, ring->flush, or intel_ring_advance)
578 *
579 * FIXME: We should try not to special case reset
580 */
581 if (synchronous ||
582 i915_reset_in_progress(&dev_priv->gpu_error)) {
583 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
584 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
585 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
586 POSTING_READ(RING_PP_DIR_BASE(ring));
587 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700588 }
589
Ben Widawsky90252e52013-12-06 14:11:12 -0800590 /* NB: TLBs must be flushed and invalidated before a switch */
591 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
592 if (ret)
593 return ret;
594
595 ret = intel_ring_begin(ring, 6);
596 if (ret)
597 return ret;
598
599 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
600 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
601 intel_ring_emit(ring, PP_DIR_DCLV_2G);
602 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
603 intel_ring_emit(ring, get_pd_offset(ppgtt));
604 intel_ring_emit(ring, MI_NOOP);
605 intel_ring_advance(ring);
606
607 return 0;
608}
609
Ben Widawsky48a10382013-12-06 14:11:11 -0800610static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
611 struct intel_ring_buffer *ring,
612 bool synchronous)
613{
614 struct drm_device *dev = ppgtt->base.dev;
615 struct drm_i915_private *dev_priv = dev->dev_private;
616 int ret;
617
618 /* If we're in reset, we can assume the GPU is sufficiently idle to
619 * manually frob these bits. Ideally we could use the ring functions,
620 * except our error handling makes it quite difficult (can't use
621 * intel_ring_begin, ring->flush, or intel_ring_advance)
622 *
623 * FIXME: We should try not to special case reset
624 */
625 if (synchronous ||
626 i915_reset_in_progress(&dev_priv->gpu_error)) {
627 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
628 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
629 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
630 POSTING_READ(RING_PP_DIR_BASE(ring));
631 return 0;
632 }
633
634 /* NB: TLBs must be flushed and invalidated before a switch */
635 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
636 if (ret)
637 return ret;
638
639 ret = intel_ring_begin(ring, 6);
640 if (ret)
641 return ret;
642
643 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
644 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
645 intel_ring_emit(ring, PP_DIR_DCLV_2G);
646 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
647 intel_ring_emit(ring, get_pd_offset(ppgtt));
648 intel_ring_emit(ring, MI_NOOP);
649 intel_ring_advance(ring);
650
Ben Widawsky90252e52013-12-06 14:11:12 -0800651 /* XXX: RCS is the only one to auto invalidate the TLBs? */
652 if (ring->id != RCS) {
653 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
654 if (ret)
655 return ret;
656 }
657
Ben Widawsky48a10382013-12-06 14:11:11 -0800658 return 0;
659}
660
Ben Widawskyeeb94882013-12-06 14:11:10 -0800661static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
662 struct intel_ring_buffer *ring,
663 bool synchronous)
664{
665 struct drm_device *dev = ppgtt->base.dev;
666 struct drm_i915_private *dev_priv = dev->dev_private;
667
Ben Widawsky48a10382013-12-06 14:11:11 -0800668 if (!synchronous)
669 return 0;
670
Ben Widawskyeeb94882013-12-06 14:11:10 -0800671 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
672 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
673
674 POSTING_READ(RING_PP_DIR_DCLV(ring));
675
676 return 0;
677}
678
679static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
680{
681 struct drm_device *dev = ppgtt->base.dev;
682 struct drm_i915_private *dev_priv = dev->dev_private;
683 struct intel_ring_buffer *ring;
684 int j, ret;
685
686 for_each_ring(ring, dev_priv, j) {
687 I915_WRITE(RING_MODE_GEN7(ring),
688 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800689
690 /* We promise to do a switch later with FULL PPGTT. If this is
691 * aliasing, this is the one and only switch we'll do */
692 if (USES_FULL_PPGTT(dev))
693 continue;
694
Ben Widawskyeeb94882013-12-06 14:11:10 -0800695 ret = ppgtt->switch_mm(ppgtt, ring, true);
696 if (ret)
697 goto err_out;
698 }
699
700 return 0;
701
702err_out:
703 for_each_ring(ring, dev_priv, j)
704 I915_WRITE(RING_MODE_GEN7(ring),
705 _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
706 return ret;
707}
708
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800709static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
710{
711 struct drm_device *dev = ppgtt->base.dev;
712 drm_i915_private_t *dev_priv = dev->dev_private;
713 struct intel_ring_buffer *ring;
714 uint32_t ecochk, ecobits;
715 int i;
716
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800717 ecobits = I915_READ(GAC_ECO_BITS);
718 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
719
720 ecochk = I915_READ(GAM_ECOCHK);
721 if (IS_HASWELL(dev)) {
722 ecochk |= ECOCHK_PPGTT_WB_HSW;
723 } else {
724 ecochk |= ECOCHK_PPGTT_LLC_IVB;
725 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
726 }
727 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800728
Ben Widawsky61973492013-04-08 18:43:54 -0700729 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800730 int ret;
731 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800732 I915_WRITE(RING_MODE_GEN7(ring),
733 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700734
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800735 /* We promise to do a switch later with FULL PPGTT. If this is
736 * aliasing, this is the one and only switch we'll do */
737 if (USES_FULL_PPGTT(dev))
738 continue;
739
Ben Widawskyeeb94882013-12-06 14:11:10 -0800740 ret = ppgtt->switch_mm(ppgtt, ring, true);
741 if (ret)
742 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700743 }
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800744
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800745 return 0;
746}
747
Ben Widawskya3d67d22013-12-06 14:11:06 -0800748static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700749{
Ben Widawskya3d67d22013-12-06 14:11:06 -0800750 struct drm_device *dev = ppgtt->base.dev;
Ben Widawsky61973492013-04-08 18:43:54 -0700751 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700752 struct intel_ring_buffer *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800753 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -0700754 int i;
755
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800756 ecobits = I915_READ(GAC_ECO_BITS);
757 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
758 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700759
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800760 gab_ctl = I915_READ(GAB_CTL);
761 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700762
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800763 ecochk = I915_READ(GAM_ECOCHK);
764 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700765
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800766 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700767
768 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800769 int ret = ppgtt->switch_mm(ppgtt, ring, true);
770 if (ret)
771 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700772 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800773
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700774 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700775}
776
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100777/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700778static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100779 unsigned first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700780 unsigned num_entries,
781 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100782{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700783 struct i915_hw_ppgtt *ppgtt =
784 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700785 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Daniel Vettera15326a2013-03-19 23:48:39 +0100786 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100787 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
788 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100789
Ben Widawskyb35b3802013-10-16 09:18:21 -0700790 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100791
Daniel Vetter7bddb012012-02-09 17:15:47 +0100792 while (num_entries) {
793 last_pte = first_pte + num_entries;
794 if (last_pte > I915_PPGTT_PT_ENTRIES)
795 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100796
Daniel Vettera15326a2013-03-19 23:48:39 +0100797 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100798
799 for (i = first_pte; i < last_pte; i++)
800 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100801
802 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100803
Daniel Vetter7bddb012012-02-09 17:15:47 +0100804 num_entries -= last_pte - first_pte;
805 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100806 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100807 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100808}
809
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700810static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800811 struct sg_table *pages,
812 unsigned first_entry,
813 enum i915_cache_level cache_level)
814{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700815 struct i915_hw_ppgtt *ppgtt =
816 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700817 gen6_gtt_pte_t *pt_vaddr;
Daniel Vettera15326a2013-03-19 23:48:39 +0100818 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200819 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
820 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800821
Chris Wilsoncc797142013-12-31 15:50:30 +0000822 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +0200823 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +0000824 if (pt_vaddr == NULL)
825 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800826
Chris Wilsoncc797142013-12-31 15:50:30 +0000827 pt_vaddr[act_pte] =
828 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
829 cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200830 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
831 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +0000832 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +0100833 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +0200834 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800835 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800836 }
Chris Wilsoncc797142013-12-31 15:50:30 +0000837 if (pt_vaddr)
838 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800839}
840
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700841static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100842{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700843 struct i915_hw_ppgtt *ppgtt =
844 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -0800845 int i;
846
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800847 list_del(&vm->global_link);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700848 drm_mm_takedown(&ppgtt->base.mm);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800849 drm_mm_remove_node(&ppgtt->node);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700850
Daniel Vetter3440d262013-01-24 13:49:56 -0800851 if (ppgtt->pt_dma_addr) {
852 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700853 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800854 ppgtt->pt_dma_addr[i],
855 4096, PCI_DMA_BIDIRECTIONAL);
856 }
857
858 kfree(ppgtt->pt_dma_addr);
859 for (i = 0; i < ppgtt->num_pd_entries; i++)
860 __free_page(ppgtt->pt_pages[i]);
861 kfree(ppgtt->pt_pages);
862 kfree(ppgtt);
863}
864
865static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
866{
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800867#define GEN6_PD_ALIGN (PAGE_SIZE * 16)
868#define GEN6_PD_SIZE (GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700869 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100870 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -0800871 bool retried = false;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800872 int i, ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100873
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800874 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
875 * allocator works in address space sizes, so it's multiplied by page
876 * size. We allocate at the top of the GTT to avoid fragmentation.
877 */
878 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -0800879alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800880 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
881 &ppgtt->node, GEN6_PD_SIZE,
882 GEN6_PD_ALIGN, 0,
883 0, dev_priv->gtt.base.total,
884 DRM_MM_SEARCH_DEFAULT);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800885 if (ret == -ENOSPC && !retried) {
886 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
887 GEN6_PD_SIZE, GEN6_PD_ALIGN,
888 I915_CACHE_NONE, false, true);
889 if (ret)
890 return ret;
891
892 retried = true;
893 goto alloc;
894 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800895
896 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
897 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100898
Chris Wilson08c45262013-07-30 19:04:37 +0100899 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700900 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky48a10382013-12-06 14:11:11 -0800901 if (IS_GEN6(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800902 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -0800903 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -0800904 } else if (IS_HASWELL(dev)) {
905 ppgtt->enable = gen7_ppgtt_enable;
906 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -0800907 } else if (IS_GEN7(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800908 ppgtt->enable = gen7_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -0800909 ppgtt->switch_mm = gen7_mm_switch;
910 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800911 BUG();
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700912 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
913 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
914 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
915 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Ben Widawsky686e1f62013-11-25 09:54:34 -0800916 ppgtt->base.start = 0;
917 ppgtt->base.total = GEN6_PPGTT_PD_ENTRIES * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Daniel Vettera1e22652013-09-21 00:35:38 +0200918 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100919 GFP_KERNEL);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800920 if (!ppgtt->pt_pages) {
921 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -0800922 return -ENOMEM;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800923 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100924
925 for (i = 0; i < ppgtt->num_pd_entries; i++) {
926 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
927 if (!ppgtt->pt_pages[i])
928 goto err_pt_alloc;
929 }
930
Daniel Vettera1e22652013-09-21 00:35:38 +0200931 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800932 GFP_KERNEL);
933 if (!ppgtt->pt_dma_addr)
934 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100935
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800936 for (i = 0; i < ppgtt->num_pd_entries; i++) {
937 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200938
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800939 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
940 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100941
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800942 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
943 ret = -EIO;
944 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100945
Daniel Vetter211c5682012-04-10 17:29:17 +0200946 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800947 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100948 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100949
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700950 ppgtt->base.clear_range(&ppgtt->base, 0,
Ben Widawsky828c7902013-10-16 09:21:30 -0700951 ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800952 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100953
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800954 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
955 ppgtt->node.size >> 20,
956 ppgtt->node.start / PAGE_SIZE);
957 ppgtt->pd_offset =
958 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100959
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100960 return 0;
961
962err_pd_pin:
963 if (ppgtt->pt_dma_addr) {
964 for (i--; i >= 0; i--)
965 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
966 4096, PCI_DMA_BIDIRECTIONAL);
967 }
968err_pt_alloc:
969 kfree(ppgtt->pt_dma_addr);
970 for (i = 0; i < ppgtt->num_pd_entries; i++) {
971 if (ppgtt->pt_pages[i])
972 __free_page(ppgtt->pt_pages[i]);
973 }
974 kfree(ppgtt->pt_pages);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800975 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -0800976
977 return ret;
978}
979
Ben Widawsky246cbfb2013-12-06 14:11:14 -0800980int i915_gem_init_ppgtt(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -0800981{
982 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd6660ad2013-12-06 14:11:13 -0800983 int ret = 0;
Daniel Vetter3440d262013-01-24 13:49:56 -0800984
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700985 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -0800986
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700987 if (INTEL_INFO(dev)->gen < 8)
988 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -0700989 else if (IS_GEN8(dev))
Ben Widawsky37aca442013-11-04 20:47:32 -0800990 ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700991 else
992 BUG();
993
Ben Widawskyc7c48df2013-12-06 14:11:15 -0800994 if (!ret) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800995 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyc7c48df2013-12-06 14:11:15 -0800996 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700997 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
998 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800999 i915_init_vm(dev_priv, &ppgtt->base);
1000 if (INTEL_INFO(dev)->gen < 8) {
Ben Widawsky9f273d42013-12-06 14:11:16 -08001001 gen6_write_pdes(ppgtt);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001002 DRM_DEBUG("Adding PPGTT at offset %x\n",
1003 ppgtt->pd_offset << 10);
1004 }
Ben Widawsky93bd8642013-07-16 16:50:06 -07001005 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001006
1007 return ret;
1008}
1009
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001010static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001011ppgtt_bind_vma(struct i915_vma *vma,
1012 enum i915_cache_level cache_level,
1013 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001014{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001015 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001016
Ben Widawsky6f65e292013-12-06 14:10:56 -08001017 WARN_ON(flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001018
Ben Widawsky6f65e292013-12-06 14:10:56 -08001019 vma->vm->insert_entries(vma->vm, vma->obj->pages, entry, cache_level);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001020}
1021
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001022static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001023{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001024 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001025
Ben Widawsky6f65e292013-12-06 14:10:56 -08001026 vma->vm->clear_range(vma->vm,
1027 entry,
1028 vma->obj->base.size >> PAGE_SHIFT,
1029 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001030}
1031
Ben Widawskya81cc002013-01-18 12:30:31 -08001032extern int intel_iommu_gfx_mapped;
1033/* Certain Gen5 chipsets require require idling the GPU before
1034 * unmapping anything from the GTT when VT-d is enabled.
1035 */
1036static inline bool needs_idle_maps(struct drm_device *dev)
1037{
1038#ifdef CONFIG_INTEL_IOMMU
1039 /* Query intel_iommu to see if we need the workaround. Presumably that
1040 * was loaded first.
1041 */
1042 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1043 return true;
1044#endif
1045 return false;
1046}
1047
Ben Widawsky5c042282011-10-17 15:51:55 -07001048static bool do_idling(struct drm_i915_private *dev_priv)
1049{
1050 bool ret = dev_priv->mm.interruptible;
1051
Ben Widawskya81cc002013-01-18 12:30:31 -08001052 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001053 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001054 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001055 DRM_ERROR("Couldn't idle GPU\n");
1056 /* Wait a bit, in hopes it avoids the hang */
1057 udelay(10);
1058 }
1059 }
1060
1061 return ret;
1062}
1063
1064static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1065{
Ben Widawskya81cc002013-01-18 12:30:31 -08001066 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001067 dev_priv->mm.interruptible = interruptible;
1068}
1069
Ben Widawsky828c7902013-10-16 09:21:30 -07001070void i915_check_and_clear_faults(struct drm_device *dev)
1071{
1072 struct drm_i915_private *dev_priv = dev->dev_private;
1073 struct intel_ring_buffer *ring;
1074 int i;
1075
1076 if (INTEL_INFO(dev)->gen < 6)
1077 return;
1078
1079 for_each_ring(ring, dev_priv, i) {
1080 u32 fault_reg;
1081 fault_reg = I915_READ(RING_FAULT_REG(ring));
1082 if (fault_reg & RING_FAULT_VALID) {
1083 DRM_DEBUG_DRIVER("Unexpected fault\n"
1084 "\tAddr: 0x%08lx\\n"
1085 "\tAddress space: %s\n"
1086 "\tSource ID: %d\n"
1087 "\tType: %d\n",
1088 fault_reg & PAGE_MASK,
1089 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1090 RING_FAULT_SRCID(fault_reg),
1091 RING_FAULT_FAULT_TYPE(fault_reg));
1092 I915_WRITE(RING_FAULT_REG(ring),
1093 fault_reg & ~RING_FAULT_VALID);
1094 }
1095 }
1096 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1097}
1098
1099void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1100{
1101 struct drm_i915_private *dev_priv = dev->dev_private;
1102
1103 /* Don't bother messing with faults pre GEN6 as we have little
1104 * documentation supporting that it's a good idea.
1105 */
1106 if (INTEL_INFO(dev)->gen < 6)
1107 return;
1108
1109 i915_check_and_clear_faults(dev);
1110
1111 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1112 dev_priv->gtt.base.start / PAGE_SIZE,
1113 dev_priv->gtt.base.total / PAGE_SIZE,
1114 false);
1115}
1116
Daniel Vetter76aaf222010-11-05 22:23:30 +01001117void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1118{
1119 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001120 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001121 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001122
Ben Widawsky828c7902013-10-16 09:21:30 -07001123 i915_check_and_clear_faults(dev);
1124
Chris Wilsonbee4a182011-01-21 10:54:32 +00001125 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001126 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1127 dev_priv->gtt.base.start / PAGE_SIZE,
Ben Widawsky828c7902013-10-16 09:21:30 -07001128 dev_priv->gtt.base.total / PAGE_SIZE,
1129 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001130
Ben Widawsky35c20a62013-05-31 11:28:48 -07001131 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001132 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1133 &dev_priv->gtt.base);
1134 if (!vma)
1135 continue;
1136
Chris Wilson2c225692013-08-09 12:26:45 +01001137 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001138 /* The bind_vma code tries to be smart about tracking mappings.
1139 * Unfortunately above, we've just wiped out the mappings
1140 * without telling our object about it. So we need to fake it.
1141 */
1142 obj->has_global_gtt_mapping = 0;
1143 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001144 }
1145
Ben Widawsky80da2162013-12-06 14:11:17 -08001146
1147 if (INTEL_INFO(dev)->gen >= 8)
1148 return;
1149
1150 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1151 /* TODO: Perhaps it shouldn't be gen6 specific */
1152 if (i915_is_ggtt(vm)) {
1153 if (dev_priv->mm.aliasing_ppgtt)
1154 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1155 continue;
1156 }
1157
1158 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001159 }
1160
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001161 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001162}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001163
Daniel Vetter74163902012-02-15 23:50:21 +01001164int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001165{
Chris Wilson9da3da62012-06-01 15:20:22 +01001166 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001167 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001168
1169 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1170 obj->pages->sgl, obj->pages->nents,
1171 PCI_DMA_BIDIRECTIONAL))
1172 return -ENOSPC;
1173
1174 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001175}
1176
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001177static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1178{
1179#ifdef writeq
1180 writeq(pte, addr);
1181#else
1182 iowrite32((u32)pte, addr);
1183 iowrite32(pte >> 32, addr + 4);
1184#endif
1185}
1186
1187static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1188 struct sg_table *st,
1189 unsigned int first_entry,
1190 enum i915_cache_level level)
1191{
1192 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1193 gen8_gtt_pte_t __iomem *gtt_entries =
1194 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1195 int i = 0;
1196 struct sg_page_iter sg_iter;
1197 dma_addr_t addr;
1198
1199 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1200 addr = sg_dma_address(sg_iter.sg) +
1201 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1202 gen8_set_pte(&gtt_entries[i],
1203 gen8_pte_encode(addr, level, true));
1204 i++;
1205 }
1206
1207 /*
1208 * XXX: This serves as a posting read to make sure that the PTE has
1209 * actually been updated. There is some concern that even though
1210 * registers and PTEs are within the same BAR that they are potentially
1211 * of NUMA access patterns. Therefore, even with the way we assume
1212 * hardware should work, we must keep this posting read for paranoia.
1213 */
1214 if (i != 0)
1215 WARN_ON(readq(&gtt_entries[i-1])
1216 != gen8_pte_encode(addr, level, true));
1217
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001218 /* This next bit makes the above posting read even more important. We
1219 * want to flush the TLBs only after we're certain all the PTE updates
1220 * have finished.
1221 */
1222 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1223 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001224}
1225
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001226/*
1227 * Binds an object into the global gtt with the specified cache level. The object
1228 * will be accessible to the GPU via commands whose operands reference offsets
1229 * within the global GTT as well as accessible by the GPU through the GMADR
1230 * mapped BAR (dev_priv->mm.gtt->gtt).
1231 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001232static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001233 struct sg_table *st,
1234 unsigned int first_entry,
1235 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001236{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001237 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001238 gen6_gtt_pte_t __iomem *gtt_entries =
1239 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001240 int i = 0;
1241 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001242 dma_addr_t addr;
1243
Imre Deak6e995e22013-02-18 19:28:04 +02001244 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001245 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -07001246 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001247 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001248 }
1249
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001250 /* XXX: This serves as a posting read to make sure that the PTE has
1251 * actually been updated. There is some concern that even though
1252 * registers and PTEs are within the same BAR that they are potentially
1253 * of NUMA access patterns. Therefore, even with the way we assume
1254 * hardware should work, we must keep this posting read for paranoia.
1255 */
1256 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001257 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -07001258 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001259
1260 /* This next bit makes the above posting read even more important. We
1261 * want to flush the TLBs only after we're certain all the PTE updates
1262 * have finished.
1263 */
1264 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1265 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001266}
1267
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001268static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1269 unsigned int first_entry,
1270 unsigned int num_entries,
1271 bool use_scratch)
1272{
1273 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1274 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1275 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1276 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1277 int i;
1278
1279 if (WARN(num_entries > max_entries,
1280 "First entry = %d; Num entries = %d (max=%d)\n",
1281 first_entry, num_entries, max_entries))
1282 num_entries = max_entries;
1283
1284 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1285 I915_CACHE_LLC,
1286 use_scratch);
1287 for (i = 0; i < num_entries; i++)
1288 gen8_set_pte(&gtt_base[i], scratch_pte);
1289 readl(gtt_base);
1290}
1291
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001292static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001293 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -07001294 unsigned int num_entries,
1295 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001296{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001297 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001298 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1299 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001300 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001301 int i;
1302
1303 if (WARN(num_entries > max_entries,
1304 "First entry = %d; Num entries = %d (max=%d)\n",
1305 first_entry, num_entries, max_entries))
1306 num_entries = max_entries;
1307
Ben Widawsky828c7902013-10-16 09:21:30 -07001308 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
1309
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001310 for (i = 0; i < num_entries; i++)
1311 iowrite32(scratch_pte, &gtt_base[i]);
1312 readl(gtt_base);
1313}
1314
Ben Widawsky6f65e292013-12-06 14:10:56 -08001315
1316static void i915_ggtt_bind_vma(struct i915_vma *vma,
1317 enum i915_cache_level cache_level,
1318 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001319{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001320 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001321 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1322 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1323
Ben Widawsky6f65e292013-12-06 14:10:56 -08001324 BUG_ON(!i915_is_ggtt(vma->vm));
1325 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1326 vma->obj->has_global_gtt_mapping = 1;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001327}
1328
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001329static void i915_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001330 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -07001331 unsigned int num_entries,
1332 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001333{
1334 intel_gtt_clear_range(first_entry, num_entries);
1335}
1336
Ben Widawsky6f65e292013-12-06 14:10:56 -08001337static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001338{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001339 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1340 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001341
Ben Widawsky6f65e292013-12-06 14:10:56 -08001342 BUG_ON(!i915_is_ggtt(vma->vm));
1343 vma->obj->has_global_gtt_mapping = 0;
1344 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001345}
1346
Ben Widawsky6f65e292013-12-06 14:10:56 -08001347static void ggtt_bind_vma(struct i915_vma *vma,
1348 enum i915_cache_level cache_level,
1349 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001350{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001351 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001352 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001353 struct drm_i915_gem_object *obj = vma->obj;
1354 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001355
Ben Widawsky6f65e292013-12-06 14:10:56 -08001356 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1357 * or we have a global mapping already but the cacheability flags have
1358 * changed, set the global PTEs.
1359 *
1360 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1361 * instead if none of the above hold true.
1362 *
1363 * NB: A global mapping should only be needed for special regions like
1364 * "gtt mappable", SNB errata, or if specified via special execbuf
1365 * flags. At all other times, the GPU will use the aliasing PPGTT.
1366 */
1367 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1368 if (!obj->has_global_gtt_mapping ||
1369 (cache_level != obj->cache_level)) {
1370 vma->vm->insert_entries(vma->vm, obj->pages, entry,
1371 cache_level);
1372 obj->has_global_gtt_mapping = 1;
1373 }
1374 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001375
Ben Widawsky6f65e292013-12-06 14:10:56 -08001376 if (dev_priv->mm.aliasing_ppgtt &&
1377 (!obj->has_aliasing_ppgtt_mapping ||
1378 (cache_level != obj->cache_level))) {
1379 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1380 appgtt->base.insert_entries(&appgtt->base,
1381 vma->obj->pages, entry, cache_level);
1382 vma->obj->has_aliasing_ppgtt_mapping = 1;
1383 }
1384}
1385
1386static void ggtt_unbind_vma(struct i915_vma *vma)
1387{
1388 struct drm_device *dev = vma->vm->dev;
1389 struct drm_i915_private *dev_priv = dev->dev_private;
1390 struct drm_i915_gem_object *obj = vma->obj;
1391 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1392
1393 if (obj->has_global_gtt_mapping) {
1394 vma->vm->clear_range(vma->vm, entry,
1395 vma->obj->base.size >> PAGE_SHIFT,
1396 true);
1397 obj->has_global_gtt_mapping = 0;
1398 }
1399
1400 if (obj->has_aliasing_ppgtt_mapping) {
1401 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1402 appgtt->base.clear_range(&appgtt->base,
1403 entry,
1404 obj->base.size >> PAGE_SHIFT,
1405 true);
1406 obj->has_aliasing_ppgtt_mapping = 0;
1407 }
Daniel Vetter74163902012-02-15 23:50:21 +01001408}
1409
1410void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1411{
Ben Widawsky5c042282011-10-17 15:51:55 -07001412 struct drm_device *dev = obj->base.dev;
1413 struct drm_i915_private *dev_priv = dev->dev_private;
1414 bool interruptible;
1415
1416 interruptible = do_idling(dev_priv);
1417
Chris Wilson9da3da62012-06-01 15:20:22 +01001418 if (!obj->has_dma_mapping)
1419 dma_unmap_sg(&dev->pdev->dev,
1420 obj->pages->sgl, obj->pages->nents,
1421 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001422
1423 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001424}
Daniel Vetter644ec022012-03-26 09:45:40 +02001425
Chris Wilson42d6ab42012-07-26 11:49:32 +01001426static void i915_gtt_color_adjust(struct drm_mm_node *node,
1427 unsigned long color,
1428 unsigned long *start,
1429 unsigned long *end)
1430{
1431 if (node->color != color)
1432 *start += 4096;
1433
1434 if (!list_empty(&node->node_list)) {
1435 node = list_entry(node->node_list.next,
1436 struct drm_mm_node,
1437 node_list);
1438 if (node->allocated && node->color != color)
1439 *end -= 4096;
1440 }
1441}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001442
Ben Widawskyd7e50082012-12-18 10:31:25 -08001443void i915_gem_setup_global_gtt(struct drm_device *dev,
1444 unsigned long start,
1445 unsigned long mappable_end,
1446 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001447{
Ben Widawskye78891c2013-01-25 16:41:04 -08001448 /* Let GEM Manage all of the aperture.
1449 *
1450 * However, leave one page at the end still bound to the scratch page.
1451 * There are a number of places where the hardware apparently prefetches
1452 * past the end of the object, and we've seen multiple hangs with the
1453 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1454 * aperture. One page should be enough to keep any prefetching inside
1455 * of the aperture.
1456 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001457 struct drm_i915_private *dev_priv = dev->dev_private;
1458 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001459 struct drm_mm_node *entry;
1460 struct drm_i915_gem_object *obj;
1461 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +02001462
Ben Widawsky35451cb2013-01-17 12:45:13 -08001463 BUG_ON(mappable_end > end);
1464
Chris Wilsoned2f3452012-11-15 11:32:19 +00001465 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001466 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001467 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001468 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001469
Chris Wilsoned2f3452012-11-15 11:32:19 +00001470 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001471 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001472 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001473 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -07001474 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001475 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001476
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001477 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001478 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001479 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001480 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +00001481 obj->has_global_gtt_mapping = 1;
1482 }
1483
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001484 dev_priv->gtt.base.start = start;
1485 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001486
Chris Wilsoned2f3452012-11-15 11:32:19 +00001487 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001488 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001489 const unsigned long count = (hole_end - hole_start) / PAGE_SIZE;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001490 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1491 hole_start, hole_end);
Ben Widawsky828c7902013-10-16 09:21:30 -07001492 ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001493 }
1494
1495 /* And finally clear the reserved guard page */
Ben Widawsky828c7902013-10-16 09:21:30 -07001496 ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001497}
1498
Ben Widawskyd7e50082012-12-18 10:31:25 -08001499void i915_gem_init_global_gtt(struct drm_device *dev)
1500{
1501 struct drm_i915_private *dev_priv = dev->dev_private;
1502 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001503
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001504 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001505 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001506
Ben Widawskye78891c2013-01-25 16:41:04 -08001507 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001508}
1509
1510static int setup_scratch_page(struct drm_device *dev)
1511{
1512 struct drm_i915_private *dev_priv = dev->dev_private;
1513 struct page *page;
1514 dma_addr_t dma_addr;
1515
1516 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1517 if (page == NULL)
1518 return -ENOMEM;
1519 get_page(page);
1520 set_pages_uc(page, 1);
1521
1522#ifdef CONFIG_INTEL_IOMMU
1523 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1524 PCI_DMA_BIDIRECTIONAL);
1525 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1526 return -EINVAL;
1527#else
1528 dma_addr = page_to_phys(page);
1529#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001530 dev_priv->gtt.base.scratch.page = page;
1531 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001532
1533 return 0;
1534}
1535
1536static void teardown_scratch_page(struct drm_device *dev)
1537{
1538 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001539 struct page *page = dev_priv->gtt.base.scratch.page;
1540
1541 set_pages_wb(page, 1);
1542 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001543 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001544 put_page(page);
1545 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001546}
1547
1548static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1549{
1550 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1551 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1552 return snb_gmch_ctl << 20;
1553}
1554
Ben Widawsky9459d252013-11-03 16:53:55 -08001555static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1556{
1557 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1558 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1559 if (bdw_gmch_ctl)
1560 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky3a2ffb62013-11-07 21:40:51 -08001561 if (bdw_gmch_ctl > 4) {
Jani Nikulad330a952014-01-21 11:24:25 +02001562 WARN_ON(!i915.preliminary_hw_support);
Ben Widawsky3a2ffb62013-11-07 21:40:51 -08001563 return 4<<20;
1564 }
1565
Ben Widawsky9459d252013-11-03 16:53:55 -08001566 return bdw_gmch_ctl << 20;
1567}
1568
Ben Widawskybaa09f52013-01-24 13:49:57 -08001569static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001570{
1571 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1572 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1573 return snb_gmch_ctl << 25; /* 32 MB units */
1574}
1575
Ben Widawsky9459d252013-11-03 16:53:55 -08001576static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1577{
1578 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1579 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1580 return bdw_gmch_ctl << 25; /* 32 MB units */
1581}
1582
Ben Widawsky63340132013-11-04 19:32:22 -08001583static int ggtt_probe_common(struct drm_device *dev,
1584 size_t gtt_size)
1585{
1586 struct drm_i915_private *dev_priv = dev->dev_private;
1587 phys_addr_t gtt_bus_addr;
1588 int ret;
1589
1590 /* For Modern GENs the PTEs and register space are split in the BAR */
1591 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
1592 (pci_resource_len(dev->pdev, 0) / 2);
1593
1594 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
1595 if (!dev_priv->gtt.gsm) {
1596 DRM_ERROR("Failed to map the gtt page table\n");
1597 return -ENOMEM;
1598 }
1599
1600 ret = setup_scratch_page(dev);
1601 if (ret) {
1602 DRM_ERROR("Scratch setup failed\n");
1603 /* iounmap will also get called at remove, but meh */
1604 iounmap(dev_priv->gtt.gsm);
1605 }
1606
1607 return ret;
1608}
1609
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001610/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1611 * bits. When using advanced contexts each context stores its own PAT, but
1612 * writing this data shouldn't be harmful even in those cases. */
1613static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1614{
1615#define GEN8_PPAT_UC (0<<0)
1616#define GEN8_PPAT_WC (1<<0)
1617#define GEN8_PPAT_WT (2<<0)
1618#define GEN8_PPAT_WB (3<<0)
1619#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
1620/* FIXME(BDW): Bspec is completely confused about cache control bits. */
1621#define GEN8_PPAT_LLC (1<<2)
1622#define GEN8_PPAT_LLCELLC (2<<2)
1623#define GEN8_PPAT_LLCeLLC (3<<2)
1624#define GEN8_PPAT_AGE(x) (x<<4)
1625#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
1626 uint64_t pat;
1627
1628 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1629 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1630 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1631 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1632 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1633 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1634 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1635 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1636
1637 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1638 * write would work. */
1639 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1640 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1641}
1642
Ben Widawsky63340132013-11-04 19:32:22 -08001643static int gen8_gmch_probe(struct drm_device *dev,
1644 size_t *gtt_total,
1645 size_t *stolen,
1646 phys_addr_t *mappable_base,
1647 unsigned long *mappable_end)
1648{
1649 struct drm_i915_private *dev_priv = dev->dev_private;
1650 unsigned int gtt_size;
1651 u16 snb_gmch_ctl;
1652 int ret;
1653
1654 /* TODO: We're not aware of mappable constraints on gen8 yet */
1655 *mappable_base = pci_resource_start(dev->pdev, 2);
1656 *mappable_end = pci_resource_len(dev->pdev, 2);
1657
1658 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1659 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1660
1661 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1662
1663 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1664
1665 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskyd31eb102013-11-02 21:07:17 -07001666 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001667
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001668 gen8_setup_private_ppat(dev_priv);
1669
Ben Widawsky63340132013-11-04 19:32:22 -08001670 ret = ggtt_probe_common(dev, gtt_size);
1671
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001672 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1673 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001674
1675 return ret;
1676}
1677
Ben Widawskybaa09f52013-01-24 13:49:57 -08001678static int gen6_gmch_probe(struct drm_device *dev,
1679 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001680 size_t *stolen,
1681 phys_addr_t *mappable_base,
1682 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001683{
1684 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001685 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001686 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001687 int ret;
1688
Ben Widawsky41907dd2013-02-08 11:32:47 -08001689 *mappable_base = pci_resource_start(dev->pdev, 2);
1690 *mappable_end = pci_resource_len(dev->pdev, 2);
1691
Ben Widawskybaa09f52013-01-24 13:49:57 -08001692 /* 64/512MB is the current min/max we actually know of, but this is just
1693 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001694 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001695 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001696 DRM_ERROR("Unknown GMADR size (%lx)\n",
1697 dev_priv->gtt.mappable_end);
1698 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001699 }
1700
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001701 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1702 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001703 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001704
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07001705 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001706
Ben Widawsky63340132013-11-04 19:32:22 -08001707 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001708 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
1709
Ben Widawsky63340132013-11-04 19:32:22 -08001710 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001711
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001712 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1713 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001714
1715 return ret;
1716}
1717
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001718static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001719{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001720
1721 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08001722
1723 drm_mm_takedown(&vm->mm);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001724 iounmap(gtt->gsm);
1725 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001726}
1727
1728static int i915_gmch_probe(struct drm_device *dev,
1729 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001730 size_t *stolen,
1731 phys_addr_t *mappable_base,
1732 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001733{
1734 struct drm_i915_private *dev_priv = dev->dev_private;
1735 int ret;
1736
Ben Widawskybaa09f52013-01-24 13:49:57 -08001737 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1738 if (!ret) {
1739 DRM_ERROR("failed to set up gmch\n");
1740 return -EIO;
1741 }
1742
Ben Widawsky41907dd2013-02-08 11:32:47 -08001743 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001744
1745 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001746 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001747
Chris Wilsonc0a7f812013-12-30 12:16:15 +00001748 if (unlikely(dev_priv->gtt.do_idle_maps))
1749 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
1750
Ben Widawskybaa09f52013-01-24 13:49:57 -08001751 return 0;
1752}
1753
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001754static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001755{
1756 intel_gmch_remove();
1757}
1758
1759int i915_gem_gtt_init(struct drm_device *dev)
1760{
1761 struct drm_i915_private *dev_priv = dev->dev_private;
1762 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001763 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001764
Ben Widawskybaa09f52013-01-24 13:49:57 -08001765 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001766 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001767 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08001768 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001769 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001770 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001771 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001772 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001773 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001774 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001775 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001776 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001777 else if (INTEL_INFO(dev)->gen >= 7)
1778 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001779 else
Chris Wilson350ec882013-08-06 13:17:02 +01001780 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08001781 } else {
1782 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1783 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001784 }
1785
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001786 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001787 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001788 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001789 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001790
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001791 gtt->base.dev = dev;
1792
Ben Widawskybaa09f52013-01-24 13:49:57 -08001793 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001794 DRM_INFO("Memory usable by graphics device = %zdM\n",
1795 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001796 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1797 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001798
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001799 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001800}
Ben Widawsky6f65e292013-12-06 14:10:56 -08001801
1802static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
1803 struct i915_address_space *vm)
1804{
1805 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
1806 if (vma == NULL)
1807 return ERR_PTR(-ENOMEM);
1808
1809 INIT_LIST_HEAD(&vma->vma_link);
1810 INIT_LIST_HEAD(&vma->mm_list);
1811 INIT_LIST_HEAD(&vma->exec_list);
1812 vma->vm = vm;
1813 vma->obj = obj;
1814
1815 switch (INTEL_INFO(vm->dev)->gen) {
1816 case 8:
1817 case 7:
1818 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001819 if (i915_is_ggtt(vm)) {
1820 vma->unbind_vma = ggtt_unbind_vma;
1821 vma->bind_vma = ggtt_bind_vma;
1822 } else {
1823 vma->unbind_vma = ppgtt_unbind_vma;
1824 vma->bind_vma = ppgtt_bind_vma;
1825 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08001826 break;
1827 case 5:
1828 case 4:
1829 case 3:
1830 case 2:
1831 BUG_ON(!i915_is_ggtt(vm));
1832 vma->unbind_vma = i915_ggtt_unbind_vma;
1833 vma->bind_vma = i915_ggtt_bind_vma;
1834 break;
1835 default:
1836 BUG();
1837 }
1838
1839 /* Keep GGTT vmas first to make debug easier */
1840 if (i915_is_ggtt(vm))
1841 list_add(&vma->vma_link, &obj->vma_list);
1842 else
1843 list_add_tail(&vma->vma_link, &obj->vma_list);
1844
1845 return vma;
1846}
1847
1848struct i915_vma *
1849i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
1850 struct i915_address_space *vm)
1851{
1852 struct i915_vma *vma;
1853
1854 vma = i915_gem_obj_to_vma(obj, vm);
1855 if (!vma)
1856 vma = __i915_gem_vma_create(obj, vm);
1857
1858 return vma;
1859}