blob: cb6401fe96370be862bc4aa1e3ce39629a1022f7 [file] [log] [blame]
Daniel Vetter76aaf222010-11-05 22:23:30 +01001/*
2 * Copyright © 2010 Daniel Vetter
Ben Widawskyc4ac5242014-02-19 22:05:47 -08003 * Copyright © 2011-2014 Intel Corporation
Daniel Vetter76aaf222010-11-05 22:23:30 +01004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
Daniel Vetter0e46ce22014-01-08 16:10:27 +010026#include <linux/seq_file.h>
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
28#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010029#include "i915_drv.h"
30#include "i915_trace.h"
31#include "intel_drv.h"
32
Ben Widawskya2319c02014-03-18 16:09:37 -070033static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv);
34
Daniel Vetter93a25a92014-03-06 09:40:43 +010035bool intel_enable_ppgtt(struct drm_device *dev, bool full)
36{
37 if (i915.enable_ppgtt == 0 || !HAS_ALIASING_PPGTT(dev))
38 return false;
39
40 if (i915.enable_ppgtt == 1 && full)
41 return false;
42
43#ifdef CONFIG_INTEL_IOMMU
44 /* Disable ppgtt on SNB if VT-d is on. */
45 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
46 DRM_INFO("Disabling PPGTT because VT-d is on\n");
47 return false;
48 }
49#endif
50
51 /* Full ppgtt disabled by default for now due to issues. */
52 if (full)
Ben Widawsky8d214b72014-03-24 18:06:00 -070053 return HAS_PPGTT(dev) && (i915.enable_ppgtt == 2);
Daniel Vetter93a25a92014-03-06 09:40:43 +010054 else
55 return HAS_ALIASING_PPGTT(dev);
56}
57
Ben Widawskyfbe5d362013-11-04 19:56:49 -080058
Ben Widawsky6f65e292013-12-06 14:10:56 -080059static void ppgtt_bind_vma(struct i915_vma *vma,
60 enum i915_cache_level cache_level,
61 u32 flags);
62static void ppgtt_unbind_vma(struct i915_vma *vma);
Ben Widawskyeeb94882013-12-06 14:11:10 -080063static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt);
Ben Widawsky6f65e292013-12-06 14:10:56 -080064
Ben Widawsky94ec8f62013-11-02 21:07:18 -070065static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
66 enum i915_cache_level level,
67 bool valid)
68{
69 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
70 pte |= addr;
Ben Widawskyfbe5d362013-11-04 19:56:49 -080071 if (level != I915_CACHE_NONE)
72 pte |= PPAT_CACHED_INDEX;
73 else
74 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky94ec8f62013-11-02 21:07:18 -070075 return pte;
76}
77
Ben Widawskyb1fe6672013-11-04 21:20:14 -080078static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
79 dma_addr_t addr,
80 enum i915_cache_level level)
81{
82 gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
83 pde |= addr;
84 if (level != I915_CACHE_NONE)
85 pde |= PPAT_CACHED_PDE_INDEX;
86 else
87 pde |= PPAT_UNCACHED_INDEX;
88 return pde;
89}
90
Chris Wilson350ec882013-08-06 13:17:02 +010091static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -070092 enum i915_cache_level level,
93 bool valid)
Ben Widawsky54d12522012-09-24 16:44:32 -070094{
Ben Widawskyb35b3802013-10-16 09:18:21 -070095 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -070096 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -070097
98 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +010099 case I915_CACHE_L3_LLC:
100 case I915_CACHE_LLC:
101 pte |= GEN6_PTE_CACHE_LLC;
102 break;
103 case I915_CACHE_NONE:
104 pte |= GEN6_PTE_UNCACHED;
105 break;
106 default:
107 WARN_ON(1);
108 }
109
110 return pte;
111}
112
113static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700114 enum i915_cache_level level,
115 bool valid)
Chris Wilson350ec882013-08-06 13:17:02 +0100116{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700117 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100118 pte |= GEN6_PTE_ADDR_ENCODE(addr);
119
120 switch (level) {
121 case I915_CACHE_L3_LLC:
122 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700123 break;
124 case I915_CACHE_LLC:
125 pte |= GEN6_PTE_CACHE_LLC;
126 break;
127 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700128 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700129 break;
130 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100131 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700132 }
133
Ben Widawsky54d12522012-09-24 16:44:32 -0700134 return pte;
135}
136
Ben Widawsky80a74f72013-06-27 16:30:19 -0700137static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700138 enum i915_cache_level level,
139 bool valid)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700140{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700141 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700142 pte |= GEN6_PTE_ADDR_ENCODE(addr);
143
144 /* Mark the page as writeable. Other platforms don't have a
145 * setting for read-only/writable, so this matches that behavior.
146 */
147 pte |= BYT_PTE_WRITEABLE;
148
149 if (level != I915_CACHE_NONE)
150 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
151
152 return pte;
153}
154
Ben Widawsky80a74f72013-06-27 16:30:19 -0700155static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700156 enum i915_cache_level level,
157 bool valid)
Kenneth Graunke91197082013-04-22 00:53:51 -0700158{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700159 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700160 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700161
162 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700163 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700164
165 return pte;
166}
167
Ben Widawsky4d15c142013-07-04 11:02:06 -0700168static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700169 enum i915_cache_level level,
170 bool valid)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700171{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700172 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700173 pte |= HSW_PTE_ADDR_ENCODE(addr);
174
Chris Wilson651d7942013-08-08 14:41:10 +0100175 switch (level) {
176 case I915_CACHE_NONE:
177 break;
178 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000179 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100180 break;
181 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000182 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100183 break;
184 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700185
186 return pte;
187}
188
Ben Widawsky94e409c2013-11-04 22:29:36 -0800189/* Broadwell Page Directory Pointer Descriptors */
190static int gen8_write_pdp(struct intel_ring_buffer *ring, unsigned entry,
Ben Widawskye178f702013-12-06 14:10:47 -0800191 uint64_t val, bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800192{
Ben Widawskye178f702013-12-06 14:10:47 -0800193 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800194 int ret;
195
196 BUG_ON(entry >= 4);
197
Ben Widawskye178f702013-12-06 14:10:47 -0800198 if (synchronous) {
199 I915_WRITE(GEN8_RING_PDP_UDW(ring, entry), val >> 32);
200 I915_WRITE(GEN8_RING_PDP_LDW(ring, entry), (u32)val);
201 return 0;
202 }
203
Ben Widawsky94e409c2013-11-04 22:29:36 -0800204 ret = intel_ring_begin(ring, 6);
205 if (ret)
206 return ret;
207
208 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
209 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
210 intel_ring_emit(ring, (u32)(val >> 32));
211 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
212 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
213 intel_ring_emit(ring, (u32)(val));
214 intel_ring_advance(ring);
215
216 return 0;
217}
218
Ben Widawskyeeb94882013-12-06 14:11:10 -0800219static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
220 struct intel_ring_buffer *ring,
221 bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800222{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800223 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800224
225 /* bit of a hack to find the actual last used pd */
226 int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
227
Ben Widawsky94e409c2013-11-04 22:29:36 -0800228 for (i = used_pd - 1; i >= 0; i--) {
229 dma_addr_t addr = ppgtt->pd_dma_addr[i];
Ben Widawskyeeb94882013-12-06 14:11:10 -0800230 ret = gen8_write_pdp(ring, i, addr, synchronous);
231 if (ret)
232 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800233 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800234
Ben Widawskyeeb94882013-12-06 14:11:10 -0800235 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800236}
237
Ben Widawsky459108b2013-11-02 21:07:23 -0700238static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800239 uint64_t start,
240 uint64_t length,
Ben Widawsky459108b2013-11-02 21:07:23 -0700241 bool use_scratch)
242{
243 struct i915_hw_ppgtt *ppgtt =
244 container_of(vm, struct i915_hw_ppgtt, base);
245 gen8_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800246 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
247 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
248 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800249 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700250 unsigned last_pte, i;
251
252 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
253 I915_CACHE_LLC, use_scratch);
254
255 while (num_entries) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800256 struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde];
Ben Widawsky459108b2013-11-02 21:07:23 -0700257
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800258 last_pte = pte + num_entries;
Ben Widawsky459108b2013-11-02 21:07:23 -0700259 if (last_pte > GEN8_PTES_PER_PAGE)
260 last_pte = GEN8_PTES_PER_PAGE;
261
262 pt_vaddr = kmap_atomic(page_table);
263
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800264 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700265 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800266 num_entries--;
267 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700268
269 kunmap_atomic(pt_vaddr);
270
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800271 pte = 0;
272 if (++pde == GEN8_PDES_PER_PAGE) {
273 pdpe++;
274 pde = 0;
275 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700276 }
277}
278
Ben Widawsky9df15b42013-11-02 21:07:24 -0700279static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
280 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800281 uint64_t start,
Ben Widawsky9df15b42013-11-02 21:07:24 -0700282 enum i915_cache_level cache_level)
283{
284 struct i915_hw_ppgtt *ppgtt =
285 container_of(vm, struct i915_hw_ppgtt, base);
286 gen8_gtt_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800287 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
288 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
289 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700290 struct sg_page_iter sg_iter;
291
Chris Wilson6f1cc992013-12-31 15:50:31 +0000292 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700293
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800294 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
295 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS))
296 break;
297
298 if (pt_vaddr == NULL)
299 pt_vaddr = kmap_atomic(ppgtt->gen8_pt_pages[pdpe][pde]);
300
301 pt_vaddr[pte] =
Chris Wilson6f1cc992013-12-31 15:50:31 +0000302 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
303 cache_level, true);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800304 if (++pte == GEN8_PTES_PER_PAGE) {
Ben Widawsky9df15b42013-11-02 21:07:24 -0700305 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000306 pt_vaddr = NULL;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800307 if (++pde == GEN8_PDES_PER_PAGE) {
308 pdpe++;
309 pde = 0;
310 }
311 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700312 }
313 }
Chris Wilson6f1cc992013-12-31 15:50:31 +0000314 if (pt_vaddr)
315 kunmap_atomic(pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700316}
317
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800318static void gen8_free_page_tables(struct page **pt_pages)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800319{
320 int i;
321
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800322 if (pt_pages == NULL)
323 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800324
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800325 for (i = 0; i < GEN8_PDES_PER_PAGE; i++)
326 if (pt_pages[i])
327 __free_pages(pt_pages[i], 0);
328}
329
330static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt)
331{
332 int i;
333
334 for (i = 0; i < ppgtt->num_pd_pages; i++) {
335 gen8_free_page_tables(ppgtt->gen8_pt_pages[i]);
336 kfree(ppgtt->gen8_pt_pages[i]);
337 kfree(ppgtt->gen8_pt_dma_addr[i]);
338 }
339
Ben Widawskyb45a6712014-02-12 14:28:44 -0800340 __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
341}
342
343static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
344{
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800345 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800346 int i, j;
347
348 for (i = 0; i < ppgtt->num_pd_pages; i++) {
349 /* TODO: In the future we'll support sparse mappings, so this
350 * will have to change. */
351 if (!ppgtt->pd_dma_addr[i])
352 continue;
353
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800354 pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
355 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800356
357 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
358 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
359 if (addr)
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800360 pci_unmap_page(hwdev, addr, PAGE_SIZE,
361 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800362 }
363 }
364}
365
Ben Widawsky37aca442013-11-04 20:47:32 -0800366static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
367{
368 struct i915_hw_ppgtt *ppgtt =
369 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800370
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800371 list_del(&vm->global_link);
Ben Widawsky686e1f62013-11-25 09:54:34 -0800372 drm_mm_takedown(&vm->mm);
373
Ben Widawskyb45a6712014-02-12 14:28:44 -0800374 gen8_ppgtt_unmap_pages(ppgtt);
375 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800376}
377
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800378static struct page **__gen8_alloc_page_tables(void)
379{
380 struct page **pt_pages;
381 int i;
382
383 pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL);
384 if (!pt_pages)
385 return ERR_PTR(-ENOMEM);
386
387 for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
388 pt_pages[i] = alloc_page(GFP_KERNEL);
389 if (!pt_pages[i])
390 goto bail;
391 }
392
393 return pt_pages;
394
395bail:
396 gen8_free_page_tables(pt_pages);
397 kfree(pt_pages);
398 return ERR_PTR(-ENOMEM);
399}
400
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800401static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
402 const int max_pdp)
403{
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800404 struct page **pt_pages[GEN8_LEGACY_PDPS];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800405 int i, ret;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800406
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800407 for (i = 0; i < max_pdp; i++) {
408 pt_pages[i] = __gen8_alloc_page_tables();
409 if (IS_ERR(pt_pages[i])) {
410 ret = PTR_ERR(pt_pages[i]);
411 goto unwind_out;
412 }
413 }
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800414
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800415 /* NB: Avoid touching gen8_pt_pages until last to keep the allocation,
416 * "atomic" - for cleanup purposes.
417 */
418 for (i = 0; i < max_pdp; i++)
419 ppgtt->gen8_pt_pages[i] = pt_pages[i];
420
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800421 return 0;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800422
423unwind_out:
424 while (i--) {
425 gen8_free_page_tables(pt_pages[i]);
426 kfree(pt_pages[i]);
427 }
428
429 return ret;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800430}
431
432static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
433{
434 int i;
435
436 for (i = 0; i < ppgtt->num_pd_pages; i++) {
437 ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
438 sizeof(dma_addr_t),
439 GFP_KERNEL);
440 if (!ppgtt->gen8_pt_dma_addr[i])
441 return -ENOMEM;
442 }
443
444 return 0;
445}
446
447static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
448 const int max_pdp)
449{
450 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
451 if (!ppgtt->pd_pages)
452 return -ENOMEM;
453
454 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
455 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
456
457 return 0;
458}
459
460static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
461 const int max_pdp)
462{
463 int ret;
464
465 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
466 if (ret)
467 return ret;
468
469 ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
470 if (ret) {
471 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
472 return ret;
473 }
474
475 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
476
477 ret = gen8_ppgtt_allocate_dma(ppgtt);
478 if (ret)
479 gen8_ppgtt_free(ppgtt);
480
481 return ret;
482}
483
484static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
485 const int pd)
486{
487 dma_addr_t pd_addr;
488 int ret;
489
490 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
491 &ppgtt->pd_pages[pd], 0,
492 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
493
494 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
495 if (ret)
496 return ret;
497
498 ppgtt->pd_dma_addr[pd] = pd_addr;
499
500 return 0;
501}
502
503static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
504 const int pd,
505 const int pt)
506{
507 dma_addr_t pt_addr;
508 struct page *p;
509 int ret;
510
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800511 p = ppgtt->gen8_pt_pages[pd][pt];
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800512 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
513 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
514 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
515 if (ret)
516 return ret;
517
518 ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
519
520 return 0;
521}
522
Ben Widawsky37aca442013-11-04 20:47:32 -0800523/**
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800524 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
525 * with a net effect resembling a 2-level page table in normal x86 terms. Each
526 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
527 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800528 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800529 * FIXME: split allocation into smaller pieces. For now we only ever do this
530 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
Ben Widawsky37aca442013-11-04 20:47:32 -0800531 * TODO: Do something with the size parameter
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800532 */
Ben Widawsky37aca442013-11-04 20:47:32 -0800533static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
534{
Ben Widawsky37aca442013-11-04 20:47:32 -0800535 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800536 const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800537 int i, j, ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800538
539 if (size % (1<<30))
540 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
541
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800542 /* 1. Do all our allocations for page directories and page tables. */
543 ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
544 if (ret)
545 return ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800546
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800547 /*
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800548 * 2. Create DMA mappings for the page directories and page tables.
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800549 */
550 for (i = 0; i < max_pdp; i++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800551 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800552 if (ret)
553 goto bail;
554
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800555 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800556 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800557 if (ret)
558 goto bail;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800559 }
560 }
561
562 /*
563 * 3. Map all the page directory entires to point to the page tables
564 * we've allocated.
565 *
566 * For now, the PPGTT helper functions all require that the PDEs are
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800567 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800568 * will never need to touch the PDEs again.
569 */
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800570 for (i = 0; i < max_pdp; i++) {
571 gen8_ppgtt_pde_t *pd_vaddr;
572 pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
573 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
574 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
575 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
576 I915_CACHE_LLC);
577 }
578 kunmap_atomic(pd_vaddr);
579 }
580
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800581 ppgtt->enable = gen8_ppgtt_enable;
582 ppgtt->switch_mm = gen8_mm_switch;
583 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
584 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
585 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
586 ppgtt->base.start = 0;
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800587 ppgtt->base.total = ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800588
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800589 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Ben Widawsky459108b2013-11-02 21:07:23 -0700590
Ben Widawsky37aca442013-11-04 20:47:32 -0800591 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
592 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
593 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800594 ppgtt->num_pd_entries,
595 (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700596 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800597
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800598bail:
599 gen8_ppgtt_unmap_pages(ppgtt);
600 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800601 return ret;
602}
603
Ben Widawsky87d60b62013-12-06 14:11:29 -0800604static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
605{
606 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
607 struct i915_address_space *vm = &ppgtt->base;
608 gen6_gtt_pte_t __iomem *pd_addr;
609 gen6_gtt_pte_t scratch_pte;
610 uint32_t pd_entry;
611 int pte, pde;
612
613 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
614
615 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
616 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
617
618 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
619 ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
620 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
621 u32 expected;
622 gen6_gtt_pte_t *pt_vaddr;
623 dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
624 pd_entry = readl(pd_addr + pde);
625 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
626
627 if (pd_entry != expected)
628 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
629 pde,
630 pd_entry,
631 expected);
632 seq_printf(m, "\tPDE: %x\n", pd_entry);
633
634 pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
635 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
636 unsigned long va =
637 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
638 (pte * PAGE_SIZE);
639 int i;
640 bool found = false;
641 for (i = 0; i < 4; i++)
642 if (pt_vaddr[pte + i] != scratch_pte)
643 found = true;
644 if (!found)
645 continue;
646
647 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
648 for (i = 0; i < 4; i++) {
649 if (pt_vaddr[pte + i] != scratch_pte)
650 seq_printf(m, " %08x", pt_vaddr[pte + i]);
651 else
652 seq_puts(m, " SCRATCH ");
653 }
654 seq_puts(m, "\n");
655 }
656 kunmap_atomic(pt_vaddr);
657 }
658}
659
Ben Widawsky3e302542013-04-23 23:15:32 -0700660static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700661{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700662 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700663 gen6_gtt_pte_t __iomem *pd_addr;
664 uint32_t pd_entry;
665 int i;
666
Ben Widawsky0a732872013-04-23 23:15:30 -0700667 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700668 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
669 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
670 for (i = 0; i < ppgtt->num_pd_entries; i++) {
671 dma_addr_t pt_addr;
672
673 pt_addr = ppgtt->pt_dma_addr[i];
674 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
675 pd_entry |= GEN6_PDE_VALID;
676
677 writel(pd_entry, pd_addr + i);
678 }
679 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700680}
681
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800682static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700683{
Ben Widawsky3e302542013-04-23 23:15:32 -0700684 BUG_ON(ppgtt->pd_offset & 0x3f);
685
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800686 return (ppgtt->pd_offset / 64) << 16;
687}
Ben Widawsky61973492013-04-08 18:43:54 -0700688
Ben Widawsky90252e52013-12-06 14:11:12 -0800689static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
690 struct intel_ring_buffer *ring,
691 bool synchronous)
692{
693 struct drm_device *dev = ppgtt->base.dev;
694 struct drm_i915_private *dev_priv = dev->dev_private;
695 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700696
Ben Widawsky90252e52013-12-06 14:11:12 -0800697 /* If we're in reset, we can assume the GPU is sufficiently idle to
698 * manually frob these bits. Ideally we could use the ring functions,
699 * except our error handling makes it quite difficult (can't use
700 * intel_ring_begin, ring->flush, or intel_ring_advance)
701 *
702 * FIXME: We should try not to special case reset
703 */
704 if (synchronous ||
705 i915_reset_in_progress(&dev_priv->gpu_error)) {
706 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
707 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
708 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
709 POSTING_READ(RING_PP_DIR_BASE(ring));
710 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700711 }
712
Ben Widawsky90252e52013-12-06 14:11:12 -0800713 /* NB: TLBs must be flushed and invalidated before a switch */
714 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
715 if (ret)
716 return ret;
717
718 ret = intel_ring_begin(ring, 6);
719 if (ret)
720 return ret;
721
722 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
723 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
724 intel_ring_emit(ring, PP_DIR_DCLV_2G);
725 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
726 intel_ring_emit(ring, get_pd_offset(ppgtt));
727 intel_ring_emit(ring, MI_NOOP);
728 intel_ring_advance(ring);
729
730 return 0;
731}
732
Ben Widawsky48a10382013-12-06 14:11:11 -0800733static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
734 struct intel_ring_buffer *ring,
735 bool synchronous)
736{
737 struct drm_device *dev = ppgtt->base.dev;
738 struct drm_i915_private *dev_priv = dev->dev_private;
739 int ret;
740
741 /* If we're in reset, we can assume the GPU is sufficiently idle to
742 * manually frob these bits. Ideally we could use the ring functions,
743 * except our error handling makes it quite difficult (can't use
744 * intel_ring_begin, ring->flush, or intel_ring_advance)
745 *
746 * FIXME: We should try not to special case reset
747 */
748 if (synchronous ||
749 i915_reset_in_progress(&dev_priv->gpu_error)) {
750 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
751 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
752 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
753 POSTING_READ(RING_PP_DIR_BASE(ring));
754 return 0;
755 }
756
757 /* NB: TLBs must be flushed and invalidated before a switch */
758 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
759 if (ret)
760 return ret;
761
762 ret = intel_ring_begin(ring, 6);
763 if (ret)
764 return ret;
765
766 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
767 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
768 intel_ring_emit(ring, PP_DIR_DCLV_2G);
769 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
770 intel_ring_emit(ring, get_pd_offset(ppgtt));
771 intel_ring_emit(ring, MI_NOOP);
772 intel_ring_advance(ring);
773
Ben Widawsky90252e52013-12-06 14:11:12 -0800774 /* XXX: RCS is the only one to auto invalidate the TLBs? */
775 if (ring->id != RCS) {
776 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
777 if (ret)
778 return ret;
779 }
780
Ben Widawsky48a10382013-12-06 14:11:11 -0800781 return 0;
782}
783
Ben Widawskyeeb94882013-12-06 14:11:10 -0800784static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
785 struct intel_ring_buffer *ring,
786 bool synchronous)
787{
788 struct drm_device *dev = ppgtt->base.dev;
789 struct drm_i915_private *dev_priv = dev->dev_private;
790
Ben Widawsky48a10382013-12-06 14:11:11 -0800791 if (!synchronous)
792 return 0;
793
Ben Widawskyeeb94882013-12-06 14:11:10 -0800794 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
795 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
796
797 POSTING_READ(RING_PP_DIR_DCLV(ring));
798
799 return 0;
800}
801
802static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
803{
804 struct drm_device *dev = ppgtt->base.dev;
805 struct drm_i915_private *dev_priv = dev->dev_private;
806 struct intel_ring_buffer *ring;
807 int j, ret;
808
809 for_each_ring(ring, dev_priv, j) {
810 I915_WRITE(RING_MODE_GEN7(ring),
811 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800812
813 /* We promise to do a switch later with FULL PPGTT. If this is
814 * aliasing, this is the one and only switch we'll do */
815 if (USES_FULL_PPGTT(dev))
816 continue;
817
Ben Widawskyeeb94882013-12-06 14:11:10 -0800818 ret = ppgtt->switch_mm(ppgtt, ring, true);
819 if (ret)
820 goto err_out;
821 }
822
823 return 0;
824
825err_out:
826 for_each_ring(ring, dev_priv, j)
827 I915_WRITE(RING_MODE_GEN7(ring),
828 _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
829 return ret;
830}
831
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800832static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
833{
834 struct drm_device *dev = ppgtt->base.dev;
Jani Nikula50227e12014-03-31 14:27:21 +0300835 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800836 struct intel_ring_buffer *ring;
837 uint32_t ecochk, ecobits;
838 int i;
839
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800840 ecobits = I915_READ(GAC_ECO_BITS);
841 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
842
843 ecochk = I915_READ(GAM_ECOCHK);
844 if (IS_HASWELL(dev)) {
845 ecochk |= ECOCHK_PPGTT_WB_HSW;
846 } else {
847 ecochk |= ECOCHK_PPGTT_LLC_IVB;
848 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
849 }
850 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800851
Ben Widawsky61973492013-04-08 18:43:54 -0700852 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800853 int ret;
854 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800855 I915_WRITE(RING_MODE_GEN7(ring),
856 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700857
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800858 /* We promise to do a switch later with FULL PPGTT. If this is
859 * aliasing, this is the one and only switch we'll do */
860 if (USES_FULL_PPGTT(dev))
861 continue;
862
Ben Widawskyeeb94882013-12-06 14:11:10 -0800863 ret = ppgtt->switch_mm(ppgtt, ring, true);
864 if (ret)
865 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700866 }
Ben Widawskyd2ff7192013-12-06 14:11:27 -0800867
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800868 return 0;
869}
870
Ben Widawskya3d67d22013-12-06 14:11:06 -0800871static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700872{
Ben Widawskya3d67d22013-12-06 14:11:06 -0800873 struct drm_device *dev = ppgtt->base.dev;
Jani Nikula50227e12014-03-31 14:27:21 +0300874 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700875 struct intel_ring_buffer *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800876 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -0700877 int i;
878
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800879 ecobits = I915_READ(GAC_ECO_BITS);
880 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
881 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700882
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800883 gab_ctl = I915_READ(GAB_CTL);
884 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700885
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800886 ecochk = I915_READ(GAM_ECOCHK);
887 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700888
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800889 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700890
891 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800892 int ret = ppgtt->switch_mm(ppgtt, ring, true);
893 if (ret)
894 return ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700895 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800896
Ben Widawskyb7c36d22013-04-08 18:43:56 -0700897 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700898}
899
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100900/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700901static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800902 uint64_t start,
903 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -0700904 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100905{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700906 struct i915_hw_ppgtt *ppgtt =
907 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700908 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -0800909 unsigned first_entry = start >> PAGE_SHIFT;
910 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100911 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100912 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
913 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100914
Ben Widawskyb35b3802013-10-16 09:18:21 -0700915 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100916
Daniel Vetter7bddb012012-02-09 17:15:47 +0100917 while (num_entries) {
918 last_pte = first_pte + num_entries;
919 if (last_pte > I915_PPGTT_PT_ENTRIES)
920 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100921
Daniel Vettera15326a2013-03-19 23:48:39 +0100922 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100923
924 for (i = first_pte; i < last_pte; i++)
925 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100926
927 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100928
Daniel Vetter7bddb012012-02-09 17:15:47 +0100929 num_entries -= last_pte - first_pte;
930 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100931 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100932 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100933}
934
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700935static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800936 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800937 uint64_t start,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800938 enum i915_cache_level cache_level)
939{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700940 struct i915_hw_ppgtt *ppgtt =
941 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700942 gen6_gtt_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -0800943 unsigned first_entry = start >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100944 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200945 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
946 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800947
Chris Wilsoncc797142013-12-31 15:50:30 +0000948 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +0200949 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +0000950 if (pt_vaddr == NULL)
951 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800952
Chris Wilsoncc797142013-12-31 15:50:30 +0000953 pt_vaddr[act_pte] =
954 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
955 cache_level, true);
Imre Deak6e995e22013-02-18 19:28:04 +0200956 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
957 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +0000958 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +0100959 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +0200960 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800961 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800962 }
Chris Wilsoncc797142013-12-31 15:50:30 +0000963 if (pt_vaddr)
964 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800965}
966
Ben Widawskya00d8252014-02-19 22:05:48 -0800967static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100968{
Daniel Vetter3440d262013-01-24 13:49:56 -0800969 int i;
970
971 if (ppgtt->pt_dma_addr) {
972 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700973 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800974 ppgtt->pt_dma_addr[i],
975 4096, PCI_DMA_BIDIRECTIONAL);
976 }
Ben Widawskya00d8252014-02-19 22:05:48 -0800977}
978
979static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
980{
981 int i;
Daniel Vetter3440d262013-01-24 13:49:56 -0800982
983 kfree(ppgtt->pt_dma_addr);
984 for (i = 0; i < ppgtt->num_pd_entries; i++)
985 __free_page(ppgtt->pt_pages[i]);
986 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800987}
988
Ben Widawskya00d8252014-02-19 22:05:48 -0800989static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
990{
991 struct i915_hw_ppgtt *ppgtt =
992 container_of(vm, struct i915_hw_ppgtt, base);
993
994 list_del(&vm->global_link);
995 drm_mm_takedown(&ppgtt->base.mm);
996 drm_mm_remove_node(&ppgtt->node);
997
998 gen6_ppgtt_unmap_pages(ppgtt);
999 gen6_ppgtt_free(ppgtt);
1000}
1001
Ben Widawskyb1465202014-02-19 22:05:49 -08001002static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001003{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001004 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001005 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001006 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001007 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001008
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001009 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1010 * allocator works in address space sizes, so it's multiplied by page
1011 * size. We allocate at the top of the GTT to avoid fragmentation.
1012 */
1013 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -08001014alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001015 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1016 &ppgtt->node, GEN6_PD_SIZE,
1017 GEN6_PD_ALIGN, 0,
1018 0, dev_priv->gtt.base.total,
1019 DRM_MM_SEARCH_DEFAULT);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001020 if (ret == -ENOSPC && !retried) {
1021 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1022 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Daniel Vetterd47c3ea2014-02-14 14:01:18 +01001023 I915_CACHE_NONE, 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001024 if (ret)
1025 return ret;
1026
1027 retried = true;
1028 goto alloc;
1029 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001030
1031 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1032 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001033
Ben Widawsky6670a5a2013-06-27 16:30:04 -07001034 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawskyb1465202014-02-19 22:05:49 -08001035 return ret;
1036}
1037
1038static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
1039{
1040 int i;
1041
1042 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
1043 GFP_KERNEL);
1044
1045 if (!ppgtt->pt_pages)
1046 return -ENOMEM;
1047
1048 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1049 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
1050 if (!ppgtt->pt_pages[i]) {
1051 gen6_ppgtt_free(ppgtt);
1052 return -ENOMEM;
1053 }
1054 }
1055
1056 return 0;
1057}
1058
1059static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1060{
1061 int ret;
1062
1063 ret = gen6_ppgtt_allocate_page_directories(ppgtt);
1064 if (ret)
1065 return ret;
1066
1067 ret = gen6_ppgtt_allocate_page_tables(ppgtt);
1068 if (ret) {
1069 drm_mm_remove_node(&ppgtt->node);
1070 return ret;
1071 }
1072
1073 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
1074 GFP_KERNEL);
1075 if (!ppgtt->pt_dma_addr) {
1076 drm_mm_remove_node(&ppgtt->node);
1077 gen6_ppgtt_free(ppgtt);
1078 return -ENOMEM;
1079 }
1080
1081 return 0;
1082}
1083
1084static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
1085{
1086 struct drm_device *dev = ppgtt->base.dev;
1087 int i;
1088
1089 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1090 dma_addr_t pt_addr;
1091
1092 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
1093 PCI_DMA_BIDIRECTIONAL);
1094
1095 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1096 gen6_ppgtt_unmap_pages(ppgtt);
1097 return -EIO;
1098 }
1099
1100 ppgtt->pt_dma_addr[i] = pt_addr;
1101 }
1102
1103 return 0;
1104}
1105
1106static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1107{
1108 struct drm_device *dev = ppgtt->base.dev;
1109 struct drm_i915_private *dev_priv = dev->dev_private;
1110 int ret;
1111
1112 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001113 if (IS_GEN6(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001114 ppgtt->enable = gen6_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -08001115 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001116 } else if (IS_HASWELL(dev)) {
1117 ppgtt->enable = gen7_ppgtt_enable;
1118 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001119 } else if (IS_GEN7(dev)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001120 ppgtt->enable = gen7_ppgtt_enable;
Ben Widawsky48a10382013-12-06 14:11:11 -08001121 ppgtt->switch_mm = gen7_mm_switch;
1122 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001123 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001124
1125 ret = gen6_ppgtt_alloc(ppgtt);
1126 if (ret)
1127 return ret;
1128
1129 ret = gen6_ppgtt_setup_page_tables(ppgtt);
1130 if (ret) {
1131 gen6_ppgtt_free(ppgtt);
1132 return ret;
1133 }
1134
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001135 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1136 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1137 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001138 ppgtt->base.start = 0;
Ben Widawsky5a6c93f2014-03-08 11:58:17 -08001139 ppgtt->base.total = ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001140 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001141
Ben Widawskyb1465202014-02-19 22:05:49 -08001142 ppgtt->pd_offset =
1143 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001144
Ben Widawsky782f1492014-02-20 11:50:33 -08001145 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001146
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001147 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1148 ppgtt->node.size >> 20,
1149 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001150
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001151 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001152}
1153
Ben Widawsky246cbfb2013-12-06 14:11:14 -08001154int i915_gem_init_ppgtt(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001155{
1156 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd6660ad2013-12-06 14:11:13 -08001157 int ret = 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001158
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001159 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001160 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001161
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001162 if (INTEL_INFO(dev)->gen < 8)
1163 ret = gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -07001164 else if (IS_GEN8(dev))
Ben Widawsky37aca442013-11-04 20:47:32 -08001165 ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001166 else
1167 BUG();
1168
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001169 if (!ret) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001170 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001171 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001172 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1173 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001174 i915_init_vm(dev_priv, &ppgtt->base);
1175 if (INTEL_INFO(dev)->gen < 8) {
Ben Widawsky9f273d42013-12-06 14:11:16 -08001176 gen6_write_pdes(ppgtt);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001177 DRM_DEBUG("Adding PPGTT at offset %x\n",
1178 ppgtt->pd_offset << 10);
1179 }
Ben Widawsky93bd8642013-07-16 16:50:06 -07001180 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001181
1182 return ret;
1183}
1184
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001185static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001186ppgtt_bind_vma(struct i915_vma *vma,
1187 enum i915_cache_level cache_level,
1188 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001189{
Ben Widawsky782f1492014-02-20 11:50:33 -08001190 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
1191 cache_level);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001192}
1193
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001194static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001195{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001196 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001197 vma->node.start,
1198 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001199 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001200}
1201
Ben Widawskya81cc002013-01-18 12:30:31 -08001202extern int intel_iommu_gfx_mapped;
1203/* Certain Gen5 chipsets require require idling the GPU before
1204 * unmapping anything from the GTT when VT-d is enabled.
1205 */
1206static inline bool needs_idle_maps(struct drm_device *dev)
1207{
1208#ifdef CONFIG_INTEL_IOMMU
1209 /* Query intel_iommu to see if we need the workaround. Presumably that
1210 * was loaded first.
1211 */
1212 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1213 return true;
1214#endif
1215 return false;
1216}
1217
Ben Widawsky5c042282011-10-17 15:51:55 -07001218static bool do_idling(struct drm_i915_private *dev_priv)
1219{
1220 bool ret = dev_priv->mm.interruptible;
1221
Ben Widawskya81cc002013-01-18 12:30:31 -08001222 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001223 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001224 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001225 DRM_ERROR("Couldn't idle GPU\n");
1226 /* Wait a bit, in hopes it avoids the hang */
1227 udelay(10);
1228 }
1229 }
1230
1231 return ret;
1232}
1233
1234static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1235{
Ben Widawskya81cc002013-01-18 12:30:31 -08001236 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001237 dev_priv->mm.interruptible = interruptible;
1238}
1239
Ben Widawsky828c7902013-10-16 09:21:30 -07001240void i915_check_and_clear_faults(struct drm_device *dev)
1241{
1242 struct drm_i915_private *dev_priv = dev->dev_private;
1243 struct intel_ring_buffer *ring;
1244 int i;
1245
1246 if (INTEL_INFO(dev)->gen < 6)
1247 return;
1248
1249 for_each_ring(ring, dev_priv, i) {
1250 u32 fault_reg;
1251 fault_reg = I915_READ(RING_FAULT_REG(ring));
1252 if (fault_reg & RING_FAULT_VALID) {
1253 DRM_DEBUG_DRIVER("Unexpected fault\n"
1254 "\tAddr: 0x%08lx\\n"
1255 "\tAddress space: %s\n"
1256 "\tSource ID: %d\n"
1257 "\tType: %d\n",
1258 fault_reg & PAGE_MASK,
1259 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1260 RING_FAULT_SRCID(fault_reg),
1261 RING_FAULT_FAULT_TYPE(fault_reg));
1262 I915_WRITE(RING_FAULT_REG(ring),
1263 fault_reg & ~RING_FAULT_VALID);
1264 }
1265 }
1266 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1267}
1268
1269void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1270{
1271 struct drm_i915_private *dev_priv = dev->dev_private;
1272
1273 /* Don't bother messing with faults pre GEN6 as we have little
1274 * documentation supporting that it's a good idea.
1275 */
1276 if (INTEL_INFO(dev)->gen < 6)
1277 return;
1278
1279 i915_check_and_clear_faults(dev);
1280
1281 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001282 dev_priv->gtt.base.start,
1283 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001284 true);
Ben Widawsky828c7902013-10-16 09:21:30 -07001285}
1286
Daniel Vetter76aaf222010-11-05 22:23:30 +01001287void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1288{
1289 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001290 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001291 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001292
Ben Widawsky828c7902013-10-16 09:21:30 -07001293 i915_check_and_clear_faults(dev);
1294
Chris Wilsonbee4a182011-01-21 10:54:32 +00001295 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001296 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001297 dev_priv->gtt.base.start,
1298 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001299 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001300
Ben Widawsky35c20a62013-05-31 11:28:48 -07001301 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001302 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1303 &dev_priv->gtt.base);
1304 if (!vma)
1305 continue;
1306
Chris Wilson2c225692013-08-09 12:26:45 +01001307 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001308 /* The bind_vma code tries to be smart about tracking mappings.
1309 * Unfortunately above, we've just wiped out the mappings
1310 * without telling our object about it. So we need to fake it.
1311 */
1312 obj->has_global_gtt_mapping = 0;
1313 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001314 }
1315
Ben Widawsky80da2162013-12-06 14:11:17 -08001316
Ben Widawskya2319c02014-03-18 16:09:37 -07001317 if (INTEL_INFO(dev)->gen >= 8) {
1318 gen8_setup_private_ppat(dev_priv);
Ben Widawsky80da2162013-12-06 14:11:17 -08001319 return;
Ben Widawskya2319c02014-03-18 16:09:37 -07001320 }
Ben Widawsky80da2162013-12-06 14:11:17 -08001321
1322 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1323 /* TODO: Perhaps it shouldn't be gen6 specific */
1324 if (i915_is_ggtt(vm)) {
1325 if (dev_priv->mm.aliasing_ppgtt)
1326 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1327 continue;
1328 }
1329
1330 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001331 }
1332
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001333 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001334}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001335
Daniel Vetter74163902012-02-15 23:50:21 +01001336int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001337{
Chris Wilson9da3da62012-06-01 15:20:22 +01001338 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001339 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001340
1341 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1342 obj->pages->sgl, obj->pages->nents,
1343 PCI_DMA_BIDIRECTIONAL))
1344 return -ENOSPC;
1345
1346 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001347}
1348
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001349static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1350{
1351#ifdef writeq
1352 writeq(pte, addr);
1353#else
1354 iowrite32((u32)pte, addr);
1355 iowrite32(pte >> 32, addr + 4);
1356#endif
1357}
1358
1359static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1360 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001361 uint64_t start,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001362 enum i915_cache_level level)
1363{
1364 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001365 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001366 gen8_gtt_pte_t __iomem *gtt_entries =
1367 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1368 int i = 0;
1369 struct sg_page_iter sg_iter;
1370 dma_addr_t addr;
1371
1372 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1373 addr = sg_dma_address(sg_iter.sg) +
1374 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1375 gen8_set_pte(&gtt_entries[i],
1376 gen8_pte_encode(addr, level, true));
1377 i++;
1378 }
1379
1380 /*
1381 * XXX: This serves as a posting read to make sure that the PTE has
1382 * actually been updated. There is some concern that even though
1383 * registers and PTEs are within the same BAR that they are potentially
1384 * of NUMA access patterns. Therefore, even with the way we assume
1385 * hardware should work, we must keep this posting read for paranoia.
1386 */
1387 if (i != 0)
1388 WARN_ON(readq(&gtt_entries[i-1])
1389 != gen8_pte_encode(addr, level, true));
1390
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001391 /* This next bit makes the above posting read even more important. We
1392 * want to flush the TLBs only after we're certain all the PTE updates
1393 * have finished.
1394 */
1395 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1396 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001397}
1398
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001399/*
1400 * Binds an object into the global gtt with the specified cache level. The object
1401 * will be accessible to the GPU via commands whose operands reference offsets
1402 * within the global GTT as well as accessible by the GPU through the GMADR
1403 * mapped BAR (dev_priv->mm.gtt->gtt).
1404 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001405static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001406 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001407 uint64_t start,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001408 enum i915_cache_level level)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001409{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001410 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001411 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001412 gen6_gtt_pte_t __iomem *gtt_entries =
1413 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001414 int i = 0;
1415 struct sg_page_iter sg_iter;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001416 dma_addr_t addr;
1417
Imre Deak6e995e22013-02-18 19:28:04 +02001418 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001419 addr = sg_page_iter_dma_address(&sg_iter);
Ben Widawskyb35b3802013-10-16 09:18:21 -07001420 iowrite32(vm->pte_encode(addr, level, true), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001421 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001422 }
1423
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001424 /* XXX: This serves as a posting read to make sure that the PTE has
1425 * actually been updated. There is some concern that even though
1426 * registers and PTEs are within the same BAR that they are potentially
1427 * of NUMA access patterns. Therefore, even with the way we assume
1428 * hardware should work, we must keep this posting read for paranoia.
1429 */
1430 if (i != 0)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001431 WARN_ON(readl(&gtt_entries[i-1]) !=
Ben Widawskyb35b3802013-10-16 09:18:21 -07001432 vm->pte_encode(addr, level, true));
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001433
1434 /* This next bit makes the above posting read even more important. We
1435 * want to flush the TLBs only after we're certain all the PTE updates
1436 * have finished.
1437 */
1438 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1439 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001440}
1441
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001442static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001443 uint64_t start,
1444 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001445 bool use_scratch)
1446{
1447 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001448 unsigned first_entry = start >> PAGE_SHIFT;
1449 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001450 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1451 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1452 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1453 int i;
1454
1455 if (WARN(num_entries > max_entries,
1456 "First entry = %d; Num entries = %d (max=%d)\n",
1457 first_entry, num_entries, max_entries))
1458 num_entries = max_entries;
1459
1460 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1461 I915_CACHE_LLC,
1462 use_scratch);
1463 for (i = 0; i < num_entries; i++)
1464 gen8_set_pte(&gtt_base[i], scratch_pte);
1465 readl(gtt_base);
1466}
1467
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001468static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001469 uint64_t start,
1470 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001471 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001472{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001473 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001474 unsigned first_entry = start >> PAGE_SHIFT;
1475 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001476 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1477 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001478 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001479 int i;
1480
1481 if (WARN(num_entries > max_entries,
1482 "First entry = %d; Num entries = %d (max=%d)\n",
1483 first_entry, num_entries, max_entries))
1484 num_entries = max_entries;
1485
Ben Widawsky828c7902013-10-16 09:21:30 -07001486 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
1487
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001488 for (i = 0; i < num_entries; i++)
1489 iowrite32(scratch_pte, &gtt_base[i]);
1490 readl(gtt_base);
1491}
1492
Ben Widawsky6f65e292013-12-06 14:10:56 -08001493
1494static void i915_ggtt_bind_vma(struct i915_vma *vma,
1495 enum i915_cache_level cache_level,
1496 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001497{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001498 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001499 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1500 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1501
Ben Widawsky6f65e292013-12-06 14:10:56 -08001502 BUG_ON(!i915_is_ggtt(vma->vm));
1503 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1504 vma->obj->has_global_gtt_mapping = 1;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001505}
1506
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001507static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001508 uint64_t start,
1509 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001510 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001511{
Ben Widawsky782f1492014-02-20 11:50:33 -08001512 unsigned first_entry = start >> PAGE_SHIFT;
1513 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001514 intel_gtt_clear_range(first_entry, num_entries);
1515}
1516
Ben Widawsky6f65e292013-12-06 14:10:56 -08001517static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001518{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001519 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1520 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001521
Ben Widawsky6f65e292013-12-06 14:10:56 -08001522 BUG_ON(!i915_is_ggtt(vma->vm));
1523 vma->obj->has_global_gtt_mapping = 0;
1524 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001525}
1526
Ben Widawsky6f65e292013-12-06 14:10:56 -08001527static void ggtt_bind_vma(struct i915_vma *vma,
1528 enum i915_cache_level cache_level,
1529 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001530{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001531 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001532 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001533 struct drm_i915_gem_object *obj = vma->obj;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001534
Ben Widawsky6f65e292013-12-06 14:10:56 -08001535 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1536 * or we have a global mapping already but the cacheability flags have
1537 * changed, set the global PTEs.
1538 *
1539 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1540 * instead if none of the above hold true.
1541 *
1542 * NB: A global mapping should only be needed for special regions like
1543 * "gtt mappable", SNB errata, or if specified via special execbuf
1544 * flags. At all other times, the GPU will use the aliasing PPGTT.
1545 */
1546 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1547 if (!obj->has_global_gtt_mapping ||
1548 (cache_level != obj->cache_level)) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001549 vma->vm->insert_entries(vma->vm, obj->pages,
1550 vma->node.start,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001551 cache_level);
1552 obj->has_global_gtt_mapping = 1;
1553 }
1554 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001555
Ben Widawsky6f65e292013-12-06 14:10:56 -08001556 if (dev_priv->mm.aliasing_ppgtt &&
1557 (!obj->has_aliasing_ppgtt_mapping ||
1558 (cache_level != obj->cache_level))) {
1559 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1560 appgtt->base.insert_entries(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001561 vma->obj->pages,
1562 vma->node.start,
1563 cache_level);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001564 vma->obj->has_aliasing_ppgtt_mapping = 1;
1565 }
1566}
1567
1568static void ggtt_unbind_vma(struct i915_vma *vma)
1569{
1570 struct drm_device *dev = vma->vm->dev;
1571 struct drm_i915_private *dev_priv = dev->dev_private;
1572 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001573
1574 if (obj->has_global_gtt_mapping) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001575 vma->vm->clear_range(vma->vm,
1576 vma->node.start,
1577 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001578 true);
1579 obj->has_global_gtt_mapping = 0;
1580 }
1581
1582 if (obj->has_aliasing_ppgtt_mapping) {
1583 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1584 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001585 vma->node.start,
1586 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001587 true);
1588 obj->has_aliasing_ppgtt_mapping = 0;
1589 }
Daniel Vetter74163902012-02-15 23:50:21 +01001590}
1591
1592void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1593{
Ben Widawsky5c042282011-10-17 15:51:55 -07001594 struct drm_device *dev = obj->base.dev;
1595 struct drm_i915_private *dev_priv = dev->dev_private;
1596 bool interruptible;
1597
1598 interruptible = do_idling(dev_priv);
1599
Chris Wilson9da3da62012-06-01 15:20:22 +01001600 if (!obj->has_dma_mapping)
1601 dma_unmap_sg(&dev->pdev->dev,
1602 obj->pages->sgl, obj->pages->nents,
1603 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001604
1605 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001606}
Daniel Vetter644ec022012-03-26 09:45:40 +02001607
Chris Wilson42d6ab42012-07-26 11:49:32 +01001608static void i915_gtt_color_adjust(struct drm_mm_node *node,
1609 unsigned long color,
1610 unsigned long *start,
1611 unsigned long *end)
1612{
1613 if (node->color != color)
1614 *start += 4096;
1615
1616 if (!list_empty(&node->node_list)) {
1617 node = list_entry(node->node_list.next,
1618 struct drm_mm_node,
1619 node_list);
1620 if (node->allocated && node->color != color)
1621 *end -= 4096;
1622 }
1623}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001624
Ben Widawskyd7e50082012-12-18 10:31:25 -08001625void i915_gem_setup_global_gtt(struct drm_device *dev,
1626 unsigned long start,
1627 unsigned long mappable_end,
1628 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001629{
Ben Widawskye78891c2013-01-25 16:41:04 -08001630 /* Let GEM Manage all of the aperture.
1631 *
1632 * However, leave one page at the end still bound to the scratch page.
1633 * There are a number of places where the hardware apparently prefetches
1634 * past the end of the object, and we've seen multiple hangs with the
1635 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1636 * aperture. One page should be enough to keep any prefetching inside
1637 * of the aperture.
1638 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001639 struct drm_i915_private *dev_priv = dev->dev_private;
1640 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001641 struct drm_mm_node *entry;
1642 struct drm_i915_gem_object *obj;
1643 unsigned long hole_start, hole_end;
Daniel Vetter644ec022012-03-26 09:45:40 +02001644
Ben Widawsky35451cb2013-01-17 12:45:13 -08001645 BUG_ON(mappable_end > end);
1646
Chris Wilsoned2f3452012-11-15 11:32:19 +00001647 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001648 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001649 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001650 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001651
Chris Wilsoned2f3452012-11-15 11:32:19 +00001652 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001653 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001654 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001655 int ret;
Ben Widawskyedd41a82013-07-05 14:41:05 -07001656 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001657 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001658
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001659 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001660 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001661 if (ret)
Ben Widawskyb3a070c2013-07-05 14:41:02 -07001662 DRM_DEBUG_KMS("Reservation failed\n");
Chris Wilsoned2f3452012-11-15 11:32:19 +00001663 obj->has_global_gtt_mapping = 1;
1664 }
1665
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001666 dev_priv->gtt.base.start = start;
1667 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001668
Chris Wilsoned2f3452012-11-15 11:32:19 +00001669 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001670 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00001671 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1672 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08001673 ggtt_vm->clear_range(ggtt_vm, hole_start,
1674 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001675 }
1676
1677 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08001678 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001679}
1680
Ben Widawskyd7e50082012-12-18 10:31:25 -08001681void i915_gem_init_global_gtt(struct drm_device *dev)
1682{
1683 struct drm_i915_private *dev_priv = dev->dev_private;
1684 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001685
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001686 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001687 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001688
Ben Widawskye78891c2013-01-25 16:41:04 -08001689 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001690}
1691
1692static int setup_scratch_page(struct drm_device *dev)
1693{
1694 struct drm_i915_private *dev_priv = dev->dev_private;
1695 struct page *page;
1696 dma_addr_t dma_addr;
1697
1698 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1699 if (page == NULL)
1700 return -ENOMEM;
1701 get_page(page);
1702 set_pages_uc(page, 1);
1703
1704#ifdef CONFIG_INTEL_IOMMU
1705 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1706 PCI_DMA_BIDIRECTIONAL);
1707 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1708 return -EINVAL;
1709#else
1710 dma_addr = page_to_phys(page);
1711#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001712 dev_priv->gtt.base.scratch.page = page;
1713 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001714
1715 return 0;
1716}
1717
1718static void teardown_scratch_page(struct drm_device *dev)
1719{
1720 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001721 struct page *page = dev_priv->gtt.base.scratch.page;
1722
1723 set_pages_wb(page, 1);
1724 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001725 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001726 put_page(page);
1727 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001728}
1729
1730static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1731{
1732 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1733 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1734 return snb_gmch_ctl << 20;
1735}
1736
Ben Widawsky9459d252013-11-03 16:53:55 -08001737static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1738{
1739 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1740 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1741 if (bdw_gmch_ctl)
1742 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
1743 return bdw_gmch_ctl << 20;
1744}
1745
Ben Widawskybaa09f52013-01-24 13:49:57 -08001746static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001747{
1748 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1749 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1750 return snb_gmch_ctl << 25; /* 32 MB units */
1751}
1752
Ben Widawsky9459d252013-11-03 16:53:55 -08001753static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1754{
1755 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1756 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1757 return bdw_gmch_ctl << 25; /* 32 MB units */
1758}
1759
Ben Widawsky63340132013-11-04 19:32:22 -08001760static int ggtt_probe_common(struct drm_device *dev,
1761 size_t gtt_size)
1762{
1763 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001764 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08001765 int ret;
1766
1767 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001768 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08001769 (pci_resource_len(dev->pdev, 0) / 2);
1770
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001771 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08001772 if (!dev_priv->gtt.gsm) {
1773 DRM_ERROR("Failed to map the gtt page table\n");
1774 return -ENOMEM;
1775 }
1776
1777 ret = setup_scratch_page(dev);
1778 if (ret) {
1779 DRM_ERROR("Scratch setup failed\n");
1780 /* iounmap will also get called at remove, but meh */
1781 iounmap(dev_priv->gtt.gsm);
1782 }
1783
1784 return ret;
1785}
1786
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001787/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1788 * bits. When using advanced contexts each context stores its own PAT, but
1789 * writing this data shouldn't be harmful even in those cases. */
1790static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1791{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001792 uint64_t pat;
1793
1794 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1795 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1796 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1797 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1798 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1799 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1800 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1801 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1802
1803 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1804 * write would work. */
1805 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1806 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1807}
1808
Ben Widawsky63340132013-11-04 19:32:22 -08001809static int gen8_gmch_probe(struct drm_device *dev,
1810 size_t *gtt_total,
1811 size_t *stolen,
1812 phys_addr_t *mappable_base,
1813 unsigned long *mappable_end)
1814{
1815 struct drm_i915_private *dev_priv = dev->dev_private;
1816 unsigned int gtt_size;
1817 u16 snb_gmch_ctl;
1818 int ret;
1819
1820 /* TODO: We're not aware of mappable constraints on gen8 yet */
1821 *mappable_base = pci_resource_start(dev->pdev, 2);
1822 *mappable_end = pci_resource_len(dev->pdev, 2);
1823
1824 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1825 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1826
1827 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1828
1829 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1830
1831 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskyd31eb102013-11-02 21:07:17 -07001832 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001833
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001834 gen8_setup_private_ppat(dev_priv);
1835
Ben Widawsky63340132013-11-04 19:32:22 -08001836 ret = ggtt_probe_common(dev, gtt_size);
1837
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001838 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1839 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08001840
1841 return ret;
1842}
1843
Ben Widawskybaa09f52013-01-24 13:49:57 -08001844static int gen6_gmch_probe(struct drm_device *dev,
1845 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001846 size_t *stolen,
1847 phys_addr_t *mappable_base,
1848 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001849{
1850 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001851 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001852 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001853 int ret;
1854
Ben Widawsky41907dd2013-02-08 11:32:47 -08001855 *mappable_base = pci_resource_start(dev->pdev, 2);
1856 *mappable_end = pci_resource_len(dev->pdev, 2);
1857
Ben Widawskybaa09f52013-01-24 13:49:57 -08001858 /* 64/512MB is the current min/max we actually know of, but this is just
1859 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001860 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08001861 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08001862 DRM_ERROR("Unknown GMADR size (%lx)\n",
1863 dev_priv->gtt.mappable_end);
1864 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001865 }
1866
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001867 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1868 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08001869 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001870
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07001871 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001872
Ben Widawsky63340132013-11-04 19:32:22 -08001873 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001874 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
1875
Ben Widawsky63340132013-11-04 19:32:22 -08001876 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001877
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001878 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1879 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001880
1881 return ret;
1882}
1883
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001884static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001885{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001886
1887 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08001888
1889 drm_mm_takedown(&vm->mm);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001890 iounmap(gtt->gsm);
1891 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001892}
1893
1894static int i915_gmch_probe(struct drm_device *dev,
1895 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08001896 size_t *stolen,
1897 phys_addr_t *mappable_base,
1898 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001899{
1900 struct drm_i915_private *dev_priv = dev->dev_private;
1901 int ret;
1902
Ben Widawskybaa09f52013-01-24 13:49:57 -08001903 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1904 if (!ret) {
1905 DRM_ERROR("failed to set up gmch\n");
1906 return -EIO;
1907 }
1908
Ben Widawsky41907dd2013-02-08 11:32:47 -08001909 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08001910
1911 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001912 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001913
Chris Wilsonc0a7f812013-12-30 12:16:15 +00001914 if (unlikely(dev_priv->gtt.do_idle_maps))
1915 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
1916
Ben Widawskybaa09f52013-01-24 13:49:57 -08001917 return 0;
1918}
1919
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001920static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001921{
1922 intel_gmch_remove();
1923}
1924
1925int i915_gem_gtt_init(struct drm_device *dev)
1926{
1927 struct drm_i915_private *dev_priv = dev->dev_private;
1928 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08001929 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001930
Ben Widawskybaa09f52013-01-24 13:49:57 -08001931 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001932 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001933 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08001934 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001935 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001936 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001937 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001938 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07001939 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001940 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001941 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001942 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01001943 else if (INTEL_INFO(dev)->gen >= 7)
1944 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001945 else
Chris Wilson350ec882013-08-06 13:17:02 +01001946 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08001947 } else {
1948 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1949 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001950 }
1951
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001952 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001953 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08001954 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08001955 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001956
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001957 gtt->base.dev = dev;
1958
Ben Widawskybaa09f52013-01-24 13:49:57 -08001959 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001960 DRM_INFO("Memory usable by graphics device = %zdM\n",
1961 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07001962 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1963 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02001964#ifdef CONFIG_INTEL_IOMMU
1965 if (intel_iommu_gfx_mapped)
1966 DRM_INFO("VT-d active for gfx access\n");
1967#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001968
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001969 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02001970}
Ben Widawsky6f65e292013-12-06 14:10:56 -08001971
1972static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
1973 struct i915_address_space *vm)
1974{
1975 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
1976 if (vma == NULL)
1977 return ERR_PTR(-ENOMEM);
1978
1979 INIT_LIST_HEAD(&vma->vma_link);
1980 INIT_LIST_HEAD(&vma->mm_list);
1981 INIT_LIST_HEAD(&vma->exec_list);
1982 vma->vm = vm;
1983 vma->obj = obj;
1984
1985 switch (INTEL_INFO(vm->dev)->gen) {
1986 case 8:
1987 case 7:
1988 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001989 if (i915_is_ggtt(vm)) {
1990 vma->unbind_vma = ggtt_unbind_vma;
1991 vma->bind_vma = ggtt_bind_vma;
1992 } else {
1993 vma->unbind_vma = ppgtt_unbind_vma;
1994 vma->bind_vma = ppgtt_bind_vma;
1995 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08001996 break;
1997 case 5:
1998 case 4:
1999 case 3:
2000 case 2:
2001 BUG_ON(!i915_is_ggtt(vm));
2002 vma->unbind_vma = i915_ggtt_unbind_vma;
2003 vma->bind_vma = i915_ggtt_bind_vma;
2004 break;
2005 default:
2006 BUG();
2007 }
2008
2009 /* Keep GGTT vmas first to make debug easier */
2010 if (i915_is_ggtt(vm))
2011 list_add(&vma->vma_link, &obj->vma_list);
2012 else
2013 list_add_tail(&vma->vma_link, &obj->vma_list);
2014
2015 return vma;
2016}
2017
2018struct i915_vma *
2019i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2020 struct i915_address_space *vm)
2021{
2022 struct i915_vma *vma;
2023
2024 vma = i915_gem_obj_to_vma(obj, vm);
2025 if (!vma)
2026 vma = __i915_gem_vma_create(obj, vm);
2027
2028 return vma;
2029}