blob: beca57154d6f3e266e4d234fab49b545cb4ec134 [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{
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800335 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800336 int i, j;
337
338 for (i = 0; i < ppgtt->num_pd_pages; i++) {
339 /* TODO: In the future we'll support sparse mappings, so this
340 * will have to change. */
341 if (!ppgtt->pd_dma_addr[i])
342 continue;
343
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800344 pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
345 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800346
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)
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800350 pci_unmap_page(hwdev, addr, PAGE_SIZE,
351 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800352 }
353 }
354}
355
Ben Widawsky37aca442013-11-04 20:47:32 -0800356static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
357{
358 struct i915_hw_ppgtt *ppgtt =
359 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800360
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800361 list_del(&vm->global_link);
Ben Widawsky686e1f6f2013-11-25 09:54:34 -0800362 drm_mm_takedown(&vm->mm);
363
Ben Widawskyb45a6712014-02-12 14:28:44 -0800364 gen8_ppgtt_unmap_pages(ppgtt);
365 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800366}
367
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800368static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
369 const int max_pdp)
370{
371 struct page *pt_pages;
372 const int num_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
373
374 pt_pages = alloc_pages(GFP_KERNEL, get_order(num_pt_pages << PAGE_SHIFT));
375 if (!pt_pages)
376 return -ENOMEM;
377
378 ppgtt->gen8_pt_pages = pt_pages;
379 ppgtt->num_pt_pages = 1 << get_order(num_pt_pages << PAGE_SHIFT);
380
381 return 0;
382}
383
384static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
385{
386 int i;
387
388 for (i = 0; i < ppgtt->num_pd_pages; i++) {
389 ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
390 sizeof(dma_addr_t),
391 GFP_KERNEL);
392 if (!ppgtt->gen8_pt_dma_addr[i])
393 return -ENOMEM;
394 }
395
396 return 0;
397}
398
399static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
400 const int max_pdp)
401{
402 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
403 if (!ppgtt->pd_pages)
404 return -ENOMEM;
405
406 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
407 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
408
409 return 0;
410}
411
412static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
413 const int max_pdp)
414{
415 int ret;
416
417 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
418 if (ret)
419 return ret;
420
421 ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
422 if (ret) {
423 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
424 return ret;
425 }
426
427 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
428
429 ret = gen8_ppgtt_allocate_dma(ppgtt);
430 if (ret)
431 gen8_ppgtt_free(ppgtt);
432
433 return ret;
434}
435
436static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
437 const int pd)
438{
439 dma_addr_t pd_addr;
440 int ret;
441
442 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
443 &ppgtt->pd_pages[pd], 0,
444 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
445
446 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
447 if (ret)
448 return ret;
449
450 ppgtt->pd_dma_addr[pd] = pd_addr;
451
452 return 0;
453}
454
455static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
456 const int pd,
457 const int pt)
458{
459 dma_addr_t pt_addr;
460 struct page *p;
461 int ret;
462
463 p = &ppgtt->gen8_pt_pages[pd * GEN8_PDES_PER_PAGE + pt];
464 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
465 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
466 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
467 if (ret)
468 return ret;
469
470 ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
471
472 return 0;
473}
474
Ben Widawsky37aca442013-11-04 20:47:32 -0800475/**
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800476 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
477 * with a net effect resembling a 2-level page table in normal x86 terms. Each
478 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
479 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800480 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800481 * FIXME: split allocation into smaller pieces. For now we only ever do this
482 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
Ben Widawsky37aca442013-11-04 20:47:32 -0800483 * TODO: Do something with the size parameter
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800484 */
Ben Widawsky37aca442013-11-04 20:47:32 -0800485static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
486{
Ben Widawsky37aca442013-11-04 20:47:32 -0800487 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800488 const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800489 int i, j, ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800490
491 if (size % (1<<30))
492 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
493
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800494 /* 1. Do all our allocations for page directories and page tables. */
495 ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
496 if (ret)
497 return ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800498
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800499 /*
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800500 * 2. Create DMA mappings for the page directories and page tables.
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800501 */
502 for (i = 0; i < max_pdp; i++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800503 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800504 if (ret)
505 goto bail;
506
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800507 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800508 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800509 if (ret)
510 goto bail;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800511 }
512 }
513
514 /*
515 * 3. Map all the page directory entires to point to the page tables
516 * we've allocated.
517 *
518 * For now, the PPGTT helper functions all require that the PDEs are
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800519 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800520 * will never need to touch the PDEs again.
521 */
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800522 for (i = 0; i < max_pdp; i++) {
523 gen8_ppgtt_pde_t *pd_vaddr;
524 pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
525 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
526 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
527 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
528 I915_CACHE_LLC);
529 }
530 kunmap_atomic(pd_vaddr);
531 }
532
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800533 ppgtt->enable = gen8_ppgtt_enable;
534 ppgtt->switch_mm = gen8_mm_switch;
535 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
536 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
537 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
538 ppgtt->base.start = 0;
539 ppgtt->base.total = ppgtt->num_pt_pages * GEN8_PTES_PER_PAGE * PAGE_SIZE;
540
Ben Widawsky459108b2013-11-02 21:07:23 -0700541 ppgtt->base.clear_range(&ppgtt->base, 0,
542 ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE,
543 true);
544
Ben Widawsky37aca442013-11-04 20:47:32 -0800545 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
546 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
547 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
548 ppgtt->num_pt_pages,
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800549 (ppgtt->num_pt_pages - min_pt_pages) +
Ben Widawsky37aca442013-11-04 20:47:32 -0800550 size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700551 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800552
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800553bail:
554 gen8_ppgtt_unmap_pages(ppgtt);
555 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800556 return ret;
557}
558
Ben Widawsky87d60b62013-12-06 14:11:29 -0800559static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
560{
561 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
562 struct i915_address_space *vm = &ppgtt->base;
563 gen6_gtt_pte_t __iomem *pd_addr;
564 gen6_gtt_pte_t scratch_pte;
565 uint32_t pd_entry;
566 int pte, pde;
567
568 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
569
570 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
571 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
572
573 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
574 ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
575 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
576 u32 expected;
577 gen6_gtt_pte_t *pt_vaddr;
578 dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
579 pd_entry = readl(pd_addr + pde);
580 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
581
582 if (pd_entry != expected)
583 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
584 pde,
585 pd_entry,
586 expected);
587 seq_printf(m, "\tPDE: %x\n", pd_entry);
588
589 pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
590 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
591 unsigned long va =
592 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
593 (pte * PAGE_SIZE);
594 int i;
595 bool found = false;
596 for (i = 0; i < 4; i++)
597 if (pt_vaddr[pte + i] != scratch_pte)
598 found = true;
599 if (!found)
600 continue;
601
602 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
603 for (i = 0; i < 4; i++) {
604 if (pt_vaddr[pte + i] != scratch_pte)
605 seq_printf(m, " %08x", pt_vaddr[pte + i]);
606 else
607 seq_puts(m, " SCRATCH ");
608 }
609 seq_puts(m, "\n");
610 }
611 kunmap_atomic(pt_vaddr);
612 }
613}
614
Ben Widawsky3e302542013-04-23 23:15:32 -0700615static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700616{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700617 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700618 gen6_gtt_pte_t __iomem *pd_addr;
619 uint32_t pd_entry;
620 int i;
621
Ben Widawsky0a732872013-04-23 23:15:30 -0700622 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700623 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
624 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
625 for (i = 0; i < ppgtt->num_pd_entries; i++) {
626 dma_addr_t pt_addr;
627
628 pt_addr = ppgtt->pt_dma_addr[i];
629 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
630 pd_entry |= GEN6_PDE_VALID;
631
632 writel(pd_entry, pd_addr + i);
633 }
634 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700635}
636
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800637static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700638{
Ben Widawsky3e302542013-04-23 23:15:32 -0700639 BUG_ON(ppgtt->pd_offset & 0x3f);
640
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800641 return (ppgtt->pd_offset / 64) << 16;
642}
Ben Widawsky61973492013-04-08 18:43:54 -0700643
Ben Widawsky90252e52013-12-06 14:11:12 -0800644static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
645 struct intel_ring_buffer *ring,
646 bool synchronous)
647{
648 struct drm_device *dev = ppgtt->base.dev;
649 struct drm_i915_private *dev_priv = dev->dev_private;
650 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700651
Ben Widawsky90252e52013-12-06 14:11:12 -0800652 /* If we're in reset, we can assume the GPU is sufficiently idle to
653 * manually frob these bits. Ideally we could use the ring functions,
654 * except our error handling makes it quite difficult (can't use
655 * intel_ring_begin, ring->flush, or intel_ring_advance)
656 *
657 * FIXME: We should try not to special case reset
658 */
659 if (synchronous ||
660 i915_reset_in_progress(&dev_priv->gpu_error)) {
661 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
662 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
663 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
664 POSTING_READ(RING_PP_DIR_BASE(ring));
665 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700666 }
667
Ben Widawsky90252e52013-12-06 14:11:12 -0800668 /* NB: TLBs must be flushed and invalidated before a switch */
669 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
670 if (ret)
671 return ret;
672
673 ret = intel_ring_begin(ring, 6);
674 if (ret)
675 return ret;
676
677 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
678 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
679 intel_ring_emit(ring, PP_DIR_DCLV_2G);
680 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
681 intel_ring_emit(ring, get_pd_offset(ppgtt));
682 intel_ring_emit(ring, MI_NOOP);
683 intel_ring_advance(ring);
684
685 return 0;
686}
687
Ben Widawsky48a10382013-12-06 14:11:11 -0800688static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
689 struct intel_ring_buffer *ring,
690 bool synchronous)
691{
692 struct drm_device *dev = ppgtt->base.dev;
693 struct drm_i915_private *dev_priv = dev->dev_private;
694 int ret;
695
696 /* If we're in reset, we can assume the GPU is sufficiently idle to
697 * manually frob these bits. Ideally we could use the ring functions,
698 * except our error handling makes it quite difficult (can't use
699 * intel_ring_begin, ring->flush, or intel_ring_advance)
700 *
701 * FIXME: We should try not to special case reset
702 */
703 if (synchronous ||
704 i915_reset_in_progress(&dev_priv->gpu_error)) {
705 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
706 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
707 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
708 POSTING_READ(RING_PP_DIR_BASE(ring));
709 return 0;
710 }
711
712 /* NB: TLBs must be flushed and invalidated before a switch */
713 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
714 if (ret)
715 return ret;
716
717 ret = intel_ring_begin(ring, 6);
718 if (ret)
719 return ret;
720
721 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
722 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
723 intel_ring_emit(ring, PP_DIR_DCLV_2G);
724 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
725 intel_ring_emit(ring, get_pd_offset(ppgtt));
726 intel_ring_emit(ring, MI_NOOP);
727 intel_ring_advance(ring);
728
Ben Widawsky90252e52013-12-06 14:11:12 -0800729 /* XXX: RCS is the only one to auto invalidate the TLBs? */
730 if (ring->id != RCS) {
731 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
732 if (ret)
733 return ret;
734 }
735
Ben Widawsky48a10382013-12-06 14:11:11 -0800736 return 0;
737}
738
Ben Widawskyeeb94882013-12-06 14:11:10 -0800739static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
740 struct intel_ring_buffer *ring,
741 bool synchronous)
742{
743 struct drm_device *dev = ppgtt->base.dev;
744 struct drm_i915_private *dev_priv = dev->dev_private;
745
Ben Widawsky48a10382013-12-06 14:11:11 -0800746 if (!synchronous)
747 return 0;
748
Ben Widawskyeeb94882013-12-06 14:11:10 -0800749 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
750 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
751
752 POSTING_READ(RING_PP_DIR_DCLV(ring));
753
754 return 0;
755}
756
757static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
758{
759 struct drm_device *dev = ppgtt->base.dev;
760 struct drm_i915_private *dev_priv = dev->dev_private;
761 struct intel_ring_buffer *ring;
762 int j, ret;
763
764 for_each_ring(ring, dev_priv, j) {
765 I915_WRITE(RING_MODE_GEN7(ring),
766 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800767
768 /* We promise to do a switch later with FULL PPGTT. If this is
769 * aliasing, this is the one and only switch we'll do */
770 if (USES_FULL_PPGTT(dev))
771 continue;
772
Ben Widawskyeeb94882013-12-06 14:11:10 -0800773 ret = ppgtt->switch_mm(ppgtt, ring, true);
774 if (ret)
775 goto err_out;
776 }
777
778 return 0;
779
780err_out:
781 for_each_ring(ring, dev_priv, j)
782 I915_WRITE(RING_MODE_GEN7(ring),
783 _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
784 return ret;
785}
786
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800787static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
788{
789 struct drm_device *dev = ppgtt->base.dev;
790 drm_i915_private_t *dev_priv = dev->dev_private;
791 struct intel_ring_buffer *ring;
792 uint32_t ecochk, ecobits;
793 int i;
794
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800795 ecobits = I915_READ(GAC_ECO_BITS);
796 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
797
798 ecochk = I915_READ(GAM_ECOCHK);
799 if (IS_HASWELL(dev)) {
800 ecochk |= ECOCHK_PPGTT_WB_HSW;
801 } else {
802 ecochk |= ECOCHK_PPGTT_LLC_IVB;
803 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
804 }
805 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800806
Ben Widawsky61973492013-04-08 18:43:54 -0700807 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800808 int ret;
809 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800810 I915_WRITE(RING_MODE_GEN7(ring),
811 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700812
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800813 /* We promise to do a switch later with FULL PPGTT. If this is
814 * aliasing, this is the one and only switch we'll do */
815 if (USES_FULL_PPGTT(dev))
816 continue;
817
Ben Widawskyeeb94882013-12-06 14:11:10 -0800818 ret = ppgtt->switch_mm(ppgtt, ring, true);
819 if (ret)
820 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700821 }
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800822
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800823 return 0;
824}
825
Ben Widawskya3d67d22013-12-06 14:11:06 -0800826static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700827{
Ben Widawskya3d67d22013-12-06 14:11:06 -0800828 struct drm_device *dev = ppgtt->base.dev;
Ben Widawsky61973492013-04-08 18:43:54 -0700829 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700830 struct intel_ring_buffer *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800831 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -0700832 int i;
833
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800834 ecobits = I915_READ(GAC_ECO_BITS);
835 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
836 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700837
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800838 gab_ctl = I915_READ(GAB_CTL);
839 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700840
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800841 ecochk = I915_READ(GAM_ECOCHK);
842 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700843
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800844 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700845
846 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800847 int ret = ppgtt->switch_mm(ppgtt, ring, true);
848 if (ret)
849 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700850 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800851
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700852 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700853}
854
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100855/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700856static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100857 unsigned first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700858 unsigned num_entries,
859 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100860{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700861 struct i915_hw_ppgtt *ppgtt =
862 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700863 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Daniel Vettera15326a2013-03-19 23:48:39 +0100864 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100865 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
866 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100867
Ben Widawskyb35b3802013-10-16 09:18:21 -0700868 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100869
Daniel Vetter7bddb012012-02-09 17:15:47 +0100870 while (num_entries) {
871 last_pte = first_pte + num_entries;
872 if (last_pte > I915_PPGTT_PT_ENTRIES)
873 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100874
Daniel Vettera15326a2013-03-19 23:48:39 +0100875 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100876
877 for (i = first_pte; i < last_pte; i++)
878 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100879
880 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100881
Daniel Vetter7bddb012012-02-09 17:15:47 +0100882 num_entries -= last_pte - first_pte;
883 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100884 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100885 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100886}
887
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700888static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800889 struct sg_table *pages,
890 unsigned first_entry,
891 enum i915_cache_level cache_level)
892{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700893 struct i915_hw_ppgtt *ppgtt =
894 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700895 gen6_gtt_pte_t *pt_vaddr;
Daniel Vettera15326a2013-03-19 23:48:39 +0100896 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200897 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
898 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800899
Chris Wilsoncc797142013-12-31 15:50:30 +0000900 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +0200901 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +0000902 if (pt_vaddr == NULL)
903 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800904
Chris Wilsoncc797142013-12-31 15:50:30 +0000905 pt_vaddr[act_pte] =
906 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
907 cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200908 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
909 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +0000910 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +0100911 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +0200912 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800913 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800914 }
Chris Wilsoncc797142013-12-31 15:50:30 +0000915 if (pt_vaddr)
916 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800917}
918
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700919static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100920{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700921 struct i915_hw_ppgtt *ppgtt =
922 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -0800923 int i;
924
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800925 list_del(&vm->global_link);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700926 drm_mm_takedown(&ppgtt->base.mm);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800927 drm_mm_remove_node(&ppgtt->node);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700928
Daniel Vetter3440d262013-01-24 13:49:56 -0800929 if (ppgtt->pt_dma_addr) {
930 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700931 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800932 ppgtt->pt_dma_addr[i],
933 4096, PCI_DMA_BIDIRECTIONAL);
934 }
935
936 kfree(ppgtt->pt_dma_addr);
937 for (i = 0; i < ppgtt->num_pd_entries; i++)
938 __free_page(ppgtt->pt_pages[i]);
939 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800940}
941
942static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
943{
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800944#define GEN6_PD_ALIGN (PAGE_SIZE * 16)
945#define GEN6_PD_SIZE (GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700946 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100947 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -0800948 bool retried = false;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800949 int i, ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100950
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800951 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
952 * allocator works in address space sizes, so it's multiplied by page
953 * size. We allocate at the top of the GTT to avoid fragmentation.
954 */
955 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -0800956alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800957 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
958 &ppgtt->node, GEN6_PD_SIZE,
959 GEN6_PD_ALIGN, 0,
960 0, dev_priv->gtt.base.total,
961 DRM_MM_SEARCH_DEFAULT);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800962 if (ret == -ENOSPC && !retried) {
963 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
964 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Daniel Vetterd47c3ea2014-02-14 14:01:18 +0100965 I915_CACHE_NONE, 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800966 if (ret)
967 return ret;
968
969 retried = true;
970 goto alloc;
971 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800972
973 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
974 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100975
Chris Wilson08c45262013-07-30 19:04:37 +0100976 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700977 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky48a10382013-12-06 14:11:11 -0800978 if (IS_GEN6(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800979 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -0800980 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -0800981 } else if (IS_HASWELL(dev)) {
982 ppgtt->enable = gen7_ppgtt_enable;
983 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -0800984 } else if (IS_GEN7(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800985 ppgtt->enable = gen7_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -0800986 ppgtt->switch_mm = gen7_mm_switch;
987 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800988 BUG();
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700989 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
990 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
991 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
992 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Ben Widawsky686e1f6f2013-11-25 09:54:34 -0800993 ppgtt->base.start = 0;
994 ppgtt->base.total = GEN6_PPGTT_PD_ENTRIES * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Daniel Vettera1e22652013-09-21 00:35:38 +0200995 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100996 GFP_KERNEL);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800997 if (!ppgtt->pt_pages) {
998 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -0800999 return -ENOMEM;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001000 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001001
1002 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1003 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
1004 if (!ppgtt->pt_pages[i])
1005 goto err_pt_alloc;
1006 }
1007
Daniel Vettera1e22652013-09-21 00:35:38 +02001008 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001009 GFP_KERNEL);
1010 if (!ppgtt->pt_dma_addr)
1011 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001012
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001013 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1014 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +02001015
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001016 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
1017 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001018
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001019 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1020 ret = -EIO;
1021 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001022
Daniel Vetter211c5682012-04-10 17:29:17 +02001023 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001024 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001025 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001026
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001027 ppgtt->base.clear_range(&ppgtt->base, 0,
Ben Widawsky828c7902013-10-16 09:21:30 -07001028 ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001029 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001030
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001031 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1032 ppgtt->node.size >> 20,
1033 ppgtt->node.start / PAGE_SIZE);
1034 ppgtt->pd_offset =
1035 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001036
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001037 return 0;
1038
1039err_pd_pin:
1040 if (ppgtt->pt_dma_addr) {
1041 for (i--; i >= 0; i--)
1042 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
1043 4096, PCI_DMA_BIDIRECTIONAL);
1044 }
1045err_pt_alloc:
1046 kfree(ppgtt->pt_dma_addr);
1047 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1048 if (ppgtt->pt_pages[i])
1049 __free_page(ppgtt->pt_pages[i]);
1050 }
1051 kfree(ppgtt->pt_pages);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001052 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -08001053
1054 return ret;
1055}
1056
Ben Widawsky246cbfb2013-12-06 14:11:14 -08001057int i915_gem_init_ppgtt(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001058{
1059 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd6660ad2013-12-06 14:11:13 -08001060 int ret = 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001061
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001062 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08001063
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001064 if (INTEL_INFO(dev)->gen < 8)
1065 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -07001066 else if (IS_GEN8(dev))
Ben Widawsky37aca442013-11-04 20:47:32 -08001067 ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001068 else
1069 BUG();
1070
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001071 if (!ret) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001072 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001073 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001074 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1075 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001076 i915_init_vm(dev_priv, &ppgtt->base);
1077 if (INTEL_INFO(dev)->gen < 8) {
Ben Widawsky9f273d42013-12-06 14:11:16 -08001078 gen6_write_pdes(ppgtt);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001079 DRM_DEBUG("Adding PPGTT at offset %x\n",
1080 ppgtt->pd_offset << 10);
1081 }
Ben Widawsky93bd8642013-07-16 16:50:06 -07001082 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001083
1084 return ret;
1085}
1086
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001087static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001088ppgtt_bind_vma(struct i915_vma *vma,
1089 enum i915_cache_level cache_level,
1090 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001091{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001092 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001093
Ben Widawsky6f65e292013-12-06 14:10:56 -08001094 WARN_ON(flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001095
Ben Widawsky6f65e292013-12-06 14:10:56 -08001096 vma->vm->insert_entries(vma->vm, vma->obj->pages, entry, cache_level);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001097}
1098
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001099static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001100{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001101 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001102
Ben Widawsky6f65e292013-12-06 14:10:56 -08001103 vma->vm->clear_range(vma->vm,
1104 entry,
1105 vma->obj->base.size >> PAGE_SHIFT,
1106 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001107}
1108
Ben Widawskya81cc002013-01-18 12:30:31 -08001109extern int intel_iommu_gfx_mapped;
1110/* Certain Gen5 chipsets require require idling the GPU before
1111 * unmapping anything from the GTT when VT-d is enabled.
1112 */
1113static inline bool needs_idle_maps(struct drm_device *dev)
1114{
1115#ifdef CONFIG_INTEL_IOMMU
1116 /* Query intel_iommu to see if we need the workaround. Presumably that
1117 * was loaded first.
1118 */
1119 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1120 return true;
1121#endif
1122 return false;
1123}
1124
Ben Widawsky5c042282011-10-17 15:51:55 -07001125static bool do_idling(struct drm_i915_private *dev_priv)
1126{
1127 bool ret = dev_priv->mm.interruptible;
1128
Ben Widawskya81cc002013-01-18 12:30:31 -08001129 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001130 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001131 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001132 DRM_ERROR("Couldn't idle GPU\n");
1133 /* Wait a bit, in hopes it avoids the hang */
1134 udelay(10);
1135 }
1136 }
1137
1138 return ret;
1139}
1140
1141static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1142{
Ben Widawskya81cc002013-01-18 12:30:31 -08001143 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001144 dev_priv->mm.interruptible = interruptible;
1145}
1146
Ben Widawsky828c7902013-10-16 09:21:30 -07001147void i915_check_and_clear_faults(struct drm_device *dev)
1148{
1149 struct drm_i915_private *dev_priv = dev->dev_private;
1150 struct intel_ring_buffer *ring;
1151 int i;
1152
1153 if (INTEL_INFO(dev)->gen < 6)
1154 return;
1155
1156 for_each_ring(ring, dev_priv, i) {
1157 u32 fault_reg;
1158 fault_reg = I915_READ(RING_FAULT_REG(ring));
1159 if (fault_reg & RING_FAULT_VALID) {
1160 DRM_DEBUG_DRIVER("Unexpected fault\n"
1161 "\tAddr: 0x%08lx\\n"
1162 "\tAddress space: %s\n"
1163 "\tSource ID: %d\n"
1164 "\tType: %d\n",
1165 fault_reg & PAGE_MASK,
1166 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1167 RING_FAULT_SRCID(fault_reg),
1168 RING_FAULT_FAULT_TYPE(fault_reg));
1169 I915_WRITE(RING_FAULT_REG(ring),
1170 fault_reg & ~RING_FAULT_VALID);
1171 }
1172 }
1173 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1174}
1175
1176void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1177{
1178 struct drm_i915_private *dev_priv = dev->dev_private;
1179
1180 /* Don't bother messing with faults pre GEN6 as we have little
1181 * documentation supporting that it's a good idea.
1182 */
1183 if (INTEL_INFO(dev)->gen < 6)
1184 return;
1185
1186 i915_check_and_clear_faults(dev);
1187
1188 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1189 dev_priv->gtt.base.start / PAGE_SIZE,
1190 dev_priv->gtt.base.total / PAGE_SIZE,
1191 false);
1192}
1193
Daniel Vetter76aaf222010-11-05 22:23:30 +01001194void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1195{
1196 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001197 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001198 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001199
Ben Widawsky828c7902013-10-16 09:21:30 -07001200 i915_check_and_clear_faults(dev);
1201
Chris Wilsonbee4a182011-01-21 10:54:32 +00001202 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001203 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1204 dev_priv->gtt.base.start / PAGE_SIZE,
Ben Widawsky828c7902013-10-16 09:21:30 -07001205 dev_priv->gtt.base.total / PAGE_SIZE,
1206 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001207
Ben Widawsky35c20a62013-05-31 11:28:48 -07001208 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001209 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1210 &dev_priv->gtt.base);
1211 if (!vma)
1212 continue;
1213
Chris Wilson2c225692013-08-09 12:26:45 +01001214 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001215 /* The bind_vma code tries to be smart about tracking mappings.
1216 * Unfortunately above, we've just wiped out the mappings
1217 * without telling our object about it. So we need to fake it.
1218 */
1219 obj->has_global_gtt_mapping = 0;
1220 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001221 }
1222
Ben Widawsky80da2162013-12-06 14:11:17 -08001223
1224 if (INTEL_INFO(dev)->gen >= 8)
1225 return;
1226
1227 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1228 /* TODO: Perhaps it shouldn't be gen6 specific */
1229 if (i915_is_ggtt(vm)) {
1230 if (dev_priv->mm.aliasing_ppgtt)
1231 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1232 continue;
1233 }
1234
1235 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001236 }
1237
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001238 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001239}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001240
Daniel Vetter74163902012-02-15 23:50:21 +01001241int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001242{
Chris Wilson9da3da62012-06-01 15:20:22 +01001243 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001244 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001245
1246 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1247 obj->pages->sgl, obj->pages->nents,
1248 PCI_DMA_BIDIRECTIONAL))
1249 return -ENOSPC;
1250
1251 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001252}
1253
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001254static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1255{
1256#ifdef writeq
1257 writeq(pte, addr);
1258#else
1259 iowrite32((u32)pte, addr);
1260 iowrite32(pte >> 32, addr + 4);
1261#endif
1262}
1263
1264static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1265 struct sg_table *st,
1266 unsigned int first_entry,
1267 enum i915_cache_level level)
1268{
1269 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1270 gen8_gtt_pte_t __iomem *gtt_entries =
1271 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1272 int i = 0;
1273 struct sg_page_iter sg_iter;
1274 dma_addr_t addr;
1275
1276 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1277 addr = sg_dma_address(sg_iter.sg) +
1278 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1279 gen8_set_pte(&gtt_entries[i],
1280 gen8_pte_encode(addr, level, true));
1281 i++;
1282 }
1283
1284 /*
1285 * XXX: This serves as a posting read to make sure that the PTE has
1286 * actually been updated. There is some concern that even though
1287 * registers and PTEs are within the same BAR that they are potentially
1288 * of NUMA access patterns. Therefore, even with the way we assume
1289 * hardware should work, we must keep this posting read for paranoia.
1290 */
1291 if (i != 0)
1292 WARN_ON(readq(&gtt_entries[i-1])
1293 != gen8_pte_encode(addr, level, true));
1294
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001295 /* This next bit makes the above posting read even more important. We
1296 * want to flush the TLBs only after we're certain all the PTE updates
1297 * have finished.
1298 */
1299 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1300 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001301}
1302
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001303/*
1304 * Binds an object into the global gtt with the specified cache level. The object
1305 * will be accessible to the GPU via commands whose operands reference offsets
1306 * within the global GTT as well as accessible by the GPU through the GMADR
1307 * mapped BAR (dev_priv->mm.gtt->gtt).
1308 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001309static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001310 struct sg_table *st,
1311 unsigned int first_entry,
1312 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001313{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001314 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001315 gen6_gtt_pte_t __iomem *gtt_entries =
1316 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001317 int i = 0;
1318 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001319 dma_addr_t addr;
1320
Imre Deak6e995e22013-02-18 19:28:04 +02001321 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001322 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -07001323 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001324 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001325 }
1326
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001327 /* XXX: This serves as a posting read to make sure that the PTE has
1328 * actually been updated. There is some concern that even though
1329 * registers and PTEs are within the same BAR that they are potentially
1330 * of NUMA access patterns. Therefore, even with the way we assume
1331 * hardware should work, we must keep this posting read for paranoia.
1332 */
1333 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001334 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -07001335 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001336
1337 /* This next bit makes the above posting read even more important. We
1338 * want to flush the TLBs only after we're certain all the PTE updates
1339 * have finished.
1340 */
1341 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1342 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001343}
1344
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001345static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1346 unsigned int first_entry,
1347 unsigned int num_entries,
1348 bool use_scratch)
1349{
1350 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1351 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1352 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1353 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1354 int i;
1355
1356 if (WARN(num_entries > max_entries,
1357 "First entry = %d; Num entries = %d (max=%d)\n",
1358 first_entry, num_entries, max_entries))
1359 num_entries = max_entries;
1360
1361 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1362 I915_CACHE_LLC,
1363 use_scratch);
1364 for (i = 0; i < num_entries; i++)
1365 gen8_set_pte(&gtt_base[i], scratch_pte);
1366 readl(gtt_base);
1367}
1368
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001369static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001370 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -07001371 unsigned int num_entries,
1372 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001373{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001374 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001375 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1376 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001377 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001378 int i;
1379
1380 if (WARN(num_entries > max_entries,
1381 "First entry = %d; Num entries = %d (max=%d)\n",
1382 first_entry, num_entries, max_entries))
1383 num_entries = max_entries;
1384
Ben Widawsky828c7902013-10-16 09:21:30 -07001385 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
1386
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001387 for (i = 0; i < num_entries; i++)
1388 iowrite32(scratch_pte, &gtt_base[i]);
1389 readl(gtt_base);
1390}
1391
Ben Widawsky6f65e292013-12-06 14:10:56 -08001392
1393static void i915_ggtt_bind_vma(struct i915_vma *vma,
1394 enum i915_cache_level cache_level,
1395 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001396{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001397 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001398 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1399 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1400
Ben Widawsky6f65e292013-12-06 14:10:56 -08001401 BUG_ON(!i915_is_ggtt(vma->vm));
1402 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1403 vma->obj->has_global_gtt_mapping = 1;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001404}
1405
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001406static void i915_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001407 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -07001408 unsigned int num_entries,
1409 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001410{
1411 intel_gtt_clear_range(first_entry, num_entries);
1412}
1413
Ben Widawsky6f65e292013-12-06 14:10:56 -08001414static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001415{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001416 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1417 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001418
Ben Widawsky6f65e292013-12-06 14:10:56 -08001419 BUG_ON(!i915_is_ggtt(vma->vm));
1420 vma->obj->has_global_gtt_mapping = 0;
1421 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001422}
1423
Ben Widawsky6f65e292013-12-06 14:10:56 -08001424static void ggtt_bind_vma(struct i915_vma *vma,
1425 enum i915_cache_level cache_level,
1426 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001427{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001428 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001429 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001430 struct drm_i915_gem_object *obj = vma->obj;
1431 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001432
Ben Widawsky6f65e292013-12-06 14:10:56 -08001433 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1434 * or we have a global mapping already but the cacheability flags have
1435 * changed, set the global PTEs.
1436 *
1437 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1438 * instead if none of the above hold true.
1439 *
1440 * NB: A global mapping should only be needed for special regions like
1441 * "gtt mappable", SNB errata, or if specified via special execbuf
1442 * flags. At all other times, the GPU will use the aliasing PPGTT.
1443 */
1444 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1445 if (!obj->has_global_gtt_mapping ||
1446 (cache_level != obj->cache_level)) {
1447 vma->vm->insert_entries(vma->vm, obj->pages, entry,
1448 cache_level);
1449 obj->has_global_gtt_mapping = 1;
1450 }
1451 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001452
Ben Widawsky6f65e292013-12-06 14:10:56 -08001453 if (dev_priv->mm.aliasing_ppgtt &&
1454 (!obj->has_aliasing_ppgtt_mapping ||
1455 (cache_level != obj->cache_level))) {
1456 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1457 appgtt->base.insert_entries(&appgtt->base,
1458 vma->obj->pages, entry, cache_level);
1459 vma->obj->has_aliasing_ppgtt_mapping = 1;
1460 }
1461}
1462
1463static void ggtt_unbind_vma(struct i915_vma *vma)
1464{
1465 struct drm_device *dev = vma->vm->dev;
1466 struct drm_i915_private *dev_priv = dev->dev_private;
1467 struct drm_i915_gem_object *obj = vma->obj;
1468 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1469
1470 if (obj->has_global_gtt_mapping) {
1471 vma->vm->clear_range(vma->vm, entry,
1472 vma->obj->base.size >> PAGE_SHIFT,
1473 true);
1474 obj->has_global_gtt_mapping = 0;
1475 }
1476
1477 if (obj->has_aliasing_ppgtt_mapping) {
1478 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1479 appgtt->base.clear_range(&appgtt->base,
1480 entry,
1481 obj->base.size >> PAGE_SHIFT,
1482 true);
1483 obj->has_aliasing_ppgtt_mapping = 0;
1484 }
Daniel Vetter74163902012-02-15 23:50:21 +01001485}
1486
1487void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1488{
Ben Widawsky5c042282011-10-17 15:51:55 -07001489 struct drm_device *dev = obj->base.dev;
1490 struct drm_i915_private *dev_priv = dev->dev_private;
1491 bool interruptible;
1492
1493 interruptible = do_idling(dev_priv);
1494
Chris Wilson9da3da62012-06-01 15:20:22 +01001495 if (!obj->has_dma_mapping)
1496 dma_unmap_sg(&dev->pdev->dev,
1497 obj->pages->sgl, obj->pages->nents,
1498 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001499
1500 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001501}
Daniel Vetter644ec022012-03-26 09:45:40 +02001502
Chris Wilson42d6ab42012-07-26 11:49:32 +01001503static void i915_gtt_color_adjust(struct drm_mm_node *node,
1504 unsigned long color,
1505 unsigned long *start,
1506 unsigned long *end)
1507{
1508 if (node->color != color)
1509 *start += 4096;
1510
1511 if (!list_empty(&node->node_list)) {
1512 node = list_entry(node->node_list.next,
1513 struct drm_mm_node,
1514 node_list);
1515 if (node->allocated && node->color != color)
1516 *end -= 4096;
1517 }
1518}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001519
Ben Widawskyd7e50082012-12-18 10:31:25 -08001520void i915_gem_setup_global_gtt(struct drm_device *dev,
1521 unsigned long start,
1522 unsigned long mappable_end,
1523 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001524{
Ben Widawskye78891c2013-01-25 16:41:04 -08001525 /* Let GEM Manage all of the aperture.
1526 *
1527 * However, leave one page at the end still bound to the scratch page.
1528 * There are a number of places where the hardware apparently prefetches
1529 * past the end of the object, and we've seen multiple hangs with the
1530 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1531 * aperture. One page should be enough to keep any prefetching inside
1532 * of the aperture.
1533 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001534 struct drm_i915_private *dev_priv = dev->dev_private;
1535 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001536 struct drm_mm_node *entry;
1537 struct drm_i915_gem_object *obj;
1538 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +02001539
Ben Widawsky35451cb2013-01-17 12:45:13 -08001540 BUG_ON(mappable_end > end);
1541
Chris Wilsoned2f3452012-11-15 11:32:19 +00001542 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001543 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001544 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001545 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001546
Chris Wilsoned2f3452012-11-15 11:32:19 +00001547 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001548 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001549 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001550 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -07001551 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001552 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001553
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001554 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001555 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001556 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001557 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +00001558 obj->has_global_gtt_mapping = 1;
1559 }
1560
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001561 dev_priv->gtt.base.start = start;
1562 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001563
Chris Wilsoned2f3452012-11-15 11:32:19 +00001564 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001565 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001566 const unsigned long count = (hole_end - hole_start) / PAGE_SIZE;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001567 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1568 hole_start, hole_end);
Ben Widawsky828c7902013-10-16 09:21:30 -07001569 ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001570 }
1571
1572 /* And finally clear the reserved guard page */
Ben Widawsky828c7902013-10-16 09:21:30 -07001573 ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001574}
1575
Ben Widawskyd7e50082012-12-18 10:31:25 -08001576void i915_gem_init_global_gtt(struct drm_device *dev)
1577{
1578 struct drm_i915_private *dev_priv = dev->dev_private;
1579 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001580
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001581 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001582 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001583
Ben Widawskye78891c2013-01-25 16:41:04 -08001584 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001585}
1586
1587static int setup_scratch_page(struct drm_device *dev)
1588{
1589 struct drm_i915_private *dev_priv = dev->dev_private;
1590 struct page *page;
1591 dma_addr_t dma_addr;
1592
1593 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1594 if (page == NULL)
1595 return -ENOMEM;
1596 get_page(page);
1597 set_pages_uc(page, 1);
1598
1599#ifdef CONFIG_INTEL_IOMMU
1600 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1601 PCI_DMA_BIDIRECTIONAL);
1602 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1603 return -EINVAL;
1604#else
1605 dma_addr = page_to_phys(page);
1606#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001607 dev_priv->gtt.base.scratch.page = page;
1608 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001609
1610 return 0;
1611}
1612
1613static void teardown_scratch_page(struct drm_device *dev)
1614{
1615 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001616 struct page *page = dev_priv->gtt.base.scratch.page;
1617
1618 set_pages_wb(page, 1);
1619 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001620 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001621 put_page(page);
1622 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001623}
1624
1625static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1626{
1627 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1628 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1629 return snb_gmch_ctl << 20;
1630}
1631
Ben Widawsky9459d252013-11-03 16:53:55 -08001632static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1633{
1634 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1635 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1636 if (bdw_gmch_ctl)
1637 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky3a2ffb62013-11-07 21:40:51 -08001638 if (bdw_gmch_ctl > 4) {
Jani Nikulad330a952014-01-21 11:24:25 +02001639 WARN_ON(!i915.preliminary_hw_support);
Ben Widawsky3a2ffb62013-11-07 21:40:51 -08001640 return 4<<20;
1641 }
1642
Ben Widawsky9459d252013-11-03 16:53:55 -08001643 return bdw_gmch_ctl << 20;
1644}
1645
Ben Widawskybaa09f52013-01-24 13:49:57 -08001646static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001647{
1648 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1649 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1650 return snb_gmch_ctl << 25; /* 32 MB units */
1651}
1652
Ben Widawsky9459d252013-11-03 16:53:55 -08001653static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1654{
1655 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1656 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1657 return bdw_gmch_ctl << 25; /* 32 MB units */
1658}
1659
Ben Widawsky63340132013-11-04 19:32:22 -08001660static int ggtt_probe_common(struct drm_device *dev,
1661 size_t gtt_size)
1662{
1663 struct drm_i915_private *dev_priv = dev->dev_private;
1664 phys_addr_t gtt_bus_addr;
1665 int ret;
1666
1667 /* For Modern GENs the PTEs and register space are split in the BAR */
1668 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
1669 (pci_resource_len(dev->pdev, 0) / 2);
1670
1671 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
1672 if (!dev_priv->gtt.gsm) {
1673 DRM_ERROR("Failed to map the gtt page table\n");
1674 return -ENOMEM;
1675 }
1676
1677 ret = setup_scratch_page(dev);
1678 if (ret) {
1679 DRM_ERROR("Scratch setup failed\n");
1680 /* iounmap will also get called at remove, but meh */
1681 iounmap(dev_priv->gtt.gsm);
1682 }
1683
1684 return ret;
1685}
1686
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001687/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1688 * bits. When using advanced contexts each context stores its own PAT, but
1689 * writing this data shouldn't be harmful even in those cases. */
1690static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1691{
1692#define GEN8_PPAT_UC (0<<0)
1693#define GEN8_PPAT_WC (1<<0)
1694#define GEN8_PPAT_WT (2<<0)
1695#define GEN8_PPAT_WB (3<<0)
1696#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
1697/* FIXME(BDW): Bspec is completely confused about cache control bits. */
1698#define GEN8_PPAT_LLC (1<<2)
1699#define GEN8_PPAT_LLCELLC (2<<2)
1700#define GEN8_PPAT_LLCeLLC (3<<2)
1701#define GEN8_PPAT_AGE(x) (x<<4)
1702#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
1703 uint64_t pat;
1704
1705 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1706 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1707 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1708 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1709 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1710 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1711 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1712 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1713
1714 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1715 * write would work. */
1716 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1717 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1718}
1719
Ben Widawsky63340132013-11-04 19:32:22 -08001720static int gen8_gmch_probe(struct drm_device *dev,
1721 size_t *gtt_total,
1722 size_t *stolen,
1723 phys_addr_t *mappable_base,
1724 unsigned long *mappable_end)
1725{
1726 struct drm_i915_private *dev_priv = dev->dev_private;
1727 unsigned int gtt_size;
1728 u16 snb_gmch_ctl;
1729 int ret;
1730
1731 /* TODO: We're not aware of mappable constraints on gen8 yet */
1732 *mappable_base = pci_resource_start(dev->pdev, 2);
1733 *mappable_end = pci_resource_len(dev->pdev, 2);
1734
1735 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1736 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1737
1738 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1739
1740 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1741
1742 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskyd31eb102013-11-02 21:07:17 -07001743 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001744
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001745 gen8_setup_private_ppat(dev_priv);
1746
Ben Widawsky63340132013-11-04 19:32:22 -08001747 ret = ggtt_probe_common(dev, gtt_size);
1748
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001749 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1750 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001751
1752 return ret;
1753}
1754
Ben Widawskybaa09f52013-01-24 13:49:57 -08001755static int gen6_gmch_probe(struct drm_device *dev,
1756 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001757 size_t *stolen,
1758 phys_addr_t *mappable_base,
1759 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001760{
1761 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001762 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001763 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001764 int ret;
1765
Ben Widawsky41907dd2013-02-08 11:32:47 -08001766 *mappable_base = pci_resource_start(dev->pdev, 2);
1767 *mappable_end = pci_resource_len(dev->pdev, 2);
1768
Ben Widawskybaa09f52013-01-24 13:49:57 -08001769 /* 64/512MB is the current min/max we actually know of, but this is just
1770 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001771 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001772 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001773 DRM_ERROR("Unknown GMADR size (%lx)\n",
1774 dev_priv->gtt.mappable_end);
1775 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001776 }
1777
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001778 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1779 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001780 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001781
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07001782 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001783
Ben Widawsky63340132013-11-04 19:32:22 -08001784 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001785 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
1786
Ben Widawsky63340132013-11-04 19:32:22 -08001787 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001788
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001789 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1790 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001791
1792 return ret;
1793}
1794
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001795static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001796{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001797
1798 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08001799
1800 drm_mm_takedown(&vm->mm);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001801 iounmap(gtt->gsm);
1802 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001803}
1804
1805static int i915_gmch_probe(struct drm_device *dev,
1806 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001807 size_t *stolen,
1808 phys_addr_t *mappable_base,
1809 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001810{
1811 struct drm_i915_private *dev_priv = dev->dev_private;
1812 int ret;
1813
Ben Widawskybaa09f52013-01-24 13:49:57 -08001814 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1815 if (!ret) {
1816 DRM_ERROR("failed to set up gmch\n");
1817 return -EIO;
1818 }
1819
Ben Widawsky41907dd2013-02-08 11:32:47 -08001820 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001821
1822 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001823 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001824
Chris Wilsonc0a7f812013-12-30 12:16:15 +00001825 if (unlikely(dev_priv->gtt.do_idle_maps))
1826 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
1827
Ben Widawskybaa09f52013-01-24 13:49:57 -08001828 return 0;
1829}
1830
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001831static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001832{
1833 intel_gmch_remove();
1834}
1835
1836int i915_gem_gtt_init(struct drm_device *dev)
1837{
1838 struct drm_i915_private *dev_priv = dev->dev_private;
1839 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001840 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001841
Ben Widawskybaa09f52013-01-24 13:49:57 -08001842 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001843 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001844 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08001845 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001846 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001847 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001848 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001849 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001850 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001851 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001852 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001853 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001854 else if (INTEL_INFO(dev)->gen >= 7)
1855 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001856 else
Chris Wilson350ec882013-08-06 13:17:02 +01001857 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08001858 } else {
1859 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1860 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001861 }
1862
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001863 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001864 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001865 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001866 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001867
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001868 gtt->base.dev = dev;
1869
Ben Widawskybaa09f52013-01-24 13:49:57 -08001870 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001871 DRM_INFO("Memory usable by graphics device = %zdM\n",
1872 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001873 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1874 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001875
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001876 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001877}
Ben Widawsky6f65e292013-12-06 14:10:56 -08001878
1879static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
1880 struct i915_address_space *vm)
1881{
1882 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
1883 if (vma == NULL)
1884 return ERR_PTR(-ENOMEM);
1885
1886 INIT_LIST_HEAD(&vma->vma_link);
1887 INIT_LIST_HEAD(&vma->mm_list);
1888 INIT_LIST_HEAD(&vma->exec_list);
1889 vma->vm = vm;
1890 vma->obj = obj;
1891
1892 switch (INTEL_INFO(vm->dev)->gen) {
1893 case 8:
1894 case 7:
1895 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001896 if (i915_is_ggtt(vm)) {
1897 vma->unbind_vma = ggtt_unbind_vma;
1898 vma->bind_vma = ggtt_bind_vma;
1899 } else {
1900 vma->unbind_vma = ppgtt_unbind_vma;
1901 vma->bind_vma = ppgtt_bind_vma;
1902 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08001903 break;
1904 case 5:
1905 case 4:
1906 case 3:
1907 case 2:
1908 BUG_ON(!i915_is_ggtt(vm));
1909 vma->unbind_vma = i915_ggtt_unbind_vma;
1910 vma->bind_vma = i915_ggtt_bind_vma;
1911 break;
1912 default:
1913 BUG();
1914 }
1915
1916 /* Keep GGTT vmas first to make debug easier */
1917 if (i915_is_ggtt(vm))
1918 list_add(&vma->vma_link, &obj->vma_list);
1919 else
1920 list_add_tail(&vma->vma_link, &obj->vma_list);
1921
1922 return vma;
1923}
1924
1925struct i915_vma *
1926i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
1927 struct i915_address_space *vm)
1928{
1929 struct i915_vma *vma;
1930
1931 vma = i915_gem_obj_to_vma(obj, vm);
1932 if (!vma)
1933 vma = __i915_gem_vma_create(obj, vm);
1934
1935 return vma;
1936}