blob: 4498a068a5a75ae7e78be534349d8edf4c9021b3 [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{
Chris Wilson1893a712014-09-19 11:56:27 +010038 bool has_aliasing_ppgtt;
39 bool has_full_ppgtt;
40
41 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
42 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
43 if (IS_GEN8(dev))
44 has_full_ppgtt = false; /* XXX why? */
45
46 if (enable_ppgtt == 0 || !has_aliasing_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +020047 return 0;
48
49 if (enable_ppgtt == 1)
50 return 1;
51
Chris Wilson1893a712014-09-19 11:56:27 +010052 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +020053 return 2;
54
Daniel Vetter93a25a92014-03-06 09:40:43 +010055#ifdef CONFIG_INTEL_IOMMU
56 /* Disable ppgtt on SNB if VT-d is on. */
57 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
58 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +020059 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +010060 }
61#endif
62
Jesse Barnes62942ed2014-06-13 09:28:33 -070063 /* Early VLV doesn't have this */
Ville Syrjäläca2aed6c2014-06-28 02:03:56 +030064 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
65 dev->pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -070066 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
67 return 0;
68 }
69
Daniel Vettercacc6c82014-10-22 11:18:51 +020070 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +010071}
72
Ben Widawskyfbe5d362013-11-04 19:56:49 -080073
Ben Widawsky6f65e292013-12-06 14:10:56 -080074static void ppgtt_bind_vma(struct i915_vma *vma,
75 enum i915_cache_level cache_level,
76 u32 flags);
77static void ppgtt_unbind_vma(struct i915_vma *vma);
78
Ben Widawsky94ec8f62013-11-02 21:07:18 -070079static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
80 enum i915_cache_level level,
81 bool valid)
82{
83 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
84 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -030085
86 switch (level) {
87 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -080088 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -030089 break;
90 case I915_CACHE_WT:
91 pte |= PPAT_DISPLAY_ELLC_INDEX;
92 break;
93 default:
94 pte |= PPAT_CACHED_INDEX;
95 break;
96 }
97
Ben Widawsky94ec8f62013-11-02 21:07:18 -070098 return pte;
99}
100
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800101static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
102 dma_addr_t addr,
103 enum i915_cache_level level)
104{
105 gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
106 pde |= addr;
107 if (level != I915_CACHE_NONE)
108 pde |= PPAT_CACHED_PDE_INDEX;
109 else
110 pde |= PPAT_UNCACHED_INDEX;
111 return pde;
112}
113
Chris Wilson350ec882013-08-06 13:17:02 +0100114static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700115 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530116 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700117{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700118 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700119 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700120
121 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100122 case I915_CACHE_L3_LLC:
123 case I915_CACHE_LLC:
124 pte |= GEN6_PTE_CACHE_LLC;
125 break;
126 case I915_CACHE_NONE:
127 pte |= GEN6_PTE_UNCACHED;
128 break;
129 default:
130 WARN_ON(1);
131 }
132
133 return pte;
134}
135
136static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700137 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530138 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100139{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700140 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100141 pte |= GEN6_PTE_ADDR_ENCODE(addr);
142
143 switch (level) {
144 case I915_CACHE_L3_LLC:
145 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700146 break;
147 case I915_CACHE_LLC:
148 pte |= GEN6_PTE_CACHE_LLC;
149 break;
150 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700151 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700152 break;
153 default:
Chris Wilson350ec882013-08-06 13:17:02 +0100154 WARN_ON(1);
Ben Widawskye7210c32012-10-19 09:33:22 -0700155 }
156
Ben Widawsky54d12522012-09-24 16:44:32 -0700157 return pte;
158}
159
Ben Widawsky80a74f72013-06-27 16:30:19 -0700160static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700161 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530162 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700163{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700164 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700165 pte |= GEN6_PTE_ADDR_ENCODE(addr);
166
167 /* Mark the page as writeable. Other platforms don't have a
168 * setting for read-only/writable, so this matches that behavior.
169 */
Akash Goel24f3a8c2014-06-17 10:59:42 +0530170 if (!(flags & PTE_READ_ONLY))
171 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700172
173 if (level != I915_CACHE_NONE)
174 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
175
176 return pte;
177}
178
Ben Widawsky80a74f72013-06-27 16:30:19 -0700179static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700180 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530181 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700182{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700183 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700184 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700185
186 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700187 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700188
189 return pte;
190}
191
Ben Widawsky4d15c142013-07-04 11:02:06 -0700192static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700193 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530194 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700195{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700196 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700197 pte |= HSW_PTE_ADDR_ENCODE(addr);
198
Chris Wilson651d7942013-08-08 14:41:10 +0100199 switch (level) {
200 case I915_CACHE_NONE:
201 break;
202 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000203 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100204 break;
205 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000206 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100207 break;
208 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700209
210 return pte;
211}
212
Ben Widawsky94e409c2013-11-04 22:29:36 -0800213/* Broadwell Page Directory Pointer Descriptors */
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100214static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100215 uint64_t val)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800216{
217 int ret;
218
219 BUG_ON(entry >= 4);
220
221 ret = intel_ring_begin(ring, 6);
222 if (ret)
223 return ret;
224
225 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
226 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
227 intel_ring_emit(ring, (u32)(val >> 32));
228 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
229 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
230 intel_ring_emit(ring, (u32)(val));
231 intel_ring_advance(ring);
232
233 return 0;
234}
235
Ben Widawskyeeb94882013-12-06 14:11:10 -0800236static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100237 struct intel_engine_cs *ring)
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];
McAulay, Alistair6689c162014-08-15 18:51:35 +0100246 ret = gen8_write_pdp(ring, i, addr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800247 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,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100711 struct intel_engine_cs *ring)
Ben Widawsky90252e52013-12-06 14:11:12 -0800712{
Ben Widawsky90252e52013-12-06 14:11:12 -0800713 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700714
Ben Widawsky90252e52013-12-06 14:11:12 -0800715 /* NB: TLBs must be flushed and invalidated before a switch */
716 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
717 if (ret)
718 return ret;
719
720 ret = intel_ring_begin(ring, 6);
721 if (ret)
722 return ret;
723
724 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
725 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
726 intel_ring_emit(ring, PP_DIR_DCLV_2G);
727 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
728 intel_ring_emit(ring, get_pd_offset(ppgtt));
729 intel_ring_emit(ring, MI_NOOP);
730 intel_ring_advance(ring);
731
732 return 0;
733}
734
Ben Widawsky48a10382013-12-06 14:11:11 -0800735static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100736 struct intel_engine_cs *ring)
Ben Widawsky48a10382013-12-06 14:11:11 -0800737{
Ben Widawsky48a10382013-12-06 14:11:11 -0800738 int ret;
739
Ben Widawsky48a10382013-12-06 14:11:11 -0800740 /* NB: TLBs must be flushed and invalidated before a switch */
741 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
742 if (ret)
743 return ret;
744
745 ret = intel_ring_begin(ring, 6);
746 if (ret)
747 return ret;
748
749 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
750 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
751 intel_ring_emit(ring, PP_DIR_DCLV_2G);
752 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
753 intel_ring_emit(ring, get_pd_offset(ppgtt));
754 intel_ring_emit(ring, MI_NOOP);
755 intel_ring_advance(ring);
756
Ben Widawsky90252e52013-12-06 14:11:12 -0800757 /* XXX: RCS is the only one to auto invalidate the TLBs? */
758 if (ring->id != RCS) {
759 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
760 if (ret)
761 return ret;
762 }
763
Ben Widawsky48a10382013-12-06 14:11:11 -0800764 return 0;
765}
766
Ben Widawskyeeb94882013-12-06 14:11:10 -0800767static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100768 struct intel_engine_cs *ring)
Ben Widawskyeeb94882013-12-06 14:11:10 -0800769{
770 struct drm_device *dev = ppgtt->base.dev;
771 struct drm_i915_private *dev_priv = dev->dev_private;
772
Ben Widawsky48a10382013-12-06 14:11:11 -0800773
Ben Widawskyeeb94882013-12-06 14:11:10 -0800774 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
775 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
776
777 POSTING_READ(RING_PP_DIR_DCLV(ring));
778
779 return 0;
780}
781
Daniel Vetter82460d92014-08-06 20:19:53 +0200782static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -0800783{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800784 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100785 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +0200786 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -0800787
788 for_each_ring(ring, dev_priv, j) {
789 I915_WRITE(RING_MODE_GEN7(ring),
790 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -0800791 }
Ben Widawskyeeb94882013-12-06 14:11:10 -0800792}
793
Daniel Vetter82460d92014-08-06 20:19:53 +0200794static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800795{
Jani Nikula50227e12014-03-31 14:27:21 +0300796 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100797 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800798 uint32_t ecochk, ecobits;
799 int i;
800
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800801 ecobits = I915_READ(GAC_ECO_BITS);
802 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
803
804 ecochk = I915_READ(GAM_ECOCHK);
805 if (IS_HASWELL(dev)) {
806 ecochk |= ECOCHK_PPGTT_WB_HSW;
807 } else {
808 ecochk |= ECOCHK_PPGTT_LLC_IVB;
809 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
810 }
811 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800812
Ben Widawsky61973492013-04-08 18:43:54 -0700813 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800814 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800815 I915_WRITE(RING_MODE_GEN7(ring),
816 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700817 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800818}
819
Daniel Vetter82460d92014-08-06 20:19:53 +0200820static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -0700821{
Jani Nikula50227e12014-03-31 14:27:21 +0300822 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800823 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -0700824
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800825 ecobits = I915_READ(GAC_ECO_BITS);
826 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
827 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700828
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800829 gab_ctl = I915_READ(GAB_CTL);
830 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -0700831
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800832 ecochk = I915_READ(GAM_ECOCHK);
833 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -0700834
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800835 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700836}
837
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100838/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700839static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800840 uint64_t start,
841 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -0700842 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100843{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700844 struct i915_hw_ppgtt *ppgtt =
845 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700846 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -0800847 unsigned first_entry = start >> PAGE_SHIFT;
848 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100849 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100850 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
851 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100852
Akash Goel24f3a8c2014-06-17 10:59:42 +0530853 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100854
Daniel Vetter7bddb012012-02-09 17:15:47 +0100855 while (num_entries) {
856 last_pte = first_pte + num_entries;
857 if (last_pte > I915_PPGTT_PT_ENTRIES)
858 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100859
Daniel Vettera15326a2013-03-19 23:48:39 +0100860 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100861
862 for (i = first_pte; i < last_pte; i++)
863 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100864
865 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100866
Daniel Vetter7bddb012012-02-09 17:15:47 +0100867 num_entries -= last_pte - first_pte;
868 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +0100869 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100870 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100871}
872
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700873static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -0800874 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800875 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530876 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -0800877{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700878 struct i915_hw_ppgtt *ppgtt =
879 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -0700880 gen6_gtt_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -0800881 unsigned first_entry = start >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +0100882 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +0200883 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
884 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800885
Chris Wilsoncc797142013-12-31 15:50:30 +0000886 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +0200887 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +0000888 if (pt_vaddr == NULL)
889 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800890
Chris Wilsoncc797142013-12-31 15:50:30 +0000891 pt_vaddr[act_pte] =
892 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +0530893 cache_level, true, flags);
894
Imre Deak6e995e22013-02-18 19:28:04 +0200895 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
896 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +0000897 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +0100898 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +0200899 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -0800900 }
Daniel Vetterdef886c2013-01-24 14:44:56 -0800901 }
Chris Wilsoncc797142013-12-31 15:50:30 +0000902 if (pt_vaddr)
903 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -0800904}
905
Ben Widawskya00d8252014-02-19 22:05:48 -0800906static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100907{
Daniel Vetter3440d262013-01-24 13:49:56 -0800908 int i;
909
910 if (ppgtt->pt_dma_addr) {
911 for (i = 0; i < ppgtt->num_pd_entries; i++)
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700912 pci_unmap_page(ppgtt->base.dev->pdev,
Daniel Vetter3440d262013-01-24 13:49:56 -0800913 ppgtt->pt_dma_addr[i],
914 4096, PCI_DMA_BIDIRECTIONAL);
915 }
Ben Widawskya00d8252014-02-19 22:05:48 -0800916}
917
918static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
919{
920 int i;
Daniel Vetter3440d262013-01-24 13:49:56 -0800921
922 kfree(ppgtt->pt_dma_addr);
923 for (i = 0; i < ppgtt->num_pd_entries; i++)
924 __free_page(ppgtt->pt_pages[i]);
925 kfree(ppgtt->pt_pages);
Daniel Vetter3440d262013-01-24 13:49:56 -0800926}
927
Ben Widawskya00d8252014-02-19 22:05:48 -0800928static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
929{
930 struct i915_hw_ppgtt *ppgtt =
931 container_of(vm, struct i915_hw_ppgtt, base);
932
Ben Widawskya00d8252014-02-19 22:05:48 -0800933 drm_mm_remove_node(&ppgtt->node);
934
935 gen6_ppgtt_unmap_pages(ppgtt);
936 gen6_ppgtt_free(ppgtt);
937}
938
Ben Widawskyb1465202014-02-19 22:05:49 -0800939static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -0800940{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700941 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100942 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -0800943 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -0800944 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100945
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800946 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
947 * allocator works in address space sizes, so it's multiplied by page
948 * size. We allocate at the top of the GTT to avoid fragmentation.
949 */
950 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -0800951alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800952 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
953 &ppgtt->node, GEN6_PD_SIZE,
954 GEN6_PD_ALIGN, 0,
955 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -0700956 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800957 if (ret == -ENOSPC && !retried) {
958 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
959 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +0200960 I915_CACHE_NONE,
961 0, dev_priv->gtt.base.total,
962 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -0800963 if (ret)
964 return ret;
965
966 retried = true;
967 goto alloc;
968 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -0800969
970 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
971 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100972
Ben Widawsky6670a5a2013-06-27 16:30:04 -0700973 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawskyb1465202014-02-19 22:05:49 -0800974 return ret;
975}
976
977static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
978{
979 int i;
980
981 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
982 GFP_KERNEL);
983
984 if (!ppgtt->pt_pages)
985 return -ENOMEM;
986
987 for (i = 0; i < ppgtt->num_pd_entries; i++) {
988 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
989 if (!ppgtt->pt_pages[i]) {
990 gen6_ppgtt_free(ppgtt);
991 return -ENOMEM;
992 }
993 }
994
995 return 0;
996}
997
998static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
999{
1000 int ret;
1001
1002 ret = gen6_ppgtt_allocate_page_directories(ppgtt);
1003 if (ret)
1004 return ret;
1005
1006 ret = gen6_ppgtt_allocate_page_tables(ppgtt);
1007 if (ret) {
1008 drm_mm_remove_node(&ppgtt->node);
1009 return ret;
1010 }
1011
1012 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
1013 GFP_KERNEL);
1014 if (!ppgtt->pt_dma_addr) {
1015 drm_mm_remove_node(&ppgtt->node);
1016 gen6_ppgtt_free(ppgtt);
1017 return -ENOMEM;
1018 }
1019
1020 return 0;
1021}
1022
1023static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
1024{
1025 struct drm_device *dev = ppgtt->base.dev;
1026 int i;
1027
1028 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1029 dma_addr_t pt_addr;
1030
1031 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
1032 PCI_DMA_BIDIRECTIONAL);
1033
1034 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1035 gen6_ppgtt_unmap_pages(ppgtt);
1036 return -EIO;
1037 }
1038
1039 ppgtt->pt_dma_addr[i] = pt_addr;
1040 }
1041
1042 return 0;
1043}
1044
1045static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1046{
1047 struct drm_device *dev = ppgtt->base.dev;
1048 struct drm_i915_private *dev_priv = dev->dev_private;
1049 int ret;
1050
1051 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001052 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001053 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001054 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001055 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001056 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001057 ppgtt->switch_mm = gen7_mm_switch;
1058 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001059 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001060
1061 ret = gen6_ppgtt_alloc(ppgtt);
1062 if (ret)
1063 return ret;
1064
1065 ret = gen6_ppgtt_setup_page_tables(ppgtt);
1066 if (ret) {
1067 gen6_ppgtt_free(ppgtt);
1068 return ret;
1069 }
1070
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001071 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1072 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1073 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001074 ppgtt->base.start = 0;
Ben Widawsky5a6c93f2014-03-08 11:58:17 -08001075 ppgtt->base.total = ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001076 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001077
Ben Widawskyb1465202014-02-19 22:05:49 -08001078 ppgtt->pd_offset =
1079 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001080
Ben Widawsky782f1492014-02-20 11:50:33 -08001081 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001082
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001083 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1084 ppgtt->node.size >> 20,
1085 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001086
Daniel Vetterfa76da32014-08-06 20:19:54 +02001087 gen6_write_pdes(ppgtt);
1088 DRM_DEBUG("Adding PPGTT at offset %x\n",
1089 ppgtt->pd_offset << 10);
1090
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001091 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001092}
1093
Daniel Vetterfa76da32014-08-06 20:19:54 +02001094static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001095{
1096 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter3440d262013-01-24 13:49:56 -08001097
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001098 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001099 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001100
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001101 if (INTEL_INFO(dev)->gen < 8)
Daniel Vetterfa76da32014-08-06 20:19:54 +02001102 return gen6_ppgtt_init(ppgtt);
Damien Lespiau3fdcf802014-01-23 13:59:49 +00001103 else if (IS_GEN8(dev) || IS_GEN9(dev))
Daniel Vetterfa76da32014-08-06 20:19:54 +02001104 return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001105 else
1106 BUG();
Daniel Vetterfa76da32014-08-06 20:19:54 +02001107}
1108int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1109{
1110 struct drm_i915_private *dev_priv = dev->dev_private;
1111 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001112
Daniel Vetterfa76da32014-08-06 20:19:54 +02001113 ret = __hw_ppgtt_init(dev, ppgtt);
1114 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001115 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001116 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1117 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001118 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001119 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001120
1121 return ret;
1122}
1123
Daniel Vetter82460d92014-08-06 20:19:53 +02001124int i915_ppgtt_init_hw(struct drm_device *dev)
1125{
1126 struct drm_i915_private *dev_priv = dev->dev_private;
1127 struct intel_engine_cs *ring;
1128 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1129 int i, ret = 0;
1130
Thomas Daniel671b50132014-08-20 16:24:50 +01001131 /* In the case of execlists, PPGTT is enabled by the context descriptor
1132 * and the PDPs are contained within the context itself. We don't
1133 * need to do anything here. */
1134 if (i915.enable_execlists)
1135 return 0;
1136
Daniel Vetter82460d92014-08-06 20:19:53 +02001137 if (!USES_PPGTT(dev))
1138 return 0;
1139
1140 if (IS_GEN6(dev))
1141 gen6_ppgtt_enable(dev);
1142 else if (IS_GEN7(dev))
1143 gen7_ppgtt_enable(dev);
1144 else if (INTEL_INFO(dev)->gen >= 8)
1145 gen8_ppgtt_enable(dev);
1146 else
1147 WARN_ON(1);
1148
1149 if (ppgtt) {
1150 for_each_ring(ring, dev_priv, i) {
McAulay, Alistair6689c162014-08-15 18:51:35 +01001151 ret = ppgtt->switch_mm(ppgtt, ring);
Daniel Vetter82460d92014-08-06 20:19:53 +02001152 if (ret != 0)
1153 return ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001154 }
1155 }
1156
1157 return ret;
1158}
Daniel Vetter4d884702014-08-06 15:04:47 +02001159struct i915_hw_ppgtt *
1160i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1161{
1162 struct i915_hw_ppgtt *ppgtt;
1163 int ret;
1164
1165 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1166 if (!ppgtt)
1167 return ERR_PTR(-ENOMEM);
1168
1169 ret = i915_ppgtt_init(dev, ppgtt);
1170 if (ret) {
1171 kfree(ppgtt);
1172 return ERR_PTR(ret);
1173 }
1174
1175 ppgtt->file_priv = fpriv;
1176
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001177 trace_i915_ppgtt_create(&ppgtt->base);
1178
Daniel Vetter4d884702014-08-06 15:04:47 +02001179 return ppgtt;
1180}
1181
Daniel Vetteree960be2014-08-06 15:04:45 +02001182void i915_ppgtt_release(struct kref *kref)
1183{
1184 struct i915_hw_ppgtt *ppgtt =
1185 container_of(kref, struct i915_hw_ppgtt, ref);
1186
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001187 trace_i915_ppgtt_release(&ppgtt->base);
1188
Daniel Vetteree960be2014-08-06 15:04:45 +02001189 /* vmas should already be unbound */
1190 WARN_ON(!list_empty(&ppgtt->base.active_list));
1191 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1192
Daniel Vetter19dd1202014-08-06 15:04:55 +02001193 list_del(&ppgtt->base.global_link);
1194 drm_mm_takedown(&ppgtt->base.mm);
1195
Daniel Vetteree960be2014-08-06 15:04:45 +02001196 ppgtt->base.cleanup(&ppgtt->base);
1197 kfree(ppgtt);
1198}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001199
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001200static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001201ppgtt_bind_vma(struct i915_vma *vma,
1202 enum i915_cache_level cache_level,
1203 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001204{
Akash Goel24f3a8c2014-06-17 10:59:42 +05301205 /* Currently applicable only to VLV */
1206 if (vma->obj->gt_ro)
1207 flags |= PTE_READ_ONLY;
1208
Ben Widawsky782f1492014-02-20 11:50:33 -08001209 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301210 cache_level, flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001211}
1212
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001213static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001214{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001215 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001216 vma->node.start,
1217 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001218 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001219}
1220
Ben Widawskya81cc002013-01-18 12:30:31 -08001221extern int intel_iommu_gfx_mapped;
1222/* Certain Gen5 chipsets require require idling the GPU before
1223 * unmapping anything from the GTT when VT-d is enabled.
1224 */
1225static inline bool needs_idle_maps(struct drm_device *dev)
1226{
1227#ifdef CONFIG_INTEL_IOMMU
1228 /* Query intel_iommu to see if we need the workaround. Presumably that
1229 * was loaded first.
1230 */
1231 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1232 return true;
1233#endif
1234 return false;
1235}
1236
Ben Widawsky5c042282011-10-17 15:51:55 -07001237static bool do_idling(struct drm_i915_private *dev_priv)
1238{
1239 bool ret = dev_priv->mm.interruptible;
1240
Ben Widawskya81cc002013-01-18 12:30:31 -08001241 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001242 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001243 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001244 DRM_ERROR("Couldn't idle GPU\n");
1245 /* Wait a bit, in hopes it avoids the hang */
1246 udelay(10);
1247 }
1248 }
1249
1250 return ret;
1251}
1252
1253static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1254{
Ben Widawskya81cc002013-01-18 12:30:31 -08001255 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001256 dev_priv->mm.interruptible = interruptible;
1257}
1258
Ben Widawsky828c7902013-10-16 09:21:30 -07001259void i915_check_and_clear_faults(struct drm_device *dev)
1260{
1261 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001262 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07001263 int i;
1264
1265 if (INTEL_INFO(dev)->gen < 6)
1266 return;
1267
1268 for_each_ring(ring, dev_priv, i) {
1269 u32 fault_reg;
1270 fault_reg = I915_READ(RING_FAULT_REG(ring));
1271 if (fault_reg & RING_FAULT_VALID) {
1272 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02001273 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07001274 "\tAddress space: %s\n"
1275 "\tSource ID: %d\n"
1276 "\tType: %d\n",
1277 fault_reg & PAGE_MASK,
1278 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1279 RING_FAULT_SRCID(fault_reg),
1280 RING_FAULT_FAULT_TYPE(fault_reg));
1281 I915_WRITE(RING_FAULT_REG(ring),
1282 fault_reg & ~RING_FAULT_VALID);
1283 }
1284 }
1285 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1286}
1287
Chris Wilson91e56492014-09-25 10:13:12 +01001288static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
1289{
1290 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
1291 intel_gtt_chipset_flush();
1292 } else {
1293 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1294 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1295 }
1296}
1297
Ben Widawsky828c7902013-10-16 09:21:30 -07001298void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1299{
1300 struct drm_i915_private *dev_priv = dev->dev_private;
1301
1302 /* Don't bother messing with faults pre GEN6 as we have little
1303 * documentation supporting that it's a good idea.
1304 */
1305 if (INTEL_INFO(dev)->gen < 6)
1306 return;
1307
1308 i915_check_and_clear_faults(dev);
1309
1310 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001311 dev_priv->gtt.base.start,
1312 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001313 true);
Chris Wilson91e56492014-09-25 10:13:12 +01001314
1315 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07001316}
1317
Daniel Vetter76aaf222010-11-05 22:23:30 +01001318void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1319{
1320 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001321 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001322 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001323
Ben Widawsky828c7902013-10-16 09:21:30 -07001324 i915_check_and_clear_faults(dev);
1325
Chris Wilsonbee4a182011-01-21 10:54:32 +00001326 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001327 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001328 dev_priv->gtt.base.start,
1329 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001330 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001331
Ben Widawsky35c20a62013-05-31 11:28:48 -07001332 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001333 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1334 &dev_priv->gtt.base);
1335 if (!vma)
1336 continue;
1337
Chris Wilson2c225692013-08-09 12:26:45 +01001338 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001339 /* The bind_vma code tries to be smart about tracking mappings.
1340 * Unfortunately above, we've just wiped out the mappings
1341 * without telling our object about it. So we need to fake it.
1342 */
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001343 vma->bound &= ~GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001344 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001345 }
1346
Ben Widawsky80da2162013-12-06 14:11:17 -08001347
Ben Widawskya2319c02014-03-18 16:09:37 -07001348 if (INTEL_INFO(dev)->gen >= 8) {
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001349 if (IS_CHERRYVIEW(dev))
1350 chv_setup_private_ppat(dev_priv);
1351 else
1352 bdw_setup_private_ppat(dev_priv);
1353
Ben Widawsky80da2162013-12-06 14:11:17 -08001354 return;
Ben Widawskya2319c02014-03-18 16:09:37 -07001355 }
Ben Widawsky80da2162013-12-06 14:11:17 -08001356
1357 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1358 /* TODO: Perhaps it shouldn't be gen6 specific */
1359 if (i915_is_ggtt(vm)) {
1360 if (dev_priv->mm.aliasing_ppgtt)
1361 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1362 continue;
1363 }
1364
1365 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001366 }
1367
Chris Wilson91e56492014-09-25 10:13:12 +01001368 i915_ggtt_flush(dev_priv);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001369}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001370
Daniel Vetter74163902012-02-15 23:50:21 +01001371int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001372{
Chris Wilson9da3da62012-06-01 15:20:22 +01001373 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001374 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001375
1376 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1377 obj->pages->sgl, obj->pages->nents,
1378 PCI_DMA_BIDIRECTIONAL))
1379 return -ENOSPC;
1380
1381 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001382}
1383
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001384static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1385{
1386#ifdef writeq
1387 writeq(pte, addr);
1388#else
1389 iowrite32((u32)pte, addr);
1390 iowrite32(pte >> 32, addr + 4);
1391#endif
1392}
1393
1394static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1395 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001396 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301397 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001398{
1399 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001400 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001401 gen8_gtt_pte_t __iomem *gtt_entries =
1402 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1403 int i = 0;
1404 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001405 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001406
1407 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1408 addr = sg_dma_address(sg_iter.sg) +
1409 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1410 gen8_set_pte(&gtt_entries[i],
1411 gen8_pte_encode(addr, level, true));
1412 i++;
1413 }
1414
1415 /*
1416 * XXX: This serves as a posting read to make sure that the PTE has
1417 * actually been updated. There is some concern that even though
1418 * registers and PTEs are within the same BAR that they are potentially
1419 * of NUMA access patterns. Therefore, even with the way we assume
1420 * hardware should work, we must keep this posting read for paranoia.
1421 */
1422 if (i != 0)
1423 WARN_ON(readq(&gtt_entries[i-1])
1424 != gen8_pte_encode(addr, level, true));
1425
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001426 /* This next bit makes the above posting read even more important. We
1427 * want to flush the TLBs only after we're certain all the PTE updates
1428 * have finished.
1429 */
1430 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1431 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001432}
1433
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001434/*
1435 * Binds an object into the global gtt with the specified cache level. The object
1436 * will be accessible to the GPU via commands whose operands reference offsets
1437 * within the global GTT as well as accessible by the GPU through the GMADR
1438 * mapped BAR (dev_priv->mm.gtt->gtt).
1439 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001440static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001441 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001442 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301443 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001444{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001445 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001446 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001447 gen6_gtt_pte_t __iomem *gtt_entries =
1448 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001449 int i = 0;
1450 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001451 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001452
Imre Deak6e995e22013-02-18 19:28:04 +02001453 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001454 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301455 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001456 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001457 }
1458
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001459 /* XXX: This serves as a posting read to make sure that the PTE has
1460 * actually been updated. There is some concern that even though
1461 * registers and PTEs are within the same BAR that they are potentially
1462 * of NUMA access patterns. Therefore, even with the way we assume
1463 * hardware should work, we must keep this posting read for paranoia.
1464 */
Pavel Machek57007df2014-07-28 13:20:58 +02001465 if (i != 0) {
1466 unsigned long gtt = readl(&gtt_entries[i-1]);
1467 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1468 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001469
1470 /* This next bit makes the above posting read even more important. We
1471 * want to flush the TLBs only after we're certain all the PTE updates
1472 * have finished.
1473 */
1474 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1475 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001476}
1477
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001478static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001479 uint64_t start,
1480 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001481 bool use_scratch)
1482{
1483 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001484 unsigned first_entry = start >> PAGE_SHIFT;
1485 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001486 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1487 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1488 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1489 int i;
1490
1491 if (WARN(num_entries > max_entries,
1492 "First entry = %d; Num entries = %d (max=%d)\n",
1493 first_entry, num_entries, max_entries))
1494 num_entries = max_entries;
1495
1496 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1497 I915_CACHE_LLC,
1498 use_scratch);
1499 for (i = 0; i < num_entries; i++)
1500 gen8_set_pte(&gtt_base[i], scratch_pte);
1501 readl(gtt_base);
1502}
1503
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001504static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001505 uint64_t start,
1506 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001507 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001508{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001509 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001510 unsigned first_entry = start >> PAGE_SHIFT;
1511 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001512 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1513 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001514 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001515 int i;
1516
1517 if (WARN(num_entries > max_entries,
1518 "First entry = %d; Num entries = %d (max=%d)\n",
1519 first_entry, num_entries, max_entries))
1520 num_entries = max_entries;
1521
Akash Goel24f3a8c2014-06-17 10:59:42 +05301522 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07001523
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001524 for (i = 0; i < num_entries; i++)
1525 iowrite32(scratch_pte, &gtt_base[i]);
1526 readl(gtt_base);
1527}
1528
Ben Widawsky6f65e292013-12-06 14:10:56 -08001529
1530static void i915_ggtt_bind_vma(struct i915_vma *vma,
1531 enum i915_cache_level cache_level,
1532 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001533{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001534 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001535 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1536 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1537
Ben Widawsky6f65e292013-12-06 14:10:56 -08001538 BUG_ON(!i915_is_ggtt(vma->vm));
1539 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001540 vma->bound = GLOBAL_BIND;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001541}
1542
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001543static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001544 uint64_t start,
1545 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001546 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001547{
Ben Widawsky782f1492014-02-20 11:50:33 -08001548 unsigned first_entry = start >> PAGE_SHIFT;
1549 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001550 intel_gtt_clear_range(first_entry, num_entries);
1551}
1552
Ben Widawsky6f65e292013-12-06 14:10:56 -08001553static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001554{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001555 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1556 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001557
Ben Widawsky6f65e292013-12-06 14:10:56 -08001558 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001559 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001560 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001561}
1562
Ben Widawsky6f65e292013-12-06 14:10:56 -08001563static void ggtt_bind_vma(struct i915_vma *vma,
1564 enum i915_cache_level cache_level,
1565 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001566{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001567 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001568 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001569 struct drm_i915_gem_object *obj = vma->obj;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001570
Akash Goel24f3a8c2014-06-17 10:59:42 +05301571 /* Currently applicable only to VLV */
1572 if (obj->gt_ro)
1573 flags |= PTE_READ_ONLY;
1574
Ben Widawsky6f65e292013-12-06 14:10:56 -08001575 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1576 * or we have a global mapping already but the cacheability flags have
1577 * changed, set the global PTEs.
1578 *
1579 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1580 * instead if none of the above hold true.
1581 *
1582 * NB: A global mapping should only be needed for special regions like
1583 * "gtt mappable", SNB errata, or if specified via special execbuf
1584 * flags. At all other times, the GPU will use the aliasing PPGTT.
1585 */
1586 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001587 if (!(vma->bound & GLOBAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08001588 (cache_level != obj->cache_level)) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001589 vma->vm->insert_entries(vma->vm, obj->pages,
1590 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301591 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001592 vma->bound |= GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001593 }
1594 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001595
Ben Widawsky6f65e292013-12-06 14:10:56 -08001596 if (dev_priv->mm.aliasing_ppgtt &&
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001597 (!(vma->bound & LOCAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08001598 (cache_level != obj->cache_level))) {
1599 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1600 appgtt->base.insert_entries(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001601 vma->obj->pages,
1602 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301603 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001604 vma->bound |= LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001605 }
1606}
1607
1608static void ggtt_unbind_vma(struct i915_vma *vma)
1609{
1610 struct drm_device *dev = vma->vm->dev;
1611 struct drm_i915_private *dev_priv = dev->dev_private;
1612 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001613
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001614 if (vma->bound & GLOBAL_BIND) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001615 vma->vm->clear_range(vma->vm,
1616 vma->node.start,
1617 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001618 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001619 vma->bound &= ~GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001620 }
1621
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001622 if (vma->bound & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001623 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1624 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001625 vma->node.start,
1626 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001627 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001628 vma->bound &= ~LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001629 }
Daniel Vetter74163902012-02-15 23:50:21 +01001630}
1631
1632void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1633{
Ben Widawsky5c042282011-10-17 15:51:55 -07001634 struct drm_device *dev = obj->base.dev;
1635 struct drm_i915_private *dev_priv = dev->dev_private;
1636 bool interruptible;
1637
1638 interruptible = do_idling(dev_priv);
1639
Chris Wilson9da3da62012-06-01 15:20:22 +01001640 if (!obj->has_dma_mapping)
1641 dma_unmap_sg(&dev->pdev->dev,
1642 obj->pages->sgl, obj->pages->nents,
1643 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001644
1645 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001646}
Daniel Vetter644ec022012-03-26 09:45:40 +02001647
Chris Wilson42d6ab42012-07-26 11:49:32 +01001648static void i915_gtt_color_adjust(struct drm_mm_node *node,
1649 unsigned long color,
1650 unsigned long *start,
1651 unsigned long *end)
1652{
1653 if (node->color != color)
1654 *start += 4096;
1655
1656 if (!list_empty(&node->node_list)) {
1657 node = list_entry(node->node_list.next,
1658 struct drm_mm_node,
1659 node_list);
1660 if (node->allocated && node->color != color)
1661 *end -= 4096;
1662 }
1663}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001664
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001665int i915_gem_setup_global_gtt(struct drm_device *dev,
1666 unsigned long start,
1667 unsigned long mappable_end,
1668 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001669{
Ben Widawskye78891c2013-01-25 16:41:04 -08001670 /* Let GEM Manage all of the aperture.
1671 *
1672 * However, leave one page at the end still bound to the scratch page.
1673 * There are a number of places where the hardware apparently prefetches
1674 * past the end of the object, and we've seen multiple hangs with the
1675 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1676 * aperture. One page should be enough to keep any prefetching inside
1677 * of the aperture.
1678 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001679 struct drm_i915_private *dev_priv = dev->dev_private;
1680 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001681 struct drm_mm_node *entry;
1682 struct drm_i915_gem_object *obj;
1683 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02001684 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02001685
Ben Widawsky35451cb2013-01-17 12:45:13 -08001686 BUG_ON(mappable_end > end);
1687
Chris Wilsoned2f3452012-11-15 11:32:19 +00001688 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001689 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +01001690 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001691 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001692
Chris Wilsoned2f3452012-11-15 11:32:19 +00001693 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001694 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001695 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001696
Ben Widawskyedd41a82013-07-05 14:41:05 -07001697 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001698 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001699
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001700 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001701 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001702 if (ret) {
1703 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
1704 return ret;
1705 }
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001706 vma->bound |= GLOBAL_BIND;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001707 }
1708
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001709 dev_priv->gtt.base.start = start;
1710 dev_priv->gtt.base.total = end - start;
Daniel Vetter644ec022012-03-26 09:45:40 +02001711
Chris Wilsoned2f3452012-11-15 11:32:19 +00001712 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001713 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00001714 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1715 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08001716 ggtt_vm->clear_range(ggtt_vm, hole_start,
1717 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001718 }
1719
1720 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08001721 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001722
Daniel Vetterfa76da32014-08-06 20:19:54 +02001723 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
1724 struct i915_hw_ppgtt *ppgtt;
1725
1726 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1727 if (!ppgtt)
1728 return -ENOMEM;
1729
1730 ret = __hw_ppgtt_init(dev, ppgtt);
1731 if (ret != 0)
1732 return ret;
1733
1734 dev_priv->mm.aliasing_ppgtt = ppgtt;
1735 }
1736
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001737 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001738}
1739
Ben Widawskyd7e50082012-12-18 10:31:25 -08001740void i915_gem_init_global_gtt(struct drm_device *dev)
1741{
1742 struct drm_i915_private *dev_priv = dev->dev_private;
1743 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001744
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001745 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001746 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001747
Ben Widawskye78891c2013-01-25 16:41:04 -08001748 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001749}
1750
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001751void i915_global_gtt_cleanup(struct drm_device *dev)
1752{
1753 struct drm_i915_private *dev_priv = dev->dev_private;
1754 struct i915_address_space *vm = &dev_priv->gtt.base;
1755
Daniel Vetter70e32542014-08-06 15:04:57 +02001756 if (dev_priv->mm.aliasing_ppgtt) {
1757 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1758
1759 ppgtt->base.cleanup(&ppgtt->base);
1760 }
1761
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001762 if (drm_mm_initialized(&vm->mm)) {
1763 drm_mm_takedown(&vm->mm);
1764 list_del(&vm->global_link);
1765 }
1766
1767 vm->cleanup(vm);
1768}
Daniel Vetter70e32542014-08-06 15:04:57 +02001769
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001770static int setup_scratch_page(struct drm_device *dev)
1771{
1772 struct drm_i915_private *dev_priv = dev->dev_private;
1773 struct page *page;
1774 dma_addr_t dma_addr;
1775
1776 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1777 if (page == NULL)
1778 return -ENOMEM;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001779 set_pages_uc(page, 1);
1780
1781#ifdef CONFIG_INTEL_IOMMU
1782 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1783 PCI_DMA_BIDIRECTIONAL);
1784 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1785 return -EINVAL;
1786#else
1787 dma_addr = page_to_phys(page);
1788#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001789 dev_priv->gtt.base.scratch.page = page;
1790 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001791
1792 return 0;
1793}
1794
1795static void teardown_scratch_page(struct drm_device *dev)
1796{
1797 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001798 struct page *page = dev_priv->gtt.base.scratch.page;
1799
1800 set_pages_wb(page, 1);
1801 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001802 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001803 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001804}
1805
1806static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1807{
1808 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1809 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1810 return snb_gmch_ctl << 20;
1811}
1812
Ben Widawsky9459d252013-11-03 16:53:55 -08001813static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1814{
1815 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1816 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1817 if (bdw_gmch_ctl)
1818 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07001819
1820#ifdef CONFIG_X86_32
1821 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
1822 if (bdw_gmch_ctl > 4)
1823 bdw_gmch_ctl = 4;
1824#endif
1825
Ben Widawsky9459d252013-11-03 16:53:55 -08001826 return bdw_gmch_ctl << 20;
1827}
1828
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001829static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
1830{
1831 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
1832 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
1833
1834 if (gmch_ctrl)
1835 return 1 << (20 + gmch_ctrl);
1836
1837 return 0;
1838}
1839
Ben Widawskybaa09f52013-01-24 13:49:57 -08001840static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001841{
1842 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1843 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1844 return snb_gmch_ctl << 25; /* 32 MB units */
1845}
1846
Ben Widawsky9459d252013-11-03 16:53:55 -08001847static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1848{
1849 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1850 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1851 return bdw_gmch_ctl << 25; /* 32 MB units */
1852}
1853
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001854static size_t chv_get_stolen_size(u16 gmch_ctrl)
1855{
1856 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
1857 gmch_ctrl &= SNB_GMCH_GMS_MASK;
1858
1859 /*
1860 * 0x0 to 0x10: 32MB increments starting at 0MB
1861 * 0x11 to 0x16: 4MB increments starting at 8MB
1862 * 0x17 to 0x1d: 4MB increments start at 36MB
1863 */
1864 if (gmch_ctrl < 0x11)
1865 return gmch_ctrl << 25;
1866 else if (gmch_ctrl < 0x17)
1867 return (gmch_ctrl - 0x11 + 2) << 22;
1868 else
1869 return (gmch_ctrl - 0x17 + 9) << 22;
1870}
1871
Damien Lespiau66375012014-01-09 18:02:46 +00001872static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
1873{
1874 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1875 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
1876
1877 if (gen9_gmch_ctl < 0xf0)
1878 return gen9_gmch_ctl << 25; /* 32 MB units */
1879 else
1880 /* 4MB increments starting at 0xf0 for 4MB */
1881 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
1882}
1883
Ben Widawsky63340132013-11-04 19:32:22 -08001884static int ggtt_probe_common(struct drm_device *dev,
1885 size_t gtt_size)
1886{
1887 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001888 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08001889 int ret;
1890
1891 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001892 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08001893 (pci_resource_len(dev->pdev, 0) / 2);
1894
Bjorn Helgaas21c34602013-12-21 10:52:52 -07001895 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08001896 if (!dev_priv->gtt.gsm) {
1897 DRM_ERROR("Failed to map the gtt page table\n");
1898 return -ENOMEM;
1899 }
1900
1901 ret = setup_scratch_page(dev);
1902 if (ret) {
1903 DRM_ERROR("Scratch setup failed\n");
1904 /* iounmap will also get called at remove, but meh */
1905 iounmap(dev_priv->gtt.gsm);
1906 }
1907
1908 return ret;
1909}
1910
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001911/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1912 * bits. When using advanced contexts each context stores its own PAT, but
1913 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001914static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001915{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001916 uint64_t pat;
1917
1918 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1919 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1920 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1921 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1922 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1923 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1924 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1925 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1926
1927 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1928 * write would work. */
1929 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1930 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1931}
1932
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001933static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
1934{
1935 uint64_t pat;
1936
1937 /*
1938 * Map WB on BDW to snooped on CHV.
1939 *
1940 * Only the snoop bit has meaning for CHV, the rest is
1941 * ignored.
1942 *
1943 * Note that the harware enforces snooping for all page
1944 * table accesses. The snoop bit is actually ignored for
1945 * PDEs.
1946 */
1947 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
1948 GEN8_PPAT(1, 0) |
1949 GEN8_PPAT(2, 0) |
1950 GEN8_PPAT(3, 0) |
1951 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
1952 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
1953 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
1954 GEN8_PPAT(7, CHV_PPAT_SNOOP);
1955
1956 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1957 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1958}
1959
Ben Widawsky63340132013-11-04 19:32:22 -08001960static int gen8_gmch_probe(struct drm_device *dev,
1961 size_t *gtt_total,
1962 size_t *stolen,
1963 phys_addr_t *mappable_base,
1964 unsigned long *mappable_end)
1965{
1966 struct drm_i915_private *dev_priv = dev->dev_private;
1967 unsigned int gtt_size;
1968 u16 snb_gmch_ctl;
1969 int ret;
1970
1971 /* TODO: We're not aware of mappable constraints on gen8 yet */
1972 *mappable_base = pci_resource_start(dev->pdev, 2);
1973 *mappable_end = pci_resource_len(dev->pdev, 2);
1974
1975 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1976 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1977
1978 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1979
Damien Lespiau66375012014-01-09 18:02:46 +00001980 if (INTEL_INFO(dev)->gen >= 9) {
1981 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
1982 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
1983 } else if (IS_CHERRYVIEW(dev)) {
Damien Lespiaud7f25f22014-05-08 22:19:40 +03001984 *stolen = chv_get_stolen_size(snb_gmch_ctl);
1985 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
1986 } else {
1987 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1988 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
1989 }
Ben Widawsky63340132013-11-04 19:32:22 -08001990
Ben Widawskyd31eb102013-11-02 21:07:17 -07001991 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08001992
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001993 if (IS_CHERRYVIEW(dev))
1994 chv_setup_private_ppat(dev_priv);
1995 else
1996 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001997
Ben Widawsky63340132013-11-04 19:32:22 -08001998 ret = ggtt_probe_common(dev, gtt_size);
1999
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002000 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2001 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08002002
2003 return ret;
2004}
2005
Ben Widawskybaa09f52013-01-24 13:49:57 -08002006static int gen6_gmch_probe(struct drm_device *dev,
2007 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002008 size_t *stolen,
2009 phys_addr_t *mappable_base,
2010 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002011{
2012 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002013 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002014 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002015 int ret;
2016
Ben Widawsky41907dd2013-02-08 11:32:47 -08002017 *mappable_base = pci_resource_start(dev->pdev, 2);
2018 *mappable_end = pci_resource_len(dev->pdev, 2);
2019
Ben Widawskybaa09f52013-01-24 13:49:57 -08002020 /* 64/512MB is the current min/max we actually know of, but this is just
2021 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002022 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08002023 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08002024 DRM_ERROR("Unknown GMADR size (%lx)\n",
2025 dev_priv->gtt.mappable_end);
2026 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002027 }
2028
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002029 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2030 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08002031 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002032
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07002033 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002034
Ben Widawsky63340132013-11-04 19:32:22 -08002035 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002036 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
2037
Ben Widawsky63340132013-11-04 19:32:22 -08002038 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002039
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002040 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2041 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002042
2043 return ret;
2044}
2045
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002046static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002047{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002048
2049 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002050
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002051 iounmap(gtt->gsm);
2052 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002053}
2054
2055static int i915_gmch_probe(struct drm_device *dev,
2056 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002057 size_t *stolen,
2058 phys_addr_t *mappable_base,
2059 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002060{
2061 struct drm_i915_private *dev_priv = dev->dev_private;
2062 int ret;
2063
Ben Widawskybaa09f52013-01-24 13:49:57 -08002064 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2065 if (!ret) {
2066 DRM_ERROR("failed to set up gmch\n");
2067 return -EIO;
2068 }
2069
Ben Widawsky41907dd2013-02-08 11:32:47 -08002070 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002071
2072 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002073 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002074
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002075 if (unlikely(dev_priv->gtt.do_idle_maps))
2076 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2077
Ben Widawskybaa09f52013-01-24 13:49:57 -08002078 return 0;
2079}
2080
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002081static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002082{
2083 intel_gmch_remove();
2084}
2085
2086int i915_gem_gtt_init(struct drm_device *dev)
2087{
2088 struct drm_i915_private *dev_priv = dev->dev_private;
2089 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002090 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002091
Ben Widawskybaa09f52013-01-24 13:49:57 -08002092 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002093 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002094 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002095 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002096 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002097 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002098 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002099 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002100 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002101 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002102 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002103 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002104 else if (INTEL_INFO(dev)->gen >= 7)
2105 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002106 else
Chris Wilson350ec882013-08-06 13:17:02 +01002107 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002108 } else {
2109 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2110 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002111 }
2112
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002113 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002114 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002115 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002116 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002117
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002118 gtt->base.dev = dev;
2119
Ben Widawskybaa09f52013-01-24 13:49:57 -08002120 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002121 DRM_INFO("Memory usable by graphics device = %zdM\n",
2122 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002123 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2124 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002125#ifdef CONFIG_INTEL_IOMMU
2126 if (intel_iommu_gfx_mapped)
2127 DRM_INFO("VT-d active for gfx access\n");
2128#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002129 /*
2130 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2131 * user's requested state against the hardware/driver capabilities. We
2132 * do this now so that we can print out any log messages once rather
2133 * than every time we check intel_enable_ppgtt().
2134 */
2135 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2136 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002137
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002138 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002139}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002140
2141static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2142 struct i915_address_space *vm)
2143{
2144 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2145 if (vma == NULL)
2146 return ERR_PTR(-ENOMEM);
2147
2148 INIT_LIST_HEAD(&vma->vma_link);
2149 INIT_LIST_HEAD(&vma->mm_list);
2150 INIT_LIST_HEAD(&vma->exec_list);
2151 vma->vm = vm;
2152 vma->obj = obj;
2153
2154 switch (INTEL_INFO(vm->dev)->gen) {
Damien Lespiaufb8aad42014-01-16 16:42:32 +00002155 case 9:
Ben Widawsky6f65e292013-12-06 14:10:56 -08002156 case 8:
2157 case 7:
2158 case 6:
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002159 if (i915_is_ggtt(vm)) {
2160 vma->unbind_vma = ggtt_unbind_vma;
2161 vma->bind_vma = ggtt_bind_vma;
2162 } else {
2163 vma->unbind_vma = ppgtt_unbind_vma;
2164 vma->bind_vma = ppgtt_bind_vma;
2165 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002166 break;
2167 case 5:
2168 case 4:
2169 case 3:
2170 case 2:
2171 BUG_ON(!i915_is_ggtt(vm));
2172 vma->unbind_vma = i915_ggtt_unbind_vma;
2173 vma->bind_vma = i915_ggtt_bind_vma;
2174 break;
2175 default:
2176 BUG();
2177 }
2178
2179 /* Keep GGTT vmas first to make debug easier */
2180 if (i915_is_ggtt(vm))
2181 list_add(&vma->vma_link, &obj->vma_list);
Michel Thierrye07f0552014-08-19 15:49:41 +01002182 else {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002183 list_add_tail(&vma->vma_link, &obj->vma_list);
Michel Thierrye07f0552014-08-19 15:49:41 +01002184 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
2185 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002186
2187 return vma;
2188}
2189
2190struct i915_vma *
2191i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2192 struct i915_address_space *vm)
2193{
2194 struct i915_vma *vma;
2195
2196 vma = i915_gem_obj_to_vma(obj, vm);
2197 if (!vma)
2198 vma = __i915_gem_vma_create(obj, vm);
2199
2200 return vma;
2201}