blob: f6b0ddc7a5fa3d84e431dc0e2112e5764d1588c5 [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);
Michel Thierrye5815a22015-04-08 12:13:32 +0100656 kunmap_atomic(page_directory);
657}
658
Michel Thierryec565b32015-04-08 12:13:23 +0100659static void gen8_free_page_tables(struct i915_page_directory *pd, struct drm_device *dev)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800660{
661 int i;
662
Ben Widawsky06fda602015-02-24 16:22:36 +0000663 if (!pd->page)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800664 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800665
Michel Thierry33c88192015-04-08 12:13:33 +0100666 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000667 if (WARN_ON(!pd->page_table[i]))
668 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800669
Michel Thierry06dc68d2015-02-24 16:22:37 +0000670 unmap_and_free_pt(pd->page_table[i], dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000671 pd->page_table[i] = NULL;
672 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000673}
674
675static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800676{
677 int i;
678
Michel Thierry33c88192015-04-08 12:13:33 +0100679 for_each_set_bit(i, ppgtt->pdp.used_pdpes, GEN8_LEGACY_PDPES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000680 if (WARN_ON(!ppgtt->pdp.page_directory[i]))
681 continue;
682
Michel Thierry06dc68d2015-02-24 16:22:37 +0000683 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Michel Thierrye5815a22015-04-08 12:13:32 +0100684 unmap_and_free_pd(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800685 }
Michel Thierry69876be2015-04-08 12:13:27 +0100686
Michel Thierrye5815a22015-04-08 12:13:32 +0100687 unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev);
Michel Thierry69876be2015-04-08 12:13:27 +0100688 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800689}
690
Ben Widawsky37aca442013-11-04 20:47:32 -0800691static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
692{
693 struct i915_hw_ppgtt *ppgtt =
694 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky37aca442013-11-04 20:47:32 -0800695
Ben Widawskyb45a6712014-02-12 14:28:44 -0800696 gen8_ppgtt_free(ppgtt);
Ben Widawsky37aca442013-11-04 20:47:32 -0800697}
698
Michel Thierryd7b26332015-04-08 12:13:34 +0100699/**
700 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
701 * @ppgtt: Master ppgtt structure.
702 * @pd: Page directory for this address range.
703 * @start: Starting virtual address to begin allocations.
704 * @length Size of the allocations.
705 * @new_pts: Bitmap set by function with new allocations. Likely used by the
706 * caller to free on error.
707 *
708 * Allocate the required number of page tables. Extremely similar to
709 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
710 * the page directory boundary (instead of the page directory pointer). That
711 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
712 * possible, and likely that the caller will need to use multiple calls of this
713 * function to achieve the appropriate allocation.
714 *
715 * Return: 0 if success; negative error code otherwise.
716 */
Michel Thierrye5815a22015-04-08 12:13:32 +0100717static int gen8_ppgtt_alloc_pagetabs(struct i915_hw_ppgtt *ppgtt,
718 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +0100719 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +0100720 uint64_t length,
721 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000722{
Michel Thierrye5815a22015-04-08 12:13:32 +0100723 struct drm_device *dev = ppgtt->base.dev;
Michel Thierryd7b26332015-04-08 12:13:34 +0100724 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100725 uint64_t temp;
726 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000727
Michel Thierryd7b26332015-04-08 12:13:34 +0100728 gen8_for_each_pde(pt, pd, start, length, temp, pde) {
729 /* Don't reallocate page tables */
730 if (pt) {
731 /* Scratch is never allocated this way */
732 WARN_ON(pt == ppgtt->scratch_pt);
733 continue;
734 }
735
736 pt = alloc_pt_single(dev);
737 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +0000738 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100739
Michel Thierryd7b26332015-04-08 12:13:34 +0100740 gen8_initialize_pt(&ppgtt->base, pt);
741 pd->page_table[pde] = pt;
742 set_bit(pde, new_pts);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000743 }
744
745 return 0;
746
747unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +0100748 for_each_set_bit(pde, new_pts, I915_PDES)
Michel Thierrye5815a22015-04-08 12:13:32 +0100749 unmap_and_free_pt(pd->page_table[pde], dev);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000750
751 return -ENOMEM;
752}
753
Michel Thierryd7b26332015-04-08 12:13:34 +0100754/**
755 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
756 * @ppgtt: Master ppgtt structure.
757 * @pdp: Page directory pointer for this address range.
758 * @start: Starting virtual address to begin allocations.
759 * @length Size of the allocations.
760 * @new_pds Bitmap set by function with new allocations. Likely used by the
761 * caller to free on error.
762 *
763 * Allocate the required number of page directories starting at the pde index of
764 * @start, and ending at the pde index @start + @length. This function will skip
765 * over already allocated page directories within the range, and only allocate
766 * new ones, setting the appropriate pointer within the pdp as well as the
767 * correct position in the bitmap @new_pds.
768 *
769 * The function will only allocate the pages within the range for a give page
770 * directory pointer. In other words, if @start + @length straddles a virtually
771 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
772 * required by the caller, This is not currently possible, and the BUG in the
773 * code will prevent it.
774 *
775 * Return: 0 if success; negative error code otherwise.
776 */
Michel Thierryc488dbb2015-04-08 12:13:31 +0100777static int gen8_ppgtt_alloc_page_directories(struct i915_hw_ppgtt *ppgtt,
778 struct i915_page_directory_pointer *pdp,
Michel Thierry69876be2015-04-08 12:13:27 +0100779 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +0100780 uint64_t length,
781 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800782{
Michel Thierrye5815a22015-04-08 12:13:32 +0100783 struct drm_device *dev = ppgtt->base.dev;
Michel Thierryd7b26332015-04-08 12:13:34 +0100784 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +0100785 uint64_t temp;
786 uint32_t pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800787
Michel Thierryd7b26332015-04-08 12:13:34 +0100788 WARN_ON(!bitmap_empty(new_pds, GEN8_LEGACY_PDPES));
789
Michel Thierry69876be2015-04-08 12:13:27 +0100790 /* FIXME: PPGTT container_of won't work for 64b */
791 WARN_ON((start + length) > 0x800000000ULL);
792
Michel Thierryd7b26332015-04-08 12:13:34 +0100793 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
794 if (pd)
795 continue;
Michel Thierry33c88192015-04-08 12:13:33 +0100796
Michel Thierryd7b26332015-04-08 12:13:34 +0100797 pd = alloc_pd_single(dev);
798 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000799 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +0100800
Michel Thierryd7b26332015-04-08 12:13:34 +0100801 gen8_initialize_pd(&ppgtt->base, pd);
802 pdp->page_directory[pdpe] = pd;
803 set_bit(pdpe, new_pds);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000804 }
805
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800806 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000807
808unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +0100809 for_each_set_bit(pdpe, new_pds, GEN8_LEGACY_PDPES)
Michel Thierrye5815a22015-04-08 12:13:32 +0100810 unmap_and_free_pd(pdp->page_directory[pdpe], dev);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000811
812 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800813}
814
Michel Thierryd7b26332015-04-08 12:13:34 +0100815static void
816free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long **new_pts)
817{
818 int i;
819
820 for (i = 0; i < GEN8_LEGACY_PDPES; i++)
821 kfree(new_pts[i]);
822 kfree(new_pts);
823 kfree(new_pds);
824}
825
826/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
827 * of these are based on the number of PDPEs in the system.
828 */
829static
830int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
831 unsigned long ***new_pts)
832{
833 int i;
834 unsigned long *pds;
835 unsigned long **pts;
836
837 pds = kcalloc(BITS_TO_LONGS(GEN8_LEGACY_PDPES), sizeof(unsigned long), GFP_KERNEL);
838 if (!pds)
839 return -ENOMEM;
840
841 pts = kcalloc(GEN8_LEGACY_PDPES, sizeof(unsigned long *), GFP_KERNEL);
842 if (!pts) {
843 kfree(pds);
844 return -ENOMEM;
845 }
846
847 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
848 pts[i] = kcalloc(BITS_TO_LONGS(I915_PDES),
849 sizeof(unsigned long), GFP_KERNEL);
850 if (!pts[i])
851 goto err_out;
852 }
853
854 *new_pds = pds;
855 *new_pts = pts;
856
857 return 0;
858
859err_out:
860 free_gen8_temp_bitmaps(pds, pts);
861 return -ENOMEM;
862}
863
Michel Thierrye5815a22015-04-08 12:13:32 +0100864static int gen8_alloc_va_range(struct i915_address_space *vm,
865 uint64_t start,
866 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800867{
Michel Thierrye5815a22015-04-08 12:13:32 +0100868 struct i915_hw_ppgtt *ppgtt =
869 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryd7b26332015-04-08 12:13:34 +0100870 unsigned long *new_page_dirs, **new_page_tables;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100871 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100872 const uint64_t orig_start = start;
873 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100874 uint64_t temp;
875 uint32_t pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800876 int ret;
877
Michel Thierryd7b26332015-04-08 12:13:34 +0100878#ifndef CONFIG_64BIT
879 /* Disallow 64b address on 32b platforms. Nothing is wrong with doing
880 * this in hardware, but a lot of the drm code is not prepared to handle
881 * 64b offset on 32b platforms.
882 * This will be addressed when 48b PPGTT is added */
883 if (start + length > 0x100000000ULL)
884 return -E2BIG;
885#endif
886
887 /* Wrap is never okay since we can only represent 48b, and we don't
888 * actually use the other side of the canonical address space.
889 */
890 if (WARN_ON(start + length < start))
891 return -ERANGE;
892
893 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800894 if (ret)
895 return ret;
896
Michel Thierryd7b26332015-04-08 12:13:34 +0100897 /* Do the allocations first so we can easily bail out */
898 ret = gen8_ppgtt_alloc_page_directories(ppgtt, &ppgtt->pdp, start, length,
899 new_page_dirs);
900 if (ret) {
901 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
902 return ret;
903 }
904
905 /* For every page directory referenced, allocate page tables */
Michel Thierry5441f0c2015-04-08 12:13:28 +0100906 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
Michel Thierryd7b26332015-04-08 12:13:34 +0100907 ret = gen8_ppgtt_alloc_pagetabs(ppgtt, pd, start, length,
908 new_page_tables[pdpe]);
Michel Thierry5441f0c2015-04-08 12:13:28 +0100909 if (ret)
910 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100911 }
912
Michel Thierry33c88192015-04-08 12:13:33 +0100913 start = orig_start;
914 length = orig_length;
915
Michel Thierryd7b26332015-04-08 12:13:34 +0100916 /* Allocations have completed successfully, so set the bitmaps, and do
917 * the mappings. */
Michel Thierry33c88192015-04-08 12:13:33 +0100918 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
Michel Thierryd7b26332015-04-08 12:13:34 +0100919 gen8_pde_t *const page_directory = kmap_atomic(pd->page);
Michel Thierry33c88192015-04-08 12:13:33 +0100920 struct i915_page_table *pt;
921 uint64_t pd_len = gen8_clamp_pd(start, length);
922 uint64_t pd_start = start;
923 uint32_t pde;
924
Michel Thierryd7b26332015-04-08 12:13:34 +0100925 /* Every pd should be allocated, we just did that above. */
926 WARN_ON(!pd);
927
928 gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
929 /* Same reasoning as pd */
930 WARN_ON(!pt);
931 WARN_ON(!pd_len);
932 WARN_ON(!gen8_pte_count(pd_start, pd_len));
933
934 /* Set our used ptes within the page table */
935 bitmap_set(pt->used_ptes,
936 gen8_pte_index(pd_start),
937 gen8_pte_count(pd_start, pd_len));
938
939 /* Our pde is now pointing to the pagetable, pt */
Michel Thierry33c88192015-04-08 12:13:33 +0100940 set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +0100941
942 /* Map the PDE to the page table */
943 __gen8_do_map_pt(page_directory + pde, pt, vm->dev);
944
945 /* NB: We haven't yet mapped ptes to pages. At this
946 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +0100947 }
Michel Thierryd7b26332015-04-08 12:13:34 +0100948
949 if (!HAS_LLC(vm->dev))
950 drm_clflush_virt_range(page_directory, PAGE_SIZE);
951
952 kunmap_atomic(page_directory);
953
Michel Thierry33c88192015-04-08 12:13:33 +0100954 set_bit(pdpe, ppgtt->pdp.used_pdpes);
955 }
956
Michel Thierryd7b26332015-04-08 12:13:34 +0100957 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000958 return 0;
959
960err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +0100961 while (pdpe--) {
962 for_each_set_bit(temp, new_page_tables[pdpe], I915_PDES)
963 unmap_and_free_pt(ppgtt->pdp.page_directory[pdpe]->page_table[temp], vm->dev);
964 }
965
966 for_each_set_bit(pdpe, new_page_dirs, GEN8_LEGACY_PDPES)
967 unmap_and_free_pd(ppgtt->pdp.page_directory[pdpe], vm->dev);
968
969 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800970 return ret;
971}
972
Daniel Vettereb0b44a2015-03-18 14:47:59 +0100973/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800974 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
975 * with a net effect resembling a 2-level page table in normal x86 terms. Each
976 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
977 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800978 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800979 */
Michel Thierryd7b26332015-04-08 12:13:34 +0100980static int gen8_ppgtt_init_common(struct i915_hw_ppgtt *ppgtt, uint64_t size)
Ben Widawsky37aca442013-11-04 20:47:32 -0800981{
Michel Thierry69876be2015-04-08 12:13:27 +0100982 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
983 if (IS_ERR(ppgtt->scratch_pt))
984 return PTR_ERR(ppgtt->scratch_pt);
985
Michel Thierrye5815a22015-04-08 12:13:32 +0100986 ppgtt->scratch_pd = alloc_pd_single(ppgtt->base.dev);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100987 if (IS_ERR(ppgtt->scratch_pd))
988 return PTR_ERR(ppgtt->scratch_pd);
989
Michel Thierry69876be2015-04-08 12:13:27 +0100990 gen8_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100991 gen8_initialize_pd(&ppgtt->base, ppgtt->scratch_pd);
Michel Thierry69876be2015-04-08 12:13:27 +0100992
Michel Thierryd7b26332015-04-08 12:13:34 +0100993 ppgtt->base.start = 0;
994 ppgtt->base.total = size;
995 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
996 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
997
998 ppgtt->switch_mm = gen8_mm_switch;
999
1000 return 0;
1001}
1002
1003static int gen8_aliasing_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1004{
1005 struct drm_device *dev = ppgtt->base.dev;
1006 struct drm_i915_private *dev_priv = dev->dev_private;
1007 uint64_t start = 0, size = dev_priv->gtt.base.total;
1008 int ret;
1009
1010 ret = gen8_ppgtt_init_common(ppgtt, dev_priv->gtt.base.total);
1011 if (ret)
1012 return ret;
1013
1014 /* Aliasing PPGTT has to always work and be mapped because of the way we
1015 * use RESTORE_INHIBIT in the context switch. This will be fixed
1016 * eventually. */
Michel Thierrye5815a22015-04-08 12:13:32 +01001017 ret = gen8_alloc_va_range(&ppgtt->base, start, size);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +01001018 if (ret) {
Michel Thierrye5815a22015-04-08 12:13:32 +01001019 unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +01001020 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001021 return ret;
Michel Thierry7cb6d7a2015-04-08 12:13:29 +01001022 }
Ben Widawsky37aca442013-11-04 20:47:32 -08001023
Michel Thierryd7b26332015-04-08 12:13:34 +01001024 ppgtt->base.allocate_va_range = NULL;
1025 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Michel Thierry09942c62015-04-08 12:13:30 +01001026 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
Michel Thierryd7b26332015-04-08 12:13:34 +01001027
1028 return 0;
1029}
1030
1031static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1032{
1033 struct drm_device *dev = ppgtt->base.dev;
1034 struct drm_i915_private *dev_priv = dev->dev_private;
1035 int ret;
1036
1037 ret = gen8_ppgtt_init_common(ppgtt, dev_priv->gtt.base.total);
1038 if (ret)
1039 return ret;
1040
1041 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
1042 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
1043
Ben Widawsky28cf5412013-11-02 21:07:26 -07001044 return 0;
Ben Widawsky37aca442013-11-04 20:47:32 -08001045}
1046
Ben Widawsky87d60b62013-12-06 14:11:29 -08001047static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1048{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001049 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001050 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001051 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001052 uint32_t pd_entry;
Michel Thierry09942c62015-04-08 12:13:30 +01001053 uint32_t pte, pde, temp;
1054 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001055
Akash Goel24f3a8c2014-06-17 10:59:42 +05301056 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001057
Michel Thierry09942c62015-04-08 12:13:30 +01001058 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001059 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001060 gen6_pte_t *pt_vaddr;
Ben Widawsky06fda602015-02-24 16:22:36 +00001061 dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
Michel Thierry09942c62015-04-08 12:13:30 +01001062 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001063 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1064
1065 if (pd_entry != expected)
1066 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1067 pde,
1068 pd_entry,
1069 expected);
1070 seq_printf(m, "\tPDE: %x\n", pd_entry);
1071
Ben Widawsky06fda602015-02-24 16:22:36 +00001072 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
Michel Thierry07749ef2015-03-16 16:00:54 +00001073 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001074 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001075 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001076 (pte * PAGE_SIZE);
1077 int i;
1078 bool found = false;
1079 for (i = 0; i < 4; i++)
1080 if (pt_vaddr[pte + i] != scratch_pte)
1081 found = true;
1082 if (!found)
1083 continue;
1084
1085 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1086 for (i = 0; i < 4; i++) {
1087 if (pt_vaddr[pte + i] != scratch_pte)
1088 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1089 else
1090 seq_puts(m, " SCRATCH ");
1091 }
1092 seq_puts(m, "\n");
1093 }
1094 kunmap_atomic(pt_vaddr);
1095 }
1096}
1097
Ben Widawsky678d96f2015-03-16 16:00:56 +00001098/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001099static void gen6_write_pde(struct i915_page_directory *pd,
1100 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001101{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001102 /* Caller needs to make sure the write completes if necessary */
1103 struct i915_hw_ppgtt *ppgtt =
1104 container_of(pd, struct i915_hw_ppgtt, pd);
1105 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001106
Ben Widawsky678d96f2015-03-16 16:00:56 +00001107 pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr);
1108 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001109
Ben Widawsky678d96f2015-03-16 16:00:56 +00001110 writel(pd_entry, ppgtt->pd_addr + pde);
1111}
Ben Widawsky61973492013-04-08 18:43:54 -07001112
Ben Widawsky678d96f2015-03-16 16:00:56 +00001113/* Write all the page tables found in the ppgtt structure to incrementing page
1114 * directories. */
1115static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001116 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001117 uint32_t start, uint32_t length)
1118{
Michel Thierryec565b32015-04-08 12:13:23 +01001119 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001120 uint32_t pde, temp;
1121
1122 gen6_for_each_pde(pt, pd, start, length, temp, pde)
1123 gen6_write_pde(pd, pde, pt);
1124
1125 /* Make sure write is complete before other code can use this page
1126 * table. Also require for WC mapped PTEs */
1127 readl(dev_priv->gtt.gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001128}
1129
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001130static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001131{
Ben Widawsky7324cc02015-02-24 16:22:35 +00001132 BUG_ON(ppgtt->pd.pd_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001133
Ben Widawsky7324cc02015-02-24 16:22:35 +00001134 return (ppgtt->pd.pd_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001135}
Ben Widawsky61973492013-04-08 18:43:54 -07001136
Ben Widawsky90252e52013-12-06 14:11:12 -08001137static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001138 struct intel_engine_cs *ring)
Ben Widawsky90252e52013-12-06 14:11:12 -08001139{
Ben Widawsky90252e52013-12-06 14:11:12 -08001140 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001141
Ben Widawsky90252e52013-12-06 14:11:12 -08001142 /* NB: TLBs must be flushed and invalidated before a switch */
1143 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1144 if (ret)
1145 return ret;
1146
1147 ret = intel_ring_begin(ring, 6);
1148 if (ret)
1149 return ret;
1150
1151 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1152 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1153 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1154 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1155 intel_ring_emit(ring, get_pd_offset(ppgtt));
1156 intel_ring_emit(ring, MI_NOOP);
1157 intel_ring_advance(ring);
1158
1159 return 0;
1160}
1161
Yu Zhang71ba2d62015-02-10 19:05:54 +08001162static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
1163 struct intel_engine_cs *ring)
1164{
1165 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1166
1167 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1168 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1169 return 0;
1170}
1171
Ben Widawsky48a10382013-12-06 14:11:11 -08001172static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001173 struct intel_engine_cs *ring)
Ben Widawsky48a10382013-12-06 14:11:11 -08001174{
Ben Widawsky48a10382013-12-06 14:11:11 -08001175 int ret;
1176
Ben Widawsky48a10382013-12-06 14:11:11 -08001177 /* NB: TLBs must be flushed and invalidated before a switch */
1178 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1179 if (ret)
1180 return ret;
1181
1182 ret = intel_ring_begin(ring, 6);
1183 if (ret)
1184 return ret;
1185
1186 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1187 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1188 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1189 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1190 intel_ring_emit(ring, get_pd_offset(ppgtt));
1191 intel_ring_emit(ring, MI_NOOP);
1192 intel_ring_advance(ring);
1193
Ben Widawsky90252e52013-12-06 14:11:12 -08001194 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1195 if (ring->id != RCS) {
1196 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1197 if (ret)
1198 return ret;
1199 }
1200
Ben Widawsky48a10382013-12-06 14:11:11 -08001201 return 0;
1202}
1203
Ben Widawskyeeb94882013-12-06 14:11:10 -08001204static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001205 struct intel_engine_cs *ring)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001206{
1207 struct drm_device *dev = ppgtt->base.dev;
1208 struct drm_i915_private *dev_priv = dev->dev_private;
1209
Ben Widawsky48a10382013-12-06 14:11:11 -08001210
Ben Widawskyeeb94882013-12-06 14:11:10 -08001211 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1212 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1213
1214 POSTING_READ(RING_PP_DIR_DCLV(ring));
1215
1216 return 0;
1217}
1218
Daniel Vetter82460d92014-08-06 20:19:53 +02001219static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001220{
Ben Widawskyeeb94882013-12-06 14:11:10 -08001221 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001222 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +02001223 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001224
1225 for_each_ring(ring, dev_priv, j) {
1226 I915_WRITE(RING_MODE_GEN7(ring),
1227 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001228 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001229}
1230
Daniel Vetter82460d92014-08-06 20:19:53 +02001231static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001232{
Jani Nikula50227e12014-03-31 14:27:21 +03001233 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001234 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001235 uint32_t ecochk, ecobits;
1236 int i;
1237
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001238 ecobits = I915_READ(GAC_ECO_BITS);
1239 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1240
1241 ecochk = I915_READ(GAM_ECOCHK);
1242 if (IS_HASWELL(dev)) {
1243 ecochk |= ECOCHK_PPGTT_WB_HSW;
1244 } else {
1245 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1246 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1247 }
1248 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001249
Ben Widawsky61973492013-04-08 18:43:54 -07001250 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001251 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001252 I915_WRITE(RING_MODE_GEN7(ring),
1253 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001254 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001255}
1256
Daniel Vetter82460d92014-08-06 20:19:53 +02001257static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001258{
Jani Nikula50227e12014-03-31 14:27:21 +03001259 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001260 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001261
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001262 ecobits = I915_READ(GAC_ECO_BITS);
1263 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1264 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001265
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001266 gab_ctl = I915_READ(GAB_CTL);
1267 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001268
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001269 ecochk = I915_READ(GAM_ECOCHK);
1270 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001271
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001272 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001273}
1274
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001275/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001276static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001277 uint64_t start,
1278 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001279 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001280{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001281 struct i915_hw_ppgtt *ppgtt =
1282 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001283 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001284 unsigned first_entry = start >> PAGE_SHIFT;
1285 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001286 unsigned act_pt = first_entry / GEN6_PTES;
1287 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001288 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001289
Akash Goel24f3a8c2014-06-17 10:59:42 +05301290 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001291
Daniel Vetter7bddb012012-02-09 17:15:47 +01001292 while (num_entries) {
1293 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001294 if (last_pte > GEN6_PTES)
1295 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001296
Ben Widawsky06fda602015-02-24 16:22:36 +00001297 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001298
1299 for (i = first_pte; i < last_pte; i++)
1300 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001301
1302 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001303
Daniel Vetter7bddb012012-02-09 17:15:47 +01001304 num_entries -= last_pte - first_pte;
1305 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001306 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001307 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001308}
1309
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001310static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001311 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001312 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301313 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001314{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001315 struct i915_hw_ppgtt *ppgtt =
1316 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001317 gen6_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -08001318 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001319 unsigned act_pt = first_entry / GEN6_PTES;
1320 unsigned act_pte = first_entry % GEN6_PTES;
Imre Deak6e995e22013-02-18 19:28:04 +02001321 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001322
Chris Wilsoncc797142013-12-31 15:50:30 +00001323 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +02001324 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001325 if (pt_vaddr == NULL)
Ben Widawsky06fda602015-02-24 16:22:36 +00001326 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001327
Chris Wilsoncc797142013-12-31 15:50:30 +00001328 pt_vaddr[act_pte] =
1329 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +05301330 cache_level, true, flags);
1331
Michel Thierry07749ef2015-03-16 16:00:54 +00001332 if (++act_pte == GEN6_PTES) {
Imre Deak6e995e22013-02-18 19:28:04 +02001333 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001334 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001335 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001336 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001337 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001338 }
Chris Wilsoncc797142013-12-31 15:50:30 +00001339 if (pt_vaddr)
1340 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001341}
1342
Ben Widawsky563222a2015-03-19 12:53:28 +00001343/* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we
1344 * are switching between contexts with the same LRCA, we also must do a force
1345 * restore.
1346 */
1347static inline void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1348{
1349 /* If current vm != vm, */
1350 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1351}
1352
Michel Thierry4933d512015-03-24 15:46:22 +00001353static void gen6_initialize_pt(struct i915_address_space *vm,
Michel Thierryec565b32015-04-08 12:13:23 +01001354 struct i915_page_table *pt)
Michel Thierry4933d512015-03-24 15:46:22 +00001355{
1356 gen6_pte_t *pt_vaddr, scratch_pte;
1357 int i;
1358
1359 WARN_ON(vm->scratch.addr == 0);
1360
1361 scratch_pte = vm->pte_encode(vm->scratch.addr,
1362 I915_CACHE_LLC, true, 0);
1363
1364 pt_vaddr = kmap_atomic(pt->page);
1365
1366 for (i = 0; i < GEN6_PTES; i++)
1367 pt_vaddr[i] = scratch_pte;
1368
1369 kunmap_atomic(pt_vaddr);
1370}
1371
Ben Widawsky678d96f2015-03-16 16:00:56 +00001372static int gen6_alloc_va_range(struct i915_address_space *vm,
1373 uint64_t start, uint64_t length)
1374{
Michel Thierry4933d512015-03-24 15:46:22 +00001375 DECLARE_BITMAP(new_page_tables, I915_PDES);
1376 struct drm_device *dev = vm->dev;
1377 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001378 struct i915_hw_ppgtt *ppgtt =
1379 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryec565b32015-04-08 12:13:23 +01001380 struct i915_page_table *pt;
Michel Thierry4933d512015-03-24 15:46:22 +00001381 const uint32_t start_save = start, length_save = length;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001382 uint32_t pde, temp;
Michel Thierry4933d512015-03-24 15:46:22 +00001383 int ret;
1384
1385 WARN_ON(upper_32_bits(start));
1386
1387 bitmap_zero(new_page_tables, I915_PDES);
1388
1389 /* The allocation is done in two stages so that we can bail out with
1390 * minimal amount of pain. The first stage finds new page tables that
1391 * need allocation. The second stage marks use ptes within the page
1392 * tables.
1393 */
1394 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1395 if (pt != ppgtt->scratch_pt) {
1396 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1397 continue;
1398 }
1399
1400 /* We've already allocated a page table */
1401 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1402
1403 pt = alloc_pt_single(dev);
1404 if (IS_ERR(pt)) {
1405 ret = PTR_ERR(pt);
1406 goto unwind_out;
1407 }
1408
1409 gen6_initialize_pt(vm, pt);
1410
1411 ppgtt->pd.page_table[pde] = pt;
1412 set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001413 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001414 }
1415
1416 start = start_save;
1417 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001418
1419 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1420 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1421
1422 bitmap_zero(tmp_bitmap, GEN6_PTES);
1423 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1424 gen6_pte_count(start, length));
1425
Michel Thierry4933d512015-03-24 15:46:22 +00001426 if (test_and_clear_bit(pde, new_page_tables))
1427 gen6_write_pde(&ppgtt->pd, pde, pt);
1428
Michel Thierry72744cb2015-03-24 15:46:23 +00001429 trace_i915_page_table_entry_map(vm, pde, pt,
1430 gen6_pte_index(start),
1431 gen6_pte_count(start, length),
1432 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001433 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001434 GEN6_PTES);
1435 }
1436
Michel Thierry4933d512015-03-24 15:46:22 +00001437 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1438
1439 /* Make sure write is complete before other code can use this page
1440 * table. Also require for WC mapped PTEs */
1441 readl(dev_priv->gtt.gsm);
1442
Ben Widawsky563222a2015-03-19 12:53:28 +00001443 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001444 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001445
1446unwind_out:
1447 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001448 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001449
1450 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1451 unmap_and_free_pt(pt, vm->dev);
1452 }
1453
1454 mark_tlbs_dirty(ppgtt);
1455 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001456}
1457
Ben Widawskya00d8252014-02-19 22:05:48 -08001458static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
1459{
Michel Thierry09942c62015-04-08 12:13:30 +01001460 struct i915_page_table *pt;
1461 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001462
Michel Thierry09942c62015-04-08 12:13:30 +01001463 gen6_for_all_pdes(pt, ppgtt, pde) {
Michel Thierry4933d512015-03-24 15:46:22 +00001464 if (pt != ppgtt->scratch_pt)
Michel Thierry09942c62015-04-08 12:13:30 +01001465 unmap_and_free_pt(pt, ppgtt->base.dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001466 }
1467
1468 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Michel Thierrye5815a22015-04-08 12:13:32 +01001469 unmap_and_free_pd(&ppgtt->pd, ppgtt->base.dev);
Daniel Vetter3440d262013-01-24 13:49:56 -08001470}
1471
Ben Widawskya00d8252014-02-19 22:05:48 -08001472static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1473{
1474 struct i915_hw_ppgtt *ppgtt =
1475 container_of(vm, struct i915_hw_ppgtt, base);
1476
Ben Widawskya00d8252014-02-19 22:05:48 -08001477 drm_mm_remove_node(&ppgtt->node);
1478
Ben Widawskya00d8252014-02-19 22:05:48 -08001479 gen6_ppgtt_free(ppgtt);
1480}
1481
Ben Widawskyb1465202014-02-19 22:05:49 -08001482static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001483{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001484 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001485 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001486 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001487 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001488
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001489 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1490 * allocator works in address space sizes, so it's multiplied by page
1491 * size. We allocate at the top of the GTT to avoid fragmentation.
1492 */
1493 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001494 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
1495 if (IS_ERR(ppgtt->scratch_pt))
1496 return PTR_ERR(ppgtt->scratch_pt);
1497
1498 gen6_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
1499
Ben Widawskye3cc1992013-12-06 14:11:08 -08001500alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001501 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1502 &ppgtt->node, GEN6_PD_SIZE,
1503 GEN6_PD_ALIGN, 0,
1504 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07001505 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001506 if (ret == -ENOSPC && !retried) {
1507 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1508 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02001509 I915_CACHE_NONE,
1510 0, dev_priv->gtt.base.total,
1511 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001512 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001513 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001514
1515 retried = true;
1516 goto alloc;
1517 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001518
Ben Widawskyc8c26622015-01-22 17:01:25 +00001519 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001520 goto err_out;
1521
Ben Widawskyc8c26622015-01-22 17:01:25 +00001522
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001523 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1524 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001525
Ben Widawskyc8c26622015-01-22 17:01:25 +00001526 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001527
1528err_out:
Michel Thierry4933d512015-03-24 15:46:22 +00001529 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001530 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001531}
1532
Ben Widawskyb1465202014-02-19 22:05:49 -08001533static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1534{
kbuild test robot2f2cf682015-03-27 19:26:35 +08001535 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08001536}
1537
Michel Thierry4933d512015-03-24 15:46:22 +00001538static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1539 uint64_t start, uint64_t length)
1540{
Michel Thierryec565b32015-04-08 12:13:23 +01001541 struct i915_page_table *unused;
Michel Thierry4933d512015-03-24 15:46:22 +00001542 uint32_t pde, temp;
1543
1544 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
1545 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1546}
1547
1548static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt, bool aliasing)
Ben Widawskyb1465202014-02-19 22:05:49 -08001549{
1550 struct drm_device *dev = ppgtt->base.dev;
1551 struct drm_i915_private *dev_priv = dev->dev_private;
1552 int ret;
1553
1554 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001555 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001556 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001557 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001558 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001559 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001560 ppgtt->switch_mm = gen7_mm_switch;
1561 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001562 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001563
Yu Zhang71ba2d62015-02-10 19:05:54 +08001564 if (intel_vgpu_active(dev))
1565 ppgtt->switch_mm = vgpu_mm_switch;
1566
Ben Widawskyb1465202014-02-19 22:05:49 -08001567 ret = gen6_ppgtt_alloc(ppgtt);
1568 if (ret)
1569 return ret;
1570
Michel Thierry4933d512015-03-24 15:46:22 +00001571 if (aliasing) {
1572 /* preallocate all pts */
Michel Thierry09942c62015-04-08 12:13:30 +01001573 ret = alloc_pt_range(&ppgtt->pd, 0, I915_PDES,
Michel Thierry4933d512015-03-24 15:46:22 +00001574 ppgtt->base.dev);
1575
1576 if (ret) {
1577 gen6_ppgtt_cleanup(&ppgtt->base);
1578 return ret;
1579 }
1580 }
1581
Michel Thierryd7b26332015-04-08 12:13:34 +01001582 ppgtt->base.allocate_va_range = aliasing ? NULL : gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001583 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1584 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1585 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001586 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01001587 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001588 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001589
Ben Widawsky7324cc02015-02-24 16:22:35 +00001590 ppgtt->pd.pd_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00001591 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001592
Ben Widawsky678d96f2015-03-16 16:00:56 +00001593 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
1594 ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
1595
Michel Thierry4933d512015-03-24 15:46:22 +00001596 if (aliasing)
1597 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
1598 else
1599 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001600
Ben Widawsky678d96f2015-03-16 16:00:56 +00001601 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
1602
Thierry Reding440fd522015-01-23 09:05:06 +01001603 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001604 ppgtt->node.size >> 20,
1605 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001606
Daniel Vetterfa76da32014-08-06 20:19:54 +02001607 DRM_DEBUG("Adding PPGTT at offset %x\n",
Ben Widawsky7324cc02015-02-24 16:22:35 +00001608 ppgtt->pd.pd_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001609
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001610 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001611}
1612
Michel Thierry4933d512015-03-24 15:46:22 +00001613static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt,
1614 bool aliasing)
Daniel Vetter3440d262013-01-24 13:49:56 -08001615{
1616 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter3440d262013-01-24 13:49:56 -08001617
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001618 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001619 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001620
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001621 if (INTEL_INFO(dev)->gen < 8)
Michel Thierry4933d512015-03-24 15:46:22 +00001622 return gen6_ppgtt_init(ppgtt, aliasing);
Michel Thierryd7b26332015-04-08 12:13:34 +01001623 else if (aliasing)
1624 return gen8_aliasing_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001625 else
Michel Thierryd7b26332015-04-08 12:13:34 +01001626 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001627}
1628int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1629{
1630 struct drm_i915_private *dev_priv = dev->dev_private;
1631 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001632
Michel Thierry4933d512015-03-24 15:46:22 +00001633 ret = __hw_ppgtt_init(dev, ppgtt, false);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001634 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001635 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001636 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1637 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001638 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001639 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001640
1641 return ret;
1642}
1643
Daniel Vetter82460d92014-08-06 20:19:53 +02001644int i915_ppgtt_init_hw(struct drm_device *dev)
1645{
1646 struct drm_i915_private *dev_priv = dev->dev_private;
1647 struct intel_engine_cs *ring;
1648 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1649 int i, ret = 0;
1650
Thomas Daniel671b50132014-08-20 16:24:50 +01001651 /* In the case of execlists, PPGTT is enabled by the context descriptor
1652 * and the PDPs are contained within the context itself. We don't
1653 * need to do anything here. */
1654 if (i915.enable_execlists)
1655 return 0;
1656
Daniel Vetter82460d92014-08-06 20:19:53 +02001657 if (!USES_PPGTT(dev))
1658 return 0;
1659
1660 if (IS_GEN6(dev))
1661 gen6_ppgtt_enable(dev);
1662 else if (IS_GEN7(dev))
1663 gen7_ppgtt_enable(dev);
1664 else if (INTEL_INFO(dev)->gen >= 8)
1665 gen8_ppgtt_enable(dev);
1666 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01001667 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02001668
1669 if (ppgtt) {
1670 for_each_ring(ring, dev_priv, i) {
McAulay, Alistair6689c162014-08-15 18:51:35 +01001671 ret = ppgtt->switch_mm(ppgtt, ring);
Daniel Vetter82460d92014-08-06 20:19:53 +02001672 if (ret != 0)
1673 return ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001674 }
1675 }
1676
1677 return ret;
1678}
Daniel Vetter4d884702014-08-06 15:04:47 +02001679struct i915_hw_ppgtt *
1680i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1681{
1682 struct i915_hw_ppgtt *ppgtt;
1683 int ret;
1684
1685 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1686 if (!ppgtt)
1687 return ERR_PTR(-ENOMEM);
1688
1689 ret = i915_ppgtt_init(dev, ppgtt);
1690 if (ret) {
1691 kfree(ppgtt);
1692 return ERR_PTR(ret);
1693 }
1694
1695 ppgtt->file_priv = fpriv;
1696
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001697 trace_i915_ppgtt_create(&ppgtt->base);
1698
Daniel Vetter4d884702014-08-06 15:04:47 +02001699 return ppgtt;
1700}
1701
Daniel Vetteree960be2014-08-06 15:04:45 +02001702void i915_ppgtt_release(struct kref *kref)
1703{
1704 struct i915_hw_ppgtt *ppgtt =
1705 container_of(kref, struct i915_hw_ppgtt, ref);
1706
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001707 trace_i915_ppgtt_release(&ppgtt->base);
1708
Daniel Vetteree960be2014-08-06 15:04:45 +02001709 /* vmas should already be unbound */
1710 WARN_ON(!list_empty(&ppgtt->base.active_list));
1711 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1712
Daniel Vetter19dd1202014-08-06 15:04:55 +02001713 list_del(&ppgtt->base.global_link);
1714 drm_mm_takedown(&ppgtt->base.mm);
1715
Daniel Vetteree960be2014-08-06 15:04:45 +02001716 ppgtt->base.cleanup(&ppgtt->base);
1717 kfree(ppgtt);
1718}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001719
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001720static void
Ben Widawsky6f65e292013-12-06 14:10:56 -08001721ppgtt_bind_vma(struct i915_vma *vma,
1722 enum i915_cache_level cache_level,
1723 u32 flags)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001724{
Akash Goel24f3a8c2014-06-17 10:59:42 +05301725 /* Currently applicable only to VLV */
1726 if (vma->obj->gt_ro)
1727 flags |= PTE_READ_ONLY;
1728
Ben Widawsky782f1492014-02-20 11:50:33 -08001729 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301730 cache_level, flags);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001731}
1732
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001733static void ppgtt_unbind_vma(struct i915_vma *vma)
Daniel Vetter7bddb012012-02-09 17:15:47 +01001734{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001735 vma->vm->clear_range(vma->vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001736 vma->node.start,
1737 vma->obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001738 true);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001739}
1740
Ben Widawskya81cc002013-01-18 12:30:31 -08001741extern int intel_iommu_gfx_mapped;
1742/* Certain Gen5 chipsets require require idling the GPU before
1743 * unmapping anything from the GTT when VT-d is enabled.
1744 */
1745static inline bool needs_idle_maps(struct drm_device *dev)
1746{
1747#ifdef CONFIG_INTEL_IOMMU
1748 /* Query intel_iommu to see if we need the workaround. Presumably that
1749 * was loaded first.
1750 */
1751 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1752 return true;
1753#endif
1754 return false;
1755}
1756
Ben Widawsky5c042282011-10-17 15:51:55 -07001757static bool do_idling(struct drm_i915_private *dev_priv)
1758{
1759 bool ret = dev_priv->mm.interruptible;
1760
Ben Widawskya81cc002013-01-18 12:30:31 -08001761 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001762 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001763 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001764 DRM_ERROR("Couldn't idle GPU\n");
1765 /* Wait a bit, in hopes it avoids the hang */
1766 udelay(10);
1767 }
1768 }
1769
1770 return ret;
1771}
1772
1773static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1774{
Ben Widawskya81cc002013-01-18 12:30:31 -08001775 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001776 dev_priv->mm.interruptible = interruptible;
1777}
1778
Ben Widawsky828c7902013-10-16 09:21:30 -07001779void i915_check_and_clear_faults(struct drm_device *dev)
1780{
1781 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001782 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07001783 int i;
1784
1785 if (INTEL_INFO(dev)->gen < 6)
1786 return;
1787
1788 for_each_ring(ring, dev_priv, i) {
1789 u32 fault_reg;
1790 fault_reg = I915_READ(RING_FAULT_REG(ring));
1791 if (fault_reg & RING_FAULT_VALID) {
1792 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02001793 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07001794 "\tAddress space: %s\n"
1795 "\tSource ID: %d\n"
1796 "\tType: %d\n",
1797 fault_reg & PAGE_MASK,
1798 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1799 RING_FAULT_SRCID(fault_reg),
1800 RING_FAULT_FAULT_TYPE(fault_reg));
1801 I915_WRITE(RING_FAULT_REG(ring),
1802 fault_reg & ~RING_FAULT_VALID);
1803 }
1804 }
1805 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1806}
1807
Chris Wilson91e56492014-09-25 10:13:12 +01001808static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
1809{
1810 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
1811 intel_gtt_chipset_flush();
1812 } else {
1813 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1814 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1815 }
1816}
1817
Ben Widawsky828c7902013-10-16 09:21:30 -07001818void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1819{
1820 struct drm_i915_private *dev_priv = dev->dev_private;
1821
1822 /* Don't bother messing with faults pre GEN6 as we have little
1823 * documentation supporting that it's a good idea.
1824 */
1825 if (INTEL_INFO(dev)->gen < 6)
1826 return;
1827
1828 i915_check_and_clear_faults(dev);
1829
1830 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001831 dev_priv->gtt.base.start,
1832 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001833 true);
Chris Wilson91e56492014-09-25 10:13:12 +01001834
1835 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07001836}
1837
Daniel Vetter76aaf222010-11-05 22:23:30 +01001838void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1839{
1840 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001841 struct drm_i915_gem_object *obj;
Ben Widawsky80da2162013-12-06 14:11:17 -08001842 struct i915_address_space *vm;
Daniel Vetter76aaf222010-11-05 22:23:30 +01001843
Ben Widawsky828c7902013-10-16 09:21:30 -07001844 i915_check_and_clear_faults(dev);
1845
Chris Wilsonbee4a182011-01-21 10:54:32 +00001846 /* First fill our portion of the GTT with scratch pages */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001847 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001848 dev_priv->gtt.base.start,
1849 dev_priv->gtt.base.total,
Ben Widawsky828c7902013-10-16 09:21:30 -07001850 true);
Chris Wilsonbee4a182011-01-21 10:54:32 +00001851
Ben Widawsky35c20a62013-05-31 11:28:48 -07001852 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001853 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1854 &dev_priv->gtt.base);
1855 if (!vma)
1856 continue;
1857
Chris Wilson2c225692013-08-09 12:26:45 +01001858 i915_gem_clflush_object(obj, obj->pin_display);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001859 /* The bind_vma code tries to be smart about tracking mappings.
1860 * Unfortunately above, we've just wiped out the mappings
1861 * without telling our object about it. So we need to fake it.
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001862 *
1863 * Bind is not expected to fail since this is only called on
1864 * resume and assumption is all requirements exist already.
Ben Widawsky6f65e292013-12-06 14:10:56 -08001865 */
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001866 vma->bound &= ~GLOBAL_BIND;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00001867 WARN_ON(i915_vma_bind(vma, obj->cache_level, GLOBAL_BIND));
Daniel Vetter76aaf222010-11-05 22:23:30 +01001868 }
1869
Ben Widawsky80da2162013-12-06 14:11:17 -08001870
Ben Widawskya2319c02014-03-18 16:09:37 -07001871 if (INTEL_INFO(dev)->gen >= 8) {
Ville Syrjäläee0ce472014-04-09 13:28:01 +03001872 if (IS_CHERRYVIEW(dev))
1873 chv_setup_private_ppat(dev_priv);
1874 else
1875 bdw_setup_private_ppat(dev_priv);
1876
Ben Widawsky80da2162013-12-06 14:11:17 -08001877 return;
Ben Widawskya2319c02014-03-18 16:09:37 -07001878 }
Ben Widawsky80da2162013-12-06 14:11:17 -08001879
Ben Widawsky678d96f2015-03-16 16:00:56 +00001880 if (USES_PPGTT(dev)) {
1881 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1882 /* TODO: Perhaps it shouldn't be gen6 specific */
Ben Widawsky80da2162013-12-06 14:11:17 -08001883
Ben Widawsky678d96f2015-03-16 16:00:56 +00001884 struct i915_hw_ppgtt *ppgtt =
1885 container_of(vm, struct i915_hw_ppgtt,
1886 base);
1887
1888 if (i915_is_ggtt(vm))
1889 ppgtt = dev_priv->mm.aliasing_ppgtt;
1890
1891 gen6_write_page_range(dev_priv, &ppgtt->pd,
1892 0, ppgtt->base.total);
1893 }
Daniel Vetter76aaf222010-11-05 22:23:30 +01001894 }
1895
Chris Wilson91e56492014-09-25 10:13:12 +01001896 i915_ggtt_flush(dev_priv);
Daniel Vetter76aaf222010-11-05 22:23:30 +01001897}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001898
Daniel Vetter74163902012-02-15 23:50:21 +01001899int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001900{
Chris Wilson9da3da62012-06-01 15:20:22 +01001901 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001902 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001903
1904 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1905 obj->pages->sgl, obj->pages->nents,
1906 PCI_DMA_BIDIRECTIONAL))
1907 return -ENOSPC;
1908
1909 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001910}
1911
Michel Thierry07749ef2015-03-16 16:00:54 +00001912static inline void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001913{
1914#ifdef writeq
1915 writeq(pte, addr);
1916#else
1917 iowrite32((u32)pte, addr);
1918 iowrite32(pte >> 32, addr + 4);
1919#endif
1920}
1921
1922static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1923 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001924 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301925 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001926{
1927 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001928 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001929 gen8_pte_t __iomem *gtt_entries =
1930 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001931 int i = 0;
1932 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001933 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001934
1935 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1936 addr = sg_dma_address(sg_iter.sg) +
1937 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1938 gen8_set_pte(&gtt_entries[i],
1939 gen8_pte_encode(addr, level, true));
1940 i++;
1941 }
1942
1943 /*
1944 * XXX: This serves as a posting read to make sure that the PTE has
1945 * actually been updated. There is some concern that even though
1946 * registers and PTEs are within the same BAR that they are potentially
1947 * of NUMA access patterns. Therefore, even with the way we assume
1948 * hardware should work, we must keep this posting read for paranoia.
1949 */
1950 if (i != 0)
1951 WARN_ON(readq(&gtt_entries[i-1])
1952 != gen8_pte_encode(addr, level, true));
1953
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001954 /* This next bit makes the above posting read even more important. We
1955 * want to flush the TLBs only after we're certain all the PTE updates
1956 * have finished.
1957 */
1958 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1959 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001960}
1961
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001962/*
1963 * Binds an object into the global gtt with the specified cache level. The object
1964 * will be accessible to the GPU via commands whose operands reference offsets
1965 * within the global GTT as well as accessible by the GPU through the GMADR
1966 * mapped BAR (dev_priv->mm.gtt->gtt).
1967 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001968static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001969 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001970 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301971 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001972{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001973 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001974 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001975 gen6_pte_t __iomem *gtt_entries =
1976 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001977 int i = 0;
1978 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001979 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001980
Imre Deak6e995e22013-02-18 19:28:04 +02001981 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001982 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301983 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001984 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001985 }
1986
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001987 /* XXX: This serves as a posting read to make sure that the PTE has
1988 * actually been updated. There is some concern that even though
1989 * registers and PTEs are within the same BAR that they are potentially
1990 * of NUMA access patterns. Therefore, even with the way we assume
1991 * hardware should work, we must keep this posting read for paranoia.
1992 */
Pavel Machek57007df2014-07-28 13:20:58 +02001993 if (i != 0) {
1994 unsigned long gtt = readl(&gtt_entries[i-1]);
1995 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1996 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001997
1998 /* This next bit makes the above posting read even more important. We
1999 * want to flush the TLBs only after we're certain all the PTE updates
2000 * have finished.
2001 */
2002 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2003 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002004}
2005
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002006static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002007 uint64_t start,
2008 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002009 bool use_scratch)
2010{
2011 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08002012 unsigned first_entry = start >> PAGE_SHIFT;
2013 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002014 gen8_pte_t scratch_pte, __iomem *gtt_base =
2015 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002016 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
2017 int i;
2018
2019 if (WARN(num_entries > max_entries,
2020 "First entry = %d; Num entries = %d (max=%d)\n",
2021 first_entry, num_entries, max_entries))
2022 num_entries = max_entries;
2023
2024 scratch_pte = gen8_pte_encode(vm->scratch.addr,
2025 I915_CACHE_LLC,
2026 use_scratch);
2027 for (i = 0; i < num_entries; i++)
2028 gen8_set_pte(&gtt_base[i], scratch_pte);
2029 readl(gtt_base);
2030}
2031
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002032static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002033 uint64_t start,
2034 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07002035 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002036{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002037 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08002038 unsigned first_entry = start >> PAGE_SHIFT;
2039 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002040 gen6_pte_t scratch_pte, __iomem *gtt_base =
2041 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08002042 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002043 int i;
2044
2045 if (WARN(num_entries > max_entries,
2046 "First entry = %d; Num entries = %d (max=%d)\n",
2047 first_entry, num_entries, max_entries))
2048 num_entries = max_entries;
2049
Akash Goel24f3a8c2014-06-17 10:59:42 +05302050 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002051
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002052 for (i = 0; i < num_entries; i++)
2053 iowrite32(scratch_pte, &gtt_base[i]);
2054 readl(gtt_base);
2055}
2056
Ben Widawsky6f65e292013-12-06 14:10:56 -08002057
2058static void i915_ggtt_bind_vma(struct i915_vma *vma,
2059 enum i915_cache_level cache_level,
2060 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002061{
Ben Widawsky6f65e292013-12-06 14:10:56 -08002062 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002063 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2064 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2065
Ben Widawsky6f65e292013-12-06 14:10:56 -08002066 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002067 intel_gtt_insert_sg_entries(vma->ggtt_view.pages, entry, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002068 vma->bound = GLOBAL_BIND;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002069}
2070
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002071static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002072 uint64_t start,
2073 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07002074 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002075{
Ben Widawsky782f1492014-02-20 11:50:33 -08002076 unsigned first_entry = start >> PAGE_SHIFT;
2077 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002078 intel_gtt_clear_range(first_entry, num_entries);
2079}
2080
Ben Widawsky6f65e292013-12-06 14:10:56 -08002081static void i915_ggtt_unbind_vma(struct i915_vma *vma)
Chris Wilsond5bd1442011-04-14 06:48:26 +01002082{
Ben Widawsky6f65e292013-12-06 14:10:56 -08002083 const unsigned int first = vma->node.start >> PAGE_SHIFT;
2084 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002085
Ben Widawsky6f65e292013-12-06 14:10:56 -08002086 BUG_ON(!i915_is_ggtt(vma->vm));
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002087 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002088 intel_gtt_clear_range(first, size);
Chris Wilsond5bd1442011-04-14 06:48:26 +01002089}
2090
Ben Widawsky6f65e292013-12-06 14:10:56 -08002091static void ggtt_bind_vma(struct i915_vma *vma,
2092 enum i915_cache_level cache_level,
2093 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002094{
Ben Widawsky6f65e292013-12-06 14:10:56 -08002095 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002096 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002097 struct drm_i915_gem_object *obj = vma->obj;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002098 struct sg_table *pages = obj->pages;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002099
Akash Goel24f3a8c2014-06-17 10:59:42 +05302100 /* Currently applicable only to VLV */
2101 if (obj->gt_ro)
2102 flags |= PTE_READ_ONLY;
2103
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002104 if (i915_is_ggtt(vma->vm))
2105 pages = vma->ggtt_view.pages;
2106
Ben Widawsky6f65e292013-12-06 14:10:56 -08002107 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
2108 * or we have a global mapping already but the cacheability flags have
2109 * changed, set the global PTEs.
2110 *
2111 * If there is an aliasing PPGTT it is anecdotally faster, so use that
2112 * instead if none of the above hold true.
2113 *
2114 * NB: A global mapping should only be needed for special regions like
2115 * "gtt mappable", SNB errata, or if specified via special execbuf
2116 * flags. At all other times, the GPU will use the aliasing PPGTT.
2117 */
2118 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002119 if (!(vma->bound & GLOBAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08002120 (cache_level != obj->cache_level)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002121 vma->vm->insert_entries(vma->vm, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08002122 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302123 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002124 vma->bound |= GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002125 }
2126 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002127
Ben Widawsky6f65e292013-12-06 14:10:56 -08002128 if (dev_priv->mm.aliasing_ppgtt &&
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002129 (!(vma->bound & LOCAL_BIND) ||
Ben Widawsky6f65e292013-12-06 14:10:56 -08002130 (cache_level != obj->cache_level))) {
2131 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002132 appgtt->base.insert_entries(&appgtt->base, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08002133 vma->node.start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302134 cache_level, flags);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002135 vma->bound |= LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002136 }
2137}
2138
2139static void ggtt_unbind_vma(struct i915_vma *vma)
2140{
2141 struct drm_device *dev = vma->vm->dev;
2142 struct drm_i915_private *dev_priv = dev->dev_private;
2143 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002144
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002145 if (vma->bound & GLOBAL_BIND) {
Ben Widawsky782f1492014-02-20 11:50:33 -08002146 vma->vm->clear_range(vma->vm,
2147 vma->node.start,
2148 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08002149 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002150 vma->bound &= ~GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002151 }
2152
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002153 if (vma->bound & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002154 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2155 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08002156 vma->node.start,
2157 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08002158 true);
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002159 vma->bound &= ~LOCAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002160 }
Daniel Vetter74163902012-02-15 23:50:21 +01002161}
2162
2163void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2164{
Ben Widawsky5c042282011-10-17 15:51:55 -07002165 struct drm_device *dev = obj->base.dev;
2166 struct drm_i915_private *dev_priv = dev->dev_private;
2167 bool interruptible;
2168
2169 interruptible = do_idling(dev_priv);
2170
Chris Wilson9da3da62012-06-01 15:20:22 +01002171 if (!obj->has_dma_mapping)
2172 dma_unmap_sg(&dev->pdev->dev,
2173 obj->pages->sgl, obj->pages->nents,
2174 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07002175
2176 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002177}
Daniel Vetter644ec022012-03-26 09:45:40 +02002178
Chris Wilson42d6ab42012-07-26 11:49:32 +01002179static void i915_gtt_color_adjust(struct drm_mm_node *node,
2180 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002181 u64 *start,
2182 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002183{
2184 if (node->color != color)
2185 *start += 4096;
2186
2187 if (!list_empty(&node->node_list)) {
2188 node = list_entry(node->node_list.next,
2189 struct drm_mm_node,
2190 node_list);
2191 if (node->allocated && node->color != color)
2192 *end -= 4096;
2193 }
2194}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002195
Daniel Vetterf548c0e2014-11-19 21:40:13 +01002196static int i915_gem_setup_global_gtt(struct drm_device *dev,
2197 unsigned long start,
2198 unsigned long mappable_end,
2199 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02002200{
Ben Widawskye78891c2013-01-25 16:41:04 -08002201 /* Let GEM Manage all of the aperture.
2202 *
2203 * However, leave one page at the end still bound to the scratch page.
2204 * There are a number of places where the hardware apparently prefetches
2205 * past the end of the object, and we've seen multiple hangs with the
2206 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2207 * aperture. One page should be enough to keep any prefetching inside
2208 * of the aperture.
2209 */
Ben Widawsky40d749802013-07-31 16:59:59 -07002210 struct drm_i915_private *dev_priv = dev->dev_private;
2211 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002212 struct drm_mm_node *entry;
2213 struct drm_i915_gem_object *obj;
2214 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002215 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002216
Ben Widawsky35451cb2013-01-17 12:45:13 -08002217 BUG_ON(mappable_end > end);
2218
Chris Wilsoned2f3452012-11-15 11:32:19 +00002219 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07002220 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002221
2222 dev_priv->gtt.base.start = start;
2223 dev_priv->gtt.base.total = end - start;
2224
2225 if (intel_vgpu_active(dev)) {
2226 ret = intel_vgt_balloon(dev);
2227 if (ret)
2228 return ret;
2229 }
2230
Chris Wilson42d6ab42012-07-26 11:49:32 +01002231 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07002232 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02002233
Chris Wilsoned2f3452012-11-15 11:32:19 +00002234 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002235 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07002236 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002237
Ben Widawskyedd41a82013-07-05 14:41:05 -07002238 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002239 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002240
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002241 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07002242 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002243 if (ret) {
2244 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2245 return ret;
2246 }
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002247 vma->bound |= GLOBAL_BIND;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002248 }
2249
Chris Wilsoned2f3452012-11-15 11:32:19 +00002250 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07002251 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002252 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2253 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08002254 ggtt_vm->clear_range(ggtt_vm, hole_start,
2255 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002256 }
2257
2258 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08002259 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002260
Daniel Vetterfa76da32014-08-06 20:19:54 +02002261 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2262 struct i915_hw_ppgtt *ppgtt;
2263
2264 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2265 if (!ppgtt)
2266 return -ENOMEM;
2267
Michel Thierry4933d512015-03-24 15:46:22 +00002268 ret = __hw_ppgtt_init(dev, ppgtt, true);
2269 if (ret) {
2270 kfree(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002271 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002272 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002273
2274 dev_priv->mm.aliasing_ppgtt = ppgtt;
2275 }
2276
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002277 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002278}
2279
Ben Widawskyd7e50082012-12-18 10:31:25 -08002280void i915_gem_init_global_gtt(struct drm_device *dev)
2281{
2282 struct drm_i915_private *dev_priv = dev->dev_private;
2283 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002284
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002285 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08002286 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002287
Ben Widawskye78891c2013-01-25 16:41:04 -08002288 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002289}
2290
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002291void i915_global_gtt_cleanup(struct drm_device *dev)
2292{
2293 struct drm_i915_private *dev_priv = dev->dev_private;
2294 struct i915_address_space *vm = &dev_priv->gtt.base;
2295
Daniel Vetter70e32542014-08-06 15:04:57 +02002296 if (dev_priv->mm.aliasing_ppgtt) {
2297 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2298
2299 ppgtt->base.cleanup(&ppgtt->base);
2300 }
2301
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002302 if (drm_mm_initialized(&vm->mm)) {
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002303 if (intel_vgpu_active(dev))
2304 intel_vgt_deballoon();
2305
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002306 drm_mm_takedown(&vm->mm);
2307 list_del(&vm->global_link);
2308 }
2309
2310 vm->cleanup(vm);
2311}
Daniel Vetter70e32542014-08-06 15:04:57 +02002312
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002313static int setup_scratch_page(struct drm_device *dev)
2314{
2315 struct drm_i915_private *dev_priv = dev->dev_private;
2316 struct page *page;
2317 dma_addr_t dma_addr;
2318
2319 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
2320 if (page == NULL)
2321 return -ENOMEM;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002322 set_pages_uc(page, 1);
2323
2324#ifdef CONFIG_INTEL_IOMMU
2325 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
2326 PCI_DMA_BIDIRECTIONAL);
2327 if (pci_dma_mapping_error(dev->pdev, dma_addr))
2328 return -EINVAL;
2329#else
2330 dma_addr = page_to_phys(page);
2331#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002332 dev_priv->gtt.base.scratch.page = page;
2333 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002334
2335 return 0;
2336}
2337
2338static void teardown_scratch_page(struct drm_device *dev)
2339{
2340 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002341 struct page *page = dev_priv->gtt.base.scratch.page;
2342
2343 set_pages_wb(page, 1);
2344 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002345 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002346 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002347}
2348
2349static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2350{
2351 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2352 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2353 return snb_gmch_ctl << 20;
2354}
2355
Ben Widawsky9459d252013-11-03 16:53:55 -08002356static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2357{
2358 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2359 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2360 if (bdw_gmch_ctl)
2361 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002362
2363#ifdef CONFIG_X86_32
2364 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2365 if (bdw_gmch_ctl > 4)
2366 bdw_gmch_ctl = 4;
2367#endif
2368
Ben Widawsky9459d252013-11-03 16:53:55 -08002369 return bdw_gmch_ctl << 20;
2370}
2371
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002372static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2373{
2374 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2375 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2376
2377 if (gmch_ctrl)
2378 return 1 << (20 + gmch_ctrl);
2379
2380 return 0;
2381}
2382
Ben Widawskybaa09f52013-01-24 13:49:57 -08002383static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002384{
2385 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2386 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2387 return snb_gmch_ctl << 25; /* 32 MB units */
2388}
2389
Ben Widawsky9459d252013-11-03 16:53:55 -08002390static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2391{
2392 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2393 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2394 return bdw_gmch_ctl << 25; /* 32 MB units */
2395}
2396
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002397static size_t chv_get_stolen_size(u16 gmch_ctrl)
2398{
2399 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2400 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2401
2402 /*
2403 * 0x0 to 0x10: 32MB increments starting at 0MB
2404 * 0x11 to 0x16: 4MB increments starting at 8MB
2405 * 0x17 to 0x1d: 4MB increments start at 36MB
2406 */
2407 if (gmch_ctrl < 0x11)
2408 return gmch_ctrl << 25;
2409 else if (gmch_ctrl < 0x17)
2410 return (gmch_ctrl - 0x11 + 2) << 22;
2411 else
2412 return (gmch_ctrl - 0x17 + 9) << 22;
2413}
2414
Damien Lespiau66375012014-01-09 18:02:46 +00002415static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2416{
2417 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2418 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2419
2420 if (gen9_gmch_ctl < 0xf0)
2421 return gen9_gmch_ctl << 25; /* 32 MB units */
2422 else
2423 /* 4MB increments starting at 0xf0 for 4MB */
2424 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2425}
2426
Ben Widawsky63340132013-11-04 19:32:22 -08002427static int ggtt_probe_common(struct drm_device *dev,
2428 size_t gtt_size)
2429{
2430 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002431 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08002432 int ret;
2433
2434 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002435 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08002436 (pci_resource_len(dev->pdev, 0) / 2);
2437
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002438 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08002439 if (!dev_priv->gtt.gsm) {
2440 DRM_ERROR("Failed to map the gtt page table\n");
2441 return -ENOMEM;
2442 }
2443
2444 ret = setup_scratch_page(dev);
2445 if (ret) {
2446 DRM_ERROR("Scratch setup failed\n");
2447 /* iounmap will also get called at remove, but meh */
2448 iounmap(dev_priv->gtt.gsm);
2449 }
2450
2451 return ret;
2452}
2453
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002454/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2455 * bits. When using advanced contexts each context stores its own PAT, but
2456 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002457static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002458{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002459 uint64_t pat;
2460
2461 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2462 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2463 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2464 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2465 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2466 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2467 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2468 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2469
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002470 if (!USES_PPGTT(dev_priv->dev))
2471 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2472 * so RTL will always use the value corresponding to
2473 * pat_sel = 000".
2474 * So let's disable cache for GGTT to avoid screen corruptions.
2475 * MOCS still can be used though.
2476 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2477 * before this patch, i.e. the same uncached + snooping access
2478 * like on gen6/7 seems to be in effect.
2479 * - So this just fixes blitter/render access. Again it looks
2480 * like it's not just uncached access, but uncached + snooping.
2481 * So we can still hold onto all our assumptions wrt cpu
2482 * clflushing on LLC machines.
2483 */
2484 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2485
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002486 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2487 * write would work. */
2488 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2489 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2490}
2491
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002492static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2493{
2494 uint64_t pat;
2495
2496 /*
2497 * Map WB on BDW to snooped on CHV.
2498 *
2499 * Only the snoop bit has meaning for CHV, the rest is
2500 * ignored.
2501 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002502 * The hardware will never snoop for certain types of accesses:
2503 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2504 * - PPGTT page tables
2505 * - some other special cycles
2506 *
2507 * As with BDW, we also need to consider the following for GT accesses:
2508 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2509 * so RTL will always use the value corresponding to
2510 * pat_sel = 000".
2511 * Which means we must set the snoop bit in PAT entry 0
2512 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002513 */
2514 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2515 GEN8_PPAT(1, 0) |
2516 GEN8_PPAT(2, 0) |
2517 GEN8_PPAT(3, 0) |
2518 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2519 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2520 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2521 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2522
2523 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2524 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2525}
2526
Ben Widawsky63340132013-11-04 19:32:22 -08002527static int gen8_gmch_probe(struct drm_device *dev,
2528 size_t *gtt_total,
2529 size_t *stolen,
2530 phys_addr_t *mappable_base,
2531 unsigned long *mappable_end)
2532{
2533 struct drm_i915_private *dev_priv = dev->dev_private;
2534 unsigned int gtt_size;
2535 u16 snb_gmch_ctl;
2536 int ret;
2537
2538 /* TODO: We're not aware of mappable constraints on gen8 yet */
2539 *mappable_base = pci_resource_start(dev->pdev, 2);
2540 *mappable_end = pci_resource_len(dev->pdev, 2);
2541
2542 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
2543 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
2544
2545 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2546
Damien Lespiau66375012014-01-09 18:02:46 +00002547 if (INTEL_INFO(dev)->gen >= 9) {
2548 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
2549 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2550 } else if (IS_CHERRYVIEW(dev)) {
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002551 *stolen = chv_get_stolen_size(snb_gmch_ctl);
2552 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2553 } else {
2554 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
2555 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2556 }
Ben Widawsky63340132013-11-04 19:32:22 -08002557
Michel Thierry07749ef2015-03-16 16:00:54 +00002558 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002559
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002560 if (IS_CHERRYVIEW(dev))
2561 chv_setup_private_ppat(dev_priv);
2562 else
2563 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002564
Ben Widawsky63340132013-11-04 19:32:22 -08002565 ret = ggtt_probe_common(dev, gtt_size);
2566
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002567 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2568 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Ben Widawsky63340132013-11-04 19:32:22 -08002569
2570 return ret;
2571}
2572
Ben Widawskybaa09f52013-01-24 13:49:57 -08002573static int gen6_gmch_probe(struct drm_device *dev,
2574 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002575 size_t *stolen,
2576 phys_addr_t *mappable_base,
2577 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002578{
2579 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002580 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002581 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002582 int ret;
2583
Ben Widawsky41907dd2013-02-08 11:32:47 -08002584 *mappable_base = pci_resource_start(dev->pdev, 2);
2585 *mappable_end = pci_resource_len(dev->pdev, 2);
2586
Ben Widawskybaa09f52013-01-24 13:49:57 -08002587 /* 64/512MB is the current min/max we actually know of, but this is just
2588 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002589 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08002590 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08002591 DRM_ERROR("Unknown GMADR size (%lx)\n",
2592 dev_priv->gtt.mappable_end);
2593 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002594 }
2595
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002596 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2597 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08002598 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002599
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07002600 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002601
Ben Widawsky63340132013-11-04 19:32:22 -08002602 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Michel Thierry07749ef2015-03-16 16:00:54 +00002603 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002604
Ben Widawsky63340132013-11-04 19:32:22 -08002605 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002606
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002607 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2608 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002609
2610 return ret;
2611}
2612
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002613static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002614{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002615
2616 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002617
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002618 iounmap(gtt->gsm);
2619 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002620}
2621
2622static int i915_gmch_probe(struct drm_device *dev,
2623 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002624 size_t *stolen,
2625 phys_addr_t *mappable_base,
2626 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002627{
2628 struct drm_i915_private *dev_priv = dev->dev_private;
2629 int ret;
2630
Ben Widawskybaa09f52013-01-24 13:49:57 -08002631 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2632 if (!ret) {
2633 DRM_ERROR("failed to set up gmch\n");
2634 return -EIO;
2635 }
2636
Ben Widawsky41907dd2013-02-08 11:32:47 -08002637 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002638
2639 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002640 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002641
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002642 if (unlikely(dev_priv->gtt.do_idle_maps))
2643 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2644
Ben Widawskybaa09f52013-01-24 13:49:57 -08002645 return 0;
2646}
2647
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002648static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002649{
2650 intel_gmch_remove();
2651}
2652
2653int i915_gem_gtt_init(struct drm_device *dev)
2654{
2655 struct drm_i915_private *dev_priv = dev->dev_private;
2656 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002657 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002658
Ben Widawskybaa09f52013-01-24 13:49:57 -08002659 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002660 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002661 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002662 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002663 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002664 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002665 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002666 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002667 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002668 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002669 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002670 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002671 else if (INTEL_INFO(dev)->gen >= 7)
2672 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002673 else
Chris Wilson350ec882013-08-06 13:17:02 +01002674 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002675 } else {
2676 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2677 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002678 }
2679
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002680 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002681 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002682 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002683 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002684
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002685 gtt->base.dev = dev;
2686
Ben Widawskybaa09f52013-01-24 13:49:57 -08002687 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002688 DRM_INFO("Memory usable by graphics device = %zdM\n",
2689 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002690 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2691 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002692#ifdef CONFIG_INTEL_IOMMU
2693 if (intel_iommu_gfx_mapped)
2694 DRM_INFO("VT-d active for gfx access\n");
2695#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002696 /*
2697 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2698 * user's requested state against the hardware/driver capabilities. We
2699 * do this now so that we can print out any log messages once rather
2700 * than every time we check intel_enable_ppgtt().
2701 */
2702 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2703 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002704
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002705 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002706}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002707
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002708static struct i915_vma *
2709__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2710 struct i915_address_space *vm,
2711 const struct i915_ggtt_view *ggtt_view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002712{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002713 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002714
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002715 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2716 return ERR_PTR(-EINVAL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002717 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2718 if (vma == NULL)
2719 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002720
Ben Widawsky6f65e292013-12-06 14:10:56 -08002721 INIT_LIST_HEAD(&vma->vma_link);
2722 INIT_LIST_HEAD(&vma->mm_list);
2723 INIT_LIST_HEAD(&vma->exec_list);
2724 vma->vm = vm;
2725 vma->obj = obj;
2726
Rodrigo Vivib1252bc2014-12-03 04:55:29 -08002727 if (INTEL_INFO(vm->dev)->gen >= 6) {
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002728 if (i915_is_ggtt(vm)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002729 vma->ggtt_view = *ggtt_view;
2730
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08002731 vma->unbind_vma = ggtt_unbind_vma;
2732 vma->bind_vma = ggtt_bind_vma;
2733 } else {
2734 vma->unbind_vma = ppgtt_unbind_vma;
2735 vma->bind_vma = ppgtt_bind_vma;
2736 }
Rodrigo Vivib1252bc2014-12-03 04:55:29 -08002737 } else {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002738 BUG_ON(!i915_is_ggtt(vm));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002739 vma->ggtt_view = *ggtt_view;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002740 vma->unbind_vma = i915_ggtt_unbind_vma;
2741 vma->bind_vma = i915_ggtt_bind_vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002742 }
2743
Tvrtko Ursulinf7635662014-12-03 14:59:24 +00002744 list_add_tail(&vma->vma_link, &obj->vma_list);
2745 if (!i915_is_ggtt(vm))
Michel Thierrye07f0552014-08-19 15:49:41 +01002746 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Ben Widawsky6f65e292013-12-06 14:10:56 -08002747
2748 return vma;
2749}
2750
2751struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002752i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2753 struct i915_address_space *vm)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002754{
2755 struct i915_vma *vma;
2756
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002757 vma = i915_gem_obj_to_vma(obj, vm);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002758 if (!vma)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002759 vma = __i915_gem_vma_create(obj, vm,
2760 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002761
2762 return vma;
2763}
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002764
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002765struct i915_vma *
2766i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
2767 const struct i915_ggtt_view *view)
2768{
2769 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
2770 struct i915_vma *vma;
2771
2772 if (WARN_ON(!view))
2773 return ERR_PTR(-EINVAL);
2774
2775 vma = i915_gem_obj_to_ggtt_view(obj, view);
2776
2777 if (IS_ERR(vma))
2778 return vma;
2779
2780 if (!vma)
2781 vma = __i915_gem_vma_create(obj, ggtt, view);
2782
2783 return vma;
2784
2785}
2786
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002787static void
2788rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
2789 struct sg_table *st)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002790{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002791 unsigned int column, row;
2792 unsigned int src_idx;
2793 struct scatterlist *sg = st->sgl;
2794
2795 st->nents = 0;
2796
2797 for (column = 0; column < width; column++) {
2798 src_idx = width * (height - 1) + column;
2799 for (row = 0; row < height; row++) {
2800 st->nents++;
2801 /* We don't need the pages, but need to initialize
2802 * the entries so the sg list can be happily traversed.
2803 * The only thing we need are DMA addresses.
2804 */
2805 sg_set_page(sg, NULL, PAGE_SIZE, 0);
2806 sg_dma_address(sg) = in[src_idx];
2807 sg_dma_len(sg) = PAGE_SIZE;
2808 sg = sg_next(sg);
2809 src_idx -= width;
2810 }
2811 }
2812}
2813
2814static struct sg_table *
2815intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
2816 struct drm_i915_gem_object *obj)
2817{
2818 struct drm_device *dev = obj->base.dev;
2819 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
2820 unsigned long size, pages, rot_pages;
2821 struct sg_page_iter sg_iter;
2822 unsigned long i;
2823 dma_addr_t *page_addr_list;
2824 struct sg_table *st;
2825 unsigned int tile_pitch, tile_height;
2826 unsigned int width_pages, height_pages;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00002827 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002828
2829 pages = obj->base.size / PAGE_SIZE;
2830
2831 /* Calculate tiling geometry. */
2832 tile_height = intel_tile_height(dev, rot_info->pixel_format,
2833 rot_info->fb_modifier);
2834 tile_pitch = PAGE_SIZE / tile_height;
2835 width_pages = DIV_ROUND_UP(rot_info->pitch, tile_pitch);
2836 height_pages = DIV_ROUND_UP(rot_info->height, tile_height);
2837 rot_pages = width_pages * height_pages;
2838 size = rot_pages * PAGE_SIZE;
2839
2840 /* Allocate a temporary list of source pages for random access. */
2841 page_addr_list = drm_malloc_ab(pages, sizeof(dma_addr_t));
2842 if (!page_addr_list)
2843 return ERR_PTR(ret);
2844
2845 /* Allocate target SG list. */
2846 st = kmalloc(sizeof(*st), GFP_KERNEL);
2847 if (!st)
2848 goto err_st_alloc;
2849
2850 ret = sg_alloc_table(st, rot_pages, GFP_KERNEL);
2851 if (ret)
2852 goto err_sg_alloc;
2853
2854 /* Populate source page list from the object. */
2855 i = 0;
2856 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
2857 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
2858 i++;
2859 }
2860
2861 /* Rotate the pages. */
2862 rotate_pages(page_addr_list, width_pages, height_pages, st);
2863
2864 DRM_DEBUG_KMS(
2865 "Created rotated page mapping for object size %lu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages).\n",
2866 size, rot_info->pitch, rot_info->height,
2867 rot_info->pixel_format, width_pages, height_pages,
2868 rot_pages);
2869
2870 drm_free_large(page_addr_list);
2871
2872 return st;
2873
2874err_sg_alloc:
2875 kfree(st);
2876err_st_alloc:
2877 drm_free_large(page_addr_list);
2878
2879 DRM_DEBUG_KMS(
2880 "Failed to create rotated mapping for object size %lu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages)\n",
2881 size, ret, rot_info->pitch, rot_info->height,
2882 rot_info->pixel_format, width_pages, height_pages,
2883 rot_pages);
2884 return ERR_PTR(ret);
2885}
2886
2887static inline int
2888i915_get_ggtt_vma_pages(struct i915_vma *vma)
2889{
2890 int ret = 0;
2891
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002892 if (vma->ggtt_view.pages)
2893 return 0;
2894
2895 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
2896 vma->ggtt_view.pages = vma->obj->pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002897 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
2898 vma->ggtt_view.pages =
2899 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002900 else
2901 WARN_ONCE(1, "GGTT view %u not implemented!\n",
2902 vma->ggtt_view.type);
2903
2904 if (!vma->ggtt_view.pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002905 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002906 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002907 ret = -EINVAL;
2908 } else if (IS_ERR(vma->ggtt_view.pages)) {
2909 ret = PTR_ERR(vma->ggtt_view.pages);
2910 vma->ggtt_view.pages = NULL;
2911 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
2912 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002913 }
2914
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002915 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002916}
2917
2918/**
2919 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
2920 * @vma: VMA to map
2921 * @cache_level: mapping cache level
2922 * @flags: flags like global or local mapping
2923 *
2924 * DMA addresses are taken from the scatter-gather table of this object (or of
2925 * this VMA in case of non-default GGTT views) and PTE entries set up.
2926 * Note that DMA addresses are also the only part of the SG table we care about.
2927 */
2928int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2929 u32 flags)
2930{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002931 if (i915_is_ggtt(vma->vm)) {
2932 int ret = i915_get_ggtt_vma_pages(vma);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002933
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002934 if (ret)
2935 return ret;
2936 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002937
2938 vma->bind_vma(vma, cache_level, flags);
2939
2940 return 0;
2941}