blob: 8d94e35820453559ded5cd70af9a482a86f30096 [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
Ville Syrjäläee0ce472014-04-09 13:28:01 +030033static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
34static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
Ben Widawskya2319c02014-03-18 16:09:37 -070035
Daniel Vettercfa7c862014-04-29 11:53:58 +020036static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
37{
38 if (enable_ppgtt == 0 || !HAS_ALIASING_PPGTT(dev))
39 return 0;
40
41 if (enable_ppgtt == 1)
42 return 1;
43
44 if (enable_ppgtt == 2 && HAS_PPGTT(dev))
45 return 2;
46
Daniel Vetter93a25a92014-03-06 09:40:43 +010047#ifdef CONFIG_INTEL_IOMMU
48 /* Disable ppgtt on SNB if VT-d is on. */
49 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
50 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +020051 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +010052 }
53#endif
54
Jesse Barnes62942ed2014-06-13 09:28:33 -070055 /* Early VLV doesn't have this */
Ville Syrjäläca2aed6c2014-06-28 02:03:56 +030056 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
57 dev->pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -070058 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
59 return 0;
60 }
61
Daniel Vettercfa7c862014-04-29 11:53:58 +020062 return HAS_ALIASING_PPGTT(dev) ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +010063}
64
Ben Widawskyfbe5d362013-11-04 19:56:49 -080065
Ben Widawsky6f65e292013-12-06 14:10:56 -080066static void ppgtt_bind_vma(struct i915_vma *vma,
67 enum i915_cache_level cache_level,
68 u32 flags);
69static void ppgtt_unbind_vma(struct i915_vma *vma);
70
Ben Widawsky94ec8f62013-11-02 21:07:18 -070071static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
72 enum i915_cache_level level,
73 bool valid)
74{
75 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
76 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -030077
78 switch (level) {
79 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -080080 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -030081 break;
82 case I915_CACHE_WT:
83 pte |= PPAT_DISPLAY_ELLC_INDEX;
84 break;
85 default:
86 pte |= PPAT_CACHED_INDEX;
87 break;
88 }
89
Ben Widawsky94ec8f62013-11-02 21:07:18 -070090 return pte;
91}
92
Ben Widawskyb1fe6672013-11-04 21:20:14 -080093static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
94 dma_addr_t addr,
95 enum i915_cache_level level)
96{
97 gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
98 pde |= addr;
99 if (level != I915_CACHE_NONE)
100 pde |= PPAT_CACHED_PDE_INDEX;
101 else
102 pde |= PPAT_UNCACHED_INDEX;
103 return pde;
104}
105
Chris Wilson350ec882013-08-06 13:17:02 +0100106static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700107 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530108 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700109{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700110 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700111 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700112
113 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100114 case I915_CACHE_L3_LLC:
115 case I915_CACHE_LLC:
116 pte |= GEN6_PTE_CACHE_LLC;
117 break;
118 case I915_CACHE_NONE:
119 pte |= GEN6_PTE_UNCACHED;
120 break;
121 default:
122 WARN_ON(1);
123 }
124
125 return pte;
126}
127
128static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700129 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530130 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100131{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700132 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100133 pte |= GEN6_PTE_ADDR_ENCODE(addr);
134
135 switch (level) {
136 case I915_CACHE_L3_LLC:
137 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700138 break;
139 case I915_CACHE_LLC:
140 pte |= GEN6_PTE_CACHE_LLC;
141 break;
142 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700143 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700144 break;
145 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100146 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700147 }
148
Ben Widawsky54d12522012-09-24 16:44:32 -0700149 return pte;
150}
151
Ben Widawsky80a74f72013-06-27 16:30:19 -0700152static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700153 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530154 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700155{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700156 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700157 pte |= GEN6_PTE_ADDR_ENCODE(addr);
158
159 /* Mark the page as writeable. Other platforms don't have a
160 * setting for read-only/writable, so this matches that behavior.
161 */
Akash Goel24f3a8c2014-06-17 10:59:42 +0530162 if (!(flags & PTE_READ_ONLY))
163 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700164
165 if (level != I915_CACHE_NONE)
166 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
167
168 return pte;
169}
170
Ben Widawsky80a74f72013-06-27 16:30:19 -0700171static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700172 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530173 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700174{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700175 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700176 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700177
178 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700179 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700180
181 return pte;
182}
183
Ben Widawsky4d15c142013-07-04 11:02:06 -0700184static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700185 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530186 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700187{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700188 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700189 pte |= HSW_PTE_ADDR_ENCODE(addr);
190
Chris Wilson651d7942013-08-08 14:41:10 +0100191 switch (level) {
192 case I915_CACHE_NONE:
193 break;
194 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000195 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100196 break;
197 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000198 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100199 break;
200 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700201
202 return pte;
203}
204
Ben Widawsky94e409c2013-11-04 22:29:36 -0800205/* Broadwell Page Directory Pointer Descriptors */
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100206static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
Ben Widawskye178f702013-12-06 14:10:47 -0800207 uint64_t val, bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800208{
Ben Widawskye178f702013-12-06 14:10:47 -0800209 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800210 int ret;
211
212 BUG_ON(entry >= 4);
213
Ben Widawskye178f702013-12-06 14:10:47 -0800214 if (synchronous) {
215 I915_WRITE(GEN8_RING_PDP_UDW(ring, entry), val >> 32);
216 I915_WRITE(GEN8_RING_PDP_LDW(ring, entry), (u32)val);
217 return 0;
218 }
219
Ben Widawsky94e409c2013-11-04 22:29:36 -0800220 ret = intel_ring_begin(ring, 6);
221 if (ret)
222 return ret;
223
224 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
225 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
226 intel_ring_emit(ring, (u32)(val >> 32));
227 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
228 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
229 intel_ring_emit(ring, (u32)(val));
230 intel_ring_advance(ring);
231
232 return 0;
233}
234
Ben Widawskyeeb94882013-12-06 14:11:10 -0800235static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100236 struct intel_engine_cs *ring,
Ben Widawskyeeb94882013-12-06 14:11:10 -0800237 bool synchronous)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800238{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800239 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800240
241 /* bit of a hack to find the actual last used pd */
242 int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
243
Ben Widawsky94e409c2013-11-04 22:29:36 -0800244 for (i = used_pd - 1; i >= 0; i--) {
245 dma_addr_t addr = ppgtt->pd_dma_addr[i];
Ben Widawskyeeb94882013-12-06 14:11:10 -0800246 ret = gen8_write_pdp(ring, i, addr, synchronous);
247 if (ret)
248 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800249 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800250
Ben Widawskyeeb94882013-12-06 14:11:10 -0800251 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800252}
253
Ben Widawsky459108b2013-11-02 21:07:23 -0700254static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800255 uint64_t start,
256 uint64_t length,
Ben Widawsky459108b2013-11-02 21:07:23 -0700257 bool use_scratch)
258{
259 struct i915_hw_ppgtt *ppgtt =
260 container_of(vm, struct i915_hw_ppgtt, base);
261 gen8_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800262 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
263 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
264 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800265 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700266 unsigned last_pte, i;
267
268 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
269 I915_CACHE_LLC, use_scratch);
270
271 while (num_entries) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800272 struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde];
Ben Widawsky459108b2013-11-02 21:07:23 -0700273
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800274 last_pte = pte + num_entries;
Ben Widawsky459108b2013-11-02 21:07:23 -0700275 if (last_pte > GEN8_PTES_PER_PAGE)
276 last_pte = GEN8_PTES_PER_PAGE;
277
278 pt_vaddr = kmap_atomic(page_table);
279
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800280 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700281 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800282 num_entries--;
283 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700284
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300285 if (!HAS_LLC(ppgtt->base.dev))
286 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky459108b2013-11-02 21:07:23 -0700287 kunmap_atomic(pt_vaddr);
288
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800289 pte = 0;
290 if (++pde == GEN8_PDES_PER_PAGE) {
291 pdpe++;
292 pde = 0;
293 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700294 }
295}
296
Ben Widawsky9df15b42013-11-02 21:07:24 -0700297static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
298 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800299 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530300 enum i915_cache_level cache_level, u32 unused)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700301{
302 struct i915_hw_ppgtt *ppgtt =
303 container_of(vm, struct i915_hw_ppgtt, base);
304 gen8_gtt_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800305 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
306 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
307 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700308 struct sg_page_iter sg_iter;
309
Chris Wilson6f1cc992013-12-31 15:50:31 +0000310 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700311
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800312 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
313 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS))
314 break;
315
316 if (pt_vaddr == NULL)
317 pt_vaddr = kmap_atomic(ppgtt->gen8_pt_pages[pdpe][pde]);
318
319 pt_vaddr[pte] =
Chris Wilson6f1cc992013-12-31 15:50:31 +0000320 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
321 cache_level, true);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800322 if (++pte == GEN8_PTES_PER_PAGE) {
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300323 if (!HAS_LLC(ppgtt->base.dev))
324 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700325 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000326 pt_vaddr = NULL;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800327 if (++pde == GEN8_PDES_PER_PAGE) {
328 pdpe++;
329 pde = 0;
330 }
331 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700332 }
333 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300334 if (pt_vaddr) {
335 if (!HAS_LLC(ppgtt->base.dev))
336 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000337 kunmap_atomic(pt_vaddr);
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300338 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700339}
340
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800341static void gen8_free_page_tables(struct page **pt_pages)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800342{
343 int i;
344
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800345 if (pt_pages == NULL)
346 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800347
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800348 for (i = 0; i < GEN8_PDES_PER_PAGE; i++)
349 if (pt_pages[i])
350 __free_pages(pt_pages[i], 0);
351}
352
353static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt)
354{
355 int i;
356
357 for (i = 0; i < ppgtt->num_pd_pages; i++) {
358 gen8_free_page_tables(ppgtt->gen8_pt_pages[i]);
359 kfree(ppgtt->gen8_pt_pages[i]);
360 kfree(ppgtt->gen8_pt_dma_addr[i]);
361 }
362
Ben Widawskyb45a6712014-02-12 14:28:44 -0800363 __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
364}
365
366static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
367{
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800368 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800369 int i, j;
370
371 for (i = 0; i < ppgtt->num_pd_pages; i++) {
372 /* TODO: In the future we'll support sparse mappings, so this
373 * will have to change. */
374 if (!ppgtt->pd_dma_addr[i])
375 continue;
376
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800377 pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
378 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800379
380 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
381 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
382 if (addr)
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800383 pci_unmap_page(hwdev, addr, PAGE_SIZE,
384 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800385 }
386 }
387}
388
Ben Widawsky37aca442013-11-04 20:47:32 -0800389static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
390{
391 struct i915_hw_ppgtt *ppgtt =
392 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800393
Ben Widawskyb45a6712014-02-12 14:28:44 -0800394 gen8_ppgtt_unmap_pages(ppgtt);
395 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800396}
397
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800398static struct page **__gen8_alloc_page_tables(void)
399{
400 struct page **pt_pages;
401 int i;
402
403 pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL);
404 if (!pt_pages)
405 return ERR_PTR(-ENOMEM);
406
407 for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
408 pt_pages[i] = alloc_page(GFP_KERNEL);
409 if (!pt_pages[i])
410 goto bail;
411 }
412
413 return pt_pages;
414
415bail:
416 gen8_free_page_tables(pt_pages);
417 kfree(pt_pages);
418 return ERR_PTR(-ENOMEM);
419}
420
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800421static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
422 const int max_pdp)
423{
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800424 struct page **pt_pages[GEN8_LEGACY_PDPS];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800425 int i, ret;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800426
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800427 for (i = 0; i < max_pdp; i++) {
428 pt_pages[i] = __gen8_alloc_page_tables();
429 if (IS_ERR(pt_pages[i])) {
430 ret = PTR_ERR(pt_pages[i]);
431 goto unwind_out;
432 }
433 }
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800434
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800435 /* NB: Avoid touching gen8_pt_pages until last to keep the allocation,
436 * "atomic" - for cleanup purposes.
437 */
438 for (i = 0; i < max_pdp; i++)
439 ppgtt->gen8_pt_pages[i] = pt_pages[i];
440
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800441 return 0;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800442
443unwind_out:
444 while (i--) {
445 gen8_free_page_tables(pt_pages[i]);
446 kfree(pt_pages[i]);
447 }
448
449 return ret;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800450}
451
452static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
453{
454 int i;
455
456 for (i = 0; i < ppgtt->num_pd_pages; i++) {
457 ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
458 sizeof(dma_addr_t),
459 GFP_KERNEL);
460 if (!ppgtt->gen8_pt_dma_addr[i])
461 return -ENOMEM;
462 }
463
464 return 0;
465}
466
467static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
468 const int max_pdp)
469{
470 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
471 if (!ppgtt->pd_pages)
472 return -ENOMEM;
473
474 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
475 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
476
477 return 0;
478}
479
480static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
481 const int max_pdp)
482{
483 int ret;
484
485 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
486 if (ret)
487 return ret;
488
489 ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
490 if (ret) {
491 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
492 return ret;
493 }
494
495 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
496
497 ret = gen8_ppgtt_allocate_dma(ppgtt);
498 if (ret)
499 gen8_ppgtt_free(ppgtt);
500
501 return ret;
502}
503
504static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
505 const int pd)
506{
507 dma_addr_t pd_addr;
508 int ret;
509
510 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
511 &ppgtt->pd_pages[pd], 0,
512 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
513
514 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
515 if (ret)
516 return ret;
517
518 ppgtt->pd_dma_addr[pd] = pd_addr;
519
520 return 0;
521}
522
523static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
524 const int pd,
525 const int pt)
526{
527 dma_addr_t pt_addr;
528 struct page *p;
529 int ret;
530
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800531 p = ppgtt->gen8_pt_pages[pd][pt];
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800532 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
533 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
534 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
535 if (ret)
536 return ret;
537
538 ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
539
540 return 0;
541}
542
Ben Widawsky37aca442013-11-04 20:47:32 -0800543/**
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800544 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
545 * with a net effect resembling a 2-level page table in normal x86 terms. Each
546 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
547 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800548 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800549 * FIXME: split allocation into smaller pieces. For now we only ever do this
550 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
Ben Widawsky37aca442013-11-04 20:47:32 -0800551 * TODO: Do something with the size parameter
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800552 */
Ben Widawsky37aca442013-11-04 20:47:32 -0800553static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
554{
Ben Widawsky37aca442013-11-04 20:47:32 -0800555 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800556 const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800557 int i, j, ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800558
559 if (size % (1<<30))
560 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
561
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800562 /* 1. Do all our allocations for page directories and page tables. */
563 ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
564 if (ret)
565 return ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800566
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800567 /*
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800568 * 2. Create DMA mappings for the page directories and page tables.
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800569 */
570 for (i = 0; i < max_pdp; i++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800571 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800572 if (ret)
573 goto bail;
574
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800575 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800576 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800577 if (ret)
578 goto bail;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800579 }
580 }
581
582 /*
583 * 3. Map all the page directory entires to point to the page tables
584 * we've allocated.
585 *
586 * For now, the PPGTT helper functions all require that the PDEs are
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800587 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800588 * will never need to touch the PDEs again.
589 */
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800590 for (i = 0; i < max_pdp; i++) {
591 gen8_ppgtt_pde_t *pd_vaddr;
592 pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
593 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
594 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
595 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
596 I915_CACHE_LLC);
597 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300598 if (!HAS_LLC(ppgtt->base.dev))
599 drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800600 kunmap_atomic(pd_vaddr);
601 }
602
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800603 ppgtt->switch_mm = gen8_mm_switch;
604 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
605 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
606 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
607 ppgtt->base.start = 0;
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800608 ppgtt->base.total = ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800609
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800610 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Ben Widawsky459108b2013-11-02 21:07:23 -0700611
Ben Widawsky37aca442013-11-04 20:47:32 -0800612 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
613 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
614 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800615 ppgtt->num_pd_entries,
616 (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700617 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800618
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800619bail:
620 gen8_ppgtt_unmap_pages(ppgtt);
621 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800622 return ret;
623}
624
Ben Widawsky87d60b62013-12-06 14:11:29 -0800625static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
626{
627 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
628 struct i915_address_space *vm = &ppgtt->base;
629 gen6_gtt_pte_t __iomem *pd_addr;
630 gen6_gtt_pte_t scratch_pte;
631 uint32_t pd_entry;
632 int pte, pde;
633
Akash Goel24f3a8c2014-06-17 10:59:42 +0530634 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800635
636 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
637 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
638
639 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
640 ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
641 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
642 u32 expected;
643 gen6_gtt_pte_t *pt_vaddr;
644 dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
645 pd_entry = readl(pd_addr + pde);
646 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
647
648 if (pd_entry != expected)
649 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
650 pde,
651 pd_entry,
652 expected);
653 seq_printf(m, "\tPDE: %x\n", pd_entry);
654
655 pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
656 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
657 unsigned long va =
658 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
659 (pte * PAGE_SIZE);
660 int i;
661 bool found = false;
662 for (i = 0; i < 4; i++)
663 if (pt_vaddr[pte + i] != scratch_pte)
664 found = true;
665 if (!found)
666 continue;
667
668 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
669 for (i = 0; i < 4; i++) {
670 if (pt_vaddr[pte + i] != scratch_pte)
671 seq_printf(m, " %08x", pt_vaddr[pte + i]);
672 else
673 seq_puts(m, " SCRATCH ");
674 }
675 seq_puts(m, "\n");
676 }
677 kunmap_atomic(pt_vaddr);
678 }
679}
680
Ben Widawsky3e302542013-04-23 23:15:32 -0700681static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700682{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700683 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700684 gen6_gtt_pte_t __iomem *pd_addr;
685 uint32_t pd_entry;
686 int i;
687
Ben Widawsky0a732872013-04-23 23:15:30 -0700688 WARN_ON(ppgtt->pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700689 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
690 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
691 for (i = 0; i < ppgtt->num_pd_entries; i++) {
692 dma_addr_t pt_addr;
693
694 pt_addr = ppgtt->pt_dma_addr[i];
695 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
696 pd_entry |= GEN6_PDE_VALID;
697
698 writel(pd_entry, pd_addr + i);
699 }
700 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700701}
702
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800703static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700704{
Ben Widawsky3e302542013-04-23 23:15:32 -0700705 BUG_ON(ppgtt->pd_offset & 0x3f);
706
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800707 return (ppgtt->pd_offset / 64) << 16;
708}
Ben Widawsky61973492013-04-08 18:43:54 -0700709
Ben Widawsky90252e52013-12-06 14:11:12 -0800710static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100711 struct intel_engine_cs *ring,
Ben Widawsky90252e52013-12-06 14:11:12 -0800712 bool synchronous)
713{
714 struct drm_device *dev = ppgtt->base.dev;
715 struct drm_i915_private *dev_priv = dev->dev_private;
716 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700717
Ben Widawsky90252e52013-12-06 14:11:12 -0800718 /* If we're in reset, we can assume the GPU is sufficiently idle to
719 * manually frob these bits. Ideally we could use the ring functions,
720 * except our error handling makes it quite difficult (can't use
721 * intel_ring_begin, ring->flush, or intel_ring_advance)
722 *
723 * FIXME: We should try not to special case reset
724 */
725 if (synchronous ||
726 i915_reset_in_progress(&dev_priv->gpu_error)) {
727 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
728 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
729 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
730 POSTING_READ(RING_PP_DIR_BASE(ring));
731 return 0;
Ben Widawsky61973492013-04-08 18:43:54 -0700732 }
733
Ben Widawsky90252e52013-12-06 14:11:12 -0800734 /* NB: TLBs must be flushed and invalidated before a switch */
735 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
736 if (ret)
737 return ret;
738
739 ret = intel_ring_begin(ring, 6);
740 if (ret)
741 return ret;
742
743 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
744 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
745 intel_ring_emit(ring, PP_DIR_DCLV_2G);
746 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
747 intel_ring_emit(ring, get_pd_offset(ppgtt));
748 intel_ring_emit(ring, MI_NOOP);
749 intel_ring_advance(ring);
750
751 return 0;
752}
753
Ben Widawsky48a10382013-12-06 14:11:11 -0800754static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100755 struct intel_engine_cs *ring,
Ben Widawsky48a10382013-12-06 14:11:11 -0800756 bool synchronous)
757{
758 struct drm_device *dev = ppgtt->base.dev;
759 struct drm_i915_private *dev_priv = dev->dev_private;
760 int ret;
761
762 /* If we're in reset, we can assume the GPU is sufficiently idle to
763 * manually frob these bits. Ideally we could use the ring functions,
764 * except our error handling makes it quite difficult (can't use
765 * intel_ring_begin, ring->flush, or intel_ring_advance)
766 *
767 * FIXME: We should try not to special case reset
768 */
769 if (synchronous ||
770 i915_reset_in_progress(&dev_priv->gpu_error)) {
771 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
772 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
773 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
774 POSTING_READ(RING_PP_DIR_BASE(ring));
775 return 0;
776 }
777
778 /* NB: TLBs must be flushed and invalidated before a switch */
779 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
780 if (ret)
781 return ret;
782
783 ret = intel_ring_begin(ring, 6);
784 if (ret)
785 return ret;
786
787 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
788 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
789 intel_ring_emit(ring, PP_DIR_DCLV_2G);
790 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
791 intel_ring_emit(ring, get_pd_offset(ppgtt));
792 intel_ring_emit(ring, MI_NOOP);
793 intel_ring_advance(ring);
794
Ben Widawsky90252e52013-12-06 14:11:12 -0800795 /* XXX: RCS is the only one to auto invalidate the TLBs? */
796 if (ring->id != RCS) {
797 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
798 if (ret)
799 return ret;
800 }
801
Ben Widawsky48a10382013-12-06 14:11:11 -0800802 return 0;
803}
804
Ben Widawskyeeb94882013-12-06 14:11:10 -0800805static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100806 struct intel_engine_cs *ring,
Ben Widawskyeeb94882013-12-06 14:11:10 -0800807 bool synchronous)
808{
809 struct drm_device *dev = ppgtt->base.dev;
810 struct drm_i915_private *dev_priv = dev->dev_private;
811
Ben Widawsky48a10382013-12-06 14:11:11 -0800812 if (!synchronous)
813 return 0;
814
Ben Widawskyeeb94882013-12-06 14:11:10 -0800815 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
816 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
817
818 POSTING_READ(RING_PP_DIR_DCLV(ring));
819
820 return 0;
821}
822
Daniel Vetter82460d92014-08-06 20:19:53 +0200823static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -0800824{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800825 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100826 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +0200827 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -0800828
829 for_each_ring(ring, dev_priv, j) {
830 I915_WRITE(RING_MODE_GEN7(ring),
831 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -0800832 }
Ben Widawskyeeb94882013-12-06 14:11:10 -0800833}
834
Daniel Vetter82460d92014-08-06 20:19:53 +0200835static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800836{
Jani Nikula50227e12014-03-31 14:27:21 +0300837 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100838 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800839 uint32_t ecochk, ecobits;
840 int i;
841
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800842 ecobits = I915_READ(GAC_ECO_BITS);
843 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
844
845 ecochk = I915_READ(GAM_ECOCHK);
846 if (IS_HASWELL(dev)) {
847 ecochk |= ECOCHK_PPGTT_WB_HSW;
848 } else {
849 ecochk |= ECOCHK_PPGTT_LLC_IVB;
850 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
851 }
852 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800853
Ben Widawsky61973492013-04-08 18:43:54 -0700854 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800855 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800856 I915_WRITE(RING_MODE_GEN7(ring),
857 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700858 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800859}
860
Daniel Vetter82460d92014-08-06 20:19:53 +0200861static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -0700862{
Jani Nikula50227e12014-03-31 14:27:21 +0300863 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800864 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -0700865
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800866 ecobits = I915_READ(GAC_ECO_BITS);
867 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
868 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700869
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800870 gab_ctl = I915_READ(GAB_CTL);
871 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700872
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800873 ecochk = I915_READ(GAM_ECOCHK);
874 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700875
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800876 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700877}
878
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100879/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700880static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800881 uint64_t start,
882 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -0700883 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100884{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700885 struct i915_hw_ppgtt *ppgtt =
886 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700887 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -0800888 unsigned first_entry = start >> PAGE_SHIFT;
889 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100890 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100891 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
892 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100893
Akash Goel24f3a8c2014-06-17 10:59:42 +0530894 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100895
Daniel Vetter7bddb012012-02-09 17:15:47 +0100896 while (num_entries) {
897 last_pte = first_pte + num_entries;
898 if (last_pte > I915_PPGTT_PT_ENTRIES)
899 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100900
Daniel Vettera15326a2013-03-19 23:48:39 +0100901 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100902
903 for (i = first_pte; i < last_pte; i++)
904 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100905
906 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100907
Daniel Vetter7bddb012012-02-09 17:15:47 +0100908 num_entries -= last_pte - first_pte;
909 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100910 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100911 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100912}
913
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700914static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800915 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800916 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530917 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -0800918{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700919 struct i915_hw_ppgtt *ppgtt =
920 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700921 gen6_gtt_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -0800922 unsigned first_entry = start >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100923 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200924 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
925 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800926
Chris Wilsoncc797142013-12-31 15:50:30 +0000927 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +0200928 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +0000929 if (pt_vaddr == NULL)
930 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800931
Chris Wilsoncc797142013-12-31 15:50:30 +0000932 pt_vaddr[act_pte] =
933 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +0530934 cache_level, true, flags);
935
Imre Deak6e995e22013-02-18 19:28:04 +0200936 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
937 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +0000938 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +0100939 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +0200940 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800941 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800942 }
Chris Wilsoncc797142013-12-31 15:50:30 +0000943 if (pt_vaddr)
944 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800945}
946
Ben Widawskya00d8252014-02-19 22:05:48 -0800947static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100948{
Daniel Vetter3440d262013-01-24 13:49:56 -0800949 int i;
950
951 if (ppgtt->pt_dma_addr) {
952 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700953 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800954 ppgtt->pt_dma_addr[i],
955 4096, PCI_DMA_BIDIRECTIONAL);
956 }
Ben Widawskya00d8252014-02-19 22:05:48 -0800957}
958
959static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
960{
961 int i;
Daniel Vetter3440d262013-01-24 13:49:56 -0800962
963 kfree(ppgtt->pt_dma_addr);
964 for (i = 0; i < ppgtt->num_pd_entries; i++)
965 __free_page(ppgtt->pt_pages[i]);
966 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800967}
968
Ben Widawskya00d8252014-02-19 22:05:48 -0800969static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
970{
971 struct i915_hw_ppgtt *ppgtt =
972 container_of(vm, struct i915_hw_ppgtt, base);
973
Ben Widawskya00d8252014-02-19 22:05:48 -0800974 drm_mm_remove_node(&ppgtt->node);
975
976 gen6_ppgtt_unmap_pages(ppgtt);
977 gen6_ppgtt_free(ppgtt);
978}
979
Ben Widawskyb1465202014-02-19 22:05:49 -0800980static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -0800981{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700982 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100983 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -0800984 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -0800985 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100986
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800987 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
988 * allocator works in address space sizes, so it's multiplied by page
989 * size. We allocate at the top of the GTT to avoid fragmentation.
990 */
991 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -0800992alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800993 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
994 &ppgtt->node, GEN6_PD_SIZE,
995 GEN6_PD_ALIGN, 0,
996 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -0700997 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800998 if (ret == -ENOSPC && !retried) {
999 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1000 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02001001 I915_CACHE_NONE,
1002 0, dev_priv->gtt.base.total,
1003 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001004 if (ret)
1005 return ret;
1006
1007 retried = true;
1008 goto alloc;
1009 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001010
1011 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1012 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001013
Ben Widawsky6670a5a2013-06-27 16:30:04 -07001014 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawskyb1465202014-02-19 22:05:49 -08001015 return ret;
1016}
1017
1018static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
1019{
1020 int i;
1021
1022 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
1023 GFP_KERNEL);
1024
1025 if (!ppgtt->pt_pages)
1026 return -ENOMEM;
1027
1028 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1029 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
1030 if (!ppgtt->pt_pages[i]) {
1031 gen6_ppgtt_free(ppgtt);
1032 return -ENOMEM;
1033 }
1034 }
1035
1036 return 0;
1037}
1038
1039static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1040{
1041 int ret;
1042
1043 ret = gen6_ppgtt_allocate_page_directories(ppgtt);
1044 if (ret)
1045 return ret;
1046
1047 ret = gen6_ppgtt_allocate_page_tables(ppgtt);
1048 if (ret) {
1049 drm_mm_remove_node(&ppgtt->node);
1050 return ret;
1051 }
1052
1053 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
1054 GFP_KERNEL);
1055 if (!ppgtt->pt_dma_addr) {
1056 drm_mm_remove_node(&ppgtt->node);
1057 gen6_ppgtt_free(ppgtt);
1058 return -ENOMEM;
1059 }
1060
1061 return 0;
1062}
1063
1064static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
1065{
1066 struct drm_device *dev = ppgtt->base.dev;
1067 int i;
1068
1069 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1070 dma_addr_t pt_addr;
1071
1072 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
1073 PCI_DMA_BIDIRECTIONAL);
1074
1075 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1076 gen6_ppgtt_unmap_pages(ppgtt);
1077 return -EIO;
1078 }
1079
1080 ppgtt->pt_dma_addr[i] = pt_addr;
1081 }
1082
1083 return 0;
1084}
1085
1086static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1087{
1088 struct drm_device *dev = ppgtt->base.dev;
1089 struct drm_i915_private *dev_priv = dev->dev_private;
1090 int ret;
1091
1092 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001093 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001094 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001095 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001096 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001097 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001098 ppgtt->switch_mm = gen7_mm_switch;
1099 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001100 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001101
1102 ret = gen6_ppgtt_alloc(ppgtt);
1103 if (ret)
1104 return ret;
1105
1106 ret = gen6_ppgtt_setup_page_tables(ppgtt);
1107 if (ret) {
1108 gen6_ppgtt_free(ppgtt);
1109 return ret;
1110 }
1111
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001112 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1113 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1114 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001115 ppgtt->base.start = 0;
Ben Widawsky5a6c93f2014-03-08 11:58:17 -08001116 ppgtt->base.total = ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001117 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001118
Ben Widawskyb1465202014-02-19 22:05:49 -08001119 ppgtt->pd_offset =
1120 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001121
Ben Widawsky782f1492014-02-20 11:50:33 -08001122 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001123
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001124 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1125 ppgtt->node.size >> 20,
1126 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001127
Daniel Vetterfa76da32014-08-06 20:19:54 +02001128 gen6_write_pdes(ppgtt);
1129 DRM_DEBUG("Adding PPGTT at offset %x\n",
1130 ppgtt->pd_offset << 10);
1131
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001132 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001133}
1134
Daniel Vetterfa76da32014-08-06 20:19:54 +02001135static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001136{
1137 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter3440d262013-01-24 13:49:56 -08001138
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001139 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001140 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001141
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001142 if (INTEL_INFO(dev)->gen < 8)
Daniel Vetterfa76da32014-08-06 20:19:54 +02001143 return gen6_ppgtt_init(ppgtt);
Daniel Vetter8fe6bd22013-11-02 21:07:01 -07001144 else if (IS_GEN8(dev))
Daniel Vetterfa76da32014-08-06 20:19:54 +02001145 return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001146 else
1147 BUG();
Daniel Vetterfa76da32014-08-06 20:19:54 +02001148}
1149int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1150{
1151 struct drm_i915_private *dev_priv = dev->dev_private;
1152 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001153
Daniel Vetterfa76da32014-08-06 20:19:54 +02001154 ret = __hw_ppgtt_init(dev, ppgtt);
1155 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001156 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001157 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1158 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001159 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001160 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001161
1162 return ret;
1163}
1164
Daniel Vetter82460d92014-08-06 20:19:53 +02001165int i915_ppgtt_init_hw(struct drm_device *dev)
1166{
1167 struct drm_i915_private *dev_priv = dev->dev_private;
1168 struct intel_engine_cs *ring;
1169 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1170 int i, ret = 0;
1171
1172 if (!USES_PPGTT(dev))
1173 return 0;
1174
1175 if (IS_GEN6(dev))
1176 gen6_ppgtt_enable(dev);
1177 else if (IS_GEN7(dev))
1178 gen7_ppgtt_enable(dev);
1179 else if (INTEL_INFO(dev)->gen >= 8)
1180 gen8_ppgtt_enable(dev);
1181 else
1182 WARN_ON(1);
1183
1184 if (ppgtt) {
1185 for_each_ring(ring, dev_priv, i) {
1186 ret = ppgtt->switch_mm(ppgtt, ring, true);
1187 if (ret != 0)
1188 return ret;
1189 }
1190 }
1191
1192 return ret;
1193}
Daniel Vetter4d884702014-08-06 15:04:47 +02001194struct i915_hw_ppgtt *
1195i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1196{
1197 struct i915_hw_ppgtt *ppgtt;
1198 int ret;
1199
1200 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1201 if (!ppgtt)
1202 return ERR_PTR(-ENOMEM);
1203
1204 ret = i915_ppgtt_init(dev, ppgtt);
1205 if (ret) {
1206 kfree(ppgtt);
1207 return ERR_PTR(ret);
1208 }
1209
1210 ppgtt->file_priv = fpriv;
1211
1212 return ppgtt;
1213}
1214
Daniel Vetteree960be2014-08-06 15:04:45 +02001215void i915_ppgtt_release(struct kref *kref)
1216{
1217 struct i915_hw_ppgtt *ppgtt =
1218 container_of(kref, struct i915_hw_ppgtt, ref);
1219
1220 /* vmas should already be unbound */
1221 WARN_ON(!list_empty(&ppgtt->base.active_list));
1222 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1223
Daniel Vetter19dd1202014-08-06 15:04:55 +02001224 list_del(&ppgtt->base.global_link);
1225 drm_mm_takedown(&ppgtt->base.mm);
1226
Daniel Vetteree960be2014-08-06 15:04:45 +02001227 ppgtt->base.cleanup(&ppgtt->base);
1228 kfree(ppgtt);
1229}
1230
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001231static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001232ppgtt_bind_vma(struct i915_vma *vma,
1233 enum i915_cache_level cache_level,
1234 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001235{
Akash Goel24f3a8c2014-06-17 10:59:42 +05301236 /* Currently applicable only to VLV */
1237 if (vma->obj->gt_ro)
1238 flags |= PTE_READ_ONLY;
1239
Ben Widawsky782f1492014-02-20 11:50:33 -08001240 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301241 cache_level, flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001242}
1243
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001244static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001245{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001246 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001247 vma->node.start,
1248 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001249 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001250}
1251
Ben Widawskya81cc002013-01-18 12:30:31 -08001252extern int intel_iommu_gfx_mapped;
1253/* Certain Gen5 chipsets require require idling the GPU before
1254 * unmapping anything from the GTT when VT-d is enabled.
1255 */
1256static inline bool needs_idle_maps(struct drm_device *dev)
1257{
1258#ifdef CONFIG_INTEL_IOMMU
1259 /* Query intel_iommu to see if we need the workaround. Presumably that
1260 * was loaded first.
1261 */
1262 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1263 return true;
1264#endif
1265 return false;
1266}
1267
Ben Widawsky5c042282011-10-17 15:51:55 -07001268static bool do_idling(struct drm_i915_private *dev_priv)
1269{
1270 bool ret = dev_priv->mm.interruptible;
1271
Ben Widawskya81cc002013-01-18 12:30:31 -08001272 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001273 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001274 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001275 DRM_ERROR("Couldn't idle GPU\n");
1276 /* Wait a bit, in hopes it avoids the hang */
1277 udelay(10);
1278 }
1279 }
1280
1281 return ret;
1282}
1283
1284static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1285{
Ben Widawskya81cc002013-01-18 12:30:31 -08001286 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001287 dev_priv->mm.interruptible = interruptible;
1288}
1289
Ben Widawsky828c7902013-10-16 09:21:30 -07001290void i915_check_and_clear_faults(struct drm_device *dev)
1291{
1292 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001293 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07001294 int i;
1295
1296 if (INTEL_INFO(dev)->gen < 6)
1297 return;
1298
1299 for_each_ring(ring, dev_priv, i) {
1300 u32 fault_reg;
1301 fault_reg = I915_READ(RING_FAULT_REG(ring));
1302 if (fault_reg & RING_FAULT_VALID) {
1303 DRM_DEBUG_DRIVER("Unexpected fault\n"
1304 "\tAddr: 0x%08lx\\n"
1305 "\tAddress space: %s\n"
1306 "\tSource ID: %d\n"
1307 "\tType: %d\n",
1308 fault_reg & PAGE_MASK,
1309 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1310 RING_FAULT_SRCID(fault_reg),
1311 RING_FAULT_FAULT_TYPE(fault_reg));
1312 I915_WRITE(RING_FAULT_REG(ring),
1313 fault_reg & ~RING_FAULT_VALID);
1314 }
1315 }
1316 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1317}
1318
1319void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1320{
1321 struct drm_i915_private *dev_priv = dev->dev_private;
1322
1323 /* Don't bother messing with faults pre GEN6 as we have little
1324 * documentation supporting that it's a good idea.
1325 */
1326 if (INTEL_INFO(dev)->gen < 6)
1327 return;
1328
1329 i915_check_and_clear_faults(dev);
1330
1331 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001332 dev_priv->gtt.base.start,
1333 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001334 true);
Ben Widawsky828c7902013-10-16 09:21:30 -07001335}
1336
Daniel Vetter76aaf222010-11-05 22:23:30 +01001337void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1338{
1339 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001340 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001341 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001342
Ben Widawsky828c7902013-10-16 09:21:30 -07001343 i915_check_and_clear_faults(dev);
1344
Chris Wilsonbee4a182011-01-21 10:54:32 +00001345 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001346 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001347 dev_priv->gtt.base.start,
1348 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001349 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001350
Ben Widawsky35c20a62013-05-31 11:28:48 -07001351 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001352 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1353 &dev_priv->gtt.base);
1354 if (!vma)
1355 continue;
1356
Chris Wilson2c225692013-08-09 12:26:45 +01001357 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001358 /* The bind_vma code tries to be smart about tracking mappings.
1359 * Unfortunately above, we've just wiped out the mappings
1360 * without telling our object about it. So we need to fake it.
1361 */
1362 obj->has_global_gtt_mapping = 0;
1363 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001364 }
1365
Ben Widawsky80da2162013-12-06 14:11:17 -08001366
Ben Widawskya2319c02014-03-18 16:09:37 -07001367 if (INTEL_INFO(dev)->gen >= 8) {
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001368 if (IS_CHERRYVIEW(dev))
1369 chv_setup_private_ppat(dev_priv);
1370 else
1371 bdw_setup_private_ppat(dev_priv);
1372
Ben Widawsky80da2162013-12-06 14:11:17 -08001373 return;
Ben Widawskya2319c02014-03-18 16:09:37 -07001374 }
Ben Widawsky80da2162013-12-06 14:11:17 -08001375
1376 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1377 /* TODO: Perhaps it shouldn't be gen6 specific */
1378 if (i915_is_ggtt(vm)) {
1379 if (dev_priv->mm.aliasing_ppgtt)
1380 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1381 continue;
1382 }
1383
1384 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001385 }
1386
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001387 i915_gem_chipset_flush(dev);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001388}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001389
Daniel Vetter74163902012-02-15 23:50:21 +01001390int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001391{
Chris Wilson9da3da62012-06-01 15:20:22 +01001392 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001393 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001394
1395 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1396 obj->pages->sgl, obj->pages->nents,
1397 PCI_DMA_BIDIRECTIONAL))
1398 return -ENOSPC;
1399
1400 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001401}
1402
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001403static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1404{
1405#ifdef writeq
1406 writeq(pte, addr);
1407#else
1408 iowrite32((u32)pte, addr);
1409 iowrite32(pte >> 32, addr + 4);
1410#endif
1411}
1412
1413static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1414 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001415 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301416 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001417{
1418 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001419 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001420 gen8_gtt_pte_t __iomem *gtt_entries =
1421 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1422 int i = 0;
1423 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001424 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001425
1426 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1427 addr = sg_dma_address(sg_iter.sg) +
1428 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1429 gen8_set_pte(&gtt_entries[i],
1430 gen8_pte_encode(addr, level, true));
1431 i++;
1432 }
1433
1434 /*
1435 * XXX: This serves as a posting read to make sure that the PTE has
1436 * actually been updated. There is some concern that even though
1437 * registers and PTEs are within the same BAR that they are potentially
1438 * of NUMA access patterns. Therefore, even with the way we assume
1439 * hardware should work, we must keep this posting read for paranoia.
1440 */
1441 if (i != 0)
1442 WARN_ON(readq(&gtt_entries[i-1])
1443 != gen8_pte_encode(addr, level, true));
1444
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001445 /* This next bit makes the above posting read even more important. We
1446 * want to flush the TLBs only after we're certain all the PTE updates
1447 * have finished.
1448 */
1449 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1450 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001451}
1452
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001453/*
1454 * Binds an object into the global gtt with the specified cache level. The object
1455 * will be accessible to the GPU via commands whose operands reference offsets
1456 * within the global GTT as well as accessible by the GPU through the GMADR
1457 * mapped BAR (dev_priv->mm.gtt->gtt).
1458 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001459static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001460 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001461 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301462 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001463{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001464 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001465 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001466 gen6_gtt_pte_t __iomem *gtt_entries =
1467 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001468 int i = 0;
1469 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001470 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001471
Imre Deak6e995e22013-02-18 19:28:04 +02001472 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001473 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301474 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001475 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001476 }
1477
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001478 /* XXX: This serves as a posting read to make sure that the PTE has
1479 * actually been updated. There is some concern that even though
1480 * registers and PTEs are within the same BAR that they are potentially
1481 * of NUMA access patterns. Therefore, even with the way we assume
1482 * hardware should work, we must keep this posting read for paranoia.
1483 */
Pavel Machek57007df2014-07-28 13:20:58 +02001484 if (i != 0) {
1485 unsigned long gtt = readl(&gtt_entries[i-1]);
1486 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1487 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001488
1489 /* This next bit makes the above posting read even more important. We
1490 * want to flush the TLBs only after we're certain all the PTE updates
1491 * have finished.
1492 */
1493 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1494 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001495}
1496
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001497static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001498 uint64_t start,
1499 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001500 bool use_scratch)
1501{
1502 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001503 unsigned first_entry = start >> PAGE_SHIFT;
1504 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001505 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1506 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1507 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1508 int i;
1509
1510 if (WARN(num_entries > max_entries,
1511 "First entry = %d; Num entries = %d (max=%d)\n",
1512 first_entry, num_entries, max_entries))
1513 num_entries = max_entries;
1514
1515 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1516 I915_CACHE_LLC,
1517 use_scratch);
1518 for (i = 0; i < num_entries; i++)
1519 gen8_set_pte(&gtt_base[i], scratch_pte);
1520 readl(gtt_base);
1521}
1522
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001523static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001524 uint64_t start,
1525 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001526 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001527{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001528 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001529 unsigned first_entry = start >> PAGE_SHIFT;
1530 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001531 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1532 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001533 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001534 int i;
1535
1536 if (WARN(num_entries > max_entries,
1537 "First entry = %d; Num entries = %d (max=%d)\n",
1538 first_entry, num_entries, max_entries))
1539 num_entries = max_entries;
1540
Akash Goel24f3a8c2014-06-17 10:59:42 +05301541 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07001542
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001543 for (i = 0; i < num_entries; i++)
1544 iowrite32(scratch_pte, &gtt_base[i]);
1545 readl(gtt_base);
1546}
1547
Ben Widawsky6f65e292013-12-06 14:10:56 -08001548
1549static void i915_ggtt_bind_vma(struct i915_vma *vma,
1550 enum i915_cache_level cache_level,
1551 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001552{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001553 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001554 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1555 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1556
Ben Widawsky6f65e292013-12-06 14:10:56 -08001557 BUG_ON(!i915_is_ggtt(vma->vm));
1558 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1559 vma->obj->has_global_gtt_mapping = 1;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001560}
1561
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001562static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001563 uint64_t start,
1564 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001565 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001566{
Ben Widawsky782f1492014-02-20 11:50:33 -08001567 unsigned first_entry = start >> PAGE_SHIFT;
1568 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001569 intel_gtt_clear_range(first_entry, num_entries);
1570}
1571
Ben Widawsky6f65e292013-12-06 14:10:56 -08001572static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001573{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001574 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1575 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001576
Ben Widawsky6f65e292013-12-06 14:10:56 -08001577 BUG_ON(!i915_is_ggtt(vma->vm));
1578 vma->obj->has_global_gtt_mapping = 0;
1579 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001580}
1581
Ben Widawsky6f65e292013-12-06 14:10:56 -08001582static void ggtt_bind_vma(struct i915_vma *vma,
1583 enum i915_cache_level cache_level,
1584 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001585{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001586 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001587 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001588 struct drm_i915_gem_object *obj = vma->obj;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001589
Akash Goel24f3a8c2014-06-17 10:59:42 +05301590 /* Currently applicable only to VLV */
1591 if (obj->gt_ro)
1592 flags |= PTE_READ_ONLY;
1593
Ben Widawsky6f65e292013-12-06 14:10:56 -08001594 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1595 * or we have a global mapping already but the cacheability flags have
1596 * changed, set the global PTEs.
1597 *
1598 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1599 * instead if none of the above hold true.
1600 *
1601 * NB: A global mapping should only be needed for special regions like
1602 * "gtt mappable", SNB errata, or if specified via special execbuf
1603 * flags. At all other times, the GPU will use the aliasing PPGTT.
1604 */
1605 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1606 if (!obj->has_global_gtt_mapping ||
1607 (cache_level != obj->cache_level)) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001608 vma->vm->insert_entries(vma->vm, obj->pages,
1609 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301610 cache_level, flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001611 obj->has_global_gtt_mapping = 1;
1612 }
1613 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001614
Ben Widawsky6f65e292013-12-06 14:10:56 -08001615 if (dev_priv->mm.aliasing_ppgtt &&
1616 (!obj->has_aliasing_ppgtt_mapping ||
1617 (cache_level != obj->cache_level))) {
1618 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1619 appgtt->base.insert_entries(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001620 vma->obj->pages,
1621 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301622 cache_level, flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001623 vma->obj->has_aliasing_ppgtt_mapping = 1;
1624 }
1625}
1626
1627static void ggtt_unbind_vma(struct i915_vma *vma)
1628{
1629 struct drm_device *dev = vma->vm->dev;
1630 struct drm_i915_private *dev_priv = dev->dev_private;
1631 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001632
1633 if (obj->has_global_gtt_mapping) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001634 vma->vm->clear_range(vma->vm,
1635 vma->node.start,
1636 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001637 true);
1638 obj->has_global_gtt_mapping = 0;
1639 }
1640
1641 if (obj->has_aliasing_ppgtt_mapping) {
1642 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1643 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001644 vma->node.start,
1645 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001646 true);
1647 obj->has_aliasing_ppgtt_mapping = 0;
1648 }
Daniel Vetter74163902012-02-15 23:50:21 +01001649}
1650
1651void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1652{
Ben Widawsky5c042282011-10-17 15:51:55 -07001653 struct drm_device *dev = obj->base.dev;
1654 struct drm_i915_private *dev_priv = dev->dev_private;
1655 bool interruptible;
1656
1657 interruptible = do_idling(dev_priv);
1658
Chris Wilson9da3da62012-06-01 15:20:22 +01001659 if (!obj->has_dma_mapping)
1660 dma_unmap_sg(&dev->pdev->dev,
1661 obj->pages->sgl, obj->pages->nents,
1662 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001663
1664 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001665}
Daniel Vetter644ec022012-03-26 09:45:40 +02001666
Chris Wilson42d6ab42012-07-26 11:49:32 +01001667static void i915_gtt_color_adjust(struct drm_mm_node *node,
1668 unsigned long color,
1669 unsigned long *start,
1670 unsigned long *end)
1671{
1672 if (node->color != color)
1673 *start += 4096;
1674
1675 if (!list_empty(&node->node_list)) {
1676 node = list_entry(node->node_list.next,
1677 struct drm_mm_node,
1678 node_list);
1679 if (node->allocated && node->color != color)
1680 *end -= 4096;
1681 }
1682}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001683
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001684int i915_gem_setup_global_gtt(struct drm_device *dev,
1685 unsigned long start,
1686 unsigned long mappable_end,
1687 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001688{
Ben Widawskye78891c2013-01-25 16:41:04 -08001689 /* Let GEM Manage all of the aperture.
1690 *
1691 * However, leave one page at the end still bound to the scratch page.
1692 * There are a number of places where the hardware apparently prefetches
1693 * past the end of the object, and we've seen multiple hangs with the
1694 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1695 * aperture. One page should be enough to keep any prefetching inside
1696 * of the aperture.
1697 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001698 struct drm_i915_private *dev_priv = dev->dev_private;
1699 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001700 struct drm_mm_node *entry;
1701 struct drm_i915_gem_object *obj;
1702 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02001703 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02001704
Ben Widawsky35451cb2013-01-17 12:45:13 -08001705 BUG_ON(mappable_end > end);
1706
Chris Wilsoned2f3452012-11-15 11:32:19 +00001707 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001708 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001709 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001710 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001711
Chris Wilsoned2f3452012-11-15 11:32:19 +00001712 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001713 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001714 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001715
Ben Widawskyedd41a82013-07-05 14:41:05 -07001716 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001717 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001718
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001719 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001720 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001721 if (ret) {
1722 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
1723 return ret;
1724 }
Chris Wilsoned2f3452012-11-15 11:32:19 +00001725 obj->has_global_gtt_mapping = 1;
1726 }
1727
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001728 dev_priv->gtt.base.start = start;
1729 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001730
Chris Wilsoned2f3452012-11-15 11:32:19 +00001731 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001732 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00001733 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1734 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08001735 ggtt_vm->clear_range(ggtt_vm, hole_start,
1736 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001737 }
1738
1739 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08001740 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001741
Daniel Vetterfa76da32014-08-06 20:19:54 +02001742 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
1743 struct i915_hw_ppgtt *ppgtt;
1744
1745 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1746 if (!ppgtt)
1747 return -ENOMEM;
1748
1749 ret = __hw_ppgtt_init(dev, ppgtt);
1750 if (ret != 0)
1751 return ret;
1752
1753 dev_priv->mm.aliasing_ppgtt = ppgtt;
1754 }
1755
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001756 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001757}
1758
Ben Widawskyd7e50082012-12-18 10:31:25 -08001759void i915_gem_init_global_gtt(struct drm_device *dev)
1760{
1761 struct drm_i915_private *dev_priv = dev->dev_private;
1762 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001763
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001764 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001765 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001766
Ben Widawskye78891c2013-01-25 16:41:04 -08001767 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001768}
1769
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001770void i915_global_gtt_cleanup(struct drm_device *dev)
1771{
1772 struct drm_i915_private *dev_priv = dev->dev_private;
1773 struct i915_address_space *vm = &dev_priv->gtt.base;
1774
1775 if (drm_mm_initialized(&vm->mm)) {
1776 drm_mm_takedown(&vm->mm);
1777 list_del(&vm->global_link);
1778 }
1779
1780 vm->cleanup(vm);
1781}
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001782static int setup_scratch_page(struct drm_device *dev)
1783{
1784 struct drm_i915_private *dev_priv = dev->dev_private;
1785 struct page *page;
1786 dma_addr_t dma_addr;
1787
1788 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1789 if (page == NULL)
1790 return -ENOMEM;
1791 get_page(page);
1792 set_pages_uc(page, 1);
1793
1794#ifdef CONFIG_INTEL_IOMMU
1795 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1796 PCI_DMA_BIDIRECTIONAL);
1797 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1798 return -EINVAL;
1799#else
1800 dma_addr = page_to_phys(page);
1801#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001802 dev_priv->gtt.base.scratch.page = page;
1803 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001804
1805 return 0;
1806}
1807
1808static void teardown_scratch_page(struct drm_device *dev)
1809{
1810 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001811 struct page *page = dev_priv->gtt.base.scratch.page;
1812
1813 set_pages_wb(page, 1);
1814 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001815 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001816 put_page(page);
1817 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001818}
1819
1820static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1821{
1822 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1823 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1824 return snb_gmch_ctl << 20;
1825}
1826
Ben Widawsky9459d252013-11-03 16:53:55 -08001827static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1828{
1829 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1830 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1831 if (bdw_gmch_ctl)
1832 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07001833
1834#ifdef CONFIG_X86_32
1835 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
1836 if (bdw_gmch_ctl > 4)
1837 bdw_gmch_ctl = 4;
1838#endif
1839
Ben Widawsky9459d252013-11-03 16:53:55 -08001840 return bdw_gmch_ctl << 20;
1841}
1842
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001843static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
1844{
1845 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
1846 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
1847
1848 if (gmch_ctrl)
1849 return 1 << (20 + gmch_ctrl);
1850
1851 return 0;
1852}
1853
Ben Widawskybaa09f52013-01-24 13:49:57 -08001854static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001855{
1856 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1857 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1858 return snb_gmch_ctl << 25; /* 32 MB units */
1859}
1860
Ben Widawsky9459d252013-11-03 16:53:55 -08001861static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1862{
1863 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1864 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1865 return bdw_gmch_ctl << 25; /* 32 MB units */
1866}
1867
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001868static size_t chv_get_stolen_size(u16 gmch_ctrl)
1869{
1870 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
1871 gmch_ctrl &= SNB_GMCH_GMS_MASK;
1872
1873 /*
1874 * 0x0 to 0x10: 32MB increments starting at 0MB
1875 * 0x11 to 0x16: 4MB increments starting at 8MB
1876 * 0x17 to 0x1d: 4MB increments start at 36MB
1877 */
1878 if (gmch_ctrl < 0x11)
1879 return gmch_ctrl << 25;
1880 else if (gmch_ctrl < 0x17)
1881 return (gmch_ctrl - 0x11 + 2) << 22;
1882 else
1883 return (gmch_ctrl - 0x17 + 9) << 22;
1884}
1885
Ben Widawsky63340132013-11-04 19:32:22 -08001886static int ggtt_probe_common(struct drm_device *dev,
1887 size_t gtt_size)
1888{
1889 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001890 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08001891 int ret;
1892
1893 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001894 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08001895 (pci_resource_len(dev->pdev, 0) / 2);
1896
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001897 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08001898 if (!dev_priv->gtt.gsm) {
1899 DRM_ERROR("Failed to map the gtt page table\n");
1900 return -ENOMEM;
1901 }
1902
1903 ret = setup_scratch_page(dev);
1904 if (ret) {
1905 DRM_ERROR("Scratch setup failed\n");
1906 /* iounmap will also get called at remove, but meh */
1907 iounmap(dev_priv->gtt.gsm);
1908 }
1909
1910 return ret;
1911}
1912
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001913/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1914 * bits. When using advanced contexts each context stores its own PAT, but
1915 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001916static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001917{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001918 uint64_t pat;
1919
1920 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1921 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1922 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1923 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1924 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1925 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1926 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1927 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1928
1929 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1930 * write would work. */
1931 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1932 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1933}
1934
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001935static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
1936{
1937 uint64_t pat;
1938
1939 /*
1940 * Map WB on BDW to snooped on CHV.
1941 *
1942 * Only the snoop bit has meaning for CHV, the rest is
1943 * ignored.
1944 *
1945 * Note that the harware enforces snooping for all page
1946 * table accesses. The snoop bit is actually ignored for
1947 * PDEs.
1948 */
1949 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
1950 GEN8_PPAT(1, 0) |
1951 GEN8_PPAT(2, 0) |
1952 GEN8_PPAT(3, 0) |
1953 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
1954 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
1955 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
1956 GEN8_PPAT(7, CHV_PPAT_SNOOP);
1957
1958 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1959 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1960}
1961
Ben Widawsky63340132013-11-04 19:32:22 -08001962static int gen8_gmch_probe(struct drm_device *dev,
1963 size_t *gtt_total,
1964 size_t *stolen,
1965 phys_addr_t *mappable_base,
1966 unsigned long *mappable_end)
1967{
1968 struct drm_i915_private *dev_priv = dev->dev_private;
1969 unsigned int gtt_size;
1970 u16 snb_gmch_ctl;
1971 int ret;
1972
1973 /* TODO: We're not aware of mappable constraints on gen8 yet */
1974 *mappable_base = pci_resource_start(dev->pdev, 2);
1975 *mappable_end = pci_resource_len(dev->pdev, 2);
1976
1977 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1978 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1979
1980 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1981
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001982 if (IS_CHERRYVIEW(dev)) {
1983 *stolen = chv_get_stolen_size(snb_gmch_ctl);
1984 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
1985 } else {
1986 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1987 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
1988 }
Ben Widawsky63340132013-11-04 19:32:22 -08001989
Ben Widawskyd31eb102013-11-02 21:07:17 -07001990 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001991
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001992 if (IS_CHERRYVIEW(dev))
1993 chv_setup_private_ppat(dev_priv);
1994 else
1995 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001996
Ben Widawsky63340132013-11-04 19:32:22 -08001997 ret = ggtt_probe_common(dev, gtt_size);
1998
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001999 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2000 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08002001
2002 return ret;
2003}
2004
Ben Widawskybaa09f52013-01-24 13:49:57 -08002005static int gen6_gmch_probe(struct drm_device *dev,
2006 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002007 size_t *stolen,
2008 phys_addr_t *mappable_base,
2009 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002010{
2011 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002012 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002013 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002014 int ret;
2015
Ben Widawsky41907dd2013-02-08 11:32:47 -08002016 *mappable_base = pci_resource_start(dev->pdev, 2);
2017 *mappable_end = pci_resource_len(dev->pdev, 2);
2018
Ben Widawskybaa09f52013-01-24 13:49:57 -08002019 /* 64/512MB is the current min/max we actually know of, but this is just
2020 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002021 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08002022 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08002023 DRM_ERROR("Unknown GMADR size (%lx)\n",
2024 dev_priv->gtt.mappable_end);
2025 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002026 }
2027
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002028 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2029 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08002030 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002031
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07002032 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002033
Ben Widawsky63340132013-11-04 19:32:22 -08002034 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002035 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
2036
Ben Widawsky63340132013-11-04 19:32:22 -08002037 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002038
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002039 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2040 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002041
2042 return ret;
2043}
2044
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002045static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002046{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002047
2048 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002049
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002050 iounmap(gtt->gsm);
2051 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002052}
2053
2054static int i915_gmch_probe(struct drm_device *dev,
2055 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002056 size_t *stolen,
2057 phys_addr_t *mappable_base,
2058 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002059{
2060 struct drm_i915_private *dev_priv = dev->dev_private;
2061 int ret;
2062
Ben Widawskybaa09f52013-01-24 13:49:57 -08002063 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2064 if (!ret) {
2065 DRM_ERROR("failed to set up gmch\n");
2066 return -EIO;
2067 }
2068
Ben Widawsky41907dd2013-02-08 11:32:47 -08002069 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002070
2071 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002072 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002073
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002074 if (unlikely(dev_priv->gtt.do_idle_maps))
2075 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2076
Ben Widawskybaa09f52013-01-24 13:49:57 -08002077 return 0;
2078}
2079
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002080static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002081{
2082 intel_gmch_remove();
2083}
2084
2085int i915_gem_gtt_init(struct drm_device *dev)
2086{
2087 struct drm_i915_private *dev_priv = dev->dev_private;
2088 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002089 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002090
Ben Widawskybaa09f52013-01-24 13:49:57 -08002091 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002092 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002093 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002094 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002095 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002096 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002097 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002098 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002099 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002100 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002101 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002102 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002103 else if (INTEL_INFO(dev)->gen >= 7)
2104 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002105 else
Chris Wilson350ec882013-08-06 13:17:02 +01002106 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002107 } else {
2108 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2109 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002110 }
2111
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002112 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002113 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002114 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002115 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002116
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002117 gtt->base.dev = dev;
2118
Ben Widawskybaa09f52013-01-24 13:49:57 -08002119 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002120 DRM_INFO("Memory usable by graphics device = %zdM\n",
2121 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002122 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2123 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002124#ifdef CONFIG_INTEL_IOMMU
2125 if (intel_iommu_gfx_mapped)
2126 DRM_INFO("VT-d active for gfx access\n");
2127#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002128 /*
2129 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2130 * user's requested state against the hardware/driver capabilities. We
2131 * do this now so that we can print out any log messages once rather
2132 * than every time we check intel_enable_ppgtt().
2133 */
2134 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2135 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002136
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002137 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002138}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002139
2140static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2141 struct i915_address_space *vm)
2142{
2143 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2144 if (vma == NULL)
2145 return ERR_PTR(-ENOMEM);
2146
2147 INIT_LIST_HEAD(&vma->vma_link);
2148 INIT_LIST_HEAD(&vma->mm_list);
2149 INIT_LIST_HEAD(&vma->exec_list);
2150 vma->vm = vm;
2151 vma->obj = obj;
2152
2153 switch (INTEL_INFO(vm->dev)->gen) {
2154 case 8:
2155 case 7:
2156 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002157 if (i915_is_ggtt(vm)) {
2158 vma->unbind_vma = ggtt_unbind_vma;
2159 vma->bind_vma = ggtt_bind_vma;
2160 } else {
2161 vma->unbind_vma = ppgtt_unbind_vma;
2162 vma->bind_vma = ppgtt_bind_vma;
2163 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002164 break;
2165 case 5:
2166 case 4:
2167 case 3:
2168 case 2:
2169 BUG_ON(!i915_is_ggtt(vm));
2170 vma->unbind_vma = i915_ggtt_unbind_vma;
2171 vma->bind_vma = i915_ggtt_bind_vma;
2172 break;
2173 default:
2174 BUG();
2175 }
2176
2177 /* Keep GGTT vmas first to make debug easier */
2178 if (i915_is_ggtt(vm))
2179 list_add(&vma->vma_link, &obj->vma_list);
2180 else
2181 list_add_tail(&vma->vma_link, &obj->vma_list);
2182
2183 return vma;
2184}
2185
2186struct i915_vma *
2187i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2188 struct i915_address_space *vm)
2189{
2190 struct i915_vma *vma;
2191
2192 vma = i915_gem_obj_to_vma(obj, vm);
2193 if (!vma)
2194 vma = __i915_gem_vma_create(obj, vm);
2195
Daniel Vetter841cd772014-08-06 15:04:48 +02002196 if (!i915_is_ggtt(vm))
2197 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Michel Thierryb9d06dd2014-08-06 15:04:44 +02002198
Ben Widawsky6f65e292013-12-06 14:10:56 -08002199 return vma;
2200}