blob: 998f9a0b322a627591666addbe8db2242af75be8 [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
David Howells760285e2012-10-02 18:01:07 +010025#include <drm/drmP.h>
26#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010027#include "i915_drv.h"
28#include "i915_trace.h"
29#include "intel_drv.h"
30
Ben Widawsky6670a5a2013-06-27 16:30:04 -070031#define GEN6_PPGTT_PD_ENTRIES 512
32#define I915_PPGTT_PT_ENTRIES (PAGE_SIZE / sizeof(gen6_gtt_pte_t))
Ben Widawskyd31eb102013-11-02 21:07:17 -070033typedef uint64_t gen8_gtt_pte_t;
Ben Widawsky37aca442013-11-04 20:47:32 -080034typedef gen8_gtt_pte_t gen8_ppgtt_pde_t;
Ben Widawsky6670a5a2013-06-27 16:30:04 -070035
Ben Widawsky26b1ff32012-11-04 09:21:31 -080036/* PPGTT stuff */
37#define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0))
Ben Widawsky0d8ff152013-07-04 11:02:03 -070038#define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0))
Ben Widawsky26b1ff32012-11-04 09:21:31 -080039
40#define GEN6_PDE_VALID (1 << 0)
41/* gen6+ has bit 11-4 for physical addr bit 39-32 */
42#define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
43
44#define GEN6_PTE_VALID (1 << 0)
45#define GEN6_PTE_UNCACHED (1 << 1)
46#define HSW_PTE_UNCACHED (0)
47#define GEN6_PTE_CACHE_LLC (2 << 1)
Chris Wilson350ec882013-08-06 13:17:02 +010048#define GEN7_PTE_CACHE_L3_LLC (3 << 1)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080049#define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
Ben Widawsky0d8ff152013-07-04 11:02:03 -070050#define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr)
51
52/* Cacheability Control is a 4-bit value. The low three bits are stored in *
53 * bits 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
54 */
55#define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \
56 (((bits) & 0x8) << (11 - 3)))
Ben Widawsky87a6b682013-08-04 23:47:29 -070057#define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2)
Ben Widawsky0d8ff152013-07-04 11:02:03 -070058#define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3)
Ben Widawsky4d15c142013-07-04 11:02:06 -070059#define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb)
Chris Wilsonc51e9702013-11-22 10:37:53 +000060#define HSW_WB_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x8)
Chris Wilson651d7942013-08-08 14:41:10 +010061#define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6)
Chris Wilsonc51e9702013-11-22 10:37:53 +000062#define HSW_WT_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x7)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080063
Ben Widawsky459108b2013-11-02 21:07:23 -070064#define GEN8_PTES_PER_PAGE (PAGE_SIZE / sizeof(gen8_gtt_pte_t))
Ben Widawsky37aca442013-11-04 20:47:32 -080065#define GEN8_PDES_PER_PAGE (PAGE_SIZE / sizeof(gen8_ppgtt_pde_t))
66#define GEN8_LEGACY_PDPS 4
67
Ben Widawskyfbe5d362013-11-04 19:56:49 -080068#define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD)
69#define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */
70#define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */
71#define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */
72
Ben Widawsky6f65e292013-12-06 14:10:56 -080073static void ppgtt_bind_vma(struct i915_vma *vma,
74 enum i915_cache_level cache_level,
75 u32 flags);
76static void ppgtt_unbind_vma(struct i915_vma *vma);
Ben Widawskyeeb94882013-12-06 14:11:10 -080077static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt);
Ben Widawsky6f65e292013-12-06 14:10:56 -080078
Ben Widawsky94ec8f62013-11-02 21:07:18 -070079static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
80 enum i915_cache_level level,
81 bool valid)
82{
83 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
84 pte |= addr;
Ben Widawskyfbe5d362013-11-04 19:56:49 -080085 if (level != I915_CACHE_NONE)
86 pte |= PPAT_CACHED_INDEX;
87 else
88 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky94ec8f62013-11-02 21:07:18 -070089 return pte;
90}
91
Ben Widawskyb1fe6672013-11-04 21:20:14 -080092static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
93 dma_addr_t addr,
94 enum i915_cache_level level)
95{
96 gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
97 pde |= addr;
98 if (level != I915_CACHE_NONE)
99 pde |= PPAT_CACHED_PDE_INDEX;
100 else
101 pde |= PPAT_UNCACHED_INDEX;
102 return pde;
103}
104
Chris Wilson350ec882013-08-06 13:17:02 +0100105static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700106 enum i915_cache_level level,
107 bool valid)
Ben Widawsky54d12522012-09-24 16:44:32 -0700108{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700109 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700110 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700111
112 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100113 case I915_CACHE_L3_LLC:
114 case I915_CACHE_LLC:
115 pte |= GEN6_PTE_CACHE_LLC;
116 break;
117 case I915_CACHE_NONE:
118 pte |= GEN6_PTE_UNCACHED;
119 break;
120 default:
121 WARN_ON(1);
122 }
123
124 return pte;
125}
126
127static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700128 enum i915_cache_level level,
129 bool valid)
Chris Wilson350ec882013-08-06 13:17:02 +0100130{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700131 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100132 pte |= GEN6_PTE_ADDR_ENCODE(addr);
133
134 switch (level) {
135 case I915_CACHE_L3_LLC:
136 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700137 break;
138 case I915_CACHE_LLC:
139 pte |= GEN6_PTE_CACHE_LLC;
140 break;
141 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700142 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700143 break;
144 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100145 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700146 }
147
Ben Widawsky54d12522012-09-24 16:44:32 -0700148 return pte;
149}
150
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700151#define BYT_PTE_WRITEABLE (1 << 1)
152#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
153
Ben Widawsky80a74f72013-06-27 16:30:19 -0700154static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700155 enum i915_cache_level level,
156 bool valid)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700157{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700158 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700159 pte |= GEN6_PTE_ADDR_ENCODE(addr);
160
161 /* Mark the page as writeable. Other platforms don't have a
162 * setting for read-only/writable, so this matches that behavior.
163 */
164 pte |= BYT_PTE_WRITEABLE;
165
166 if (level != I915_CACHE_NONE)
167 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
168
169 return pte;
170}
171
Ben Widawsky80a74f72013-06-27 16:30:19 -0700172static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700173 enum i915_cache_level level,
174 bool valid)
Kenneth Graunke91197082013-04-22 00:53:51 -0700175{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700176 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700177 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700178
179 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700180 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700181
182 return pte;
183}
184
Ben Widawsky4d15c142013-07-04 11:02:06 -0700185static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700186 enum i915_cache_level level,
187 bool valid)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700188{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700189 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700190 pte |= HSW_PTE_ADDR_ENCODE(addr);
191
Chris Wilson651d7942013-08-08 14:41:10 +0100192 switch (level) {
193 case I915_CACHE_NONE:
194 break;
195 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000196 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100197 break;
198 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000199 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100200 break;
201 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700202
203 return pte;
204}
205
Ben Widawsky94e409c2013-11-04 22:29:36 -0800206/* Broadwell Page Directory Pointer Descriptors */
207static int gen8_write_pdp(struct intel_ring_buffer *ring, unsigned entry,
Ben Widawskye178f702013-12-06 14:10:47 -0800208 uint64_t val, bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800209{
Ben Widawskye178f702013-12-06 14:10:47 -0800210 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800211 int ret;
212
213 BUG_ON(entry >= 4);
214
Ben Widawskye178f702013-12-06 14:10:47 -0800215 if (synchronous) {
216 I915_WRITE(GEN8_RING_PDP_UDW(ring, entry), val >> 32);
217 I915_WRITE(GEN8_RING_PDP_LDW(ring, entry), (u32)val);
218 return 0;
219 }
220
Ben Widawsky94e409c2013-11-04 22:29:36 -0800221 ret = intel_ring_begin(ring, 6);
222 if (ret)
223 return ret;
224
225 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
226 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
227 intel_ring_emit(ring, (u32)(val >> 32));
228 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
229 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
230 intel_ring_emit(ring, (u32)(val));
231 intel_ring_advance(ring);
232
233 return 0;
234}
235
Ben Widawskyeeb94882013-12-06 14:11:10 -0800236static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
237 struct intel_ring_buffer *ring,
238 bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800239{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800240 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800241
242 /* bit of a hack to find the actual last used pd */
243 int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
244
Ben Widawsky94e409c2013-11-04 22:29:36 -0800245 for (i = used_pd - 1; i >= 0; i--) {
246 dma_addr_t addr = ppgtt->pd_dma_addr[i];
Ben Widawskyeeb94882013-12-06 14:11:10 -0800247 ret = gen8_write_pdp(ring, i, addr, synchronous);
248 if (ret)
249 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800250 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800251
Ben Widawskyeeb94882013-12-06 14:11:10 -0800252 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800253}
254
Ben Widawsky459108b2013-11-02 21:07:23 -0700255static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
256 unsigned first_entry,
257 unsigned num_entries,
258 bool use_scratch)
259{
260 struct i915_hw_ppgtt *ppgtt =
261 container_of(vm, struct i915_hw_ppgtt, base);
262 gen8_gtt_pte_t *pt_vaddr, scratch_pte;
263 unsigned act_pt = first_entry / GEN8_PTES_PER_PAGE;
264 unsigned first_pte = first_entry % GEN8_PTES_PER_PAGE;
265 unsigned last_pte, i;
266
267 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
268 I915_CACHE_LLC, use_scratch);
269
270 while (num_entries) {
271 struct page *page_table = &ppgtt->gen8_pt_pages[act_pt];
272
273 last_pte = first_pte + num_entries;
274 if (last_pte > GEN8_PTES_PER_PAGE)
275 last_pte = GEN8_PTES_PER_PAGE;
276
277 pt_vaddr = kmap_atomic(page_table);
278
279 for (i = first_pte; i < last_pte; i++)
280 pt_vaddr[i] = scratch_pte;
281
282 kunmap_atomic(pt_vaddr);
283
284 num_entries -= last_pte - first_pte;
285 first_pte = 0;
286 act_pt++;
287 }
288}
289
Ben Widawsky9df15b42013-11-02 21:07:24 -0700290static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
291 struct sg_table *pages,
292 unsigned first_entry,
293 enum i915_cache_level cache_level)
294{
295 struct i915_hw_ppgtt *ppgtt =
296 container_of(vm, struct i915_hw_ppgtt, base);
297 gen8_gtt_pte_t *pt_vaddr;
298 unsigned act_pt = first_entry / GEN8_PTES_PER_PAGE;
299 unsigned act_pte = first_entry % GEN8_PTES_PER_PAGE;
300 struct sg_page_iter sg_iter;
301
302 pt_vaddr = kmap_atomic(&ppgtt->gen8_pt_pages[act_pt]);
303 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
304 dma_addr_t page_addr;
305
306 page_addr = sg_dma_address(sg_iter.sg) +
307 (sg_iter.sg_pgoffset << PAGE_SHIFT);
308 pt_vaddr[act_pte] = gen8_pte_encode(page_addr, cache_level,
309 true);
310 if (++act_pte == GEN8_PTES_PER_PAGE) {
311 kunmap_atomic(pt_vaddr);
312 act_pt++;
313 pt_vaddr = kmap_atomic(&ppgtt->gen8_pt_pages[act_pt]);
314 act_pte = 0;
315
316 }
317 }
318 kunmap_atomic(pt_vaddr);
319}
320
Ben Widawsky37aca442013-11-04 20:47:32 -0800321static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
322{
323 struct i915_hw_ppgtt *ppgtt =
324 container_of(vm, struct i915_hw_ppgtt, base);
325 int i, j;
326
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800327 list_del(&vm->global_link);
Ben Widawsky686e1f62013-11-25 09:54:34 -0800328 drm_mm_takedown(&vm->mm);
329
Ben Widawsky37aca442013-11-04 20:47:32 -0800330 for (i = 0; i < ppgtt->num_pd_pages ; i++) {
331 if (ppgtt->pd_dma_addr[i]) {
332 pci_unmap_page(ppgtt->base.dev->pdev,
333 ppgtt->pd_dma_addr[i],
334 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
335
336 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
337 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
338 if (addr)
339 pci_unmap_page(ppgtt->base.dev->pdev,
340 addr,
341 PAGE_SIZE,
342 PCI_DMA_BIDIRECTIONAL);
343
344 }
345 }
346 kfree(ppgtt->gen8_pt_dma_addr[i]);
347 }
348
Ben Widawsky230f9552013-11-07 21:40:48 -0800349 __free_pages(ppgtt->gen8_pt_pages, get_order(ppgtt->num_pt_pages << PAGE_SHIFT));
350 __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
Ben Widawsky37aca442013-11-04 20:47:32 -0800351}
352
353/**
354 * GEN8 legacy ppgtt programming is accomplished through 4 PDP registers with a
355 * net effect resembling a 2-level page table in normal x86 terms. Each PDP
356 * represents 1GB of memory
357 * 4 * 512 * 512 * 4096 = 4GB legacy 32b address space.
358 *
359 * TODO: Do something with the size parameter
360 **/
361static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
362{
363 struct page *pt_pages;
364 int i, j, ret = -ENOMEM;
365 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
366 const int num_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
367
368 if (size % (1<<30))
369 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
370
371 /* FIXME: split allocation into smaller pieces. For now we only ever do
372 * this once, but with full PPGTT, the multiple contiguous allocations
373 * will be bad.
374 */
375 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
376 if (!ppgtt->pd_pages)
377 return -ENOMEM;
378
379 pt_pages = alloc_pages(GFP_KERNEL, get_order(num_pt_pages << PAGE_SHIFT));
380 if (!pt_pages) {
381 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
382 return -ENOMEM;
383 }
384
385 ppgtt->gen8_pt_pages = pt_pages;
386 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
387 ppgtt->num_pt_pages = 1 << get_order(num_pt_pages << PAGE_SHIFT);
388 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800389 ppgtt->enable = gen8_ppgtt_enable;
Ben Widawskyeeb94882013-12-06 14:11:10 -0800390 ppgtt->switch_mm = gen8_mm_switch;
Ben Widawsky459108b2013-11-02 21:07:23 -0700391 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700392 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Ben Widawsky37aca442013-11-04 20:47:32 -0800393 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -0800394 ppgtt->base.start = 0;
395 ppgtt->base.total = ppgtt->num_pt_pages * GEN8_PTES_PER_PAGE * PAGE_SIZE;
Ben Widawsky37aca442013-11-04 20:47:32 -0800396
397 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
398
399 /*
400 * - Create a mapping for the page directories.
401 * - For each page directory:
402 * allocate space for page table mappings.
403 * map each page table
404 */
405 for (i = 0; i < max_pdp; i++) {
406 dma_addr_t temp;
407 temp = pci_map_page(ppgtt->base.dev->pdev,
408 &ppgtt->pd_pages[i], 0,
409 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
410 if (pci_dma_mapping_error(ppgtt->base.dev->pdev, temp))
411 goto err_out;
412
413 ppgtt->pd_dma_addr[i] = temp;
414
415 ppgtt->gen8_pt_dma_addr[i] = kmalloc(sizeof(dma_addr_t) * GEN8_PDES_PER_PAGE, GFP_KERNEL);
416 if (!ppgtt->gen8_pt_dma_addr[i])
417 goto err_out;
418
419 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
420 struct page *p = &pt_pages[i * GEN8_PDES_PER_PAGE + j];
421 temp = pci_map_page(ppgtt->base.dev->pdev,
422 p, 0, PAGE_SIZE,
423 PCI_DMA_BIDIRECTIONAL);
424
425 if (pci_dma_mapping_error(ppgtt->base.dev->pdev, temp))
426 goto err_out;
427
428 ppgtt->gen8_pt_dma_addr[i][j] = temp;
429 }
430 }
431
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800432 /* For now, the PPGTT helper functions all require that the PDEs are
433 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
434 * will never need to touch the PDEs again */
435 for (i = 0; i < max_pdp; i++) {
436 gen8_ppgtt_pde_t *pd_vaddr;
437 pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
438 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
439 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
440 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
441 I915_CACHE_LLC);
442 }
443 kunmap_atomic(pd_vaddr);
444 }
445
Ben Widawsky459108b2013-11-02 21:07:23 -0700446 ppgtt->base.clear_range(&ppgtt->base, 0,
447 ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE,
448 true);
449
Ben Widawsky37aca442013-11-04 20:47:32 -0800450 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
451 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
452 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
453 ppgtt->num_pt_pages,
454 (ppgtt->num_pt_pages - num_pt_pages) +
455 size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700456 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800457
458err_out:
459 ppgtt->base.cleanup(&ppgtt->base);
460 return ret;
461}
462
Ben Widawsky87d60b62013-12-06 14:11:29 -0800463static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
464{
465 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
466 struct i915_address_space *vm = &ppgtt->base;
467 gen6_gtt_pte_t __iomem *pd_addr;
468 gen6_gtt_pte_t scratch_pte;
469 uint32_t pd_entry;
470 int pte, pde;
471
472 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
473
474 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
475 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
476
477 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
478 ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
479 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
480 u32 expected;
481 gen6_gtt_pte_t *pt_vaddr;
482 dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
483 pd_entry = readl(pd_addr + pde);
484 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
485
486 if (pd_entry != expected)
487 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
488 pde,
489 pd_entry,
490 expected);
491 seq_printf(m, "\tPDE: %x\n", pd_entry);
492
493 pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
494 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
495 unsigned long va =
496 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
497 (pte * PAGE_SIZE);
498 int i;
499 bool found = false;
500 for (i = 0; i < 4; i++)
501 if (pt_vaddr[pte + i] != scratch_pte)
502 found = true;
503 if (!found)
504 continue;
505
506 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
507 for (i = 0; i < 4; i++) {
508 if (pt_vaddr[pte + i] != scratch_pte)
509 seq_printf(m, " %08x", pt_vaddr[pte + i]);
510 else
511 seq_puts(m, " SCRATCH ");
512 }
513 seq_puts(m, "\n");
514 }
515 kunmap_atomic(pt_vaddr);
516 }
517}
518
Ben Widawsky3e302542013-04-23 23:15:32 -0700519static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700520{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700521 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700522 gen6_gtt_pte_t __iomem *pd_addr;
523 uint32_t pd_entry;
524 int i;
525
Ben Widawsky0a732872013-04-23 23:15:30 -0700526 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700527 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
528 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
529 for (i = 0; i < ppgtt->num_pd_entries; i++) {
530 dma_addr_t pt_addr;
531
532 pt_addr = ppgtt->pt_dma_addr[i];
533 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
534 pd_entry |= GEN6_PDE_VALID;
535
536 writel(pd_entry, pd_addr + i);
537 }
538 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700539}
540
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800541static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
542{
543 BUG_ON(ppgtt->pd_offset & 0x3f);
544
545 return (ppgtt->pd_offset / 64) << 16;
546}
547
Ben Widawsky90252e52013-12-06 14:11:12 -0800548static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
549 struct intel_ring_buffer *ring,
550 bool synchronous)
551{
552 struct drm_device *dev = ppgtt->base.dev;
553 struct drm_i915_private *dev_priv = dev->dev_private;
554 int ret;
555
556 /* If we're in reset, we can assume the GPU is sufficiently idle to
557 * manually frob these bits. Ideally we could use the ring functions,
558 * except our error handling makes it quite difficult (can't use
559 * intel_ring_begin, ring->flush, or intel_ring_advance)
560 *
561 * FIXME: We should try not to special case reset
562 */
563 if (synchronous ||
564 i915_reset_in_progress(&dev_priv->gpu_error)) {
565 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
566 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
567 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
568 POSTING_READ(RING_PP_DIR_BASE(ring));
569 return 0;
570 }
571
572 /* NB: TLBs must be flushed and invalidated before a switch */
573 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
574 if (ret)
575 return ret;
576
577 ret = intel_ring_begin(ring, 6);
578 if (ret)
579 return ret;
580
581 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
582 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
583 intel_ring_emit(ring, PP_DIR_DCLV_2G);
584 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
585 intel_ring_emit(ring, get_pd_offset(ppgtt));
586 intel_ring_emit(ring, MI_NOOP);
587 intel_ring_advance(ring);
588
589 return 0;
590}
591
Ben Widawsky48a10382013-12-06 14:11:11 -0800592static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
593 struct intel_ring_buffer *ring,
594 bool synchronous)
595{
596 struct drm_device *dev = ppgtt->base.dev;
597 struct drm_i915_private *dev_priv = dev->dev_private;
598 int ret;
599
600 /* If we're in reset, we can assume the GPU is sufficiently idle to
601 * manually frob these bits. Ideally we could use the ring functions,
602 * except our error handling makes it quite difficult (can't use
603 * intel_ring_begin, ring->flush, or intel_ring_advance)
604 *
605 * FIXME: We should try not to special case reset
606 */
607 if (synchronous ||
608 i915_reset_in_progress(&dev_priv->gpu_error)) {
609 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
610 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
611 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
612 POSTING_READ(RING_PP_DIR_BASE(ring));
613 return 0;
614 }
615
616 /* NB: TLBs must be flushed and invalidated before a switch */
617 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
618 if (ret)
619 return ret;
620
621 ret = intel_ring_begin(ring, 6);
622 if (ret)
623 return ret;
624
625 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
626 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
627 intel_ring_emit(ring, PP_DIR_DCLV_2G);
628 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
629 intel_ring_emit(ring, get_pd_offset(ppgtt));
630 intel_ring_emit(ring, MI_NOOP);
631 intel_ring_advance(ring);
632
Ben Widawsky90252e52013-12-06 14:11:12 -0800633 /* XXX: RCS is the only one to auto invalidate the TLBs? */
634 if (ring->id != RCS) {
635 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
636 if (ret)
637 return ret;
638 }
639
Ben Widawsky48a10382013-12-06 14:11:11 -0800640 return 0;
641}
642
Ben Widawskyeeb94882013-12-06 14:11:10 -0800643static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
644 struct intel_ring_buffer *ring,
645 bool synchronous)
646{
647 struct drm_device *dev = ppgtt->base.dev;
648 struct drm_i915_private *dev_priv = dev->dev_private;
649
Ben Widawsky48a10382013-12-06 14:11:11 -0800650 if (!synchronous)
651 return 0;
652
Ben Widawskyeeb94882013-12-06 14:11:10 -0800653 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
654 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
655
656 POSTING_READ(RING_PP_DIR_DCLV(ring));
657
658 return 0;
659}
660
661static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
662{
663 struct drm_device *dev = ppgtt->base.dev;
664 struct drm_i915_private *dev_priv = dev->dev_private;
665 struct intel_ring_buffer *ring;
666 int j, ret;
667
668 for_each_ring(ring, dev_priv, j) {
669 I915_WRITE(RING_MODE_GEN7(ring),
670 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800671
672 /* We promise to do a switch later with FULL PPGTT. If this is
673 * aliasing, this is the one and only switch we'll do */
674 if (USES_FULL_PPGTT(dev))
675 continue;
676
Ben Widawskyeeb94882013-12-06 14:11:10 -0800677 ret = ppgtt->switch_mm(ppgtt, ring, true);
678 if (ret)
679 goto err_out;
680 }
681
682 return 0;
683
684err_out:
685 for_each_ring(ring, dev_priv, j)
686 I915_WRITE(RING_MODE_GEN7(ring),
687 _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
688 return ret;
689}
690
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800691static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
692{
693 struct drm_device *dev = ppgtt->base.dev;
694 drm_i915_private_t *dev_priv = dev->dev_private;
695 struct intel_ring_buffer *ring;
696 uint32_t ecochk, ecobits;
697 int i;
698
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800699 ecobits = I915_READ(GAC_ECO_BITS);
700 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
701
702 ecochk = I915_READ(GAM_ECOCHK);
703 if (IS_HASWELL(dev)) {
704 ecochk |= ECOCHK_PPGTT_WB_HSW;
705 } else {
706 ecochk |= ECOCHK_PPGTT_LLC_IVB;
707 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
708 }
709 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800710
711 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800712 int ret;
713 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800714 I915_WRITE(RING_MODE_GEN7(ring),
715 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800716
717 /* We promise to do a switch later with FULL PPGTT. If this is
718 * aliasing, this is the one and only switch we'll do */
719 if (USES_FULL_PPGTT(dev))
720 continue;
721
Ben Widawskyeeb94882013-12-06 14:11:10 -0800722 ret = ppgtt->switch_mm(ppgtt, ring, true);
723 if (ret)
724 return ret;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800725 }
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800726
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800727 return 0;
728}
729
Ben Widawskya3d67d22013-12-06 14:11:06 -0800730static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700731{
Ben Widawskya3d67d22013-12-06 14:11:06 -0800732 struct drm_device *dev = ppgtt->base.dev;
Ben Widawsky3e302542013-04-23 23:15:32 -0700733 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky3e302542013-04-23 23:15:32 -0700734 struct intel_ring_buffer *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800735 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky3e302542013-04-23 23:15:32 -0700736 int i;
737
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800738 ecobits = I915_READ(GAC_ECO_BITS);
739 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
740 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700741
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800742 gab_ctl = I915_READ(GAB_CTL);
743 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700744
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800745 ecochk = I915_READ(GAM_ECOCHK);
746 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700747
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800748 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700749
750 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800751 int ret = ppgtt->switch_mm(ppgtt, ring, true);
752 if (ret)
753 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700754 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800755
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700756 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700757}
758
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100759/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700760static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100761 unsigned first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700762 unsigned num_entries,
763 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100764{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700765 struct i915_hw_ppgtt *ppgtt =
766 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700767 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Daniel Vettera15326a2013-03-19 23:48:39 +0100768 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100769 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
770 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100771
Ben Widawskyb35b3802013-10-16 09:18:21 -0700772 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100773
Daniel Vetter7bddb012012-02-09 17:15:47 +0100774 while (num_entries) {
775 last_pte = first_pte + num_entries;
776 if (last_pte > I915_PPGTT_PT_ENTRIES)
777 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100778
Daniel Vettera15326a2013-03-19 23:48:39 +0100779 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100780
781 for (i = first_pte; i < last_pte; i++)
782 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100783
784 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100785
Daniel Vetter7bddb012012-02-09 17:15:47 +0100786 num_entries -= last_pte - first_pte;
787 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100788 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100789 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100790}
791
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700792static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800793 struct sg_table *pages,
794 unsigned first_entry,
795 enum i915_cache_level cache_level)
796{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700797 struct i915_hw_ppgtt *ppgtt =
798 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700799 gen6_gtt_pte_t *pt_vaddr;
Daniel Vettera15326a2013-03-19 23:48:39 +0100800 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200801 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
802 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800803
Daniel Vettera15326a2013-03-19 23:48:39 +0100804 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200805 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
806 dma_addr_t page_addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800807
Imre Deak2db76d72013-03-26 15:14:18 +0200808 page_addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -0700809 pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200810 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
811 kunmap_atomic(pt_vaddr);
Daniel Vettera15326a2013-03-19 23:48:39 +0100812 act_pt++;
813 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200814 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800815
Daniel Vetterdef886c2013-01-24 14:44:56 -0800816 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800817 }
Imre Deak6e995e22013-02-18 19:28:04 +0200818 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800819}
820
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700821static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100822{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700823 struct i915_hw_ppgtt *ppgtt =
824 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -0800825 int i;
826
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800827 list_del(&vm->global_link);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700828 drm_mm_takedown(&ppgtt->base.mm);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800829 drm_mm_remove_node(&ppgtt->node);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700830
Daniel Vetter3440d262013-01-24 13:49:56 -0800831 if (ppgtt->pt_dma_addr) {
832 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700833 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800834 ppgtt->pt_dma_addr[i],
835 4096, PCI_DMA_BIDIRECTIONAL);
836 }
837
838 kfree(ppgtt->pt_dma_addr);
839 for (i = 0; i < ppgtt->num_pd_entries; i++)
840 __free_page(ppgtt->pt_pages[i]);
841 kfree(ppgtt->pt_pages);
842 kfree(ppgtt);
843}
844
845static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
846{
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800847#define GEN6_PD_ALIGN (PAGE_SIZE * 16)
848#define GEN6_PD_SIZE (GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700849 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100850 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -0800851 bool retried = false;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800852 int i, ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100853
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800854 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
855 * allocator works in address space sizes, so it's multiplied by page
856 * size. We allocate at the top of the GTT to avoid fragmentation.
857 */
858 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -0800859alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800860 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
861 &ppgtt->node, GEN6_PD_SIZE,
862 GEN6_PD_ALIGN, 0,
863 0, dev_priv->gtt.base.total,
864 DRM_MM_SEARCH_DEFAULT);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800865 if (ret == -ENOSPC && !retried) {
866 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
867 GEN6_PD_SIZE, GEN6_PD_ALIGN,
868 I915_CACHE_NONE, false, true);
869 if (ret)
870 return ret;
871
872 retried = true;
873 goto alloc;
874 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800875
876 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
877 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100878
Chris Wilson08c45262013-07-30 19:04:37 +0100879 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700880 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky48a10382013-12-06 14:11:11 -0800881 if (IS_GEN6(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800882 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -0800883 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -0800884 } else if (IS_HASWELL(dev)) {
885 ppgtt->enable = gen7_ppgtt_enable;
886 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -0800887 } else if (IS_GEN7(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800888 ppgtt->enable = gen7_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -0800889 ppgtt->switch_mm = gen7_mm_switch;
890 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800891 BUG();
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700892 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
893 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
894 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
895 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Ben Widawsky686e1f62013-11-25 09:54:34 -0800896 ppgtt->base.start = 0;
897 ppgtt->base.total = GEN6_PPGTT_PD_ENTRIES * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Daniel Vettera1e22652013-09-21 00:35:38 +0200898 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100899 GFP_KERNEL);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800900 if (!ppgtt->pt_pages) {
901 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -0800902 return -ENOMEM;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800903 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100904
905 for (i = 0; i < ppgtt->num_pd_entries; i++) {
906 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
907 if (!ppgtt->pt_pages[i])
908 goto err_pt_alloc;
909 }
910
Daniel Vettera1e22652013-09-21 00:35:38 +0200911 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800912 GFP_KERNEL);
913 if (!ppgtt->pt_dma_addr)
914 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100915
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800916 for (i = 0; i < ppgtt->num_pd_entries; i++) {
917 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200918
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800919 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
920 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100921
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800922 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
923 ret = -EIO;
924 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100925
Daniel Vetter211c5682012-04-10 17:29:17 +0200926 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800927 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100928 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100929
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700930 ppgtt->base.clear_range(&ppgtt->base, 0,
Ben Widawsky828c7902013-10-16 09:21:30 -0700931 ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800932 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100933
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800934 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
935 ppgtt->node.size >> 20,
936 ppgtt->node.start / PAGE_SIZE);
937 ppgtt->pd_offset =
938 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100939
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100940 return 0;
941
942err_pd_pin:
943 if (ppgtt->pt_dma_addr) {
944 for (i--; i >= 0; i--)
945 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
946 4096, PCI_DMA_BIDIRECTIONAL);
947 }
948err_pt_alloc:
949 kfree(ppgtt->pt_dma_addr);
950 for (i = 0; i < ppgtt->num_pd_entries; i++) {
951 if (ppgtt->pt_pages[i])
952 __free_page(ppgtt->pt_pages[i]);
953 }
954 kfree(ppgtt->pt_pages);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800955 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -0800956
957 return ret;
958}
959
Ben Widawsky246cbfb2013-12-06 14:11:14 -0800960int i915_gem_init_ppgtt(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -0800961{
962 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd6660ad2013-12-06 14:11:13 -0800963 int ret = 0;
Daniel Vetter3440d262013-01-24 13:49:56 -0800964
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700965 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -0800966
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700967 if (INTEL_INFO(dev)->gen < 8)
968 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -0700969 else if (IS_GEN8(dev))
Ben Widawsky37aca442013-11-04 20:47:32 -0800970 ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700971 else
972 BUG();
973
Ben Widawskyc7c48df2013-12-06 14:11:15 -0800974 if (!ret) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800975 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyc7c48df2013-12-06 14:11:15 -0800976 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700977 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
978 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800979 i915_init_vm(dev_priv, &ppgtt->base);
980 if (INTEL_INFO(dev)->gen < 8) {
Ben Widawsky9f273d42013-12-06 14:11:16 -0800981 gen6_write_pdes(ppgtt);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800982 DRM_DEBUG("Adding PPGTT at offset %x\n",
983 ppgtt->pd_offset << 10);
984 }
Ben Widawskyc7c48df2013-12-06 14:11:15 -0800985 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100986
987 return ret;
988}
989
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800990static void
Ben Widawsky6f65e292013-12-06 14:10:56 -0800991ppgtt_bind_vma(struct i915_vma *vma,
992 enum i915_cache_level cache_level,
993 u32 flags)
Daniel Vetter7bddb012012-02-09 17:15:47 +0100994{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800995 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
996
997 WARN_ON(flags);
998
999 vma->vm->insert_entries(vma->vm, vma->obj->pages, entry, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001000}
1001
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001002static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001003{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001004 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1005
1006 vma->vm->clear_range(vma->vm,
1007 entry,
1008 vma->obj->base.size >> PAGE_SHIFT,
1009 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001010}
1011
Ben Widawskya81cc002013-01-18 12:30:31 -08001012extern int intel_iommu_gfx_mapped;
1013/* Certain Gen5 chipsets require require idling the GPU before
1014 * unmapping anything from the GTT when VT-d is enabled.
1015 */
1016static inline bool needs_idle_maps(struct drm_device *dev)
1017{
1018#ifdef CONFIG_INTEL_IOMMU
1019 /* Query intel_iommu to see if we need the workaround. Presumably that
1020 * was loaded first.
1021 */
1022 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1023 return true;
1024#endif
1025 return false;
1026}
1027
Ben Widawsky5c042282011-10-17 15:51:55 -07001028static bool do_idling(struct drm_i915_private *dev_priv)
1029{
1030 bool ret = dev_priv->mm.interruptible;
1031
Ben Widawskya81cc002013-01-18 12:30:31 -08001032 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001033 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001034 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001035 DRM_ERROR("Couldn't idle GPU\n");
1036 /* Wait a bit, in hopes it avoids the hang */
1037 udelay(10);
1038 }
1039 }
1040
1041 return ret;
1042}
1043
1044static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1045{
Ben Widawskya81cc002013-01-18 12:30:31 -08001046 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001047 dev_priv->mm.interruptible = interruptible;
1048}
1049
Ben Widawsky828c7902013-10-16 09:21:30 -07001050void i915_check_and_clear_faults(struct drm_device *dev)
1051{
1052 struct drm_i915_private *dev_priv = dev->dev_private;
1053 struct intel_ring_buffer *ring;
1054 int i;
1055
1056 if (INTEL_INFO(dev)->gen < 6)
1057 return;
1058
1059 for_each_ring(ring, dev_priv, i) {
1060 u32 fault_reg;
1061 fault_reg = I915_READ(RING_FAULT_REG(ring));
1062 if (fault_reg & RING_FAULT_VALID) {
1063 DRM_DEBUG_DRIVER("Unexpected fault\n"
1064 "\tAddr: 0x%08lx\\n"
1065 "\tAddress space: %s\n"
1066 "\tSource ID: %d\n"
1067 "\tType: %d\n",
1068 fault_reg & PAGE_MASK,
1069 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1070 RING_FAULT_SRCID(fault_reg),
1071 RING_FAULT_FAULT_TYPE(fault_reg));
1072 I915_WRITE(RING_FAULT_REG(ring),
1073 fault_reg & ~RING_FAULT_VALID);
1074 }
1075 }
1076 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1077}
1078
1079void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1080{
1081 struct drm_i915_private *dev_priv = dev->dev_private;
1082
1083 /* Don't bother messing with faults pre GEN6 as we have little
1084 * documentation supporting that it's a good idea.
1085 */
1086 if (INTEL_INFO(dev)->gen < 6)
1087 return;
1088
1089 i915_check_and_clear_faults(dev);
1090
1091 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1092 dev_priv->gtt.base.start / PAGE_SIZE,
1093 dev_priv->gtt.base.total / PAGE_SIZE,
1094 false);
1095}
1096
Daniel Vetter76aaf222010-11-05 22:23:30 +01001097void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1098{
1099 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001100 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001101 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001102
Ben Widawsky828c7902013-10-16 09:21:30 -07001103 i915_check_and_clear_faults(dev);
1104
Chris Wilsonbee4a182011-01-21 10:54:32 +00001105 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001106 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1107 dev_priv->gtt.base.start / PAGE_SIZE,
Ben Widawsky828c7902013-10-16 09:21:30 -07001108 dev_priv->gtt.base.total / PAGE_SIZE,
1109 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001110
Ben Widawsky35c20a62013-05-31 11:28:48 -07001111 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001112 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1113 &dev_priv->gtt.base);
1114 if (!vma)
1115 continue;
1116
Chris Wilson2c225692013-08-09 12:26:45 +01001117 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001118 /* The bind_vma code tries to be smart about tracking mappings.
1119 * Unfortunately above, we've just wiped out the mappings
1120 * without telling our object about it. So we need to fake it.
1121 */
1122 obj->has_global_gtt_mapping = 0;
1123 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001124 }
1125
Ben Widawsky80da2162013-12-06 14:11:17 -08001126
1127 if (INTEL_INFO(dev)->gen >= 8)
1128 return;
1129
1130 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1131 /* TODO: Perhaps it shouldn't be gen6 specific */
1132 if (i915_is_ggtt(vm)) {
1133 if (dev_priv->mm.aliasing_ppgtt)
1134 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1135 continue;
1136 }
1137
1138 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
1139 }
Ben Widawsky9f273d42013-12-06 14:11:16 -08001140
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001141 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001142}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001143
Daniel Vetter74163902012-02-15 23:50:21 +01001144int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001145{
Chris Wilson9da3da62012-06-01 15:20:22 +01001146 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001147 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001148
1149 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1150 obj->pages->sgl, obj->pages->nents,
1151 PCI_DMA_BIDIRECTIONAL))
1152 return -ENOSPC;
1153
1154 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001155}
1156
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001157static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1158{
1159#ifdef writeq
1160 writeq(pte, addr);
1161#else
1162 iowrite32((u32)pte, addr);
1163 iowrite32(pte >> 32, addr + 4);
1164#endif
1165}
1166
1167static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1168 struct sg_table *st,
1169 unsigned int first_entry,
1170 enum i915_cache_level level)
1171{
1172 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1173 gen8_gtt_pte_t __iomem *gtt_entries =
1174 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1175 int i = 0;
1176 struct sg_page_iter sg_iter;
1177 dma_addr_t addr;
1178
1179 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1180 addr = sg_dma_address(sg_iter.sg) +
1181 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1182 gen8_set_pte(&gtt_entries[i],
1183 gen8_pte_encode(addr, level, true));
1184 i++;
1185 }
1186
1187 /*
1188 * XXX: This serves as a posting read to make sure that the PTE has
1189 * actually been updated. There is some concern that even though
1190 * registers and PTEs are within the same BAR that they are potentially
1191 * of NUMA access patterns. Therefore, even with the way we assume
1192 * hardware should work, we must keep this posting read for paranoia.
1193 */
1194 if (i != 0)
1195 WARN_ON(readq(&gtt_entries[i-1])
1196 != gen8_pte_encode(addr, level, true));
1197
1198#if 0 /* TODO: Still needed on GEN8? */
1199 /* This next bit makes the above posting read even more important. We
1200 * want to flush the TLBs only after we're certain all the PTE updates
1201 * have finished.
1202 */
1203 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1204 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1205#endif
1206}
1207
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001208/*
1209 * Binds an object into the global gtt with the specified cache level. The object
1210 * will be accessible to the GPU via commands whose operands reference offsets
1211 * within the global GTT as well as accessible by the GPU through the GMADR
1212 * mapped BAR (dev_priv->mm.gtt->gtt).
1213 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001214static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001215 struct sg_table *st,
1216 unsigned int first_entry,
1217 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001218{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001219 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001220 gen6_gtt_pte_t __iomem *gtt_entries =
1221 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001222 int i = 0;
1223 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001224 dma_addr_t addr;
1225
Imre Deak6e995e22013-02-18 19:28:04 +02001226 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001227 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -07001228 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001229 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001230 }
1231
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001232 /* XXX: This serves as a posting read to make sure that the PTE has
1233 * actually been updated. There is some concern that even though
1234 * registers and PTEs are within the same BAR that they are potentially
1235 * of NUMA access patterns. Therefore, even with the way we assume
1236 * hardware should work, we must keep this posting read for paranoia.
1237 */
1238 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001239 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -07001240 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001241
1242 /* This next bit makes the above posting read even more important. We
1243 * want to flush the TLBs only after we're certain all the PTE updates
1244 * have finished.
1245 */
1246 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1247 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001248}
1249
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001250static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1251 unsigned int first_entry,
1252 unsigned int num_entries,
1253 bool use_scratch)
1254{
1255 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1256 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1257 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1258 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1259 int i;
1260
1261 if (WARN(num_entries > max_entries,
1262 "First entry = %d; Num entries = %d (max=%d)\n",
1263 first_entry, num_entries, max_entries))
1264 num_entries = max_entries;
1265
1266 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1267 I915_CACHE_LLC,
1268 use_scratch);
1269 for (i = 0; i < num_entries; i++)
1270 gen8_set_pte(&gtt_base[i], scratch_pte);
1271 readl(gtt_base);
1272}
1273
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001274static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001275 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -07001276 unsigned int num_entries,
1277 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001278{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001279 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001280 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1281 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001282 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001283 int i;
1284
1285 if (WARN(num_entries > max_entries,
1286 "First entry = %d; Num entries = %d (max=%d)\n",
1287 first_entry, num_entries, max_entries))
1288 num_entries = max_entries;
1289
Ben Widawsky828c7902013-10-16 09:21:30 -07001290 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
1291
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001292 for (i = 0; i < num_entries; i++)
1293 iowrite32(scratch_pte, &gtt_base[i]);
1294 readl(gtt_base);
1295}
1296
Ben Widawsky6f65e292013-12-06 14:10:56 -08001297
1298static void i915_ggtt_bind_vma(struct i915_vma *vma,
1299 enum i915_cache_level cache_level,
1300 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001301{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001302 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001303 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1304 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1305
Ben Widawsky6f65e292013-12-06 14:10:56 -08001306 BUG_ON(!i915_is_ggtt(vma->vm));
1307 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1308 vma->obj->has_global_gtt_mapping = 1;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001309}
1310
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001311static void i915_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001312 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -07001313 unsigned int num_entries,
1314 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001315{
1316 intel_gtt_clear_range(first_entry, num_entries);
1317}
1318
Ben Widawsky6f65e292013-12-06 14:10:56 -08001319static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001320{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001321 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1322 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001323
Ben Widawsky6f65e292013-12-06 14:10:56 -08001324 BUG_ON(!i915_is_ggtt(vma->vm));
1325 vma->obj->has_global_gtt_mapping = 0;
1326 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001327}
1328
Ben Widawsky6f65e292013-12-06 14:10:56 -08001329static void ggtt_bind_vma(struct i915_vma *vma,
1330 enum i915_cache_level cache_level,
1331 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001332{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001333 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001334 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001335 struct drm_i915_gem_object *obj = vma->obj;
1336 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001337
Ben Widawsky6f65e292013-12-06 14:10:56 -08001338 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1339 * or we have a global mapping already but the cacheability flags have
1340 * changed, set the global PTEs.
1341 *
1342 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1343 * instead if none of the above hold true.
1344 *
1345 * NB: A global mapping should only be needed for special regions like
1346 * "gtt mappable", SNB errata, or if specified via special execbuf
1347 * flags. At all other times, the GPU will use the aliasing PPGTT.
1348 */
1349 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1350 if (!obj->has_global_gtt_mapping ||
1351 (cache_level != obj->cache_level)) {
1352 vma->vm->insert_entries(vma->vm, obj->pages, entry,
1353 cache_level);
1354 obj->has_global_gtt_mapping = 1;
1355 }
1356 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001357
Ben Widawsky6f65e292013-12-06 14:10:56 -08001358 if (dev_priv->mm.aliasing_ppgtt &&
1359 (!obj->has_aliasing_ppgtt_mapping ||
1360 (cache_level != obj->cache_level))) {
1361 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1362 appgtt->base.insert_entries(&appgtt->base,
1363 vma->obj->pages, entry, cache_level);
1364 vma->obj->has_aliasing_ppgtt_mapping = 1;
1365 }
1366}
1367
1368static void ggtt_unbind_vma(struct i915_vma *vma)
1369{
1370 struct drm_device *dev = vma->vm->dev;
1371 struct drm_i915_private *dev_priv = dev->dev_private;
1372 struct drm_i915_gem_object *obj = vma->obj;
1373 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1374
1375 if (obj->has_global_gtt_mapping) {
1376 vma->vm->clear_range(vma->vm, entry,
1377 vma->obj->base.size >> PAGE_SHIFT,
1378 true);
1379 obj->has_global_gtt_mapping = 0;
1380 }
1381
1382 if (obj->has_aliasing_ppgtt_mapping) {
1383 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1384 appgtt->base.clear_range(&appgtt->base,
1385 entry,
1386 obj->base.size >> PAGE_SHIFT,
1387 true);
1388 obj->has_aliasing_ppgtt_mapping = 0;
1389 }
Daniel Vetter74163902012-02-15 23:50:21 +01001390}
1391
1392void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1393{
Ben Widawsky5c042282011-10-17 15:51:55 -07001394 struct drm_device *dev = obj->base.dev;
1395 struct drm_i915_private *dev_priv = dev->dev_private;
1396 bool interruptible;
1397
1398 interruptible = do_idling(dev_priv);
1399
Chris Wilson9da3da62012-06-01 15:20:22 +01001400 if (!obj->has_dma_mapping)
1401 dma_unmap_sg(&dev->pdev->dev,
1402 obj->pages->sgl, obj->pages->nents,
1403 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001404
1405 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001406}
Daniel Vetter644ec022012-03-26 09:45:40 +02001407
Chris Wilson42d6ab42012-07-26 11:49:32 +01001408static void i915_gtt_color_adjust(struct drm_mm_node *node,
1409 unsigned long color,
1410 unsigned long *start,
1411 unsigned long *end)
1412{
1413 if (node->color != color)
1414 *start += 4096;
1415
1416 if (!list_empty(&node->node_list)) {
1417 node = list_entry(node->node_list.next,
1418 struct drm_mm_node,
1419 node_list);
1420 if (node->allocated && node->color != color)
1421 *end -= 4096;
1422 }
1423}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001424
Ben Widawskyd7e50082012-12-18 10:31:25 -08001425void i915_gem_setup_global_gtt(struct drm_device *dev,
1426 unsigned long start,
1427 unsigned long mappable_end,
1428 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001429{
Ben Widawskye78891c2013-01-25 16:41:04 -08001430 /* Let GEM Manage all of the aperture.
1431 *
1432 * However, leave one page at the end still bound to the scratch page.
1433 * There are a number of places where the hardware apparently prefetches
1434 * past the end of the object, and we've seen multiple hangs with the
1435 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1436 * aperture. One page should be enough to keep any prefetching inside
1437 * of the aperture.
1438 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001439 struct drm_i915_private *dev_priv = dev->dev_private;
1440 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001441 struct drm_mm_node *entry;
1442 struct drm_i915_gem_object *obj;
1443 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +02001444
Ben Widawsky35451cb2013-01-17 12:45:13 -08001445 BUG_ON(mappable_end > end);
1446
Chris Wilsoned2f3452012-11-15 11:32:19 +00001447 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001448 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001449 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001450 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001451
Chris Wilsoned2f3452012-11-15 11:32:19 +00001452 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001453 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001454 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001455 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -07001456 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001457 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001458
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001459 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001460 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001461 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001462 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +00001463 obj->has_global_gtt_mapping = 1;
1464 }
1465
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001466 dev_priv->gtt.base.start = start;
1467 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001468
Chris Wilsoned2f3452012-11-15 11:32:19 +00001469 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001470 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001471 const unsigned long count = (hole_end - hole_start) / PAGE_SIZE;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001472 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1473 hole_start, hole_end);
Ben Widawsky828c7902013-10-16 09:21:30 -07001474 ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001475 }
1476
1477 /* And finally clear the reserved guard page */
Ben Widawsky828c7902013-10-16 09:21:30 -07001478 ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001479}
1480
Ben Widawskyd7e50082012-12-18 10:31:25 -08001481void i915_gem_init_global_gtt(struct drm_device *dev)
1482{
1483 struct drm_i915_private *dev_priv = dev->dev_private;
1484 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001485
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001486 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001487 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001488
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001489 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001490}
1491
1492static int setup_scratch_page(struct drm_device *dev)
1493{
1494 struct drm_i915_private *dev_priv = dev->dev_private;
1495 struct page *page;
1496 dma_addr_t dma_addr;
1497
1498 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1499 if (page == NULL)
1500 return -ENOMEM;
1501 get_page(page);
1502 set_pages_uc(page, 1);
1503
1504#ifdef CONFIG_INTEL_IOMMU
1505 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1506 PCI_DMA_BIDIRECTIONAL);
1507 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1508 return -EINVAL;
1509#else
1510 dma_addr = page_to_phys(page);
1511#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001512 dev_priv->gtt.base.scratch.page = page;
1513 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001514
1515 return 0;
1516}
1517
1518static void teardown_scratch_page(struct drm_device *dev)
1519{
1520 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001521 struct page *page = dev_priv->gtt.base.scratch.page;
1522
1523 set_pages_wb(page, 1);
1524 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001525 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001526 put_page(page);
1527 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001528}
1529
1530static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1531{
1532 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1533 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1534 return snb_gmch_ctl << 20;
1535}
1536
Ben Widawsky9459d252013-11-03 16:53:55 -08001537static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1538{
1539 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1540 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1541 if (bdw_gmch_ctl)
1542 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky3a2ffb62013-11-07 21:40:51 -08001543 if (bdw_gmch_ctl > 4) {
1544 WARN_ON(!i915_preliminary_hw_support);
1545 return 4<<20;
1546 }
1547
Ben Widawsky9459d252013-11-03 16:53:55 -08001548 return bdw_gmch_ctl << 20;
1549}
1550
Ben Widawskybaa09f52013-01-24 13:49:57 -08001551static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001552{
1553 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1554 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1555 return snb_gmch_ctl << 25; /* 32 MB units */
1556}
1557
Ben Widawsky9459d252013-11-03 16:53:55 -08001558static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1559{
1560 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1561 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1562 return bdw_gmch_ctl << 25; /* 32 MB units */
1563}
1564
Ben Widawsky63340132013-11-04 19:32:22 -08001565static int ggtt_probe_common(struct drm_device *dev,
1566 size_t gtt_size)
1567{
1568 struct drm_i915_private *dev_priv = dev->dev_private;
1569 phys_addr_t gtt_bus_addr;
1570 int ret;
1571
1572 /* For Modern GENs the PTEs and register space are split in the BAR */
1573 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
1574 (pci_resource_len(dev->pdev, 0) / 2);
1575
1576 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
1577 if (!dev_priv->gtt.gsm) {
1578 DRM_ERROR("Failed to map the gtt page table\n");
1579 return -ENOMEM;
1580 }
1581
1582 ret = setup_scratch_page(dev);
1583 if (ret) {
1584 DRM_ERROR("Scratch setup failed\n");
1585 /* iounmap will also get called at remove, but meh */
1586 iounmap(dev_priv->gtt.gsm);
1587 }
1588
1589 return ret;
1590}
1591
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001592/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1593 * bits. When using advanced contexts each context stores its own PAT, but
1594 * writing this data shouldn't be harmful even in those cases. */
1595static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1596{
1597#define GEN8_PPAT_UC (0<<0)
1598#define GEN8_PPAT_WC (1<<0)
1599#define GEN8_PPAT_WT (2<<0)
1600#define GEN8_PPAT_WB (3<<0)
1601#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
1602/* FIXME(BDW): Bspec is completely confused about cache control bits. */
1603#define GEN8_PPAT_LLC (1<<2)
1604#define GEN8_PPAT_LLCELLC (2<<2)
1605#define GEN8_PPAT_LLCeLLC (3<<2)
1606#define GEN8_PPAT_AGE(x) (x<<4)
1607#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
1608 uint64_t pat;
1609
1610 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1611 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1612 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1613 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1614 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1615 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1616 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1617 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1618
1619 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1620 * write would work. */
1621 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1622 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1623}
1624
Ben Widawsky63340132013-11-04 19:32:22 -08001625static int gen8_gmch_probe(struct drm_device *dev,
1626 size_t *gtt_total,
1627 size_t *stolen,
1628 phys_addr_t *mappable_base,
1629 unsigned long *mappable_end)
1630{
1631 struct drm_i915_private *dev_priv = dev->dev_private;
1632 unsigned int gtt_size;
1633 u16 snb_gmch_ctl;
1634 int ret;
1635
1636 /* TODO: We're not aware of mappable constraints on gen8 yet */
1637 *mappable_base = pci_resource_start(dev->pdev, 2);
1638 *mappable_end = pci_resource_len(dev->pdev, 2);
1639
1640 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1641 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1642
1643 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1644
1645 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1646
1647 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskyd31eb102013-11-02 21:07:17 -07001648 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001649
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001650 gen8_setup_private_ppat(dev_priv);
1651
Ben Widawsky63340132013-11-04 19:32:22 -08001652 ret = ggtt_probe_common(dev, gtt_size);
1653
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001654 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1655 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001656
1657 return ret;
1658}
1659
Ben Widawskybaa09f52013-01-24 13:49:57 -08001660static int gen6_gmch_probe(struct drm_device *dev,
1661 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001662 size_t *stolen,
1663 phys_addr_t *mappable_base,
1664 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001665{
1666 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001667 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001668 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001669 int ret;
1670
Ben Widawsky41907dd2013-02-08 11:32:47 -08001671 *mappable_base = pci_resource_start(dev->pdev, 2);
1672 *mappable_end = pci_resource_len(dev->pdev, 2);
1673
Ben Widawskybaa09f52013-01-24 13:49:57 -08001674 /* 64/512MB is the current min/max we actually know of, but this is just
1675 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001676 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001677 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001678 DRM_ERROR("Unknown GMADR size (%lx)\n",
1679 dev_priv->gtt.mappable_end);
1680 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001681 }
1682
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001683 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1684 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001685 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001686
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07001687 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001688
Ben Widawsky63340132013-11-04 19:32:22 -08001689 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001690 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
1691
Ben Widawsky63340132013-11-04 19:32:22 -08001692 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001693
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001694 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1695 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001696
1697 return ret;
1698}
1699
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001700static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001701{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001702
1703 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08001704
1705 drm_mm_takedown(&vm->mm);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001706 iounmap(gtt->gsm);
1707 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001708}
1709
1710static int i915_gmch_probe(struct drm_device *dev,
1711 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001712 size_t *stolen,
1713 phys_addr_t *mappable_base,
1714 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001715{
1716 struct drm_i915_private *dev_priv = dev->dev_private;
1717 int ret;
1718
Ben Widawskybaa09f52013-01-24 13:49:57 -08001719 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1720 if (!ret) {
1721 DRM_ERROR("failed to set up gmch\n");
1722 return -EIO;
1723 }
1724
Ben Widawsky41907dd2013-02-08 11:32:47 -08001725 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001726
1727 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001728 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001729
1730 return 0;
1731}
1732
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001733static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001734{
1735 intel_gmch_remove();
1736}
1737
1738int i915_gem_gtt_init(struct drm_device *dev)
1739{
1740 struct drm_i915_private *dev_priv = dev->dev_private;
1741 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001742 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001743
Ben Widawskybaa09f52013-01-24 13:49:57 -08001744 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001745 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001746 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08001747 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001748 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001749 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001750 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001751 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001752 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001753 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001754 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001755 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001756 else if (INTEL_INFO(dev)->gen >= 7)
1757 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001758 else
Chris Wilson350ec882013-08-06 13:17:02 +01001759 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08001760 } else {
1761 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1762 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001763 }
1764
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001765 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001766 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001767 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001768 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001769
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001770 gtt->base.dev = dev;
1771
Ben Widawskybaa09f52013-01-24 13:49:57 -08001772 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001773 DRM_INFO("Memory usable by graphics device = %zdM\n",
1774 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001775 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1776 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001777
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001778 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001779}
Ben Widawsky6f65e292013-12-06 14:10:56 -08001780
1781static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
1782 struct i915_address_space *vm)
1783{
1784 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
1785 if (vma == NULL)
1786 return ERR_PTR(-ENOMEM);
1787
1788 INIT_LIST_HEAD(&vma->vma_link);
1789 INIT_LIST_HEAD(&vma->mm_list);
1790 INIT_LIST_HEAD(&vma->exec_list);
1791 vma->vm = vm;
1792 vma->obj = obj;
1793
1794 switch (INTEL_INFO(vm->dev)->gen) {
1795 case 8:
1796 case 7:
1797 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001798 if (i915_is_ggtt(vm)) {
1799 vma->unbind_vma = ggtt_unbind_vma;
1800 vma->bind_vma = ggtt_bind_vma;
1801 } else {
1802 vma->unbind_vma = ppgtt_unbind_vma;
1803 vma->bind_vma = ppgtt_bind_vma;
1804 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08001805 break;
1806 case 5:
1807 case 4:
1808 case 3:
1809 case 2:
1810 BUG_ON(!i915_is_ggtt(vm));
1811 vma->unbind_vma = i915_ggtt_unbind_vma;
1812 vma->bind_vma = i915_ggtt_bind_vma;
1813 break;
1814 default:
1815 BUG();
1816 }
1817
1818 /* Keep GGTT vmas first to make debug easier */
1819 if (i915_is_ggtt(vm))
1820 list_add(&vma->vma_link, &obj->vma_list);
1821 else
1822 list_add_tail(&vma->vma_link, &obj->vma_list);
1823
1824 return vma;
1825}
1826
1827struct i915_vma *
1828i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
1829 struct i915_address_space *vm)
1830{
1831 struct i915_vma *vma;
1832
1833 vma = i915_gem_obj_to_vma(obj, vm);
1834 if (!vma)
1835 vma = __i915_gem_vma_create(obj, vm);
1836
1837 return vma;
1838}