blob: 5704d6437c646d775ec8adc68a1343c779f7ce7f [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 Wilson651d7942013-08-08 14:41:10 +010060#define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080061
Ben Widawsky37aca442013-11-04 20:47:32 -080062#define GEN8_PDES_PER_PAGE (PAGE_SIZE / sizeof(gen8_ppgtt_pde_t))
63#define GEN8_LEGACY_PDPS 4
64
Ben Widawskyfbe5d362013-11-04 19:56:49 -080065#define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD)
66#define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */
67#define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */
68#define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */
69
Ben Widawsky94ec8f62013-11-02 21:07:18 -070070static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
71 enum i915_cache_level level,
72 bool valid)
73{
74 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
75 pte |= addr;
Ben Widawskyfbe5d362013-11-04 19:56:49 -080076 if (level != I915_CACHE_NONE)
77 pte |= PPAT_CACHED_INDEX;
78 else
79 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky94ec8f62013-11-02 21:07:18 -070080 return pte;
81}
82
Chris Wilson350ec882013-08-06 13:17:02 +010083static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -070084 enum i915_cache_level level,
85 bool valid)
Ben Widawsky54d12522012-09-24 16:44:32 -070086{
Ben Widawskyb35b3802013-10-16 09:18:21 -070087 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -070088 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -070089
90 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +010091 case I915_CACHE_L3_LLC:
92 case I915_CACHE_LLC:
93 pte |= GEN6_PTE_CACHE_LLC;
94 break;
95 case I915_CACHE_NONE:
96 pte |= GEN6_PTE_UNCACHED;
97 break;
98 default:
99 WARN_ON(1);
100 }
101
102 return pte;
103}
104
105static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700106 enum i915_cache_level level,
107 bool valid)
Chris Wilson350ec882013-08-06 13:17:02 +0100108{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700109 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100110 pte |= GEN6_PTE_ADDR_ENCODE(addr);
111
112 switch (level) {
113 case I915_CACHE_L3_LLC:
114 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700115 break;
116 case I915_CACHE_LLC:
117 pte |= GEN6_PTE_CACHE_LLC;
118 break;
119 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700120 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700121 break;
122 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100123 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700124 }
125
Ben Widawsky54d12522012-09-24 16:44:32 -0700126 return pte;
127}
128
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700129#define BYT_PTE_WRITEABLE (1 << 1)
130#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
131
Ben Widawsky80a74f72013-06-27 16:30:19 -0700132static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700133 enum i915_cache_level level,
134 bool valid)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700135{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700136 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700137 pte |= GEN6_PTE_ADDR_ENCODE(addr);
138
139 /* Mark the page as writeable. Other platforms don't have a
140 * setting for read-only/writable, so this matches that behavior.
141 */
142 pte |= BYT_PTE_WRITEABLE;
143
144 if (level != I915_CACHE_NONE)
145 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
146
147 return pte;
148}
149
Ben Widawsky80a74f72013-06-27 16:30:19 -0700150static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700151 enum i915_cache_level level,
152 bool valid)
Kenneth Graunke91197082013-04-22 00:53:51 -0700153{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700154 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700155 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700156
157 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700158 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700159
160 return pte;
161}
162
Ben Widawsky4d15c142013-07-04 11:02:06 -0700163static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700164 enum i915_cache_level level,
165 bool valid)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700166{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700167 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700168 pte |= HSW_PTE_ADDR_ENCODE(addr);
169
Chris Wilson651d7942013-08-08 14:41:10 +0100170 switch (level) {
171 case I915_CACHE_NONE:
172 break;
173 case I915_CACHE_WT:
174 pte |= HSW_WT_ELLC_LLC_AGE0;
175 break;
176 default:
Ben Widawsky4d15c142013-07-04 11:02:06 -0700177 pte |= HSW_WB_ELLC_LLC_AGE0;
Chris Wilson651d7942013-08-08 14:41:10 +0100178 break;
179 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700180
181 return pte;
182}
183
Ben Widawsky37aca442013-11-04 20:47:32 -0800184static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
185{
186 struct i915_hw_ppgtt *ppgtt =
187 container_of(vm, struct i915_hw_ppgtt, base);
188 int i, j;
189
190 for (i = 0; i < ppgtt->num_pd_pages ; i++) {
191 if (ppgtt->pd_dma_addr[i]) {
192 pci_unmap_page(ppgtt->base.dev->pdev,
193 ppgtt->pd_dma_addr[i],
194 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
195
196 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
197 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
198 if (addr)
199 pci_unmap_page(ppgtt->base.dev->pdev,
200 addr,
201 PAGE_SIZE,
202 PCI_DMA_BIDIRECTIONAL);
203
204 }
205 }
206 kfree(ppgtt->gen8_pt_dma_addr[i]);
207 }
208
209 __free_pages(ppgtt->gen8_pt_pages, ppgtt->num_pt_pages << PAGE_SHIFT);
210 __free_pages(ppgtt->pd_pages, ppgtt->num_pd_pages << PAGE_SHIFT);
211}
212
213/**
214 * GEN8 legacy ppgtt programming is accomplished through 4 PDP registers with a
215 * net effect resembling a 2-level page table in normal x86 terms. Each PDP
216 * represents 1GB of memory
217 * 4 * 512 * 512 * 4096 = 4GB legacy 32b address space.
218 *
219 * TODO: Do something with the size parameter
220 **/
221static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
222{
223 struct page *pt_pages;
224 int i, j, ret = -ENOMEM;
225 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
226 const int num_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
227
228 if (size % (1<<30))
229 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
230
231 /* FIXME: split allocation into smaller pieces. For now we only ever do
232 * this once, but with full PPGTT, the multiple contiguous allocations
233 * will be bad.
234 */
235 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
236 if (!ppgtt->pd_pages)
237 return -ENOMEM;
238
239 pt_pages = alloc_pages(GFP_KERNEL, get_order(num_pt_pages << PAGE_SHIFT));
240 if (!pt_pages) {
241 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
242 return -ENOMEM;
243 }
244
245 ppgtt->gen8_pt_pages = pt_pages;
246 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
247 ppgtt->num_pt_pages = 1 << get_order(num_pt_pages << PAGE_SHIFT);
248 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
249 ppgtt->base.clear_range = NULL;
250 ppgtt->base.insert_entries = NULL;
251 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
252
253 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
254
255 /*
256 * - Create a mapping for the page directories.
257 * - For each page directory:
258 * allocate space for page table mappings.
259 * map each page table
260 */
261 for (i = 0; i < max_pdp; i++) {
262 dma_addr_t temp;
263 temp = pci_map_page(ppgtt->base.dev->pdev,
264 &ppgtt->pd_pages[i], 0,
265 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
266 if (pci_dma_mapping_error(ppgtt->base.dev->pdev, temp))
267 goto err_out;
268
269 ppgtt->pd_dma_addr[i] = temp;
270
271 ppgtt->gen8_pt_dma_addr[i] = kmalloc(sizeof(dma_addr_t) * GEN8_PDES_PER_PAGE, GFP_KERNEL);
272 if (!ppgtt->gen8_pt_dma_addr[i])
273 goto err_out;
274
275 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
276 struct page *p = &pt_pages[i * GEN8_PDES_PER_PAGE + j];
277 temp = pci_map_page(ppgtt->base.dev->pdev,
278 p, 0, PAGE_SIZE,
279 PCI_DMA_BIDIRECTIONAL);
280
281 if (pci_dma_mapping_error(ppgtt->base.dev->pdev, temp))
282 goto err_out;
283
284 ppgtt->gen8_pt_dma_addr[i][j] = temp;
285 }
286 }
287
288 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
289 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
290 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
291 ppgtt->num_pt_pages,
292 (ppgtt->num_pt_pages - num_pt_pages) +
293 size % (1<<30));
294 return -ENOSYS; /* Not ready yet */
295
296err_out:
297 ppgtt->base.cleanup(&ppgtt->base);
298 return ret;
299}
300
Ben Widawsky3e302542013-04-23 23:15:32 -0700301static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700302{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700303 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700304 gen6_gtt_pte_t __iomem *pd_addr;
305 uint32_t pd_entry;
306 int i;
307
Ben Widawsky0a732872013-04-23 23:15:30 -0700308 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700309 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
310 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
311 for (i = 0; i < ppgtt->num_pd_entries; i++) {
312 dma_addr_t pt_addr;
313
314 pt_addr = ppgtt->pt_dma_addr[i];
315 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
316 pd_entry |= GEN6_PDE_VALID;
317
318 writel(pd_entry, pd_addr + i);
319 }
320 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700321}
322
323static int gen6_ppgtt_enable(struct drm_device *dev)
324{
325 drm_i915_private_t *dev_priv = dev->dev_private;
326 uint32_t pd_offset;
327 struct intel_ring_buffer *ring;
328 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
329 int i;
330
331 BUG_ON(ppgtt->pd_offset & 0x3f);
332
333 gen6_write_pdes(ppgtt);
Ben Widawsky61973492013-04-08 18:43:54 -0700334
335 pd_offset = ppgtt->pd_offset;
336 pd_offset /= 64; /* in cachelines, */
337 pd_offset <<= 16;
338
339 if (INTEL_INFO(dev)->gen == 6) {
340 uint32_t ecochk, gab_ctl, ecobits;
341
342 ecobits = I915_READ(GAC_ECO_BITS);
Ville Syrjälä3b9d7882013-04-04 15:13:40 +0300343 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
344 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700345
346 gab_ctl = I915_READ(GAB_CTL);
347 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
348
349 ecochk = I915_READ(GAM_ECOCHK);
350 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
351 ECOCHK_PPGTT_CACHE64B);
352 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
353 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjäläa6f429a2013-04-04 15:13:42 +0300354 uint32_t ecochk, ecobits;
Ville Syrjäläa65c2fc2013-04-04 15:13:41 +0300355
356 ecobits = I915_READ(GAC_ECO_BITS);
357 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
358
Ville Syrjäläa6f429a2013-04-04 15:13:42 +0300359 ecochk = I915_READ(GAM_ECOCHK);
360 if (IS_HASWELL(dev)) {
361 ecochk |= ECOCHK_PPGTT_WB_HSW;
362 } else {
363 ecochk |= ECOCHK_PPGTT_LLC_IVB;
364 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
365 }
366 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawsky61973492013-04-08 18:43:54 -0700367 /* GFX_MODE is per-ring on gen7+ */
368 }
369
370 for_each_ring(ring, dev_priv, i) {
371 if (INTEL_INFO(dev)->gen >= 7)
372 I915_WRITE(RING_MODE_GEN7(ring),
373 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
374
375 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
376 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
377 }
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700378 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700379}
380
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100381/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700382static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100383 unsigned first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700384 unsigned num_entries,
385 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100386{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700387 struct i915_hw_ppgtt *ppgtt =
388 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700389 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Daniel Vettera15326a2013-03-19 23:48:39 +0100390 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100391 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
392 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100393
Ben Widawskyb35b3802013-10-16 09:18:21 -0700394 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100395
Daniel Vetter7bddb012012-02-09 17:15:47 +0100396 while (num_entries) {
397 last_pte = first_pte + num_entries;
398 if (last_pte > I915_PPGTT_PT_ENTRIES)
399 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100400
Daniel Vettera15326a2013-03-19 23:48:39 +0100401 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100402
403 for (i = first_pte; i < last_pte; i++)
404 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100405
406 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100407
Daniel Vetter7bddb012012-02-09 17:15:47 +0100408 num_entries -= last_pte - first_pte;
409 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100410 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100411 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100412}
413
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700414static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800415 struct sg_table *pages,
416 unsigned first_entry,
417 enum i915_cache_level cache_level)
418{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700419 struct i915_hw_ppgtt *ppgtt =
420 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700421 gen6_gtt_pte_t *pt_vaddr;
Daniel Vettera15326a2013-03-19 23:48:39 +0100422 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200423 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
424 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800425
Daniel Vettera15326a2013-03-19 23:48:39 +0100426 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200427 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
428 dma_addr_t page_addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800429
Imre Deak2db76d72013-03-26 15:14:18 +0200430 page_addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -0700431 pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200432 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
433 kunmap_atomic(pt_vaddr);
Daniel Vettera15326a2013-03-19 23:48:39 +0100434 act_pt++;
435 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200436 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800437
Daniel Vetterdef886c2013-01-24 14:44:56 -0800438 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800439 }
Imre Deak6e995e22013-02-18 19:28:04 +0200440 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800441}
442
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700443static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100444{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700445 struct i915_hw_ppgtt *ppgtt =
446 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -0800447 int i;
448
Ben Widawsky93bd8642013-07-16 16:50:06 -0700449 drm_mm_takedown(&ppgtt->base.mm);
450
Daniel Vetter3440d262013-01-24 13:49:56 -0800451 if (ppgtt->pt_dma_addr) {
452 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700453 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800454 ppgtt->pt_dma_addr[i],
455 4096, PCI_DMA_BIDIRECTIONAL);
456 }
457
458 kfree(ppgtt->pt_dma_addr);
459 for (i = 0; i < ppgtt->num_pd_entries; i++)
460 __free_page(ppgtt->pt_pages[i]);
461 kfree(ppgtt->pt_pages);
462 kfree(ppgtt);
463}
464
465static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
466{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700467 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100468 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100469 unsigned first_pd_entry_in_global_pt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100470 int i;
471 int ret = -ENOMEM;
472
473 /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024
474 * entries. For aliasing ppgtt support we just steal them at the end for
475 * now. */
Daniel Vettere1b73cb2013-05-21 09:52:16 +0200476 first_pd_entry_in_global_pt = gtt_total_entries(dev_priv->gtt);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100477
Chris Wilson08c45262013-07-30 19:04:37 +0100478 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700479 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky61973492013-04-08 18:43:54 -0700480 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700481 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
482 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
483 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
484 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vettera1e22652013-09-21 00:35:38 +0200485 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100486 GFP_KERNEL);
487 if (!ppgtt->pt_pages)
Daniel Vetter3440d262013-01-24 13:49:56 -0800488 return -ENOMEM;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100489
490 for (i = 0; i < ppgtt->num_pd_entries; i++) {
491 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
492 if (!ppgtt->pt_pages[i])
493 goto err_pt_alloc;
494 }
495
Daniel Vettera1e22652013-09-21 00:35:38 +0200496 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800497 GFP_KERNEL);
498 if (!ppgtt->pt_dma_addr)
499 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100500
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800501 for (i = 0; i < ppgtt->num_pd_entries; i++) {
502 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200503
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800504 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
505 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100506
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800507 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
508 ret = -EIO;
509 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100510
Daniel Vetter211c5682012-04-10 17:29:17 +0200511 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800512 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100513 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100514
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700515 ppgtt->base.clear_range(&ppgtt->base, 0,
Ben Widawsky828c7902013-10-16 09:21:30 -0700516 ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100517
Ben Widawskye7c2b582013-04-08 18:43:48 -0700518 ppgtt->pd_offset = first_pd_entry_in_global_pt * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100519
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100520 return 0;
521
522err_pd_pin:
523 if (ppgtt->pt_dma_addr) {
524 for (i--; i >= 0; i--)
525 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
526 4096, PCI_DMA_BIDIRECTIONAL);
527 }
528err_pt_alloc:
529 kfree(ppgtt->pt_dma_addr);
530 for (i = 0; i < ppgtt->num_pd_entries; i++) {
531 if (ppgtt->pt_pages[i])
532 __free_page(ppgtt->pt_pages[i]);
533 }
534 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800535
536 return ret;
537}
538
539static int i915_gem_init_aliasing_ppgtt(struct drm_device *dev)
540{
541 struct drm_i915_private *dev_priv = dev->dev_private;
542 struct i915_hw_ppgtt *ppgtt;
543 int ret;
544
545 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
546 if (!ppgtt)
547 return -ENOMEM;
548
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700549 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -0800550
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700551 if (INTEL_INFO(dev)->gen < 8)
552 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -0700553 else if (IS_GEN8(dev))
Ben Widawsky37aca442013-11-04 20:47:32 -0800554 ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700555 else
556 BUG();
557
Daniel Vetter3440d262013-01-24 13:49:56 -0800558 if (ret)
559 kfree(ppgtt);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700560 else {
Daniel Vetter3440d262013-01-24 13:49:56 -0800561 dev_priv->mm.aliasing_ppgtt = ppgtt;
Ben Widawsky93bd8642013-07-16 16:50:06 -0700562 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
563 ppgtt->base.total);
564 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100565
566 return ret;
567}
568
569void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev)
570{
571 struct drm_i915_private *dev_priv = dev->dev_private;
572 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100573
574 if (!ppgtt)
575 return;
576
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700577 ppgtt->base.cleanup(&ppgtt->base);
Ben Widawsky5963cf02013-04-08 18:43:55 -0700578 dev_priv->mm.aliasing_ppgtt = NULL;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100579}
580
Daniel Vetter7bddb012012-02-09 17:15:47 +0100581void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt,
582 struct drm_i915_gem_object *obj,
583 enum i915_cache_level cache_level)
584{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700585 ppgtt->base.insert_entries(&ppgtt->base, obj->pages,
586 i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT,
587 cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100588}
589
590void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt,
591 struct drm_i915_gem_object *obj)
592{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700593 ppgtt->base.clear_range(&ppgtt->base,
594 i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT,
Ben Widawsky828c7902013-10-16 09:21:30 -0700595 obj->base.size >> PAGE_SHIFT,
596 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100597}
598
Ben Widawskya81cc002013-01-18 12:30:31 -0800599extern int intel_iommu_gfx_mapped;
600/* Certain Gen5 chipsets require require idling the GPU before
601 * unmapping anything from the GTT when VT-d is enabled.
602 */
603static inline bool needs_idle_maps(struct drm_device *dev)
604{
605#ifdef CONFIG_INTEL_IOMMU
606 /* Query intel_iommu to see if we need the workaround. Presumably that
607 * was loaded first.
608 */
609 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
610 return true;
611#endif
612 return false;
613}
614
Ben Widawsky5c042282011-10-17 15:51:55 -0700615static bool do_idling(struct drm_i915_private *dev_priv)
616{
617 bool ret = dev_priv->mm.interruptible;
618
Ben Widawskya81cc002013-01-18 12:30:31 -0800619 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700620 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700621 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700622 DRM_ERROR("Couldn't idle GPU\n");
623 /* Wait a bit, in hopes it avoids the hang */
624 udelay(10);
625 }
626 }
627
628 return ret;
629}
630
631static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
632{
Ben Widawskya81cc002013-01-18 12:30:31 -0800633 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -0700634 dev_priv->mm.interruptible = interruptible;
635}
636
Ben Widawsky828c7902013-10-16 09:21:30 -0700637void i915_check_and_clear_faults(struct drm_device *dev)
638{
639 struct drm_i915_private *dev_priv = dev->dev_private;
640 struct intel_ring_buffer *ring;
641 int i;
642
643 if (INTEL_INFO(dev)->gen < 6)
644 return;
645
646 for_each_ring(ring, dev_priv, i) {
647 u32 fault_reg;
648 fault_reg = I915_READ(RING_FAULT_REG(ring));
649 if (fault_reg & RING_FAULT_VALID) {
650 DRM_DEBUG_DRIVER("Unexpected fault\n"
651 "\tAddr: 0x%08lx\\n"
652 "\tAddress space: %s\n"
653 "\tSource ID: %d\n"
654 "\tType: %d\n",
655 fault_reg & PAGE_MASK,
656 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
657 RING_FAULT_SRCID(fault_reg),
658 RING_FAULT_FAULT_TYPE(fault_reg));
659 I915_WRITE(RING_FAULT_REG(ring),
660 fault_reg & ~RING_FAULT_VALID);
661 }
662 }
663 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
664}
665
666void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
667{
668 struct drm_i915_private *dev_priv = dev->dev_private;
669
670 /* Don't bother messing with faults pre GEN6 as we have little
671 * documentation supporting that it's a good idea.
672 */
673 if (INTEL_INFO(dev)->gen < 6)
674 return;
675
676 i915_check_and_clear_faults(dev);
677
678 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
679 dev_priv->gtt.base.start / PAGE_SIZE,
680 dev_priv->gtt.base.total / PAGE_SIZE,
681 false);
682}
683
Daniel Vetter76aaf222010-11-05 22:23:30 +0100684void i915_gem_restore_gtt_mappings(struct drm_device *dev)
685{
686 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000687 struct drm_i915_gem_object *obj;
Daniel Vetter76aaf222010-11-05 22:23:30 +0100688
Ben Widawsky828c7902013-10-16 09:21:30 -0700689 i915_check_and_clear_faults(dev);
690
Chris Wilsonbee4a182011-01-21 10:54:32 +0000691 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700692 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
693 dev_priv->gtt.base.start / PAGE_SIZE,
Ben Widawsky828c7902013-10-16 09:21:30 -0700694 dev_priv->gtt.base.total / PAGE_SIZE,
695 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +0000696
Ben Widawsky35c20a62013-05-31 11:28:48 -0700697 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Chris Wilson2c225692013-08-09 12:26:45 +0100698 i915_gem_clflush_object(obj, obj->pin_display);
Daniel Vetter74163902012-02-15 23:50:21 +0100699 i915_gem_gtt_bind_object(obj, obj->cache_level);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100700 }
701
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800702 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100703}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100704
Daniel Vetter74163902012-02-15 23:50:21 +0100705int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100706{
Chris Wilson9da3da62012-06-01 15:20:22 +0100707 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +0100708 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +0100709
710 if (!dma_map_sg(&obj->base.dev->pdev->dev,
711 obj->pages->sgl, obj->pages->nents,
712 PCI_DMA_BIDIRECTIONAL))
713 return -ENOSPC;
714
715 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100716}
717
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700718static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
719{
720#ifdef writeq
721 writeq(pte, addr);
722#else
723 iowrite32((u32)pte, addr);
724 iowrite32(pte >> 32, addr + 4);
725#endif
726}
727
728static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
729 struct sg_table *st,
730 unsigned int first_entry,
731 enum i915_cache_level level)
732{
733 struct drm_i915_private *dev_priv = vm->dev->dev_private;
734 gen8_gtt_pte_t __iomem *gtt_entries =
735 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
736 int i = 0;
737 struct sg_page_iter sg_iter;
738 dma_addr_t addr;
739
740 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
741 addr = sg_dma_address(sg_iter.sg) +
742 (sg_iter.sg_pgoffset << PAGE_SHIFT);
743 gen8_set_pte(&gtt_entries[i],
744 gen8_pte_encode(addr, level, true));
745 i++;
746 }
747
748 /*
749 * XXX: This serves as a posting read to make sure that the PTE has
750 * actually been updated. There is some concern that even though
751 * registers and PTEs are within the same BAR that they are potentially
752 * of NUMA access patterns. Therefore, even with the way we assume
753 * hardware should work, we must keep this posting read for paranoia.
754 */
755 if (i != 0)
756 WARN_ON(readq(&gtt_entries[i-1])
757 != gen8_pte_encode(addr, level, true));
758
759#if 0 /* TODO: Still needed on GEN8? */
760 /* This next bit makes the above posting read even more important. We
761 * want to flush the TLBs only after we're certain all the PTE updates
762 * have finished.
763 */
764 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
765 POSTING_READ(GFX_FLSH_CNTL_GEN6);
766#endif
767}
768
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800769/*
770 * Binds an object into the global gtt with the specified cache level. The object
771 * will be accessible to the GPU via commands whose operands reference offsets
772 * within the global GTT as well as accessible by the GPU through the GMADR
773 * mapped BAR (dev_priv->mm.gtt->gtt).
774 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700775static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800776 struct sg_table *st,
777 unsigned int first_entry,
778 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800779{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700780 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -0700781 gen6_gtt_pte_t __iomem *gtt_entries =
782 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +0200783 int i = 0;
784 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800785 dma_addr_t addr;
786
Imre Deak6e995e22013-02-18 19:28:04 +0200787 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +0200788 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -0700789 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +0200790 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800791 }
792
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800793 /* XXX: This serves as a posting read to make sure that the PTE has
794 * actually been updated. There is some concern that even though
795 * registers and PTEs are within the same BAR that they are potentially
796 * of NUMA access patterns. Therefore, even with the way we assume
797 * hardware should work, we must keep this posting read for paranoia.
798 */
799 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700800 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -0700801 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -0800802
803 /* This next bit makes the above posting read even more important. We
804 * want to flush the TLBs only after we're certain all the PTE updates
805 * have finished.
806 */
807 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
808 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800809}
810
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700811static void gen8_ggtt_clear_range(struct i915_address_space *vm,
812 unsigned int first_entry,
813 unsigned int num_entries,
814 bool use_scratch)
815{
816 struct drm_i915_private *dev_priv = vm->dev->dev_private;
817 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
818 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
819 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
820 int i;
821
822 if (WARN(num_entries > max_entries,
823 "First entry = %d; Num entries = %d (max=%d)\n",
824 first_entry, num_entries, max_entries))
825 num_entries = max_entries;
826
827 scratch_pte = gen8_pte_encode(vm->scratch.addr,
828 I915_CACHE_LLC,
829 use_scratch);
830 for (i = 0; i < num_entries; i++)
831 gen8_set_pte(&gtt_base[i], scratch_pte);
832 readl(gtt_base);
833}
834
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700835static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800836 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700837 unsigned int num_entries,
838 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800839{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700840 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -0700841 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
842 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -0800843 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800844 int i;
845
846 if (WARN(num_entries > max_entries,
847 "First entry = %d; Num entries = %d (max=%d)\n",
848 first_entry, num_entries, max_entries))
849 num_entries = max_entries;
850
Ben Widawsky828c7902013-10-16 09:21:30 -0700851 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
852
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800853 for (i = 0; i < num_entries; i++)
854 iowrite32(scratch_pte, &gtt_base[i]);
855 readl(gtt_base);
856}
857
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700858static void i915_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800859 struct sg_table *st,
860 unsigned int pg_start,
861 enum i915_cache_level cache_level)
862{
863 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
864 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
865
866 intel_gtt_insert_sg_entries(st, pg_start, flags);
867
868}
869
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700870static void i915_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800871 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700872 unsigned int num_entries,
873 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800874{
875 intel_gtt_clear_range(first_entry, num_entries);
876}
877
878
Daniel Vetter74163902012-02-15 23:50:21 +0100879void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj,
880 enum i915_cache_level cache_level)
Chris Wilsond5bd1442011-04-14 06:48:26 +0100881{
882 struct drm_device *dev = obj->base.dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800883 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700884 const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800885
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700886 dev_priv->gtt.base.insert_entries(&dev_priv->gtt.base, obj->pages,
887 entry,
888 cache_level);
Chris Wilsond5bd1442011-04-14 06:48:26 +0100889
Daniel Vetter74898d72012-02-15 23:50:22 +0100890 obj->has_global_gtt_mapping = 1;
Chris Wilsond5bd1442011-04-14 06:48:26 +0100891}
892
Chris Wilson05394f32010-11-08 19:18:58 +0000893void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100894{
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800895 struct drm_device *dev = obj->base.dev;
896 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700897 const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800898
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700899 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
900 entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700901 obj->base.size >> PAGE_SHIFT,
902 true);
Daniel Vetter74898d72012-02-15 23:50:22 +0100903
904 obj->has_global_gtt_mapping = 0;
Daniel Vetter74163902012-02-15 23:50:21 +0100905}
906
907void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
908{
Ben Widawsky5c042282011-10-17 15:51:55 -0700909 struct drm_device *dev = obj->base.dev;
910 struct drm_i915_private *dev_priv = dev->dev_private;
911 bool interruptible;
912
913 interruptible = do_idling(dev_priv);
914
Chris Wilson9da3da62012-06-01 15:20:22 +0100915 if (!obj->has_dma_mapping)
916 dma_unmap_sg(&dev->pdev->dev,
917 obj->pages->sgl, obj->pages->nents,
918 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -0700919
920 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100921}
Daniel Vetter644ec022012-03-26 09:45:40 +0200922
Chris Wilson42d6ab42012-07-26 11:49:32 +0100923static void i915_gtt_color_adjust(struct drm_mm_node *node,
924 unsigned long color,
925 unsigned long *start,
926 unsigned long *end)
927{
928 if (node->color != color)
929 *start += 4096;
930
931 if (!list_empty(&node->node_list)) {
932 node = list_entry(node->node_list.next,
933 struct drm_mm_node,
934 node_list);
935 if (node->allocated && node->color != color)
936 *end -= 4096;
937 }
938}
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800939
Ben Widawskyd7e50082012-12-18 10:31:25 -0800940void i915_gem_setup_global_gtt(struct drm_device *dev,
941 unsigned long start,
942 unsigned long mappable_end,
943 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +0200944{
Ben Widawskye78891c2013-01-25 16:41:04 -0800945 /* Let GEM Manage all of the aperture.
946 *
947 * However, leave one page at the end still bound to the scratch page.
948 * There are a number of places where the hardware apparently prefetches
949 * past the end of the object, and we've seen multiple hangs with the
950 * GPU head pointer stuck in a batchbuffer bound at the last page of the
951 * aperture. One page should be enough to keep any prefetching inside
952 * of the aperture.
953 */
Ben Widawsky40d749802013-07-31 16:59:59 -0700954 struct drm_i915_private *dev_priv = dev->dev_private;
955 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +0000956 struct drm_mm_node *entry;
957 struct drm_i915_gem_object *obj;
958 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +0200959
Ben Widawsky35451cb2013-01-17 12:45:13 -0800960 BUG_ON(mappable_end > end);
961
Chris Wilsoned2f3452012-11-15 11:32:19 +0000962 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -0700963 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +0100964 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -0700965 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +0200966
Chris Wilsoned2f3452012-11-15 11:32:19 +0000967 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -0700968 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -0700969 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700970 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -0700971 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700972 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000973
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700974 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -0700975 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700976 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700977 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +0000978 obj->has_global_gtt_mapping = 1;
Ben Widawsky2f633152013-07-17 12:19:03 -0700979 list_add(&vma->vma_link, &obj->vma_list);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000980 }
981
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700982 dev_priv->gtt.base.start = start;
983 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +0200984
Chris Wilsoned2f3452012-11-15 11:32:19 +0000985 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -0700986 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700987 const unsigned long count = (hole_end - hole_start) / PAGE_SIZE;
Chris Wilsoned2f3452012-11-15 11:32:19 +0000988 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
989 hole_start, hole_end);
Ben Widawsky828c7902013-10-16 09:21:30 -0700990 ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000991 }
992
993 /* And finally clear the reserved guard page */
Ben Widawsky828c7902013-10-16 09:21:30 -0700994 ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800995}
996
Ben Widawskyd7e50082012-12-18 10:31:25 -0800997static bool
998intel_enable_ppgtt(struct drm_device *dev)
999{
1000 if (i915_enable_ppgtt >= 0)
1001 return i915_enable_ppgtt;
1002
1003#ifdef CONFIG_INTEL_IOMMU
1004 /* Disable ppgtt on SNB if VT-d is on. */
1005 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
1006 return false;
1007#endif
1008
1009 return true;
1010}
1011
1012void i915_gem_init_global_gtt(struct drm_device *dev)
1013{
1014 struct drm_i915_private *dev_priv = dev->dev_private;
1015 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001016
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001017 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001018 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001019
1020 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
Ben Widawskye78891c2013-01-25 16:41:04 -08001021 int ret;
Ben Widawsky3eb1c002013-04-08 18:43:52 -07001022
1023 if (INTEL_INFO(dev)->gen <= 7) {
1024 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
1025 * aperture accordingly when using aliasing ppgtt. */
Ben Widawsky6670a5a2013-06-27 16:30:04 -07001026 gtt_size -= GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE;
Ben Widawsky3eb1c002013-04-08 18:43:52 -07001027 }
Ben Widawskyd7e50082012-12-18 10:31:25 -08001028
1029 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
1030
1031 ret = i915_gem_init_aliasing_ppgtt(dev);
Ben Widawskye78891c2013-01-25 16:41:04 -08001032 if (!ret)
Ben Widawskyd7e50082012-12-18 10:31:25 -08001033 return;
Ben Widawskye78891c2013-01-25 16:41:04 -08001034
1035 DRM_ERROR("Aliased PPGTT setup failed %d\n", ret);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001036 drm_mm_takedown(&dev_priv->gtt.base.mm);
Ben Widawsky6670a5a2013-06-27 16:30:04 -07001037 gtt_size += GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001038 }
Ben Widawskye78891c2013-01-25 16:41:04 -08001039 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001040}
1041
1042static int setup_scratch_page(struct drm_device *dev)
1043{
1044 struct drm_i915_private *dev_priv = dev->dev_private;
1045 struct page *page;
1046 dma_addr_t dma_addr;
1047
1048 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1049 if (page == NULL)
1050 return -ENOMEM;
1051 get_page(page);
1052 set_pages_uc(page, 1);
1053
1054#ifdef CONFIG_INTEL_IOMMU
1055 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1056 PCI_DMA_BIDIRECTIONAL);
1057 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1058 return -EINVAL;
1059#else
1060 dma_addr = page_to_phys(page);
1061#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001062 dev_priv->gtt.base.scratch.page = page;
1063 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001064
1065 return 0;
1066}
1067
1068static void teardown_scratch_page(struct drm_device *dev)
1069{
1070 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001071 struct page *page = dev_priv->gtt.base.scratch.page;
1072
1073 set_pages_wb(page, 1);
1074 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001075 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001076 put_page(page);
1077 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001078}
1079
1080static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1081{
1082 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1083 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1084 return snb_gmch_ctl << 20;
1085}
1086
Ben Widawsky9459d252013-11-03 16:53:55 -08001087static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1088{
1089 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1090 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1091 if (bdw_gmch_ctl)
1092 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
1093 return bdw_gmch_ctl << 20;
1094}
1095
Ben Widawskybaa09f52013-01-24 13:49:57 -08001096static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001097{
1098 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1099 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1100 return snb_gmch_ctl << 25; /* 32 MB units */
1101}
1102
Ben Widawsky9459d252013-11-03 16:53:55 -08001103static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1104{
1105 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1106 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1107 return bdw_gmch_ctl << 25; /* 32 MB units */
1108}
1109
Ben Widawsky63340132013-11-04 19:32:22 -08001110static int ggtt_probe_common(struct drm_device *dev,
1111 size_t gtt_size)
1112{
1113 struct drm_i915_private *dev_priv = dev->dev_private;
1114 phys_addr_t gtt_bus_addr;
1115 int ret;
1116
1117 /* For Modern GENs the PTEs and register space are split in the BAR */
1118 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
1119 (pci_resource_len(dev->pdev, 0) / 2);
1120
1121 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
1122 if (!dev_priv->gtt.gsm) {
1123 DRM_ERROR("Failed to map the gtt page table\n");
1124 return -ENOMEM;
1125 }
1126
1127 ret = setup_scratch_page(dev);
1128 if (ret) {
1129 DRM_ERROR("Scratch setup failed\n");
1130 /* iounmap will also get called at remove, but meh */
1131 iounmap(dev_priv->gtt.gsm);
1132 }
1133
1134 return ret;
1135}
1136
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001137/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1138 * bits. When using advanced contexts each context stores its own PAT, but
1139 * writing this data shouldn't be harmful even in those cases. */
1140static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1141{
1142#define GEN8_PPAT_UC (0<<0)
1143#define GEN8_PPAT_WC (1<<0)
1144#define GEN8_PPAT_WT (2<<0)
1145#define GEN8_PPAT_WB (3<<0)
1146#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
1147/* FIXME(BDW): Bspec is completely confused about cache control bits. */
1148#define GEN8_PPAT_LLC (1<<2)
1149#define GEN8_PPAT_LLCELLC (2<<2)
1150#define GEN8_PPAT_LLCeLLC (3<<2)
1151#define GEN8_PPAT_AGE(x) (x<<4)
1152#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
1153 uint64_t pat;
1154
1155 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1156 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1157 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1158 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1159 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1160 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1161 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1162 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1163
1164 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1165 * write would work. */
1166 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1167 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1168}
1169
Ben Widawsky63340132013-11-04 19:32:22 -08001170static int gen8_gmch_probe(struct drm_device *dev,
1171 size_t *gtt_total,
1172 size_t *stolen,
1173 phys_addr_t *mappable_base,
1174 unsigned long *mappable_end)
1175{
1176 struct drm_i915_private *dev_priv = dev->dev_private;
1177 unsigned int gtt_size;
1178 u16 snb_gmch_ctl;
1179 int ret;
1180
1181 /* TODO: We're not aware of mappable constraints on gen8 yet */
1182 *mappable_base = pci_resource_start(dev->pdev, 2);
1183 *mappable_end = pci_resource_len(dev->pdev, 2);
1184
1185 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1186 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1187
1188 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1189
1190 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1191
1192 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskyd31eb102013-11-02 21:07:17 -07001193 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001194
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001195 gen8_setup_private_ppat(dev_priv);
1196
Ben Widawsky63340132013-11-04 19:32:22 -08001197 ret = ggtt_probe_common(dev, gtt_size);
1198
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001199 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1200 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001201
1202 return ret;
1203}
1204
Ben Widawskybaa09f52013-01-24 13:49:57 -08001205static int gen6_gmch_probe(struct drm_device *dev,
1206 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001207 size_t *stolen,
1208 phys_addr_t *mappable_base,
1209 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001210{
1211 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001212 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001213 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001214 int ret;
1215
Ben Widawsky41907dd2013-02-08 11:32:47 -08001216 *mappable_base = pci_resource_start(dev->pdev, 2);
1217 *mappable_end = pci_resource_len(dev->pdev, 2);
1218
Ben Widawskybaa09f52013-01-24 13:49:57 -08001219 /* 64/512MB is the current min/max we actually know of, but this is just
1220 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001221 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001222 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001223 DRM_ERROR("Unknown GMADR size (%lx)\n",
1224 dev_priv->gtt.mappable_end);
1225 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001226 }
1227
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001228 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1229 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001230 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001231
Ben Widawsky63340132013-11-04 19:32:22 -08001232 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001233
Ben Widawsky63340132013-11-04 19:32:22 -08001234 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
1235 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawskya93e4162013-04-08 18:43:47 -07001236
Ben Widawsky63340132013-11-04 19:32:22 -08001237 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001238
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001239 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1240 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001241
1242 return ret;
1243}
1244
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001245static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001246{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001247
1248 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
1249 iounmap(gtt->gsm);
1250 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001251}
1252
1253static int i915_gmch_probe(struct drm_device *dev,
1254 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001255 size_t *stolen,
1256 phys_addr_t *mappable_base,
1257 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001258{
1259 struct drm_i915_private *dev_priv = dev->dev_private;
1260 int ret;
1261
Ben Widawskybaa09f52013-01-24 13:49:57 -08001262 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1263 if (!ret) {
1264 DRM_ERROR("failed to set up gmch\n");
1265 return -EIO;
1266 }
1267
Ben Widawsky41907dd2013-02-08 11:32:47 -08001268 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001269
1270 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001271 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
1272 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001273
1274 return 0;
1275}
1276
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001277static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001278{
1279 intel_gmch_remove();
1280}
1281
1282int i915_gem_gtt_init(struct drm_device *dev)
1283{
1284 struct drm_i915_private *dev_priv = dev->dev_private;
1285 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001286 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001287
Ben Widawskybaa09f52013-01-24 13:49:57 -08001288 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001289 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001290 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08001291 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001292 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001293 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001294 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001295 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001296 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001297 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001298 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001299 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001300 else if (INTEL_INFO(dev)->gen >= 7)
1301 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001302 else
Chris Wilson350ec882013-08-06 13:17:02 +01001303 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08001304 } else {
1305 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1306 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001307 }
1308
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001309 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001310 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001311 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001312 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001313
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001314 gtt->base.dev = dev;
1315
Ben Widawskybaa09f52013-01-24 13:49:57 -08001316 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001317 DRM_INFO("Memory usable by graphics device = %zdM\n",
1318 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001319 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1320 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001321
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001322 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001323}