blob: f38cc696be7f5c57df3dc1c3cf454fb00bbce1f0 [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)
46#define GEN6_PTE_CACHE_LLC_MLC (3 << 1)
47#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)
Ben Widawsky26b1ff32012-11-04 09:21:31 -080058
Ben Widawsky80a74f72013-06-27 16:30:19 -070059static gen6_gtt_pte_t gen6_pte_encode(dma_addr_t addr,
Kenneth Graunke2d04bef2013-04-22 00:53:49 -070060 enum i915_cache_level level)
Ben Widawsky54d12522012-09-24 16:44:32 -070061{
Ben Widawskye7c2b582013-04-08 18:43:48 -070062 gen6_gtt_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -070063 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -070064
65 switch (level) {
66 case I915_CACHE_LLC_MLC:
Kenneth Graunke91197082013-04-22 00:53:51 -070067 pte |= GEN6_PTE_CACHE_LLC_MLC;
Ben Widawskye7210c32012-10-19 09:33:22 -070068 break;
69 case I915_CACHE_LLC:
70 pte |= GEN6_PTE_CACHE_LLC;
71 break;
72 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -070073 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -070074 break;
75 default:
76 BUG();
77 }
78
Ben Widawsky54d12522012-09-24 16:44:32 -070079 return pte;
80}
81
Kenneth Graunke93c34e72013-04-22 00:53:50 -070082#define BYT_PTE_WRITEABLE (1 << 1)
83#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
84
Ben Widawsky80a74f72013-06-27 16:30:19 -070085static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Kenneth Graunke93c34e72013-04-22 00:53:50 -070086 enum i915_cache_level level)
87{
88 gen6_gtt_pte_t pte = GEN6_PTE_VALID;
89 pte |= GEN6_PTE_ADDR_ENCODE(addr);
90
91 /* Mark the page as writeable. Other platforms don't have a
92 * setting for read-only/writable, so this matches that behavior.
93 */
94 pte |= BYT_PTE_WRITEABLE;
95
96 if (level != I915_CACHE_NONE)
97 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
98
99 return pte;
100}
101
Ben Widawsky80a74f72013-06-27 16:30:19 -0700102static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Kenneth Graunke91197082013-04-22 00:53:51 -0700103 enum i915_cache_level level)
104{
105 gen6_gtt_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700106 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700107
108 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700109 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700110
111 return pte;
112}
113
Ben Widawsky4d15c142013-07-04 11:02:06 -0700114static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
115 enum i915_cache_level level)
116{
117 gen6_gtt_pte_t pte = GEN6_PTE_VALID;
118 pte |= HSW_PTE_ADDR_ENCODE(addr);
119
120 if (level != I915_CACHE_NONE)
121 pte |= HSW_WB_ELLC_LLC_AGE0;
122
123 return pte;
124}
125
Ben Widawsky3e302542013-04-23 23:15:32 -0700126static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700127{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700128 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700129 gen6_gtt_pte_t __iomem *pd_addr;
130 uint32_t pd_entry;
131 int i;
132
Ben Widawsky0a732872013-04-23 23:15:30 -0700133 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700134 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
135 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
136 for (i = 0; i < ppgtt->num_pd_entries; i++) {
137 dma_addr_t pt_addr;
138
139 pt_addr = ppgtt->pt_dma_addr[i];
140 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
141 pd_entry |= GEN6_PDE_VALID;
142
143 writel(pd_entry, pd_addr + i);
144 }
145 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700146}
147
148static int gen6_ppgtt_enable(struct drm_device *dev)
149{
150 drm_i915_private_t *dev_priv = dev->dev_private;
151 uint32_t pd_offset;
152 struct intel_ring_buffer *ring;
153 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
154 int i;
155
156 BUG_ON(ppgtt->pd_offset & 0x3f);
157
158 gen6_write_pdes(ppgtt);
Ben Widawsky61973492013-04-08 18:43:54 -0700159
160 pd_offset = ppgtt->pd_offset;
161 pd_offset /= 64; /* in cachelines, */
162 pd_offset <<= 16;
163
164 if (INTEL_INFO(dev)->gen == 6) {
165 uint32_t ecochk, gab_ctl, ecobits;
166
167 ecobits = I915_READ(GAC_ECO_BITS);
Ville Syrjälä3b9d7882013-04-04 15:13:40 +0300168 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
169 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700170
171 gab_ctl = I915_READ(GAB_CTL);
172 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
173
174 ecochk = I915_READ(GAM_ECOCHK);
175 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
176 ECOCHK_PPGTT_CACHE64B);
177 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
178 } else if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjäläa6f429a2013-04-04 15:13:42 +0300179 uint32_t ecochk, ecobits;
Ville Syrjäläa65c2fc2013-04-04 15:13:41 +0300180
181 ecobits = I915_READ(GAC_ECO_BITS);
182 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
183
Ville Syrjäläa6f429a2013-04-04 15:13:42 +0300184 ecochk = I915_READ(GAM_ECOCHK);
185 if (IS_HASWELL(dev)) {
186 ecochk |= ECOCHK_PPGTT_WB_HSW;
187 } else {
188 ecochk |= ECOCHK_PPGTT_LLC_IVB;
189 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
190 }
191 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawsky61973492013-04-08 18:43:54 -0700192 /* GFX_MODE is per-ring on gen7+ */
193 }
194
195 for_each_ring(ring, dev_priv, i) {
196 if (INTEL_INFO(dev)->gen >= 7)
197 I915_WRITE(RING_MODE_GEN7(ring),
198 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
199
200 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
201 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
202 }
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700203 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700204}
205
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100206/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700207static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100208 unsigned first_entry,
209 unsigned num_entries)
210{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700211 struct i915_hw_ppgtt *ppgtt =
212 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700213 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Daniel Vettera15326a2013-03-19 23:48:39 +0100214 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100215 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
216 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100217
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700218 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100219
Daniel Vetter7bddb012012-02-09 17:15:47 +0100220 while (num_entries) {
221 last_pte = first_pte + num_entries;
222 if (last_pte > I915_PPGTT_PT_ENTRIES)
223 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100224
Daniel Vettera15326a2013-03-19 23:48:39 +0100225 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100226
227 for (i = first_pte; i < last_pte; i++)
228 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100229
230 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100231
Daniel Vetter7bddb012012-02-09 17:15:47 +0100232 num_entries -= last_pte - first_pte;
233 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100234 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100235 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100236}
237
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700238static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800239 struct sg_table *pages,
240 unsigned first_entry,
241 enum i915_cache_level cache_level)
242{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700243 struct i915_hw_ppgtt *ppgtt =
244 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700245 gen6_gtt_pte_t *pt_vaddr;
Daniel Vettera15326a2013-03-19 23:48:39 +0100246 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200247 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
248 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800249
Daniel Vettera15326a2013-03-19 23:48:39 +0100250 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200251 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
252 dma_addr_t page_addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800253
Imre Deak2db76d72013-03-26 15:14:18 +0200254 page_addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700255 pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level);
Imre Deak6e995e22013-02-18 19:28:04 +0200256 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
257 kunmap_atomic(pt_vaddr);
Daniel Vettera15326a2013-03-19 23:48:39 +0100258 act_pt++;
259 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +0200260 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800261
Daniel Vetterdef886c2013-01-24 14:44:56 -0800262 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800263 }
Imre Deak6e995e22013-02-18 19:28:04 +0200264 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800265}
266
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700267static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100268{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700269 struct i915_hw_ppgtt *ppgtt =
270 container_of(vm, struct i915_hw_ppgtt, base);
Daniel Vetter3440d262013-01-24 13:49:56 -0800271 int i;
272
Ben Widawsky93bd8642013-07-16 16:50:06 -0700273 drm_mm_takedown(&ppgtt->base.mm);
274
Daniel Vetter3440d262013-01-24 13:49:56 -0800275 if (ppgtt->pt_dma_addr) {
276 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700277 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800278 ppgtt->pt_dma_addr[i],
279 4096, PCI_DMA_BIDIRECTIONAL);
280 }
281
282 kfree(ppgtt->pt_dma_addr);
283 for (i = 0; i < ppgtt->num_pd_entries; i++)
284 __free_page(ppgtt->pt_pages[i]);
285 kfree(ppgtt->pt_pages);
286 kfree(ppgtt);
287}
288
289static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
290{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700291 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100292 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100293 unsigned first_pd_entry_in_global_pt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100294 int i;
295 int ret = -ENOMEM;
296
297 /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024
298 * entries. For aliasing ppgtt support we just steal them at the end for
299 * now. */
Daniel Vettere1b73cb2013-05-21 09:52:16 +0200300 first_pd_entry_in_global_pt = gtt_total_entries(dev_priv->gtt);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100301
Chris Wilson08c45262013-07-30 19:04:37 +0100302 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700303 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawsky61973492013-04-08 18:43:54 -0700304 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700305 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
306 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
307 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
308 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100309 ppgtt->pt_pages = kzalloc(sizeof(struct page *)*ppgtt->num_pd_entries,
310 GFP_KERNEL);
311 if (!ppgtt->pt_pages)
Daniel Vetter3440d262013-01-24 13:49:56 -0800312 return -ENOMEM;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100313
314 for (i = 0; i < ppgtt->num_pd_entries; i++) {
315 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
316 if (!ppgtt->pt_pages[i])
317 goto err_pt_alloc;
318 }
319
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800320 ppgtt->pt_dma_addr = kzalloc(sizeof(dma_addr_t) *ppgtt->num_pd_entries,
321 GFP_KERNEL);
322 if (!ppgtt->pt_dma_addr)
323 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100324
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800325 for (i = 0; i < ppgtt->num_pd_entries; i++) {
326 dma_addr_t pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200327
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800328 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
329 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100330
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800331 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
332 ret = -EIO;
333 goto err_pd_pin;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100334
Daniel Vetter211c5682012-04-10 17:29:17 +0200335 }
Ben Widawsky8d2e6302013-01-18 12:30:33 -0800336 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100337 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100338
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700339 ppgtt->base.clear_range(&ppgtt->base, 0,
340 ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100341
Ben Widawskye7c2b582013-04-08 18:43:48 -0700342 ppgtt->pd_offset = first_pd_entry_in_global_pt * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100343
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100344 return 0;
345
346err_pd_pin:
347 if (ppgtt->pt_dma_addr) {
348 for (i--; i >= 0; i--)
349 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
350 4096, PCI_DMA_BIDIRECTIONAL);
351 }
352err_pt_alloc:
353 kfree(ppgtt->pt_dma_addr);
354 for (i = 0; i < ppgtt->num_pd_entries; i++) {
355 if (ppgtt->pt_pages[i])
356 __free_page(ppgtt->pt_pages[i]);
357 }
358 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800359
360 return ret;
361}
362
363static int i915_gem_init_aliasing_ppgtt(struct drm_device *dev)
364{
365 struct drm_i915_private *dev_priv = dev->dev_private;
366 struct i915_hw_ppgtt *ppgtt;
367 int ret;
368
369 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
370 if (!ppgtt)
371 return -ENOMEM;
372
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700373 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -0800374
Ben Widawsky3ed124b2013-04-08 18:43:53 -0700375 if (INTEL_INFO(dev)->gen < 8)
376 ret = gen6_ppgtt_init(ppgtt);
377 else
378 BUG();
379
Daniel Vetter3440d262013-01-24 13:49:56 -0800380 if (ret)
381 kfree(ppgtt);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700382 else {
Daniel Vetter3440d262013-01-24 13:49:56 -0800383 dev_priv->mm.aliasing_ppgtt = ppgtt;
Ben Widawsky93bd8642013-07-16 16:50:06 -0700384 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
385 ppgtt->base.total);
386 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100387
388 return ret;
389}
390
391void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev)
392{
393 struct drm_i915_private *dev_priv = dev->dev_private;
394 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100395
396 if (!ppgtt)
397 return;
398
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700399 ppgtt->base.cleanup(&ppgtt->base);
Ben Widawsky5963cf02013-04-08 18:43:55 -0700400 dev_priv->mm.aliasing_ppgtt = NULL;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100401}
402
Daniel Vetter7bddb012012-02-09 17:15:47 +0100403void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt,
404 struct drm_i915_gem_object *obj,
405 enum i915_cache_level cache_level)
406{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700407 ppgtt->base.insert_entries(&ppgtt->base, obj->pages,
408 i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT,
409 cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100410}
411
412void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt,
413 struct drm_i915_gem_object *obj)
414{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700415 ppgtt->base.clear_range(&ppgtt->base,
416 i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT,
417 obj->base.size >> PAGE_SHIFT);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100418}
419
Ben Widawskya81cc002013-01-18 12:30:31 -0800420extern int intel_iommu_gfx_mapped;
421/* Certain Gen5 chipsets require require idling the GPU before
422 * unmapping anything from the GTT when VT-d is enabled.
423 */
424static inline bool needs_idle_maps(struct drm_device *dev)
425{
426#ifdef CONFIG_INTEL_IOMMU
427 /* Query intel_iommu to see if we need the workaround. Presumably that
428 * was loaded first.
429 */
430 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
431 return true;
432#endif
433 return false;
434}
435
Ben Widawsky5c042282011-10-17 15:51:55 -0700436static bool do_idling(struct drm_i915_private *dev_priv)
437{
438 bool ret = dev_priv->mm.interruptible;
439
Ben Widawskya81cc002013-01-18 12:30:31 -0800440 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700441 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700442 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700443 DRM_ERROR("Couldn't idle GPU\n");
444 /* Wait a bit, in hopes it avoids the hang */
445 udelay(10);
446 }
447 }
448
449 return ret;
450}
451
452static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
453{
Ben Widawskya81cc002013-01-18 12:30:31 -0800454 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -0700455 dev_priv->mm.interruptible = interruptible;
456}
457
Daniel Vetter76aaf222010-11-05 22:23:30 +0100458void i915_gem_restore_gtt_mappings(struct drm_device *dev)
459{
460 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000461 struct drm_i915_gem_object *obj;
Daniel Vetter76aaf222010-11-05 22:23:30 +0100462
Chris Wilsonbee4a182011-01-21 10:54:32 +0000463 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700464 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
465 dev_priv->gtt.base.start / PAGE_SIZE,
466 dev_priv->gtt.base.total / PAGE_SIZE);
Chris Wilsonbee4a182011-01-21 10:54:32 +0000467
Ben Widawsky35c20a62013-05-31 11:28:48 -0700468 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Chris Wilsona8e93122010-12-08 14:28:54 +0000469 i915_gem_clflush_object(obj);
Daniel Vetter74163902012-02-15 23:50:21 +0100470 i915_gem_gtt_bind_object(obj, obj->cache_level);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100471 }
472
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800473 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100474}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100475
Daniel Vetter74163902012-02-15 23:50:21 +0100476int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100477{
Chris Wilson9da3da62012-06-01 15:20:22 +0100478 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +0100479 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +0100480
481 if (!dma_map_sg(&obj->base.dev->pdev->dev,
482 obj->pages->sgl, obj->pages->nents,
483 PCI_DMA_BIDIRECTIONAL))
484 return -ENOSPC;
485
486 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100487}
488
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800489/*
490 * Binds an object into the global gtt with the specified cache level. The object
491 * will be accessible to the GPU via commands whose operands reference offsets
492 * within the global GTT as well as accessible by the GPU through the GMADR
493 * mapped BAR (dev_priv->mm.gtt->gtt).
494 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700495static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800496 struct sg_table *st,
497 unsigned int first_entry,
498 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800499{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700500 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -0700501 gen6_gtt_pte_t __iomem *gtt_entries =
502 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +0200503 int i = 0;
504 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800505 dma_addr_t addr;
506
Imre Deak6e995e22013-02-18 19:28:04 +0200507 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +0200508 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700509 iowrite32(vm->pte_encode(addr, level), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +0200510 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800511 }
512
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800513 /* XXX: This serves as a posting read to make sure that the PTE has
514 * actually been updated. There is some concern that even though
515 * registers and PTEs are within the same BAR that they are potentially
516 * of NUMA access patterns. Therefore, even with the way we assume
517 * hardware should work, we must keep this posting read for paranoia.
518 */
519 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700520 WARN_ON(readl(&gtt_entries[i-1]) !=
521 vm->pte_encode(addr, level));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -0800522
523 /* This next bit makes the above posting read even more important. We
524 * want to flush the TLBs only after we're certain all the PTE updates
525 * have finished.
526 */
527 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
528 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800529}
530
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700531static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800532 unsigned int first_entry,
533 unsigned int num_entries)
534{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700535 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawskye7c2b582013-04-08 18:43:48 -0700536 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
537 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -0800538 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800539 int i;
540
541 if (WARN(num_entries > max_entries,
542 "First entry = %d; Num entries = %d (max=%d)\n",
543 first_entry, num_entries, max_entries))
544 num_entries = max_entries;
545
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700546 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800547 for (i = 0; i < num_entries; i++)
548 iowrite32(scratch_pte, &gtt_base[i]);
549 readl(gtt_base);
550}
551
552
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700553static void i915_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800554 struct sg_table *st,
555 unsigned int pg_start,
556 enum i915_cache_level cache_level)
557{
558 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
559 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
560
561 intel_gtt_insert_sg_entries(st, pg_start, flags);
562
563}
564
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700565static void i915_ggtt_clear_range(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800566 unsigned int first_entry,
567 unsigned int num_entries)
568{
569 intel_gtt_clear_range(first_entry, num_entries);
570}
571
572
Daniel Vetter74163902012-02-15 23:50:21 +0100573void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj,
574 enum i915_cache_level cache_level)
Chris Wilsond5bd1442011-04-14 06:48:26 +0100575{
576 struct drm_device *dev = obj->base.dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800577 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700578 const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800579
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700580 dev_priv->gtt.base.insert_entries(&dev_priv->gtt.base, obj->pages,
581 entry,
582 cache_level);
Chris Wilsond5bd1442011-04-14 06:48:26 +0100583
Daniel Vetter74898d72012-02-15 23:50:22 +0100584 obj->has_global_gtt_mapping = 1;
Chris Wilsond5bd1442011-04-14 06:48:26 +0100585}
586
Chris Wilson05394f32010-11-08 19:18:58 +0000587void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100588{
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800589 struct drm_device *dev = obj->base.dev;
590 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700591 const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800592
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700593 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
594 entry,
595 obj->base.size >> PAGE_SHIFT);
Daniel Vetter74898d72012-02-15 23:50:22 +0100596
597 obj->has_global_gtt_mapping = 0;
Daniel Vetter74163902012-02-15 23:50:21 +0100598}
599
600void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
601{
Ben Widawsky5c042282011-10-17 15:51:55 -0700602 struct drm_device *dev = obj->base.dev;
603 struct drm_i915_private *dev_priv = dev->dev_private;
604 bool interruptible;
605
606 interruptible = do_idling(dev_priv);
607
Chris Wilson9da3da62012-06-01 15:20:22 +0100608 if (!obj->has_dma_mapping)
609 dma_unmap_sg(&dev->pdev->dev,
610 obj->pages->sgl, obj->pages->nents,
611 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -0700612
613 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100614}
Daniel Vetter644ec022012-03-26 09:45:40 +0200615
Chris Wilson42d6ab42012-07-26 11:49:32 +0100616static void i915_gtt_color_adjust(struct drm_mm_node *node,
617 unsigned long color,
618 unsigned long *start,
619 unsigned long *end)
620{
621 if (node->color != color)
622 *start += 4096;
623
624 if (!list_empty(&node->node_list)) {
625 node = list_entry(node->node_list.next,
626 struct drm_mm_node,
627 node_list);
628 if (node->allocated && node->color != color)
629 *end -= 4096;
630 }
631}
Ben Widawskyd7e50082012-12-18 10:31:25 -0800632void i915_gem_setup_global_gtt(struct drm_device *dev,
633 unsigned long start,
634 unsigned long mappable_end,
635 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +0200636{
Ben Widawskye78891c2013-01-25 16:41:04 -0800637 /* Let GEM Manage all of the aperture.
638 *
639 * However, leave one page at the end still bound to the scratch page.
640 * There are a number of places where the hardware apparently prefetches
641 * past the end of the object, and we've seen multiple hangs with the
642 * GPU head pointer stuck in a batchbuffer bound at the last page of the
643 * aperture. One page should be enough to keep any prefetching inside
644 * of the aperture.
645 */
Ben Widawsky40d749802013-07-31 16:59:59 -0700646 struct drm_i915_private *dev_priv = dev->dev_private;
647 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +0000648 struct drm_mm_node *entry;
649 struct drm_i915_gem_object *obj;
650 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +0200651
Ben Widawsky35451cb2013-01-17 12:45:13 -0800652 BUG_ON(mappable_end > end);
653
Chris Wilsoned2f3452012-11-15 11:32:19 +0000654 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -0700655 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +0100656 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -0700657 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +0200658
Chris Wilsoned2f3452012-11-15 11:32:19 +0000659 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -0700660 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -0700661 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700662 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -0700663 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700664 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000665
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700666 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -0700667 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -0700668 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -0700669 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +0000670 obj->has_global_gtt_mapping = 1;
Ben Widawsky2f633152013-07-17 12:19:03 -0700671 list_add(&vma->vma_link, &obj->vma_list);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000672 }
673
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700674 dev_priv->gtt.base.start = start;
675 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +0200676
Chris Wilsoned2f3452012-11-15 11:32:19 +0000677 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -0700678 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700679 const unsigned long count = (hole_end - hole_start) / PAGE_SIZE;
Chris Wilsoned2f3452012-11-15 11:32:19 +0000680 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
681 hole_start, hole_end);
Ben Widawsky40d749802013-07-31 16:59:59 -0700682 ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count);
Chris Wilsoned2f3452012-11-15 11:32:19 +0000683 }
684
685 /* And finally clear the reserved guard page */
Ben Widawsky40d749802013-07-31 16:59:59 -0700686 ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800687}
688
Ben Widawskyd7e50082012-12-18 10:31:25 -0800689static bool
690intel_enable_ppgtt(struct drm_device *dev)
691{
692 if (i915_enable_ppgtt >= 0)
693 return i915_enable_ppgtt;
694
695#ifdef CONFIG_INTEL_IOMMU
696 /* Disable ppgtt on SNB if VT-d is on. */
697 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
698 return false;
699#endif
700
701 return true;
702}
703
704void i915_gem_init_global_gtt(struct drm_device *dev)
705{
706 struct drm_i915_private *dev_priv = dev->dev_private;
707 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800708
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700709 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -0800710 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800711
712 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
Ben Widawskye78891c2013-01-25 16:41:04 -0800713 int ret;
Ben Widawsky3eb1c002013-04-08 18:43:52 -0700714
715 if (INTEL_INFO(dev)->gen <= 7) {
716 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
717 * aperture accordingly when using aliasing ppgtt. */
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700718 gtt_size -= GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE;
Ben Widawsky3eb1c002013-04-08 18:43:52 -0700719 }
Ben Widawskyd7e50082012-12-18 10:31:25 -0800720
721 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
722
723 ret = i915_gem_init_aliasing_ppgtt(dev);
Ben Widawskye78891c2013-01-25 16:41:04 -0800724 if (!ret)
Ben Widawskyd7e50082012-12-18 10:31:25 -0800725 return;
Ben Widawskye78891c2013-01-25 16:41:04 -0800726
727 DRM_ERROR("Aliased PPGTT setup failed %d\n", ret);
Ben Widawsky93bd8642013-07-16 16:50:06 -0700728 drm_mm_takedown(&dev_priv->gtt.base.mm);
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700729 gtt_size += GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE;
Ben Widawskyd7e50082012-12-18 10:31:25 -0800730 }
Ben Widawskye78891c2013-01-25 16:41:04 -0800731 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800732}
733
734static int setup_scratch_page(struct drm_device *dev)
735{
736 struct drm_i915_private *dev_priv = dev->dev_private;
737 struct page *page;
738 dma_addr_t dma_addr;
739
740 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
741 if (page == NULL)
742 return -ENOMEM;
743 get_page(page);
744 set_pages_uc(page, 1);
745
746#ifdef CONFIG_INTEL_IOMMU
747 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
748 PCI_DMA_BIDIRECTIONAL);
749 if (pci_dma_mapping_error(dev->pdev, dma_addr))
750 return -EINVAL;
751#else
752 dma_addr = page_to_phys(page);
753#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700754 dev_priv->gtt.base.scratch.page = page;
755 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800756
757 return 0;
758}
759
760static void teardown_scratch_page(struct drm_device *dev)
761{
762 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700763 struct page *page = dev_priv->gtt.base.scratch.page;
764
765 set_pages_wb(page, 1);
766 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800767 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700768 put_page(page);
769 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800770}
771
772static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
773{
774 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
775 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
776 return snb_gmch_ctl << 20;
777}
778
Ben Widawskybaa09f52013-01-24 13:49:57 -0800779static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800780{
781 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
782 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
783 return snb_gmch_ctl << 25; /* 32 MB units */
784}
785
Ben Widawskybaa09f52013-01-24 13:49:57 -0800786static int gen6_gmch_probe(struct drm_device *dev,
787 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -0800788 size_t *stolen,
789 phys_addr_t *mappable_base,
790 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800791{
792 struct drm_i915_private *dev_priv = dev->dev_private;
793 phys_addr_t gtt_bus_addr;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800794 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800795 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800796 int ret;
797
Ben Widawsky41907dd2013-02-08 11:32:47 -0800798 *mappable_base = pci_resource_start(dev->pdev, 2);
799 *mappable_end = pci_resource_len(dev->pdev, 2);
800
Ben Widawskybaa09f52013-01-24 13:49:57 -0800801 /* 64/512MB is the current min/max we actually know of, but this is just
802 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800803 */
Ben Widawsky41907dd2013-02-08 11:32:47 -0800804 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -0800805 DRM_ERROR("Unknown GMADR size (%lx)\n",
806 dev_priv->gtt.mappable_end);
807 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800808 }
809
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800810 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
811 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -0800812 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
813 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
814
Ben Widawskyc4ae25e2013-05-01 11:00:34 -0700815 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700816 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800817
Ben Widawskya93e4162013-04-08 18:43:47 -0700818 /* For Modern GENs the PTEs and register space are split in the BAR */
819 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
820 (pci_resource_len(dev->pdev, 0) / 2);
821
Ben Widawskybaa09f52013-01-24 13:49:57 -0800822 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
823 if (!dev_priv->gtt.gsm) {
824 DRM_ERROR("Failed to map the gtt page table\n");
825 return -ENOMEM;
826 }
827
828 ret = setup_scratch_page(dev);
829 if (ret)
830 DRM_ERROR("Scratch setup failed\n");
831
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700832 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
833 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800834
835 return ret;
836}
837
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700838static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -0800839{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700840
841 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
842 iounmap(gtt->gsm);
843 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -0800844}
845
846static int i915_gmch_probe(struct drm_device *dev,
847 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -0800848 size_t *stolen,
849 phys_addr_t *mappable_base,
850 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -0800851{
852 struct drm_i915_private *dev_priv = dev->dev_private;
853 int ret;
854
Ben Widawskybaa09f52013-01-24 13:49:57 -0800855 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
856 if (!ret) {
857 DRM_ERROR("failed to set up gmch\n");
858 return -EIO;
859 }
860
Ben Widawsky41907dd2013-02-08 11:32:47 -0800861 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -0800862
863 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700864 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
865 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800866
867 return 0;
868}
869
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700870static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -0800871{
872 intel_gmch_remove();
873}
874
875int i915_gem_gtt_init(struct drm_device *dev)
876{
877 struct drm_i915_private *dev_priv = dev->dev_private;
878 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800879 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800880
Ben Widawskybaa09f52013-01-24 13:49:57 -0800881 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -0700882 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700883 gtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -0800884 } else {
Ben Widawskyb2f21b42013-06-27 16:30:20 -0700885 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700886 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700887 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700888 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700889 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700890 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -0700891 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700892 gtt->base.pte_encode = byt_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -0700893 else
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700894 gtt->base.pte_encode = gen6_pte_encode;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800895 }
896
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700897 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -0700898 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -0800899 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -0800900 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800901
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700902 gtt->base.dev = dev;
903
Ben Widawskybaa09f52013-01-24 13:49:57 -0800904 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700905 DRM_INFO("Memory usable by graphics device = %zdM\n",
906 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -0700907 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
908 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -0800909
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800910 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +0200911}