blob: cbf013fd6b984dc63659b66544adc1d7eb80a777 [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"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080030#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010031#include "i915_trace.h"
32#include "intel_drv.h"
33
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000034/**
35 * DOC: Global GTT views
36 *
37 * Background and previous state
38 *
39 * Historically objects could exists (be bound) in global GTT space only as
40 * singular instances with a view representing all of the object's backing pages
41 * in a linear fashion. This view will be called a normal view.
42 *
43 * To support multiple views of the same object, where the number of mapped
44 * pages is not equal to the backing store, or where the layout of the pages
45 * is not linear, concept of a GGTT view was added.
46 *
47 * One example of an alternative view is a stereo display driven by a single
48 * image. In this case we would have a framebuffer looking like this
49 * (2x2 pages):
50 *
51 * 12
52 * 34
53 *
54 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
55 * rendering. In contrast, fed to the display engine would be an alternative
56 * view which could look something like this:
57 *
58 * 1212
59 * 3434
60 *
61 * In this example both the size and layout of pages in the alternative view is
62 * different from the normal view.
63 *
64 * Implementation and usage
65 *
66 * GGTT views are implemented using VMAs and are distinguished via enum
67 * i915_ggtt_view_type and struct i915_ggtt_view.
68 *
69 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020070 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
71 * renaming in large amounts of code. They take the struct i915_ggtt_view
72 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000073 *
74 * As a helper for callers which are only interested in the normal view,
75 * globally const i915_ggtt_view_normal singleton instance exists. All old core
76 * GEM API functions, the ones not taking the view parameter, are operating on,
77 * or with the normal GGTT view.
78 *
79 * Code wanting to add or use a new GGTT view needs to:
80 *
81 * 1. Add a new enum with a suitable name.
82 * 2. Extend the metadata in the i915_ggtt_view structure if required.
83 * 3. Add support to i915_get_vma_pages().
84 *
85 * New views are required to build a scatter-gather table from within the
86 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
87 * exists for the lifetime of an VMA.
88 *
89 * Core API is designed to have copy semantics which means that passed in
90 * struct i915_ggtt_view does not need to be persistent (left around after
91 * calling the core API functions).
92 *
93 */
94
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +000095const struct i915_ggtt_view i915_ggtt_view_normal;
96
Ville Syrjäläee0ce472014-04-09 13:28:01 +030097static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
98static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
Ben Widawskya2319c02014-03-18 16:09:37 -070099
Daniel Vettercfa7c862014-04-29 11:53:58 +0200100static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
101{
Chris Wilson1893a712014-09-19 11:56:27 +0100102 bool has_aliasing_ppgtt;
103 bool has_full_ppgtt;
104
105 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
106 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
Chris Wilson1893a712014-09-19 11:56:27 +0100107
Yu Zhang71ba2d62015-02-10 19:05:54 +0800108 if (intel_vgpu_active(dev))
109 has_full_ppgtt = false; /* emulation is too hard */
110
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000111 /*
112 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
113 * execlists, the sole mechanism available to submit work.
114 */
115 if (INTEL_INFO(dev)->gen < 9 &&
116 (enable_ppgtt == 0 || !has_aliasing_ppgtt))
Daniel Vettercfa7c862014-04-29 11:53:58 +0200117 return 0;
118
119 if (enable_ppgtt == 1)
120 return 1;
121
Chris Wilson1893a712014-09-19 11:56:27 +0100122 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200123 return 2;
124
Daniel Vetter93a25a92014-03-06 09:40:43 +0100125#ifdef CONFIG_INTEL_IOMMU
126 /* Disable ppgtt on SNB if VT-d is on. */
127 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
128 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200129 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100130 }
131#endif
132
Jesse Barnes62942ed2014-06-13 09:28:33 -0700133 /* Early VLV doesn't have this */
Ville Syrjäläca2aed6c2014-06-28 02:03:56 +0300134 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
135 dev->pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700136 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
137 return 0;
138 }
139
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000140 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
141 return 2;
142 else
143 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100144}
145
Ben Widawsky6f65e292013-12-06 14:10:56 -0800146static void ppgtt_bind_vma(struct i915_vma *vma,
147 enum i915_cache_level cache_level,
148 u32 flags);
149static void ppgtt_unbind_vma(struct i915_vma *vma);
150
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700151static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
152 enum i915_cache_level level,
153 bool valid)
154{
155 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
156 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300157
158 switch (level) {
159 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800160 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300161 break;
162 case I915_CACHE_WT:
163 pte |= PPAT_DISPLAY_ELLC_INDEX;
164 break;
165 default:
166 pte |= PPAT_CACHED_INDEX;
167 break;
168 }
169
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700170 return pte;
171}
172
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800173static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
174 dma_addr_t addr,
175 enum i915_cache_level level)
176{
177 gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
178 pde |= addr;
179 if (level != I915_CACHE_NONE)
180 pde |= PPAT_CACHED_PDE_INDEX;
181 else
182 pde |= PPAT_UNCACHED_INDEX;
183 return pde;
184}
185
Chris Wilson350ec882013-08-06 13:17:02 +0100186static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700187 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530188 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700189{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700190 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700191 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700192
193 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100194 case I915_CACHE_L3_LLC:
195 case I915_CACHE_LLC:
196 pte |= GEN6_PTE_CACHE_LLC;
197 break;
198 case I915_CACHE_NONE:
199 pte |= GEN6_PTE_UNCACHED;
200 break;
201 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100202 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100203 }
204
205 return pte;
206}
207
208static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700209 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530210 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100211{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700212 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100213 pte |= GEN6_PTE_ADDR_ENCODE(addr);
214
215 switch (level) {
216 case I915_CACHE_L3_LLC:
217 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700218 break;
219 case I915_CACHE_LLC:
220 pte |= GEN6_PTE_CACHE_LLC;
221 break;
222 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700223 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700224 break;
225 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100226 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700227 }
228
Ben Widawsky54d12522012-09-24 16:44:32 -0700229 return pte;
230}
231
Ben Widawsky80a74f72013-06-27 16:30:19 -0700232static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700233 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530234 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700235{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700236 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700237 pte |= GEN6_PTE_ADDR_ENCODE(addr);
238
Akash Goel24f3a8c2014-06-17 10:59:42 +0530239 if (!(flags & PTE_READ_ONLY))
240 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700241
242 if (level != I915_CACHE_NONE)
243 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
244
245 return pte;
246}
247
Ben Widawsky80a74f72013-06-27 16:30:19 -0700248static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700249 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530250 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700251{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700252 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700253 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700254
255 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700256 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700257
258 return pte;
259}
260
Ben Widawsky4d15c142013-07-04 11:02:06 -0700261static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
Ben Widawskyb35b3802013-10-16 09:18:21 -0700262 enum i915_cache_level level,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530263 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700264{
Ben Widawskyb35b3802013-10-16 09:18:21 -0700265 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700266 pte |= HSW_PTE_ADDR_ENCODE(addr);
267
Chris Wilson651d7942013-08-08 14:41:10 +0100268 switch (level) {
269 case I915_CACHE_NONE:
270 break;
271 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000272 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100273 break;
274 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000275 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100276 break;
277 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700278
279 return pte;
280}
281
Michel Thierry06dc68d2015-02-24 16:22:37 +0000282static void unmap_and_free_pt(struct i915_page_table_entry *pt, struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000283{
284 if (WARN_ON(!pt->page))
285 return;
286 __free_page(pt->page);
287 kfree(pt);
288}
289
Michel Thierry06dc68d2015-02-24 16:22:37 +0000290static struct i915_page_table_entry *alloc_pt_single(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000291{
292 struct i915_page_table_entry *pt;
293
294 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
295 if (!pt)
296 return ERR_PTR(-ENOMEM);
297
298 pt->page = alloc_page(GFP_KERNEL | __GFP_ZERO);
299 if (!pt->page) {
300 kfree(pt);
301 return ERR_PTR(-ENOMEM);
302 }
303
304 return pt;
305}
306
307/**
308 * alloc_pt_range() - Allocate a multiple page tables
309 * @pd: The page directory which will have at least @count entries
310 * available to point to the allocated page tables.
311 * @pde: First page directory entry for which we are allocating.
312 * @count: Number of pages to allocate.
Michel Thierry719cd212015-02-26 11:28:13 +0000313 * @dev: DRM device.
Ben Widawsky06fda602015-02-24 16:22:36 +0000314 *
315 * Allocates multiple page table pages and sets the appropriate entries in the
316 * page table structure within the page directory. Function cleans up after
317 * itself on any failures.
318 *
319 * Return: 0 if allocation succeeded.
320 */
Michel Thierry06dc68d2015-02-24 16:22:37 +0000321static int alloc_pt_range(struct i915_page_directory_entry *pd, uint16_t pde, size_t count,
322 struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000323{
324 int i, ret;
325
326 /* 512 is the max page tables per page_directory on any platform. */
327 if (WARN_ON(pde + count > GEN6_PPGTT_PD_ENTRIES))
328 return -EINVAL;
329
330 for (i = pde; i < pde + count; i++) {
Michel Thierry06dc68d2015-02-24 16:22:37 +0000331 struct i915_page_table_entry *pt = alloc_pt_single(dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000332
333 if (IS_ERR(pt)) {
334 ret = PTR_ERR(pt);
335 goto err_out;
336 }
337 WARN(pd->page_table[i],
Dan Carpenter686135d2015-02-26 19:53:54 +0300338 "Leaking page directory entry %d (%p)\n",
Ben Widawsky06fda602015-02-24 16:22:36 +0000339 i, pd->page_table[i]);
340 pd->page_table[i] = pt;
341 }
342
343 return 0;
344
345err_out:
346 while (i-- > pde)
Michel Thierry06dc68d2015-02-24 16:22:37 +0000347 unmap_and_free_pt(pd->page_table[i], dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000348 return ret;
349}
350
351static void unmap_and_free_pd(struct i915_page_directory_entry *pd)
352{
353 if (pd->page) {
354 __free_page(pd->page);
355 kfree(pd);
356 }
357}
358
359static struct i915_page_directory_entry *alloc_pd_single(void)
360{
361 struct i915_page_directory_entry *pd;
362
363 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
364 if (!pd)
365 return ERR_PTR(-ENOMEM);
366
367 pd->page = alloc_page(GFP_KERNEL | __GFP_ZERO);
368 if (!pd->page) {
369 kfree(pd);
370 return ERR_PTR(-ENOMEM);
371 }
372
373 return pd;
374}
375
Ben Widawsky94e409c2013-11-04 22:29:36 -0800376/* Broadwell Page Directory Pointer Descriptors */
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100377static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100378 uint64_t val)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800379{
380 int ret;
381
382 BUG_ON(entry >= 4);
383
384 ret = intel_ring_begin(ring, 6);
385 if (ret)
386 return ret;
387
388 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
389 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
390 intel_ring_emit(ring, (u32)(val >> 32));
391 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
392 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
393 intel_ring_emit(ring, (u32)(val));
394 intel_ring_advance(ring);
395
396 return 0;
397}
398
Ben Widawskyeeb94882013-12-06 14:11:10 -0800399static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100400 struct intel_engine_cs *ring)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800401{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800402 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800403
404 /* bit of a hack to find the actual last used pd */
405 int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
406
Ben Widawsky94e409c2013-11-04 22:29:36 -0800407 for (i = used_pd - 1; i >= 0; i--) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000408 dma_addr_t addr = ppgtt->pdp.page_directory[i]->daddr;
McAulay, Alistair6689c162014-08-15 18:51:35 +0100409 ret = gen8_write_pdp(ring, i, addr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800410 if (ret)
411 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800412 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800413
Ben Widawskyeeb94882013-12-06 14:11:10 -0800414 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800415}
416
Ben Widawsky459108b2013-11-02 21:07:23 -0700417static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800418 uint64_t start,
419 uint64_t length,
Ben Widawsky459108b2013-11-02 21:07:23 -0700420 bool use_scratch)
421{
422 struct i915_hw_ppgtt *ppgtt =
423 container_of(vm, struct i915_hw_ppgtt, base);
424 gen8_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800425 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
426 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
427 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800428 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700429 unsigned last_pte, i;
430
431 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
432 I915_CACHE_LLC, use_scratch);
433
434 while (num_entries) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000435 struct i915_page_directory_entry *pd;
436 struct i915_page_table_entry *pt;
437 struct page *page_table;
438
439 if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
440 continue;
441
442 pd = ppgtt->pdp.page_directory[pdpe];
443
444 if (WARN_ON(!pd->page_table[pde]))
445 continue;
446
447 pt = pd->page_table[pde];
448
449 if (WARN_ON(!pt->page))
450 continue;
451
452 page_table = pt->page;
Ben Widawsky459108b2013-11-02 21:07:23 -0700453
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800454 last_pte = pte + num_entries;
Ben Widawsky459108b2013-11-02 21:07:23 -0700455 if (last_pte > GEN8_PTES_PER_PAGE)
456 last_pte = GEN8_PTES_PER_PAGE;
457
458 pt_vaddr = kmap_atomic(page_table);
459
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800460 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700461 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800462 num_entries--;
463 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700464
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300465 if (!HAS_LLC(ppgtt->base.dev))
466 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky459108b2013-11-02 21:07:23 -0700467 kunmap_atomic(pt_vaddr);
468
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800469 pte = 0;
470 if (++pde == GEN8_PDES_PER_PAGE) {
471 pdpe++;
472 pde = 0;
473 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700474 }
475}
476
Ben Widawsky9df15b42013-11-02 21:07:24 -0700477static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
478 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800479 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530480 enum i915_cache_level cache_level, u32 unused)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700481{
482 struct i915_hw_ppgtt *ppgtt =
483 container_of(vm, struct i915_hw_ppgtt, base);
484 gen8_gtt_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800485 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
486 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
487 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700488 struct sg_page_iter sg_iter;
489
Chris Wilson6f1cc992013-12-31 15:50:31 +0000490 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700491
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800492 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Ben Widawsky76643602015-01-22 17:01:24 +0000493 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800494 break;
495
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000496 if (pt_vaddr == NULL) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000497 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[pdpe];
498 struct i915_page_table_entry *pt = pd->page_table[pde];
499 struct page *page_table = pt->page;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000500
501 pt_vaddr = kmap_atomic(page_table);
502 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800503
504 pt_vaddr[pte] =
Chris Wilson6f1cc992013-12-31 15:50:31 +0000505 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
506 cache_level, true);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800507 if (++pte == GEN8_PTES_PER_PAGE) {
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300508 if (!HAS_LLC(ppgtt->base.dev))
509 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700510 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000511 pt_vaddr = NULL;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800512 if (++pde == GEN8_PDES_PER_PAGE) {
513 pdpe++;
514 pde = 0;
515 }
516 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700517 }
518 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300519 if (pt_vaddr) {
520 if (!HAS_LLC(ppgtt->base.dev))
521 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000522 kunmap_atomic(pt_vaddr);
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300523 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700524}
525
Michel Thierry06dc68d2015-02-24 16:22:37 +0000526static void gen8_free_page_tables(struct i915_page_directory_entry *pd, struct drm_device *dev)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800527{
528 int i;
529
Ben Widawsky06fda602015-02-24 16:22:36 +0000530 if (!pd->page)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800531 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800532
Ben Widawsky06fda602015-02-24 16:22:36 +0000533 for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
534 if (WARN_ON(!pd->page_table[i]))
535 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800536
Michel Thierry06dc68d2015-02-24 16:22:37 +0000537 unmap_and_free_pt(pd->page_table[i], dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000538 pd->page_table[i] = NULL;
539 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000540}
541
542static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800543{
544 int i;
545
546 for (i = 0; i < ppgtt->num_pd_pages; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000547 if (WARN_ON(!ppgtt->pdp.page_directory[i]))
548 continue;
549
Michel Thierry06dc68d2015-02-24 16:22:37 +0000550 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000551 unmap_and_free_pd(ppgtt->pdp.page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800552 }
Ben Widawskyb45a6712014-02-12 14:28:44 -0800553}
554
555static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
556{
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800557 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800558 int i, j;
559
560 for (i = 0; i < ppgtt->num_pd_pages; i++) {
561 /* TODO: In the future we'll support sparse mappings, so this
562 * will have to change. */
Ben Widawsky06fda602015-02-24 16:22:36 +0000563 if (!ppgtt->pdp.page_directory[i]->daddr)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800564 continue;
565
Ben Widawsky06fda602015-02-24 16:22:36 +0000566 pci_unmap_page(hwdev, ppgtt->pdp.page_directory[i]->daddr, PAGE_SIZE,
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800567 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800568
569 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000570 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i];
571 struct i915_page_table_entry *pt;
572 dma_addr_t addr;
573
574 if (WARN_ON(!pd->page_table[j]))
575 continue;
576
577 pt = pd->page_table[j];
578 addr = pt->daddr;
579
Ben Widawskyb45a6712014-02-12 14:28:44 -0800580 if (addr)
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800581 pci_unmap_page(hwdev, addr, PAGE_SIZE,
582 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800583 }
584 }
585}
586
Ben Widawsky37aca442013-11-04 20:47:32 -0800587static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
588{
589 struct i915_hw_ppgtt *ppgtt =
590 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800591
Ben Widawskyb45a6712014-02-12 14:28:44 -0800592 gen8_ppgtt_unmap_pages(ppgtt);
593 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800594}
595
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000596static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
597{
Ben Widawsky06fda602015-02-24 16:22:36 +0000598 int i, ret;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000599
600 for (i = 0; i < ppgtt->num_pd_pages; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000601 ret = alloc_pt_range(ppgtt->pdp.page_directory[i],
Michel Thierry06dc68d2015-02-24 16:22:37 +0000602 0, GEN8_PDES_PER_PAGE, ppgtt->base.dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000603 if (ret)
604 goto unwind_out;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000605 }
606
607 return 0;
608
609unwind_out:
610 while (i--)
Michel Thierry06dc68d2015-02-24 16:22:37 +0000611 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000612
613 return -ENOMEM;
614}
615
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800616static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
617 const int max_pdp)
618{
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000619 int i;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800620
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000621 for (i = 0; i < max_pdp; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000622 ppgtt->pdp.page_directory[i] = alloc_pd_single();
623 if (IS_ERR(ppgtt->pdp.page_directory[i]))
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000624 goto unwind_out;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000625 }
626
627 ppgtt->num_pd_pages = max_pdp;
Ben Widawsky76643602015-01-22 17:01:24 +0000628 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPES);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800629
630 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000631
632unwind_out:
Ben Widawsky06fda602015-02-24 16:22:36 +0000633 while (i--)
634 unmap_and_free_pd(ppgtt->pdp.page_directory[i]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000635
636 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800637}
638
639static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
640 const int max_pdp)
641{
642 int ret;
643
644 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
645 if (ret)
646 return ret;
647
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000648 ret = gen8_ppgtt_allocate_page_tables(ppgtt);
649 if (ret)
650 goto err_out;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800651
652 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
653
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000654 return 0;
655
656err_out:
657 gen8_ppgtt_free(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800658 return ret;
659}
660
661static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
662 const int pd)
663{
664 dma_addr_t pd_addr;
665 int ret;
666
667 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
Ben Widawsky06fda602015-02-24 16:22:36 +0000668 ppgtt->pdp.page_directory[pd]->page, 0,
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800669 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
670
671 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
672 if (ret)
673 return ret;
674
Ben Widawsky06fda602015-02-24 16:22:36 +0000675 ppgtt->pdp.page_directory[pd]->daddr = pd_addr;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800676
677 return 0;
678}
679
680static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
681 const int pd,
682 const int pt)
683{
684 dma_addr_t pt_addr;
Ben Widawsky06fda602015-02-24 16:22:36 +0000685 struct i915_page_directory_entry *pdir = ppgtt->pdp.page_directory[pd];
686 struct i915_page_table_entry *ptab = pdir->page_table[pt];
Ben Widawsky7324cc02015-02-24 16:22:35 +0000687 struct page *p = ptab->page;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800688 int ret;
689
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800690 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
691 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
692 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
693 if (ret)
694 return ret;
695
Ben Widawsky7324cc02015-02-24 16:22:35 +0000696 ptab->daddr = pt_addr;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800697
698 return 0;
699}
700
Ben Widawsky37aca442013-11-04 20:47:32 -0800701/**
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800702 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
703 * with a net effect resembling a 2-level page table in normal x86 terms. Each
704 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
705 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800706 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800707 * FIXME: split allocation into smaller pieces. For now we only ever do this
708 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
Ben Widawsky37aca442013-11-04 20:47:32 -0800709 * TODO: Do something with the size parameter
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800710 */
Ben Widawsky37aca442013-11-04 20:47:32 -0800711static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
712{
Ben Widawsky37aca442013-11-04 20:47:32 -0800713 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800714 const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800715 int i, j, ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800716
717 if (size % (1<<30))
718 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
719
Mika Kuoppala29343682015-03-04 14:55:17 +0200720 /* 1. Do all our allocations for page directories and page tables.
721 * We allocate more than was asked so that we can point the unused parts
722 * to valid entries that point to scratch page. Dynamic page tables
723 * will fix this eventually.
724 */
725 ret = gen8_ppgtt_alloc(ppgtt, GEN8_LEGACY_PDPES);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800726 if (ret)
727 return ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800728
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800729 /*
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800730 * 2. Create DMA mappings for the page directories and page tables.
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800731 */
Mika Kuoppala29343682015-03-04 14:55:17 +0200732 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800733 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800734 if (ret)
735 goto bail;
736
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800737 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800738 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800739 if (ret)
740 goto bail;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800741 }
742 }
743
744 /*
745 * 3. Map all the page directory entires to point to the page tables
746 * we've allocated.
747 *
748 * For now, the PPGTT helper functions all require that the PDEs are
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800749 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800750 * will never need to touch the PDEs again.
751 */
Mika Kuoppala29343682015-03-04 14:55:17 +0200752 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000753 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i];
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800754 gen8_ppgtt_pde_t *pd_vaddr;
Ben Widawsky06fda602015-02-24 16:22:36 +0000755 pd_vaddr = kmap_atomic(ppgtt->pdp.page_directory[i]->page);
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800756 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000757 struct i915_page_table_entry *pt = pd->page_table[j];
758 dma_addr_t addr = pt->daddr;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800759 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
760 I915_CACHE_LLC);
761 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300762 if (!HAS_LLC(ppgtt->base.dev))
763 drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800764 kunmap_atomic(pd_vaddr);
765 }
766
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800767 ppgtt->switch_mm = gen8_mm_switch;
768 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
769 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
770 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
771 ppgtt->base.start = 0;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800772
Mika Kuoppala29343682015-03-04 14:55:17 +0200773 /* This is the area that we advertise as usable for the caller */
774 ppgtt->base.total = max_pdp * GEN8_PDES_PER_PAGE * GEN8_PTES_PER_PAGE * PAGE_SIZE;
775
776 /* Set all ptes to a valid scratch page. Also above requested space */
777 ppgtt->base.clear_range(&ppgtt->base, 0,
778 ppgtt->num_pd_pages * GEN8_PTES_PER_PAGE * PAGE_SIZE,
779 true);
Ben Widawsky459108b2013-11-02 21:07:23 -0700780
Ben Widawsky37aca442013-11-04 20:47:32 -0800781 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
782 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
783 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800784 ppgtt->num_pd_entries,
785 (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700786 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800787
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800788bail:
789 gen8_ppgtt_unmap_pages(ppgtt);
790 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800791 return ret;
792}
793
Ben Widawsky87d60b62013-12-06 14:11:29 -0800794static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
795{
796 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
797 struct i915_address_space *vm = &ppgtt->base;
798 gen6_gtt_pte_t __iomem *pd_addr;
799 gen6_gtt_pte_t scratch_pte;
800 uint32_t pd_entry;
801 int pte, pde;
802
Akash Goel24f3a8c2014-06-17 10:59:42 +0530803 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800804
805 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
Ben Widawsky7324cc02015-02-24 16:22:35 +0000806 ppgtt->pd.pd_offset / sizeof(gen6_gtt_pte_t);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800807
808 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
Ben Widawsky7324cc02015-02-24 16:22:35 +0000809 ppgtt->pd.pd_offset,
810 ppgtt->pd.pd_offset + ppgtt->num_pd_entries);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800811 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
812 u32 expected;
813 gen6_gtt_pte_t *pt_vaddr;
Ben Widawsky06fda602015-02-24 16:22:36 +0000814 dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
Ben Widawsky87d60b62013-12-06 14:11:29 -0800815 pd_entry = readl(pd_addr + pde);
816 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
817
818 if (pd_entry != expected)
819 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
820 pde,
821 pd_entry,
822 expected);
823 seq_printf(m, "\tPDE: %x\n", pd_entry);
824
Ben Widawsky06fda602015-02-24 16:22:36 +0000825 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800826 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
827 unsigned long va =
828 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
829 (pte * PAGE_SIZE);
830 int i;
831 bool found = false;
832 for (i = 0; i < 4; i++)
833 if (pt_vaddr[pte + i] != scratch_pte)
834 found = true;
835 if (!found)
836 continue;
837
838 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
839 for (i = 0; i < 4; i++) {
840 if (pt_vaddr[pte + i] != scratch_pte)
841 seq_printf(m, " %08x", pt_vaddr[pte + i]);
842 else
843 seq_puts(m, " SCRATCH ");
844 }
845 seq_puts(m, "\n");
846 }
847 kunmap_atomic(pt_vaddr);
848 }
849}
850
Ben Widawsky3e302542013-04-23 23:15:32 -0700851static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky61973492013-04-08 18:43:54 -0700852{
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700853 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
Ben Widawsky61973492013-04-08 18:43:54 -0700854 gen6_gtt_pte_t __iomem *pd_addr;
855 uint32_t pd_entry;
856 int i;
857
Ben Widawsky7324cc02015-02-24 16:22:35 +0000858 WARN_ON(ppgtt->pd.pd_offset & 0x3f);
Ben Widawsky61973492013-04-08 18:43:54 -0700859 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
Ben Widawsky7324cc02015-02-24 16:22:35 +0000860 ppgtt->pd.pd_offset / sizeof(gen6_gtt_pte_t);
Ben Widawsky61973492013-04-08 18:43:54 -0700861 for (i = 0; i < ppgtt->num_pd_entries; i++) {
862 dma_addr_t pt_addr;
863
Ben Widawsky06fda602015-02-24 16:22:36 +0000864 pt_addr = ppgtt->pd.page_table[i]->daddr;
Ben Widawsky61973492013-04-08 18:43:54 -0700865 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
866 pd_entry |= GEN6_PDE_VALID;
867
868 writel(pd_entry, pd_addr + i);
869 }
870 readl(pd_addr);
Ben Widawsky3e302542013-04-23 23:15:32 -0700871}
872
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800873static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700874{
Ben Widawsky7324cc02015-02-24 16:22:35 +0000875 BUG_ON(ppgtt->pd.pd_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -0700876
Ben Widawsky7324cc02015-02-24 16:22:35 +0000877 return (ppgtt->pd.pd_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800878}
Ben Widawsky61973492013-04-08 18:43:54 -0700879
Ben Widawsky90252e52013-12-06 14:11:12 -0800880static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100881 struct intel_engine_cs *ring)
Ben Widawsky90252e52013-12-06 14:11:12 -0800882{
Ben Widawsky90252e52013-12-06 14:11:12 -0800883 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700884
Ben Widawsky90252e52013-12-06 14:11:12 -0800885 /* NB: TLBs must be flushed and invalidated before a switch */
886 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
887 if (ret)
888 return ret;
889
890 ret = intel_ring_begin(ring, 6);
891 if (ret)
892 return ret;
893
894 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
895 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
896 intel_ring_emit(ring, PP_DIR_DCLV_2G);
897 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
898 intel_ring_emit(ring, get_pd_offset(ppgtt));
899 intel_ring_emit(ring, MI_NOOP);
900 intel_ring_advance(ring);
901
902 return 0;
903}
904
Yu Zhang71ba2d62015-02-10 19:05:54 +0800905static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
906 struct intel_engine_cs *ring)
907{
908 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
909
910 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
911 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
912 return 0;
913}
914
Ben Widawsky48a10382013-12-06 14:11:11 -0800915static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100916 struct intel_engine_cs *ring)
Ben Widawsky48a10382013-12-06 14:11:11 -0800917{
Ben Widawsky48a10382013-12-06 14:11:11 -0800918 int ret;
919
Ben Widawsky48a10382013-12-06 14:11:11 -0800920 /* NB: TLBs must be flushed and invalidated before a switch */
921 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
922 if (ret)
923 return ret;
924
925 ret = intel_ring_begin(ring, 6);
926 if (ret)
927 return ret;
928
929 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
930 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
931 intel_ring_emit(ring, PP_DIR_DCLV_2G);
932 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
933 intel_ring_emit(ring, get_pd_offset(ppgtt));
934 intel_ring_emit(ring, MI_NOOP);
935 intel_ring_advance(ring);
936
Ben Widawsky90252e52013-12-06 14:11:12 -0800937 /* XXX: RCS is the only one to auto invalidate the TLBs? */
938 if (ring->id != RCS) {
939 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
940 if (ret)
941 return ret;
942 }
943
Ben Widawsky48a10382013-12-06 14:11:11 -0800944 return 0;
945}
946
Ben Widawskyeeb94882013-12-06 14:11:10 -0800947static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100948 struct intel_engine_cs *ring)
Ben Widawskyeeb94882013-12-06 14:11:10 -0800949{
950 struct drm_device *dev = ppgtt->base.dev;
951 struct drm_i915_private *dev_priv = dev->dev_private;
952
Ben Widawsky48a10382013-12-06 14:11:11 -0800953
Ben Widawskyeeb94882013-12-06 14:11:10 -0800954 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
955 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
956
957 POSTING_READ(RING_PP_DIR_DCLV(ring));
958
959 return 0;
960}
961
Daniel Vetter82460d92014-08-06 20:19:53 +0200962static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -0800963{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800964 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100965 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +0200966 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -0800967
968 for_each_ring(ring, dev_priv, j) {
969 I915_WRITE(RING_MODE_GEN7(ring),
970 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -0800971 }
Ben Widawskyeeb94882013-12-06 14:11:10 -0800972}
973
Daniel Vetter82460d92014-08-06 20:19:53 +0200974static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800975{
Jani Nikula50227e12014-03-31 14:27:21 +0300976 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100977 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800978 uint32_t ecochk, ecobits;
979 int i;
980
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800981 ecobits = I915_READ(GAC_ECO_BITS);
982 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
983
984 ecochk = I915_READ(GAM_ECOCHK);
985 if (IS_HASWELL(dev)) {
986 ecochk |= ECOCHK_PPGTT_WB_HSW;
987 } else {
988 ecochk |= ECOCHK_PPGTT_LLC_IVB;
989 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
990 }
991 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800992
Ben Widawsky61973492013-04-08 18:43:54 -0700993 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -0800994 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800995 I915_WRITE(RING_MODE_GEN7(ring),
996 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -0700997 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800998}
999
Daniel Vetter82460d92014-08-06 20:19:53 +02001000static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001001{
Jani Nikula50227e12014-03-31 14:27:21 +03001002 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001003 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001004
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001005 ecobits = I915_READ(GAC_ECO_BITS);
1006 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1007 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001008
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001009 gab_ctl = I915_READ(GAB_CTL);
1010 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001011
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001012 ecochk = I915_READ(GAM_ECOCHK);
1013 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001014
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001015 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001016}
1017
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001018/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001019static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001020 uint64_t start,
1021 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001022 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001023{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001024 struct i915_hw_ppgtt *ppgtt =
1025 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -07001026 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001027 unsigned first_entry = start >> PAGE_SHIFT;
1028 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +01001029 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001030 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
1031 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001032
Akash Goel24f3a8c2014-06-17 10:59:42 +05301033 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001034
Daniel Vetter7bddb012012-02-09 17:15:47 +01001035 while (num_entries) {
1036 last_pte = first_pte + num_entries;
1037 if (last_pte > I915_PPGTT_PT_ENTRIES)
1038 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001039
Ben Widawsky06fda602015-02-24 16:22:36 +00001040 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001041
1042 for (i = first_pte; i < last_pte; i++)
1043 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001044
1045 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001046
Daniel Vetter7bddb012012-02-09 17:15:47 +01001047 num_entries -= last_pte - first_pte;
1048 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001049 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001050 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001051}
1052
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001053static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001054 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001055 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301056 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001057{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001058 struct i915_hw_ppgtt *ppgtt =
1059 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawskye7c2b582013-04-08 18:43:48 -07001060 gen6_gtt_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -08001061 unsigned first_entry = start >> PAGE_SHIFT;
Daniel Vettera15326a2013-03-19 23:48:39 +01001062 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
Imre Deak6e995e22013-02-18 19:28:04 +02001063 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
1064 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001065
Chris Wilsoncc797142013-12-31 15:50:30 +00001066 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +02001067 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001068 if (pt_vaddr == NULL)
Ben Widawsky06fda602015-02-24 16:22:36 +00001069 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001070
Chris Wilsoncc797142013-12-31 15:50:30 +00001071 pt_vaddr[act_pte] =
1072 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +05301073 cache_level, true, flags);
1074
Imre Deak6e995e22013-02-18 19:28:04 +02001075 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
1076 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001077 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001078 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001079 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001080 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001081 }
Chris Wilsoncc797142013-12-31 15:50:30 +00001082 if (pt_vaddr)
1083 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001084}
1085
Ben Widawskya00d8252014-02-19 22:05:48 -08001086static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001087{
Daniel Vetter3440d262013-01-24 13:49:56 -08001088 int i;
1089
Ben Widawsky7324cc02015-02-24 16:22:35 +00001090 for (i = 0; i < ppgtt->num_pd_entries; i++)
1091 pci_unmap_page(ppgtt->base.dev->pdev,
Ben Widawsky06fda602015-02-24 16:22:36 +00001092 ppgtt->pd.page_table[i]->daddr,
Ben Widawsky7324cc02015-02-24 16:22:35 +00001093 4096, PCI_DMA_BIDIRECTIONAL);
Ben Widawskya00d8252014-02-19 22:05:48 -08001094}
1095
1096static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
1097{
1098 int i;
Daniel Vetter3440d262013-01-24 13:49:56 -08001099
Daniel Vetter3440d262013-01-24 13:49:56 -08001100 for (i = 0; i < ppgtt->num_pd_entries; i++)
Michel Thierry06dc68d2015-02-24 16:22:37 +00001101 unmap_and_free_pt(ppgtt->pd.page_table[i], ppgtt->base.dev);
Ben Widawsky06fda602015-02-24 16:22:36 +00001102
1103 unmap_and_free_pd(&ppgtt->pd);
Daniel Vetter3440d262013-01-24 13:49:56 -08001104}
1105
Ben Widawskya00d8252014-02-19 22:05:48 -08001106static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1107{
1108 struct i915_hw_ppgtt *ppgtt =
1109 container_of(vm, struct i915_hw_ppgtt, base);
1110
Ben Widawskya00d8252014-02-19 22:05:48 -08001111 drm_mm_remove_node(&ppgtt->node);
1112
1113 gen6_ppgtt_unmap_pages(ppgtt);
1114 gen6_ppgtt_free(ppgtt);
1115}
1116
Ben Widawskyb1465202014-02-19 22:05:49 -08001117static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001118{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001119 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001120 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001121 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001122 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001123
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001124 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1125 * allocator works in address space sizes, so it's multiplied by page
1126 * size. We allocate at the top of the GTT to avoid fragmentation.
1127 */
1128 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -08001129alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001130 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1131 &ppgtt->node, GEN6_PD_SIZE,
1132 GEN6_PD_ALIGN, 0,
1133 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07001134 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001135 if (ret == -ENOSPC && !retried) {
1136 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1137 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02001138 I915_CACHE_NONE,
1139 0, dev_priv->gtt.base.total,
1140 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001141 if (ret)
1142 return ret;
1143
1144 retried = true;
1145 goto alloc;
1146 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001147
Ben Widawskyc8c26622015-01-22 17:01:25 +00001148 if (ret)
1149 return ret;
1150
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001151 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1152 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001153
Ben Widawsky6670a5a2013-06-27 16:30:04 -07001154 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
Ben Widawskyc8c26622015-01-22 17:01:25 +00001155 return 0;
Ben Widawskyb1465202014-02-19 22:05:49 -08001156}
1157
Ben Widawskyb1465202014-02-19 22:05:49 -08001158static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1159{
1160 int ret;
1161
1162 ret = gen6_ppgtt_allocate_page_directories(ppgtt);
1163 if (ret)
1164 return ret;
1165
Michel Thierry06dc68d2015-02-24 16:22:37 +00001166 ret = alloc_pt_range(&ppgtt->pd, 0, ppgtt->num_pd_entries,
1167 ppgtt->base.dev);
1168
Ben Widawskyb1465202014-02-19 22:05:49 -08001169 if (ret) {
1170 drm_mm_remove_node(&ppgtt->node);
1171 return ret;
1172 }
1173
Ben Widawskyb1465202014-02-19 22:05:49 -08001174 return 0;
1175}
1176
1177static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
1178{
1179 struct drm_device *dev = ppgtt->base.dev;
1180 int i;
1181
1182 for (i = 0; i < ppgtt->num_pd_entries; i++) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001183 struct page *page;
Ben Widawskyb1465202014-02-19 22:05:49 -08001184 dma_addr_t pt_addr;
1185
Ben Widawsky06fda602015-02-24 16:22:36 +00001186 page = ppgtt->pd.page_table[i]->page;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001187 pt_addr = pci_map_page(dev->pdev, page, 0, 4096,
Ben Widawskyb1465202014-02-19 22:05:49 -08001188 PCI_DMA_BIDIRECTIONAL);
1189
1190 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1191 gen6_ppgtt_unmap_pages(ppgtt);
1192 return -EIO;
1193 }
1194
Ben Widawsky06fda602015-02-24 16:22:36 +00001195 ppgtt->pd.page_table[i]->daddr = pt_addr;
Ben Widawskyb1465202014-02-19 22:05:49 -08001196 }
1197
1198 return 0;
1199}
1200
1201static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1202{
1203 struct drm_device *dev = ppgtt->base.dev;
1204 struct drm_i915_private *dev_priv = dev->dev_private;
1205 int ret;
1206
1207 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001208 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001209 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001210 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001211 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001212 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001213 ppgtt->switch_mm = gen7_mm_switch;
1214 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001215 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001216
Yu Zhang71ba2d62015-02-10 19:05:54 +08001217 if (intel_vgpu_active(dev))
1218 ppgtt->switch_mm = vgpu_mm_switch;
1219
Ben Widawskyb1465202014-02-19 22:05:49 -08001220 ret = gen6_ppgtt_alloc(ppgtt);
1221 if (ret)
1222 return ret;
1223
1224 ret = gen6_ppgtt_setup_page_tables(ppgtt);
1225 if (ret) {
1226 gen6_ppgtt_free(ppgtt);
1227 return ret;
1228 }
1229
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001230 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1231 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1232 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001233 ppgtt->base.start = 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001234 ppgtt->base.total = ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001235 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001236
Ben Widawsky7324cc02015-02-24 16:22:35 +00001237 ppgtt->pd.pd_offset =
Ben Widawskyb1465202014-02-19 22:05:49 -08001238 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001239
Ben Widawsky782f1492014-02-20 11:50:33 -08001240 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001241
Thierry Reding440fd522015-01-23 09:05:06 +01001242 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001243 ppgtt->node.size >> 20,
1244 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001245
Daniel Vetterfa76da32014-08-06 20:19:54 +02001246 gen6_write_pdes(ppgtt);
1247 DRM_DEBUG("Adding PPGTT at offset %x\n",
Ben Widawsky7324cc02015-02-24 16:22:35 +00001248 ppgtt->pd.pd_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001249
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001250 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001251}
1252
Daniel Vetterfa76da32014-08-06 20:19:54 +02001253static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001254{
1255 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter3440d262013-01-24 13:49:56 -08001256
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001257 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001258 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001259
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001260 if (INTEL_INFO(dev)->gen < 8)
Daniel Vetterfa76da32014-08-06 20:19:54 +02001261 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001262 else
Rodrigo Vivi1eb0f002014-12-03 04:55:26 -08001263 return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001264}
1265int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1266{
1267 struct drm_i915_private *dev_priv = dev->dev_private;
1268 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001269
Daniel Vetterfa76da32014-08-06 20:19:54 +02001270 ret = __hw_ppgtt_init(dev, ppgtt);
1271 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001272 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001273 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1274 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001275 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001276 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001277
1278 return ret;
1279}
1280
Daniel Vetter82460d92014-08-06 20:19:53 +02001281int i915_ppgtt_init_hw(struct drm_device *dev)
1282{
1283 struct drm_i915_private *dev_priv = dev->dev_private;
1284 struct intel_engine_cs *ring;
1285 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1286 int i, ret = 0;
1287
Thomas Daniel671b50132014-08-20 16:24:50 +01001288 /* In the case of execlists, PPGTT is enabled by the context descriptor
1289 * and the PDPs are contained within the context itself. We don't
1290 * need to do anything here. */
1291 if (i915.enable_execlists)
1292 return 0;
1293
Daniel Vetter82460d92014-08-06 20:19:53 +02001294 if (!USES_PPGTT(dev))
1295 return 0;
1296
1297 if (IS_GEN6(dev))
1298 gen6_ppgtt_enable(dev);
1299 else if (IS_GEN7(dev))
1300 gen7_ppgtt_enable(dev);
1301 else if (INTEL_INFO(dev)->gen >= 8)
1302 gen8_ppgtt_enable(dev);
1303 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01001304 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02001305
1306 if (ppgtt) {
1307 for_each_ring(ring, dev_priv, i) {
McAulay, Alistair6689c162014-08-15 18:51:35 +01001308 ret = ppgtt->switch_mm(ppgtt, ring);
Daniel Vetter82460d92014-08-06 20:19:53 +02001309 if (ret != 0)
1310 return ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001311 }
1312 }
1313
1314 return ret;
1315}
Daniel Vetter4d884702014-08-06 15:04:47 +02001316struct i915_hw_ppgtt *
1317i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1318{
1319 struct i915_hw_ppgtt *ppgtt;
1320 int ret;
1321
1322 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1323 if (!ppgtt)
1324 return ERR_PTR(-ENOMEM);
1325
1326 ret = i915_ppgtt_init(dev, ppgtt);
1327 if (ret) {
1328 kfree(ppgtt);
1329 return ERR_PTR(ret);
1330 }
1331
1332 ppgtt->file_priv = fpriv;
1333
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001334 trace_i915_ppgtt_create(&ppgtt->base);
1335
Daniel Vetter4d884702014-08-06 15:04:47 +02001336 return ppgtt;
1337}
1338
Daniel Vetteree960be2014-08-06 15:04:45 +02001339void i915_ppgtt_release(struct kref *kref)
1340{
1341 struct i915_hw_ppgtt *ppgtt =
1342 container_of(kref, struct i915_hw_ppgtt, ref);
1343
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001344 trace_i915_ppgtt_release(&ppgtt->base);
1345
Daniel Vetteree960be2014-08-06 15:04:45 +02001346 /* vmas should already be unbound */
1347 WARN_ON(!list_empty(&ppgtt->base.active_list));
1348 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1349
Daniel Vetter19dd1202014-08-06 15:04:55 +02001350 list_del(&ppgtt->base.global_link);
1351 drm_mm_takedown(&ppgtt->base.mm);
1352
Daniel Vetteree960be2014-08-06 15:04:45 +02001353 ppgtt->base.cleanup(&ppgtt->base);
1354 kfree(ppgtt);
1355}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001356
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001357static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001358ppgtt_bind_vma(struct i915_vma *vma,
1359 enum i915_cache_level cache_level,
1360 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001361{
Akash Goel24f3a8c2014-06-17 10:59:42 +05301362 /* Currently applicable only to VLV */
1363 if (vma->obj->gt_ro)
1364 flags |= PTE_READ_ONLY;
1365
Ben Widawsky782f1492014-02-20 11:50:33 -08001366 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301367 cache_level, flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001368}
1369
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001370static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001371{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001372 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001373 vma->node.start,
1374 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001375 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001376}
1377
Ben Widawskya81cc002013-01-18 12:30:31 -08001378extern int intel_iommu_gfx_mapped;
1379/* Certain Gen5 chipsets require require idling the GPU before
1380 * unmapping anything from the GTT when VT-d is enabled.
1381 */
1382static inline bool needs_idle_maps(struct drm_device *dev)
1383{
1384#ifdef CONFIG_INTEL_IOMMU
1385 /* Query intel_iommu to see if we need the workaround. Presumably that
1386 * was loaded first.
1387 */
1388 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1389 return true;
1390#endif
1391 return false;
1392}
1393
Ben Widawsky5c042282011-10-17 15:51:55 -07001394static bool do_idling(struct drm_i915_private *dev_priv)
1395{
1396 bool ret = dev_priv->mm.interruptible;
1397
Ben Widawskya81cc002013-01-18 12:30:31 -08001398 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001399 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001400 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001401 DRM_ERROR("Couldn't idle GPU\n");
1402 /* Wait a bit, in hopes it avoids the hang */
1403 udelay(10);
1404 }
1405 }
1406
1407 return ret;
1408}
1409
1410static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1411{
Ben Widawskya81cc002013-01-18 12:30:31 -08001412 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001413 dev_priv->mm.interruptible = interruptible;
1414}
1415
Ben Widawsky828c7902013-10-16 09:21:30 -07001416void i915_check_and_clear_faults(struct drm_device *dev)
1417{
1418 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001419 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07001420 int i;
1421
1422 if (INTEL_INFO(dev)->gen < 6)
1423 return;
1424
1425 for_each_ring(ring, dev_priv, i) {
1426 u32 fault_reg;
1427 fault_reg = I915_READ(RING_FAULT_REG(ring));
1428 if (fault_reg & RING_FAULT_VALID) {
1429 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02001430 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07001431 "\tAddress space: %s\n"
1432 "\tSource ID: %d\n"
1433 "\tType: %d\n",
1434 fault_reg & PAGE_MASK,
1435 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1436 RING_FAULT_SRCID(fault_reg),
1437 RING_FAULT_FAULT_TYPE(fault_reg));
1438 I915_WRITE(RING_FAULT_REG(ring),
1439 fault_reg & ~RING_FAULT_VALID);
1440 }
1441 }
1442 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1443}
1444
Chris Wilson91e56492014-09-25 10:13:12 +01001445static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
1446{
1447 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
1448 intel_gtt_chipset_flush();
1449 } else {
1450 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1451 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1452 }
1453}
1454
Ben Widawsky828c7902013-10-16 09:21:30 -07001455void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1456{
1457 struct drm_i915_private *dev_priv = dev->dev_private;
1458
1459 /* Don't bother messing with faults pre GEN6 as we have little
1460 * documentation supporting that it's a good idea.
1461 */
1462 if (INTEL_INFO(dev)->gen < 6)
1463 return;
1464
1465 i915_check_and_clear_faults(dev);
1466
1467 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001468 dev_priv->gtt.base.start,
1469 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001470 true);
Chris Wilson91e56492014-09-25 10:13:12 +01001471
1472 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07001473}
1474
Daniel Vetter76aaf222010-11-05 22:23:30 +01001475void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1476{
1477 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001478 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001479 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001480
Ben Widawsky828c7902013-10-16 09:21:30 -07001481 i915_check_and_clear_faults(dev);
1482
Chris Wilsonbee4a182011-01-21 10:54:32 +00001483 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001484 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001485 dev_priv->gtt.base.start,
1486 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001487 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001488
Ben Widawsky35c20a62013-05-31 11:28:48 -07001489 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001490 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1491 &dev_priv->gtt.base);
1492 if (!vma)
1493 continue;
1494
Chris Wilson2c225692013-08-09 12:26:45 +01001495 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001496 /* The bind_vma code tries to be smart about tracking mappings.
1497 * Unfortunately above, we've just wiped out the mappings
1498 * without telling our object about it. So we need to fake it.
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001499 *
1500 * Bind is not expected to fail since this is only called on
1501 * resume and assumption is all requirements exist already.
Ben Widawsky6f65e292013-12-06 14:10:56 -08001502 */
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001503 vma->bound &= ~GLOBAL_BIND;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001504 WARN_ON(i915_vma_bind(vma, obj->cache_level, GLOBAL_BIND));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001505 }
1506
Ben Widawsky80da2162013-12-06 14:11:17 -08001507
Ben Widawskya2319c02014-03-18 16:09:37 -07001508 if (INTEL_INFO(dev)->gen >= 8) {
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001509 if (IS_CHERRYVIEW(dev))
1510 chv_setup_private_ppat(dev_priv);
1511 else
1512 bdw_setup_private_ppat(dev_priv);
1513
Ben Widawsky80da2162013-12-06 14:11:17 -08001514 return;
Ben Widawskya2319c02014-03-18 16:09:37 -07001515 }
Ben Widawsky80da2162013-12-06 14:11:17 -08001516
1517 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1518 /* TODO: Perhaps it shouldn't be gen6 specific */
1519 if (i915_is_ggtt(vm)) {
1520 if (dev_priv->mm.aliasing_ppgtt)
1521 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1522 continue;
1523 }
1524
1525 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001526 }
1527
Chris Wilson91e56492014-09-25 10:13:12 +01001528 i915_ggtt_flush(dev_priv);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001529}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001530
Daniel Vetter74163902012-02-15 23:50:21 +01001531int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001532{
Chris Wilson9da3da62012-06-01 15:20:22 +01001533 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001534 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001535
1536 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1537 obj->pages->sgl, obj->pages->nents,
1538 PCI_DMA_BIDIRECTIONAL))
1539 return -ENOSPC;
1540
1541 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001542}
1543
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001544static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1545{
1546#ifdef writeq
1547 writeq(pte, addr);
1548#else
1549 iowrite32((u32)pte, addr);
1550 iowrite32(pte >> 32, addr + 4);
1551#endif
1552}
1553
1554static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1555 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001556 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301557 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001558{
1559 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001560 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001561 gen8_gtt_pte_t __iomem *gtt_entries =
1562 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1563 int i = 0;
1564 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001565 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001566
1567 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1568 addr = sg_dma_address(sg_iter.sg) +
1569 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1570 gen8_set_pte(&gtt_entries[i],
1571 gen8_pte_encode(addr, level, true));
1572 i++;
1573 }
1574
1575 /*
1576 * XXX: This serves as a posting read to make sure that the PTE has
1577 * actually been updated. There is some concern that even though
1578 * registers and PTEs are within the same BAR that they are potentially
1579 * of NUMA access patterns. Therefore, even with the way we assume
1580 * hardware should work, we must keep this posting read for paranoia.
1581 */
1582 if (i != 0)
1583 WARN_ON(readq(&gtt_entries[i-1])
1584 != gen8_pte_encode(addr, level, true));
1585
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001586 /* This next bit makes the above posting read even more important. We
1587 * want to flush the TLBs only after we're certain all the PTE updates
1588 * have finished.
1589 */
1590 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1591 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001592}
1593
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001594/*
1595 * Binds an object into the global gtt with the specified cache level. The object
1596 * will be accessible to the GPU via commands whose operands reference offsets
1597 * within the global GTT as well as accessible by the GPU through the GMADR
1598 * mapped BAR (dev_priv->mm.gtt->gtt).
1599 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001600static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001601 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001602 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301603 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001604{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001605 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001606 unsigned first_entry = start >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001607 gen6_gtt_pte_t __iomem *gtt_entries =
1608 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001609 int i = 0;
1610 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001611 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001612
Imre Deak6e995e22013-02-18 19:28:04 +02001613 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001614 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301615 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001616 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001617 }
1618
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001619 /* XXX: This serves as a posting read to make sure that the PTE has
1620 * actually been updated. There is some concern that even though
1621 * registers and PTEs are within the same BAR that they are potentially
1622 * of NUMA access patterns. Therefore, even with the way we assume
1623 * hardware should work, we must keep this posting read for paranoia.
1624 */
Pavel Machek57007df2014-07-28 13:20:58 +02001625 if (i != 0) {
1626 unsigned long gtt = readl(&gtt_entries[i-1]);
1627 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1628 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001629
1630 /* This next bit makes the above posting read even more important. We
1631 * want to flush the TLBs only after we're certain all the PTE updates
1632 * have finished.
1633 */
1634 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1635 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001636}
1637
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001638static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001639 uint64_t start,
1640 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001641 bool use_scratch)
1642{
1643 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001644 unsigned first_entry = start >> PAGE_SHIFT;
1645 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001646 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1647 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1648 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1649 int i;
1650
1651 if (WARN(num_entries > max_entries,
1652 "First entry = %d; Num entries = %d (max=%d)\n",
1653 first_entry, num_entries, max_entries))
1654 num_entries = max_entries;
1655
1656 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1657 I915_CACHE_LLC,
1658 use_scratch);
1659 for (i = 0; i < num_entries; i++)
1660 gen8_set_pte(&gtt_base[i], scratch_pte);
1661 readl(gtt_base);
1662}
1663
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001664static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001665 uint64_t start,
1666 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001667 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001668{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001669 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001670 unsigned first_entry = start >> PAGE_SHIFT;
1671 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawskye7c2b582013-04-08 18:43:48 -07001672 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1673 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001674 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001675 int i;
1676
1677 if (WARN(num_entries > max_entries,
1678 "First entry = %d; Num entries = %d (max=%d)\n",
1679 first_entry, num_entries, max_entries))
1680 num_entries = max_entries;
1681
Akash Goel24f3a8c2014-06-17 10:59:42 +05301682 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07001683
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001684 for (i = 0; i < num_entries; i++)
1685 iowrite32(scratch_pte, &gtt_base[i]);
1686 readl(gtt_base);
1687}
1688
Ben Widawsky6f65e292013-12-06 14:10:56 -08001689
1690static void i915_ggtt_bind_vma(struct i915_vma *vma,
1691 enum i915_cache_level cache_level,
1692 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001693{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001694 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001695 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1696 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1697
Ben Widawsky6f65e292013-12-06 14:10:56 -08001698 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001699 intel_gtt_insert_sg_entries(vma->ggtt_view.pages, entry, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001700 vma->bound = GLOBAL_BIND;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001701}
1702
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001703static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001704 uint64_t start,
1705 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001706 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001707{
Ben Widawsky782f1492014-02-20 11:50:33 -08001708 unsigned first_entry = start >> PAGE_SHIFT;
1709 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001710 intel_gtt_clear_range(first_entry, num_entries);
1711}
1712
Ben Widawsky6f65e292013-12-06 14:10:56 -08001713static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001714{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001715 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1716 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001717
Ben Widawsky6f65e292013-12-06 14:10:56 -08001718 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001719 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001720 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001721}
1722
Ben Widawsky6f65e292013-12-06 14:10:56 -08001723static void ggtt_bind_vma(struct i915_vma *vma,
1724 enum i915_cache_level cache_level,
1725 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001726{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001727 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001728 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001729 struct drm_i915_gem_object *obj = vma->obj;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001730 struct sg_table *pages = obj->pages;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001731
Akash Goel24f3a8c2014-06-17 10:59:42 +05301732 /* Currently applicable only to VLV */
1733 if (obj->gt_ro)
1734 flags |= PTE_READ_ONLY;
1735
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001736 if (i915_is_ggtt(vma->vm))
1737 pages = vma->ggtt_view.pages;
1738
Ben Widawsky6f65e292013-12-06 14:10:56 -08001739 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1740 * or we have a global mapping already but the cacheability flags have
1741 * changed, set the global PTEs.
1742 *
1743 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1744 * instead if none of the above hold true.
1745 *
1746 * NB: A global mapping should only be needed for special regions like
1747 * "gtt mappable", SNB errata, or if specified via special execbuf
1748 * flags. At all other times, the GPU will use the aliasing PPGTT.
1749 */
1750 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001751 if (!(vma->bound & GLOBAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08001752 (cache_level != obj->cache_level)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001753 vma->vm->insert_entries(vma->vm, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001754 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301755 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001756 vma->bound |= GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001757 }
1758 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001759
Ben Widawsky6f65e292013-12-06 14:10:56 -08001760 if (dev_priv->mm.aliasing_ppgtt &&
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001761 (!(vma->bound & LOCAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08001762 (cache_level != obj->cache_level))) {
1763 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001764 appgtt->base.insert_entries(&appgtt->base, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001765 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301766 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001767 vma->bound |= LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001768 }
1769}
1770
1771static void ggtt_unbind_vma(struct i915_vma *vma)
1772{
1773 struct drm_device *dev = vma->vm->dev;
1774 struct drm_i915_private *dev_priv = dev->dev_private;
1775 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001776
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001777 if (vma->bound & GLOBAL_BIND) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001778 vma->vm->clear_range(vma->vm,
1779 vma->node.start,
1780 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001781 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001782 vma->bound &= ~GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001783 }
1784
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001785 if (vma->bound & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001786 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1787 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001788 vma->node.start,
1789 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001790 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001791 vma->bound &= ~LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001792 }
Daniel Vetter74163902012-02-15 23:50:21 +01001793}
1794
1795void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1796{
Ben Widawsky5c042282011-10-17 15:51:55 -07001797 struct drm_device *dev = obj->base.dev;
1798 struct drm_i915_private *dev_priv = dev->dev_private;
1799 bool interruptible;
1800
1801 interruptible = do_idling(dev_priv);
1802
Chris Wilson9da3da62012-06-01 15:20:22 +01001803 if (!obj->has_dma_mapping)
1804 dma_unmap_sg(&dev->pdev->dev,
1805 obj->pages->sgl, obj->pages->nents,
1806 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001807
1808 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001809}
Daniel Vetter644ec022012-03-26 09:45:40 +02001810
Chris Wilson42d6ab42012-07-26 11:49:32 +01001811static void i915_gtt_color_adjust(struct drm_mm_node *node,
1812 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01001813 u64 *start,
1814 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01001815{
1816 if (node->color != color)
1817 *start += 4096;
1818
1819 if (!list_empty(&node->node_list)) {
1820 node = list_entry(node->node_list.next,
1821 struct drm_mm_node,
1822 node_list);
1823 if (node->allocated && node->color != color)
1824 *end -= 4096;
1825 }
1826}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001827
Daniel Vetterf548c0e2014-11-19 21:40:13 +01001828static int i915_gem_setup_global_gtt(struct drm_device *dev,
1829 unsigned long start,
1830 unsigned long mappable_end,
1831 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001832{
Ben Widawskye78891c2013-01-25 16:41:04 -08001833 /* Let GEM Manage all of the aperture.
1834 *
1835 * However, leave one page at the end still bound to the scratch page.
1836 * There are a number of places where the hardware apparently prefetches
1837 * past the end of the object, and we've seen multiple hangs with the
1838 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1839 * aperture. One page should be enough to keep any prefetching inside
1840 * of the aperture.
1841 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001842 struct drm_i915_private *dev_priv = dev->dev_private;
1843 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001844 struct drm_mm_node *entry;
1845 struct drm_i915_gem_object *obj;
1846 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02001847 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02001848
Ben Widawsky35451cb2013-01-17 12:45:13 -08001849 BUG_ON(mappable_end > end);
1850
Chris Wilsoned2f3452012-11-15 11:32:19 +00001851 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001852 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08001853
1854 dev_priv->gtt.base.start = start;
1855 dev_priv->gtt.base.total = end - start;
1856
1857 if (intel_vgpu_active(dev)) {
1858 ret = intel_vgt_balloon(dev);
1859 if (ret)
1860 return ret;
1861 }
1862
Chris Wilson42d6ab42012-07-26 11:49:32 +01001863 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001864 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001865
Chris Wilsoned2f3452012-11-15 11:32:19 +00001866 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001867 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001868 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001869
Ben Widawskyedd41a82013-07-05 14:41:05 -07001870 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001871 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001872
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001873 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001874 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001875 if (ret) {
1876 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
1877 return ret;
1878 }
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001879 vma->bound |= GLOBAL_BIND;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001880 }
1881
Chris Wilsoned2f3452012-11-15 11:32:19 +00001882 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001883 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00001884 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1885 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08001886 ggtt_vm->clear_range(ggtt_vm, hole_start,
1887 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001888 }
1889
1890 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08001891 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001892
Daniel Vetterfa76da32014-08-06 20:19:54 +02001893 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
1894 struct i915_hw_ppgtt *ppgtt;
1895
1896 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1897 if (!ppgtt)
1898 return -ENOMEM;
1899
1900 ret = __hw_ppgtt_init(dev, ppgtt);
1901 if (ret != 0)
1902 return ret;
1903
1904 dev_priv->mm.aliasing_ppgtt = ppgtt;
1905 }
1906
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001907 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001908}
1909
Ben Widawskyd7e50082012-12-18 10:31:25 -08001910void i915_gem_init_global_gtt(struct drm_device *dev)
1911{
1912 struct drm_i915_private *dev_priv = dev->dev_private;
1913 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001914
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001915 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08001916 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08001917
Ben Widawskye78891c2013-01-25 16:41:04 -08001918 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001919}
1920
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001921void i915_global_gtt_cleanup(struct drm_device *dev)
1922{
1923 struct drm_i915_private *dev_priv = dev->dev_private;
1924 struct i915_address_space *vm = &dev_priv->gtt.base;
1925
Daniel Vetter70e32542014-08-06 15:04:57 +02001926 if (dev_priv->mm.aliasing_ppgtt) {
1927 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1928
1929 ppgtt->base.cleanup(&ppgtt->base);
1930 }
1931
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001932 if (drm_mm_initialized(&vm->mm)) {
Yu Zhang5dda8fa2015-02-10 19:05:48 +08001933 if (intel_vgpu_active(dev))
1934 intel_vgt_deballoon();
1935
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001936 drm_mm_takedown(&vm->mm);
1937 list_del(&vm->global_link);
1938 }
1939
1940 vm->cleanup(vm);
1941}
Daniel Vetter70e32542014-08-06 15:04:57 +02001942
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001943static int setup_scratch_page(struct drm_device *dev)
1944{
1945 struct drm_i915_private *dev_priv = dev->dev_private;
1946 struct page *page;
1947 dma_addr_t dma_addr;
1948
1949 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1950 if (page == NULL)
1951 return -ENOMEM;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001952 set_pages_uc(page, 1);
1953
1954#ifdef CONFIG_INTEL_IOMMU
1955 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1956 PCI_DMA_BIDIRECTIONAL);
1957 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1958 return -EINVAL;
1959#else
1960 dma_addr = page_to_phys(page);
1961#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001962 dev_priv->gtt.base.scratch.page = page;
1963 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001964
1965 return 0;
1966}
1967
1968static void teardown_scratch_page(struct drm_device *dev)
1969{
1970 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001971 struct page *page = dev_priv->gtt.base.scratch.page;
1972
1973 set_pages_wb(page, 1);
1974 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001975 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001976 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001977}
1978
1979static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1980{
1981 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1982 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1983 return snb_gmch_ctl << 20;
1984}
1985
Ben Widawsky9459d252013-11-03 16:53:55 -08001986static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1987{
1988 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1989 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1990 if (bdw_gmch_ctl)
1991 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07001992
1993#ifdef CONFIG_X86_32
1994 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
1995 if (bdw_gmch_ctl > 4)
1996 bdw_gmch_ctl = 4;
1997#endif
1998
Ben Widawsky9459d252013-11-03 16:53:55 -08001999 return bdw_gmch_ctl << 20;
2000}
2001
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002002static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2003{
2004 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2005 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2006
2007 if (gmch_ctrl)
2008 return 1 << (20 + gmch_ctrl);
2009
2010 return 0;
2011}
2012
Ben Widawskybaa09f52013-01-24 13:49:57 -08002013static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002014{
2015 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2016 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2017 return snb_gmch_ctl << 25; /* 32 MB units */
2018}
2019
Ben Widawsky9459d252013-11-03 16:53:55 -08002020static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2021{
2022 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2023 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2024 return bdw_gmch_ctl << 25; /* 32 MB units */
2025}
2026
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002027static size_t chv_get_stolen_size(u16 gmch_ctrl)
2028{
2029 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2030 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2031
2032 /*
2033 * 0x0 to 0x10: 32MB increments starting at 0MB
2034 * 0x11 to 0x16: 4MB increments starting at 8MB
2035 * 0x17 to 0x1d: 4MB increments start at 36MB
2036 */
2037 if (gmch_ctrl < 0x11)
2038 return gmch_ctrl << 25;
2039 else if (gmch_ctrl < 0x17)
2040 return (gmch_ctrl - 0x11 + 2) << 22;
2041 else
2042 return (gmch_ctrl - 0x17 + 9) << 22;
2043}
2044
Damien Lespiau66375012014-01-09 18:02:46 +00002045static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2046{
2047 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2048 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2049
2050 if (gen9_gmch_ctl < 0xf0)
2051 return gen9_gmch_ctl << 25; /* 32 MB units */
2052 else
2053 /* 4MB increments starting at 0xf0 for 4MB */
2054 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2055}
2056
Ben Widawsky63340132013-11-04 19:32:22 -08002057static int ggtt_probe_common(struct drm_device *dev,
2058 size_t gtt_size)
2059{
2060 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002061 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08002062 int ret;
2063
2064 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002065 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08002066 (pci_resource_len(dev->pdev, 0) / 2);
2067
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002068 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08002069 if (!dev_priv->gtt.gsm) {
2070 DRM_ERROR("Failed to map the gtt page table\n");
2071 return -ENOMEM;
2072 }
2073
2074 ret = setup_scratch_page(dev);
2075 if (ret) {
2076 DRM_ERROR("Scratch setup failed\n");
2077 /* iounmap will also get called at remove, but meh */
2078 iounmap(dev_priv->gtt.gsm);
2079 }
2080
2081 return ret;
2082}
2083
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002084/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2085 * bits. When using advanced contexts each context stores its own PAT, but
2086 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002087static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002088{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002089 uint64_t pat;
2090
2091 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2092 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2093 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2094 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2095 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2096 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2097 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2098 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2099
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002100 if (!USES_PPGTT(dev_priv->dev))
2101 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2102 * so RTL will always use the value corresponding to
2103 * pat_sel = 000".
2104 * So let's disable cache for GGTT to avoid screen corruptions.
2105 * MOCS still can be used though.
2106 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2107 * before this patch, i.e. the same uncached + snooping access
2108 * like on gen6/7 seems to be in effect.
2109 * - So this just fixes blitter/render access. Again it looks
2110 * like it's not just uncached access, but uncached + snooping.
2111 * So we can still hold onto all our assumptions wrt cpu
2112 * clflushing on LLC machines.
2113 */
2114 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2115
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002116 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2117 * write would work. */
2118 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2119 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2120}
2121
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002122static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2123{
2124 uint64_t pat;
2125
2126 /*
2127 * Map WB on BDW to snooped on CHV.
2128 *
2129 * Only the snoop bit has meaning for CHV, the rest is
2130 * ignored.
2131 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002132 * The hardware will never snoop for certain types of accesses:
2133 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2134 * - PPGTT page tables
2135 * - some other special cycles
2136 *
2137 * As with BDW, we also need to consider the following for GT accesses:
2138 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2139 * so RTL will always use the value corresponding to
2140 * pat_sel = 000".
2141 * Which means we must set the snoop bit in PAT entry 0
2142 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002143 */
2144 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2145 GEN8_PPAT(1, 0) |
2146 GEN8_PPAT(2, 0) |
2147 GEN8_PPAT(3, 0) |
2148 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2149 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2150 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2151 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2152
2153 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2154 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2155}
2156
Ben Widawsky63340132013-11-04 19:32:22 -08002157static int gen8_gmch_probe(struct drm_device *dev,
2158 size_t *gtt_total,
2159 size_t *stolen,
2160 phys_addr_t *mappable_base,
2161 unsigned long *mappable_end)
2162{
2163 struct drm_i915_private *dev_priv = dev->dev_private;
2164 unsigned int gtt_size;
2165 u16 snb_gmch_ctl;
2166 int ret;
2167
2168 /* TODO: We're not aware of mappable constraints on gen8 yet */
2169 *mappable_base = pci_resource_start(dev->pdev, 2);
2170 *mappable_end = pci_resource_len(dev->pdev, 2);
2171
2172 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
2173 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
2174
2175 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2176
Damien Lespiau66375012014-01-09 18:02:46 +00002177 if (INTEL_INFO(dev)->gen >= 9) {
2178 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
2179 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2180 } else if (IS_CHERRYVIEW(dev)) {
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002181 *stolen = chv_get_stolen_size(snb_gmch_ctl);
2182 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2183 } else {
2184 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
2185 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2186 }
Ben Widawsky63340132013-11-04 19:32:22 -08002187
Ben Widawskyd31eb102013-11-02 21:07:17 -07002188 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002189
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002190 if (IS_CHERRYVIEW(dev))
2191 chv_setup_private_ppat(dev_priv);
2192 else
2193 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002194
Ben Widawsky63340132013-11-04 19:32:22 -08002195 ret = ggtt_probe_common(dev, gtt_size);
2196
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002197 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2198 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08002199
2200 return ret;
2201}
2202
Ben Widawskybaa09f52013-01-24 13:49:57 -08002203static int gen6_gmch_probe(struct drm_device *dev,
2204 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002205 size_t *stolen,
2206 phys_addr_t *mappable_base,
2207 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002208{
2209 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002210 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002211 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002212 int ret;
2213
Ben Widawsky41907dd2013-02-08 11:32:47 -08002214 *mappable_base = pci_resource_start(dev->pdev, 2);
2215 *mappable_end = pci_resource_len(dev->pdev, 2);
2216
Ben Widawskybaa09f52013-01-24 13:49:57 -08002217 /* 64/512MB is the current min/max we actually know of, but this is just
2218 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002219 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08002220 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08002221 DRM_ERROR("Unknown GMADR size (%lx)\n",
2222 dev_priv->gtt.mappable_end);
2223 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002224 }
2225
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002226 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2227 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08002228 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002229
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07002230 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002231
Ben Widawsky63340132013-11-04 19:32:22 -08002232 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002233 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
2234
Ben Widawsky63340132013-11-04 19:32:22 -08002235 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002236
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002237 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2238 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002239
2240 return ret;
2241}
2242
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002243static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002244{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002245
2246 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002247
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002248 iounmap(gtt->gsm);
2249 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002250}
2251
2252static int i915_gmch_probe(struct drm_device *dev,
2253 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002254 size_t *stolen,
2255 phys_addr_t *mappable_base,
2256 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002257{
2258 struct drm_i915_private *dev_priv = dev->dev_private;
2259 int ret;
2260
Ben Widawskybaa09f52013-01-24 13:49:57 -08002261 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2262 if (!ret) {
2263 DRM_ERROR("failed to set up gmch\n");
2264 return -EIO;
2265 }
2266
Ben Widawsky41907dd2013-02-08 11:32:47 -08002267 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002268
2269 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002270 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002271
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002272 if (unlikely(dev_priv->gtt.do_idle_maps))
2273 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2274
Ben Widawskybaa09f52013-01-24 13:49:57 -08002275 return 0;
2276}
2277
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002278static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002279{
2280 intel_gmch_remove();
2281}
2282
2283int i915_gem_gtt_init(struct drm_device *dev)
2284{
2285 struct drm_i915_private *dev_priv = dev->dev_private;
2286 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002287 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002288
Ben Widawskybaa09f52013-01-24 13:49:57 -08002289 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002290 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002291 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002292 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002293 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002294 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002295 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002296 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002297 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002298 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002299 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002300 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002301 else if (INTEL_INFO(dev)->gen >= 7)
2302 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002303 else
Chris Wilson350ec882013-08-06 13:17:02 +01002304 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002305 } else {
2306 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2307 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002308 }
2309
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002310 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002311 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002312 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002313 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002314
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002315 gtt->base.dev = dev;
2316
Ben Widawskybaa09f52013-01-24 13:49:57 -08002317 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002318 DRM_INFO("Memory usable by graphics device = %zdM\n",
2319 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002320 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2321 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002322#ifdef CONFIG_INTEL_IOMMU
2323 if (intel_iommu_gfx_mapped)
2324 DRM_INFO("VT-d active for gfx access\n");
2325#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002326 /*
2327 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2328 * user's requested state against the hardware/driver capabilities. We
2329 * do this now so that we can print out any log messages once rather
2330 * than every time we check intel_enable_ppgtt().
2331 */
2332 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2333 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002334
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002335 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002336}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002337
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002338static struct i915_vma *
2339__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2340 struct i915_address_space *vm,
2341 const struct i915_ggtt_view *ggtt_view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002342{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002343 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002344
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002345 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2346 return ERR_PTR(-EINVAL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002347 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2348 if (vma == NULL)
2349 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002350
Ben Widawsky6f65e292013-12-06 14:10:56 -08002351 INIT_LIST_HEAD(&vma->vma_link);
2352 INIT_LIST_HEAD(&vma->mm_list);
2353 INIT_LIST_HEAD(&vma->exec_list);
2354 vma->vm = vm;
2355 vma->obj = obj;
2356
Rodrigo Vivib1252bc2014-12-03 04:55:29 -08002357 if (INTEL_INFO(vm->dev)->gen >= 6) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002358 if (i915_is_ggtt(vm)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002359 vma->ggtt_view = *ggtt_view;
2360
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002361 vma->unbind_vma = ggtt_unbind_vma;
2362 vma->bind_vma = ggtt_bind_vma;
2363 } else {
2364 vma->unbind_vma = ppgtt_unbind_vma;
2365 vma->bind_vma = ppgtt_bind_vma;
2366 }
Rodrigo Vivib1252bc2014-12-03 04:55:29 -08002367 } else {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002368 BUG_ON(!i915_is_ggtt(vm));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002369 vma->ggtt_view = *ggtt_view;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002370 vma->unbind_vma = i915_ggtt_unbind_vma;
2371 vma->bind_vma = i915_ggtt_bind_vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002372 }
2373
Tvrtko Ursulinf7635662014-12-03 14:59:24 +00002374 list_add_tail(&vma->vma_link, &obj->vma_list);
2375 if (!i915_is_ggtt(vm))
Michel Thierrye07f0552014-08-19 15:49:41 +01002376 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Ben Widawsky6f65e292013-12-06 14:10:56 -08002377
2378 return vma;
2379}
2380
2381struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002382i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2383 struct i915_address_space *vm)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002384{
2385 struct i915_vma *vma;
2386
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002387 vma = i915_gem_obj_to_vma(obj, vm);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002388 if (!vma)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002389 vma = __i915_gem_vma_create(obj, vm,
2390 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002391
2392 return vma;
2393}
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002394
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002395struct i915_vma *
2396i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
2397 const struct i915_ggtt_view *view)
2398{
2399 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
2400 struct i915_vma *vma;
2401
2402 if (WARN_ON(!view))
2403 return ERR_PTR(-EINVAL);
2404
2405 vma = i915_gem_obj_to_ggtt_view(obj, view);
2406
2407 if (IS_ERR(vma))
2408 return vma;
2409
2410 if (!vma)
2411 vma = __i915_gem_vma_create(obj, ggtt, view);
2412
2413 return vma;
2414
2415}
2416
2417
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002418static inline
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002419int i915_get_ggtt_vma_pages(struct i915_vma *vma)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002420{
2421 if (vma->ggtt_view.pages)
2422 return 0;
2423
2424 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
2425 vma->ggtt_view.pages = vma->obj->pages;
2426 else
2427 WARN_ONCE(1, "GGTT view %u not implemented!\n",
2428 vma->ggtt_view.type);
2429
2430 if (!vma->ggtt_view.pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002431 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002432 vma->ggtt_view.type);
2433 return -EINVAL;
2434 }
2435
2436 return 0;
2437}
2438
2439/**
2440 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
2441 * @vma: VMA to map
2442 * @cache_level: mapping cache level
2443 * @flags: flags like global or local mapping
2444 *
2445 * DMA addresses are taken from the scatter-gather table of this object (or of
2446 * this VMA in case of non-default GGTT views) and PTE entries set up.
2447 * Note that DMA addresses are also the only part of the SG table we care about.
2448 */
2449int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2450 u32 flags)
2451{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002452 if (i915_is_ggtt(vma->vm)) {
2453 int ret = i915_get_ggtt_vma_pages(vma);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002454
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002455 if (ret)
2456 return ret;
2457 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002458
2459 vma->bind_vma(vma, cache_level, flags);
2460
2461 return 0;
2462}