blob: 4143efd9c02b462fff6d84d69040cabf185677ab [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 Widawsky686e1f6f2013-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 Widawsky686e1f6f2013-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 Widawsky3e302542013-04-23 23:15:32 -0700463static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700464{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700465 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700466 gen6_gtt_pte_t __iomem *pd_addr;
467 uint32_t pd_entry;
468 int i;
469
Ben Widawsky0a732872013-04-23 23:15:30 -0700470 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700471 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
472 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
473 for (i = 0; i < ppgtt->num_pd_entries; i++) {
474 dma_addr_t pt_addr;
475
476 pt_addr = ppgtt->pt_dma_addr[i];
477 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
478 pd_entry |= GEN6_PDE_VALID;
479
480 writel(pd_entry, pd_addr + i);
481 }
482 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700483}
484
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800485static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
486{
487 BUG_ON(ppgtt->pd_offset & 0x3f);
488
489 return (ppgtt->pd_offset / 64) << 16;
490}
491
Ben Widawsky90252e52013-12-06 14:11:12 -0800492static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
493 struct intel_ring_buffer *ring,
494 bool synchronous)
495{
496 struct drm_device *dev = ppgtt->base.dev;
497 struct drm_i915_private *dev_priv = dev->dev_private;
498 int ret;
499
500 /* If we're in reset, we can assume the GPU is sufficiently idle to
501 * manually frob these bits. Ideally we could use the ring functions,
502 * except our error handling makes it quite difficult (can't use
503 * intel_ring_begin, ring->flush, or intel_ring_advance)
504 *
505 * FIXME: We should try not to special case reset
506 */
507 if (synchronous ||
508 i915_reset_in_progress(&dev_priv->gpu_error)) {
509 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
510 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
511 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
512 POSTING_READ(RING_PP_DIR_BASE(ring));
513 return 0;
514 }
515
516 /* NB: TLBs must be flushed and invalidated before a switch */
517 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
518 if (ret)
519 return ret;
520
521 ret = intel_ring_begin(ring, 6);
522 if (ret)
523 return ret;
524
525 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
526 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
527 intel_ring_emit(ring, PP_DIR_DCLV_2G);
528 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
529 intel_ring_emit(ring, get_pd_offset(ppgtt));
530 intel_ring_emit(ring, MI_NOOP);
531 intel_ring_advance(ring);
532
533 return 0;
534}
535
Ben Widawsky48a10382013-12-06 14:11:11 -0800536static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
537 struct intel_ring_buffer *ring,
538 bool synchronous)
539{
540 struct drm_device *dev = ppgtt->base.dev;
541 struct drm_i915_private *dev_priv = dev->dev_private;
542 int ret;
543
544 /* If we're in reset, we can assume the GPU is sufficiently idle to
545 * manually frob these bits. Ideally we could use the ring functions,
546 * except our error handling makes it quite difficult (can't use
547 * intel_ring_begin, ring->flush, or intel_ring_advance)
548 *
549 * FIXME: We should try not to special case reset
550 */
551 if (synchronous ||
552 i915_reset_in_progress(&dev_priv->gpu_error)) {
553 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
554 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
555 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
556 POSTING_READ(RING_PP_DIR_BASE(ring));
557 return 0;
558 }
559
560 /* NB: TLBs must be flushed and invalidated before a switch */
561 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
562 if (ret)
563 return ret;
564
565 ret = intel_ring_begin(ring, 6);
566 if (ret)
567 return ret;
568
569 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
570 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
571 intel_ring_emit(ring, PP_DIR_DCLV_2G);
572 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
573 intel_ring_emit(ring, get_pd_offset(ppgtt));
574 intel_ring_emit(ring, MI_NOOP);
575 intel_ring_advance(ring);
576
Ben Widawsky90252e52013-12-06 14:11:12 -0800577 /* XXX: RCS is the only one to auto invalidate the TLBs? */
578 if (ring->id != RCS) {
579 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
580 if (ret)
581 return ret;
582 }
583
Ben Widawsky48a10382013-12-06 14:11:11 -0800584 return 0;
585}
586
Ben Widawskyeeb94882013-12-06 14:11:10 -0800587static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
588 struct intel_ring_buffer *ring,
589 bool synchronous)
590{
591 struct drm_device *dev = ppgtt->base.dev;
592 struct drm_i915_private *dev_priv = dev->dev_private;
593
Ben Widawsky48a10382013-12-06 14:11:11 -0800594 if (!synchronous)
595 return 0;
596
Ben Widawskyeeb94882013-12-06 14:11:10 -0800597 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
598 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
599
600 POSTING_READ(RING_PP_DIR_DCLV(ring));
601
602 return 0;
603}
604
605static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
606{
607 struct drm_device *dev = ppgtt->base.dev;
608 struct drm_i915_private *dev_priv = dev->dev_private;
609 struct intel_ring_buffer *ring;
610 int j, ret;
611
612 for_each_ring(ring, dev_priv, j) {
613 I915_WRITE(RING_MODE_GEN7(ring),
614 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
615 ret = ppgtt->switch_mm(ppgtt, ring, true);
616 if (ret)
617 goto err_out;
618 }
619
620 return 0;
621
622err_out:
623 for_each_ring(ring, dev_priv, j)
624 I915_WRITE(RING_MODE_GEN7(ring),
625 _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
626 return ret;
627}
628
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800629static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
630{
631 struct drm_device *dev = ppgtt->base.dev;
632 drm_i915_private_t *dev_priv = dev->dev_private;
633 struct intel_ring_buffer *ring;
634 uint32_t ecochk, ecobits;
635 int i;
636
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800637 ecobits = I915_READ(GAC_ECO_BITS);
638 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
639
640 ecochk = I915_READ(GAM_ECOCHK);
641 if (IS_HASWELL(dev)) {
642 ecochk |= ECOCHK_PPGTT_WB_HSW;
643 } else {
644 ecochk |= ECOCHK_PPGTT_LLC_IVB;
645 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
646 }
647 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800648
649 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800650 int ret;
651 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800652 I915_WRITE(RING_MODE_GEN7(ring),
653 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -0800654 ret = ppgtt->switch_mm(ppgtt, ring, true);
655 if (ret)
656 return ret;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800657
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800658 }
659 return 0;
660}
661
Ben Widawskya3d67d22013-12-06 14:11:06 -0800662static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700663{
Ben Widawskya3d67d22013-12-06 14:11:06 -0800664 struct drm_device *dev = ppgtt->base.dev;
Ben Widawsky3e302542013-04-23 23:15:32 -0700665 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky3e302542013-04-23 23:15:32 -0700666 struct intel_ring_buffer *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800667 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky3e302542013-04-23 23:15:32 -0700668 int i;
669
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800670 ecobits = I915_READ(GAC_ECO_BITS);
671 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
672 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700673
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800674 gab_ctl = I915_READ(GAB_CTL);
675 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700676
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800677 ecochk = I915_READ(GAM_ECOCHK);
678 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700679
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800680 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700681
682 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800683 int ret = ppgtt->switch_mm(ppgtt, ring, true);
684 if (ret)
685 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700686 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800687
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700688 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700689}
690
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100691/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700692static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100693 unsigned first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700694 unsigned num_entries,
695 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100696{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700697 struct i915_hw_ppgtt *ppgtt =
698 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700699 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Daniel Vettera15326a2013-03-19 23:48:39 +0100700 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100701 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
702 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100703
Ben Widawskyb35b3802013-10-16 09:18:21 -0700704 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100705
Daniel Vetter7bddb012012-02-09 17:15:47 +0100706 while (num_entries) {
707 last_pte = first_pte + num_entries;
708 if (last_pte > I915_PPGTT_PT_ENTRIES)
709 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100710
Daniel Vettera15326a2013-03-19 23:48:39 +0100711 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100712
713 for (i = first_pte; i < last_pte; i++)
714 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100715
716 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100717
Daniel Vetter7bddb012012-02-09 17:15:47 +0100718 num_entries -= last_pte - first_pte;
719 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100720 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100721 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100722}
723
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700724static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800725 struct sg_table *pages,
726 unsigned first_entry,
727 enum i915_cache_level cache_level)
728{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700729 struct i915_hw_ppgtt *ppgtt =
730 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700731 gen6_gtt_pte_t *pt_vaddr;
Daniel Vettera15326a2013-03-19 23:48:39 +0100732 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200733 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
734 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800735
Daniel Vettera15326a2013-03-19 23:48:39 +0100736 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200737 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
738 dma_addr_t page_addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800739
Imre Deak2db76d72013-03-26 15:14:18 +0200740 page_addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -0700741 pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200742 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
743 kunmap_atomic(pt_vaddr);
Daniel Vettera15326a2013-03-19 23:48:39 +0100744 act_pt++;
745 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200746 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800747
Daniel Vetterdef886c2013-01-24 14:44:56 -0800748 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800749 }
Imre Deak6e995e22013-02-18 19:28:04 +0200750 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800751}
752
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700753static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100754{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700755 struct i915_hw_ppgtt *ppgtt =
756 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -0800757 int i;
758
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800759 list_del(&vm->global_link);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700760 drm_mm_takedown(&ppgtt->base.mm);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800761 drm_mm_remove_node(&ppgtt->node);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700762
Daniel Vetter3440d262013-01-24 13:49:56 -0800763 if (ppgtt->pt_dma_addr) {
764 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700765 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800766 ppgtt->pt_dma_addr[i],
767 4096, PCI_DMA_BIDIRECTIONAL);
768 }
769
770 kfree(ppgtt->pt_dma_addr);
771 for (i = 0; i < ppgtt->num_pd_entries; i++)
772 __free_page(ppgtt->pt_pages[i]);
773 kfree(ppgtt->pt_pages);
774 kfree(ppgtt);
775}
776
777static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
778{
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800779#define GEN6_PD_ALIGN (PAGE_SIZE * 16)
780#define GEN6_PD_SIZE (GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700781 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100782 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -0800783 bool retried = false;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800784 int i, ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100785
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800786 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
787 * allocator works in address space sizes, so it's multiplied by page
788 * size. We allocate at the top of the GTT to avoid fragmentation.
789 */
790 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -0800791alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800792 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
793 &ppgtt->node, GEN6_PD_SIZE,
794 GEN6_PD_ALIGN, 0,
795 0, dev_priv->gtt.base.total,
796 DRM_MM_SEARCH_DEFAULT);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800797 if (ret == -ENOSPC && !retried) {
798 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
799 GEN6_PD_SIZE, GEN6_PD_ALIGN,
800 I915_CACHE_NONE, false, true);
801 if (ret)
802 return ret;
803
804 retried = true;
805 goto alloc;
806 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800807
808 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
809 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100810
Chris Wilson08c45262013-07-30 19:04:37 +0100811 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700812 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky48a10382013-12-06 14:11:11 -0800813 if (IS_GEN6(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800814 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -0800815 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -0800816 } else if (IS_HASWELL(dev)) {
817 ppgtt->enable = gen7_ppgtt_enable;
818 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -0800819 } else if (IS_GEN7(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800820 ppgtt->enable = gen7_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -0800821 ppgtt->switch_mm = gen7_mm_switch;
822 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800823 BUG();
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700824 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
825 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
826 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
827 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Ben Widawsky686e1f6f2013-11-25 09:54:34 -0800828 ppgtt->base.start = 0;
829 ppgtt->base.total = GEN6_PPGTT_PD_ENTRIES * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Daniel Vettera1e22652013-09-21 00:35:38 +0200830 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100831 GFP_KERNEL);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800832 if (!ppgtt->pt_pages) {
833 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -0800834 return -ENOMEM;
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800835 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100836
837 for (i = 0; i < ppgtt->num_pd_entries; i++) {
838 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
839 if (!ppgtt->pt_pages[i])
840 goto err_pt_alloc;
841 }
842
Daniel Vettera1e22652013-09-21 00:35:38 +0200843 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800844 GFP_KERNEL);
845 if (!ppgtt->pt_dma_addr)
846 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100847
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800848 for (i = 0; i < ppgtt->num_pd_entries; i++) {
849 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200850
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800851 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
852 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100853
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800854 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
855 ret = -EIO;
856 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100857
Daniel Vetter211c5682012-04-10 17:29:17 +0200858 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800859 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100860 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100861
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700862 ppgtt->base.clear_range(&ppgtt->base, 0,
Ben Widawsky828c7902013-10-16 09:21:30 -0700863 ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100864
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800865 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
866 ppgtt->node.size >> 20,
867 ppgtt->node.start / PAGE_SIZE);
868 ppgtt->pd_offset =
869 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100870
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100871 return 0;
872
873err_pd_pin:
874 if (ppgtt->pt_dma_addr) {
875 for (i--; i >= 0; i--)
876 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
877 4096, PCI_DMA_BIDIRECTIONAL);
878 }
879err_pt_alloc:
880 kfree(ppgtt->pt_dma_addr);
881 for (i = 0; i < ppgtt->num_pd_entries; i++) {
882 if (ppgtt->pt_pages[i])
883 __free_page(ppgtt->pt_pages[i]);
884 }
885 kfree(ppgtt->pt_pages);
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800886 drm_mm_remove_node(&ppgtt->node);
Daniel Vetter3440d262013-01-24 13:49:56 -0800887
888 return ret;
889}
890
Ben Widawsky246cbfb2013-12-06 14:11:14 -0800891int i915_gem_init_ppgtt(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -0800892{
893 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd6660ad2013-12-06 14:11:13 -0800894 int ret = 0;
Daniel Vetter3440d262013-01-24 13:49:56 -0800895
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700896 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -0800897
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700898 if (INTEL_INFO(dev)->gen < 8)
899 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -0700900 else if (IS_GEN8(dev))
Ben Widawsky37aca442013-11-04 20:47:32 -0800901 ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700902 else
903 BUG();
904
Ben Widawskyc7c48df2013-12-06 14:11:15 -0800905 if (!ret) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800906 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyc7c48df2013-12-06 14:11:15 -0800907 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700908 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
909 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800910 i915_init_vm(dev_priv, &ppgtt->base);
911 if (INTEL_INFO(dev)->gen < 8) {
Ben Widawsky9f273d42013-12-06 14:11:16 -0800912 gen6_write_pdes(ppgtt);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800913 DRM_DEBUG("Adding PPGTT at offset %x\n",
914 ppgtt->pd_offset << 10);
915 }
Ben Widawskyc7c48df2013-12-06 14:11:15 -0800916 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100917
918 return ret;
919}
920
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800921static void
Ben Widawsky6f65e292013-12-06 14:10:56 -0800922ppgtt_bind_vma(struct i915_vma *vma,
923 enum i915_cache_level cache_level,
924 u32 flags)
Daniel Vetter7bddb012012-02-09 17:15:47 +0100925{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800926 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
927
928 WARN_ON(flags);
929
930 vma->vm->insert_entries(vma->vm, vma->obj->pages, entry, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100931}
932
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800933static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +0100934{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800935 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
936
937 vma->vm->clear_range(vma->vm,
938 entry,
939 vma->obj->base.size >> PAGE_SHIFT,
940 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100941}
942
Ben Widawskya81cc002013-01-18 12:30:31 -0800943extern int intel_iommu_gfx_mapped;
944/* Certain Gen5 chipsets require require idling the GPU before
945 * unmapping anything from the GTT when VT-d is enabled.
946 */
947static inline bool needs_idle_maps(struct drm_device *dev)
948{
949#ifdef CONFIG_INTEL_IOMMU
950 /* Query intel_iommu to see if we need the workaround. Presumably that
951 * was loaded first.
952 */
953 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
954 return true;
955#endif
956 return false;
957}
958
Ben Widawsky5c042282011-10-17 15:51:55 -0700959static bool do_idling(struct drm_i915_private *dev_priv)
960{
961 bool ret = dev_priv->mm.interruptible;
962
Ben Widawskya81cc002013-01-18 12:30:31 -0800963 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700964 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700965 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700966 DRM_ERROR("Couldn't idle GPU\n");
967 /* Wait a bit, in hopes it avoids the hang */
968 udelay(10);
969 }
970 }
971
972 return ret;
973}
974
975static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
976{
Ben Widawskya81cc002013-01-18 12:30:31 -0800977 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -0700978 dev_priv->mm.interruptible = interruptible;
979}
980
Ben Widawsky828c7902013-10-16 09:21:30 -0700981void i915_check_and_clear_faults(struct drm_device *dev)
982{
983 struct drm_i915_private *dev_priv = dev->dev_private;
984 struct intel_ring_buffer *ring;
985 int i;
986
987 if (INTEL_INFO(dev)->gen < 6)
988 return;
989
990 for_each_ring(ring, dev_priv, i) {
991 u32 fault_reg;
992 fault_reg = I915_READ(RING_FAULT_REG(ring));
993 if (fault_reg & RING_FAULT_VALID) {
994 DRM_DEBUG_DRIVER("Unexpected fault\n"
995 "\tAddr: 0x%08lx\\n"
996 "\tAddress space: %s\n"
997 "\tSource ID: %d\n"
998 "\tType: %d\n",
999 fault_reg & PAGE_MASK,
1000 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1001 RING_FAULT_SRCID(fault_reg),
1002 RING_FAULT_FAULT_TYPE(fault_reg));
1003 I915_WRITE(RING_FAULT_REG(ring),
1004 fault_reg & ~RING_FAULT_VALID);
1005 }
1006 }
1007 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1008}
1009
1010void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1011{
1012 struct drm_i915_private *dev_priv = dev->dev_private;
1013
1014 /* Don't bother messing with faults pre GEN6 as we have little
1015 * documentation supporting that it's a good idea.
1016 */
1017 if (INTEL_INFO(dev)->gen < 6)
1018 return;
1019
1020 i915_check_and_clear_faults(dev);
1021
1022 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1023 dev_priv->gtt.base.start / PAGE_SIZE,
1024 dev_priv->gtt.base.total / PAGE_SIZE,
1025 false);
1026}
1027
Daniel Vetter76aaf222010-11-05 22:23:30 +01001028void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1029{
1030 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001031 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001032 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001033
Ben Widawsky828c7902013-10-16 09:21:30 -07001034 i915_check_and_clear_faults(dev);
1035
Chris Wilsonbee4a182011-01-21 10:54:32 +00001036 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001037 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1038 dev_priv->gtt.base.start / PAGE_SIZE,
Ben Widawsky828c7902013-10-16 09:21:30 -07001039 dev_priv->gtt.base.total / PAGE_SIZE,
1040 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001041
Ben Widawsky35c20a62013-05-31 11:28:48 -07001042 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001043 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1044 &dev_priv->gtt.base);
1045 if (!vma)
1046 continue;
1047
Chris Wilson2c225692013-08-09 12:26:45 +01001048 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001049 /* The bind_vma code tries to be smart about tracking mappings.
1050 * Unfortunately above, we've just wiped out the mappings
1051 * without telling our object about it. So we need to fake it.
1052 */
1053 obj->has_global_gtt_mapping = 0;
1054 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001055 }
1056
Ben Widawsky80da2162013-12-06 14:11:17 -08001057
1058 if (INTEL_INFO(dev)->gen >= 8)
1059 return;
1060
1061 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1062 /* TODO: Perhaps it shouldn't be gen6 specific */
1063 if (i915_is_ggtt(vm)) {
1064 if (dev_priv->mm.aliasing_ppgtt)
1065 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1066 continue;
1067 }
1068
1069 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
1070 }
Ben Widawsky9f273d42013-12-06 14:11:16 -08001071
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001072 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001073}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001074
Daniel Vetter74163902012-02-15 23:50:21 +01001075int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001076{
Chris Wilson9da3da62012-06-01 15:20:22 +01001077 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001078 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001079
1080 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1081 obj->pages->sgl, obj->pages->nents,
1082 PCI_DMA_BIDIRECTIONAL))
1083 return -ENOSPC;
1084
1085 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001086}
1087
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001088static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1089{
1090#ifdef writeq
1091 writeq(pte, addr);
1092#else
1093 iowrite32((u32)pte, addr);
1094 iowrite32(pte >> 32, addr + 4);
1095#endif
1096}
1097
1098static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1099 struct sg_table *st,
1100 unsigned int first_entry,
1101 enum i915_cache_level level)
1102{
1103 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1104 gen8_gtt_pte_t __iomem *gtt_entries =
1105 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1106 int i = 0;
1107 struct sg_page_iter sg_iter;
1108 dma_addr_t addr;
1109
1110 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1111 addr = sg_dma_address(sg_iter.sg) +
1112 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1113 gen8_set_pte(&gtt_entries[i],
1114 gen8_pte_encode(addr, level, true));
1115 i++;
1116 }
1117
1118 /*
1119 * XXX: This serves as a posting read to make sure that the PTE has
1120 * actually been updated. There is some concern that even though
1121 * registers and PTEs are within the same BAR that they are potentially
1122 * of NUMA access patterns. Therefore, even with the way we assume
1123 * hardware should work, we must keep this posting read for paranoia.
1124 */
1125 if (i != 0)
1126 WARN_ON(readq(&gtt_entries[i-1])
1127 != gen8_pte_encode(addr, level, true));
1128
1129#if 0 /* TODO: Still needed on GEN8? */
1130 /* This next bit makes the above posting read even more important. We
1131 * want to flush the TLBs only after we're certain all the PTE updates
1132 * have finished.
1133 */
1134 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1135 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1136#endif
1137}
1138
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001139/*
1140 * Binds an object into the global gtt with the specified cache level. The object
1141 * will be accessible to the GPU via commands whose operands reference offsets
1142 * within the global GTT as well as accessible by the GPU through the GMADR
1143 * mapped BAR (dev_priv->mm.gtt->gtt).
1144 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001145static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001146 struct sg_table *st,
1147 unsigned int first_entry,
1148 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001149{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001150 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001151 gen6_gtt_pte_t __iomem *gtt_entries =
1152 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001153 int i = 0;
1154 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001155 dma_addr_t addr;
1156
Imre Deak6e995e22013-02-18 19:28:04 +02001157 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001158 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -07001159 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001160 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001161 }
1162
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001163 /* XXX: This serves as a posting read to make sure that the PTE has
1164 * actually been updated. There is some concern that even though
1165 * registers and PTEs are within the same BAR that they are potentially
1166 * of NUMA access patterns. Therefore, even with the way we assume
1167 * hardware should work, we must keep this posting read for paranoia.
1168 */
1169 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001170 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -07001171 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001172
1173 /* This next bit makes the above posting read even more important. We
1174 * want to flush the TLBs only after we're certain all the PTE updates
1175 * have finished.
1176 */
1177 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1178 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001179}
1180
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001181static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1182 unsigned int first_entry,
1183 unsigned int num_entries,
1184 bool use_scratch)
1185{
1186 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1187 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1188 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1189 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1190 int i;
1191
1192 if (WARN(num_entries > max_entries,
1193 "First entry = %d; Num entries = %d (max=%d)\n",
1194 first_entry, num_entries, max_entries))
1195 num_entries = max_entries;
1196
1197 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1198 I915_CACHE_LLC,
1199 use_scratch);
1200 for (i = 0; i < num_entries; i++)
1201 gen8_set_pte(&gtt_base[i], scratch_pte);
1202 readl(gtt_base);
1203}
1204
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001205static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001206 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -07001207 unsigned int num_entries,
1208 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001209{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001210 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001211 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1212 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001213 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001214 int i;
1215
1216 if (WARN(num_entries > max_entries,
1217 "First entry = %d; Num entries = %d (max=%d)\n",
1218 first_entry, num_entries, max_entries))
1219 num_entries = max_entries;
1220
Ben Widawsky828c7902013-10-16 09:21:30 -07001221 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
1222
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001223 for (i = 0; i < num_entries; i++)
1224 iowrite32(scratch_pte, &gtt_base[i]);
1225 readl(gtt_base);
1226}
1227
Ben Widawsky6f65e292013-12-06 14:10:56 -08001228
1229static void i915_ggtt_bind_vma(struct i915_vma *vma,
1230 enum i915_cache_level cache_level,
1231 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001232{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001233 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001234 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1235 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1236
Ben Widawsky6f65e292013-12-06 14:10:56 -08001237 BUG_ON(!i915_is_ggtt(vma->vm));
1238 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1239 vma->obj->has_global_gtt_mapping = 1;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001240}
1241
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001242static void i915_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001243 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -07001244 unsigned int num_entries,
1245 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001246{
1247 intel_gtt_clear_range(first_entry, num_entries);
1248}
1249
Ben Widawsky6f65e292013-12-06 14:10:56 -08001250static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001251{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001252 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1253 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001254
Ben Widawsky6f65e292013-12-06 14:10:56 -08001255 BUG_ON(!i915_is_ggtt(vma->vm));
1256 vma->obj->has_global_gtt_mapping = 0;
1257 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001258}
1259
Ben Widawsky6f65e292013-12-06 14:10:56 -08001260static void ggtt_bind_vma(struct i915_vma *vma,
1261 enum i915_cache_level cache_level,
1262 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001263{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001264 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001265 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001266 struct drm_i915_gem_object *obj = vma->obj;
1267 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001268
Ben Widawsky6f65e292013-12-06 14:10:56 -08001269 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1270 * or we have a global mapping already but the cacheability flags have
1271 * changed, set the global PTEs.
1272 *
1273 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1274 * instead if none of the above hold true.
1275 *
1276 * NB: A global mapping should only be needed for special regions like
1277 * "gtt mappable", SNB errata, or if specified via special execbuf
1278 * flags. At all other times, the GPU will use the aliasing PPGTT.
1279 */
1280 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1281 if (!obj->has_global_gtt_mapping ||
1282 (cache_level != obj->cache_level)) {
1283 vma->vm->insert_entries(vma->vm, obj->pages, entry,
1284 cache_level);
1285 obj->has_global_gtt_mapping = 1;
1286 }
1287 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001288
Ben Widawsky6f65e292013-12-06 14:10:56 -08001289 if (dev_priv->mm.aliasing_ppgtt &&
1290 (!obj->has_aliasing_ppgtt_mapping ||
1291 (cache_level != obj->cache_level))) {
1292 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1293 appgtt->base.insert_entries(&appgtt->base,
1294 vma->obj->pages, entry, cache_level);
1295 vma->obj->has_aliasing_ppgtt_mapping = 1;
1296 }
1297}
1298
1299static void ggtt_unbind_vma(struct i915_vma *vma)
1300{
1301 struct drm_device *dev = vma->vm->dev;
1302 struct drm_i915_private *dev_priv = dev->dev_private;
1303 struct drm_i915_gem_object *obj = vma->obj;
1304 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1305
1306 if (obj->has_global_gtt_mapping) {
1307 vma->vm->clear_range(vma->vm, entry,
1308 vma->obj->base.size >> PAGE_SHIFT,
1309 true);
1310 obj->has_global_gtt_mapping = 0;
1311 }
1312
1313 if (obj->has_aliasing_ppgtt_mapping) {
1314 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1315 appgtt->base.clear_range(&appgtt->base,
1316 entry,
1317 obj->base.size >> PAGE_SHIFT,
1318 true);
1319 obj->has_aliasing_ppgtt_mapping = 0;
1320 }
Daniel Vetter74163902012-02-15 23:50:21 +01001321}
1322
1323void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1324{
Ben Widawsky5c042282011-10-17 15:51:55 -07001325 struct drm_device *dev = obj->base.dev;
1326 struct drm_i915_private *dev_priv = dev->dev_private;
1327 bool interruptible;
1328
1329 interruptible = do_idling(dev_priv);
1330
Chris Wilson9da3da62012-06-01 15:20:22 +01001331 if (!obj->has_dma_mapping)
1332 dma_unmap_sg(&dev->pdev->dev,
1333 obj->pages->sgl, obj->pages->nents,
1334 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001335
1336 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001337}
Daniel Vetter644ec022012-03-26 09:45:40 +02001338
Chris Wilson42d6ab42012-07-26 11:49:32 +01001339static void i915_gtt_color_adjust(struct drm_mm_node *node,
1340 unsigned long color,
1341 unsigned long *start,
1342 unsigned long *end)
1343{
1344 if (node->color != color)
1345 *start += 4096;
1346
1347 if (!list_empty(&node->node_list)) {
1348 node = list_entry(node->node_list.next,
1349 struct drm_mm_node,
1350 node_list);
1351 if (node->allocated && node->color != color)
1352 *end -= 4096;
1353 }
1354}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001355
Ben Widawskyd7e50082012-12-18 10:31:25 -08001356void i915_gem_setup_global_gtt(struct drm_device *dev,
1357 unsigned long start,
1358 unsigned long mappable_end,
1359 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001360{
Ben Widawskye78891c2013-01-25 16:41:04 -08001361 /* Let GEM Manage all of the aperture.
1362 *
1363 * However, leave one page at the end still bound to the scratch page.
1364 * There are a number of places where the hardware apparently prefetches
1365 * past the end of the object, and we've seen multiple hangs with the
1366 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1367 * aperture. One page should be enough to keep any prefetching inside
1368 * of the aperture.
1369 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001370 struct drm_i915_private *dev_priv = dev->dev_private;
1371 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001372 struct drm_mm_node *entry;
1373 struct drm_i915_gem_object *obj;
1374 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +02001375
Ben Widawsky35451cb2013-01-17 12:45:13 -08001376 BUG_ON(mappable_end > end);
1377
Chris Wilsoned2f3452012-11-15 11:32:19 +00001378 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001379 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001380 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001381 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001382
Chris Wilsoned2f3452012-11-15 11:32:19 +00001383 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001384 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001385 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001386 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -07001387 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001388 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001389
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001390 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001391 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001392 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001393 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +00001394 obj->has_global_gtt_mapping = 1;
1395 }
1396
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001397 dev_priv->gtt.base.start = start;
1398 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001399
Chris Wilsoned2f3452012-11-15 11:32:19 +00001400 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001401 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001402 const unsigned long count = (hole_end - hole_start) / PAGE_SIZE;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001403 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1404 hole_start, hole_end);
Ben Widawsky828c7902013-10-16 09:21:30 -07001405 ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001406 }
1407
1408 /* And finally clear the reserved guard page */
Ben Widawsky828c7902013-10-16 09:21:30 -07001409 ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001410}
1411
Ben Widawskyd7e50082012-12-18 10:31:25 -08001412void i915_gem_init_global_gtt(struct drm_device *dev)
1413{
1414 struct drm_i915_private *dev_priv = dev->dev_private;
1415 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001416
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001417 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001418 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001419
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001420 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001421}
1422
1423static int setup_scratch_page(struct drm_device *dev)
1424{
1425 struct drm_i915_private *dev_priv = dev->dev_private;
1426 struct page *page;
1427 dma_addr_t dma_addr;
1428
1429 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1430 if (page == NULL)
1431 return -ENOMEM;
1432 get_page(page);
1433 set_pages_uc(page, 1);
1434
1435#ifdef CONFIG_INTEL_IOMMU
1436 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1437 PCI_DMA_BIDIRECTIONAL);
1438 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1439 return -EINVAL;
1440#else
1441 dma_addr = page_to_phys(page);
1442#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001443 dev_priv->gtt.base.scratch.page = page;
1444 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001445
1446 return 0;
1447}
1448
1449static void teardown_scratch_page(struct drm_device *dev)
1450{
1451 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001452 struct page *page = dev_priv->gtt.base.scratch.page;
1453
1454 set_pages_wb(page, 1);
1455 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001456 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001457 put_page(page);
1458 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001459}
1460
1461static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1462{
1463 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1464 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1465 return snb_gmch_ctl << 20;
1466}
1467
Ben Widawsky9459d252013-11-03 16:53:55 -08001468static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1469{
1470 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1471 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1472 if (bdw_gmch_ctl)
1473 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky3a2ffb62013-11-07 21:40:51 -08001474 if (bdw_gmch_ctl > 4) {
1475 WARN_ON(!i915_preliminary_hw_support);
1476 return 4<<20;
1477 }
1478
Ben Widawsky9459d252013-11-03 16:53:55 -08001479 return bdw_gmch_ctl << 20;
1480}
1481
Ben Widawskybaa09f52013-01-24 13:49:57 -08001482static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001483{
1484 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1485 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1486 return snb_gmch_ctl << 25; /* 32 MB units */
1487}
1488
Ben Widawsky9459d252013-11-03 16:53:55 -08001489static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1490{
1491 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1492 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1493 return bdw_gmch_ctl << 25; /* 32 MB units */
1494}
1495
Ben Widawsky63340132013-11-04 19:32:22 -08001496static int ggtt_probe_common(struct drm_device *dev,
1497 size_t gtt_size)
1498{
1499 struct drm_i915_private *dev_priv = dev->dev_private;
1500 phys_addr_t gtt_bus_addr;
1501 int ret;
1502
1503 /* For Modern GENs the PTEs and register space are split in the BAR */
1504 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
1505 (pci_resource_len(dev->pdev, 0) / 2);
1506
1507 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
1508 if (!dev_priv->gtt.gsm) {
1509 DRM_ERROR("Failed to map the gtt page table\n");
1510 return -ENOMEM;
1511 }
1512
1513 ret = setup_scratch_page(dev);
1514 if (ret) {
1515 DRM_ERROR("Scratch setup failed\n");
1516 /* iounmap will also get called at remove, but meh */
1517 iounmap(dev_priv->gtt.gsm);
1518 }
1519
1520 return ret;
1521}
1522
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001523/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1524 * bits. When using advanced contexts each context stores its own PAT, but
1525 * writing this data shouldn't be harmful even in those cases. */
1526static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1527{
1528#define GEN8_PPAT_UC (0<<0)
1529#define GEN8_PPAT_WC (1<<0)
1530#define GEN8_PPAT_WT (2<<0)
1531#define GEN8_PPAT_WB (3<<0)
1532#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
1533/* FIXME(BDW): Bspec is completely confused about cache control bits. */
1534#define GEN8_PPAT_LLC (1<<2)
1535#define GEN8_PPAT_LLCELLC (2<<2)
1536#define GEN8_PPAT_LLCeLLC (3<<2)
1537#define GEN8_PPAT_AGE(x) (x<<4)
1538#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
1539 uint64_t pat;
1540
1541 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1542 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1543 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1544 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1545 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1546 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1547 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1548 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1549
1550 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1551 * write would work. */
1552 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1553 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1554}
1555
Ben Widawsky63340132013-11-04 19:32:22 -08001556static int gen8_gmch_probe(struct drm_device *dev,
1557 size_t *gtt_total,
1558 size_t *stolen,
1559 phys_addr_t *mappable_base,
1560 unsigned long *mappable_end)
1561{
1562 struct drm_i915_private *dev_priv = dev->dev_private;
1563 unsigned int gtt_size;
1564 u16 snb_gmch_ctl;
1565 int ret;
1566
1567 /* TODO: We're not aware of mappable constraints on gen8 yet */
1568 *mappable_base = pci_resource_start(dev->pdev, 2);
1569 *mappable_end = pci_resource_len(dev->pdev, 2);
1570
1571 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1572 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1573
1574 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1575
1576 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1577
1578 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskyd31eb102013-11-02 21:07:17 -07001579 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001580
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001581 gen8_setup_private_ppat(dev_priv);
1582
Ben Widawsky63340132013-11-04 19:32:22 -08001583 ret = ggtt_probe_common(dev, gtt_size);
1584
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001585 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1586 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001587
1588 return ret;
1589}
1590
Ben Widawskybaa09f52013-01-24 13:49:57 -08001591static int gen6_gmch_probe(struct drm_device *dev,
1592 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001593 size_t *stolen,
1594 phys_addr_t *mappable_base,
1595 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001596{
1597 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001598 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001599 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001600 int ret;
1601
Ben Widawsky41907dd2013-02-08 11:32:47 -08001602 *mappable_base = pci_resource_start(dev->pdev, 2);
1603 *mappable_end = pci_resource_len(dev->pdev, 2);
1604
Ben Widawskybaa09f52013-01-24 13:49:57 -08001605 /* 64/512MB is the current min/max we actually know of, but this is just
1606 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001607 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001608 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001609 DRM_ERROR("Unknown GMADR size (%lx)\n",
1610 dev_priv->gtt.mappable_end);
1611 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001612 }
1613
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001614 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1615 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001616 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001617
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07001618 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001619
Ben Widawsky63340132013-11-04 19:32:22 -08001620 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001621 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
1622
Ben Widawsky63340132013-11-04 19:32:22 -08001623 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001624
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001625 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1626 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001627
1628 return ret;
1629}
1630
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001631static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001632{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001633
1634 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08001635
1636 drm_mm_takedown(&vm->mm);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001637 iounmap(gtt->gsm);
1638 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001639}
1640
1641static int i915_gmch_probe(struct drm_device *dev,
1642 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001643 size_t *stolen,
1644 phys_addr_t *mappable_base,
1645 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001646{
1647 struct drm_i915_private *dev_priv = dev->dev_private;
1648 int ret;
1649
Ben Widawskybaa09f52013-01-24 13:49:57 -08001650 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1651 if (!ret) {
1652 DRM_ERROR("failed to set up gmch\n");
1653 return -EIO;
1654 }
1655
Ben Widawsky41907dd2013-02-08 11:32:47 -08001656 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001657
1658 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001659 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001660
1661 return 0;
1662}
1663
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001664static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001665{
1666 intel_gmch_remove();
1667}
1668
1669int i915_gem_gtt_init(struct drm_device *dev)
1670{
1671 struct drm_i915_private *dev_priv = dev->dev_private;
1672 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001673 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001674
Ben Widawskybaa09f52013-01-24 13:49:57 -08001675 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001676 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001677 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08001678 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001679 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001680 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001681 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001682 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001683 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001684 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001685 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001686 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001687 else if (INTEL_INFO(dev)->gen >= 7)
1688 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001689 else
Chris Wilson350ec882013-08-06 13:17:02 +01001690 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08001691 } else {
1692 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1693 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001694 }
1695
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001696 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001697 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001698 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001699 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001700
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001701 gtt->base.dev = dev;
1702
Ben Widawskybaa09f52013-01-24 13:49:57 -08001703 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001704 DRM_INFO("Memory usable by graphics device = %zdM\n",
1705 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001706 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1707 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001708
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001709 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001710}
Ben Widawsky6f65e292013-12-06 14:10:56 -08001711
1712static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
1713 struct i915_address_space *vm)
1714{
1715 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
1716 if (vma == NULL)
1717 return ERR_PTR(-ENOMEM);
1718
1719 INIT_LIST_HEAD(&vma->vma_link);
1720 INIT_LIST_HEAD(&vma->mm_list);
1721 INIT_LIST_HEAD(&vma->exec_list);
1722 vma->vm = vm;
1723 vma->obj = obj;
1724
1725 switch (INTEL_INFO(vm->dev)->gen) {
1726 case 8:
1727 case 7:
1728 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001729 if (i915_is_ggtt(vm)) {
1730 vma->unbind_vma = ggtt_unbind_vma;
1731 vma->bind_vma = ggtt_bind_vma;
1732 } else {
1733 vma->unbind_vma = ppgtt_unbind_vma;
1734 vma->bind_vma = ppgtt_bind_vma;
1735 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08001736 break;
1737 case 5:
1738 case 4:
1739 case 3:
1740 case 2:
1741 BUG_ON(!i915_is_ggtt(vm));
1742 vma->unbind_vma = i915_ggtt_unbind_vma;
1743 vma->bind_vma = i915_ggtt_bind_vma;
1744 break;
1745 default:
1746 BUG();
1747 }
1748
1749 /* Keep GGTT vmas first to make debug easier */
1750 if (i915_is_ggtt(vm))
1751 list_add(&vma->vma_link, &obj->vma_list);
1752 else
1753 list_add_tail(&vma->vma_link, &obj->vma_list);
1754
1755 return vma;
1756}
1757
1758struct i915_vma *
1759i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
1760 struct i915_address_space *vm)
1761{
1762 struct i915_vma *vma;
1763
1764 vma = i915_gem_obj_to_vma(obj, vm);
1765 if (!vma)
1766 vma = __i915_gem_vma_create(obj, vm);
1767
1768 return vma;
1769}