blob: 47765d26f15aa5d9e1aed1a30e15d648df1f462f [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 Widawsky6670a5a2013-06-27 16:30:04 -070034
Ben Widawsky26b1ff32012-11-04 09:21:31 -080035/* PPGTT stuff */
36#define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0))
Ben Widawsky0d8ff152013-07-04 11:02:03 -070037#define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0))
Ben Widawsky26b1ff32012-11-04 09:21:31 -080038
39#define GEN6_PDE_VALID (1 << 0)
40/* gen6+ has bit 11-4 for physical addr bit 39-32 */
41#define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
42
43#define GEN6_PTE_VALID (1 << 0)
44#define GEN6_PTE_UNCACHED (1 << 1)
45#define HSW_PTE_UNCACHED (0)
46#define GEN6_PTE_CACHE_LLC (2 << 1)
Chris Wilson350ec882013-08-06 13:17:02 +010047#define GEN7_PTE_CACHE_L3_LLC (3 << 1)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080048#define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
Ben Widawsky0d8ff152013-07-04 11:02:03 -070049#define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr)
50
51/* Cacheability Control is a 4-bit value. The low three bits are stored in *
52 * bits 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
53 */
54#define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \
55 (((bits) & 0x8) << (11 - 3)))
Ben Widawsky87a6b682013-08-04 23:47:29 -070056#define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2)
Ben Widawsky0d8ff152013-07-04 11:02:03 -070057#define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3)
Ben Widawsky4d15c142013-07-04 11:02:06 -070058#define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb)
Chris Wilson651d7942013-08-08 14:41:10 +010059#define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080060
Ben Widawskyfbe5d362013-11-04 19:56:49 -080061#define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD)
62#define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */
63#define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */
64#define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */
65
Ben Widawsky94ec8f62013-11-02 21:07:18 -070066static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
67 enum i915_cache_level level,
68 bool valid)
69{
70 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
71 pte |= addr;
Ben Widawskyfbe5d362013-11-04 19:56:49 -080072 if (level != I915_CACHE_NONE)
73 pte |= PPAT_CACHED_INDEX;
74 else
75 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky94ec8f62013-11-02 21:07:18 -070076 return pte;
77}
78
Chris Wilson350ec882013-08-06 13:17:02 +010079static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -070080 enum i915_cache_level level,
81 bool valid)
Ben Widawsky54d12522012-09-24 16:44:32 -070082{
Ben Widawskyb35b3802013-10-16 09:18:21 -070083 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -070084 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -070085
86 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +010087 case I915_CACHE_L3_LLC:
88 case I915_CACHE_LLC:
89 pte |= GEN6_PTE_CACHE_LLC;
90 break;
91 case I915_CACHE_NONE:
92 pte |= GEN6_PTE_UNCACHED;
93 break;
94 default:
95 WARN_ON(1);
96 }
97
98 return pte;
99}
100
101static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700102 enum i915_cache_level level,
103 bool valid)
Chris Wilson350ec882013-08-06 13:17:02 +0100104{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700105 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100106 pte |= GEN6_PTE_ADDR_ENCODE(addr);
107
108 switch (level) {
109 case I915_CACHE_L3_LLC:
110 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700111 break;
112 case I915_CACHE_LLC:
113 pte |= GEN6_PTE_CACHE_LLC;
114 break;
115 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700116 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700117 break;
118 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100119 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700120 }
121
Ben Widawsky54d12522012-09-24 16:44:32 -0700122 return pte;
123}
124
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700125#define BYT_PTE_WRITEABLE (1 << 1)
126#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
127
Ben Widawsky80a74f72013-06-27 16:30:19 -0700128static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700129 enum i915_cache_level level,
130 bool valid)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700131{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700132 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700133 pte |= GEN6_PTE_ADDR_ENCODE(addr);
134
135 /* Mark the page as writeable. Other platforms don't have a
136 * setting for read-only/writable, so this matches that behavior.
137 */
138 pte |= BYT_PTE_WRITEABLE;
139
140 if (level != I915_CACHE_NONE)
141 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
142
143 return pte;
144}
145
Ben Widawsky80a74f72013-06-27 16:30:19 -0700146static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700147 enum i915_cache_level level,
148 bool valid)
Kenneth Graunke91197082013-04-22 00:53:51 -0700149{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700150 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700151 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700152
153 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700154 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700155
156 return pte;
157}
158
Ben Widawsky4d15c142013-07-04 11:02:06 -0700159static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700160 enum i915_cache_level level,
161 bool valid)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700162{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700163 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700164 pte |= HSW_PTE_ADDR_ENCODE(addr);
165
Chris Wilson651d7942013-08-08 14:41:10 +0100166 switch (level) {
167 case I915_CACHE_NONE:
168 break;
169 case I915_CACHE_WT:
170 pte |= HSW_WT_ELLC_LLC_AGE0;
171 break;
172 default:
Ben Widawsky4d15c142013-07-04 11:02:06 -0700173 pte |= HSW_WB_ELLC_LLC_AGE0;
Chris Wilson651d7942013-08-08 14:41:10 +0100174 break;
175 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700176
177 return pte;
178}
179
Ben Widawsky3e302542013-04-23 23:15:32 -0700180static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700181{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700182 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700183 gen6_gtt_pte_t __iomem *pd_addr;
184 uint32_t pd_entry;
185 int i;
186
Ben Widawsky0a732872013-04-23 23:15:30 -0700187 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700188 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
189 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
190 for (i = 0; i < ppgtt->num_pd_entries; i++) {
191 dma_addr_t pt_addr;
192
193 pt_addr = ppgtt->pt_dma_addr[i];
194 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
195 pd_entry |= GEN6_PDE_VALID;
196
197 writel(pd_entry, pd_addr + i);
198 }
199 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700200}
201
202static int gen6_ppgtt_enable(struct drm_device *dev)
203{
204 drm_i915_private_t *dev_priv = dev->dev_private;
205 uint32_t pd_offset;
206 struct intel_ring_buffer *ring;
207 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
208 int i;
209
210 BUG_ON(ppgtt->pd_offset & 0x3f);
211
212 gen6_write_pdes(ppgtt);
Ben Widawsky61973492013-04-08 18:43:54 -0700213
214 pd_offset = ppgtt->pd_offset;
215 pd_offset /= 64; /* in cachelines, */
216 pd_offset <<= 16;
217
218 if (INTEL_INFO(dev)->gen == 6) {
219 uint32_t ecochk, gab_ctl, ecobits;
220
221 ecobits = I915_READ(GAC_ECO_BITS);
Ville Syrjälä3b9d7882013-04-04 15:13:40 +0300222 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
223 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700224
225 gab_ctl = I915_READ(GAB_CTL);
226 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
227
228 ecochk = I915_READ(GAM_ECOCHK);
229 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
230 ECOCHK_PPGTT_CACHE64B);
231 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
232 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjäläa6f429a2013-04-04 15:13:42 +0300233 uint32_t ecochk, ecobits;
Ville Syrjäläa65c2fc2013-04-04 15:13:41 +0300234
235 ecobits = I915_READ(GAC_ECO_BITS);
236 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
237
Ville Syrjäläa6f429a2013-04-04 15:13:42 +0300238 ecochk = I915_READ(GAM_ECOCHK);
239 if (IS_HASWELL(dev)) {
240 ecochk |= ECOCHK_PPGTT_WB_HSW;
241 } else {
242 ecochk |= ECOCHK_PPGTT_LLC_IVB;
243 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
244 }
245 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawsky61973492013-04-08 18:43:54 -0700246 /* GFX_MODE is per-ring on gen7+ */
247 }
248
249 for_each_ring(ring, dev_priv, i) {
250 if (INTEL_INFO(dev)->gen >= 7)
251 I915_WRITE(RING_MODE_GEN7(ring),
252 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
253
254 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
255 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
256 }
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700257 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700258}
259
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100260/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700261static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100262 unsigned first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700263 unsigned num_entries,
264 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100265{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700266 struct i915_hw_ppgtt *ppgtt =
267 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700268 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Daniel Vettera15326a2013-03-19 23:48:39 +0100269 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100270 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
271 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100272
Ben Widawskyb35b3802013-10-16 09:18:21 -0700273 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100274
Daniel Vetter7bddb012012-02-09 17:15:47 +0100275 while (num_entries) {
276 last_pte = first_pte + num_entries;
277 if (last_pte > I915_PPGTT_PT_ENTRIES)
278 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100279
Daniel Vettera15326a2013-03-19 23:48:39 +0100280 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100281
282 for (i = first_pte; i < last_pte; i++)
283 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100284
285 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100286
Daniel Vetter7bddb012012-02-09 17:15:47 +0100287 num_entries -= last_pte - first_pte;
288 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100289 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100290 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100291}
292
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700293static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800294 struct sg_table *pages,
295 unsigned first_entry,
296 enum i915_cache_level cache_level)
297{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700298 struct i915_hw_ppgtt *ppgtt =
299 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700300 gen6_gtt_pte_t *pt_vaddr;
Daniel Vettera15326a2013-03-19 23:48:39 +0100301 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200302 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
303 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800304
Daniel Vettera15326a2013-03-19 23:48:39 +0100305 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200306 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
307 dma_addr_t page_addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800308
Imre Deak2db76d72013-03-26 15:14:18 +0200309 page_addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -0700310 pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200311 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
312 kunmap_atomic(pt_vaddr);
Daniel Vettera15326a2013-03-19 23:48:39 +0100313 act_pt++;
314 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200315 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800316
Daniel Vetterdef886c2013-01-24 14:44:56 -0800317 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800318 }
Imre Deak6e995e22013-02-18 19:28:04 +0200319 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800320}
321
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700322static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100323{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700324 struct i915_hw_ppgtt *ppgtt =
325 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -0800326 int i;
327
Ben Widawsky93bd8642013-07-16 16:50:06 -0700328 drm_mm_takedown(&ppgtt->base.mm);
329
Daniel Vetter3440d262013-01-24 13:49:56 -0800330 if (ppgtt->pt_dma_addr) {
331 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700332 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800333 ppgtt->pt_dma_addr[i],
334 4096, PCI_DMA_BIDIRECTIONAL);
335 }
336
337 kfree(ppgtt->pt_dma_addr);
338 for (i = 0; i < ppgtt->num_pd_entries; i++)
339 __free_page(ppgtt->pt_pages[i]);
340 kfree(ppgtt->pt_pages);
341 kfree(ppgtt);
342}
343
344static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
345{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700346 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100347 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100348 unsigned first_pd_entry_in_global_pt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100349 int i;
350 int ret = -ENOMEM;
351
352 /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024
353 * entries. For aliasing ppgtt support we just steal them at the end for
354 * now. */
Daniel Vettere1b73cb2013-05-21 09:52:16 +0200355 first_pd_entry_in_global_pt = gtt_total_entries(dev_priv->gtt);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100356
Chris Wilson08c45262013-07-30 19:04:37 +0100357 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700358 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky61973492013-04-08 18:43:54 -0700359 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700360 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
361 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
362 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
363 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vettera1e22652013-09-21 00:35:38 +0200364 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100365 GFP_KERNEL);
366 if (!ppgtt->pt_pages)
Daniel Vetter3440d262013-01-24 13:49:56 -0800367 return -ENOMEM;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100368
369 for (i = 0; i < ppgtt->num_pd_entries; i++) {
370 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
371 if (!ppgtt->pt_pages[i])
372 goto err_pt_alloc;
373 }
374
Daniel Vettera1e22652013-09-21 00:35:38 +0200375 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800376 GFP_KERNEL);
377 if (!ppgtt->pt_dma_addr)
378 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100379
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800380 for (i = 0; i < ppgtt->num_pd_entries; i++) {
381 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200382
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800383 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
384 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100385
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800386 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
387 ret = -EIO;
388 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100389
Daniel Vetter211c5682012-04-10 17:29:17 +0200390 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800391 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100392 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100393
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700394 ppgtt->base.clear_range(&ppgtt->base, 0,
Ben Widawsky828c7902013-10-16 09:21:30 -0700395 ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100396
Ben Widawskye7c2b582013-04-08 18:43:48 -0700397 ppgtt->pd_offset = first_pd_entry_in_global_pt * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100398
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100399 return 0;
400
401err_pd_pin:
402 if (ppgtt->pt_dma_addr) {
403 for (i--; i >= 0; i--)
404 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
405 4096, PCI_DMA_BIDIRECTIONAL);
406 }
407err_pt_alloc:
408 kfree(ppgtt->pt_dma_addr);
409 for (i = 0; i < ppgtt->num_pd_entries; i++) {
410 if (ppgtt->pt_pages[i])
411 __free_page(ppgtt->pt_pages[i]);
412 }
413 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800414
415 return ret;
416}
417
418static int i915_gem_init_aliasing_ppgtt(struct drm_device *dev)
419{
420 struct drm_i915_private *dev_priv = dev->dev_private;
421 struct i915_hw_ppgtt *ppgtt;
422 int ret;
423
424 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
425 if (!ppgtt)
426 return -ENOMEM;
427
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700428 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -0800429
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700430 if (INTEL_INFO(dev)->gen < 8)
431 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -0700432 else if (IS_GEN8(dev))
433 ret = -ENOSYS;
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700434 else
435 BUG();
436
Daniel Vetter3440d262013-01-24 13:49:56 -0800437 if (ret)
438 kfree(ppgtt);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700439 else {
Daniel Vetter3440d262013-01-24 13:49:56 -0800440 dev_priv->mm.aliasing_ppgtt = ppgtt;
Ben Widawsky93bd8642013-07-16 16:50:06 -0700441 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
442 ppgtt->base.total);
443 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100444
445 return ret;
446}
447
448void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev)
449{
450 struct drm_i915_private *dev_priv = dev->dev_private;
451 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100452
453 if (!ppgtt)
454 return;
455
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700456 ppgtt->base.cleanup(&ppgtt->base);
Ben Widawsky5963cf02013-04-08 18:43:55 -0700457 dev_priv->mm.aliasing_ppgtt = NULL;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100458}
459
Daniel Vetter7bddb012012-02-09 17:15:47 +0100460void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt,
461 struct drm_i915_gem_object *obj,
462 enum i915_cache_level cache_level)
463{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700464 ppgtt->base.insert_entries(&ppgtt->base, obj->pages,
465 i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT,
466 cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100467}
468
469void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt,
470 struct drm_i915_gem_object *obj)
471{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700472 ppgtt->base.clear_range(&ppgtt->base,
473 i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT,
Ben Widawsky828c7902013-10-16 09:21:30 -0700474 obj->base.size >> PAGE_SHIFT,
475 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100476}
477
Ben Widawskya81cc002013-01-18 12:30:31 -0800478extern int intel_iommu_gfx_mapped;
479/* Certain Gen5 chipsets require require idling the GPU before
480 * unmapping anything from the GTT when VT-d is enabled.
481 */
482static inline bool needs_idle_maps(struct drm_device *dev)
483{
484#ifdef CONFIG_INTEL_IOMMU
485 /* Query intel_iommu to see if we need the workaround. Presumably that
486 * was loaded first.
487 */
488 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
489 return true;
490#endif
491 return false;
492}
493
Ben Widawsky5c042282011-10-17 15:51:55 -0700494static bool do_idling(struct drm_i915_private *dev_priv)
495{
496 bool ret = dev_priv->mm.interruptible;
497
Ben Widawskya81cc002013-01-18 12:30:31 -0800498 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700499 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700500 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700501 DRM_ERROR("Couldn't idle GPU\n");
502 /* Wait a bit, in hopes it avoids the hang */
503 udelay(10);
504 }
505 }
506
507 return ret;
508}
509
510static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
511{
Ben Widawskya81cc002013-01-18 12:30:31 -0800512 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -0700513 dev_priv->mm.interruptible = interruptible;
514}
515
Ben Widawsky828c7902013-10-16 09:21:30 -0700516void i915_check_and_clear_faults(struct drm_device *dev)
517{
518 struct drm_i915_private *dev_priv = dev->dev_private;
519 struct intel_ring_buffer *ring;
520 int i;
521
522 if (INTEL_INFO(dev)->gen < 6)
523 return;
524
525 for_each_ring(ring, dev_priv, i) {
526 u32 fault_reg;
527 fault_reg = I915_READ(RING_FAULT_REG(ring));
528 if (fault_reg & RING_FAULT_VALID) {
529 DRM_DEBUG_DRIVER("Unexpected fault\n"
530 "\tAddr: 0x%08lx\\n"
531 "\tAddress space: %s\n"
532 "\tSource ID: %d\n"
533 "\tType: %d\n",
534 fault_reg & PAGE_MASK,
535 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
536 RING_FAULT_SRCID(fault_reg),
537 RING_FAULT_FAULT_TYPE(fault_reg));
538 I915_WRITE(RING_FAULT_REG(ring),
539 fault_reg & ~RING_FAULT_VALID);
540 }
541 }
542 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
543}
544
545void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
546{
547 struct drm_i915_private *dev_priv = dev->dev_private;
548
549 /* Don't bother messing with faults pre GEN6 as we have little
550 * documentation supporting that it's a good idea.
551 */
552 if (INTEL_INFO(dev)->gen < 6)
553 return;
554
555 i915_check_and_clear_faults(dev);
556
557 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
558 dev_priv->gtt.base.start / PAGE_SIZE,
559 dev_priv->gtt.base.total / PAGE_SIZE,
560 false);
561}
562
Daniel Vetter76aaf222010-11-05 22:23:30 +0100563void i915_gem_restore_gtt_mappings(struct drm_device *dev)
564{
565 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000566 struct drm_i915_gem_object *obj;
Daniel Vetter76aaf222010-11-05 22:23:30 +0100567
Ben Widawsky828c7902013-10-16 09:21:30 -0700568 i915_check_and_clear_faults(dev);
569
Chris Wilsonbee4a182011-01-21 10:54:32 +0000570 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700571 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
572 dev_priv->gtt.base.start / PAGE_SIZE,
Ben Widawsky828c7902013-10-16 09:21:30 -0700573 dev_priv->gtt.base.total / PAGE_SIZE,
574 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +0000575
Ben Widawsky35c20a62013-05-31 11:28:48 -0700576 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Chris Wilson2c225692013-08-09 12:26:45 +0100577 i915_gem_clflush_object(obj, obj->pin_display);
Daniel Vetter74163902012-02-15 23:50:21 +0100578 i915_gem_gtt_bind_object(obj, obj->cache_level);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100579 }
580
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800581 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100582}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100583
Daniel Vetter74163902012-02-15 23:50:21 +0100584int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100585{
Chris Wilson9da3da62012-06-01 15:20:22 +0100586 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +0100587 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +0100588
589 if (!dma_map_sg(&obj->base.dev->pdev->dev,
590 obj->pages->sgl, obj->pages->nents,
591 PCI_DMA_BIDIRECTIONAL))
592 return -ENOSPC;
593
594 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100595}
596
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700597static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
598{
599#ifdef writeq
600 writeq(pte, addr);
601#else
602 iowrite32((u32)pte, addr);
603 iowrite32(pte >> 32, addr + 4);
604#endif
605}
606
607static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
608 struct sg_table *st,
609 unsigned int first_entry,
610 enum i915_cache_level level)
611{
612 struct drm_i915_private *dev_priv = vm->dev->dev_private;
613 gen8_gtt_pte_t __iomem *gtt_entries =
614 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
615 int i = 0;
616 struct sg_page_iter sg_iter;
617 dma_addr_t addr;
618
619 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
620 addr = sg_dma_address(sg_iter.sg) +
621 (sg_iter.sg_pgoffset << PAGE_SHIFT);
622 gen8_set_pte(&gtt_entries[i],
623 gen8_pte_encode(addr, level, true));
624 i++;
625 }
626
627 /*
628 * XXX: This serves as a posting read to make sure that the PTE has
629 * actually been updated. There is some concern that even though
630 * registers and PTEs are within the same BAR that they are potentially
631 * of NUMA access patterns. Therefore, even with the way we assume
632 * hardware should work, we must keep this posting read for paranoia.
633 */
634 if (i != 0)
635 WARN_ON(readq(&gtt_entries[i-1])
636 != gen8_pte_encode(addr, level, true));
637
638#if 0 /* TODO: Still needed on GEN8? */
639 /* This next bit makes the above posting read even more important. We
640 * want to flush the TLBs only after we're certain all the PTE updates
641 * have finished.
642 */
643 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
644 POSTING_READ(GFX_FLSH_CNTL_GEN6);
645#endif
646}
647
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800648/*
649 * Binds an object into the global gtt with the specified cache level. The object
650 * will be accessible to the GPU via commands whose operands reference offsets
651 * within the global GTT as well as accessible by the GPU through the GMADR
652 * mapped BAR (dev_priv->mm.gtt->gtt).
653 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700654static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800655 struct sg_table *st,
656 unsigned int first_entry,
657 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800658{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700659 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -0700660 gen6_gtt_pte_t __iomem *gtt_entries =
661 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +0200662 int i = 0;
663 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800664 dma_addr_t addr;
665
Imre Deak6e995e22013-02-18 19:28:04 +0200666 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +0200667 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -0700668 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +0200669 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800670 }
671
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800672 /* XXX: This serves as a posting read to make sure that the PTE has
673 * actually been updated. There is some concern that even though
674 * registers and PTEs are within the same BAR that they are potentially
675 * of NUMA access patterns. Therefore, even with the way we assume
676 * hardware should work, we must keep this posting read for paranoia.
677 */
678 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700679 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -0700680 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -0800681
682 /* This next bit makes the above posting read even more important. We
683 * want to flush the TLBs only after we're certain all the PTE updates
684 * have finished.
685 */
686 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
687 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800688}
689
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700690static void gen8_ggtt_clear_range(struct i915_address_space *vm,
691 unsigned int first_entry,
692 unsigned int num_entries,
693 bool use_scratch)
694{
695 struct drm_i915_private *dev_priv = vm->dev->dev_private;
696 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
697 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
698 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
699 int i;
700
701 if (WARN(num_entries > max_entries,
702 "First entry = %d; Num entries = %d (max=%d)\n",
703 first_entry, num_entries, max_entries))
704 num_entries = max_entries;
705
706 scratch_pte = gen8_pte_encode(vm->scratch.addr,
707 I915_CACHE_LLC,
708 use_scratch);
709 for (i = 0; i < num_entries; i++)
710 gen8_set_pte(&gtt_base[i], scratch_pte);
711 readl(gtt_base);
712}
713
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700714static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800715 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700716 unsigned int num_entries,
717 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800718{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700719 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -0700720 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
721 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -0800722 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800723 int i;
724
725 if (WARN(num_entries > max_entries,
726 "First entry = %d; Num entries = %d (max=%d)\n",
727 first_entry, num_entries, max_entries))
728 num_entries = max_entries;
729
Ben Widawsky828c7902013-10-16 09:21:30 -0700730 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
731
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800732 for (i = 0; i < num_entries; i++)
733 iowrite32(scratch_pte, &gtt_base[i]);
734 readl(gtt_base);
735}
736
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700737static void i915_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800738 struct sg_table *st,
739 unsigned int pg_start,
740 enum i915_cache_level cache_level)
741{
742 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
743 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
744
745 intel_gtt_insert_sg_entries(st, pg_start, flags);
746
747}
748
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700749static void i915_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800750 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700751 unsigned int num_entries,
752 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800753{
754 intel_gtt_clear_range(first_entry, num_entries);
755}
756
757
Daniel Vetter74163902012-02-15 23:50:21 +0100758void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj,
759 enum i915_cache_level cache_level)
Chris Wilsond5bd1442011-04-14 06:48:26 +0100760{
761 struct drm_device *dev = obj->base.dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800762 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700763 const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800764
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700765 dev_priv->gtt.base.insert_entries(&dev_priv->gtt.base, obj->pages,
766 entry,
767 cache_level);
Chris Wilsond5bd1442011-04-14 06:48:26 +0100768
Daniel Vetter74898d72012-02-15 23:50:22 +0100769 obj->has_global_gtt_mapping = 1;
Chris Wilsond5bd1442011-04-14 06:48:26 +0100770}
771
Chris Wilson05394f32010-11-08 19:18:58 +0000772void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100773{
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800774 struct drm_device *dev = obj->base.dev;
775 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700776 const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800777
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700778 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
779 entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700780 obj->base.size >> PAGE_SHIFT,
781 true);
Daniel Vetter74898d72012-02-15 23:50:22 +0100782
783 obj->has_global_gtt_mapping = 0;
Daniel Vetter74163902012-02-15 23:50:21 +0100784}
785
786void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
787{
Ben Widawsky5c042282011-10-17 15:51:55 -0700788 struct drm_device *dev = obj->base.dev;
789 struct drm_i915_private *dev_priv = dev->dev_private;
790 bool interruptible;
791
792 interruptible = do_idling(dev_priv);
793
Chris Wilson9da3da62012-06-01 15:20:22 +0100794 if (!obj->has_dma_mapping)
795 dma_unmap_sg(&dev->pdev->dev,
796 obj->pages->sgl, obj->pages->nents,
797 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -0700798
799 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100800}
Daniel Vetter644ec022012-03-26 09:45:40 +0200801
Chris Wilson42d6ab42012-07-26 11:49:32 +0100802static void i915_gtt_color_adjust(struct drm_mm_node *node,
803 unsigned long color,
804 unsigned long *start,
805 unsigned long *end)
806{
807 if (node->color != color)
808 *start += 4096;
809
810 if (!list_empty(&node->node_list)) {
811 node = list_entry(node->node_list.next,
812 struct drm_mm_node,
813 node_list);
814 if (node->allocated && node->color != color)
815 *end -= 4096;
816 }
817}
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800818
Ben Widawskyd7e50082012-12-18 10:31:25 -0800819void i915_gem_setup_global_gtt(struct drm_device *dev,
820 unsigned long start,
821 unsigned long mappable_end,
822 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +0200823{
Ben Widawskye78891c2013-01-25 16:41:04 -0800824 /* Let GEM Manage all of the aperture.
825 *
826 * However, leave one page at the end still bound to the scratch page.
827 * There are a number of places where the hardware apparently prefetches
828 * past the end of the object, and we've seen multiple hangs with the
829 * GPU head pointer stuck in a batchbuffer bound at the last page of the
830 * aperture. One page should be enough to keep any prefetching inside
831 * of the aperture.
832 */
Ben Widawsky40d749802013-07-31 16:59:59 -0700833 struct drm_i915_private *dev_priv = dev->dev_private;
834 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +0000835 struct drm_mm_node *entry;
836 struct drm_i915_gem_object *obj;
837 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +0200838
Ben Widawsky35451cb2013-01-17 12:45:13 -0800839 BUG_ON(mappable_end > end);
840
Chris Wilsoned2f3452012-11-15 11:32:19 +0000841 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -0700842 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +0100843 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -0700844 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +0200845
Chris Wilsoned2f3452012-11-15 11:32:19 +0000846 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -0700847 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -0700848 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700849 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -0700850 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700851 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000852
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700853 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -0700854 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700855 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700856 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +0000857 obj->has_global_gtt_mapping = 1;
Ben Widawsky2f633152013-07-17 12:19:03 -0700858 list_add(&vma->vma_link, &obj->vma_list);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000859 }
860
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700861 dev_priv->gtt.base.start = start;
862 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +0200863
Chris Wilsoned2f3452012-11-15 11:32:19 +0000864 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -0700865 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700866 const unsigned long count = (hole_end - hole_start) / PAGE_SIZE;
Chris Wilsoned2f3452012-11-15 11:32:19 +0000867 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
868 hole_start, hole_end);
Ben Widawsky828c7902013-10-16 09:21:30 -0700869 ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000870 }
871
872 /* And finally clear the reserved guard page */
Ben Widawsky828c7902013-10-16 09:21:30 -0700873 ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800874}
875
Ben Widawskyd7e50082012-12-18 10:31:25 -0800876static bool
877intel_enable_ppgtt(struct drm_device *dev)
878{
879 if (i915_enable_ppgtt >= 0)
880 return i915_enable_ppgtt;
881
882#ifdef CONFIG_INTEL_IOMMU
883 /* Disable ppgtt on SNB if VT-d is on. */
884 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
885 return false;
886#endif
887
888 return true;
889}
890
891void i915_gem_init_global_gtt(struct drm_device *dev)
892{
893 struct drm_i915_private *dev_priv = dev->dev_private;
894 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800895
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700896 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -0800897 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800898
899 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
Ben Widawskye78891c2013-01-25 16:41:04 -0800900 int ret;
Ben Widawsky3eb1c002013-04-08 18:43:52 -0700901
902 if (INTEL_INFO(dev)->gen <= 7) {
903 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
904 * aperture accordingly when using aliasing ppgtt. */
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700905 gtt_size -= GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE;
Ben Widawsky3eb1c002013-04-08 18:43:52 -0700906 }
Ben Widawskyd7e50082012-12-18 10:31:25 -0800907
908 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
909
910 ret = i915_gem_init_aliasing_ppgtt(dev);
Ben Widawskye78891c2013-01-25 16:41:04 -0800911 if (!ret)
Ben Widawskyd7e50082012-12-18 10:31:25 -0800912 return;
Ben Widawskye78891c2013-01-25 16:41:04 -0800913
914 DRM_ERROR("Aliased PPGTT setup failed %d\n", ret);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700915 drm_mm_takedown(&dev_priv->gtt.base.mm);
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700916 gtt_size += GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800917 }
Ben Widawskye78891c2013-01-25 16:41:04 -0800918 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800919}
920
921static int setup_scratch_page(struct drm_device *dev)
922{
923 struct drm_i915_private *dev_priv = dev->dev_private;
924 struct page *page;
925 dma_addr_t dma_addr;
926
927 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
928 if (page == NULL)
929 return -ENOMEM;
930 get_page(page);
931 set_pages_uc(page, 1);
932
933#ifdef CONFIG_INTEL_IOMMU
934 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
935 PCI_DMA_BIDIRECTIONAL);
936 if (pci_dma_mapping_error(dev->pdev, dma_addr))
937 return -EINVAL;
938#else
939 dma_addr = page_to_phys(page);
940#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700941 dev_priv->gtt.base.scratch.page = page;
942 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800943
944 return 0;
945}
946
947static void teardown_scratch_page(struct drm_device *dev)
948{
949 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700950 struct page *page = dev_priv->gtt.base.scratch.page;
951
952 set_pages_wb(page, 1);
953 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800954 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700955 put_page(page);
956 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800957}
958
959static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
960{
961 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
962 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
963 return snb_gmch_ctl << 20;
964}
965
Ben Widawsky9459d252013-11-03 16:53:55 -0800966static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
967{
968 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
969 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
970 if (bdw_gmch_ctl)
971 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
972 return bdw_gmch_ctl << 20;
973}
974
Ben Widawskybaa09f52013-01-24 13:49:57 -0800975static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800976{
977 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
978 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
979 return snb_gmch_ctl << 25; /* 32 MB units */
980}
981
Ben Widawsky9459d252013-11-03 16:53:55 -0800982static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
983{
984 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
985 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
986 return bdw_gmch_ctl << 25; /* 32 MB units */
987}
988
Ben Widawsky63340132013-11-04 19:32:22 -0800989static int ggtt_probe_common(struct drm_device *dev,
990 size_t gtt_size)
991{
992 struct drm_i915_private *dev_priv = dev->dev_private;
993 phys_addr_t gtt_bus_addr;
994 int ret;
995
996 /* For Modern GENs the PTEs and register space are split in the BAR */
997 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
998 (pci_resource_len(dev->pdev, 0) / 2);
999
1000 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
1001 if (!dev_priv->gtt.gsm) {
1002 DRM_ERROR("Failed to map the gtt page table\n");
1003 return -ENOMEM;
1004 }
1005
1006 ret = setup_scratch_page(dev);
1007 if (ret) {
1008 DRM_ERROR("Scratch setup failed\n");
1009 /* iounmap will also get called at remove, but meh */
1010 iounmap(dev_priv->gtt.gsm);
1011 }
1012
1013 return ret;
1014}
1015
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001016/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1017 * bits. When using advanced contexts each context stores its own PAT, but
1018 * writing this data shouldn't be harmful even in those cases. */
1019static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1020{
1021#define GEN8_PPAT_UC (0<<0)
1022#define GEN8_PPAT_WC (1<<0)
1023#define GEN8_PPAT_WT (2<<0)
1024#define GEN8_PPAT_WB (3<<0)
1025#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
1026/* FIXME(BDW): Bspec is completely confused about cache control bits. */
1027#define GEN8_PPAT_LLC (1<<2)
1028#define GEN8_PPAT_LLCELLC (2<<2)
1029#define GEN8_PPAT_LLCeLLC (3<<2)
1030#define GEN8_PPAT_AGE(x) (x<<4)
1031#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
1032 uint64_t pat;
1033
1034 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1035 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1036 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1037 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1038 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1039 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1040 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1041 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1042
1043 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1044 * write would work. */
1045 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1046 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1047}
1048
Ben Widawsky63340132013-11-04 19:32:22 -08001049static int gen8_gmch_probe(struct drm_device *dev,
1050 size_t *gtt_total,
1051 size_t *stolen,
1052 phys_addr_t *mappable_base,
1053 unsigned long *mappable_end)
1054{
1055 struct drm_i915_private *dev_priv = dev->dev_private;
1056 unsigned int gtt_size;
1057 u16 snb_gmch_ctl;
1058 int ret;
1059
1060 /* TODO: We're not aware of mappable constraints on gen8 yet */
1061 *mappable_base = pci_resource_start(dev->pdev, 2);
1062 *mappable_end = pci_resource_len(dev->pdev, 2);
1063
1064 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1065 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1066
1067 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1068
1069 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1070
1071 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskyd31eb102013-11-02 21:07:17 -07001072 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001073
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001074 gen8_setup_private_ppat(dev_priv);
1075
Ben Widawsky63340132013-11-04 19:32:22 -08001076 ret = ggtt_probe_common(dev, gtt_size);
1077
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001078 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1079 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001080
1081 return ret;
1082}
1083
Ben Widawskybaa09f52013-01-24 13:49:57 -08001084static int gen6_gmch_probe(struct drm_device *dev,
1085 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001086 size_t *stolen,
1087 phys_addr_t *mappable_base,
1088 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001089{
1090 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001091 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001092 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001093 int ret;
1094
Ben Widawsky41907dd2013-02-08 11:32:47 -08001095 *mappable_base = pci_resource_start(dev->pdev, 2);
1096 *mappable_end = pci_resource_len(dev->pdev, 2);
1097
Ben Widawskybaa09f52013-01-24 13:49:57 -08001098 /* 64/512MB is the current min/max we actually know of, but this is just
1099 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001100 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001101 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001102 DRM_ERROR("Unknown GMADR size (%lx)\n",
1103 dev_priv->gtt.mappable_end);
1104 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001105 }
1106
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001107 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1108 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001109 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001110
Ben Widawsky63340132013-11-04 19:32:22 -08001111 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001112
Ben Widawsky63340132013-11-04 19:32:22 -08001113 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
1114 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawskya93e4162013-04-08 18:43:47 -07001115
Ben Widawsky63340132013-11-04 19:32:22 -08001116 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001117
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001118 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1119 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001120
1121 return ret;
1122}
1123
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001124static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001125{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001126
1127 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
1128 iounmap(gtt->gsm);
1129 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001130}
1131
1132static int i915_gmch_probe(struct drm_device *dev,
1133 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001134 size_t *stolen,
1135 phys_addr_t *mappable_base,
1136 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001137{
1138 struct drm_i915_private *dev_priv = dev->dev_private;
1139 int ret;
1140
Ben Widawskybaa09f52013-01-24 13:49:57 -08001141 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1142 if (!ret) {
1143 DRM_ERROR("failed to set up gmch\n");
1144 return -EIO;
1145 }
1146
Ben Widawsky41907dd2013-02-08 11:32:47 -08001147 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001148
1149 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001150 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
1151 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001152
1153 return 0;
1154}
1155
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001156static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001157{
1158 intel_gmch_remove();
1159}
1160
1161int i915_gem_gtt_init(struct drm_device *dev)
1162{
1163 struct drm_i915_private *dev_priv = dev->dev_private;
1164 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001165 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001166
Ben Widawskybaa09f52013-01-24 13:49:57 -08001167 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001168 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001169 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08001170 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001171 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001172 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001173 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001174 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001175 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001176 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001177 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001178 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001179 else if (INTEL_INFO(dev)->gen >= 7)
1180 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001181 else
Chris Wilson350ec882013-08-06 13:17:02 +01001182 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08001183 } else {
1184 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1185 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001186 }
1187
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001188 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001189 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001190 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001191 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001192
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001193 gtt->base.dev = dev;
1194
Ben Widawskybaa09f52013-01-24 13:49:57 -08001195 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001196 DRM_INFO("Memory usable by graphics device = %zdM\n",
1197 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001198 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1199 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001200
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001201 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001202}