blob: 074aec11fc9b1c488a955d2321afdce1b9bfca97 [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))
33
Ben Widawsky26b1ff32012-11-04 09:21:31 -080034/* PPGTT stuff */
35#define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0))
Ben Widawsky0d8ff152013-07-04 11:02:03 -070036#define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0))
Ben Widawsky26b1ff32012-11-04 09:21:31 -080037
38#define GEN6_PDE_VALID (1 << 0)
39/* gen6+ has bit 11-4 for physical addr bit 39-32 */
40#define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
41
42#define GEN6_PTE_VALID (1 << 0)
43#define GEN6_PTE_UNCACHED (1 << 1)
44#define HSW_PTE_UNCACHED (0)
45#define GEN6_PTE_CACHE_LLC (2 << 1)
Chris Wilson350ec882013-08-06 13:17:02 +010046#define GEN7_PTE_CACHE_L3_LLC (3 << 1)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080047#define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
Ben Widawsky0d8ff152013-07-04 11:02:03 -070048#define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr)
49
50/* Cacheability Control is a 4-bit value. The low three bits are stored in *
51 * bits 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
52 */
53#define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \
54 (((bits) & 0x8) << (11 - 3)))
Ben Widawsky87a6b682013-08-04 23:47:29 -070055#define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2)
Ben Widawsky0d8ff152013-07-04 11:02:03 -070056#define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3)
Ben Widawsky4d15c142013-07-04 11:02:06 -070057#define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb)
Chris Wilson651d7942013-08-08 14:41:10 +010058#define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080059
Chris Wilson350ec882013-08-06 13:17:02 +010060static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -070061 enum i915_cache_level level,
62 bool valid)
Ben Widawsky54d12522012-09-24 16:44:32 -070063{
Ben Widawskyb35b3802013-10-16 09:18:21 -070064 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -070065 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -070066
67 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +010068 case I915_CACHE_L3_LLC:
69 case I915_CACHE_LLC:
70 pte |= GEN6_PTE_CACHE_LLC;
71 break;
72 case I915_CACHE_NONE:
73 pte |= GEN6_PTE_UNCACHED;
74 break;
75 default:
76 WARN_ON(1);
77 }
78
79 return pte;
80}
81
82static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -070083 enum i915_cache_level level,
84 bool valid)
Chris Wilson350ec882013-08-06 13:17:02 +010085{
Ben Widawskyb35b3802013-10-16 09:18:21 -070086 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +010087 pte |= GEN6_PTE_ADDR_ENCODE(addr);
88
89 switch (level) {
90 case I915_CACHE_L3_LLC:
91 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -070092 break;
93 case I915_CACHE_LLC:
94 pte |= GEN6_PTE_CACHE_LLC;
95 break;
96 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -070097 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -070098 break;
99 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100100 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700101 }
102
Ben Widawsky54d12522012-09-24 16:44:32 -0700103 return pte;
104}
105
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700106#define BYT_PTE_WRITEABLE (1 << 1)
107#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
108
Ben Widawsky80a74f72013-06-27 16:30:19 -0700109static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700110 enum i915_cache_level level,
111 bool valid)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700112{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700113 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700114 pte |= GEN6_PTE_ADDR_ENCODE(addr);
115
116 /* Mark the page as writeable. Other platforms don't have a
117 * setting for read-only/writable, so this matches that behavior.
118 */
119 pte |= BYT_PTE_WRITEABLE;
120
121 if (level != I915_CACHE_NONE)
122 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
123
124 return pte;
125}
126
Ben Widawsky80a74f72013-06-27 16:30:19 -0700127static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700128 enum i915_cache_level level,
129 bool valid)
Kenneth Graunke91197082013-04-22 00:53:51 -0700130{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700131 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700132 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700133
134 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700135 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700136
137 return pte;
138}
139
Ben Widawsky4d15c142013-07-04 11:02:06 -0700140static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700141 enum i915_cache_level level,
142 bool valid)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700143{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700144 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700145 pte |= HSW_PTE_ADDR_ENCODE(addr);
146
Chris Wilson651d7942013-08-08 14:41:10 +0100147 switch (level) {
148 case I915_CACHE_NONE:
149 break;
150 case I915_CACHE_WT:
151 pte |= HSW_WT_ELLC_LLC_AGE0;
152 break;
153 default:
Ben Widawsky4d15c142013-07-04 11:02:06 -0700154 pte |= HSW_WB_ELLC_LLC_AGE0;
Chris Wilson651d7942013-08-08 14:41:10 +0100155 break;
156 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700157
158 return pte;
159}
160
Ben Widawsky3e302542013-04-23 23:15:32 -0700161static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700162{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700163 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700164 gen6_gtt_pte_t __iomem *pd_addr;
165 uint32_t pd_entry;
166 int i;
167
Ben Widawsky0a732872013-04-23 23:15:30 -0700168 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700169 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
170 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
171 for (i = 0; i < ppgtt->num_pd_entries; i++) {
172 dma_addr_t pt_addr;
173
174 pt_addr = ppgtt->pt_dma_addr[i];
175 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
176 pd_entry |= GEN6_PDE_VALID;
177
178 writel(pd_entry, pd_addr + i);
179 }
180 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700181}
182
183static int gen6_ppgtt_enable(struct drm_device *dev)
184{
185 drm_i915_private_t *dev_priv = dev->dev_private;
186 uint32_t pd_offset;
187 struct intel_ring_buffer *ring;
188 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
189 int i;
190
191 BUG_ON(ppgtt->pd_offset & 0x3f);
192
193 gen6_write_pdes(ppgtt);
Ben Widawsky61973492013-04-08 18:43:54 -0700194
195 pd_offset = ppgtt->pd_offset;
196 pd_offset /= 64; /* in cachelines, */
197 pd_offset <<= 16;
198
199 if (INTEL_INFO(dev)->gen == 6) {
200 uint32_t ecochk, gab_ctl, ecobits;
201
202 ecobits = I915_READ(GAC_ECO_BITS);
Ville Syrjälä3b9d7882013-04-04 15:13:40 +0300203 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
204 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700205
206 gab_ctl = I915_READ(GAB_CTL);
207 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
208
209 ecochk = I915_READ(GAM_ECOCHK);
210 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
211 ECOCHK_PPGTT_CACHE64B);
212 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
213 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjäläa6f429a2013-04-04 15:13:42 +0300214 uint32_t ecochk, ecobits;
Ville Syrjäläa65c2fc2013-04-04 15:13:41 +0300215
216 ecobits = I915_READ(GAC_ECO_BITS);
217 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
218
Ville Syrjäläa6f429a2013-04-04 15:13:42 +0300219 ecochk = I915_READ(GAM_ECOCHK);
220 if (IS_HASWELL(dev)) {
221 ecochk |= ECOCHK_PPGTT_WB_HSW;
222 } else {
223 ecochk |= ECOCHK_PPGTT_LLC_IVB;
224 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
225 }
226 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawsky61973492013-04-08 18:43:54 -0700227 /* GFX_MODE is per-ring on gen7+ */
228 }
229
230 for_each_ring(ring, dev_priv, i) {
231 if (INTEL_INFO(dev)->gen >= 7)
232 I915_WRITE(RING_MODE_GEN7(ring),
233 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
234
235 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
236 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
237 }
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700238 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700239}
240
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100241/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700242static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100243 unsigned first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700244 unsigned num_entries,
245 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100246{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700247 struct i915_hw_ppgtt *ppgtt =
248 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700249 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Daniel Vettera15326a2013-03-19 23:48:39 +0100250 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100251 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
252 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100253
Ben Widawskyb35b3802013-10-16 09:18:21 -0700254 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100255
Daniel Vetter7bddb012012-02-09 17:15:47 +0100256 while (num_entries) {
257 last_pte = first_pte + num_entries;
258 if (last_pte > I915_PPGTT_PT_ENTRIES)
259 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100260
Daniel Vettera15326a2013-03-19 23:48:39 +0100261 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100262
263 for (i = first_pte; i < last_pte; i++)
264 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100265
266 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100267
Daniel Vetter7bddb012012-02-09 17:15:47 +0100268 num_entries -= last_pte - first_pte;
269 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100270 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100271 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100272}
273
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700274static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800275 struct sg_table *pages,
276 unsigned first_entry,
277 enum i915_cache_level cache_level)
278{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700279 struct i915_hw_ppgtt *ppgtt =
280 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700281 gen6_gtt_pte_t *pt_vaddr;
Daniel Vettera15326a2013-03-19 23:48:39 +0100282 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200283 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
284 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800285
Daniel Vettera15326a2013-03-19 23:48:39 +0100286 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200287 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
288 dma_addr_t page_addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800289
Imre Deak2db76d72013-03-26 15:14:18 +0200290 page_addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -0700291 pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200292 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
293 kunmap_atomic(pt_vaddr);
Daniel Vettera15326a2013-03-19 23:48:39 +0100294 act_pt++;
295 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200296 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800297
Daniel Vetterdef886c2013-01-24 14:44:56 -0800298 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800299 }
Imre Deak6e995e22013-02-18 19:28:04 +0200300 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800301}
302
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700303static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100304{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700305 struct i915_hw_ppgtt *ppgtt =
306 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -0800307 int i;
308
Ben Widawsky93bd8642013-07-16 16:50:06 -0700309 drm_mm_takedown(&ppgtt->base.mm);
310
Daniel Vetter3440d262013-01-24 13:49:56 -0800311 if (ppgtt->pt_dma_addr) {
312 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700313 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800314 ppgtt->pt_dma_addr[i],
315 4096, PCI_DMA_BIDIRECTIONAL);
316 }
317
318 kfree(ppgtt->pt_dma_addr);
319 for (i = 0; i < ppgtt->num_pd_entries; i++)
320 __free_page(ppgtt->pt_pages[i]);
321 kfree(ppgtt->pt_pages);
322 kfree(ppgtt);
323}
324
325static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
326{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700327 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100328 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100329 unsigned first_pd_entry_in_global_pt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100330 int i;
331 int ret = -ENOMEM;
332
333 /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024
334 * entries. For aliasing ppgtt support we just steal them at the end for
335 * now. */
Daniel Vettere1b73cb2013-05-21 09:52:16 +0200336 first_pd_entry_in_global_pt = gtt_total_entries(dev_priv->gtt);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100337
Chris Wilson08c45262013-07-30 19:04:37 +0100338 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700339 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky61973492013-04-08 18:43:54 -0700340 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700341 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
342 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
343 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
344 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vettera1e22652013-09-21 00:35:38 +0200345 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100346 GFP_KERNEL);
347 if (!ppgtt->pt_pages)
Daniel Vetter3440d262013-01-24 13:49:56 -0800348 return -ENOMEM;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100349
350 for (i = 0; i < ppgtt->num_pd_entries; i++) {
351 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
352 if (!ppgtt->pt_pages[i])
353 goto err_pt_alloc;
354 }
355
Daniel Vettera1e22652013-09-21 00:35:38 +0200356 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800357 GFP_KERNEL);
358 if (!ppgtt->pt_dma_addr)
359 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100360
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800361 for (i = 0; i < ppgtt->num_pd_entries; i++) {
362 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200363
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800364 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
365 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100366
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800367 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
368 ret = -EIO;
369 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100370
Daniel Vetter211c5682012-04-10 17:29:17 +0200371 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800372 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100373 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100374
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700375 ppgtt->base.clear_range(&ppgtt->base, 0,
Ben Widawsky828c7902013-10-16 09:21:30 -0700376 ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100377
Ben Widawskye7c2b582013-04-08 18:43:48 -0700378 ppgtt->pd_offset = first_pd_entry_in_global_pt * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100379
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100380 return 0;
381
382err_pd_pin:
383 if (ppgtt->pt_dma_addr) {
384 for (i--; i >= 0; i--)
385 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
386 4096, PCI_DMA_BIDIRECTIONAL);
387 }
388err_pt_alloc:
389 kfree(ppgtt->pt_dma_addr);
390 for (i = 0; i < ppgtt->num_pd_entries; i++) {
391 if (ppgtt->pt_pages[i])
392 __free_page(ppgtt->pt_pages[i]);
393 }
394 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800395
396 return ret;
397}
398
399static int i915_gem_init_aliasing_ppgtt(struct drm_device *dev)
400{
401 struct drm_i915_private *dev_priv = dev->dev_private;
402 struct i915_hw_ppgtt *ppgtt;
403 int ret;
404
405 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
406 if (!ppgtt)
407 return -ENOMEM;
408
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700409 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -0800410
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700411 if (INTEL_INFO(dev)->gen < 8)
412 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -0700413 else if (IS_GEN8(dev))
414 ret = -ENOSYS;
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700415 else
416 BUG();
417
Daniel Vetter3440d262013-01-24 13:49:56 -0800418 if (ret)
419 kfree(ppgtt);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700420 else {
Daniel Vetter3440d262013-01-24 13:49:56 -0800421 dev_priv->mm.aliasing_ppgtt = ppgtt;
Ben Widawsky93bd8642013-07-16 16:50:06 -0700422 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
423 ppgtt->base.total);
424 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100425
426 return ret;
427}
428
429void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev)
430{
431 struct drm_i915_private *dev_priv = dev->dev_private;
432 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100433
434 if (!ppgtt)
435 return;
436
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700437 ppgtt->base.cleanup(&ppgtt->base);
Ben Widawsky5963cf02013-04-08 18:43:55 -0700438 dev_priv->mm.aliasing_ppgtt = NULL;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100439}
440
Daniel Vetter7bddb012012-02-09 17:15:47 +0100441void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt,
442 struct drm_i915_gem_object *obj,
443 enum i915_cache_level cache_level)
444{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700445 ppgtt->base.insert_entries(&ppgtt->base, obj->pages,
446 i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT,
447 cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100448}
449
450void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt,
451 struct drm_i915_gem_object *obj)
452{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700453 ppgtt->base.clear_range(&ppgtt->base,
454 i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT,
Ben Widawsky828c7902013-10-16 09:21:30 -0700455 obj->base.size >> PAGE_SHIFT,
456 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100457}
458
Ben Widawskya81cc002013-01-18 12:30:31 -0800459extern int intel_iommu_gfx_mapped;
460/* Certain Gen5 chipsets require require idling the GPU before
461 * unmapping anything from the GTT when VT-d is enabled.
462 */
463static inline bool needs_idle_maps(struct drm_device *dev)
464{
465#ifdef CONFIG_INTEL_IOMMU
466 /* Query intel_iommu to see if we need the workaround. Presumably that
467 * was loaded first.
468 */
469 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
470 return true;
471#endif
472 return false;
473}
474
Ben Widawsky5c042282011-10-17 15:51:55 -0700475static bool do_idling(struct drm_i915_private *dev_priv)
476{
477 bool ret = dev_priv->mm.interruptible;
478
Ben Widawskya81cc002013-01-18 12:30:31 -0800479 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700480 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700481 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700482 DRM_ERROR("Couldn't idle GPU\n");
483 /* Wait a bit, in hopes it avoids the hang */
484 udelay(10);
485 }
486 }
487
488 return ret;
489}
490
491static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
492{
Ben Widawskya81cc002013-01-18 12:30:31 -0800493 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -0700494 dev_priv->mm.interruptible = interruptible;
495}
496
Ben Widawsky828c7902013-10-16 09:21:30 -0700497void i915_check_and_clear_faults(struct drm_device *dev)
498{
499 struct drm_i915_private *dev_priv = dev->dev_private;
500 struct intel_ring_buffer *ring;
501 int i;
502
503 if (INTEL_INFO(dev)->gen < 6)
504 return;
505
506 for_each_ring(ring, dev_priv, i) {
507 u32 fault_reg;
508 fault_reg = I915_READ(RING_FAULT_REG(ring));
509 if (fault_reg & RING_FAULT_VALID) {
510 DRM_DEBUG_DRIVER("Unexpected fault\n"
511 "\tAddr: 0x%08lx\\n"
512 "\tAddress space: %s\n"
513 "\tSource ID: %d\n"
514 "\tType: %d\n",
515 fault_reg & PAGE_MASK,
516 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
517 RING_FAULT_SRCID(fault_reg),
518 RING_FAULT_FAULT_TYPE(fault_reg));
519 I915_WRITE(RING_FAULT_REG(ring),
520 fault_reg & ~RING_FAULT_VALID);
521 }
522 }
523 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
524}
525
526void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
527{
528 struct drm_i915_private *dev_priv = dev->dev_private;
529
530 /* Don't bother messing with faults pre GEN6 as we have little
531 * documentation supporting that it's a good idea.
532 */
533 if (INTEL_INFO(dev)->gen < 6)
534 return;
535
536 i915_check_and_clear_faults(dev);
537
538 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
539 dev_priv->gtt.base.start / PAGE_SIZE,
540 dev_priv->gtt.base.total / PAGE_SIZE,
541 false);
542}
543
Daniel Vetter76aaf222010-11-05 22:23:30 +0100544void i915_gem_restore_gtt_mappings(struct drm_device *dev)
545{
546 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000547 struct drm_i915_gem_object *obj;
Daniel Vetter76aaf222010-11-05 22:23:30 +0100548
Ben Widawsky828c7902013-10-16 09:21:30 -0700549 i915_check_and_clear_faults(dev);
550
Chris Wilsonbee4a182011-01-21 10:54:32 +0000551 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700552 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
553 dev_priv->gtt.base.start / PAGE_SIZE,
Ben Widawsky828c7902013-10-16 09:21:30 -0700554 dev_priv->gtt.base.total / PAGE_SIZE,
555 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +0000556
Ben Widawsky35c20a62013-05-31 11:28:48 -0700557 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Chris Wilson2c225692013-08-09 12:26:45 +0100558 i915_gem_clflush_object(obj, obj->pin_display);
Daniel Vetter74163902012-02-15 23:50:21 +0100559 i915_gem_gtt_bind_object(obj, obj->cache_level);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100560 }
561
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800562 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100563}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100564
Daniel Vetter74163902012-02-15 23:50:21 +0100565int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100566{
Chris Wilson9da3da62012-06-01 15:20:22 +0100567 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +0100568 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +0100569
570 if (!dma_map_sg(&obj->base.dev->pdev->dev,
571 obj->pages->sgl, obj->pages->nents,
572 PCI_DMA_BIDIRECTIONAL))
573 return -ENOSPC;
574
575 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100576}
577
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800578/*
579 * Binds an object into the global gtt with the specified cache level. The object
580 * will be accessible to the GPU via commands whose operands reference offsets
581 * within the global GTT as well as accessible by the GPU through the GMADR
582 * mapped BAR (dev_priv->mm.gtt->gtt).
583 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700584static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800585 struct sg_table *st,
586 unsigned int first_entry,
587 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800588{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700589 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -0700590 gen6_gtt_pte_t __iomem *gtt_entries =
591 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +0200592 int i = 0;
593 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800594 dma_addr_t addr;
595
Imre Deak6e995e22013-02-18 19:28:04 +0200596 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +0200597 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -0700598 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +0200599 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800600 }
601
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800602 /* XXX: This serves as a posting read to make sure that the PTE has
603 * actually been updated. There is some concern that even though
604 * registers and PTEs are within the same BAR that they are potentially
605 * of NUMA access patterns. Therefore, even with the way we assume
606 * hardware should work, we must keep this posting read for paranoia.
607 */
608 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700609 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -0700610 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -0800611
612 /* This next bit makes the above posting read even more important. We
613 * want to flush the TLBs only after we're certain all the PTE updates
614 * have finished.
615 */
616 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
617 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800618}
619
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700620static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800621 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700622 unsigned int num_entries,
623 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800624{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700625 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -0700626 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
627 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -0800628 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800629 int i;
630
631 if (WARN(num_entries > max_entries,
632 "First entry = %d; Num entries = %d (max=%d)\n",
633 first_entry, num_entries, max_entries))
634 num_entries = max_entries;
635
Ben Widawsky828c7902013-10-16 09:21:30 -0700636 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
637
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800638 for (i = 0; i < num_entries; i++)
639 iowrite32(scratch_pte, &gtt_base[i]);
640 readl(gtt_base);
641}
642
643
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700644static void i915_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800645 struct sg_table *st,
646 unsigned int pg_start,
647 enum i915_cache_level cache_level)
648{
649 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
650 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
651
652 intel_gtt_insert_sg_entries(st, pg_start, flags);
653
654}
655
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700656static void i915_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800657 unsigned int first_entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700658 unsigned int num_entries,
659 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800660{
661 intel_gtt_clear_range(first_entry, num_entries);
662}
663
664
Daniel Vetter74163902012-02-15 23:50:21 +0100665void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj,
666 enum i915_cache_level cache_level)
Chris Wilsond5bd1442011-04-14 06:48:26 +0100667{
668 struct drm_device *dev = obj->base.dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800669 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700670 const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800671
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700672 dev_priv->gtt.base.insert_entries(&dev_priv->gtt.base, obj->pages,
673 entry,
674 cache_level);
Chris Wilsond5bd1442011-04-14 06:48:26 +0100675
Daniel Vetter74898d72012-02-15 23:50:22 +0100676 obj->has_global_gtt_mapping = 1;
Chris Wilsond5bd1442011-04-14 06:48:26 +0100677}
678
Chris Wilson05394f32010-11-08 19:18:58 +0000679void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100680{
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800681 struct drm_device *dev = obj->base.dev;
682 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700683 const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800684
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700685 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
686 entry,
Ben Widawsky828c7902013-10-16 09:21:30 -0700687 obj->base.size >> PAGE_SHIFT,
688 true);
Daniel Vetter74898d72012-02-15 23:50:22 +0100689
690 obj->has_global_gtt_mapping = 0;
Daniel Vetter74163902012-02-15 23:50:21 +0100691}
692
693void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
694{
Ben Widawsky5c042282011-10-17 15:51:55 -0700695 struct drm_device *dev = obj->base.dev;
696 struct drm_i915_private *dev_priv = dev->dev_private;
697 bool interruptible;
698
699 interruptible = do_idling(dev_priv);
700
Chris Wilson9da3da62012-06-01 15:20:22 +0100701 if (!obj->has_dma_mapping)
702 dma_unmap_sg(&dev->pdev->dev,
703 obj->pages->sgl, obj->pages->nents,
704 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -0700705
706 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100707}
Daniel Vetter644ec022012-03-26 09:45:40 +0200708
Chris Wilson42d6ab42012-07-26 11:49:32 +0100709static void i915_gtt_color_adjust(struct drm_mm_node *node,
710 unsigned long color,
711 unsigned long *start,
712 unsigned long *end)
713{
714 if (node->color != color)
715 *start += 4096;
716
717 if (!list_empty(&node->node_list)) {
718 node = list_entry(node->node_list.next,
719 struct drm_mm_node,
720 node_list);
721 if (node->allocated && node->color != color)
722 *end -= 4096;
723 }
724}
Ben Widawskyd7e50082012-12-18 10:31:25 -0800725void i915_gem_setup_global_gtt(struct drm_device *dev,
726 unsigned long start,
727 unsigned long mappable_end,
728 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +0200729{
Ben Widawskye78891c2013-01-25 16:41:04 -0800730 /* Let GEM Manage all of the aperture.
731 *
732 * However, leave one page at the end still bound to the scratch page.
733 * There are a number of places where the hardware apparently prefetches
734 * past the end of the object, and we've seen multiple hangs with the
735 * GPU head pointer stuck in a batchbuffer bound at the last page of the
736 * aperture. One page should be enough to keep any prefetching inside
737 * of the aperture.
738 */
Ben Widawsky40d749802013-07-31 16:59:59 -0700739 struct drm_i915_private *dev_priv = dev->dev_private;
740 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +0000741 struct drm_mm_node *entry;
742 struct drm_i915_gem_object *obj;
743 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +0200744
Ben Widawsky35451cb2013-01-17 12:45:13 -0800745 BUG_ON(mappable_end > end);
746
Chris Wilsoned2f3452012-11-15 11:32:19 +0000747 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -0700748 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +0100749 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -0700750 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +0200751
Chris Wilsoned2f3452012-11-15 11:32:19 +0000752 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -0700753 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -0700754 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700755 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -0700756 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700757 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000758
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700759 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -0700760 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700761 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700762 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +0000763 obj->has_global_gtt_mapping = 1;
Ben Widawsky2f633152013-07-17 12:19:03 -0700764 list_add(&vma->vma_link, &obj->vma_list);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000765 }
766
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700767 dev_priv->gtt.base.start = start;
768 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +0200769
Chris Wilsoned2f3452012-11-15 11:32:19 +0000770 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -0700771 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700772 const unsigned long count = (hole_end - hole_start) / PAGE_SIZE;
Chris Wilsoned2f3452012-11-15 11:32:19 +0000773 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
774 hole_start, hole_end);
Ben Widawsky828c7902013-10-16 09:21:30 -0700775 ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000776 }
777
778 /* And finally clear the reserved guard page */
Ben Widawsky828c7902013-10-16 09:21:30 -0700779 ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800780}
781
Ben Widawskyd7e50082012-12-18 10:31:25 -0800782static bool
783intel_enable_ppgtt(struct drm_device *dev)
784{
785 if (i915_enable_ppgtt >= 0)
786 return i915_enable_ppgtt;
787
788#ifdef CONFIG_INTEL_IOMMU
789 /* Disable ppgtt on SNB if VT-d is on. */
790 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
791 return false;
792#endif
793
794 return true;
795}
796
797void i915_gem_init_global_gtt(struct drm_device *dev)
798{
799 struct drm_i915_private *dev_priv = dev->dev_private;
800 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800801
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700802 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -0800803 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800804
805 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
Ben Widawskye78891c2013-01-25 16:41:04 -0800806 int ret;
Ben Widawsky3eb1c002013-04-08 18:43:52 -0700807
808 if (INTEL_INFO(dev)->gen <= 7) {
809 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
810 * aperture accordingly when using aliasing ppgtt. */
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700811 gtt_size -= GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE;
Ben Widawsky3eb1c002013-04-08 18:43:52 -0700812 }
Ben Widawskyd7e50082012-12-18 10:31:25 -0800813
814 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
815
816 ret = i915_gem_init_aliasing_ppgtt(dev);
Ben Widawskye78891c2013-01-25 16:41:04 -0800817 if (!ret)
Ben Widawskyd7e50082012-12-18 10:31:25 -0800818 return;
Ben Widawskye78891c2013-01-25 16:41:04 -0800819
820 DRM_ERROR("Aliased PPGTT setup failed %d\n", ret);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700821 drm_mm_takedown(&dev_priv->gtt.base.mm);
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700822 gtt_size += GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800823 }
Ben Widawskye78891c2013-01-25 16:41:04 -0800824 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800825}
826
827static int setup_scratch_page(struct drm_device *dev)
828{
829 struct drm_i915_private *dev_priv = dev->dev_private;
830 struct page *page;
831 dma_addr_t dma_addr;
832
833 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
834 if (page == NULL)
835 return -ENOMEM;
836 get_page(page);
837 set_pages_uc(page, 1);
838
839#ifdef CONFIG_INTEL_IOMMU
840 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
841 PCI_DMA_BIDIRECTIONAL);
842 if (pci_dma_mapping_error(dev->pdev, dma_addr))
843 return -EINVAL;
844#else
845 dma_addr = page_to_phys(page);
846#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700847 dev_priv->gtt.base.scratch.page = page;
848 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800849
850 return 0;
851}
852
853static void teardown_scratch_page(struct drm_device *dev)
854{
855 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700856 struct page *page = dev_priv->gtt.base.scratch.page;
857
858 set_pages_wb(page, 1);
859 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800860 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700861 put_page(page);
862 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800863}
864
865static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
866{
867 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
868 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
869 return snb_gmch_ctl << 20;
870}
871
Ben Widawsky9459d252013-11-03 16:53:55 -0800872static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
873{
874 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
875 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
876 if (bdw_gmch_ctl)
877 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
878 return bdw_gmch_ctl << 20;
879}
880
Ben Widawskybaa09f52013-01-24 13:49:57 -0800881static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800882{
883 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
884 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
885 return snb_gmch_ctl << 25; /* 32 MB units */
886}
887
Ben Widawsky9459d252013-11-03 16:53:55 -0800888static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
889{
890 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
891 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
892 return bdw_gmch_ctl << 25; /* 32 MB units */
893}
894
Ben Widawskybaa09f52013-01-24 13:49:57 -0800895static int gen6_gmch_probe(struct drm_device *dev,
896 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -0800897 size_t *stolen,
898 phys_addr_t *mappable_base,
899 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800900{
901 struct drm_i915_private *dev_priv = dev->dev_private;
902 phys_addr_t gtt_bus_addr;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800903 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800904 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800905 int ret;
906
Ben Widawsky41907dd2013-02-08 11:32:47 -0800907 *mappable_base = pci_resource_start(dev->pdev, 2);
908 *mappable_end = pci_resource_len(dev->pdev, 2);
909
Ben Widawskybaa09f52013-01-24 13:49:57 -0800910 /* 64/512MB is the current min/max we actually know of, but this is just
911 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800912 */
Ben Widawsky41907dd2013-02-08 11:32:47 -0800913 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -0800914 DRM_ERROR("Unknown GMADR size (%lx)\n",
915 dev_priv->gtt.mappable_end);
916 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800917 }
918
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800919 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
920 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -0800921 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -0800922
Ben Widawsky9459d252013-11-03 16:53:55 -0800923 if (IS_GEN8(dev)) {
924 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
925 *gtt_total = (gtt_size / 8) << PAGE_SHIFT;
926 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
927 } else {
928 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
929 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
930 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
931 }
Ben Widawskybaa09f52013-01-24 13:49:57 -0800932
Ben Widawskya93e4162013-04-08 18:43:47 -0700933 /* For Modern GENs the PTEs and register space are split in the BAR */
934 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
935 (pci_resource_len(dev->pdev, 0) / 2);
936
Ben Widawskybaa09f52013-01-24 13:49:57 -0800937 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
938 if (!dev_priv->gtt.gsm) {
939 DRM_ERROR("Failed to map the gtt page table\n");
940 return -ENOMEM;
Ben Widawsky9459d252013-11-03 16:53:55 -0800941
Ben Widawskybaa09f52013-01-24 13:49:57 -0800942 }
943
944 ret = setup_scratch_page(dev);
945 if (ret)
946 DRM_ERROR("Scratch setup failed\n");
947
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700948 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
949 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800950
951 return ret;
952}
953
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700954static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -0800955{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700956
957 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
958 iounmap(gtt->gsm);
959 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -0800960}
961
962static int i915_gmch_probe(struct drm_device *dev,
963 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -0800964 size_t *stolen,
965 phys_addr_t *mappable_base,
966 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -0800967{
968 struct drm_i915_private *dev_priv = dev->dev_private;
969 int ret;
970
Ben Widawskybaa09f52013-01-24 13:49:57 -0800971 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
972 if (!ret) {
973 DRM_ERROR("failed to set up gmch\n");
974 return -EIO;
975 }
976
Ben Widawsky41907dd2013-02-08 11:32:47 -0800977 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -0800978
979 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700980 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
981 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800982
983 return 0;
984}
985
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700986static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -0800987{
988 intel_gmch_remove();
989}
990
991int i915_gem_gtt_init(struct drm_device *dev)
992{
993 struct drm_i915_private *dev_priv = dev->dev_private;
994 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800995 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800996
Ben Widawskybaa09f52013-01-24 13:49:57 -0800997 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -0700998 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700999 gtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001000 } else {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001001 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001002 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001003 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001004 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001005 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001006 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001007 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001008 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001009 else if (INTEL_INFO(dev)->gen >= 7)
1010 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001011 else
Chris Wilson350ec882013-08-06 13:17:02 +01001012 gtt->base.pte_encode = snb_pte_encode;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001013 }
1014
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001015 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001016 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001017 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001018 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001019
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001020 gtt->base.dev = dev;
1021
Ben Widawskybaa09f52013-01-24 13:49:57 -08001022 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001023 DRM_INFO("Memory usable by graphics device = %zdM\n",
1024 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001025 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1026 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001027
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001028 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001029}