blob: 862ae371697a6ea1746c0c5abc2b1f13a8d1b90d [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))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -080067
68/* GEN8 legacy style addressis defined as a 3 level page table:
69 * 31:30 | 29:21 | 20:12 | 11:0
70 * PDPE | PDE | PTE | offset
71 * The difference as compared to normal x86 3 level page table is the PDPEs are
72 * programmed via register.
73 */
74#define GEN8_PDPE_SHIFT 30
75#define GEN8_PDPE_MASK 0x3
76#define GEN8_PDE_SHIFT 21
77#define GEN8_PDE_MASK 0x1ff
78#define GEN8_PTE_SHIFT 12
79#define GEN8_PTE_MASK 0x1ff
Ben Widawsky37aca442013-11-04 20:47:32 -080080
Ben Widawskyfbe5d362013-11-04 19:56:49 -080081#define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD)
82#define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */
83#define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */
84#define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */
85
Ben Widawsky6f65e292013-12-06 14:10:56 -080086static void ppgtt_bind_vma(struct i915_vma *vma,
87 enum i915_cache_level cache_level,
88 u32 flags);
89static void ppgtt_unbind_vma(struct i915_vma *vma);
Ben Widawskyeeb94882013-12-06 14:11:10 -080090static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt);
Ben Widawsky6f65e292013-12-06 14:10:56 -080091
Ben Widawsky94ec8f62013-11-02 21:07:18 -070092static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
93 enum i915_cache_level level,
94 bool valid)
95{
96 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
97 pte |= addr;
Ben Widawskyfbe5d362013-11-04 19:56:49 -080098 if (level != I915_CACHE_NONE)
99 pte |= PPAT_CACHED_INDEX;
100 else
101 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700102 return pte;
103}
104
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800105static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
106 dma_addr_t addr,
107 enum i915_cache_level level)
108{
109 gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
110 pde |= addr;
111 if (level != I915_CACHE_NONE)
112 pde |= PPAT_CACHED_PDE_INDEX;
113 else
114 pde |= PPAT_UNCACHED_INDEX;
115 return pde;
116}
117
Chris Wilson350ec882013-08-06 13:17:02 +0100118static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700119 enum i915_cache_level level,
120 bool valid)
Ben Widawsky54d12522012-09-24 16:44:32 -0700121{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700122 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700123 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700124
125 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100126 case I915_CACHE_L3_LLC:
127 case I915_CACHE_LLC:
128 pte |= GEN6_PTE_CACHE_LLC;
129 break;
130 case I915_CACHE_NONE:
131 pte |= GEN6_PTE_UNCACHED;
132 break;
133 default:
134 WARN_ON(1);
135 }
136
137 return pte;
138}
139
140static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700141 enum i915_cache_level level,
142 bool valid)
Chris Wilson350ec882013-08-06 13:17:02 +0100143{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700144 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100145 pte |= GEN6_PTE_ADDR_ENCODE(addr);
146
147 switch (level) {
148 case I915_CACHE_L3_LLC:
149 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700150 break;
151 case I915_CACHE_LLC:
152 pte |= GEN6_PTE_CACHE_LLC;
153 break;
154 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700155 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700156 break;
157 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100158 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700159 }
160
Ben Widawsky54d12522012-09-24 16:44:32 -0700161 return pte;
162}
163
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700164#define BYT_PTE_WRITEABLE (1 << 1)
165#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
166
Ben Widawsky80a74f72013-06-27 16:30:19 -0700167static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700168 enum i915_cache_level level,
169 bool valid)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700170{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700171 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700172 pte |= GEN6_PTE_ADDR_ENCODE(addr);
173
174 /* Mark the page as writeable. Other platforms don't have a
175 * setting for read-only/writable, so this matches that behavior.
176 */
177 pte |= BYT_PTE_WRITEABLE;
178
179 if (level != I915_CACHE_NONE)
180 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
181
182 return pte;
183}
184
Ben Widawsky80a74f72013-06-27 16:30:19 -0700185static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700186 enum i915_cache_level level,
187 bool valid)
Kenneth Graunke91197082013-04-22 00:53:51 -0700188{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700189 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700190 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700191
192 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700193 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700194
195 return pte;
196}
197
Ben Widawsky4d15c142013-07-04 11:02:06 -0700198static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700199 enum i915_cache_level level,
200 bool valid)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700201{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700202 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700203 pte |= HSW_PTE_ADDR_ENCODE(addr);
204
Chris Wilson651d7942013-08-08 14:41:10 +0100205 switch (level) {
206 case I915_CACHE_NONE:
207 break;
208 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000209 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100210 break;
211 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000212 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100213 break;
214 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700215
216 return pte;
217}
218
Ben Widawsky94e409c2013-11-04 22:29:36 -0800219/* Broadwell Page Directory Pointer Descriptors */
220static int gen8_write_pdp(struct intel_ring_buffer *ring, unsigned entry,
Ben Widawskye178f702013-12-06 14:10:47 -0800221 uint64_t val, bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800222{
Ben Widawskye178f702013-12-06 14:10:47 -0800223 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800224 int ret;
225
226 BUG_ON(entry >= 4);
227
Ben Widawskye178f702013-12-06 14:10:47 -0800228 if (synchronous) {
229 I915_WRITE(GEN8_RING_PDP_UDW(ring, entry), val >> 32);
230 I915_WRITE(GEN8_RING_PDP_LDW(ring, entry), (u32)val);
231 return 0;
232 }
233
Ben Widawsky94e409c2013-11-04 22:29:36 -0800234 ret = intel_ring_begin(ring, 6);
235 if (ret)
236 return ret;
237
238 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
239 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
240 intel_ring_emit(ring, (u32)(val >> 32));
241 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
242 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
243 intel_ring_emit(ring, (u32)(val));
244 intel_ring_advance(ring);
245
246 return 0;
247}
248
Ben Widawskyeeb94882013-12-06 14:11:10 -0800249static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
250 struct intel_ring_buffer *ring,
251 bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800252{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800253 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800254
255 /* bit of a hack to find the actual last used pd */
256 int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
257
Ben Widawsky94e409c2013-11-04 22:29:36 -0800258 for (i = used_pd - 1; i >= 0; i--) {
259 dma_addr_t addr = ppgtt->pd_dma_addr[i];
Ben Widawskyeeb94882013-12-06 14:11:10 -0800260 ret = gen8_write_pdp(ring, i, addr, synchronous);
261 if (ret)
262 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800263 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800264
Ben Widawskyeeb94882013-12-06 14:11:10 -0800265 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800266}
267
Ben Widawsky459108b2013-11-02 21:07:23 -0700268static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800269 uint64_t start,
270 uint64_t length,
Ben Widawsky459108b2013-11-02 21:07:23 -0700271 bool use_scratch)
272{
273 struct i915_hw_ppgtt *ppgtt =
274 container_of(vm, struct i915_hw_ppgtt, base);
275 gen8_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800276 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
277 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
278 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800279 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700280 unsigned last_pte, i;
281
282 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
283 I915_CACHE_LLC, use_scratch);
284
285 while (num_entries) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800286 struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde];
Ben Widawsky459108b2013-11-02 21:07:23 -0700287
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800288 last_pte = pte + num_entries;
Ben Widawsky459108b2013-11-02 21:07:23 -0700289 if (last_pte > GEN8_PTES_PER_PAGE)
290 last_pte = GEN8_PTES_PER_PAGE;
291
292 pt_vaddr = kmap_atomic(page_table);
293
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800294 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700295 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800296 num_entries--;
297 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700298
299 kunmap_atomic(pt_vaddr);
300
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800301 pte = 0;
302 if (++pde == GEN8_PDES_PER_PAGE) {
303 pdpe++;
304 pde = 0;
305 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700306 }
307}
308
Ben Widawsky9df15b42013-11-02 21:07:24 -0700309static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
310 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800311 uint64_t start,
Ben Widawsky9df15b42013-11-02 21:07:24 -0700312 enum i915_cache_level cache_level)
313{
314 struct i915_hw_ppgtt *ppgtt =
315 container_of(vm, struct i915_hw_ppgtt, base);
316 gen8_gtt_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800317 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
318 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
319 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700320 struct sg_page_iter sg_iter;
321
Chris Wilson6f1cc992013-12-31 15:50:31 +0000322 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700323
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800324 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
325 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS))
326 break;
327
328 if (pt_vaddr == NULL)
329 pt_vaddr = kmap_atomic(ppgtt->gen8_pt_pages[pdpe][pde]);
330
331 pt_vaddr[pte] =
Chris Wilson6f1cc992013-12-31 15:50:31 +0000332 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
333 cache_level, true);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800334 if (++pte == GEN8_PTES_PER_PAGE) {
Ben Widawsky9df15b42013-11-02 21:07:24 -0700335 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000336 pt_vaddr = NULL;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800337 if (++pde == GEN8_PDES_PER_PAGE) {
338 pdpe++;
339 pde = 0;
340 }
341 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700342 }
343 }
Chris Wilson6f1cc992013-12-31 15:50:31 +0000344 if (pt_vaddr)
345 kunmap_atomic(pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700346}
347
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800348static void gen8_free_page_tables(struct page **pt_pages)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800349{
350 int i;
351
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800352 if (pt_pages == NULL)
353 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800354
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800355 for (i = 0; i < GEN8_PDES_PER_PAGE; i++)
356 if (pt_pages[i])
357 __free_pages(pt_pages[i], 0);
358}
359
360static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt)
361{
362 int i;
363
364 for (i = 0; i < ppgtt->num_pd_pages; i++) {
365 gen8_free_page_tables(ppgtt->gen8_pt_pages[i]);
366 kfree(ppgtt->gen8_pt_pages[i]);
367 kfree(ppgtt->gen8_pt_dma_addr[i]);
368 }
369
Ben Widawskyb45a6712014-02-12 14:28:44 -0800370 __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
371}
372
373static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
374{
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800375 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800376 int i, j;
377
378 for (i = 0; i < ppgtt->num_pd_pages; i++) {
379 /* TODO: In the future we'll support sparse mappings, so this
380 * will have to change. */
381 if (!ppgtt->pd_dma_addr[i])
382 continue;
383
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800384 pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
385 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800386
387 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
388 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
389 if (addr)
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800390 pci_unmap_page(hwdev, addr, PAGE_SIZE,
391 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800392 }
393 }
394}
395
Ben Widawsky37aca442013-11-04 20:47:32 -0800396static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
397{
398 struct i915_hw_ppgtt *ppgtt =
399 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800400
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800401 list_del(&vm->global_link);
Ben Widawsky686e1f62013-11-25 09:54:34 -0800402 drm_mm_takedown(&vm->mm);
403
Ben Widawskyb45a6712014-02-12 14:28:44 -0800404 gen8_ppgtt_unmap_pages(ppgtt);
405 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800406}
407
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800408static struct page **__gen8_alloc_page_tables(void)
409{
410 struct page **pt_pages;
411 int i;
412
413 pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL);
414 if (!pt_pages)
415 return ERR_PTR(-ENOMEM);
416
417 for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
418 pt_pages[i] = alloc_page(GFP_KERNEL);
419 if (!pt_pages[i])
420 goto bail;
421 }
422
423 return pt_pages;
424
425bail:
426 gen8_free_page_tables(pt_pages);
427 kfree(pt_pages);
428 return ERR_PTR(-ENOMEM);
429}
430
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800431static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
432 const int max_pdp)
433{
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800434 struct page **pt_pages[GEN8_LEGACY_PDPS];
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800435 const int num_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800436 int i, ret;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800437
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800438 for (i = 0; i < max_pdp; i++) {
439 pt_pages[i] = __gen8_alloc_page_tables();
440 if (IS_ERR(pt_pages[i])) {
441 ret = PTR_ERR(pt_pages[i]);
442 goto unwind_out;
443 }
444 }
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800445
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800446 /* NB: Avoid touching gen8_pt_pages until last to keep the allocation,
447 * "atomic" - for cleanup purposes.
448 */
449 for (i = 0; i < max_pdp; i++)
450 ppgtt->gen8_pt_pages[i] = pt_pages[i];
451
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800452 ppgtt->num_pt_pages = 1 << get_order(num_pt_pages << PAGE_SHIFT);
453
454 return 0;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800455
456unwind_out:
457 while (i--) {
458 gen8_free_page_tables(pt_pages[i]);
459 kfree(pt_pages[i]);
460 }
461
462 return ret;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800463}
464
465static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
466{
467 int i;
468
469 for (i = 0; i < ppgtt->num_pd_pages; i++) {
470 ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
471 sizeof(dma_addr_t),
472 GFP_KERNEL);
473 if (!ppgtt->gen8_pt_dma_addr[i])
474 return -ENOMEM;
475 }
476
477 return 0;
478}
479
480static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
481 const int max_pdp)
482{
483 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
484 if (!ppgtt->pd_pages)
485 return -ENOMEM;
486
487 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
488 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
489
490 return 0;
491}
492
493static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
494 const int max_pdp)
495{
496 int ret;
497
498 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
499 if (ret)
500 return ret;
501
502 ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
503 if (ret) {
504 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
505 return ret;
506 }
507
508 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
509
510 ret = gen8_ppgtt_allocate_dma(ppgtt);
511 if (ret)
512 gen8_ppgtt_free(ppgtt);
513
514 return ret;
515}
516
517static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
518 const int pd)
519{
520 dma_addr_t pd_addr;
521 int ret;
522
523 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
524 &ppgtt->pd_pages[pd], 0,
525 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
526
527 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
528 if (ret)
529 return ret;
530
531 ppgtt->pd_dma_addr[pd] = pd_addr;
532
533 return 0;
534}
535
536static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
537 const int pd,
538 const int pt)
539{
540 dma_addr_t pt_addr;
541 struct page *p;
542 int ret;
543
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800544 p = ppgtt->gen8_pt_pages[pd][pt];
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800545 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
546 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
547 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
548 if (ret)
549 return ret;
550
551 ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
552
553 return 0;
554}
555
Ben Widawsky37aca442013-11-04 20:47:32 -0800556/**
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800557 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
558 * with a net effect resembling a 2-level page table in normal x86 terms. Each
559 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
560 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800561 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800562 * FIXME: split allocation into smaller pieces. For now we only ever do this
563 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
Ben Widawsky37aca442013-11-04 20:47:32 -0800564 * TODO: Do something with the size parameter
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800565 */
Ben Widawsky37aca442013-11-04 20:47:32 -0800566static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
567{
Ben Widawsky37aca442013-11-04 20:47:32 -0800568 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800569 const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800570 int i, j, ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800571
572 if (size % (1<<30))
573 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
574
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800575 /* 1. Do all our allocations for page directories and page tables. */
576 ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
577 if (ret)
578 return ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800579
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800580 /*
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800581 * 2. Create DMA mappings for the page directories and page tables.
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800582 */
583 for (i = 0; i < max_pdp; i++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800584 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800585 if (ret)
586 goto bail;
587
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800588 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800589 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800590 if (ret)
591 goto bail;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800592 }
593 }
594
595 /*
596 * 3. Map all the page directory entires to point to the page tables
597 * we've allocated.
598 *
599 * For now, the PPGTT helper functions all require that the PDEs are
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800600 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800601 * will never need to touch the PDEs again.
602 */
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800603 for (i = 0; i < max_pdp; i++) {
604 gen8_ppgtt_pde_t *pd_vaddr;
605 pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
606 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
607 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
608 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
609 I915_CACHE_LLC);
610 }
611 kunmap_atomic(pd_vaddr);
612 }
613
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800614 ppgtt->enable = gen8_ppgtt_enable;
615 ppgtt->switch_mm = gen8_mm_switch;
616 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
617 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
618 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
619 ppgtt->base.start = 0;
620 ppgtt->base.total = ppgtt->num_pt_pages * GEN8_PTES_PER_PAGE * PAGE_SIZE;
621
Ben Widawsky459108b2013-11-02 21:07:23 -0700622 ppgtt->base.clear_range(&ppgtt->base, 0,
Ben Widawsky782f1492014-02-20 11:50:33 -0800623 ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE,
Ben Widawsky459108b2013-11-02 21:07:23 -0700624 true);
625
Ben Widawsky37aca442013-11-04 20:47:32 -0800626 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
627 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
628 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
629 ppgtt->num_pt_pages,
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800630 (ppgtt->num_pt_pages - min_pt_pages) +
Ben Widawsky37aca442013-11-04 20:47:32 -0800631 size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700632 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800633
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800634bail:
635 gen8_ppgtt_unmap_pages(ppgtt);
636 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800637 return ret;
638}
639
Ben Widawsky87d60b62013-12-06 14:11:29 -0800640static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
641{
642 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
643 struct i915_address_space *vm = &ppgtt->base;
644 gen6_gtt_pte_t __iomem *pd_addr;
645 gen6_gtt_pte_t scratch_pte;
646 uint32_t pd_entry;
647 int pte, pde;
648
649 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
650
651 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
652 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
653
654 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
655 ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
656 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
657 u32 expected;
658 gen6_gtt_pte_t *pt_vaddr;
659 dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
660 pd_entry = readl(pd_addr + pde);
661 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
662
663 if (pd_entry != expected)
664 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
665 pde,
666 pd_entry,
667 expected);
668 seq_printf(m, "\tPDE: %x\n", pd_entry);
669
670 pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
671 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
672 unsigned long va =
673 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
674 (pte * PAGE_SIZE);
675 int i;
676 bool found = false;
677 for (i = 0; i < 4; i++)
678 if (pt_vaddr[pte + i] != scratch_pte)
679 found = true;
680 if (!found)
681 continue;
682
683 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
684 for (i = 0; i < 4; i++) {
685 if (pt_vaddr[pte + i] != scratch_pte)
686 seq_printf(m, " %08x", pt_vaddr[pte + i]);
687 else
688 seq_puts(m, " SCRATCH ");
689 }
690 seq_puts(m, "\n");
691 }
692 kunmap_atomic(pt_vaddr);
693 }
694}
695
Ben Widawsky3e302542013-04-23 23:15:32 -0700696static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700697{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700698 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700699 gen6_gtt_pte_t __iomem *pd_addr;
700 uint32_t pd_entry;
701 int i;
702
Ben Widawsky0a732872013-04-23 23:15:30 -0700703 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700704 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
705 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
706 for (i = 0; i < ppgtt->num_pd_entries; i++) {
707 dma_addr_t pt_addr;
708
709 pt_addr = ppgtt->pt_dma_addr[i];
710 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
711 pd_entry |= GEN6_PDE_VALID;
712
713 writel(pd_entry, pd_addr + i);
714 }
715 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700716}
717
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800718static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700719{
Ben Widawsky3e302542013-04-23 23:15:32 -0700720 BUG_ON(ppgtt->pd_offset & 0x3f);
721
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800722 return (ppgtt->pd_offset / 64) << 16;
723}
Ben Widawsky61973492013-04-08 18:43:54 -0700724
Ben Widawsky90252e52013-12-06 14:11:12 -0800725static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
726 struct intel_ring_buffer *ring,
727 bool synchronous)
728{
729 struct drm_device *dev = ppgtt->base.dev;
730 struct drm_i915_private *dev_priv = dev->dev_private;
731 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700732
Ben Widawsky90252e52013-12-06 14:11:12 -0800733 /* If we're in reset, we can assume the GPU is sufficiently idle to
734 * manually frob these bits. Ideally we could use the ring functions,
735 * except our error handling makes it quite difficult (can't use
736 * intel_ring_begin, ring->flush, or intel_ring_advance)
737 *
738 * FIXME: We should try not to special case reset
739 */
740 if (synchronous ||
741 i915_reset_in_progress(&dev_priv->gpu_error)) {
742 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
743 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
744 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
745 POSTING_READ(RING_PP_DIR_BASE(ring));
746 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700747 }
748
Ben Widawsky90252e52013-12-06 14:11:12 -0800749 /* NB: TLBs must be flushed and invalidated before a switch */
750 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
751 if (ret)
752 return ret;
753
754 ret = intel_ring_begin(ring, 6);
755 if (ret)
756 return ret;
757
758 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
759 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
760 intel_ring_emit(ring, PP_DIR_DCLV_2G);
761 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
762 intel_ring_emit(ring, get_pd_offset(ppgtt));
763 intel_ring_emit(ring, MI_NOOP);
764 intel_ring_advance(ring);
765
766 return 0;
767}
768
Ben Widawsky48a10382013-12-06 14:11:11 -0800769static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
770 struct intel_ring_buffer *ring,
771 bool synchronous)
772{
773 struct drm_device *dev = ppgtt->base.dev;
774 struct drm_i915_private *dev_priv = dev->dev_private;
775 int ret;
776
777 /* If we're in reset, we can assume the GPU is sufficiently idle to
778 * manually frob these bits. Ideally we could use the ring functions,
779 * except our error handling makes it quite difficult (can't use
780 * intel_ring_begin, ring->flush, or intel_ring_advance)
781 *
782 * FIXME: We should try not to special case reset
783 */
784 if (synchronous ||
785 i915_reset_in_progress(&dev_priv->gpu_error)) {
786 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
787 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
788 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
789 POSTING_READ(RING_PP_DIR_BASE(ring));
790 return 0;
791 }
792
793 /* NB: TLBs must be flushed and invalidated before a switch */
794 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
795 if (ret)
796 return ret;
797
798 ret = intel_ring_begin(ring, 6);
799 if (ret)
800 return ret;
801
802 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
803 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
804 intel_ring_emit(ring, PP_DIR_DCLV_2G);
805 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
806 intel_ring_emit(ring, get_pd_offset(ppgtt));
807 intel_ring_emit(ring, MI_NOOP);
808 intel_ring_advance(ring);
809
Ben Widawsky90252e52013-12-06 14:11:12 -0800810 /* XXX: RCS is the only one to auto invalidate the TLBs? */
811 if (ring->id != RCS) {
812 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
813 if (ret)
814 return ret;
815 }
816
Ben Widawsky48a10382013-12-06 14:11:11 -0800817 return 0;
818}
819
Ben Widawskyeeb94882013-12-06 14:11:10 -0800820static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
821 struct intel_ring_buffer *ring,
822 bool synchronous)
823{
824 struct drm_device *dev = ppgtt->base.dev;
825 struct drm_i915_private *dev_priv = dev->dev_private;
826
Ben Widawsky48a10382013-12-06 14:11:11 -0800827 if (!synchronous)
828 return 0;
829
Ben Widawskyeeb94882013-12-06 14:11:10 -0800830 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
831 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
832
833 POSTING_READ(RING_PP_DIR_DCLV(ring));
834
835 return 0;
836}
837
838static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
839{
840 struct drm_device *dev = ppgtt->base.dev;
841 struct drm_i915_private *dev_priv = dev->dev_private;
842 struct intel_ring_buffer *ring;
843 int j, ret;
844
845 for_each_ring(ring, dev_priv, j) {
846 I915_WRITE(RING_MODE_GEN7(ring),
847 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800848
849 /* We promise to do a switch later with FULL PPGTT. If this is
850 * aliasing, this is the one and only switch we'll do */
851 if (USES_FULL_PPGTT(dev))
852 continue;
853
Ben Widawskyeeb94882013-12-06 14:11:10 -0800854 ret = ppgtt->switch_mm(ppgtt, ring, true);
855 if (ret)
856 goto err_out;
857 }
858
859 return 0;
860
861err_out:
862 for_each_ring(ring, dev_priv, j)
863 I915_WRITE(RING_MODE_GEN7(ring),
864 _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
865 return ret;
866}
867
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800868static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
869{
870 struct drm_device *dev = ppgtt->base.dev;
871 drm_i915_private_t *dev_priv = dev->dev_private;
872 struct intel_ring_buffer *ring;
873 uint32_t ecochk, ecobits;
874 int i;
875
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800876 ecobits = I915_READ(GAC_ECO_BITS);
877 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
878
879 ecochk = I915_READ(GAM_ECOCHK);
880 if (IS_HASWELL(dev)) {
881 ecochk |= ECOCHK_PPGTT_WB_HSW;
882 } else {
883 ecochk |= ECOCHK_PPGTT_LLC_IVB;
884 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
885 }
886 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800887
Ben Widawsky61973492013-04-08 18:43:54 -0700888 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800889 int ret;
890 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800891 I915_WRITE(RING_MODE_GEN7(ring),
892 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700893
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800894 /* We promise to do a switch later with FULL PPGTT. If this is
895 * aliasing, this is the one and only switch we'll do */
896 if (USES_FULL_PPGTT(dev))
897 continue;
898
Ben Widawskyeeb94882013-12-06 14:11:10 -0800899 ret = ppgtt->switch_mm(ppgtt, ring, true);
900 if (ret)
901 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700902 }
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800903
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800904 return 0;
905}
906
Ben Widawskya3d67d22013-12-06 14:11:06 -0800907static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700908{
Ben Widawskya3d67d22013-12-06 14:11:06 -0800909 struct drm_device *dev = ppgtt->base.dev;
Ben Widawsky61973492013-04-08 18:43:54 -0700910 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700911 struct intel_ring_buffer *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800912 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -0700913 int i;
914
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800915 ecobits = I915_READ(GAC_ECO_BITS);
916 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
917 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700918
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800919 gab_ctl = I915_READ(GAB_CTL);
920 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700921
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800922 ecochk = I915_READ(GAM_ECOCHK);
923 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700924
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800925 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700926
927 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800928 int ret = ppgtt->switch_mm(ppgtt, ring, true);
929 if (ret)
930 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700931 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800932
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700933 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700934}
935
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100936/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700937static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800938 uint64_t start,
939 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -0700940 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100941{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700942 struct i915_hw_ppgtt *ppgtt =
943 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700944 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -0800945 unsigned first_entry = start >> PAGE_SHIFT;
946 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100947 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100948 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
949 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100950
Ben Widawskyb35b3802013-10-16 09:18:21 -0700951 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100952
Daniel Vetter7bddb012012-02-09 17:15:47 +0100953 while (num_entries) {
954 last_pte = first_pte + num_entries;
955 if (last_pte > I915_PPGTT_PT_ENTRIES)
956 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100957
Daniel Vettera15326a2013-03-19 23:48:39 +0100958 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100959
960 for (i = first_pte; i < last_pte; i++)
961 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100962
963 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100964
Daniel Vetter7bddb012012-02-09 17:15:47 +0100965 num_entries -= last_pte - first_pte;
966 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100967 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100968 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100969}
970
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700971static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800972 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800973 uint64_t start,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800974 enum i915_cache_level cache_level)
975{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700976 struct i915_hw_ppgtt *ppgtt =
977 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700978 gen6_gtt_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -0800979 unsigned first_entry = start >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100980 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200981 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
982 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800983
Chris Wilsoncc797142013-12-31 15:50:30 +0000984 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +0200985 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +0000986 if (pt_vaddr == NULL)
987 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800988
Chris Wilsoncc797142013-12-31 15:50:30 +0000989 pt_vaddr[act_pte] =
990 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
991 cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200992 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
993 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +0000994 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +0100995 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +0200996 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800997 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800998 }
Chris Wilsoncc797142013-12-31 15:50:30 +0000999 if (pt_vaddr)
1000 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001001}
1002
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001003static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001004{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001005 struct i915_hw_ppgtt *ppgtt =
1006 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -08001007 int i;
1008
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001009 list_del(&vm->global_link);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001010 drm_mm_takedown(&ppgtt->base.mm);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001011 drm_mm_remove_node(&ppgtt->node);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001012
Daniel Vetter3440d262013-01-24 13:49:56 -08001013 if (ppgtt->pt_dma_addr) {
1014 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001015 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -08001016 ppgtt->pt_dma_addr[i],
1017 4096, PCI_DMA_BIDIRECTIONAL);
1018 }
1019
1020 kfree(ppgtt->pt_dma_addr);
1021 for (i = 0; i < ppgtt->num_pd_entries; i++)
1022 __free_page(ppgtt->pt_pages[i]);
1023 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -08001024}
1025
1026static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1027{
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001028#define GEN6_PD_ALIGN (PAGE_SIZE * 16)
1029#define GEN6_PD_SIZE (GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001030 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001031 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001032 bool retried = false;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001033 int i, ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001034
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001035 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1036 * allocator works in address space sizes, so it's multiplied by page
1037 * size. We allocate at the top of the GTT to avoid fragmentation.
1038 */
1039 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -08001040alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001041 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1042 &ppgtt->node, GEN6_PD_SIZE,
1043 GEN6_PD_ALIGN, 0,
1044 0, dev_priv->gtt.base.total,
1045 DRM_MM_SEARCH_DEFAULT);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001046 if (ret == -ENOSPC && !retried) {
1047 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1048 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Daniel Vetterd47c3ea2014-02-14 14:01:18 +01001049 I915_CACHE_NONE, 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001050 if (ret)
1051 return ret;
1052
1053 retried = true;
1054 goto alloc;
1055 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001056
1057 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1058 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001059
Chris Wilson08c45262013-07-30 19:04:37 +01001060 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -07001061 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky48a10382013-12-06 14:11:11 -08001062 if (IS_GEN6(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001063 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -08001064 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001065 } else if (IS_HASWELL(dev)) {
1066 ppgtt->enable = gen7_ppgtt_enable;
1067 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001068 } else if (IS_GEN7(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001069 ppgtt->enable = gen7_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -08001070 ppgtt->switch_mm = gen7_mm_switch;
1071 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001072 BUG();
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001073 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1074 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1075 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
1076 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001077 ppgtt->base.start = 0;
1078 ppgtt->base.total = GEN6_PPGTT_PD_ENTRIES * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Daniel Vettera1e22652013-09-21 00:35:38 +02001079 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001080 GFP_KERNEL);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001081 if (!ppgtt->pt_pages) {
1082 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -08001083 return -ENOMEM;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001084 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001085
1086 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1087 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
1088 if (!ppgtt->pt_pages[i])
1089 goto err_pt_alloc;
1090 }
1091
Daniel Vettera1e22652013-09-21 00:35:38 +02001092 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001093 GFP_KERNEL);
1094 if (!ppgtt->pt_dma_addr)
1095 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001096
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001097 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1098 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +02001099
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001100 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
1101 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001102
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001103 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1104 ret = -EIO;
1105 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001106
Daniel Vetter211c5682012-04-10 17:29:17 +02001107 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -08001108 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001109 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001110
Ben Widawsky782f1492014-02-20 11:50:33 -08001111 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001112 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001113
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001114 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1115 ppgtt->node.size >> 20,
1116 ppgtt->node.start / PAGE_SIZE);
1117 ppgtt->pd_offset =
1118 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001119
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001120 return 0;
1121
1122err_pd_pin:
1123 if (ppgtt->pt_dma_addr) {
1124 for (i--; i >= 0; i--)
1125 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
1126 4096, PCI_DMA_BIDIRECTIONAL);
1127 }
1128err_pt_alloc:
1129 kfree(ppgtt->pt_dma_addr);
1130 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1131 if (ppgtt->pt_pages[i])
1132 __free_page(ppgtt->pt_pages[i]);
1133 }
1134 kfree(ppgtt->pt_pages);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001135 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -08001136
1137 return ret;
1138}
1139
Ben Widawsky246cbfb2013-12-06 14:11:14 -08001140int i915_gem_init_ppgtt(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001141{
1142 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd6660ad2013-12-06 14:11:13 -08001143 int ret = 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001144
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001145 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08001146
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001147 if (INTEL_INFO(dev)->gen < 8)
1148 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -07001149 else if (IS_GEN8(dev))
Ben Widawsky37aca442013-11-04 20:47:32 -08001150 ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001151 else
1152 BUG();
1153
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001154 if (!ret) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001155 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001156 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001157 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1158 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001159 i915_init_vm(dev_priv, &ppgtt->base);
1160 if (INTEL_INFO(dev)->gen < 8) {
Ben Widawsky9f273d42013-12-06 14:11:16 -08001161 gen6_write_pdes(ppgtt);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001162 DRM_DEBUG("Adding PPGTT at offset %x\n",
1163 ppgtt->pd_offset << 10);
1164 }
Ben Widawsky93bd8642013-07-16 16:50:06 -07001165 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001166
1167 return ret;
1168}
1169
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001170static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001171ppgtt_bind_vma(struct i915_vma *vma,
1172 enum i915_cache_level cache_level,
1173 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001174{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001175 WARN_ON(flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001176
Ben Widawsky782f1492014-02-20 11:50:33 -08001177 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
1178 cache_level);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001179}
1180
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001181static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001182{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001183 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001184 vma->node.start,
1185 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001186 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001187}
1188
Ben Widawskya81cc002013-01-18 12:30:31 -08001189extern int intel_iommu_gfx_mapped;
1190/* Certain Gen5 chipsets require require idling the GPU before
1191 * unmapping anything from the GTT when VT-d is enabled.
1192 */
1193static inline bool needs_idle_maps(struct drm_device *dev)
1194{
1195#ifdef CONFIG_INTEL_IOMMU
1196 /* Query intel_iommu to see if we need the workaround. Presumably that
1197 * was loaded first.
1198 */
1199 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1200 return true;
1201#endif
1202 return false;
1203}
1204
Ben Widawsky5c042282011-10-17 15:51:55 -07001205static bool do_idling(struct drm_i915_private *dev_priv)
1206{
1207 bool ret = dev_priv->mm.interruptible;
1208
Ben Widawskya81cc002013-01-18 12:30:31 -08001209 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001210 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001211 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001212 DRM_ERROR("Couldn't idle GPU\n");
1213 /* Wait a bit, in hopes it avoids the hang */
1214 udelay(10);
1215 }
1216 }
1217
1218 return ret;
1219}
1220
1221static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1222{
Ben Widawskya81cc002013-01-18 12:30:31 -08001223 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001224 dev_priv->mm.interruptible = interruptible;
1225}
1226
Ben Widawsky828c7902013-10-16 09:21:30 -07001227void i915_check_and_clear_faults(struct drm_device *dev)
1228{
1229 struct drm_i915_private *dev_priv = dev->dev_private;
1230 struct intel_ring_buffer *ring;
1231 int i;
1232
1233 if (INTEL_INFO(dev)->gen < 6)
1234 return;
1235
1236 for_each_ring(ring, dev_priv, i) {
1237 u32 fault_reg;
1238 fault_reg = I915_READ(RING_FAULT_REG(ring));
1239 if (fault_reg & RING_FAULT_VALID) {
1240 DRM_DEBUG_DRIVER("Unexpected fault\n"
1241 "\tAddr: 0x%08lx\\n"
1242 "\tAddress space: %s\n"
1243 "\tSource ID: %d\n"
1244 "\tType: %d\n",
1245 fault_reg & PAGE_MASK,
1246 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1247 RING_FAULT_SRCID(fault_reg),
1248 RING_FAULT_FAULT_TYPE(fault_reg));
1249 I915_WRITE(RING_FAULT_REG(ring),
1250 fault_reg & ~RING_FAULT_VALID);
1251 }
1252 }
1253 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1254}
1255
1256void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1257{
1258 struct drm_i915_private *dev_priv = dev->dev_private;
1259
1260 /* Don't bother messing with faults pre GEN6 as we have little
1261 * documentation supporting that it's a good idea.
1262 */
1263 if (INTEL_INFO(dev)->gen < 6)
1264 return;
1265
1266 i915_check_and_clear_faults(dev);
1267
1268 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001269 dev_priv->gtt.base.start,
1270 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001271 false);
1272}
1273
Daniel Vetter76aaf222010-11-05 22:23:30 +01001274void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1275{
1276 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001277 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001278 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001279
Ben Widawsky828c7902013-10-16 09:21:30 -07001280 i915_check_and_clear_faults(dev);
1281
Chris Wilsonbee4a182011-01-21 10:54:32 +00001282 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001283 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001284 dev_priv->gtt.base.start,
1285 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001286 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001287
Ben Widawsky35c20a62013-05-31 11:28:48 -07001288 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001289 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1290 &dev_priv->gtt.base);
1291 if (!vma)
1292 continue;
1293
Chris Wilson2c225692013-08-09 12:26:45 +01001294 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001295 /* The bind_vma code tries to be smart about tracking mappings.
1296 * Unfortunately above, we've just wiped out the mappings
1297 * without telling our object about it. So we need to fake it.
1298 */
1299 obj->has_global_gtt_mapping = 0;
1300 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001301 }
1302
Ben Widawsky80da2162013-12-06 14:11:17 -08001303
1304 if (INTEL_INFO(dev)->gen >= 8)
1305 return;
1306
1307 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1308 /* TODO: Perhaps it shouldn't be gen6 specific */
1309 if (i915_is_ggtt(vm)) {
1310 if (dev_priv->mm.aliasing_ppgtt)
1311 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1312 continue;
1313 }
1314
1315 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001316 }
1317
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001318 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001319}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001320
Daniel Vetter74163902012-02-15 23:50:21 +01001321int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001322{
Chris Wilson9da3da62012-06-01 15:20:22 +01001323 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001324 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001325
1326 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1327 obj->pages->sgl, obj->pages->nents,
1328 PCI_DMA_BIDIRECTIONAL))
1329 return -ENOSPC;
1330
1331 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001332}
1333
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001334static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1335{
1336#ifdef writeq
1337 writeq(pte, addr);
1338#else
1339 iowrite32((u32)pte, addr);
1340 iowrite32(pte >> 32, addr + 4);
1341#endif
1342}
1343
1344static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1345 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001346 uint64_t start,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001347 enum i915_cache_level level)
1348{
1349 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001350 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001351 gen8_gtt_pte_t __iomem *gtt_entries =
1352 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1353 int i = 0;
1354 struct sg_page_iter sg_iter;
1355 dma_addr_t addr;
1356
1357 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1358 addr = sg_dma_address(sg_iter.sg) +
1359 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1360 gen8_set_pte(&gtt_entries[i],
1361 gen8_pte_encode(addr, level, true));
1362 i++;
1363 }
1364
1365 /*
1366 * XXX: This serves as a posting read to make sure that the PTE has
1367 * actually been updated. There is some concern that even though
1368 * registers and PTEs are within the same BAR that they are potentially
1369 * of NUMA access patterns. Therefore, even with the way we assume
1370 * hardware should work, we must keep this posting read for paranoia.
1371 */
1372 if (i != 0)
1373 WARN_ON(readq(&gtt_entries[i-1])
1374 != gen8_pte_encode(addr, level, true));
1375
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001376 /* This next bit makes the above posting read even more important. We
1377 * want to flush the TLBs only after we're certain all the PTE updates
1378 * have finished.
1379 */
1380 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1381 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001382}
1383
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001384/*
1385 * Binds an object into the global gtt with the specified cache level. The object
1386 * will be accessible to the GPU via commands whose operands reference offsets
1387 * within the global GTT as well as accessible by the GPU through the GMADR
1388 * mapped BAR (dev_priv->mm.gtt->gtt).
1389 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001390static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001391 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001392 uint64_t start,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001393 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001394{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001395 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001396 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001397 gen6_gtt_pte_t __iomem *gtt_entries =
1398 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001399 int i = 0;
1400 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001401 dma_addr_t addr;
1402
Imre Deak6e995e22013-02-18 19:28:04 +02001403 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001404 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -07001405 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001406 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001407 }
1408
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001409 /* XXX: This serves as a posting read to make sure that the PTE has
1410 * actually been updated. There is some concern that even though
1411 * registers and PTEs are within the same BAR that they are potentially
1412 * of NUMA access patterns. Therefore, even with the way we assume
1413 * hardware should work, we must keep this posting read for paranoia.
1414 */
1415 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001416 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -07001417 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001418
1419 /* This next bit makes the above posting read even more important. We
1420 * want to flush the TLBs only after we're certain all the PTE updates
1421 * have finished.
1422 */
1423 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1424 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001425}
1426
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001427static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001428 uint64_t start,
1429 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001430 bool use_scratch)
1431{
1432 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001433 unsigned first_entry = start >> PAGE_SHIFT;
1434 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001435 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1436 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1437 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1438 int i;
1439
1440 if (WARN(num_entries > max_entries,
1441 "First entry = %d; Num entries = %d (max=%d)\n",
1442 first_entry, num_entries, max_entries))
1443 num_entries = max_entries;
1444
1445 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1446 I915_CACHE_LLC,
1447 use_scratch);
1448 for (i = 0; i < num_entries; i++)
1449 gen8_set_pte(&gtt_base[i], scratch_pte);
1450 readl(gtt_base);
1451}
1452
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001453static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001454 uint64_t start,
1455 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001456 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001457{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001458 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001459 unsigned first_entry = start >> PAGE_SHIFT;
1460 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001461 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1462 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001463 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001464 int i;
1465
1466 if (WARN(num_entries > max_entries,
1467 "First entry = %d; Num entries = %d (max=%d)\n",
1468 first_entry, num_entries, max_entries))
1469 num_entries = max_entries;
1470
Ben Widawsky828c7902013-10-16 09:21:30 -07001471 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
1472
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001473 for (i = 0; i < num_entries; i++)
1474 iowrite32(scratch_pte, &gtt_base[i]);
1475 readl(gtt_base);
1476}
1477
Ben Widawsky6f65e292013-12-06 14:10:56 -08001478
1479static void i915_ggtt_bind_vma(struct i915_vma *vma,
1480 enum i915_cache_level cache_level,
1481 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001482{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001483 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001484 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1485 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1486
Ben Widawsky6f65e292013-12-06 14:10:56 -08001487 BUG_ON(!i915_is_ggtt(vma->vm));
1488 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1489 vma->obj->has_global_gtt_mapping = 1;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001490}
1491
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001492static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001493 uint64_t start,
1494 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001495 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001496{
Ben Widawsky782f1492014-02-20 11:50:33 -08001497 unsigned first_entry = start >> PAGE_SHIFT;
1498 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001499 intel_gtt_clear_range(first_entry, num_entries);
1500}
1501
Ben Widawsky6f65e292013-12-06 14:10:56 -08001502static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001503{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001504 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1505 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001506
Ben Widawsky6f65e292013-12-06 14:10:56 -08001507 BUG_ON(!i915_is_ggtt(vma->vm));
1508 vma->obj->has_global_gtt_mapping = 0;
1509 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001510}
1511
Ben Widawsky6f65e292013-12-06 14:10:56 -08001512static void ggtt_bind_vma(struct i915_vma *vma,
1513 enum i915_cache_level cache_level,
1514 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001515{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001516 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001517 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001518 struct drm_i915_gem_object *obj = vma->obj;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001519
Ben Widawsky6f65e292013-12-06 14:10:56 -08001520 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1521 * or we have a global mapping already but the cacheability flags have
1522 * changed, set the global PTEs.
1523 *
1524 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1525 * instead if none of the above hold true.
1526 *
1527 * NB: A global mapping should only be needed for special regions like
1528 * "gtt mappable", SNB errata, or if specified via special execbuf
1529 * flags. At all other times, the GPU will use the aliasing PPGTT.
1530 */
1531 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1532 if (!obj->has_global_gtt_mapping ||
1533 (cache_level != obj->cache_level)) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001534 vma->vm->insert_entries(vma->vm, obj->pages,
1535 vma->node.start,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001536 cache_level);
1537 obj->has_global_gtt_mapping = 1;
1538 }
1539 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001540
Ben Widawsky6f65e292013-12-06 14:10:56 -08001541 if (dev_priv->mm.aliasing_ppgtt &&
1542 (!obj->has_aliasing_ppgtt_mapping ||
1543 (cache_level != obj->cache_level))) {
1544 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1545 appgtt->base.insert_entries(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001546 vma->obj->pages,
1547 vma->node.start,
1548 cache_level);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001549 vma->obj->has_aliasing_ppgtt_mapping = 1;
1550 }
1551}
1552
1553static void ggtt_unbind_vma(struct i915_vma *vma)
1554{
1555 struct drm_device *dev = vma->vm->dev;
1556 struct drm_i915_private *dev_priv = dev->dev_private;
1557 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001558
1559 if (obj->has_global_gtt_mapping) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001560 vma->vm->clear_range(vma->vm,
1561 vma->node.start,
1562 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001563 true);
1564 obj->has_global_gtt_mapping = 0;
1565 }
1566
1567 if (obj->has_aliasing_ppgtt_mapping) {
1568 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1569 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001570 vma->node.start,
1571 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001572 true);
1573 obj->has_aliasing_ppgtt_mapping = 0;
1574 }
Daniel Vetter74163902012-02-15 23:50:21 +01001575}
1576
1577void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1578{
Ben Widawsky5c042282011-10-17 15:51:55 -07001579 struct drm_device *dev = obj->base.dev;
1580 struct drm_i915_private *dev_priv = dev->dev_private;
1581 bool interruptible;
1582
1583 interruptible = do_idling(dev_priv);
1584
Chris Wilson9da3da62012-06-01 15:20:22 +01001585 if (!obj->has_dma_mapping)
1586 dma_unmap_sg(&dev->pdev->dev,
1587 obj->pages->sgl, obj->pages->nents,
1588 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001589
1590 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001591}
Daniel Vetter644ec022012-03-26 09:45:40 +02001592
Chris Wilson42d6ab42012-07-26 11:49:32 +01001593static void i915_gtt_color_adjust(struct drm_mm_node *node,
1594 unsigned long color,
1595 unsigned long *start,
1596 unsigned long *end)
1597{
1598 if (node->color != color)
1599 *start += 4096;
1600
1601 if (!list_empty(&node->node_list)) {
1602 node = list_entry(node->node_list.next,
1603 struct drm_mm_node,
1604 node_list);
1605 if (node->allocated && node->color != color)
1606 *end -= 4096;
1607 }
1608}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001609
Ben Widawskyd7e50082012-12-18 10:31:25 -08001610void i915_gem_setup_global_gtt(struct drm_device *dev,
1611 unsigned long start,
1612 unsigned long mappable_end,
1613 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001614{
Ben Widawskye78891c2013-01-25 16:41:04 -08001615 /* Let GEM Manage all of the aperture.
1616 *
1617 * However, leave one page at the end still bound to the scratch page.
1618 * There are a number of places where the hardware apparently prefetches
1619 * past the end of the object, and we've seen multiple hangs with the
1620 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1621 * aperture. One page should be enough to keep any prefetching inside
1622 * of the aperture.
1623 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001624 struct drm_i915_private *dev_priv = dev->dev_private;
1625 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001626 struct drm_mm_node *entry;
1627 struct drm_i915_gem_object *obj;
1628 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +02001629
Ben Widawsky35451cb2013-01-17 12:45:13 -08001630 BUG_ON(mappable_end > end);
1631
Chris Wilsoned2f3452012-11-15 11:32:19 +00001632 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001633 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001634 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001635 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001636
Chris Wilsoned2f3452012-11-15 11:32:19 +00001637 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001638 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001639 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001640 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -07001641 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001642 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001643
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001644 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001645 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001646 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001647 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +00001648 obj->has_global_gtt_mapping = 1;
1649 }
1650
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001651 dev_priv->gtt.base.start = start;
1652 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001653
Chris Wilsoned2f3452012-11-15 11:32:19 +00001654 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001655 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00001656 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1657 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08001658 ggtt_vm->clear_range(ggtt_vm, hole_start,
1659 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001660 }
1661
1662 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08001663 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001664}
1665
Ben Widawskyd7e50082012-12-18 10:31:25 -08001666void i915_gem_init_global_gtt(struct drm_device *dev)
1667{
1668 struct drm_i915_private *dev_priv = dev->dev_private;
1669 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001670
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001671 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001672 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001673
Ben Widawskye78891c2013-01-25 16:41:04 -08001674 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001675}
1676
1677static int setup_scratch_page(struct drm_device *dev)
1678{
1679 struct drm_i915_private *dev_priv = dev->dev_private;
1680 struct page *page;
1681 dma_addr_t dma_addr;
1682
1683 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1684 if (page == NULL)
1685 return -ENOMEM;
1686 get_page(page);
1687 set_pages_uc(page, 1);
1688
1689#ifdef CONFIG_INTEL_IOMMU
1690 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1691 PCI_DMA_BIDIRECTIONAL);
1692 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1693 return -EINVAL;
1694#else
1695 dma_addr = page_to_phys(page);
1696#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001697 dev_priv->gtt.base.scratch.page = page;
1698 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001699
1700 return 0;
1701}
1702
1703static void teardown_scratch_page(struct drm_device *dev)
1704{
1705 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001706 struct page *page = dev_priv->gtt.base.scratch.page;
1707
1708 set_pages_wb(page, 1);
1709 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001710 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001711 put_page(page);
1712 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001713}
1714
1715static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1716{
1717 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1718 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1719 return snb_gmch_ctl << 20;
1720}
1721
Ben Widawsky9459d252013-11-03 16:53:55 -08001722static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1723{
1724 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1725 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1726 if (bdw_gmch_ctl)
1727 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky3a2ffb62013-11-07 21:40:51 -08001728 if (bdw_gmch_ctl > 4) {
Jani Nikulad330a952014-01-21 11:24:25 +02001729 WARN_ON(!i915.preliminary_hw_support);
Ben Widawsky3a2ffb62013-11-07 21:40:51 -08001730 return 4<<20;
1731 }
1732
Ben Widawsky9459d252013-11-03 16:53:55 -08001733 return bdw_gmch_ctl << 20;
1734}
1735
Ben Widawskybaa09f52013-01-24 13:49:57 -08001736static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001737{
1738 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1739 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1740 return snb_gmch_ctl << 25; /* 32 MB units */
1741}
1742
Ben Widawsky9459d252013-11-03 16:53:55 -08001743static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1744{
1745 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1746 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1747 return bdw_gmch_ctl << 25; /* 32 MB units */
1748}
1749
Ben Widawsky63340132013-11-04 19:32:22 -08001750static int ggtt_probe_common(struct drm_device *dev,
1751 size_t gtt_size)
1752{
1753 struct drm_i915_private *dev_priv = dev->dev_private;
1754 phys_addr_t gtt_bus_addr;
1755 int ret;
1756
1757 /* For Modern GENs the PTEs and register space are split in the BAR */
1758 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
1759 (pci_resource_len(dev->pdev, 0) / 2);
1760
1761 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
1762 if (!dev_priv->gtt.gsm) {
1763 DRM_ERROR("Failed to map the gtt page table\n");
1764 return -ENOMEM;
1765 }
1766
1767 ret = setup_scratch_page(dev);
1768 if (ret) {
1769 DRM_ERROR("Scratch setup failed\n");
1770 /* iounmap will also get called at remove, but meh */
1771 iounmap(dev_priv->gtt.gsm);
1772 }
1773
1774 return ret;
1775}
1776
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001777/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1778 * bits. When using advanced contexts each context stores its own PAT, but
1779 * writing this data shouldn't be harmful even in those cases. */
1780static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1781{
1782#define GEN8_PPAT_UC (0<<0)
1783#define GEN8_PPAT_WC (1<<0)
1784#define GEN8_PPAT_WT (2<<0)
1785#define GEN8_PPAT_WB (3<<0)
1786#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
1787/* FIXME(BDW): Bspec is completely confused about cache control bits. */
1788#define GEN8_PPAT_LLC (1<<2)
1789#define GEN8_PPAT_LLCELLC (2<<2)
1790#define GEN8_PPAT_LLCeLLC (3<<2)
1791#define GEN8_PPAT_AGE(x) (x<<4)
1792#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
1793 uint64_t pat;
1794
1795 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1796 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1797 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1798 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1799 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1800 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1801 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1802 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1803
1804 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1805 * write would work. */
1806 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1807 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1808}
1809
Ben Widawsky63340132013-11-04 19:32:22 -08001810static int gen8_gmch_probe(struct drm_device *dev,
1811 size_t *gtt_total,
1812 size_t *stolen,
1813 phys_addr_t *mappable_base,
1814 unsigned long *mappable_end)
1815{
1816 struct drm_i915_private *dev_priv = dev->dev_private;
1817 unsigned int gtt_size;
1818 u16 snb_gmch_ctl;
1819 int ret;
1820
1821 /* TODO: We're not aware of mappable constraints on gen8 yet */
1822 *mappable_base = pci_resource_start(dev->pdev, 2);
1823 *mappable_end = pci_resource_len(dev->pdev, 2);
1824
1825 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1826 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1827
1828 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1829
1830 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1831
1832 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskyd31eb102013-11-02 21:07:17 -07001833 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001834
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001835 gen8_setup_private_ppat(dev_priv);
1836
Ben Widawsky63340132013-11-04 19:32:22 -08001837 ret = ggtt_probe_common(dev, gtt_size);
1838
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001839 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1840 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001841
1842 return ret;
1843}
1844
Ben Widawskybaa09f52013-01-24 13:49:57 -08001845static int gen6_gmch_probe(struct drm_device *dev,
1846 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001847 size_t *stolen,
1848 phys_addr_t *mappable_base,
1849 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001850{
1851 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001852 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001853 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001854 int ret;
1855
Ben Widawsky41907dd2013-02-08 11:32:47 -08001856 *mappable_base = pci_resource_start(dev->pdev, 2);
1857 *mappable_end = pci_resource_len(dev->pdev, 2);
1858
Ben Widawskybaa09f52013-01-24 13:49:57 -08001859 /* 64/512MB is the current min/max we actually know of, but this is just
1860 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001861 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001862 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001863 DRM_ERROR("Unknown GMADR size (%lx)\n",
1864 dev_priv->gtt.mappable_end);
1865 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001866 }
1867
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001868 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1869 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001870 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001871
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07001872 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001873
Ben Widawsky63340132013-11-04 19:32:22 -08001874 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001875 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
1876
Ben Widawsky63340132013-11-04 19:32:22 -08001877 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001878
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001879 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1880 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001881
1882 return ret;
1883}
1884
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001885static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001886{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001887
1888 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08001889
1890 drm_mm_takedown(&vm->mm);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001891 iounmap(gtt->gsm);
1892 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001893}
1894
1895static int i915_gmch_probe(struct drm_device *dev,
1896 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001897 size_t *stolen,
1898 phys_addr_t *mappable_base,
1899 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001900{
1901 struct drm_i915_private *dev_priv = dev->dev_private;
1902 int ret;
1903
Ben Widawskybaa09f52013-01-24 13:49:57 -08001904 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1905 if (!ret) {
1906 DRM_ERROR("failed to set up gmch\n");
1907 return -EIO;
1908 }
1909
Ben Widawsky41907dd2013-02-08 11:32:47 -08001910 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001911
1912 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001913 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001914
Chris Wilsonc0a7f812013-12-30 12:16:15 +00001915 if (unlikely(dev_priv->gtt.do_idle_maps))
1916 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
1917
Ben Widawskybaa09f52013-01-24 13:49:57 -08001918 return 0;
1919}
1920
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001921static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001922{
1923 intel_gmch_remove();
1924}
1925
1926int i915_gem_gtt_init(struct drm_device *dev)
1927{
1928 struct drm_i915_private *dev_priv = dev->dev_private;
1929 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001930 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001931
Ben Widawskybaa09f52013-01-24 13:49:57 -08001932 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001933 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001934 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08001935 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001936 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001937 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001938 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001939 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001940 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001941 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001942 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001943 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001944 else if (INTEL_INFO(dev)->gen >= 7)
1945 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001946 else
Chris Wilson350ec882013-08-06 13:17:02 +01001947 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08001948 } else {
1949 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1950 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001951 }
1952
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001953 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001954 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001955 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001956 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001957
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001958 gtt->base.dev = dev;
1959
Ben Widawskybaa09f52013-01-24 13:49:57 -08001960 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001961 DRM_INFO("Memory usable by graphics device = %zdM\n",
1962 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001963 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1964 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001965
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001966 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001967}
Ben Widawsky6f65e292013-12-06 14:10:56 -08001968
1969static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
1970 struct i915_address_space *vm)
1971{
1972 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
1973 if (vma == NULL)
1974 return ERR_PTR(-ENOMEM);
1975
1976 INIT_LIST_HEAD(&vma->vma_link);
1977 INIT_LIST_HEAD(&vma->mm_list);
1978 INIT_LIST_HEAD(&vma->exec_list);
1979 vma->vm = vm;
1980 vma->obj = obj;
1981
1982 switch (INTEL_INFO(vm->dev)->gen) {
1983 case 8:
1984 case 7:
1985 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001986 if (i915_is_ggtt(vm)) {
1987 vma->unbind_vma = ggtt_unbind_vma;
1988 vma->bind_vma = ggtt_bind_vma;
1989 } else {
1990 vma->unbind_vma = ppgtt_unbind_vma;
1991 vma->bind_vma = ppgtt_bind_vma;
1992 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08001993 break;
1994 case 5:
1995 case 4:
1996 case 3:
1997 case 2:
1998 BUG_ON(!i915_is_ggtt(vm));
1999 vma->unbind_vma = i915_ggtt_unbind_vma;
2000 vma->bind_vma = i915_ggtt_bind_vma;
2001 break;
2002 default:
2003 BUG();
2004 }
2005
2006 /* Keep GGTT vmas first to make debug easier */
2007 if (i915_is_ggtt(vm))
2008 list_add(&vma->vma_link, &obj->vma_list);
2009 else
2010 list_add_tail(&vma->vma_link, &obj->vma_list);
2011
2012 return vma;
2013}
2014
2015struct i915_vma *
2016i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2017 struct i915_address_space *vm)
2018{
2019 struct i915_vma *vma;
2020
2021 vma = i915_gem_obj_to_vma(obj, vm);
2022 if (!vma)
2023 vma = __i915_gem_vma_create(obj, vm);
2024
2025 return vma;
2026}