blob: 645c3636c5712eacd19673385dbd42cd06e16f86 [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
Michel Thierry07749ef2015-03-16 16:00:54 +0000151static inline gen8_pte_t gen8_pte_encode(dma_addr_t addr,
152 enum i915_cache_level level,
153 bool valid)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700154{
Michel Thierry07749ef2015-03-16 16:00:54 +0000155 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700156 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
Michel Thierry07749ef2015-03-16 16:00:54 +0000173static inline gen8_pde_t gen8_pde_encode(struct drm_device *dev,
174 dma_addr_t addr,
175 enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800176{
Michel Thierry07749ef2015-03-16 16:00:54 +0000177 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800178 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
Michel Thierry07749ef2015-03-16 16:00:54 +0000186static gen6_pte_t snb_pte_encode(dma_addr_t addr,
187 enum i915_cache_level level,
188 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700189{
Michel Thierry07749ef2015-03-16 16:00:54 +0000190 gen6_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
Michel Thierry07749ef2015-03-16 16:00:54 +0000208static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
209 enum i915_cache_level level,
210 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100211{
Michel Thierry07749ef2015-03-16 16:00:54 +0000212 gen6_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
Michel Thierry07749ef2015-03-16 16:00:54 +0000232static gen6_pte_t byt_pte_encode(dma_addr_t addr,
233 enum i915_cache_level level,
234 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700235{
Michel Thierry07749ef2015-03-16 16:00:54 +0000236 gen6_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
Michel Thierry07749ef2015-03-16 16:00:54 +0000248static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
249 enum i915_cache_level level,
250 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700251{
Michel Thierry07749ef2015-03-16 16:00:54 +0000252 gen6_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
Michel Thierry07749ef2015-03-16 16:00:54 +0000261static gen6_pte_t iris_pte_encode(dma_addr_t addr,
262 enum i915_cache_level level,
263 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700264{
Michel Thierry07749ef2015-03-16 16:00:54 +0000265 gen6_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
Ben Widawsky678d96f2015-03-16 16:00:56 +0000282#define i915_dma_unmap_single(px, dev) \
283 __i915_dma_unmap_single((px)->daddr, dev)
284
285static inline void __i915_dma_unmap_single(dma_addr_t daddr,
286 struct drm_device *dev)
287{
288 struct device *device = &dev->pdev->dev;
289
290 dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
291}
292
293/**
294 * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc.
295 * @px: Page table/dir/etc to get a DMA map for
296 * @dev: drm device
297 *
298 * Page table allocations are unified across all gens. They always require a
299 * single 4k allocation, as well as a DMA mapping. If we keep the structs
300 * symmetric here, the simple macro covers us for every page table type.
301 *
302 * Return: 0 if success.
303 */
304#define i915_dma_map_single(px, dev) \
305 i915_dma_map_page_single((px)->page, (dev), &(px)->daddr)
306
307static inline int i915_dma_map_page_single(struct page *page,
308 struct drm_device *dev,
309 dma_addr_t *daddr)
310{
311 struct device *device = &dev->pdev->dev;
312
313 *daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
314 return dma_mapping_error(device, *daddr);
315}
316
317static void unmap_and_free_pt(struct i915_page_table_entry *pt,
318 struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000319{
320 if (WARN_ON(!pt->page))
321 return;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000322
323 i915_dma_unmap_single(pt, dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000324 __free_page(pt->page);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000325 kfree(pt->used_ptes);
Ben Widawsky06fda602015-02-24 16:22:36 +0000326 kfree(pt);
327}
328
Michel Thierry06dc68d2015-02-24 16:22:37 +0000329static struct i915_page_table_entry *alloc_pt_single(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000330{
331 struct i915_page_table_entry *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000332 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
333 GEN8_PTES : GEN6_PTES;
334 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000335
336 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
337 if (!pt)
338 return ERR_PTR(-ENOMEM);
339
Ben Widawsky678d96f2015-03-16 16:00:56 +0000340 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
341 GFP_KERNEL);
342
343 if (!pt->used_ptes)
344 goto fail_bitmap;
345
Ben Widawsky06fda602015-02-24 16:22:36 +0000346 pt->page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000347 if (!pt->page)
348 goto fail_page;
349
350 ret = i915_dma_map_single(pt, dev);
351 if (ret)
352 goto fail_dma;
Ben Widawsky06fda602015-02-24 16:22:36 +0000353
354 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000355
356fail_dma:
357 __free_page(pt->page);
358fail_page:
359 kfree(pt->used_ptes);
360fail_bitmap:
361 kfree(pt);
362
363 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000364}
365
366/**
367 * alloc_pt_range() - Allocate a multiple page tables
368 * @pd: The page directory which will have at least @count entries
369 * available to point to the allocated page tables.
370 * @pde: First page directory entry for which we are allocating.
371 * @count: Number of pages to allocate.
Michel Thierry719cd212015-02-26 11:28:13 +0000372 * @dev: DRM device.
Ben Widawsky06fda602015-02-24 16:22:36 +0000373 *
374 * Allocates multiple page table pages and sets the appropriate entries in the
375 * page table structure within the page directory. Function cleans up after
376 * itself on any failures.
377 *
378 * Return: 0 if allocation succeeded.
379 */
Michel Thierry06dc68d2015-02-24 16:22:37 +0000380static int alloc_pt_range(struct i915_page_directory_entry *pd, uint16_t pde, size_t count,
381 struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000382{
383 int i, ret;
384
385 /* 512 is the max page tables per page_directory on any platform. */
Michel Thierry07749ef2015-03-16 16:00:54 +0000386 if (WARN_ON(pde + count > I915_PDES))
Ben Widawsky06fda602015-02-24 16:22:36 +0000387 return -EINVAL;
388
389 for (i = pde; i < pde + count; i++) {
Michel Thierry06dc68d2015-02-24 16:22:37 +0000390 struct i915_page_table_entry *pt = alloc_pt_single(dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000391
392 if (IS_ERR(pt)) {
393 ret = PTR_ERR(pt);
394 goto err_out;
395 }
396 WARN(pd->page_table[i],
Dan Carpenter686135d2015-02-26 19:53:54 +0300397 "Leaking page directory entry %d (%p)\n",
Ben Widawsky06fda602015-02-24 16:22:36 +0000398 i, pd->page_table[i]);
399 pd->page_table[i] = pt;
400 }
401
402 return 0;
403
404err_out:
405 while (i-- > pde)
Michel Thierry06dc68d2015-02-24 16:22:37 +0000406 unmap_and_free_pt(pd->page_table[i], dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000407 return ret;
408}
409
410static void unmap_and_free_pd(struct i915_page_directory_entry *pd)
411{
412 if (pd->page) {
413 __free_page(pd->page);
414 kfree(pd);
415 }
416}
417
418static struct i915_page_directory_entry *alloc_pd_single(void)
419{
420 struct i915_page_directory_entry *pd;
421
422 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
423 if (!pd)
424 return ERR_PTR(-ENOMEM);
425
426 pd->page = alloc_page(GFP_KERNEL | __GFP_ZERO);
427 if (!pd->page) {
428 kfree(pd);
429 return ERR_PTR(-ENOMEM);
430 }
431
432 return pd;
433}
434
Ben Widawsky94e409c2013-11-04 22:29:36 -0800435/* Broadwell Page Directory Pointer Descriptors */
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100436static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100437 uint64_t val)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800438{
439 int ret;
440
441 BUG_ON(entry >= 4);
442
443 ret = intel_ring_begin(ring, 6);
444 if (ret)
445 return ret;
446
447 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
448 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
449 intel_ring_emit(ring, (u32)(val >> 32));
450 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
451 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
452 intel_ring_emit(ring, (u32)(val));
453 intel_ring_advance(ring);
454
455 return 0;
456}
457
Ben Widawskyeeb94882013-12-06 14:11:10 -0800458static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100459 struct intel_engine_cs *ring)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800460{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800461 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800462
463 /* bit of a hack to find the actual last used pd */
Michel Thierry07749ef2015-03-16 16:00:54 +0000464 int used_pd = ppgtt->num_pd_entries / I915_PDES;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800465
Ben Widawsky94e409c2013-11-04 22:29:36 -0800466 for (i = used_pd - 1; i >= 0; i--) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000467 dma_addr_t addr = ppgtt->pdp.page_directory[i]->daddr;
McAulay, Alistair6689c162014-08-15 18:51:35 +0100468 ret = gen8_write_pdp(ring, i, addr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800469 if (ret)
470 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800471 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800472
Ben Widawskyeeb94882013-12-06 14:11:10 -0800473 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800474}
475
Ben Widawsky459108b2013-11-02 21:07:23 -0700476static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800477 uint64_t start,
478 uint64_t length,
Ben Widawsky459108b2013-11-02 21:07:23 -0700479 bool use_scratch)
480{
481 struct i915_hw_ppgtt *ppgtt =
482 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +0000483 gen8_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800484 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
485 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
486 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800487 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700488 unsigned last_pte, i;
489
490 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
491 I915_CACHE_LLC, use_scratch);
492
493 while (num_entries) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000494 struct i915_page_directory_entry *pd;
495 struct i915_page_table_entry *pt;
496 struct page *page_table;
497
498 if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
499 continue;
500
501 pd = ppgtt->pdp.page_directory[pdpe];
502
503 if (WARN_ON(!pd->page_table[pde]))
504 continue;
505
506 pt = pd->page_table[pde];
507
508 if (WARN_ON(!pt->page))
509 continue;
510
511 page_table = pt->page;
Ben Widawsky459108b2013-11-02 21:07:23 -0700512
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800513 last_pte = pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +0000514 if (last_pte > GEN8_PTES)
515 last_pte = GEN8_PTES;
Ben Widawsky459108b2013-11-02 21:07:23 -0700516
517 pt_vaddr = kmap_atomic(page_table);
518
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800519 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700520 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800521 num_entries--;
522 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700523
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300524 if (!HAS_LLC(ppgtt->base.dev))
525 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky459108b2013-11-02 21:07:23 -0700526 kunmap_atomic(pt_vaddr);
527
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800528 pte = 0;
Michel Thierry07749ef2015-03-16 16:00:54 +0000529 if (++pde == I915_PDES) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800530 pdpe++;
531 pde = 0;
532 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700533 }
534}
535
Ben Widawsky9df15b42013-11-02 21:07:24 -0700536static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
537 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800538 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530539 enum i915_cache_level cache_level, u32 unused)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700540{
541 struct i915_hw_ppgtt *ppgtt =
542 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +0000543 gen8_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800544 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
545 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
546 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700547 struct sg_page_iter sg_iter;
548
Chris Wilson6f1cc992013-12-31 15:50:31 +0000549 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700550
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800551 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Ben Widawsky76643602015-01-22 17:01:24 +0000552 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800553 break;
554
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000555 if (pt_vaddr == NULL) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000556 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[pdpe];
557 struct i915_page_table_entry *pt = pd->page_table[pde];
558 struct page *page_table = pt->page;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000559
560 pt_vaddr = kmap_atomic(page_table);
561 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800562
563 pt_vaddr[pte] =
Chris Wilson6f1cc992013-12-31 15:50:31 +0000564 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
565 cache_level, true);
Michel Thierry07749ef2015-03-16 16:00:54 +0000566 if (++pte == GEN8_PTES) {
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300567 if (!HAS_LLC(ppgtt->base.dev))
568 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700569 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000570 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000571 if (++pde == I915_PDES) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800572 pdpe++;
573 pde = 0;
574 }
575 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700576 }
577 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300578 if (pt_vaddr) {
579 if (!HAS_LLC(ppgtt->base.dev))
580 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000581 kunmap_atomic(pt_vaddr);
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300582 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700583}
584
Michel Thierry06dc68d2015-02-24 16:22:37 +0000585static void gen8_free_page_tables(struct i915_page_directory_entry *pd, struct drm_device *dev)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800586{
587 int i;
588
Ben Widawsky06fda602015-02-24 16:22:36 +0000589 if (!pd->page)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800590 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800591
Michel Thierry07749ef2015-03-16 16:00:54 +0000592 for (i = 0; i < I915_PDES; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000593 if (WARN_ON(!pd->page_table[i]))
594 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800595
Michel Thierry06dc68d2015-02-24 16:22:37 +0000596 unmap_and_free_pt(pd->page_table[i], dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000597 pd->page_table[i] = NULL;
598 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000599}
600
601static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800602{
603 int i;
604
605 for (i = 0; i < ppgtt->num_pd_pages; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000606 if (WARN_ON(!ppgtt->pdp.page_directory[i]))
607 continue;
608
Michel Thierry06dc68d2015-02-24 16:22:37 +0000609 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000610 unmap_and_free_pd(ppgtt->pdp.page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800611 }
Ben Widawskyb45a6712014-02-12 14:28:44 -0800612}
613
614static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
615{
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800616 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800617 int i, j;
618
619 for (i = 0; i < ppgtt->num_pd_pages; i++) {
620 /* TODO: In the future we'll support sparse mappings, so this
621 * will have to change. */
Ben Widawsky06fda602015-02-24 16:22:36 +0000622 if (!ppgtt->pdp.page_directory[i]->daddr)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800623 continue;
624
Ben Widawsky06fda602015-02-24 16:22:36 +0000625 pci_unmap_page(hwdev, ppgtt->pdp.page_directory[i]->daddr, PAGE_SIZE,
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800626 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800627
Michel Thierry07749ef2015-03-16 16:00:54 +0000628 for (j = 0; j < I915_PDES; j++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000629 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i];
630 struct i915_page_table_entry *pt;
631 dma_addr_t addr;
632
633 if (WARN_ON(!pd->page_table[j]))
634 continue;
635
636 pt = pd->page_table[j];
637 addr = pt->daddr;
638
Ben Widawskyb45a6712014-02-12 14:28:44 -0800639 if (addr)
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800640 pci_unmap_page(hwdev, addr, PAGE_SIZE,
641 PCI_DMA_BIDIRECTIONAL);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800642 }
643 }
644}
645
Ben Widawsky37aca442013-11-04 20:47:32 -0800646static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
647{
648 struct i915_hw_ppgtt *ppgtt =
649 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800650
Ben Widawskyb45a6712014-02-12 14:28:44 -0800651 gen8_ppgtt_unmap_pages(ppgtt);
652 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800653}
654
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000655static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
656{
Ben Widawsky06fda602015-02-24 16:22:36 +0000657 int i, ret;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000658
659 for (i = 0; i < ppgtt->num_pd_pages; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000660 ret = alloc_pt_range(ppgtt->pdp.page_directory[i],
Michel Thierry07749ef2015-03-16 16:00:54 +0000661 0, I915_PDES, ppgtt->base.dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000662 if (ret)
663 goto unwind_out;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000664 }
665
666 return 0;
667
668unwind_out:
669 while (i--)
Michel Thierry06dc68d2015-02-24 16:22:37 +0000670 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000671
672 return -ENOMEM;
673}
674
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800675static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
676 const int max_pdp)
677{
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000678 int i;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800679
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000680 for (i = 0; i < max_pdp; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000681 ppgtt->pdp.page_directory[i] = alloc_pd_single();
682 if (IS_ERR(ppgtt->pdp.page_directory[i]))
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000683 goto unwind_out;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000684 }
685
686 ppgtt->num_pd_pages = max_pdp;
Ben Widawsky76643602015-01-22 17:01:24 +0000687 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPES);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800688
689 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000690
691unwind_out:
Ben Widawsky06fda602015-02-24 16:22:36 +0000692 while (i--)
693 unmap_and_free_pd(ppgtt->pdp.page_directory[i]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000694
695 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800696}
697
698static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
699 const int max_pdp)
700{
701 int ret;
702
703 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
704 if (ret)
705 return ret;
706
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000707 ret = gen8_ppgtt_allocate_page_tables(ppgtt);
708 if (ret)
709 goto err_out;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800710
Michel Thierry07749ef2015-03-16 16:00:54 +0000711 ppgtt->num_pd_entries = max_pdp * I915_PDES;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800712
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000713 return 0;
714
715err_out:
716 gen8_ppgtt_free(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800717 return ret;
718}
719
720static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
721 const int pd)
722{
723 dma_addr_t pd_addr;
724 int ret;
725
726 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
Ben Widawsky06fda602015-02-24 16:22:36 +0000727 ppgtt->pdp.page_directory[pd]->page, 0,
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800728 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
729
730 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
731 if (ret)
732 return ret;
733
Ben Widawsky06fda602015-02-24 16:22:36 +0000734 ppgtt->pdp.page_directory[pd]->daddr = pd_addr;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800735
736 return 0;
737}
738
739static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
740 const int pd,
741 const int pt)
742{
743 dma_addr_t pt_addr;
Ben Widawsky06fda602015-02-24 16:22:36 +0000744 struct i915_page_directory_entry *pdir = ppgtt->pdp.page_directory[pd];
745 struct i915_page_table_entry *ptab = pdir->page_table[pt];
Ben Widawsky7324cc02015-02-24 16:22:35 +0000746 struct page *p = ptab->page;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800747 int ret;
748
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800749 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
750 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
751 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
752 if (ret)
753 return ret;
754
Ben Widawsky7324cc02015-02-24 16:22:35 +0000755 ptab->daddr = pt_addr;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800756
757 return 0;
758}
759
Daniel Vettereb0b44a2015-03-18 14:47:59 +0100760/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800761 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
762 * with a net effect resembling a 2-level page table in normal x86 terms. Each
763 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
764 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800765 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800766 * FIXME: split allocation into smaller pieces. For now we only ever do this
767 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
Ben Widawsky37aca442013-11-04 20:47:32 -0800768 * TODO: Do something with the size parameter
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800769 */
Ben Widawsky37aca442013-11-04 20:47:32 -0800770static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
771{
Ben Widawsky37aca442013-11-04 20:47:32 -0800772 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
Michel Thierry07749ef2015-03-16 16:00:54 +0000773 const int min_pt_pages = I915_PDES * max_pdp;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800774 int i, j, ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800775
776 if (size % (1<<30))
777 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
778
Mika Kuoppala29343682015-03-04 14:55:17 +0200779 /* 1. Do all our allocations for page directories and page tables.
780 * We allocate more than was asked so that we can point the unused parts
781 * to valid entries that point to scratch page. Dynamic page tables
782 * will fix this eventually.
783 */
784 ret = gen8_ppgtt_alloc(ppgtt, GEN8_LEGACY_PDPES);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800785 if (ret)
786 return ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800787
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800788 /*
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800789 * 2. Create DMA mappings for the page directories and page tables.
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800790 */
Mika Kuoppala29343682015-03-04 14:55:17 +0200791 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800792 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800793 if (ret)
794 goto bail;
795
Michel Thierry07749ef2015-03-16 16:00:54 +0000796 for (j = 0; j < I915_PDES; j++) {
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800797 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800798 if (ret)
799 goto bail;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800800 }
801 }
802
803 /*
804 * 3. Map all the page directory entires to point to the page tables
805 * we've allocated.
806 *
807 * For now, the PPGTT helper functions all require that the PDEs are
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800808 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800809 * will never need to touch the PDEs again.
810 */
Mika Kuoppala29343682015-03-04 14:55:17 +0200811 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000812 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i];
Michel Thierry07749ef2015-03-16 16:00:54 +0000813 gen8_pde_t *pd_vaddr;
Ben Widawsky06fda602015-02-24 16:22:36 +0000814 pd_vaddr = kmap_atomic(ppgtt->pdp.page_directory[i]->page);
Michel Thierry07749ef2015-03-16 16:00:54 +0000815 for (j = 0; j < I915_PDES; j++) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000816 struct i915_page_table_entry *pt = pd->page_table[j];
817 dma_addr_t addr = pt->daddr;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800818 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
819 I915_CACHE_LLC);
820 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300821 if (!HAS_LLC(ppgtt->base.dev))
822 drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800823 kunmap_atomic(pd_vaddr);
824 }
825
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800826 ppgtt->switch_mm = gen8_mm_switch;
827 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
828 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
829 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
830 ppgtt->base.start = 0;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800831
Mika Kuoppala29343682015-03-04 14:55:17 +0200832 /* This is the area that we advertise as usable for the caller */
Michel Thierry07749ef2015-03-16 16:00:54 +0000833 ppgtt->base.total = max_pdp * I915_PDES * GEN8_PTES * PAGE_SIZE;
Mika Kuoppala29343682015-03-04 14:55:17 +0200834
835 /* Set all ptes to a valid scratch page. Also above requested space */
836 ppgtt->base.clear_range(&ppgtt->base, 0,
Michel Thierry07749ef2015-03-16 16:00:54 +0000837 ppgtt->num_pd_pages * GEN8_PTES * PAGE_SIZE,
Mika Kuoppala29343682015-03-04 14:55:17 +0200838 true);
Ben Widawsky459108b2013-11-02 21:07:23 -0700839
Ben Widawsky37aca442013-11-04 20:47:32 -0800840 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
841 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
842 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
Ben Widawsky5abbcca2014-02-21 13:06:34 -0800843 ppgtt->num_pd_entries,
844 (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
Ben Widawsky28cf5412013-11-02 21:07:26 -0700845 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800846
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800847bail:
848 gen8_ppgtt_unmap_pages(ppgtt);
849 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800850 return ret;
851}
852
Ben Widawsky87d60b62013-12-06 14:11:29 -0800853static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
854{
855 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
856 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry07749ef2015-03-16 16:00:54 +0000857 gen6_pte_t __iomem *pd_addr;
858 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -0800859 uint32_t pd_entry;
860 int pte, pde;
861
Akash Goel24f3a8c2014-06-17 10:59:42 +0530862 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800863
Michel Thierry07749ef2015-03-16 16:00:54 +0000864 pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
865 ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800866
867 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
Ben Widawsky7324cc02015-02-24 16:22:35 +0000868 ppgtt->pd.pd_offset,
869 ppgtt->pd.pd_offset + ppgtt->num_pd_entries);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800870 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
871 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +0000872 gen6_pte_t *pt_vaddr;
Ben Widawsky06fda602015-02-24 16:22:36 +0000873 dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
Ben Widawsky87d60b62013-12-06 14:11:29 -0800874 pd_entry = readl(pd_addr + pde);
875 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
876
877 if (pd_entry != expected)
878 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
879 pde,
880 pd_entry,
881 expected);
882 seq_printf(m, "\tPDE: %x\n", pd_entry);
883
Ben Widawsky06fda602015-02-24 16:22:36 +0000884 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
Michel Thierry07749ef2015-03-16 16:00:54 +0000885 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -0800886 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +0000887 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -0800888 (pte * PAGE_SIZE);
889 int i;
890 bool found = false;
891 for (i = 0; i < 4; i++)
892 if (pt_vaddr[pte + i] != scratch_pte)
893 found = true;
894 if (!found)
895 continue;
896
897 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
898 for (i = 0; i < 4; i++) {
899 if (pt_vaddr[pte + i] != scratch_pte)
900 seq_printf(m, " %08x", pt_vaddr[pte + i]);
901 else
902 seq_puts(m, " SCRATCH ");
903 }
904 seq_puts(m, "\n");
905 }
906 kunmap_atomic(pt_vaddr);
907 }
908}
909
Ben Widawsky678d96f2015-03-16 16:00:56 +0000910/* Write pde (index) from the page directory @pd to the page table @pt */
911static void gen6_write_pde(struct i915_page_directory_entry *pd,
912 const int pde, struct i915_page_table_entry *pt)
Ben Widawsky61973492013-04-08 18:43:54 -0700913{
Ben Widawsky678d96f2015-03-16 16:00:56 +0000914 /* Caller needs to make sure the write completes if necessary */
915 struct i915_hw_ppgtt *ppgtt =
916 container_of(pd, struct i915_hw_ppgtt, pd);
917 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -0700918
Ben Widawsky678d96f2015-03-16 16:00:56 +0000919 pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr);
920 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -0700921
Ben Widawsky678d96f2015-03-16 16:00:56 +0000922 writel(pd_entry, ppgtt->pd_addr + pde);
923}
Ben Widawsky61973492013-04-08 18:43:54 -0700924
Ben Widawsky678d96f2015-03-16 16:00:56 +0000925/* Write all the page tables found in the ppgtt structure to incrementing page
926 * directories. */
927static void gen6_write_page_range(struct drm_i915_private *dev_priv,
928 struct i915_page_directory_entry *pd,
929 uint32_t start, uint32_t length)
930{
931 struct i915_page_table_entry *pt;
932 uint32_t pde, temp;
933
934 gen6_for_each_pde(pt, pd, start, length, temp, pde)
935 gen6_write_pde(pd, pde, pt);
936
937 /* Make sure write is complete before other code can use this page
938 * table. Also require for WC mapped PTEs */
939 readl(dev_priv->gtt.gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -0700940}
941
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800942static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700943{
Ben Widawsky7324cc02015-02-24 16:22:35 +0000944 BUG_ON(ppgtt->pd.pd_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -0700945
Ben Widawsky7324cc02015-02-24 16:22:35 +0000946 return (ppgtt->pd.pd_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800947}
Ben Widawsky61973492013-04-08 18:43:54 -0700948
Ben Widawsky90252e52013-12-06 14:11:12 -0800949static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100950 struct intel_engine_cs *ring)
Ben Widawsky90252e52013-12-06 14:11:12 -0800951{
Ben Widawsky90252e52013-12-06 14:11:12 -0800952 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700953
Ben Widawsky90252e52013-12-06 14:11:12 -0800954 /* NB: TLBs must be flushed and invalidated before a switch */
955 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
956 if (ret)
957 return ret;
958
959 ret = intel_ring_begin(ring, 6);
960 if (ret)
961 return ret;
962
963 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
964 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
965 intel_ring_emit(ring, PP_DIR_DCLV_2G);
966 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
967 intel_ring_emit(ring, get_pd_offset(ppgtt));
968 intel_ring_emit(ring, MI_NOOP);
969 intel_ring_advance(ring);
970
971 return 0;
972}
973
Yu Zhang71ba2d62015-02-10 19:05:54 +0800974static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
975 struct intel_engine_cs *ring)
976{
977 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
978
979 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
980 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
981 return 0;
982}
983
Ben Widawsky48a10382013-12-06 14:11:11 -0800984static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100985 struct intel_engine_cs *ring)
Ben Widawsky48a10382013-12-06 14:11:11 -0800986{
Ben Widawsky48a10382013-12-06 14:11:11 -0800987 int ret;
988
Ben Widawsky48a10382013-12-06 14:11:11 -0800989 /* NB: TLBs must be flushed and invalidated before a switch */
990 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
991 if (ret)
992 return ret;
993
994 ret = intel_ring_begin(ring, 6);
995 if (ret)
996 return ret;
997
998 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
999 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1000 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1001 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1002 intel_ring_emit(ring, get_pd_offset(ppgtt));
1003 intel_ring_emit(ring, MI_NOOP);
1004 intel_ring_advance(ring);
1005
Ben Widawsky90252e52013-12-06 14:11:12 -08001006 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1007 if (ring->id != RCS) {
1008 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1009 if (ret)
1010 return ret;
1011 }
1012
Ben Widawsky48a10382013-12-06 14:11:11 -08001013 return 0;
1014}
1015
Ben Widawskyeeb94882013-12-06 14:11:10 -08001016static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001017 struct intel_engine_cs *ring)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001018{
1019 struct drm_device *dev = ppgtt->base.dev;
1020 struct drm_i915_private *dev_priv = dev->dev_private;
1021
Ben Widawsky48a10382013-12-06 14:11:11 -08001022
Ben Widawskyeeb94882013-12-06 14:11:10 -08001023 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1024 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1025
1026 POSTING_READ(RING_PP_DIR_DCLV(ring));
1027
1028 return 0;
1029}
1030
Daniel Vetter82460d92014-08-06 20:19:53 +02001031static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001032{
Ben Widawskyeeb94882013-12-06 14:11:10 -08001033 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001034 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +02001035 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001036
1037 for_each_ring(ring, dev_priv, j) {
1038 I915_WRITE(RING_MODE_GEN7(ring),
1039 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001040 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001041}
1042
Daniel Vetter82460d92014-08-06 20:19:53 +02001043static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001044{
Jani Nikula50227e12014-03-31 14:27:21 +03001045 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001046 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001047 uint32_t ecochk, ecobits;
1048 int i;
1049
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001050 ecobits = I915_READ(GAC_ECO_BITS);
1051 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1052
1053 ecochk = I915_READ(GAM_ECOCHK);
1054 if (IS_HASWELL(dev)) {
1055 ecochk |= ECOCHK_PPGTT_WB_HSW;
1056 } else {
1057 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1058 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1059 }
1060 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001061
Ben Widawsky61973492013-04-08 18:43:54 -07001062 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001063 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001064 I915_WRITE(RING_MODE_GEN7(ring),
1065 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001066 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001067}
1068
Daniel Vetter82460d92014-08-06 20:19:53 +02001069static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001070{
Jani Nikula50227e12014-03-31 14:27:21 +03001071 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001072 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001073
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001074 ecobits = I915_READ(GAC_ECO_BITS);
1075 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1076 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001077
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001078 gab_ctl = I915_READ(GAB_CTL);
1079 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001080
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001081 ecochk = I915_READ(GAM_ECOCHK);
1082 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001083
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001084 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001085}
1086
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001087/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001088static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001089 uint64_t start,
1090 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001091 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001092{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001093 struct i915_hw_ppgtt *ppgtt =
1094 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001095 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001096 unsigned first_entry = start >> PAGE_SHIFT;
1097 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001098 unsigned act_pt = first_entry / GEN6_PTES;
1099 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001100 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001101
Akash Goel24f3a8c2014-06-17 10:59:42 +05301102 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001103
Daniel Vetter7bddb012012-02-09 17:15:47 +01001104 while (num_entries) {
1105 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001106 if (last_pte > GEN6_PTES)
1107 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001108
Ben Widawsky06fda602015-02-24 16:22:36 +00001109 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001110
1111 for (i = first_pte; i < last_pte; i++)
1112 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001113
1114 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001115
Daniel Vetter7bddb012012-02-09 17:15:47 +01001116 num_entries -= last_pte - first_pte;
1117 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001118 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001119 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001120}
1121
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001122static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001123 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001124 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301125 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001126{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001127 struct i915_hw_ppgtt *ppgtt =
1128 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001129 gen6_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -08001130 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001131 unsigned act_pt = first_entry / GEN6_PTES;
1132 unsigned act_pte = first_entry % GEN6_PTES;
Imre Deak6e995e22013-02-18 19:28:04 +02001133 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001134
Chris Wilsoncc797142013-12-31 15:50:30 +00001135 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +02001136 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001137 if (pt_vaddr == NULL)
Ben Widawsky06fda602015-02-24 16:22:36 +00001138 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001139
Chris Wilsoncc797142013-12-31 15:50:30 +00001140 pt_vaddr[act_pte] =
1141 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +05301142 cache_level, true, flags);
1143
Michel Thierry07749ef2015-03-16 16:00:54 +00001144 if (++act_pte == GEN6_PTES) {
Imre Deak6e995e22013-02-18 19:28:04 +02001145 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001146 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001147 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001148 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001149 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001150 }
Chris Wilsoncc797142013-12-31 15:50:30 +00001151 if (pt_vaddr)
1152 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001153}
1154
Ben Widawskya00d8252014-02-19 22:05:48 -08001155static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001156{
Daniel Vetter3440d262013-01-24 13:49:56 -08001157 int i;
1158
Ben Widawsky7324cc02015-02-24 16:22:35 +00001159 for (i = 0; i < ppgtt->num_pd_entries; i++)
1160 pci_unmap_page(ppgtt->base.dev->pdev,
Ben Widawsky06fda602015-02-24 16:22:36 +00001161 ppgtt->pd.page_table[i]->daddr,
Ben Widawsky7324cc02015-02-24 16:22:35 +00001162 4096, PCI_DMA_BIDIRECTIONAL);
Ben Widawskya00d8252014-02-19 22:05:48 -08001163}
1164
Ben Widawsky563222a2015-03-19 12:53:28 +00001165/* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we
1166 * are switching between contexts with the same LRCA, we also must do a force
1167 * restore.
1168 */
1169static inline void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1170{
1171 /* If current vm != vm, */
1172 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1173}
1174
Ben Widawsky678d96f2015-03-16 16:00:56 +00001175static int gen6_alloc_va_range(struct i915_address_space *vm,
1176 uint64_t start, uint64_t length)
1177{
1178 struct i915_hw_ppgtt *ppgtt =
1179 container_of(vm, struct i915_hw_ppgtt, base);
1180 struct i915_page_table_entry *pt;
1181 uint32_t pde, temp;
1182
1183 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1184 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1185
1186 bitmap_zero(tmp_bitmap, GEN6_PTES);
1187 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1188 gen6_pte_count(start, length));
1189
1190 bitmap_or(pt->used_ptes, pt->used_ptes, tmp_bitmap,
1191 GEN6_PTES);
1192 }
1193
Ben Widawsky563222a2015-03-19 12:53:28 +00001194 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001195 return 0;
1196}
1197
Ben Widawskya00d8252014-02-19 22:05:48 -08001198static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
1199{
1200 int i;
Daniel Vetter3440d262013-01-24 13:49:56 -08001201
Daniel Vetter3440d262013-01-24 13:49:56 -08001202 for (i = 0; i < ppgtt->num_pd_entries; i++)
Michel Thierry06dc68d2015-02-24 16:22:37 +00001203 unmap_and_free_pt(ppgtt->pd.page_table[i], ppgtt->base.dev);
Ben Widawsky06fda602015-02-24 16:22:36 +00001204
1205 unmap_and_free_pd(&ppgtt->pd);
Daniel Vetter3440d262013-01-24 13:49:56 -08001206}
1207
Ben Widawskya00d8252014-02-19 22:05:48 -08001208static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1209{
1210 struct i915_hw_ppgtt *ppgtt =
1211 container_of(vm, struct i915_hw_ppgtt, base);
1212
Ben Widawskya00d8252014-02-19 22:05:48 -08001213 drm_mm_remove_node(&ppgtt->node);
1214
1215 gen6_ppgtt_unmap_pages(ppgtt);
1216 gen6_ppgtt_free(ppgtt);
1217}
1218
Ben Widawskyb1465202014-02-19 22:05:49 -08001219static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001220{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001221 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001222 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001223 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001224 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001225
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001226 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1227 * allocator works in address space sizes, so it's multiplied by page
1228 * size. We allocate at the top of the GTT to avoid fragmentation.
1229 */
1230 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Ben Widawskye3cc1992013-12-06 14:11:08 -08001231alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001232 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1233 &ppgtt->node, GEN6_PD_SIZE,
1234 GEN6_PD_ALIGN, 0,
1235 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07001236 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001237 if (ret == -ENOSPC && !retried) {
1238 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1239 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02001240 I915_CACHE_NONE,
1241 0, dev_priv->gtt.base.total,
1242 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001243 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001244 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001245
1246 retried = true;
1247 goto alloc;
1248 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001249
Ben Widawskyc8c26622015-01-22 17:01:25 +00001250 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001251 goto err_out;
1252
Ben Widawskyc8c26622015-01-22 17:01:25 +00001253
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001254 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1255 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001256
Michel Thierry07749ef2015-03-16 16:00:54 +00001257 ppgtt->num_pd_entries = I915_PDES;
Ben Widawskyc8c26622015-01-22 17:01:25 +00001258 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001259
1260err_out:
1261 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001262}
1263
Ben Widawskyb1465202014-02-19 22:05:49 -08001264static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1265{
1266 int ret;
1267
1268 ret = gen6_ppgtt_allocate_page_directories(ppgtt);
1269 if (ret)
1270 return ret;
1271
Michel Thierry06dc68d2015-02-24 16:22:37 +00001272 ret = alloc_pt_range(&ppgtt->pd, 0, ppgtt->num_pd_entries,
1273 ppgtt->base.dev);
1274
Ben Widawskyb1465202014-02-19 22:05:49 -08001275 if (ret) {
1276 drm_mm_remove_node(&ppgtt->node);
1277 return ret;
1278 }
1279
Ben Widawskyb1465202014-02-19 22:05:49 -08001280 return 0;
1281}
1282
Ben Widawskyb1465202014-02-19 22:05:49 -08001283static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1284{
1285 struct drm_device *dev = ppgtt->base.dev;
1286 struct drm_i915_private *dev_priv = dev->dev_private;
1287 int ret;
1288
1289 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001290 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001291 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001292 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001293 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001294 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001295 ppgtt->switch_mm = gen7_mm_switch;
1296 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001297 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001298
Yu Zhang71ba2d62015-02-10 19:05:54 +08001299 if (intel_vgpu_active(dev))
1300 ppgtt->switch_mm = vgpu_mm_switch;
1301
Ben Widawskyb1465202014-02-19 22:05:49 -08001302 ret = gen6_ppgtt_alloc(ppgtt);
1303 if (ret)
1304 return ret;
1305
Ben Widawsky678d96f2015-03-16 16:00:56 +00001306 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001307 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1308 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1309 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001310 ppgtt->base.start = 0;
Michel Thierry07749ef2015-03-16 16:00:54 +00001311 ppgtt->base.total = ppgtt->num_pd_entries * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001312 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001313
Ben Widawsky7324cc02015-02-24 16:22:35 +00001314 ppgtt->pd.pd_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00001315 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001316
Ben Widawsky678d96f2015-03-16 16:00:56 +00001317 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
1318 ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
1319
Ben Widawsky782f1492014-02-20 11:50:33 -08001320 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001321
Ben Widawsky678d96f2015-03-16 16:00:56 +00001322 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
1323
Thierry Reding440fd522015-01-23 09:05:06 +01001324 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001325 ppgtt->node.size >> 20,
1326 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001327
Daniel Vetterfa76da32014-08-06 20:19:54 +02001328 DRM_DEBUG("Adding PPGTT at offset %x\n",
Ben Widawsky7324cc02015-02-24 16:22:35 +00001329 ppgtt->pd.pd_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001330
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001331 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001332}
1333
Daniel Vetterfa76da32014-08-06 20:19:54 +02001334static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001335{
1336 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter3440d262013-01-24 13:49:56 -08001337
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001338 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001339 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001340
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001341 if (INTEL_INFO(dev)->gen < 8)
Daniel Vetterfa76da32014-08-06 20:19:54 +02001342 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001343 else
Rodrigo Vivi1eb0f002014-12-03 04:55:26 -08001344 return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001345}
1346int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1347{
1348 struct drm_i915_private *dev_priv = dev->dev_private;
1349 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001350
Daniel Vetterfa76da32014-08-06 20:19:54 +02001351 ret = __hw_ppgtt_init(dev, ppgtt);
1352 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001353 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001354 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1355 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001356 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001357 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001358
1359 return ret;
1360}
1361
Daniel Vetter82460d92014-08-06 20:19:53 +02001362int i915_ppgtt_init_hw(struct drm_device *dev)
1363{
1364 struct drm_i915_private *dev_priv = dev->dev_private;
1365 struct intel_engine_cs *ring;
1366 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1367 int i, ret = 0;
1368
Thomas Daniel671b50132014-08-20 16:24:50 +01001369 /* In the case of execlists, PPGTT is enabled by the context descriptor
1370 * and the PDPs are contained within the context itself. We don't
1371 * need to do anything here. */
1372 if (i915.enable_execlists)
1373 return 0;
1374
Daniel Vetter82460d92014-08-06 20:19:53 +02001375 if (!USES_PPGTT(dev))
1376 return 0;
1377
1378 if (IS_GEN6(dev))
1379 gen6_ppgtt_enable(dev);
1380 else if (IS_GEN7(dev))
1381 gen7_ppgtt_enable(dev);
1382 else if (INTEL_INFO(dev)->gen >= 8)
1383 gen8_ppgtt_enable(dev);
1384 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01001385 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02001386
1387 if (ppgtt) {
1388 for_each_ring(ring, dev_priv, i) {
McAulay, Alistair6689c162014-08-15 18:51:35 +01001389 ret = ppgtt->switch_mm(ppgtt, ring);
Daniel Vetter82460d92014-08-06 20:19:53 +02001390 if (ret != 0)
1391 return ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001392 }
1393 }
1394
1395 return ret;
1396}
Daniel Vetter4d884702014-08-06 15:04:47 +02001397struct i915_hw_ppgtt *
1398i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1399{
1400 struct i915_hw_ppgtt *ppgtt;
1401 int ret;
1402
1403 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1404 if (!ppgtt)
1405 return ERR_PTR(-ENOMEM);
1406
1407 ret = i915_ppgtt_init(dev, ppgtt);
1408 if (ret) {
1409 kfree(ppgtt);
1410 return ERR_PTR(ret);
1411 }
1412
1413 ppgtt->file_priv = fpriv;
1414
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001415 trace_i915_ppgtt_create(&ppgtt->base);
1416
Daniel Vetter4d884702014-08-06 15:04:47 +02001417 return ppgtt;
1418}
1419
Daniel Vetteree960be2014-08-06 15:04:45 +02001420void i915_ppgtt_release(struct kref *kref)
1421{
1422 struct i915_hw_ppgtt *ppgtt =
1423 container_of(kref, struct i915_hw_ppgtt, ref);
1424
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001425 trace_i915_ppgtt_release(&ppgtt->base);
1426
Daniel Vetteree960be2014-08-06 15:04:45 +02001427 /* vmas should already be unbound */
1428 WARN_ON(!list_empty(&ppgtt->base.active_list));
1429 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1430
Daniel Vetter19dd1202014-08-06 15:04:55 +02001431 list_del(&ppgtt->base.global_link);
1432 drm_mm_takedown(&ppgtt->base.mm);
1433
Daniel Vetteree960be2014-08-06 15:04:45 +02001434 ppgtt->base.cleanup(&ppgtt->base);
1435 kfree(ppgtt);
1436}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001437
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001438static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001439ppgtt_bind_vma(struct i915_vma *vma,
1440 enum i915_cache_level cache_level,
1441 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001442{
Akash Goel24f3a8c2014-06-17 10:59:42 +05301443 /* Currently applicable only to VLV */
1444 if (vma->obj->gt_ro)
1445 flags |= PTE_READ_ONLY;
1446
Ben Widawsky782f1492014-02-20 11:50:33 -08001447 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301448 cache_level, flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001449}
1450
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001451static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001452{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001453 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001454 vma->node.start,
1455 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001456 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001457}
1458
Ben Widawskya81cc002013-01-18 12:30:31 -08001459extern int intel_iommu_gfx_mapped;
1460/* Certain Gen5 chipsets require require idling the GPU before
1461 * unmapping anything from the GTT when VT-d is enabled.
1462 */
1463static inline bool needs_idle_maps(struct drm_device *dev)
1464{
1465#ifdef CONFIG_INTEL_IOMMU
1466 /* Query intel_iommu to see if we need the workaround. Presumably that
1467 * was loaded first.
1468 */
1469 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1470 return true;
1471#endif
1472 return false;
1473}
1474
Ben Widawsky5c042282011-10-17 15:51:55 -07001475static bool do_idling(struct drm_i915_private *dev_priv)
1476{
1477 bool ret = dev_priv->mm.interruptible;
1478
Ben Widawskya81cc002013-01-18 12:30:31 -08001479 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001480 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001481 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001482 DRM_ERROR("Couldn't idle GPU\n");
1483 /* Wait a bit, in hopes it avoids the hang */
1484 udelay(10);
1485 }
1486 }
1487
1488 return ret;
1489}
1490
1491static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1492{
Ben Widawskya81cc002013-01-18 12:30:31 -08001493 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001494 dev_priv->mm.interruptible = interruptible;
1495}
1496
Ben Widawsky828c7902013-10-16 09:21:30 -07001497void i915_check_and_clear_faults(struct drm_device *dev)
1498{
1499 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001500 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07001501 int i;
1502
1503 if (INTEL_INFO(dev)->gen < 6)
1504 return;
1505
1506 for_each_ring(ring, dev_priv, i) {
1507 u32 fault_reg;
1508 fault_reg = I915_READ(RING_FAULT_REG(ring));
1509 if (fault_reg & RING_FAULT_VALID) {
1510 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02001511 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07001512 "\tAddress space: %s\n"
1513 "\tSource ID: %d\n"
1514 "\tType: %d\n",
1515 fault_reg & PAGE_MASK,
1516 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1517 RING_FAULT_SRCID(fault_reg),
1518 RING_FAULT_FAULT_TYPE(fault_reg));
1519 I915_WRITE(RING_FAULT_REG(ring),
1520 fault_reg & ~RING_FAULT_VALID);
1521 }
1522 }
1523 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1524}
1525
Chris Wilson91e56492014-09-25 10:13:12 +01001526static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
1527{
1528 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
1529 intel_gtt_chipset_flush();
1530 } else {
1531 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1532 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1533 }
1534}
1535
Ben Widawsky828c7902013-10-16 09:21:30 -07001536void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1537{
1538 struct drm_i915_private *dev_priv = dev->dev_private;
1539
1540 /* Don't bother messing with faults pre GEN6 as we have little
1541 * documentation supporting that it's a good idea.
1542 */
1543 if (INTEL_INFO(dev)->gen < 6)
1544 return;
1545
1546 i915_check_and_clear_faults(dev);
1547
1548 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001549 dev_priv->gtt.base.start,
1550 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001551 true);
Chris Wilson91e56492014-09-25 10:13:12 +01001552
1553 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07001554}
1555
Daniel Vetter76aaf222010-11-05 22:23:30 +01001556void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1557{
1558 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001559 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001560 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001561
Ben Widawsky828c7902013-10-16 09:21:30 -07001562 i915_check_and_clear_faults(dev);
1563
Chris Wilsonbee4a182011-01-21 10:54:32 +00001564 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001565 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001566 dev_priv->gtt.base.start,
1567 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001568 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001569
Ben Widawsky35c20a62013-05-31 11:28:48 -07001570 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001571 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1572 &dev_priv->gtt.base);
1573 if (!vma)
1574 continue;
1575
Chris Wilson2c225692013-08-09 12:26:45 +01001576 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001577 /* The bind_vma code tries to be smart about tracking mappings.
1578 * Unfortunately above, we've just wiped out the mappings
1579 * without telling our object about it. So we need to fake it.
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001580 *
1581 * Bind is not expected to fail since this is only called on
1582 * resume and assumption is all requirements exist already.
Ben Widawsky6f65e292013-12-06 14:10:56 -08001583 */
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001584 vma->bound &= ~GLOBAL_BIND;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001585 WARN_ON(i915_vma_bind(vma, obj->cache_level, GLOBAL_BIND));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001586 }
1587
Ben Widawsky80da2162013-12-06 14:11:17 -08001588
Ben Widawskya2319c02014-03-18 16:09:37 -07001589 if (INTEL_INFO(dev)->gen >= 8) {
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001590 if (IS_CHERRYVIEW(dev))
1591 chv_setup_private_ppat(dev_priv);
1592 else
1593 bdw_setup_private_ppat(dev_priv);
1594
Ben Widawsky80da2162013-12-06 14:11:17 -08001595 return;
Ben Widawskya2319c02014-03-18 16:09:37 -07001596 }
Ben Widawsky80da2162013-12-06 14:11:17 -08001597
Ben Widawsky678d96f2015-03-16 16:00:56 +00001598 if (USES_PPGTT(dev)) {
1599 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1600 /* TODO: Perhaps it shouldn't be gen6 specific */
Ben Widawsky80da2162013-12-06 14:11:17 -08001601
Ben Widawsky678d96f2015-03-16 16:00:56 +00001602 struct i915_hw_ppgtt *ppgtt =
1603 container_of(vm, struct i915_hw_ppgtt,
1604 base);
1605
1606 if (i915_is_ggtt(vm))
1607 ppgtt = dev_priv->mm.aliasing_ppgtt;
1608
1609 gen6_write_page_range(dev_priv, &ppgtt->pd,
1610 0, ppgtt->base.total);
1611 }
Daniel Vetter76aaf222010-11-05 22:23:30 +01001612 }
1613
Chris Wilson91e56492014-09-25 10:13:12 +01001614 i915_ggtt_flush(dev_priv);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001615}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001616
Daniel Vetter74163902012-02-15 23:50:21 +01001617int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001618{
Chris Wilson9da3da62012-06-01 15:20:22 +01001619 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001620 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001621
1622 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1623 obj->pages->sgl, obj->pages->nents,
1624 PCI_DMA_BIDIRECTIONAL))
1625 return -ENOSPC;
1626
1627 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001628}
1629
Michel Thierry07749ef2015-03-16 16:00:54 +00001630static inline void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001631{
1632#ifdef writeq
1633 writeq(pte, addr);
1634#else
1635 iowrite32((u32)pte, addr);
1636 iowrite32(pte >> 32, addr + 4);
1637#endif
1638}
1639
1640static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1641 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001642 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301643 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001644{
1645 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001646 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001647 gen8_pte_t __iomem *gtt_entries =
1648 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001649 int i = 0;
1650 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001651 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001652
1653 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1654 addr = sg_dma_address(sg_iter.sg) +
1655 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1656 gen8_set_pte(&gtt_entries[i],
1657 gen8_pte_encode(addr, level, true));
1658 i++;
1659 }
1660
1661 /*
1662 * XXX: This serves as a posting read to make sure that the PTE has
1663 * actually been updated. There is some concern that even though
1664 * registers and PTEs are within the same BAR that they are potentially
1665 * of NUMA access patterns. Therefore, even with the way we assume
1666 * hardware should work, we must keep this posting read for paranoia.
1667 */
1668 if (i != 0)
1669 WARN_ON(readq(&gtt_entries[i-1])
1670 != gen8_pte_encode(addr, level, true));
1671
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001672 /* This next bit makes the above posting read even more important. We
1673 * want to flush the TLBs only after we're certain all the PTE updates
1674 * have finished.
1675 */
1676 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1677 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001678}
1679
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001680/*
1681 * Binds an object into the global gtt with the specified cache level. The object
1682 * will be accessible to the GPU via commands whose operands reference offsets
1683 * within the global GTT as well as accessible by the GPU through the GMADR
1684 * mapped BAR (dev_priv->mm.gtt->gtt).
1685 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001686static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001687 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001688 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301689 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001690{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001691 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001692 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001693 gen6_pte_t __iomem *gtt_entries =
1694 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001695 int i = 0;
1696 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001697 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001698
Imre Deak6e995e22013-02-18 19:28:04 +02001699 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001700 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301701 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001702 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001703 }
1704
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001705 /* XXX: This serves as a posting read to make sure that the PTE has
1706 * actually been updated. There is some concern that even though
1707 * registers and PTEs are within the same BAR that they are potentially
1708 * of NUMA access patterns. Therefore, even with the way we assume
1709 * hardware should work, we must keep this posting read for paranoia.
1710 */
Pavel Machek57007df2014-07-28 13:20:58 +02001711 if (i != 0) {
1712 unsigned long gtt = readl(&gtt_entries[i-1]);
1713 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1714 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001715
1716 /* This next bit makes the above posting read even more important. We
1717 * want to flush the TLBs only after we're certain all the PTE updates
1718 * have finished.
1719 */
1720 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1721 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001722}
1723
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001724static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001725 uint64_t start,
1726 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001727 bool use_scratch)
1728{
1729 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001730 unsigned first_entry = start >> PAGE_SHIFT;
1731 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001732 gen8_pte_t scratch_pte, __iomem *gtt_base =
1733 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001734 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1735 int i;
1736
1737 if (WARN(num_entries > max_entries,
1738 "First entry = %d; Num entries = %d (max=%d)\n",
1739 first_entry, num_entries, max_entries))
1740 num_entries = max_entries;
1741
1742 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1743 I915_CACHE_LLC,
1744 use_scratch);
1745 for (i = 0; i < num_entries; i++)
1746 gen8_set_pte(&gtt_base[i], scratch_pte);
1747 readl(gtt_base);
1748}
1749
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001750static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001751 uint64_t start,
1752 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001753 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001754{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001755 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001756 unsigned first_entry = start >> PAGE_SHIFT;
1757 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001758 gen6_pte_t scratch_pte, __iomem *gtt_base =
1759 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001760 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001761 int i;
1762
1763 if (WARN(num_entries > max_entries,
1764 "First entry = %d; Num entries = %d (max=%d)\n",
1765 first_entry, num_entries, max_entries))
1766 num_entries = max_entries;
1767
Akash Goel24f3a8c2014-06-17 10:59:42 +05301768 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07001769
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001770 for (i = 0; i < num_entries; i++)
1771 iowrite32(scratch_pte, &gtt_base[i]);
1772 readl(gtt_base);
1773}
1774
Ben Widawsky6f65e292013-12-06 14:10:56 -08001775
1776static void i915_ggtt_bind_vma(struct i915_vma *vma,
1777 enum i915_cache_level cache_level,
1778 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001779{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001780 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001781 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1782 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1783
Ben Widawsky6f65e292013-12-06 14:10:56 -08001784 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001785 intel_gtt_insert_sg_entries(vma->ggtt_view.pages, entry, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001786 vma->bound = GLOBAL_BIND;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001787}
1788
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001789static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001790 uint64_t start,
1791 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001792 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001793{
Ben Widawsky782f1492014-02-20 11:50:33 -08001794 unsigned first_entry = start >> PAGE_SHIFT;
1795 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001796 intel_gtt_clear_range(first_entry, num_entries);
1797}
1798
Ben Widawsky6f65e292013-12-06 14:10:56 -08001799static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001800{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001801 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1802 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001803
Ben Widawsky6f65e292013-12-06 14:10:56 -08001804 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001805 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001806 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001807}
1808
Ben Widawsky6f65e292013-12-06 14:10:56 -08001809static void ggtt_bind_vma(struct i915_vma *vma,
1810 enum i915_cache_level cache_level,
1811 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001812{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001813 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001814 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001815 struct drm_i915_gem_object *obj = vma->obj;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001816 struct sg_table *pages = obj->pages;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001817
Akash Goel24f3a8c2014-06-17 10:59:42 +05301818 /* Currently applicable only to VLV */
1819 if (obj->gt_ro)
1820 flags |= PTE_READ_ONLY;
1821
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001822 if (i915_is_ggtt(vma->vm))
1823 pages = vma->ggtt_view.pages;
1824
Ben Widawsky6f65e292013-12-06 14:10:56 -08001825 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1826 * or we have a global mapping already but the cacheability flags have
1827 * changed, set the global PTEs.
1828 *
1829 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1830 * instead if none of the above hold true.
1831 *
1832 * NB: A global mapping should only be needed for special regions like
1833 * "gtt mappable", SNB errata, or if specified via special execbuf
1834 * flags. At all other times, the GPU will use the aliasing PPGTT.
1835 */
1836 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001837 if (!(vma->bound & GLOBAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08001838 (cache_level != obj->cache_level)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001839 vma->vm->insert_entries(vma->vm, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001840 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301841 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001842 vma->bound |= GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001843 }
1844 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001845
Ben Widawsky6f65e292013-12-06 14:10:56 -08001846 if (dev_priv->mm.aliasing_ppgtt &&
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001847 (!(vma->bound & LOCAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08001848 (cache_level != obj->cache_level))) {
1849 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001850 appgtt->base.insert_entries(&appgtt->base, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001851 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301852 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001853 vma->bound |= LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001854 }
1855}
1856
1857static void ggtt_unbind_vma(struct i915_vma *vma)
1858{
1859 struct drm_device *dev = vma->vm->dev;
1860 struct drm_i915_private *dev_priv = dev->dev_private;
1861 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001862
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001863 if (vma->bound & GLOBAL_BIND) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001864 vma->vm->clear_range(vma->vm,
1865 vma->node.start,
1866 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001867 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001868 vma->bound &= ~GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001869 }
1870
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001871 if (vma->bound & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001872 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1873 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001874 vma->node.start,
1875 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001876 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001877 vma->bound &= ~LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001878 }
Daniel Vetter74163902012-02-15 23:50:21 +01001879}
1880
1881void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1882{
Ben Widawsky5c042282011-10-17 15:51:55 -07001883 struct drm_device *dev = obj->base.dev;
1884 struct drm_i915_private *dev_priv = dev->dev_private;
1885 bool interruptible;
1886
1887 interruptible = do_idling(dev_priv);
1888
Chris Wilson9da3da62012-06-01 15:20:22 +01001889 if (!obj->has_dma_mapping)
1890 dma_unmap_sg(&dev->pdev->dev,
1891 obj->pages->sgl, obj->pages->nents,
1892 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001893
1894 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001895}
Daniel Vetter644ec022012-03-26 09:45:40 +02001896
Chris Wilson42d6ab42012-07-26 11:49:32 +01001897static void i915_gtt_color_adjust(struct drm_mm_node *node,
1898 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01001899 u64 *start,
1900 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01001901{
1902 if (node->color != color)
1903 *start += 4096;
1904
1905 if (!list_empty(&node->node_list)) {
1906 node = list_entry(node->node_list.next,
1907 struct drm_mm_node,
1908 node_list);
1909 if (node->allocated && node->color != color)
1910 *end -= 4096;
1911 }
1912}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001913
Daniel Vetterf548c0e2014-11-19 21:40:13 +01001914static int i915_gem_setup_global_gtt(struct drm_device *dev,
1915 unsigned long start,
1916 unsigned long mappable_end,
1917 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001918{
Ben Widawskye78891c2013-01-25 16:41:04 -08001919 /* Let GEM Manage all of the aperture.
1920 *
1921 * However, leave one page at the end still bound to the scratch page.
1922 * There are a number of places where the hardware apparently prefetches
1923 * past the end of the object, and we've seen multiple hangs with the
1924 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1925 * aperture. One page should be enough to keep any prefetching inside
1926 * of the aperture.
1927 */
Ben Widawsky40d749802013-07-31 16:59:59 -07001928 struct drm_i915_private *dev_priv = dev->dev_private;
1929 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001930 struct drm_mm_node *entry;
1931 struct drm_i915_gem_object *obj;
1932 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02001933 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02001934
Ben Widawsky35451cb2013-01-17 12:45:13 -08001935 BUG_ON(mappable_end > end);
1936
Chris Wilsoned2f3452012-11-15 11:32:19 +00001937 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07001938 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08001939
1940 dev_priv->gtt.base.start = start;
1941 dev_priv->gtt.base.total = end - start;
1942
1943 if (intel_vgpu_active(dev)) {
1944 ret = intel_vgt_balloon(dev);
1945 if (ret)
1946 return ret;
1947 }
1948
Chris Wilson42d6ab42012-07-26 11:49:32 +01001949 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07001950 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02001951
Chris Wilsoned2f3452012-11-15 11:32:19 +00001952 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001953 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07001954 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001955
Ben Widawskyedd41a82013-07-05 14:41:05 -07001956 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001957 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001958
Ben Widawskyc6cfb322013-07-05 14:41:06 -07001959 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07001960 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001961 if (ret) {
1962 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
1963 return ret;
1964 }
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001965 vma->bound |= GLOBAL_BIND;
Chris Wilsoned2f3452012-11-15 11:32:19 +00001966 }
1967
Chris Wilsoned2f3452012-11-15 11:32:19 +00001968 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07001969 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00001970 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1971 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08001972 ggtt_vm->clear_range(ggtt_vm, hole_start,
1973 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00001974 }
1975
1976 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08001977 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001978
Daniel Vetterfa76da32014-08-06 20:19:54 +02001979 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
1980 struct i915_hw_ppgtt *ppgtt;
1981
1982 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1983 if (!ppgtt)
1984 return -ENOMEM;
1985
1986 ret = __hw_ppgtt_init(dev, ppgtt);
1987 if (ret != 0)
1988 return ret;
1989
1990 dev_priv->mm.aliasing_ppgtt = ppgtt;
1991 }
1992
Daniel Vetter6c5566a2014-08-06 15:04:50 +02001993 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001994}
1995
Ben Widawskyd7e50082012-12-18 10:31:25 -08001996void i915_gem_init_global_gtt(struct drm_device *dev)
1997{
1998 struct drm_i915_private *dev_priv = dev->dev_private;
1999 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002000
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002001 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08002002 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002003
Ben Widawskye78891c2013-01-25 16:41:04 -08002004 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002005}
2006
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002007void i915_global_gtt_cleanup(struct drm_device *dev)
2008{
2009 struct drm_i915_private *dev_priv = dev->dev_private;
2010 struct i915_address_space *vm = &dev_priv->gtt.base;
2011
Daniel Vetter70e32542014-08-06 15:04:57 +02002012 if (dev_priv->mm.aliasing_ppgtt) {
2013 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2014
2015 ppgtt->base.cleanup(&ppgtt->base);
2016 }
2017
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002018 if (drm_mm_initialized(&vm->mm)) {
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002019 if (intel_vgpu_active(dev))
2020 intel_vgt_deballoon();
2021
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002022 drm_mm_takedown(&vm->mm);
2023 list_del(&vm->global_link);
2024 }
2025
2026 vm->cleanup(vm);
2027}
Daniel Vetter70e32542014-08-06 15:04:57 +02002028
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002029static int setup_scratch_page(struct drm_device *dev)
2030{
2031 struct drm_i915_private *dev_priv = dev->dev_private;
2032 struct page *page;
2033 dma_addr_t dma_addr;
2034
2035 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
2036 if (page == NULL)
2037 return -ENOMEM;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002038 set_pages_uc(page, 1);
2039
2040#ifdef CONFIG_INTEL_IOMMU
2041 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
2042 PCI_DMA_BIDIRECTIONAL);
2043 if (pci_dma_mapping_error(dev->pdev, dma_addr))
2044 return -EINVAL;
2045#else
2046 dma_addr = page_to_phys(page);
2047#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002048 dev_priv->gtt.base.scratch.page = page;
2049 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002050
2051 return 0;
2052}
2053
2054static void teardown_scratch_page(struct drm_device *dev)
2055{
2056 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002057 struct page *page = dev_priv->gtt.base.scratch.page;
2058
2059 set_pages_wb(page, 1);
2060 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002061 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002062 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002063}
2064
2065static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2066{
2067 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2068 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2069 return snb_gmch_ctl << 20;
2070}
2071
Ben Widawsky9459d252013-11-03 16:53:55 -08002072static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2073{
2074 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2075 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2076 if (bdw_gmch_ctl)
2077 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002078
2079#ifdef CONFIG_X86_32
2080 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2081 if (bdw_gmch_ctl > 4)
2082 bdw_gmch_ctl = 4;
2083#endif
2084
Ben Widawsky9459d252013-11-03 16:53:55 -08002085 return bdw_gmch_ctl << 20;
2086}
2087
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002088static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2089{
2090 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2091 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2092
2093 if (gmch_ctrl)
2094 return 1 << (20 + gmch_ctrl);
2095
2096 return 0;
2097}
2098
Ben Widawskybaa09f52013-01-24 13:49:57 -08002099static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002100{
2101 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2102 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2103 return snb_gmch_ctl << 25; /* 32 MB units */
2104}
2105
Ben Widawsky9459d252013-11-03 16:53:55 -08002106static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2107{
2108 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2109 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2110 return bdw_gmch_ctl << 25; /* 32 MB units */
2111}
2112
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002113static size_t chv_get_stolen_size(u16 gmch_ctrl)
2114{
2115 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2116 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2117
2118 /*
2119 * 0x0 to 0x10: 32MB increments starting at 0MB
2120 * 0x11 to 0x16: 4MB increments starting at 8MB
2121 * 0x17 to 0x1d: 4MB increments start at 36MB
2122 */
2123 if (gmch_ctrl < 0x11)
2124 return gmch_ctrl << 25;
2125 else if (gmch_ctrl < 0x17)
2126 return (gmch_ctrl - 0x11 + 2) << 22;
2127 else
2128 return (gmch_ctrl - 0x17 + 9) << 22;
2129}
2130
Damien Lespiau66375012014-01-09 18:02:46 +00002131static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2132{
2133 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2134 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2135
2136 if (gen9_gmch_ctl < 0xf0)
2137 return gen9_gmch_ctl << 25; /* 32 MB units */
2138 else
2139 /* 4MB increments starting at 0xf0 for 4MB */
2140 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2141}
2142
Ben Widawsky63340132013-11-04 19:32:22 -08002143static int ggtt_probe_common(struct drm_device *dev,
2144 size_t gtt_size)
2145{
2146 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002147 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08002148 int ret;
2149
2150 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002151 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08002152 (pci_resource_len(dev->pdev, 0) / 2);
2153
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002154 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08002155 if (!dev_priv->gtt.gsm) {
2156 DRM_ERROR("Failed to map the gtt page table\n");
2157 return -ENOMEM;
2158 }
2159
2160 ret = setup_scratch_page(dev);
2161 if (ret) {
2162 DRM_ERROR("Scratch setup failed\n");
2163 /* iounmap will also get called at remove, but meh */
2164 iounmap(dev_priv->gtt.gsm);
2165 }
2166
2167 return ret;
2168}
2169
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002170/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2171 * bits. When using advanced contexts each context stores its own PAT, but
2172 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002173static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002174{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002175 uint64_t pat;
2176
2177 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2178 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2179 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2180 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2181 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2182 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2183 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2184 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2185
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002186 if (!USES_PPGTT(dev_priv->dev))
2187 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2188 * so RTL will always use the value corresponding to
2189 * pat_sel = 000".
2190 * So let's disable cache for GGTT to avoid screen corruptions.
2191 * MOCS still can be used though.
2192 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2193 * before this patch, i.e. the same uncached + snooping access
2194 * like on gen6/7 seems to be in effect.
2195 * - So this just fixes blitter/render access. Again it looks
2196 * like it's not just uncached access, but uncached + snooping.
2197 * So we can still hold onto all our assumptions wrt cpu
2198 * clflushing on LLC machines.
2199 */
2200 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2201
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002202 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2203 * write would work. */
2204 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2205 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2206}
2207
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002208static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2209{
2210 uint64_t pat;
2211
2212 /*
2213 * Map WB on BDW to snooped on CHV.
2214 *
2215 * Only the snoop bit has meaning for CHV, the rest is
2216 * ignored.
2217 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002218 * The hardware will never snoop for certain types of accesses:
2219 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2220 * - PPGTT page tables
2221 * - some other special cycles
2222 *
2223 * As with BDW, we also need to consider the following for GT accesses:
2224 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2225 * so RTL will always use the value corresponding to
2226 * pat_sel = 000".
2227 * Which means we must set the snoop bit in PAT entry 0
2228 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002229 */
2230 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2231 GEN8_PPAT(1, 0) |
2232 GEN8_PPAT(2, 0) |
2233 GEN8_PPAT(3, 0) |
2234 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2235 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2236 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2237 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2238
2239 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2240 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2241}
2242
Ben Widawsky63340132013-11-04 19:32:22 -08002243static int gen8_gmch_probe(struct drm_device *dev,
2244 size_t *gtt_total,
2245 size_t *stolen,
2246 phys_addr_t *mappable_base,
2247 unsigned long *mappable_end)
2248{
2249 struct drm_i915_private *dev_priv = dev->dev_private;
2250 unsigned int gtt_size;
2251 u16 snb_gmch_ctl;
2252 int ret;
2253
2254 /* TODO: We're not aware of mappable constraints on gen8 yet */
2255 *mappable_base = pci_resource_start(dev->pdev, 2);
2256 *mappable_end = pci_resource_len(dev->pdev, 2);
2257
2258 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
2259 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
2260
2261 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2262
Damien Lespiau66375012014-01-09 18:02:46 +00002263 if (INTEL_INFO(dev)->gen >= 9) {
2264 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
2265 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2266 } else if (IS_CHERRYVIEW(dev)) {
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002267 *stolen = chv_get_stolen_size(snb_gmch_ctl);
2268 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2269 } else {
2270 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
2271 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2272 }
Ben Widawsky63340132013-11-04 19:32:22 -08002273
Michel Thierry07749ef2015-03-16 16:00:54 +00002274 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002275
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002276 if (IS_CHERRYVIEW(dev))
2277 chv_setup_private_ppat(dev_priv);
2278 else
2279 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002280
Ben Widawsky63340132013-11-04 19:32:22 -08002281 ret = ggtt_probe_common(dev, gtt_size);
2282
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002283 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2284 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08002285
2286 return ret;
2287}
2288
Ben Widawskybaa09f52013-01-24 13:49:57 -08002289static int gen6_gmch_probe(struct drm_device *dev,
2290 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002291 size_t *stolen,
2292 phys_addr_t *mappable_base,
2293 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002294{
2295 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002296 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002297 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002298 int ret;
2299
Ben Widawsky41907dd2013-02-08 11:32:47 -08002300 *mappable_base = pci_resource_start(dev->pdev, 2);
2301 *mappable_end = pci_resource_len(dev->pdev, 2);
2302
Ben Widawskybaa09f52013-01-24 13:49:57 -08002303 /* 64/512MB is the current min/max we actually know of, but this is just
2304 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002305 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08002306 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08002307 DRM_ERROR("Unknown GMADR size (%lx)\n",
2308 dev_priv->gtt.mappable_end);
2309 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002310 }
2311
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002312 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2313 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08002314 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002315
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07002316 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002317
Ben Widawsky63340132013-11-04 19:32:22 -08002318 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Michel Thierry07749ef2015-03-16 16:00:54 +00002319 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002320
Ben Widawsky63340132013-11-04 19:32:22 -08002321 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002322
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002323 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2324 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002325
2326 return ret;
2327}
2328
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002329static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002330{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002331
2332 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002333
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002334 iounmap(gtt->gsm);
2335 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002336}
2337
2338static int i915_gmch_probe(struct drm_device *dev,
2339 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002340 size_t *stolen,
2341 phys_addr_t *mappable_base,
2342 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002343{
2344 struct drm_i915_private *dev_priv = dev->dev_private;
2345 int ret;
2346
Ben Widawskybaa09f52013-01-24 13:49:57 -08002347 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2348 if (!ret) {
2349 DRM_ERROR("failed to set up gmch\n");
2350 return -EIO;
2351 }
2352
Ben Widawsky41907dd2013-02-08 11:32:47 -08002353 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002354
2355 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002356 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002357
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002358 if (unlikely(dev_priv->gtt.do_idle_maps))
2359 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2360
Ben Widawskybaa09f52013-01-24 13:49:57 -08002361 return 0;
2362}
2363
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002364static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002365{
2366 intel_gmch_remove();
2367}
2368
2369int i915_gem_gtt_init(struct drm_device *dev)
2370{
2371 struct drm_i915_private *dev_priv = dev->dev_private;
2372 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002373 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002374
Ben Widawskybaa09f52013-01-24 13:49:57 -08002375 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002376 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002377 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002378 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002379 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002380 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002381 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002382 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002383 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002384 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002385 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002386 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002387 else if (INTEL_INFO(dev)->gen >= 7)
2388 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002389 else
Chris Wilson350ec882013-08-06 13:17:02 +01002390 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002391 } else {
2392 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2393 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002394 }
2395
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002396 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002397 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002398 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002399 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002400
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002401 gtt->base.dev = dev;
2402
Ben Widawskybaa09f52013-01-24 13:49:57 -08002403 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002404 DRM_INFO("Memory usable by graphics device = %zdM\n",
2405 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002406 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2407 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002408#ifdef CONFIG_INTEL_IOMMU
2409 if (intel_iommu_gfx_mapped)
2410 DRM_INFO("VT-d active for gfx access\n");
2411#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002412 /*
2413 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2414 * user's requested state against the hardware/driver capabilities. We
2415 * do this now so that we can print out any log messages once rather
2416 * than every time we check intel_enable_ppgtt().
2417 */
2418 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2419 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002420
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002421 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002422}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002423
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002424static struct i915_vma *
2425__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2426 struct i915_address_space *vm,
2427 const struct i915_ggtt_view *ggtt_view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002428{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002429 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002430
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002431 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2432 return ERR_PTR(-EINVAL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002433 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2434 if (vma == NULL)
2435 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002436
Ben Widawsky6f65e292013-12-06 14:10:56 -08002437 INIT_LIST_HEAD(&vma->vma_link);
2438 INIT_LIST_HEAD(&vma->mm_list);
2439 INIT_LIST_HEAD(&vma->exec_list);
2440 vma->vm = vm;
2441 vma->obj = obj;
2442
Rodrigo Vivib1252bc2014-12-03 04:55:29 -08002443 if (INTEL_INFO(vm->dev)->gen >= 6) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002444 if (i915_is_ggtt(vm)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002445 vma->ggtt_view = *ggtt_view;
2446
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002447 vma->unbind_vma = ggtt_unbind_vma;
2448 vma->bind_vma = ggtt_bind_vma;
2449 } else {
2450 vma->unbind_vma = ppgtt_unbind_vma;
2451 vma->bind_vma = ppgtt_bind_vma;
2452 }
Rodrigo Vivib1252bc2014-12-03 04:55:29 -08002453 } else {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002454 BUG_ON(!i915_is_ggtt(vm));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002455 vma->ggtt_view = *ggtt_view;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002456 vma->unbind_vma = i915_ggtt_unbind_vma;
2457 vma->bind_vma = i915_ggtt_bind_vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002458 }
2459
Tvrtko Ursulinf7635662014-12-03 14:59:24 +00002460 list_add_tail(&vma->vma_link, &obj->vma_list);
2461 if (!i915_is_ggtt(vm))
Michel Thierrye07f0552014-08-19 15:49:41 +01002462 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Ben Widawsky6f65e292013-12-06 14:10:56 -08002463
2464 return vma;
2465}
2466
2467struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002468i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2469 struct i915_address_space *vm)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002470{
2471 struct i915_vma *vma;
2472
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002473 vma = i915_gem_obj_to_vma(obj, vm);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002474 if (!vma)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002475 vma = __i915_gem_vma_create(obj, vm,
2476 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002477
2478 return vma;
2479}
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002480
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002481struct i915_vma *
2482i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
2483 const struct i915_ggtt_view *view)
2484{
2485 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
2486 struct i915_vma *vma;
2487
2488 if (WARN_ON(!view))
2489 return ERR_PTR(-EINVAL);
2490
2491 vma = i915_gem_obj_to_ggtt_view(obj, view);
2492
2493 if (IS_ERR(vma))
2494 return vma;
2495
2496 if (!vma)
2497 vma = __i915_gem_vma_create(obj, ggtt, view);
2498
2499 return vma;
2500
2501}
2502
2503
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002504static inline
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002505int i915_get_ggtt_vma_pages(struct i915_vma *vma)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002506{
2507 if (vma->ggtt_view.pages)
2508 return 0;
2509
2510 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
2511 vma->ggtt_view.pages = vma->obj->pages;
2512 else
2513 WARN_ONCE(1, "GGTT view %u not implemented!\n",
2514 vma->ggtt_view.type);
2515
2516 if (!vma->ggtt_view.pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002517 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002518 vma->ggtt_view.type);
2519 return -EINVAL;
2520 }
2521
2522 return 0;
2523}
2524
2525/**
2526 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
2527 * @vma: VMA to map
2528 * @cache_level: mapping cache level
2529 * @flags: flags like global or local mapping
2530 *
2531 * DMA addresses are taken from the scatter-gather table of this object (or of
2532 * this VMA in case of non-default GGTT views) and PTE entries set up.
2533 * Note that DMA addresses are also the only part of the SG table we care about.
2534 */
2535int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2536 u32 flags)
2537{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002538 if (i915_is_ggtt(vma->vm)) {
2539 int ret = i915_get_ggtt_vma_pages(vma);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002540
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002541 if (ret)
2542 return ret;
2543 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002544
2545 vma->bind_vma(vma, cache_level, flags);
2546
2547 return 0;
2548}