blob: 1be0a8ee8dd7fa1914504bab9b0bbff1a15e2cae [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;
Joonas Lahtinen9abc4642015-03-27 13:09:22 +020096const struct i915_ggtt_view i915_ggtt_view_rotated = {
97 .type = I915_GGTT_VIEW_ROTATED
98};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +000099
Ville Syrjäläee0ce472014-04-09 13:28:01 +0300100static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
101static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
Ben Widawskya2319c02014-03-18 16:09:37 -0700102
Daniel Vettercfa7c862014-04-29 11:53:58 +0200103static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
104{
Chris Wilson1893a712014-09-19 11:56:27 +0100105 bool has_aliasing_ppgtt;
106 bool has_full_ppgtt;
107
108 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
109 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
Chris Wilson1893a712014-09-19 11:56:27 +0100110
Yu Zhang71ba2d62015-02-10 19:05:54 +0800111 if (intel_vgpu_active(dev))
112 has_full_ppgtt = false; /* emulation is too hard */
113
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000114 /*
115 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
116 * execlists, the sole mechanism available to submit work.
117 */
118 if (INTEL_INFO(dev)->gen < 9 &&
119 (enable_ppgtt == 0 || !has_aliasing_ppgtt))
Daniel Vettercfa7c862014-04-29 11:53:58 +0200120 return 0;
121
122 if (enable_ppgtt == 1)
123 return 1;
124
Chris Wilson1893a712014-09-19 11:56:27 +0100125 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200126 return 2;
127
Daniel Vetter93a25a92014-03-06 09:40:43 +0100128#ifdef CONFIG_INTEL_IOMMU
129 /* Disable ppgtt on SNB if VT-d is on. */
130 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
131 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200132 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100133 }
134#endif
135
Jesse Barnes62942ed2014-06-13 09:28:33 -0700136 /* Early VLV doesn't have this */
Ville Syrjäläca2aed6c2014-06-28 02:03:56 +0300137 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
138 dev->pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700139 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
140 return 0;
141 }
142
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000143 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
144 return 2;
145 else
146 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100147}
148
Ben Widawsky6f65e292013-12-06 14:10:56 -0800149static void ppgtt_bind_vma(struct i915_vma *vma,
150 enum i915_cache_level cache_level,
151 u32 flags);
152static void ppgtt_unbind_vma(struct i915_vma *vma);
153
Michel Thierry07749ef2015-03-16 16:00:54 +0000154static inline gen8_pte_t gen8_pte_encode(dma_addr_t addr,
155 enum i915_cache_level level,
156 bool valid)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700157{
Michel Thierry07749ef2015-03-16 16:00:54 +0000158 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700159 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300160
161 switch (level) {
162 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800163 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300164 break;
165 case I915_CACHE_WT:
166 pte |= PPAT_DISPLAY_ELLC_INDEX;
167 break;
168 default:
169 pte |= PPAT_CACHED_INDEX;
170 break;
171 }
172
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700173 return pte;
174}
175
Michel Thierry07749ef2015-03-16 16:00:54 +0000176static inline gen8_pde_t gen8_pde_encode(struct drm_device *dev,
177 dma_addr_t addr,
178 enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800179{
Michel Thierry07749ef2015-03-16 16:00:54 +0000180 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800181 pde |= addr;
182 if (level != I915_CACHE_NONE)
183 pde |= PPAT_CACHED_PDE_INDEX;
184 else
185 pde |= PPAT_UNCACHED_INDEX;
186 return pde;
187}
188
Michel Thierry07749ef2015-03-16 16:00:54 +0000189static gen6_pte_t snb_pte_encode(dma_addr_t addr,
190 enum i915_cache_level level,
191 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700192{
Michel Thierry07749ef2015-03-16 16:00:54 +0000193 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700194 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700195
196 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100197 case I915_CACHE_L3_LLC:
198 case I915_CACHE_LLC:
199 pte |= GEN6_PTE_CACHE_LLC;
200 break;
201 case I915_CACHE_NONE:
202 pte |= GEN6_PTE_UNCACHED;
203 break;
204 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100205 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100206 }
207
208 return pte;
209}
210
Michel Thierry07749ef2015-03-16 16:00:54 +0000211static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
212 enum i915_cache_level level,
213 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100214{
Michel Thierry07749ef2015-03-16 16:00:54 +0000215 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100216 pte |= GEN6_PTE_ADDR_ENCODE(addr);
217
218 switch (level) {
219 case I915_CACHE_L3_LLC:
220 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700221 break;
222 case I915_CACHE_LLC:
223 pte |= GEN6_PTE_CACHE_LLC;
224 break;
225 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700226 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700227 break;
228 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100229 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700230 }
231
Ben Widawsky54d12522012-09-24 16:44:32 -0700232 return pte;
233}
234
Michel Thierry07749ef2015-03-16 16:00:54 +0000235static gen6_pte_t byt_pte_encode(dma_addr_t addr,
236 enum i915_cache_level level,
237 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700238{
Michel Thierry07749ef2015-03-16 16:00:54 +0000239 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700240 pte |= GEN6_PTE_ADDR_ENCODE(addr);
241
Akash Goel24f3a8c2014-06-17 10:59:42 +0530242 if (!(flags & PTE_READ_ONLY))
243 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700244
245 if (level != I915_CACHE_NONE)
246 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
247
248 return pte;
249}
250
Michel Thierry07749ef2015-03-16 16:00:54 +0000251static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
252 enum i915_cache_level level,
253 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700254{
Michel Thierry07749ef2015-03-16 16:00:54 +0000255 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700256 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700257
258 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700259 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700260
261 return pte;
262}
263
Michel Thierry07749ef2015-03-16 16:00:54 +0000264static gen6_pte_t iris_pte_encode(dma_addr_t addr,
265 enum i915_cache_level level,
266 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700267{
Michel Thierry07749ef2015-03-16 16:00:54 +0000268 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700269 pte |= HSW_PTE_ADDR_ENCODE(addr);
270
Chris Wilson651d7942013-08-08 14:41:10 +0100271 switch (level) {
272 case I915_CACHE_NONE:
273 break;
274 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000275 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100276 break;
277 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000278 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100279 break;
280 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700281
282 return pte;
283}
284
Ben Widawsky678d96f2015-03-16 16:00:56 +0000285#define i915_dma_unmap_single(px, dev) \
286 __i915_dma_unmap_single((px)->daddr, dev)
287
288static inline void __i915_dma_unmap_single(dma_addr_t daddr,
289 struct drm_device *dev)
290{
291 struct device *device = &dev->pdev->dev;
292
293 dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
294}
295
296/**
297 * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc.
298 * @px: Page table/dir/etc to get a DMA map for
299 * @dev: drm device
300 *
301 * Page table allocations are unified across all gens. They always require a
302 * single 4k allocation, as well as a DMA mapping. If we keep the structs
303 * symmetric here, the simple macro covers us for every page table type.
304 *
305 * Return: 0 if success.
306 */
307#define i915_dma_map_single(px, dev) \
308 i915_dma_map_page_single((px)->page, (dev), &(px)->daddr)
309
310static inline int i915_dma_map_page_single(struct page *page,
311 struct drm_device *dev,
312 dma_addr_t *daddr)
313{
314 struct device *device = &dev->pdev->dev;
315
316 *daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
Michel Thierry1266cdb2015-03-24 17:06:33 +0000317 if (dma_mapping_error(device, *daddr))
318 return -ENOMEM;
319
320 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000321}
322
Michel Thierryec565b32015-04-08 12:13:23 +0100323static void unmap_and_free_pt(struct i915_page_table *pt,
Ben Widawsky678d96f2015-03-16 16:00:56 +0000324 struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000325{
326 if (WARN_ON(!pt->page))
327 return;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000328
329 i915_dma_unmap_single(pt, dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000330 __free_page(pt->page);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000331 kfree(pt->used_ptes);
Ben Widawsky06fda602015-02-24 16:22:36 +0000332 kfree(pt);
333}
334
Michel Thierry5a8e9942015-04-08 12:13:25 +0100335static void gen8_initialize_pt(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +0100336 struct i915_page_table *pt)
Michel Thierry5a8e9942015-04-08 12:13:25 +0100337{
338 gen8_pte_t *pt_vaddr, scratch_pte;
339 int i;
340
341 pt_vaddr = kmap_atomic(pt->page);
342 scratch_pte = gen8_pte_encode(vm->scratch.addr,
343 I915_CACHE_LLC, true);
344
345 for (i = 0; i < GEN8_PTES; i++)
346 pt_vaddr[i] = scratch_pte;
347
348 if (!HAS_LLC(vm->dev))
349 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
350 kunmap_atomic(pt_vaddr);
351}
352
Michel Thierryec565b32015-04-08 12:13:23 +0100353static struct i915_page_table *alloc_pt_single(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000354{
Michel Thierryec565b32015-04-08 12:13:23 +0100355 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000356 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
357 GEN8_PTES : GEN6_PTES;
358 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000359
360 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
361 if (!pt)
362 return ERR_PTR(-ENOMEM);
363
Ben Widawsky678d96f2015-03-16 16:00:56 +0000364 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
365 GFP_KERNEL);
366
367 if (!pt->used_ptes)
368 goto fail_bitmap;
369
Michel Thierry4933d512015-03-24 15:46:22 +0000370 pt->page = alloc_page(GFP_KERNEL);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000371 if (!pt->page)
372 goto fail_page;
373
374 ret = i915_dma_map_single(pt, dev);
375 if (ret)
376 goto fail_dma;
Ben Widawsky06fda602015-02-24 16:22:36 +0000377
378 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000379
380fail_dma:
381 __free_page(pt->page);
382fail_page:
383 kfree(pt->used_ptes);
384fail_bitmap:
385 kfree(pt);
386
387 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000388}
389
390/**
391 * alloc_pt_range() - Allocate a multiple page tables
392 * @pd: The page directory which will have at least @count entries
393 * available to point to the allocated page tables.
394 * @pde: First page directory entry for which we are allocating.
395 * @count: Number of pages to allocate.
Michel Thierry719cd212015-02-26 11:28:13 +0000396 * @dev: DRM device.
Ben Widawsky06fda602015-02-24 16:22:36 +0000397 *
398 * Allocates multiple page table pages and sets the appropriate entries in the
399 * page table structure within the page directory. Function cleans up after
400 * itself on any failures.
401 *
402 * Return: 0 if allocation succeeded.
403 */
Michel Thierryec565b32015-04-08 12:13:23 +0100404static int alloc_pt_range(struct i915_page_directory *pd, uint16_t pde, size_t count,
Michel Thierry4933d512015-03-24 15:46:22 +0000405 struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000406{
407 int i, ret;
408
409 /* 512 is the max page tables per page_directory on any platform. */
Michel Thierry07749ef2015-03-16 16:00:54 +0000410 if (WARN_ON(pde + count > I915_PDES))
Ben Widawsky06fda602015-02-24 16:22:36 +0000411 return -EINVAL;
412
413 for (i = pde; i < pde + count; i++) {
Michel Thierryec565b32015-04-08 12:13:23 +0100414 struct i915_page_table *pt = alloc_pt_single(dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000415
416 if (IS_ERR(pt)) {
417 ret = PTR_ERR(pt);
418 goto err_out;
419 }
420 WARN(pd->page_table[i],
Dan Carpenter686135d2015-02-26 19:53:54 +0300421 "Leaking page directory entry %d (%p)\n",
Ben Widawsky06fda602015-02-24 16:22:36 +0000422 i, pd->page_table[i]);
423 pd->page_table[i] = pt;
424 }
425
426 return 0;
427
428err_out:
429 while (i-- > pde)
Michel Thierry06dc68d2015-02-24 16:22:37 +0000430 unmap_and_free_pt(pd->page_table[i], dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000431 return ret;
432}
433
Michel Thierrye5815a22015-04-08 12:13:32 +0100434static void unmap_and_free_pd(struct i915_page_directory *pd,
435 struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000436{
437 if (pd->page) {
Michel Thierrye5815a22015-04-08 12:13:32 +0100438 i915_dma_unmap_single(pd, dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000439 __free_page(pd->page);
Michel Thierry33c88192015-04-08 12:13:33 +0100440 kfree(pd->used_pdes);
Ben Widawsky06fda602015-02-24 16:22:36 +0000441 kfree(pd);
442 }
443}
444
Michel Thierrye5815a22015-04-08 12:13:32 +0100445static struct i915_page_directory *alloc_pd_single(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000446{
Michel Thierryec565b32015-04-08 12:13:23 +0100447 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100448 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000449
450 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
451 if (!pd)
452 return ERR_PTR(-ENOMEM);
453
Michel Thierry33c88192015-04-08 12:13:33 +0100454 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
455 sizeof(*pd->used_pdes), GFP_KERNEL);
456 if (!pd->used_pdes)
457 goto free_pd;
458
Michel Thierry5a8e9942015-04-08 12:13:25 +0100459 pd->page = alloc_page(GFP_KERNEL);
Michel Thierry33c88192015-04-08 12:13:33 +0100460 if (!pd->page)
461 goto free_bitmap;
Ben Widawsky06fda602015-02-24 16:22:36 +0000462
Michel Thierrye5815a22015-04-08 12:13:32 +0100463 ret = i915_dma_map_single(pd, dev);
Michel Thierry33c88192015-04-08 12:13:33 +0100464 if (ret)
465 goto free_page;
Michel Thierrye5815a22015-04-08 12:13:32 +0100466
Ben Widawsky06fda602015-02-24 16:22:36 +0000467 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100468
469free_page:
470 __free_page(pd->page);
471free_bitmap:
472 kfree(pd->used_pdes);
473free_pd:
474 kfree(pd);
475
476 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000477}
478
Ben Widawsky94e409c2013-11-04 22:29:36 -0800479/* Broadwell Page Directory Pointer Descriptors */
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100480static int gen8_write_pdp(struct intel_engine_cs *ring,
481 unsigned entry,
482 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800483{
484 int ret;
485
486 BUG_ON(entry >= 4);
487
488 ret = intel_ring_begin(ring, 6);
489 if (ret)
490 return ret;
491
492 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
493 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100494 intel_ring_emit(ring, upper_32_bits(addr));
Ben Widawsky94e409c2013-11-04 22:29:36 -0800495 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
496 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100497 intel_ring_emit(ring, lower_32_bits(addr));
Ben Widawsky94e409c2013-11-04 22:29:36 -0800498 intel_ring_advance(ring);
499
500 return 0;
501}
502
Ben Widawskyeeb94882013-12-06 14:11:10 -0800503static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100504 struct intel_engine_cs *ring)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800505{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800506 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800507
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100508 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
509 struct i915_page_directory *pd = ppgtt->pdp.page_directory[i];
510 dma_addr_t pd_daddr = pd ? pd->daddr : ppgtt->scratch_pd->daddr;
511 /* The page directory might be NULL, but we need to clear out
512 * whatever the previous context might have used. */
513 ret = gen8_write_pdp(ring, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800514 if (ret)
515 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800516 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800517
Ben Widawskyeeb94882013-12-06 14:11:10 -0800518 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800519}
520
Ben Widawsky459108b2013-11-02 21:07:23 -0700521static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800522 uint64_t start,
523 uint64_t length,
Ben Widawsky459108b2013-11-02 21:07:23 -0700524 bool use_scratch)
525{
526 struct i915_hw_ppgtt *ppgtt =
527 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +0000528 gen8_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800529 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
530 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
531 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800532 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700533 unsigned last_pte, i;
534
535 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
536 I915_CACHE_LLC, use_scratch);
537
538 while (num_entries) {
Michel Thierryec565b32015-04-08 12:13:23 +0100539 struct i915_page_directory *pd;
540 struct i915_page_table *pt;
Ben Widawsky06fda602015-02-24 16:22:36 +0000541 struct page *page_table;
542
543 if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
544 continue;
545
546 pd = ppgtt->pdp.page_directory[pdpe];
547
548 if (WARN_ON(!pd->page_table[pde]))
549 continue;
550
551 pt = pd->page_table[pde];
552
553 if (WARN_ON(!pt->page))
554 continue;
555
556 page_table = pt->page;
Ben Widawsky459108b2013-11-02 21:07:23 -0700557
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800558 last_pte = pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +0000559 if (last_pte > GEN8_PTES)
560 last_pte = GEN8_PTES;
Ben Widawsky459108b2013-11-02 21:07:23 -0700561
562 pt_vaddr = kmap_atomic(page_table);
563
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800564 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700565 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800566 num_entries--;
567 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700568
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300569 if (!HAS_LLC(ppgtt->base.dev))
570 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky459108b2013-11-02 21:07:23 -0700571 kunmap_atomic(pt_vaddr);
572
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800573 pte = 0;
Michel Thierry07749ef2015-03-16 16:00:54 +0000574 if (++pde == I915_PDES) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800575 pdpe++;
576 pde = 0;
577 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700578 }
579}
580
Ben Widawsky9df15b42013-11-02 21:07:24 -0700581static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
582 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800583 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530584 enum i915_cache_level cache_level, u32 unused)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700585{
586 struct i915_hw_ppgtt *ppgtt =
587 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +0000588 gen8_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800589 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
590 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
591 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700592 struct sg_page_iter sg_iter;
593
Chris Wilson6f1cc992013-12-31 15:50:31 +0000594 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700595
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800596 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Ben Widawsky76643602015-01-22 17:01:24 +0000597 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800598 break;
599
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000600 if (pt_vaddr == NULL) {
Michel Thierryec565b32015-04-08 12:13:23 +0100601 struct i915_page_directory *pd = ppgtt->pdp.page_directory[pdpe];
602 struct i915_page_table *pt = pd->page_table[pde];
Ben Widawsky06fda602015-02-24 16:22:36 +0000603 struct page *page_table = pt->page;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000604
605 pt_vaddr = kmap_atomic(page_table);
606 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800607
608 pt_vaddr[pte] =
Chris Wilson6f1cc992013-12-31 15:50:31 +0000609 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
610 cache_level, true);
Michel Thierry07749ef2015-03-16 16:00:54 +0000611 if (++pte == GEN8_PTES) {
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300612 if (!HAS_LLC(ppgtt->base.dev))
613 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700614 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000615 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000616 if (++pde == I915_PDES) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800617 pdpe++;
618 pde = 0;
619 }
620 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700621 }
622 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300623 if (pt_vaddr) {
624 if (!HAS_LLC(ppgtt->base.dev))
625 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000626 kunmap_atomic(pt_vaddr);
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300627 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700628}
629
Michel Thierry69876be2015-04-08 12:13:27 +0100630static void __gen8_do_map_pt(gen8_pde_t * const pde,
631 struct i915_page_table *pt,
632 struct drm_device *dev)
633{
634 gen8_pde_t entry =
635 gen8_pde_encode(dev, pt->daddr, I915_CACHE_LLC);
636 *pde = entry;
637}
638
639static void gen8_initialize_pd(struct i915_address_space *vm,
640 struct i915_page_directory *pd)
641{
642 struct i915_hw_ppgtt *ppgtt =
643 container_of(vm, struct i915_hw_ppgtt, base);
644 gen8_pde_t *page_directory;
645 struct i915_page_table *pt;
646 int i;
647
648 page_directory = kmap_atomic(pd->page);
649 pt = ppgtt->scratch_pt;
650 for (i = 0; i < I915_PDES; i++)
651 /* Map the PDE to the page table */
652 __gen8_do_map_pt(page_directory + i, pt, vm->dev);
653
654 if (!HAS_LLC(vm->dev))
655 drm_clflush_virt_range(page_directory, PAGE_SIZE);
656
657 kunmap_atomic(page_directory);
658}
659
Michel Thierrye5815a22015-04-08 12:13:32 +0100660/* It's likely we'll map more than one pagetable at a time. This function will
661 * save us unnecessary kmap calls, but do no more functionally than multiple
662 * calls to map_pt. */
663static void gen8_map_pagetable_range(struct i915_page_directory *pd,
664 uint64_t start,
665 uint64_t length,
666 struct drm_device *dev)
667{
668 gen8_pde_t *page_directory = kmap_atomic(pd->page);
669 struct i915_page_table *pt;
670 uint64_t temp, pde;
671
672 gen8_for_each_pde(pt, pd, start, length, temp, pde)
673 __gen8_do_map_pt(page_directory + pde, pt, dev);
674
675 if (!HAS_LLC(dev))
676 drm_clflush_virt_range(page_directory, PAGE_SIZE);
677
678 kunmap_atomic(page_directory);
679}
680
Michel Thierryec565b32015-04-08 12:13:23 +0100681static void gen8_free_page_tables(struct i915_page_directory *pd, struct drm_device *dev)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800682{
683 int i;
684
Ben Widawsky06fda602015-02-24 16:22:36 +0000685 if (!pd->page)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800686 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800687
Michel Thierry33c88192015-04-08 12:13:33 +0100688 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000689 if (WARN_ON(!pd->page_table[i]))
690 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800691
Michel Thierry06dc68d2015-02-24 16:22:37 +0000692 unmap_and_free_pt(pd->page_table[i], dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000693 pd->page_table[i] = NULL;
694 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000695}
696
697static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800698{
699 int i;
700
Michel Thierry33c88192015-04-08 12:13:33 +0100701 for_each_set_bit(i, ppgtt->pdp.used_pdpes, GEN8_LEGACY_PDPES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000702 if (WARN_ON(!ppgtt->pdp.page_directory[i]))
703 continue;
704
Michel Thierry06dc68d2015-02-24 16:22:37 +0000705 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Michel Thierrye5815a22015-04-08 12:13:32 +0100706 unmap_and_free_pd(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800707 }
Michel Thierry69876be2015-04-08 12:13:27 +0100708
Michel Thierrye5815a22015-04-08 12:13:32 +0100709 unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev);
Michel Thierry69876be2015-04-08 12:13:27 +0100710 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800711}
712
Ben Widawsky37aca442013-11-04 20:47:32 -0800713static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
714{
715 struct i915_hw_ppgtt *ppgtt =
716 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800717
Ben Widawskyb45a6712014-02-12 14:28:44 -0800718 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800719}
720
Michel Thierrye5815a22015-04-08 12:13:32 +0100721static int gen8_ppgtt_alloc_pagetabs(struct i915_hw_ppgtt *ppgtt,
722 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +0100723 uint64_t start,
Michel Thierrye5815a22015-04-08 12:13:32 +0100724 uint64_t length)
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000725{
Michel Thierrye5815a22015-04-08 12:13:32 +0100726 struct drm_device *dev = ppgtt->base.dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100727 struct i915_page_table *unused;
728 uint64_t temp;
729 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000730
Michel Thierry5441f0c2015-04-08 12:13:28 +0100731 gen8_for_each_pde(unused, pd, start, length, temp, pde) {
732 WARN_ON(unused);
Michel Thierrye5815a22015-04-08 12:13:32 +0100733 pd->page_table[pde] = alloc_pt_single(dev);
Michel Thierry5441f0c2015-04-08 12:13:28 +0100734 if (IS_ERR(pd->page_table[pde]))
Ben Widawsky06fda602015-02-24 16:22:36 +0000735 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100736
Michel Thierrye5815a22015-04-08 12:13:32 +0100737 gen8_initialize_pt(&ppgtt->base, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000738 }
739
740 return 0;
741
742unwind_out:
Michel Thierry5441f0c2015-04-08 12:13:28 +0100743 while (pde--)
Michel Thierrye5815a22015-04-08 12:13:32 +0100744 unmap_and_free_pt(pd->page_table[pde], dev);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000745
746 return -ENOMEM;
747}
748
Michel Thierryc488dbb2015-04-08 12:13:31 +0100749static int gen8_ppgtt_alloc_page_directories(struct i915_hw_ppgtt *ppgtt,
750 struct i915_page_directory_pointer *pdp,
Michel Thierry69876be2015-04-08 12:13:27 +0100751 uint64_t start,
752 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800753{
Michel Thierrye5815a22015-04-08 12:13:32 +0100754 struct drm_device *dev = ppgtt->base.dev;
Michel Thierry69876be2015-04-08 12:13:27 +0100755 struct i915_page_directory *unused;
756 uint64_t temp;
757 uint32_t pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800758
Michel Thierry69876be2015-04-08 12:13:27 +0100759 /* FIXME: PPGTT container_of won't work for 64b */
760 WARN_ON((start + length) > 0x800000000ULL);
761
762 gen8_for_each_pdpe(unused, pdp, start, length, temp, pdpe) {
763 WARN_ON(unused);
Michel Thierrye5815a22015-04-08 12:13:32 +0100764 pdp->page_directory[pdpe] = alloc_pd_single(dev);
Michel Thierry33c88192015-04-08 12:13:33 +0100765
Michel Thierryc488dbb2015-04-08 12:13:31 +0100766 if (IS_ERR(pdp->page_directory[pdpe]))
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000767 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +0100768
769 gen8_initialize_pd(&ppgtt->base,
770 ppgtt->pdp.page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000771 }
772
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800773 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000774
775unwind_out:
Michel Thierry09942c62015-04-08 12:13:30 +0100776 while (pdpe--)
Michel Thierrye5815a22015-04-08 12:13:32 +0100777 unmap_and_free_pd(pdp->page_directory[pdpe], dev);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000778
779 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800780}
781
Michel Thierrye5815a22015-04-08 12:13:32 +0100782static int gen8_alloc_va_range(struct i915_address_space *vm,
783 uint64_t start,
784 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800785{
Michel Thierrye5815a22015-04-08 12:13:32 +0100786 struct i915_hw_ppgtt *ppgtt =
787 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry5441f0c2015-04-08 12:13:28 +0100788 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100789 const uint64_t orig_start = start;
790 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100791 uint64_t temp;
792 uint32_t pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800793 int ret;
794
Michel Thierry33c88192015-04-08 12:13:33 +0100795 /* Do the allocations first so we can easily bail out */
Michel Thierryc488dbb2015-04-08 12:13:31 +0100796 ret = gen8_ppgtt_alloc_page_directories(ppgtt, &ppgtt->pdp, start, length);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800797 if (ret)
798 return ret;
799
Michel Thierry5441f0c2015-04-08 12:13:28 +0100800 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
Michel Thierrye5815a22015-04-08 12:13:32 +0100801 ret = gen8_ppgtt_alloc_pagetabs(ppgtt, pd, start, length);
Michel Thierry5441f0c2015-04-08 12:13:28 +0100802 if (ret)
803 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100804 }
805
Michel Thierry33c88192015-04-08 12:13:33 +0100806 /* Now mark everything we've touched as used. This doesn't allow for
807 * robust error checking, but it makes the code a hell of a lot simpler.
808 */
809 start = orig_start;
810 length = orig_length;
811
812 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
813 struct i915_page_table *pt;
814 uint64_t pd_len = gen8_clamp_pd(start, length);
815 uint64_t pd_start = start;
816 uint32_t pde;
817
818 gen8_for_each_pde(pt, &ppgtt->pd, pd_start, pd_len, temp, pde) {
819 bitmap_set(pd->page_table[pde]->used_ptes,
820 gen8_pte_index(start),
821 gen8_pte_count(start, length));
822 set_bit(pde, pd->used_pdes);
823 }
824 set_bit(pdpe, ppgtt->pdp.used_pdpes);
825 }
826
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000827 return 0;
828
829err_out:
830 gen8_ppgtt_free(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800831 return ret;
832}
833
Daniel Vettereb0b44a2015-03-18 14:47:59 +0100834/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800835 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
836 * with a net effect resembling a 2-level page table in normal x86 terms. Each
837 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
838 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800839 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800840 */
Ben Widawsky37aca442013-11-04 20:47:32 -0800841static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
842{
Michel Thierrye5815a22015-04-08 12:13:32 +0100843 struct i915_page_directory *pd;
844 uint64_t temp, start = 0;
845 const uint64_t orig_length = size;
846 uint32_t pdpe;
847 int ret;
Ben Widawsky37aca442013-11-04 20:47:32 -0800848
Michel Thierry69876be2015-04-08 12:13:27 +0100849 ppgtt->base.start = 0;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100850 ppgtt->base.total = size;
Michel Thierrye5815a22015-04-08 12:13:32 +0100851 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
852 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
853 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
854 ppgtt->switch_mm = gen8_mm_switch;
Michel Thierry69876be2015-04-08 12:13:27 +0100855
856 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
857 if (IS_ERR(ppgtt->scratch_pt))
858 return PTR_ERR(ppgtt->scratch_pt);
859
Michel Thierrye5815a22015-04-08 12:13:32 +0100860 ppgtt->scratch_pd = alloc_pd_single(ppgtt->base.dev);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100861 if (IS_ERR(ppgtt->scratch_pd))
862 return PTR_ERR(ppgtt->scratch_pd);
863
Michel Thierry69876be2015-04-08 12:13:27 +0100864 gen8_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100865 gen8_initialize_pd(&ppgtt->base, ppgtt->scratch_pd);
Michel Thierry69876be2015-04-08 12:13:27 +0100866
Michel Thierrye5815a22015-04-08 12:13:32 +0100867 ret = gen8_alloc_va_range(&ppgtt->base, start, size);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100868 if (ret) {
Michel Thierrye5815a22015-04-08 12:13:32 +0100869 unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100870 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800871 return ret;
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100872 }
Ben Widawsky37aca442013-11-04 20:47:32 -0800873
Michel Thierrye5815a22015-04-08 12:13:32 +0100874 start = 0;
875 size = orig_length;
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800876
Michel Thierrye5815a22015-04-08 12:13:32 +0100877 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, size, temp, pdpe)
878 gen8_map_pagetable_range(pd, start, size, ppgtt->base.dev);
Mika Kuoppala29343682015-03-04 14:55:17 +0200879
Michel Thierry09942c62015-04-08 12:13:30 +0100880 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Ben Widawsky28cf5412013-11-02 21:07:26 -0700881 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -0800882}
883
Ben Widawsky87d60b62013-12-06 14:11:29 -0800884static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
885{
Ben Widawsky87d60b62013-12-06 14:11:29 -0800886 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +0100887 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +0000888 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -0800889 uint32_t pd_entry;
Michel Thierry09942c62015-04-08 12:13:30 +0100890 uint32_t pte, pde, temp;
891 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -0800892
Akash Goel24f3a8c2014-06-17 10:59:42 +0530893 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800894
Michel Thierry09942c62015-04-08 12:13:30 +0100895 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -0800896 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +0000897 gen6_pte_t *pt_vaddr;
Ben Widawsky06fda602015-02-24 16:22:36 +0000898 dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
Michel Thierry09942c62015-04-08 12:13:30 +0100899 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800900 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
901
902 if (pd_entry != expected)
903 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
904 pde,
905 pd_entry,
906 expected);
907 seq_printf(m, "\tPDE: %x\n", pd_entry);
908
Ben Widawsky06fda602015-02-24 16:22:36 +0000909 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
Michel Thierry07749ef2015-03-16 16:00:54 +0000910 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -0800911 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +0000912 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -0800913 (pte * PAGE_SIZE);
914 int i;
915 bool found = false;
916 for (i = 0; i < 4; i++)
917 if (pt_vaddr[pte + i] != scratch_pte)
918 found = true;
919 if (!found)
920 continue;
921
922 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
923 for (i = 0; i < 4; i++) {
924 if (pt_vaddr[pte + i] != scratch_pte)
925 seq_printf(m, " %08x", pt_vaddr[pte + i]);
926 else
927 seq_puts(m, " SCRATCH ");
928 }
929 seq_puts(m, "\n");
930 }
931 kunmap_atomic(pt_vaddr);
932 }
933}
934
Ben Widawsky678d96f2015-03-16 16:00:56 +0000935/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +0100936static void gen6_write_pde(struct i915_page_directory *pd,
937 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -0700938{
Ben Widawsky678d96f2015-03-16 16:00:56 +0000939 /* Caller needs to make sure the write completes if necessary */
940 struct i915_hw_ppgtt *ppgtt =
941 container_of(pd, struct i915_hw_ppgtt, pd);
942 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -0700943
Ben Widawsky678d96f2015-03-16 16:00:56 +0000944 pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr);
945 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -0700946
Ben Widawsky678d96f2015-03-16 16:00:56 +0000947 writel(pd_entry, ppgtt->pd_addr + pde);
948}
Ben Widawsky61973492013-04-08 18:43:54 -0700949
Ben Widawsky678d96f2015-03-16 16:00:56 +0000950/* Write all the page tables found in the ppgtt structure to incrementing page
951 * directories. */
952static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +0100953 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +0000954 uint32_t start, uint32_t length)
955{
Michel Thierryec565b32015-04-08 12:13:23 +0100956 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000957 uint32_t pde, temp;
958
959 gen6_for_each_pde(pt, pd, start, length, temp, pde)
960 gen6_write_pde(pd, pde, pt);
961
962 /* Make sure write is complete before other code can use this page
963 * table. Also require for WC mapped PTEs */
964 readl(dev_priv->gtt.gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -0700965}
966
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800967static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -0700968{
Ben Widawsky7324cc02015-02-24 16:22:35 +0000969 BUG_ON(ppgtt->pd.pd_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -0700970
Ben Widawsky7324cc02015-02-24 16:22:35 +0000971 return (ppgtt->pd.pd_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -0800972}
Ben Widawsky61973492013-04-08 18:43:54 -0700973
Ben Widawsky90252e52013-12-06 14:11:12 -0800974static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100975 struct intel_engine_cs *ring)
Ben Widawsky90252e52013-12-06 14:11:12 -0800976{
Ben Widawsky90252e52013-12-06 14:11:12 -0800977 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -0700978
Ben Widawsky90252e52013-12-06 14:11:12 -0800979 /* NB: TLBs must be flushed and invalidated before a switch */
980 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
981 if (ret)
982 return ret;
983
984 ret = intel_ring_begin(ring, 6);
985 if (ret)
986 return ret;
987
988 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
989 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
990 intel_ring_emit(ring, PP_DIR_DCLV_2G);
991 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
992 intel_ring_emit(ring, get_pd_offset(ppgtt));
993 intel_ring_emit(ring, MI_NOOP);
994 intel_ring_advance(ring);
995
996 return 0;
997}
998
Yu Zhang71ba2d62015-02-10 19:05:54 +0800999static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
1000 struct intel_engine_cs *ring)
1001{
1002 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1003
1004 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1005 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1006 return 0;
1007}
1008
Ben Widawsky48a10382013-12-06 14:11:11 -08001009static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001010 struct intel_engine_cs *ring)
Ben Widawsky48a10382013-12-06 14:11:11 -08001011{
Ben Widawsky48a10382013-12-06 14:11:11 -08001012 int ret;
1013
Ben Widawsky48a10382013-12-06 14:11:11 -08001014 /* NB: TLBs must be flushed and invalidated before a switch */
1015 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1016 if (ret)
1017 return ret;
1018
1019 ret = intel_ring_begin(ring, 6);
1020 if (ret)
1021 return ret;
1022
1023 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1024 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1025 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1026 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1027 intel_ring_emit(ring, get_pd_offset(ppgtt));
1028 intel_ring_emit(ring, MI_NOOP);
1029 intel_ring_advance(ring);
1030
Ben Widawsky90252e52013-12-06 14:11:12 -08001031 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1032 if (ring->id != RCS) {
1033 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1034 if (ret)
1035 return ret;
1036 }
1037
Ben Widawsky48a10382013-12-06 14:11:11 -08001038 return 0;
1039}
1040
Ben Widawskyeeb94882013-12-06 14:11:10 -08001041static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001042 struct intel_engine_cs *ring)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001043{
1044 struct drm_device *dev = ppgtt->base.dev;
1045 struct drm_i915_private *dev_priv = dev->dev_private;
1046
Ben Widawsky48a10382013-12-06 14:11:11 -08001047
Ben Widawskyeeb94882013-12-06 14:11:10 -08001048 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1049 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1050
1051 POSTING_READ(RING_PP_DIR_DCLV(ring));
1052
1053 return 0;
1054}
1055
Daniel Vetter82460d92014-08-06 20:19:53 +02001056static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001057{
Ben Widawskyeeb94882013-12-06 14:11:10 -08001058 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001059 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +02001060 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001061
1062 for_each_ring(ring, dev_priv, j) {
1063 I915_WRITE(RING_MODE_GEN7(ring),
1064 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001065 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001066}
1067
Daniel Vetter82460d92014-08-06 20:19:53 +02001068static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001069{
Jani Nikula50227e12014-03-31 14:27:21 +03001070 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001071 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001072 uint32_t ecochk, ecobits;
1073 int i;
1074
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001075 ecobits = I915_READ(GAC_ECO_BITS);
1076 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1077
1078 ecochk = I915_READ(GAM_ECOCHK);
1079 if (IS_HASWELL(dev)) {
1080 ecochk |= ECOCHK_PPGTT_WB_HSW;
1081 } else {
1082 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1083 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1084 }
1085 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001086
Ben Widawsky61973492013-04-08 18:43:54 -07001087 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001088 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001089 I915_WRITE(RING_MODE_GEN7(ring),
1090 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001091 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001092}
1093
Daniel Vetter82460d92014-08-06 20:19:53 +02001094static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001095{
Jani Nikula50227e12014-03-31 14:27:21 +03001096 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001097 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001098
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001099 ecobits = I915_READ(GAC_ECO_BITS);
1100 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1101 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001102
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001103 gab_ctl = I915_READ(GAB_CTL);
1104 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001105
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001106 ecochk = I915_READ(GAM_ECOCHK);
1107 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001108
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001109 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001110}
1111
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001112/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001113static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001114 uint64_t start,
1115 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001116 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001117{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001118 struct i915_hw_ppgtt *ppgtt =
1119 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001120 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001121 unsigned first_entry = start >> PAGE_SHIFT;
1122 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001123 unsigned act_pt = first_entry / GEN6_PTES;
1124 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001125 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001126
Akash Goel24f3a8c2014-06-17 10:59:42 +05301127 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001128
Daniel Vetter7bddb012012-02-09 17:15:47 +01001129 while (num_entries) {
1130 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001131 if (last_pte > GEN6_PTES)
1132 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001133
Ben Widawsky06fda602015-02-24 16:22:36 +00001134 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001135
1136 for (i = first_pte; i < last_pte; i++)
1137 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001138
1139 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001140
Daniel Vetter7bddb012012-02-09 17:15:47 +01001141 num_entries -= last_pte - first_pte;
1142 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001143 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001144 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001145}
1146
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001147static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001148 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001149 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301150 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001151{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001152 struct i915_hw_ppgtt *ppgtt =
1153 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001154 gen6_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -08001155 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001156 unsigned act_pt = first_entry / GEN6_PTES;
1157 unsigned act_pte = first_entry % GEN6_PTES;
Imre Deak6e995e22013-02-18 19:28:04 +02001158 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001159
Chris Wilsoncc797142013-12-31 15:50:30 +00001160 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +02001161 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001162 if (pt_vaddr == NULL)
Ben Widawsky06fda602015-02-24 16:22:36 +00001163 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001164
Chris Wilsoncc797142013-12-31 15:50:30 +00001165 pt_vaddr[act_pte] =
1166 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +05301167 cache_level, true, flags);
1168
Michel Thierry07749ef2015-03-16 16:00:54 +00001169 if (++act_pte == GEN6_PTES) {
Imre Deak6e995e22013-02-18 19:28:04 +02001170 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001171 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001172 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001173 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001174 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001175 }
Chris Wilsoncc797142013-12-31 15:50:30 +00001176 if (pt_vaddr)
1177 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001178}
1179
Ben Widawsky563222a2015-03-19 12:53:28 +00001180/* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we
1181 * are switching between contexts with the same LRCA, we also must do a force
1182 * restore.
1183 */
1184static inline void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1185{
1186 /* If current vm != vm, */
1187 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1188}
1189
Michel Thierry4933d512015-03-24 15:46:22 +00001190static void gen6_initialize_pt(struct i915_address_space *vm,
Michel Thierryec565b32015-04-08 12:13:23 +01001191 struct i915_page_table *pt)
Michel Thierry4933d512015-03-24 15:46:22 +00001192{
1193 gen6_pte_t *pt_vaddr, scratch_pte;
1194 int i;
1195
1196 WARN_ON(vm->scratch.addr == 0);
1197
1198 scratch_pte = vm->pte_encode(vm->scratch.addr,
1199 I915_CACHE_LLC, true, 0);
1200
1201 pt_vaddr = kmap_atomic(pt->page);
1202
1203 for (i = 0; i < GEN6_PTES; i++)
1204 pt_vaddr[i] = scratch_pte;
1205
1206 kunmap_atomic(pt_vaddr);
1207}
1208
Ben Widawsky678d96f2015-03-16 16:00:56 +00001209static int gen6_alloc_va_range(struct i915_address_space *vm,
1210 uint64_t start, uint64_t length)
1211{
Michel Thierry4933d512015-03-24 15:46:22 +00001212 DECLARE_BITMAP(new_page_tables, I915_PDES);
1213 struct drm_device *dev = vm->dev;
1214 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001215 struct i915_hw_ppgtt *ppgtt =
1216 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryec565b32015-04-08 12:13:23 +01001217 struct i915_page_table *pt;
Michel Thierry4933d512015-03-24 15:46:22 +00001218 const uint32_t start_save = start, length_save = length;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001219 uint32_t pde, temp;
Michel Thierry4933d512015-03-24 15:46:22 +00001220 int ret;
1221
1222 WARN_ON(upper_32_bits(start));
1223
1224 bitmap_zero(new_page_tables, I915_PDES);
1225
1226 /* The allocation is done in two stages so that we can bail out with
1227 * minimal amount of pain. The first stage finds new page tables that
1228 * need allocation. The second stage marks use ptes within the page
1229 * tables.
1230 */
1231 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1232 if (pt != ppgtt->scratch_pt) {
1233 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1234 continue;
1235 }
1236
1237 /* We've already allocated a page table */
1238 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1239
1240 pt = alloc_pt_single(dev);
1241 if (IS_ERR(pt)) {
1242 ret = PTR_ERR(pt);
1243 goto unwind_out;
1244 }
1245
1246 gen6_initialize_pt(vm, pt);
1247
1248 ppgtt->pd.page_table[pde] = pt;
1249 set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001250 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001251 }
1252
1253 start = start_save;
1254 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001255
1256 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1257 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1258
1259 bitmap_zero(tmp_bitmap, GEN6_PTES);
1260 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1261 gen6_pte_count(start, length));
1262
Michel Thierry4933d512015-03-24 15:46:22 +00001263 if (test_and_clear_bit(pde, new_page_tables))
1264 gen6_write_pde(&ppgtt->pd, pde, pt);
1265
Michel Thierry72744cb2015-03-24 15:46:23 +00001266 trace_i915_page_table_entry_map(vm, pde, pt,
1267 gen6_pte_index(start),
1268 gen6_pte_count(start, length),
1269 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001270 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001271 GEN6_PTES);
1272 }
1273
Michel Thierry4933d512015-03-24 15:46:22 +00001274 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1275
1276 /* Make sure write is complete before other code can use this page
1277 * table. Also require for WC mapped PTEs */
1278 readl(dev_priv->gtt.gsm);
1279
Ben Widawsky563222a2015-03-19 12:53:28 +00001280 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001281 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001282
1283unwind_out:
1284 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001285 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001286
1287 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1288 unmap_and_free_pt(pt, vm->dev);
1289 }
1290
1291 mark_tlbs_dirty(ppgtt);
1292 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001293}
1294
Ben Widawskya00d8252014-02-19 22:05:48 -08001295static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
1296{
Michel Thierry09942c62015-04-08 12:13:30 +01001297 struct i915_page_table *pt;
1298 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001299
Michel Thierry09942c62015-04-08 12:13:30 +01001300 gen6_for_all_pdes(pt, ppgtt, pde) {
Michel Thierry4933d512015-03-24 15:46:22 +00001301 if (pt != ppgtt->scratch_pt)
Michel Thierry09942c62015-04-08 12:13:30 +01001302 unmap_and_free_pt(pt, ppgtt->base.dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001303 }
1304
1305 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Michel Thierrye5815a22015-04-08 12:13:32 +01001306 unmap_and_free_pd(&ppgtt->pd, ppgtt->base.dev);
Daniel Vetter3440d262013-01-24 13:49:56 -08001307}
1308
Ben Widawskya00d8252014-02-19 22:05:48 -08001309static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1310{
1311 struct i915_hw_ppgtt *ppgtt =
1312 container_of(vm, struct i915_hw_ppgtt, base);
1313
Ben Widawskya00d8252014-02-19 22:05:48 -08001314 drm_mm_remove_node(&ppgtt->node);
1315
Ben Widawskya00d8252014-02-19 22:05:48 -08001316 gen6_ppgtt_free(ppgtt);
1317}
1318
Ben Widawskyb1465202014-02-19 22:05:49 -08001319static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001320{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001321 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001322 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001323 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001324 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001325
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001326 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1327 * allocator works in address space sizes, so it's multiplied by page
1328 * size. We allocate at the top of the GTT to avoid fragmentation.
1329 */
1330 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001331 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
1332 if (IS_ERR(ppgtt->scratch_pt))
1333 return PTR_ERR(ppgtt->scratch_pt);
1334
1335 gen6_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
1336
Ben Widawskye3cc1992013-12-06 14:11:08 -08001337alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001338 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1339 &ppgtt->node, GEN6_PD_SIZE,
1340 GEN6_PD_ALIGN, 0,
1341 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07001342 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001343 if (ret == -ENOSPC && !retried) {
1344 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1345 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02001346 I915_CACHE_NONE,
1347 0, dev_priv->gtt.base.total,
1348 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001349 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001350 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001351
1352 retried = true;
1353 goto alloc;
1354 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001355
Ben Widawskyc8c26622015-01-22 17:01:25 +00001356 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001357 goto err_out;
1358
Ben Widawskyc8c26622015-01-22 17:01:25 +00001359
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001360 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1361 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001362
Ben Widawskyc8c26622015-01-22 17:01:25 +00001363 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001364
1365err_out:
Michel Thierry4933d512015-03-24 15:46:22 +00001366 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001367 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001368}
1369
Ben Widawskyb1465202014-02-19 22:05:49 -08001370static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1371{
kbuild test robot2f2cf682015-03-27 19:26:35 +08001372 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08001373}
1374
Michel Thierry4933d512015-03-24 15:46:22 +00001375static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1376 uint64_t start, uint64_t length)
1377{
Michel Thierryec565b32015-04-08 12:13:23 +01001378 struct i915_page_table *unused;
Michel Thierry4933d512015-03-24 15:46:22 +00001379 uint32_t pde, temp;
1380
1381 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
1382 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1383}
1384
1385static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt, bool aliasing)
Ben Widawskyb1465202014-02-19 22:05:49 -08001386{
1387 struct drm_device *dev = ppgtt->base.dev;
1388 struct drm_i915_private *dev_priv = dev->dev_private;
1389 int ret;
1390
1391 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001392 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001393 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001394 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001395 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001396 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001397 ppgtt->switch_mm = gen7_mm_switch;
1398 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001399 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001400
Yu Zhang71ba2d62015-02-10 19:05:54 +08001401 if (intel_vgpu_active(dev))
1402 ppgtt->switch_mm = vgpu_mm_switch;
1403
Ben Widawskyb1465202014-02-19 22:05:49 -08001404 ret = gen6_ppgtt_alloc(ppgtt);
1405 if (ret)
1406 return ret;
1407
Michel Thierry4933d512015-03-24 15:46:22 +00001408 if (aliasing) {
1409 /* preallocate all pts */
Michel Thierry09942c62015-04-08 12:13:30 +01001410 ret = alloc_pt_range(&ppgtt->pd, 0, I915_PDES,
Michel Thierry4933d512015-03-24 15:46:22 +00001411 ppgtt->base.dev);
1412
1413 if (ret) {
1414 gen6_ppgtt_cleanup(&ppgtt->base);
1415 return ret;
1416 }
1417 }
1418
Ben Widawsky678d96f2015-03-16 16:00:56 +00001419 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001420 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1421 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1422 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001423 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01001424 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001425 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001426
Ben Widawsky7324cc02015-02-24 16:22:35 +00001427 ppgtt->pd.pd_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00001428 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001429
Ben Widawsky678d96f2015-03-16 16:00:56 +00001430 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
1431 ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
1432
Michel Thierry4933d512015-03-24 15:46:22 +00001433 if (aliasing)
1434 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
1435 else
1436 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001437
Ben Widawsky678d96f2015-03-16 16:00:56 +00001438 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
1439
Thierry Reding440fd522015-01-23 09:05:06 +01001440 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001441 ppgtt->node.size >> 20,
1442 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001443
Daniel Vetterfa76da32014-08-06 20:19:54 +02001444 DRM_DEBUG("Adding PPGTT at offset %x\n",
Ben Widawsky7324cc02015-02-24 16:22:35 +00001445 ppgtt->pd.pd_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001446
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001447 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001448}
1449
Michel Thierry4933d512015-03-24 15:46:22 +00001450static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt,
1451 bool aliasing)
Daniel Vetter3440d262013-01-24 13:49:56 -08001452{
1453 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter3440d262013-01-24 13:49:56 -08001454
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001455 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001456 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001457
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001458 if (INTEL_INFO(dev)->gen < 8)
Michel Thierry4933d512015-03-24 15:46:22 +00001459 return gen6_ppgtt_init(ppgtt, aliasing);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001460 else
Rodrigo Vivi1eb0f002014-12-03 04:55:26 -08001461 return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001462}
1463int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1464{
1465 struct drm_i915_private *dev_priv = dev->dev_private;
1466 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001467
Michel Thierry4933d512015-03-24 15:46:22 +00001468 ret = __hw_ppgtt_init(dev, ppgtt, false);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001469 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001470 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001471 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1472 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001473 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001474 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001475
1476 return ret;
1477}
1478
Daniel Vetter82460d92014-08-06 20:19:53 +02001479int i915_ppgtt_init_hw(struct drm_device *dev)
1480{
1481 struct drm_i915_private *dev_priv = dev->dev_private;
1482 struct intel_engine_cs *ring;
1483 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1484 int i, ret = 0;
1485
Thomas Daniel671b50132014-08-20 16:24:50 +01001486 /* In the case of execlists, PPGTT is enabled by the context descriptor
1487 * and the PDPs are contained within the context itself. We don't
1488 * need to do anything here. */
1489 if (i915.enable_execlists)
1490 return 0;
1491
Daniel Vetter82460d92014-08-06 20:19:53 +02001492 if (!USES_PPGTT(dev))
1493 return 0;
1494
1495 if (IS_GEN6(dev))
1496 gen6_ppgtt_enable(dev);
1497 else if (IS_GEN7(dev))
1498 gen7_ppgtt_enable(dev);
1499 else if (INTEL_INFO(dev)->gen >= 8)
1500 gen8_ppgtt_enable(dev);
1501 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01001502 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02001503
1504 if (ppgtt) {
1505 for_each_ring(ring, dev_priv, i) {
McAulay, Alistair6689c162014-08-15 18:51:35 +01001506 ret = ppgtt->switch_mm(ppgtt, ring);
Daniel Vetter82460d92014-08-06 20:19:53 +02001507 if (ret != 0)
1508 return ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001509 }
1510 }
1511
1512 return ret;
1513}
Daniel Vetter4d884702014-08-06 15:04:47 +02001514struct i915_hw_ppgtt *
1515i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1516{
1517 struct i915_hw_ppgtt *ppgtt;
1518 int ret;
1519
1520 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1521 if (!ppgtt)
1522 return ERR_PTR(-ENOMEM);
1523
1524 ret = i915_ppgtt_init(dev, ppgtt);
1525 if (ret) {
1526 kfree(ppgtt);
1527 return ERR_PTR(ret);
1528 }
1529
1530 ppgtt->file_priv = fpriv;
1531
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001532 trace_i915_ppgtt_create(&ppgtt->base);
1533
Daniel Vetter4d884702014-08-06 15:04:47 +02001534 return ppgtt;
1535}
1536
Daniel Vetteree960be2014-08-06 15:04:45 +02001537void i915_ppgtt_release(struct kref *kref)
1538{
1539 struct i915_hw_ppgtt *ppgtt =
1540 container_of(kref, struct i915_hw_ppgtt, ref);
1541
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001542 trace_i915_ppgtt_release(&ppgtt->base);
1543
Daniel Vetteree960be2014-08-06 15:04:45 +02001544 /* vmas should already be unbound */
1545 WARN_ON(!list_empty(&ppgtt->base.active_list));
1546 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1547
Daniel Vetter19dd1202014-08-06 15:04:55 +02001548 list_del(&ppgtt->base.global_link);
1549 drm_mm_takedown(&ppgtt->base.mm);
1550
Daniel Vetteree960be2014-08-06 15:04:45 +02001551 ppgtt->base.cleanup(&ppgtt->base);
1552 kfree(ppgtt);
1553}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001554
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001555static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001556ppgtt_bind_vma(struct i915_vma *vma,
1557 enum i915_cache_level cache_level,
1558 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001559{
Akash Goel24f3a8c2014-06-17 10:59:42 +05301560 /* Currently applicable only to VLV */
1561 if (vma->obj->gt_ro)
1562 flags |= PTE_READ_ONLY;
1563
Ben Widawsky782f1492014-02-20 11:50:33 -08001564 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301565 cache_level, flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001566}
1567
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001568static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001569{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001570 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001571 vma->node.start,
1572 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001573 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001574}
1575
Ben Widawskya81cc002013-01-18 12:30:31 -08001576extern int intel_iommu_gfx_mapped;
1577/* Certain Gen5 chipsets require require idling the GPU before
1578 * unmapping anything from the GTT when VT-d is enabled.
1579 */
1580static inline bool needs_idle_maps(struct drm_device *dev)
1581{
1582#ifdef CONFIG_INTEL_IOMMU
1583 /* Query intel_iommu to see if we need the workaround. Presumably that
1584 * was loaded first.
1585 */
1586 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1587 return true;
1588#endif
1589 return false;
1590}
1591
Ben Widawsky5c042282011-10-17 15:51:55 -07001592static bool do_idling(struct drm_i915_private *dev_priv)
1593{
1594 bool ret = dev_priv->mm.interruptible;
1595
Ben Widawskya81cc002013-01-18 12:30:31 -08001596 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001597 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001598 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001599 DRM_ERROR("Couldn't idle GPU\n");
1600 /* Wait a bit, in hopes it avoids the hang */
1601 udelay(10);
1602 }
1603 }
1604
1605 return ret;
1606}
1607
1608static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1609{
Ben Widawskya81cc002013-01-18 12:30:31 -08001610 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001611 dev_priv->mm.interruptible = interruptible;
1612}
1613
Ben Widawsky828c7902013-10-16 09:21:30 -07001614void i915_check_and_clear_faults(struct drm_device *dev)
1615{
1616 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001617 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07001618 int i;
1619
1620 if (INTEL_INFO(dev)->gen < 6)
1621 return;
1622
1623 for_each_ring(ring, dev_priv, i) {
1624 u32 fault_reg;
1625 fault_reg = I915_READ(RING_FAULT_REG(ring));
1626 if (fault_reg & RING_FAULT_VALID) {
1627 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02001628 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07001629 "\tAddress space: %s\n"
1630 "\tSource ID: %d\n"
1631 "\tType: %d\n",
1632 fault_reg & PAGE_MASK,
1633 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1634 RING_FAULT_SRCID(fault_reg),
1635 RING_FAULT_FAULT_TYPE(fault_reg));
1636 I915_WRITE(RING_FAULT_REG(ring),
1637 fault_reg & ~RING_FAULT_VALID);
1638 }
1639 }
1640 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1641}
1642
Chris Wilson91e56492014-09-25 10:13:12 +01001643static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
1644{
1645 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
1646 intel_gtt_chipset_flush();
1647 } else {
1648 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1649 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1650 }
1651}
1652
Ben Widawsky828c7902013-10-16 09:21:30 -07001653void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1654{
1655 struct drm_i915_private *dev_priv = dev->dev_private;
1656
1657 /* Don't bother messing with faults pre GEN6 as we have little
1658 * documentation supporting that it's a good idea.
1659 */
1660 if (INTEL_INFO(dev)->gen < 6)
1661 return;
1662
1663 i915_check_and_clear_faults(dev);
1664
1665 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001666 dev_priv->gtt.base.start,
1667 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001668 true);
Chris Wilson91e56492014-09-25 10:13:12 +01001669
1670 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07001671}
1672
Daniel Vetter76aaf222010-11-05 22:23:30 +01001673void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1674{
1675 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001676 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001677 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001678
Ben Widawsky828c7902013-10-16 09:21:30 -07001679 i915_check_and_clear_faults(dev);
1680
Chris Wilsonbee4a182011-01-21 10:54:32 +00001681 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001682 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001683 dev_priv->gtt.base.start,
1684 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001685 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001686
Ben Widawsky35c20a62013-05-31 11:28:48 -07001687 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001688 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1689 &dev_priv->gtt.base);
1690 if (!vma)
1691 continue;
1692
Chris Wilson2c225692013-08-09 12:26:45 +01001693 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001694 /* The bind_vma code tries to be smart about tracking mappings.
1695 * Unfortunately above, we've just wiped out the mappings
1696 * without telling our object about it. So we need to fake it.
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001697 *
1698 * Bind is not expected to fail since this is only called on
1699 * resume and assumption is all requirements exist already.
Ben Widawsky6f65e292013-12-06 14:10:56 -08001700 */
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001701 vma->bound &= ~GLOBAL_BIND;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001702 WARN_ON(i915_vma_bind(vma, obj->cache_level, GLOBAL_BIND));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001703 }
1704
Ben Widawsky80da2162013-12-06 14:11:17 -08001705
Ben Widawskya2319c02014-03-18 16:09:37 -07001706 if (INTEL_INFO(dev)->gen >= 8) {
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001707 if (IS_CHERRYVIEW(dev))
1708 chv_setup_private_ppat(dev_priv);
1709 else
1710 bdw_setup_private_ppat(dev_priv);
1711
Ben Widawsky80da2162013-12-06 14:11:17 -08001712 return;
Ben Widawskya2319c02014-03-18 16:09:37 -07001713 }
Ben Widawsky80da2162013-12-06 14:11:17 -08001714
Ben Widawsky678d96f2015-03-16 16:00:56 +00001715 if (USES_PPGTT(dev)) {
1716 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1717 /* TODO: Perhaps it shouldn't be gen6 specific */
Ben Widawsky80da2162013-12-06 14:11:17 -08001718
Ben Widawsky678d96f2015-03-16 16:00:56 +00001719 struct i915_hw_ppgtt *ppgtt =
1720 container_of(vm, struct i915_hw_ppgtt,
1721 base);
1722
1723 if (i915_is_ggtt(vm))
1724 ppgtt = dev_priv->mm.aliasing_ppgtt;
1725
1726 gen6_write_page_range(dev_priv, &ppgtt->pd,
1727 0, ppgtt->base.total);
1728 }
Daniel Vetter76aaf222010-11-05 22:23:30 +01001729 }
1730
Chris Wilson91e56492014-09-25 10:13:12 +01001731 i915_ggtt_flush(dev_priv);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001732}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001733
Daniel Vetter74163902012-02-15 23:50:21 +01001734int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001735{
Chris Wilson9da3da62012-06-01 15:20:22 +01001736 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001737 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001738
1739 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1740 obj->pages->sgl, obj->pages->nents,
1741 PCI_DMA_BIDIRECTIONAL))
1742 return -ENOSPC;
1743
1744 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001745}
1746
Michel Thierry07749ef2015-03-16 16:00:54 +00001747static inline void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001748{
1749#ifdef writeq
1750 writeq(pte, addr);
1751#else
1752 iowrite32((u32)pte, addr);
1753 iowrite32(pte >> 32, addr + 4);
1754#endif
1755}
1756
1757static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1758 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001759 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301760 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001761{
1762 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001763 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001764 gen8_pte_t __iomem *gtt_entries =
1765 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001766 int i = 0;
1767 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001768 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001769
1770 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1771 addr = sg_dma_address(sg_iter.sg) +
1772 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1773 gen8_set_pte(&gtt_entries[i],
1774 gen8_pte_encode(addr, level, true));
1775 i++;
1776 }
1777
1778 /*
1779 * XXX: This serves as a posting read to make sure that the PTE has
1780 * actually been updated. There is some concern that even though
1781 * registers and PTEs are within the same BAR that they are potentially
1782 * of NUMA access patterns. Therefore, even with the way we assume
1783 * hardware should work, we must keep this posting read for paranoia.
1784 */
1785 if (i != 0)
1786 WARN_ON(readq(&gtt_entries[i-1])
1787 != gen8_pte_encode(addr, level, true));
1788
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001789 /* This next bit makes the above posting read even more important. We
1790 * want to flush the TLBs only after we're certain all the PTE updates
1791 * have finished.
1792 */
1793 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1794 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001795}
1796
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001797/*
1798 * Binds an object into the global gtt with the specified cache level. The object
1799 * will be accessible to the GPU via commands whose operands reference offsets
1800 * within the global GTT as well as accessible by the GPU through the GMADR
1801 * mapped BAR (dev_priv->mm.gtt->gtt).
1802 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001803static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001804 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001805 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301806 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001807{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001808 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001809 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001810 gen6_pte_t __iomem *gtt_entries =
1811 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001812 int i = 0;
1813 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001814 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001815
Imre Deak6e995e22013-02-18 19:28:04 +02001816 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001817 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301818 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001819 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001820 }
1821
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001822 /* XXX: This serves as a posting read to make sure that the PTE has
1823 * actually been updated. There is some concern that even though
1824 * registers and PTEs are within the same BAR that they are potentially
1825 * of NUMA access patterns. Therefore, even with the way we assume
1826 * hardware should work, we must keep this posting read for paranoia.
1827 */
Pavel Machek57007df2014-07-28 13:20:58 +02001828 if (i != 0) {
1829 unsigned long gtt = readl(&gtt_entries[i-1]);
1830 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1831 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001832
1833 /* This next bit makes the above posting read even more important. We
1834 * want to flush the TLBs only after we're certain all the PTE updates
1835 * have finished.
1836 */
1837 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1838 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001839}
1840
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001841static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001842 uint64_t start,
1843 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001844 bool use_scratch)
1845{
1846 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001847 unsigned first_entry = start >> PAGE_SHIFT;
1848 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001849 gen8_pte_t scratch_pte, __iomem *gtt_base =
1850 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001851 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1852 int i;
1853
1854 if (WARN(num_entries > max_entries,
1855 "First entry = %d; Num entries = %d (max=%d)\n",
1856 first_entry, num_entries, max_entries))
1857 num_entries = max_entries;
1858
1859 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1860 I915_CACHE_LLC,
1861 use_scratch);
1862 for (i = 0; i < num_entries; i++)
1863 gen8_set_pte(&gtt_base[i], scratch_pte);
1864 readl(gtt_base);
1865}
1866
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001867static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001868 uint64_t start,
1869 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001870 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001871{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001872 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001873 unsigned first_entry = start >> PAGE_SHIFT;
1874 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001875 gen6_pte_t scratch_pte, __iomem *gtt_base =
1876 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001877 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001878 int i;
1879
1880 if (WARN(num_entries > max_entries,
1881 "First entry = %d; Num entries = %d (max=%d)\n",
1882 first_entry, num_entries, max_entries))
1883 num_entries = max_entries;
1884
Akash Goel24f3a8c2014-06-17 10:59:42 +05301885 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07001886
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001887 for (i = 0; i < num_entries; i++)
1888 iowrite32(scratch_pte, &gtt_base[i]);
1889 readl(gtt_base);
1890}
1891
Ben Widawsky6f65e292013-12-06 14:10:56 -08001892
1893static void i915_ggtt_bind_vma(struct i915_vma *vma,
1894 enum i915_cache_level cache_level,
1895 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001896{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001897 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001898 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1899 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1900
Ben Widawsky6f65e292013-12-06 14:10:56 -08001901 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001902 intel_gtt_insert_sg_entries(vma->ggtt_view.pages, entry, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001903 vma->bound = GLOBAL_BIND;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001904}
1905
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001906static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001907 uint64_t start,
1908 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001909 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001910{
Ben Widawsky782f1492014-02-20 11:50:33 -08001911 unsigned first_entry = start >> PAGE_SHIFT;
1912 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001913 intel_gtt_clear_range(first_entry, num_entries);
1914}
1915
Ben Widawsky6f65e292013-12-06 14:10:56 -08001916static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01001917{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001918 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1919 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001920
Ben Widawsky6f65e292013-12-06 14:10:56 -08001921 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001922 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001923 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01001924}
1925
Ben Widawsky6f65e292013-12-06 14:10:56 -08001926static void ggtt_bind_vma(struct i915_vma *vma,
1927 enum i915_cache_level cache_level,
1928 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001929{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001930 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001931 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001932 struct drm_i915_gem_object *obj = vma->obj;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001933 struct sg_table *pages = obj->pages;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001934
Akash Goel24f3a8c2014-06-17 10:59:42 +05301935 /* Currently applicable only to VLV */
1936 if (obj->gt_ro)
1937 flags |= PTE_READ_ONLY;
1938
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001939 if (i915_is_ggtt(vma->vm))
1940 pages = vma->ggtt_view.pages;
1941
Ben Widawsky6f65e292013-12-06 14:10:56 -08001942 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1943 * or we have a global mapping already but the cacheability flags have
1944 * changed, set the global PTEs.
1945 *
1946 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1947 * instead if none of the above hold true.
1948 *
1949 * NB: A global mapping should only be needed for special regions like
1950 * "gtt mappable", SNB errata, or if specified via special execbuf
1951 * flags. At all other times, the GPU will use the aliasing PPGTT.
1952 */
1953 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001954 if (!(vma->bound & GLOBAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08001955 (cache_level != obj->cache_level)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001956 vma->vm->insert_entries(vma->vm, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001957 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301958 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001959 vma->bound |= GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001960 }
1961 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001962
Ben Widawsky6f65e292013-12-06 14:10:56 -08001963 if (dev_priv->mm.aliasing_ppgtt &&
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001964 (!(vma->bound & LOCAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08001965 (cache_level != obj->cache_level))) {
1966 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001967 appgtt->base.insert_entries(&appgtt->base, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001968 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301969 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001970 vma->bound |= LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001971 }
1972}
1973
1974static void ggtt_unbind_vma(struct i915_vma *vma)
1975{
1976 struct drm_device *dev = vma->vm->dev;
1977 struct drm_i915_private *dev_priv = dev->dev_private;
1978 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001979
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001980 if (vma->bound & GLOBAL_BIND) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001981 vma->vm->clear_range(vma->vm,
1982 vma->node.start,
1983 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001984 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001985 vma->bound &= ~GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001986 }
1987
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001988 if (vma->bound & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001989 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1990 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001991 vma->node.start,
1992 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001993 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001994 vma->bound &= ~LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001995 }
Daniel Vetter74163902012-02-15 23:50:21 +01001996}
1997
1998void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1999{
Ben Widawsky5c042282011-10-17 15:51:55 -07002000 struct drm_device *dev = obj->base.dev;
2001 struct drm_i915_private *dev_priv = dev->dev_private;
2002 bool interruptible;
2003
2004 interruptible = do_idling(dev_priv);
2005
Chris Wilson9da3da62012-06-01 15:20:22 +01002006 if (!obj->has_dma_mapping)
2007 dma_unmap_sg(&dev->pdev->dev,
2008 obj->pages->sgl, obj->pages->nents,
2009 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07002010
2011 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002012}
Daniel Vetter644ec022012-03-26 09:45:40 +02002013
Chris Wilson42d6ab42012-07-26 11:49:32 +01002014static void i915_gtt_color_adjust(struct drm_mm_node *node,
2015 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002016 u64 *start,
2017 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002018{
2019 if (node->color != color)
2020 *start += 4096;
2021
2022 if (!list_empty(&node->node_list)) {
2023 node = list_entry(node->node_list.next,
2024 struct drm_mm_node,
2025 node_list);
2026 if (node->allocated && node->color != color)
2027 *end -= 4096;
2028 }
2029}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002030
Daniel Vetterf548c0e2014-11-19 21:40:13 +01002031static int i915_gem_setup_global_gtt(struct drm_device *dev,
2032 unsigned long start,
2033 unsigned long mappable_end,
2034 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02002035{
Ben Widawskye78891c2013-01-25 16:41:04 -08002036 /* Let GEM Manage all of the aperture.
2037 *
2038 * However, leave one page at the end still bound to the scratch page.
2039 * There are a number of places where the hardware apparently prefetches
2040 * past the end of the object, and we've seen multiple hangs with the
2041 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2042 * aperture. One page should be enough to keep any prefetching inside
2043 * of the aperture.
2044 */
Ben Widawsky40d749802013-07-31 16:59:59 -07002045 struct drm_i915_private *dev_priv = dev->dev_private;
2046 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002047 struct drm_mm_node *entry;
2048 struct drm_i915_gem_object *obj;
2049 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002050 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002051
Ben Widawsky35451cb2013-01-17 12:45:13 -08002052 BUG_ON(mappable_end > end);
2053
Chris Wilsoned2f3452012-11-15 11:32:19 +00002054 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07002055 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002056
2057 dev_priv->gtt.base.start = start;
2058 dev_priv->gtt.base.total = end - start;
2059
2060 if (intel_vgpu_active(dev)) {
2061 ret = intel_vgt_balloon(dev);
2062 if (ret)
2063 return ret;
2064 }
2065
Chris Wilson42d6ab42012-07-26 11:49:32 +01002066 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07002067 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02002068
Chris Wilsoned2f3452012-11-15 11:32:19 +00002069 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002070 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07002071 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002072
Ben Widawskyedd41a82013-07-05 14:41:05 -07002073 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002074 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002075
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002076 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07002077 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002078 if (ret) {
2079 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2080 return ret;
2081 }
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002082 vma->bound |= GLOBAL_BIND;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002083 }
2084
Chris Wilsoned2f3452012-11-15 11:32:19 +00002085 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07002086 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002087 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2088 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08002089 ggtt_vm->clear_range(ggtt_vm, hole_start,
2090 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002091 }
2092
2093 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08002094 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002095
Daniel Vetterfa76da32014-08-06 20:19:54 +02002096 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2097 struct i915_hw_ppgtt *ppgtt;
2098
2099 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2100 if (!ppgtt)
2101 return -ENOMEM;
2102
Michel Thierry4933d512015-03-24 15:46:22 +00002103 ret = __hw_ppgtt_init(dev, ppgtt, true);
2104 if (ret) {
2105 kfree(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002106 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002107 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002108
2109 dev_priv->mm.aliasing_ppgtt = ppgtt;
2110 }
2111
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002112 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002113}
2114
Ben Widawskyd7e50082012-12-18 10:31:25 -08002115void i915_gem_init_global_gtt(struct drm_device *dev)
2116{
2117 struct drm_i915_private *dev_priv = dev->dev_private;
2118 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002119
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002120 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08002121 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002122
Ben Widawskye78891c2013-01-25 16:41:04 -08002123 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002124}
2125
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002126void i915_global_gtt_cleanup(struct drm_device *dev)
2127{
2128 struct drm_i915_private *dev_priv = dev->dev_private;
2129 struct i915_address_space *vm = &dev_priv->gtt.base;
2130
Daniel Vetter70e32542014-08-06 15:04:57 +02002131 if (dev_priv->mm.aliasing_ppgtt) {
2132 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2133
2134 ppgtt->base.cleanup(&ppgtt->base);
2135 }
2136
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002137 if (drm_mm_initialized(&vm->mm)) {
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002138 if (intel_vgpu_active(dev))
2139 intel_vgt_deballoon();
2140
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002141 drm_mm_takedown(&vm->mm);
2142 list_del(&vm->global_link);
2143 }
2144
2145 vm->cleanup(vm);
2146}
Daniel Vetter70e32542014-08-06 15:04:57 +02002147
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002148static int setup_scratch_page(struct drm_device *dev)
2149{
2150 struct drm_i915_private *dev_priv = dev->dev_private;
2151 struct page *page;
2152 dma_addr_t dma_addr;
2153
2154 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
2155 if (page == NULL)
2156 return -ENOMEM;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002157 set_pages_uc(page, 1);
2158
2159#ifdef CONFIG_INTEL_IOMMU
2160 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
2161 PCI_DMA_BIDIRECTIONAL);
2162 if (pci_dma_mapping_error(dev->pdev, dma_addr))
2163 return -EINVAL;
2164#else
2165 dma_addr = page_to_phys(page);
2166#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002167 dev_priv->gtt.base.scratch.page = page;
2168 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002169
2170 return 0;
2171}
2172
2173static void teardown_scratch_page(struct drm_device *dev)
2174{
2175 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002176 struct page *page = dev_priv->gtt.base.scratch.page;
2177
2178 set_pages_wb(page, 1);
2179 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002180 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002181 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002182}
2183
2184static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2185{
2186 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2187 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2188 return snb_gmch_ctl << 20;
2189}
2190
Ben Widawsky9459d252013-11-03 16:53:55 -08002191static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2192{
2193 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2194 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2195 if (bdw_gmch_ctl)
2196 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002197
2198#ifdef CONFIG_X86_32
2199 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2200 if (bdw_gmch_ctl > 4)
2201 bdw_gmch_ctl = 4;
2202#endif
2203
Ben Widawsky9459d252013-11-03 16:53:55 -08002204 return bdw_gmch_ctl << 20;
2205}
2206
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002207static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2208{
2209 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2210 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2211
2212 if (gmch_ctrl)
2213 return 1 << (20 + gmch_ctrl);
2214
2215 return 0;
2216}
2217
Ben Widawskybaa09f52013-01-24 13:49:57 -08002218static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002219{
2220 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2221 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2222 return snb_gmch_ctl << 25; /* 32 MB units */
2223}
2224
Ben Widawsky9459d252013-11-03 16:53:55 -08002225static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2226{
2227 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2228 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2229 return bdw_gmch_ctl << 25; /* 32 MB units */
2230}
2231
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002232static size_t chv_get_stolen_size(u16 gmch_ctrl)
2233{
2234 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2235 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2236
2237 /*
2238 * 0x0 to 0x10: 32MB increments starting at 0MB
2239 * 0x11 to 0x16: 4MB increments starting at 8MB
2240 * 0x17 to 0x1d: 4MB increments start at 36MB
2241 */
2242 if (gmch_ctrl < 0x11)
2243 return gmch_ctrl << 25;
2244 else if (gmch_ctrl < 0x17)
2245 return (gmch_ctrl - 0x11 + 2) << 22;
2246 else
2247 return (gmch_ctrl - 0x17 + 9) << 22;
2248}
2249
Damien Lespiau66375012014-01-09 18:02:46 +00002250static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2251{
2252 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2253 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2254
2255 if (gen9_gmch_ctl < 0xf0)
2256 return gen9_gmch_ctl << 25; /* 32 MB units */
2257 else
2258 /* 4MB increments starting at 0xf0 for 4MB */
2259 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2260}
2261
Ben Widawsky63340132013-11-04 19:32:22 -08002262static int ggtt_probe_common(struct drm_device *dev,
2263 size_t gtt_size)
2264{
2265 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002266 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08002267 int ret;
2268
2269 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002270 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08002271 (pci_resource_len(dev->pdev, 0) / 2);
2272
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002273 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08002274 if (!dev_priv->gtt.gsm) {
2275 DRM_ERROR("Failed to map the gtt page table\n");
2276 return -ENOMEM;
2277 }
2278
2279 ret = setup_scratch_page(dev);
2280 if (ret) {
2281 DRM_ERROR("Scratch setup failed\n");
2282 /* iounmap will also get called at remove, but meh */
2283 iounmap(dev_priv->gtt.gsm);
2284 }
2285
2286 return ret;
2287}
2288
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002289/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2290 * bits. When using advanced contexts each context stores its own PAT, but
2291 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002292static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002293{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002294 uint64_t pat;
2295
2296 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2297 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2298 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2299 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2300 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2301 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2302 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2303 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2304
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002305 if (!USES_PPGTT(dev_priv->dev))
2306 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2307 * so RTL will always use the value corresponding to
2308 * pat_sel = 000".
2309 * So let's disable cache for GGTT to avoid screen corruptions.
2310 * MOCS still can be used though.
2311 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2312 * before this patch, i.e. the same uncached + snooping access
2313 * like on gen6/7 seems to be in effect.
2314 * - So this just fixes blitter/render access. Again it looks
2315 * like it's not just uncached access, but uncached + snooping.
2316 * So we can still hold onto all our assumptions wrt cpu
2317 * clflushing on LLC machines.
2318 */
2319 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2320
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002321 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2322 * write would work. */
2323 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2324 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2325}
2326
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002327static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2328{
2329 uint64_t pat;
2330
2331 /*
2332 * Map WB on BDW to snooped on CHV.
2333 *
2334 * Only the snoop bit has meaning for CHV, the rest is
2335 * ignored.
2336 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002337 * The hardware will never snoop for certain types of accesses:
2338 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2339 * - PPGTT page tables
2340 * - some other special cycles
2341 *
2342 * As with BDW, we also need to consider the following for GT accesses:
2343 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2344 * so RTL will always use the value corresponding to
2345 * pat_sel = 000".
2346 * Which means we must set the snoop bit in PAT entry 0
2347 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002348 */
2349 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2350 GEN8_PPAT(1, 0) |
2351 GEN8_PPAT(2, 0) |
2352 GEN8_PPAT(3, 0) |
2353 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2354 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2355 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2356 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2357
2358 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2359 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2360}
2361
Ben Widawsky63340132013-11-04 19:32:22 -08002362static int gen8_gmch_probe(struct drm_device *dev,
2363 size_t *gtt_total,
2364 size_t *stolen,
2365 phys_addr_t *mappable_base,
2366 unsigned long *mappable_end)
2367{
2368 struct drm_i915_private *dev_priv = dev->dev_private;
2369 unsigned int gtt_size;
2370 u16 snb_gmch_ctl;
2371 int ret;
2372
2373 /* TODO: We're not aware of mappable constraints on gen8 yet */
2374 *mappable_base = pci_resource_start(dev->pdev, 2);
2375 *mappable_end = pci_resource_len(dev->pdev, 2);
2376
2377 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
2378 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
2379
2380 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2381
Damien Lespiau66375012014-01-09 18:02:46 +00002382 if (INTEL_INFO(dev)->gen >= 9) {
2383 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
2384 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2385 } else if (IS_CHERRYVIEW(dev)) {
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002386 *stolen = chv_get_stolen_size(snb_gmch_ctl);
2387 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2388 } else {
2389 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
2390 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2391 }
Ben Widawsky63340132013-11-04 19:32:22 -08002392
Michel Thierry07749ef2015-03-16 16:00:54 +00002393 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002394
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002395 if (IS_CHERRYVIEW(dev))
2396 chv_setup_private_ppat(dev_priv);
2397 else
2398 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002399
Ben Widawsky63340132013-11-04 19:32:22 -08002400 ret = ggtt_probe_common(dev, gtt_size);
2401
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002402 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2403 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08002404
2405 return ret;
2406}
2407
Ben Widawskybaa09f52013-01-24 13:49:57 -08002408static int gen6_gmch_probe(struct drm_device *dev,
2409 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002410 size_t *stolen,
2411 phys_addr_t *mappable_base,
2412 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002413{
2414 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002415 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002416 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002417 int ret;
2418
Ben Widawsky41907dd2013-02-08 11:32:47 -08002419 *mappable_base = pci_resource_start(dev->pdev, 2);
2420 *mappable_end = pci_resource_len(dev->pdev, 2);
2421
Ben Widawskybaa09f52013-01-24 13:49:57 -08002422 /* 64/512MB is the current min/max we actually know of, but this is just
2423 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002424 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08002425 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08002426 DRM_ERROR("Unknown GMADR size (%lx)\n",
2427 dev_priv->gtt.mappable_end);
2428 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002429 }
2430
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002431 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2432 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08002433 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002434
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07002435 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002436
Ben Widawsky63340132013-11-04 19:32:22 -08002437 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Michel Thierry07749ef2015-03-16 16:00:54 +00002438 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002439
Ben Widawsky63340132013-11-04 19:32:22 -08002440 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002441
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002442 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2443 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002444
2445 return ret;
2446}
2447
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002448static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002449{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002450
2451 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002452
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002453 iounmap(gtt->gsm);
2454 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002455}
2456
2457static int i915_gmch_probe(struct drm_device *dev,
2458 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002459 size_t *stolen,
2460 phys_addr_t *mappable_base,
2461 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002462{
2463 struct drm_i915_private *dev_priv = dev->dev_private;
2464 int ret;
2465
Ben Widawskybaa09f52013-01-24 13:49:57 -08002466 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2467 if (!ret) {
2468 DRM_ERROR("failed to set up gmch\n");
2469 return -EIO;
2470 }
2471
Ben Widawsky41907dd2013-02-08 11:32:47 -08002472 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002473
2474 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002475 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002476
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002477 if (unlikely(dev_priv->gtt.do_idle_maps))
2478 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2479
Ben Widawskybaa09f52013-01-24 13:49:57 -08002480 return 0;
2481}
2482
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002483static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002484{
2485 intel_gmch_remove();
2486}
2487
2488int i915_gem_gtt_init(struct drm_device *dev)
2489{
2490 struct drm_i915_private *dev_priv = dev->dev_private;
2491 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002492 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002493
Ben Widawskybaa09f52013-01-24 13:49:57 -08002494 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002495 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002496 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002497 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002498 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002499 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002500 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002501 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002502 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002503 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002504 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002505 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002506 else if (INTEL_INFO(dev)->gen >= 7)
2507 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002508 else
Chris Wilson350ec882013-08-06 13:17:02 +01002509 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002510 } else {
2511 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2512 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002513 }
2514
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002515 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002516 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002517 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002518 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002519
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002520 gtt->base.dev = dev;
2521
Ben Widawskybaa09f52013-01-24 13:49:57 -08002522 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002523 DRM_INFO("Memory usable by graphics device = %zdM\n",
2524 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002525 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2526 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002527#ifdef CONFIG_INTEL_IOMMU
2528 if (intel_iommu_gfx_mapped)
2529 DRM_INFO("VT-d active for gfx access\n");
2530#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002531 /*
2532 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2533 * user's requested state against the hardware/driver capabilities. We
2534 * do this now so that we can print out any log messages once rather
2535 * than every time we check intel_enable_ppgtt().
2536 */
2537 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2538 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002539
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002540 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002541}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002542
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002543static struct i915_vma *
2544__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2545 struct i915_address_space *vm,
2546 const struct i915_ggtt_view *ggtt_view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002547{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002548 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002549
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002550 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2551 return ERR_PTR(-EINVAL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002552 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2553 if (vma == NULL)
2554 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002555
Ben Widawsky6f65e292013-12-06 14:10:56 -08002556 INIT_LIST_HEAD(&vma->vma_link);
2557 INIT_LIST_HEAD(&vma->mm_list);
2558 INIT_LIST_HEAD(&vma->exec_list);
2559 vma->vm = vm;
2560 vma->obj = obj;
2561
Rodrigo Vivib1252bc2014-12-03 04:55:29 -08002562 if (INTEL_INFO(vm->dev)->gen >= 6) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002563 if (i915_is_ggtt(vm)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002564 vma->ggtt_view = *ggtt_view;
2565
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002566 vma->unbind_vma = ggtt_unbind_vma;
2567 vma->bind_vma = ggtt_bind_vma;
2568 } else {
2569 vma->unbind_vma = ppgtt_unbind_vma;
2570 vma->bind_vma = ppgtt_bind_vma;
2571 }
Rodrigo Vivib1252bc2014-12-03 04:55:29 -08002572 } else {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002573 BUG_ON(!i915_is_ggtt(vm));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002574 vma->ggtt_view = *ggtt_view;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002575 vma->unbind_vma = i915_ggtt_unbind_vma;
2576 vma->bind_vma = i915_ggtt_bind_vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002577 }
2578
Tvrtko Ursulinf7635662014-12-03 14:59:24 +00002579 list_add_tail(&vma->vma_link, &obj->vma_list);
2580 if (!i915_is_ggtt(vm))
Michel Thierrye07f0552014-08-19 15:49:41 +01002581 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Ben Widawsky6f65e292013-12-06 14:10:56 -08002582
2583 return vma;
2584}
2585
2586struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002587i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2588 struct i915_address_space *vm)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002589{
2590 struct i915_vma *vma;
2591
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002592 vma = i915_gem_obj_to_vma(obj, vm);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002593 if (!vma)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002594 vma = __i915_gem_vma_create(obj, vm,
2595 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002596
2597 return vma;
2598}
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002599
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002600struct i915_vma *
2601i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
2602 const struct i915_ggtt_view *view)
2603{
2604 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
2605 struct i915_vma *vma;
2606
2607 if (WARN_ON(!view))
2608 return ERR_PTR(-EINVAL);
2609
2610 vma = i915_gem_obj_to_ggtt_view(obj, view);
2611
2612 if (IS_ERR(vma))
2613 return vma;
2614
2615 if (!vma)
2616 vma = __i915_gem_vma_create(obj, ggtt, view);
2617
2618 return vma;
2619
2620}
2621
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002622static void
2623rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
2624 struct sg_table *st)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002625{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002626 unsigned int column, row;
2627 unsigned int src_idx;
2628 struct scatterlist *sg = st->sgl;
2629
2630 st->nents = 0;
2631
2632 for (column = 0; column < width; column++) {
2633 src_idx = width * (height - 1) + column;
2634 for (row = 0; row < height; row++) {
2635 st->nents++;
2636 /* We don't need the pages, but need to initialize
2637 * the entries so the sg list can be happily traversed.
2638 * The only thing we need are DMA addresses.
2639 */
2640 sg_set_page(sg, NULL, PAGE_SIZE, 0);
2641 sg_dma_address(sg) = in[src_idx];
2642 sg_dma_len(sg) = PAGE_SIZE;
2643 sg = sg_next(sg);
2644 src_idx -= width;
2645 }
2646 }
2647}
2648
2649static struct sg_table *
2650intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
2651 struct drm_i915_gem_object *obj)
2652{
2653 struct drm_device *dev = obj->base.dev;
2654 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
2655 unsigned long size, pages, rot_pages;
2656 struct sg_page_iter sg_iter;
2657 unsigned long i;
2658 dma_addr_t *page_addr_list;
2659 struct sg_table *st;
2660 unsigned int tile_pitch, tile_height;
2661 unsigned int width_pages, height_pages;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00002662 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002663
2664 pages = obj->base.size / PAGE_SIZE;
2665
2666 /* Calculate tiling geometry. */
2667 tile_height = intel_tile_height(dev, rot_info->pixel_format,
2668 rot_info->fb_modifier);
2669 tile_pitch = PAGE_SIZE / tile_height;
2670 width_pages = DIV_ROUND_UP(rot_info->pitch, tile_pitch);
2671 height_pages = DIV_ROUND_UP(rot_info->height, tile_height);
2672 rot_pages = width_pages * height_pages;
2673 size = rot_pages * PAGE_SIZE;
2674
2675 /* Allocate a temporary list of source pages for random access. */
2676 page_addr_list = drm_malloc_ab(pages, sizeof(dma_addr_t));
2677 if (!page_addr_list)
2678 return ERR_PTR(ret);
2679
2680 /* Allocate target SG list. */
2681 st = kmalloc(sizeof(*st), GFP_KERNEL);
2682 if (!st)
2683 goto err_st_alloc;
2684
2685 ret = sg_alloc_table(st, rot_pages, GFP_KERNEL);
2686 if (ret)
2687 goto err_sg_alloc;
2688
2689 /* Populate source page list from the object. */
2690 i = 0;
2691 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
2692 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
2693 i++;
2694 }
2695
2696 /* Rotate the pages. */
2697 rotate_pages(page_addr_list, width_pages, height_pages, st);
2698
2699 DRM_DEBUG_KMS(
2700 "Created rotated page mapping for object size %lu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages).\n",
2701 size, rot_info->pitch, rot_info->height,
2702 rot_info->pixel_format, width_pages, height_pages,
2703 rot_pages);
2704
2705 drm_free_large(page_addr_list);
2706
2707 return st;
2708
2709err_sg_alloc:
2710 kfree(st);
2711err_st_alloc:
2712 drm_free_large(page_addr_list);
2713
2714 DRM_DEBUG_KMS(
2715 "Failed to create rotated mapping for object size %lu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages)\n",
2716 size, ret, rot_info->pitch, rot_info->height,
2717 rot_info->pixel_format, width_pages, height_pages,
2718 rot_pages);
2719 return ERR_PTR(ret);
2720}
2721
2722static inline int
2723i915_get_ggtt_vma_pages(struct i915_vma *vma)
2724{
2725 int ret = 0;
2726
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002727 if (vma->ggtt_view.pages)
2728 return 0;
2729
2730 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
2731 vma->ggtt_view.pages = vma->obj->pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002732 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
2733 vma->ggtt_view.pages =
2734 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002735 else
2736 WARN_ONCE(1, "GGTT view %u not implemented!\n",
2737 vma->ggtt_view.type);
2738
2739 if (!vma->ggtt_view.pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002740 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002741 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002742 ret = -EINVAL;
2743 } else if (IS_ERR(vma->ggtt_view.pages)) {
2744 ret = PTR_ERR(vma->ggtt_view.pages);
2745 vma->ggtt_view.pages = NULL;
2746 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
2747 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002748 }
2749
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002750 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002751}
2752
2753/**
2754 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
2755 * @vma: VMA to map
2756 * @cache_level: mapping cache level
2757 * @flags: flags like global or local mapping
2758 *
2759 * DMA addresses are taken from the scatter-gather table of this object (or of
2760 * this VMA in case of non-default GGTT views) and PTE entries set up.
2761 * Note that DMA addresses are also the only part of the SG table we care about.
2762 */
2763int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2764 u32 flags)
2765{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002766 if (i915_is_ggtt(vma->vm)) {
2767 int ret = i915_get_ggtt_vma_pages(vma);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002768
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002769 if (ret)
2770 return ret;
2771 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002772
2773 vma->bind_vma(vma, cache_level, flags);
2774
2775 return 0;
2776}