blob: 1b1a459e2b68175a19367a75d29fbbd5f821fd2d [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>
Chris Wilson5bab6f62015-10-23 18:43:32 +010027#include <linux/stop_machine.h>
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010030#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080031#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010032#include "i915_trace.h"
33#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010034#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010035
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010036#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
37
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000038/**
39 * DOC: Global GTT views
40 *
41 * Background and previous state
42 *
43 * Historically objects could exists (be bound) in global GTT space only as
44 * singular instances with a view representing all of the object's backing pages
45 * in a linear fashion. This view will be called a normal view.
46 *
47 * To support multiple views of the same object, where the number of mapped
48 * pages is not equal to the backing store, or where the layout of the pages
49 * is not linear, concept of a GGTT view was added.
50 *
51 * One example of an alternative view is a stereo display driven by a single
52 * image. In this case we would have a framebuffer looking like this
53 * (2x2 pages):
54 *
55 * 12
56 * 34
57 *
58 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
59 * rendering. In contrast, fed to the display engine would be an alternative
60 * view which could look something like this:
61 *
62 * 1212
63 * 3434
64 *
65 * In this example both the size and layout of pages in the alternative view is
66 * different from the normal view.
67 *
68 * Implementation and usage
69 *
70 * GGTT views are implemented using VMAs and are distinguished via enum
71 * i915_ggtt_view_type and struct i915_ggtt_view.
72 *
73 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020074 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
75 * renaming in large amounts of code. They take the struct i915_ggtt_view
76 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000077 *
78 * As a helper for callers which are only interested in the normal view,
79 * globally const i915_ggtt_view_normal singleton instance exists. All old core
80 * GEM API functions, the ones not taking the view parameter, are operating on,
81 * or with the normal GGTT view.
82 *
83 * Code wanting to add or use a new GGTT view needs to:
84 *
85 * 1. Add a new enum with a suitable name.
86 * 2. Extend the metadata in the i915_ggtt_view structure if required.
87 * 3. Add support to i915_get_vma_pages().
88 *
89 * New views are required to build a scatter-gather table from within the
90 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
91 * exists for the lifetime of an VMA.
92 *
93 * Core API is designed to have copy semantics which means that passed in
94 * struct i915_ggtt_view does not need to be persistent (left around after
95 * calling the core API functions).
96 *
97 */
98
Chris Wilsonce7fda22016-04-28 09:56:38 +010099static inline struct i915_ggtt *
100i915_vm_to_ggtt(struct i915_address_space *vm)
101{
102 GEM_BUG_ON(!i915_is_ggtt(vm));
103 return container_of(vm, struct i915_ggtt, base);
104}
105
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200106static int
107i915_get_ggtt_vma_pages(struct i915_vma *vma);
108
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200109const struct i915_ggtt_view i915_ggtt_view_normal = {
110 .type = I915_GGTT_VIEW_NORMAL,
111};
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200112const struct i915_ggtt_view i915_ggtt_view_rotated = {
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200113 .type = I915_GGTT_VIEW_ROTATED,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200114};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000115
Chris Wilsonc0336662016-05-06 15:40:21 +0100116int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
117 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200118{
Chris Wilson1893a712014-09-19 11:56:27 +0100119 bool has_aliasing_ppgtt;
120 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100121 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100122
Chris Wilsonc0336662016-05-06 15:40:21 +0100123 has_aliasing_ppgtt = INTEL_GEN(dev_priv) >= 6;
124 has_full_ppgtt = INTEL_GEN(dev_priv) >= 7;
125 has_full_48bit_ppgtt =
126 IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9;
Chris Wilson1893a712014-09-19 11:56:27 +0100127
Zhi Wange320d402016-09-06 12:04:12 +0800128 if (intel_vgpu_active(dev_priv)) {
129 /* emulation is too hard */
130 has_full_ppgtt = false;
131 has_full_48bit_ppgtt = false;
132 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800133
Chris Wilson0e4ca102016-04-29 13:18:22 +0100134 if (!has_aliasing_ppgtt)
135 return 0;
136
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000137 /*
138 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
139 * execlists, the sole mechanism available to submit work.
140 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100141 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200142 return 0;
143
144 if (enable_ppgtt == 1)
145 return 1;
146
Chris Wilson1893a712014-09-19 11:56:27 +0100147 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200148 return 2;
149
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100150 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
151 return 3;
152
Daniel Vetter93a25a92014-03-06 09:40:43 +0100153#ifdef CONFIG_INTEL_IOMMU
154 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100155 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100156 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200157 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100158 }
159#endif
160
Jesse Barnes62942ed2014-06-13 09:28:33 -0700161 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100162 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700163 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
164 return 0;
165 }
166
Zhi Wange320d402016-09-06 12:04:12 +0800167 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100168 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000169 else
170 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100171}
172
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200173static int ppgtt_bind_vma(struct i915_vma *vma,
174 enum i915_cache_level cache_level,
175 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200176{
177 u32 pte_flags = 0;
178
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100179 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100180
Daniel Vetter47552652015-04-14 17:35:24 +0200181 /* Currently applicable only to VLV */
182 if (vma->obj->gt_ro)
183 pte_flags |= PTE_READ_ONLY;
184
Chris Wilson247177d2016-08-15 10:48:47 +0100185 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200186 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200187
188 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200189}
190
191static void ppgtt_unbind_vma(struct i915_vma *vma)
192{
193 vma->vm->clear_range(vma->vm,
194 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200195 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200196}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800197
Daniel Vetter2c642b02015-04-14 17:35:26 +0200198static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200199 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700200{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200201 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700202 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300203
204 switch (level) {
205 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800206 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300207 break;
208 case I915_CACHE_WT:
209 pte |= PPAT_DISPLAY_ELLC_INDEX;
210 break;
211 default:
212 pte |= PPAT_CACHED_INDEX;
213 break;
214 }
215
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700216 return pte;
217}
218
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300219static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
220 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800221{
Michel Thierry07749ef2015-03-16 16:00:54 +0000222 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800223 pde |= addr;
224 if (level != I915_CACHE_NONE)
225 pde |= PPAT_CACHED_PDE_INDEX;
226 else
227 pde |= PPAT_UNCACHED_INDEX;
228 return pde;
229}
230
Michel Thierry762d9932015-07-30 11:05:29 +0100231#define gen8_pdpe_encode gen8_pde_encode
232#define gen8_pml4e_encode gen8_pde_encode
233
Michel Thierry07749ef2015-03-16 16:00:54 +0000234static gen6_pte_t snb_pte_encode(dma_addr_t addr,
235 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200236 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700237{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200238 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700239 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700240
241 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100242 case I915_CACHE_L3_LLC:
243 case I915_CACHE_LLC:
244 pte |= GEN6_PTE_CACHE_LLC;
245 break;
246 case I915_CACHE_NONE:
247 pte |= GEN6_PTE_UNCACHED;
248 break;
249 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100250 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100251 }
252
253 return pte;
254}
255
Michel Thierry07749ef2015-03-16 16:00:54 +0000256static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
257 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200258 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100259{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200260 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100261 pte |= GEN6_PTE_ADDR_ENCODE(addr);
262
263 switch (level) {
264 case I915_CACHE_L3_LLC:
265 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700266 break;
267 case I915_CACHE_LLC:
268 pte |= GEN6_PTE_CACHE_LLC;
269 break;
270 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700271 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700272 break;
273 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100274 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700275 }
276
Ben Widawsky54d12522012-09-24 16:44:32 -0700277 return pte;
278}
279
Michel Thierry07749ef2015-03-16 16:00:54 +0000280static gen6_pte_t byt_pte_encode(dma_addr_t addr,
281 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200282 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700283{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200284 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700285 pte |= GEN6_PTE_ADDR_ENCODE(addr);
286
Akash Goel24f3a8c2014-06-17 10:59:42 +0530287 if (!(flags & PTE_READ_ONLY))
288 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700289
290 if (level != I915_CACHE_NONE)
291 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
292
293 return pte;
294}
295
Michel Thierry07749ef2015-03-16 16:00:54 +0000296static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
297 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200298 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700299{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200300 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700301 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700302
303 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700304 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700305
306 return pte;
307}
308
Michel Thierry07749ef2015-03-16 16:00:54 +0000309static gen6_pte_t iris_pte_encode(dma_addr_t addr,
310 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200311 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700312{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200313 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700314 pte |= HSW_PTE_ADDR_ENCODE(addr);
315
Chris Wilson651d7942013-08-08 14:41:10 +0100316 switch (level) {
317 case I915_CACHE_NONE:
318 break;
319 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000320 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100321 break;
322 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000323 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100324 break;
325 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700326
327 return pte;
328}
329
Mika Kuoppalac114f762015-06-25 18:35:13 +0300330static int __setup_page_dma(struct drm_device *dev,
331 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000332{
David Weinehallc49d13e2016-08-22 13:32:42 +0300333 struct device *kdev = &dev->pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000334
Mika Kuoppalac114f762015-06-25 18:35:13 +0300335 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300336 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000337 return -ENOMEM;
338
David Weinehallc49d13e2016-08-22 13:32:42 +0300339 p->daddr = dma_map_page(kdev,
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300340 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
341
David Weinehallc49d13e2016-08-22 13:32:42 +0300342 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300343 __free_page(p->page);
344 return -EINVAL;
345 }
346
Michel Thierry1266cdb2015-03-24 17:06:33 +0000347 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000348}
349
Mika Kuoppalac114f762015-06-25 18:35:13 +0300350static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
351{
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100352 return __setup_page_dma(dev, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300353}
354
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300355static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
356{
David Weinehall52a05c32016-08-22 13:32:44 +0300357 struct pci_dev *pdev = dev->pdev;
358
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300359 if (WARN_ON(!p->page))
360 return;
361
David Weinehall52a05c32016-08-22 13:32:44 +0300362 dma_unmap_page(&pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300363 __free_page(p->page);
364 memset(p, 0, sizeof(*p));
365}
366
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300367static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300368{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300369 return kmap_atomic(p->page);
370}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300371
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300372/* We use the flushing unmap only with ppgtt structures:
373 * page directories, page tables and scratch pages.
374 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100375static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300376{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300377 /* There are only few exceptions for gen >=6. chv and bxt.
378 * And we are not sure about the latter so play safe for now.
379 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100380 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300381 drm_clflush_virt_range(vaddr, PAGE_SIZE);
382
383 kunmap_atomic(vaddr);
384}
385
Mika Kuoppala567047b2015-06-25 18:35:12 +0300386#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100387#define kunmap_px(ppgtt, vaddr) \
388 kunmap_page_dma(to_i915((ppgtt)->base.dev), (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300389
Mika Kuoppala567047b2015-06-25 18:35:12 +0300390#define setup_px(dev, px) setup_page_dma((dev), px_base(px))
391#define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100392#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
393#define fill32_px(dev_priv, px, v) \
394 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300395
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100396static void fill_page_dma(struct drm_i915_private *dev_priv,
397 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300398{
399 int i;
400 uint64_t * const vaddr = kmap_page_dma(p);
401
402 for (i = 0; i < 512; i++)
403 vaddr[i] = val;
404
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100405 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300406}
407
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100408static void fill_page_dma_32(struct drm_i915_private *dev_priv,
409 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300410{
411 uint64_t v = val32;
412
413 v = v << 32 | val32;
414
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100415 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300416}
417
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100418static int
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100419setup_scratch_page(struct drm_device *dev,
420 struct i915_page_dma *scratch,
421 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300422{
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100423 return __setup_page_dma(dev, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300424}
425
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100426static void cleanup_scratch_page(struct drm_device *dev,
427 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300428{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100429 cleanup_page_dma(dev, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300430}
431
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300432static struct i915_page_table *alloc_pt(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000433{
Michel Thierryec565b32015-04-08 12:13:23 +0100434 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000435 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
436 GEN8_PTES : GEN6_PTES;
437 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000438
439 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
440 if (!pt)
441 return ERR_PTR(-ENOMEM);
442
Ben Widawsky678d96f2015-03-16 16:00:56 +0000443 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
444 GFP_KERNEL);
445
446 if (!pt->used_ptes)
447 goto fail_bitmap;
448
Mika Kuoppala567047b2015-06-25 18:35:12 +0300449 ret = setup_px(dev, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000450 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300451 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000452
453 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000454
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300455fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000456 kfree(pt->used_ptes);
457fail_bitmap:
458 kfree(pt);
459
460 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000461}
462
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300463static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000464{
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300465 cleanup_px(dev, pt);
466 kfree(pt->used_ptes);
467 kfree(pt);
468}
469
470static void gen8_initialize_pt(struct i915_address_space *vm,
471 struct i915_page_table *pt)
472{
473 gen8_pte_t scratch_pte;
474
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100475 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200476 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300477
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100478 fill_px(to_i915(vm->dev), pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300479}
480
481static void gen6_initialize_pt(struct i915_address_space *vm,
482 struct i915_page_table *pt)
483{
484 gen6_pte_t scratch_pte;
485
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100486 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300487
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100488 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200489 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300490
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100491 fill32_px(to_i915(vm->dev), pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000492}
493
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300494static struct i915_page_directory *alloc_pd(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000495{
Michel Thierryec565b32015-04-08 12:13:23 +0100496 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100497 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000498
499 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
500 if (!pd)
501 return ERR_PTR(-ENOMEM);
502
Michel Thierry33c88192015-04-08 12:13:33 +0100503 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
504 sizeof(*pd->used_pdes), GFP_KERNEL);
505 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300506 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100507
Mika Kuoppala567047b2015-06-25 18:35:12 +0300508 ret = setup_px(dev, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100509 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300510 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100511
Ben Widawsky06fda602015-02-24 16:22:36 +0000512 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100513
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300514fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100515 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300516fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100517 kfree(pd);
518
519 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000520}
521
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300522static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
523{
524 if (px_page(pd)) {
525 cleanup_px(dev, pd);
526 kfree(pd->used_pdes);
527 kfree(pd);
528 }
529}
530
531static void gen8_initialize_pd(struct i915_address_space *vm,
532 struct i915_page_directory *pd)
533{
534 gen8_pde_t scratch_pde;
535
536 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
537
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100538 fill_px(to_i915(vm->dev), pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300539}
540
Michel Thierry6ac18502015-07-29 17:23:46 +0100541static int __pdp_init(struct drm_device *dev,
542 struct i915_page_directory_pointer *pdp)
543{
544 size_t pdpes = I915_PDPES_PER_PDP(dev);
545
546 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
547 sizeof(unsigned long),
548 GFP_KERNEL);
549 if (!pdp->used_pdpes)
550 return -ENOMEM;
551
552 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
553 GFP_KERNEL);
554 if (!pdp->page_directory) {
555 kfree(pdp->used_pdpes);
556 /* the PDP might be the statically allocated top level. Keep it
557 * as clean as possible */
558 pdp->used_pdpes = NULL;
559 return -ENOMEM;
560 }
561
562 return 0;
563}
564
565static void __pdp_fini(struct i915_page_directory_pointer *pdp)
566{
567 kfree(pdp->used_pdpes);
568 kfree(pdp->page_directory);
569 pdp->page_directory = NULL;
570}
571
Michel Thierry762d9932015-07-30 11:05:29 +0100572static struct
573i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
574{
575 struct i915_page_directory_pointer *pdp;
576 int ret = -ENOMEM;
577
578 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
579
580 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
581 if (!pdp)
582 return ERR_PTR(-ENOMEM);
583
584 ret = __pdp_init(dev, pdp);
585 if (ret)
586 goto fail_bitmap;
587
588 ret = setup_px(dev, pdp);
589 if (ret)
590 goto fail_page_m;
591
592 return pdp;
593
594fail_page_m:
595 __pdp_fini(pdp);
596fail_bitmap:
597 kfree(pdp);
598
599 return ERR_PTR(ret);
600}
601
Michel Thierry6ac18502015-07-29 17:23:46 +0100602static void free_pdp(struct drm_device *dev,
603 struct i915_page_directory_pointer *pdp)
604{
605 __pdp_fini(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100606 if (USES_FULL_48BIT_PPGTT(dev)) {
607 cleanup_px(dev, pdp);
608 kfree(pdp);
609 }
610}
611
Michel Thierry69ab76f2015-07-29 17:23:55 +0100612static void gen8_initialize_pdp(struct i915_address_space *vm,
613 struct i915_page_directory_pointer *pdp)
614{
615 gen8_ppgtt_pdpe_t scratch_pdpe;
616
617 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
618
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100619 fill_px(to_i915(vm->dev), pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100620}
621
622static void gen8_initialize_pml4(struct i915_address_space *vm,
623 struct i915_pml4 *pml4)
624{
625 gen8_ppgtt_pml4e_t scratch_pml4e;
626
627 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
628 I915_CACHE_LLC);
629
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100630 fill_px(to_i915(vm->dev), pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100631}
632
Michel Thierry762d9932015-07-30 11:05:29 +0100633static void
634gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
635 struct i915_page_directory_pointer *pdp,
636 struct i915_page_directory *pd,
637 int index)
638{
639 gen8_ppgtt_pdpe_t *page_directorypo;
640
641 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
642 return;
643
644 page_directorypo = kmap_px(pdp);
645 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
646 kunmap_px(ppgtt, page_directorypo);
647}
648
649static void
650gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
651 struct i915_pml4 *pml4,
652 struct i915_page_directory_pointer *pdp,
653 int index)
654{
655 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
656
657 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
658 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
659 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100660}
661
Ben Widawsky94e409c2013-11-04 22:29:36 -0800662/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100663static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100664 unsigned entry,
665 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800666{
Chris Wilson7e37f882016-08-02 22:50:21 +0100667 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000668 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800669 int ret;
670
671 BUG_ON(entry >= 4);
672
John Harrison5fb9de12015-05-29 17:44:07 +0100673 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800674 if (ret)
675 return ret;
676
Chris Wilsonb5321f32016-08-02 22:50:18 +0100677 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
678 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
679 intel_ring_emit(ring, upper_32_bits(addr));
680 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
681 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
682 intel_ring_emit(ring, lower_32_bits(addr));
683 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800684
685 return 0;
686}
687
Michel Thierry2dba3232015-07-30 11:06:23 +0100688static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
689 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800690{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800691 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800692
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100693 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300694 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
695
John Harrisone85b26d2015-05-29 17:43:56 +0100696 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800697 if (ret)
698 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800699 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800700
Ben Widawskyeeb94882013-12-06 14:11:10 -0800701 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800702}
703
Michel Thierry2dba3232015-07-30 11:06:23 +0100704static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
705 struct drm_i915_gem_request *req)
706{
707 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
708}
709
Michał Winiarski2ce51792016-10-13 14:02:42 +0200710/* Removes entries from a single page table, releasing it if it's empty.
711 * Caller can use the return value to update higher-level entries.
712 */
713static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200714 struct i915_page_table *pt,
715 uint64_t start,
716 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700717{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300718 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200719 unsigned int pte_start = gen8_pte_index(start);
720 unsigned int num_entries = gen8_pte_count(start, length);
721 uint64_t pte;
722 gen8_pte_t *pt_vaddr;
723 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
724 I915_CACHE_LLC);
725
726 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200727 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700728
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200729 bitmap_clear(pt->used_ptes, pte_start, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000730
Michał Winiarski2ce51792016-10-13 14:02:42 +0200731 if (bitmap_empty(pt->used_ptes, GEN8_PTES)) {
732 free_pt(vm->dev, pt);
733 return true;
734 }
735
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200736 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000737
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200738 for (pte = pte_start; pte < num_entries; pte++)
739 pt_vaddr[pte] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000740
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200741 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200742
743 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200744}
745
Michał Winiarski2ce51792016-10-13 14:02:42 +0200746/* Removes entries from a single page dir, releasing it if it's empty.
747 * Caller can use the return value to update higher-level entries
748 */
749static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200750 struct i915_page_directory *pd,
751 uint64_t start,
752 uint64_t length)
753{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200754 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200755 struct i915_page_table *pt;
756 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200757 gen8_pde_t *pde_vaddr;
758 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
759 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200760
761 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000762 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100763 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000764
Michał Winiarski2ce51792016-10-13 14:02:42 +0200765 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
766 __clear_bit(pde, pd->used_pdes);
767 pde_vaddr = kmap_px(pd);
768 pde_vaddr[pde] = scratch_pde;
769 kunmap_px(ppgtt, pde_vaddr);
770 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200771 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200772
773 if (bitmap_empty(pd->used_pdes, I915_PDES)) {
774 free_pd(vm->dev, pd);
775 return true;
776 }
777
778 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200779}
Ben Widawsky06fda602015-02-24 16:22:36 +0000780
Michał Winiarski2ce51792016-10-13 14:02:42 +0200781/* Removes entries from a single page dir pointer, releasing it if it's empty.
782 * Caller can use the return value to update higher-level entries
783 */
784static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200785 struct i915_page_directory_pointer *pdp,
786 uint64_t start,
787 uint64_t length)
788{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200789 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200790 struct i915_page_directory *pd;
791 uint64_t pdpe;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200792 gen8_ppgtt_pdpe_t *pdpe_vaddr;
793 gen8_ppgtt_pdpe_t scratch_pdpe =
794 gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200795
796 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
797 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100798 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000799
Michał Winiarski2ce51792016-10-13 14:02:42 +0200800 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
801 __clear_bit(pdpe, pdp->used_pdpes);
802 if (USES_FULL_48BIT_PPGTT(vm->dev)) {
803 pdpe_vaddr = kmap_px(pdp);
804 pdpe_vaddr[pdpe] = scratch_pdpe;
805 kunmap_px(ppgtt, pdpe_vaddr);
806 }
807 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200808 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809
810 if (USES_FULL_48BIT_PPGTT(vm->dev) &&
811 bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(vm->dev))) {
812 free_pdp(vm->dev, pdp);
813 return true;
814 }
815
816 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200817}
Ben Widawsky459108b2013-11-02 21:07:23 -0700818
Michał Winiarski2ce51792016-10-13 14:02:42 +0200819/* Removes entries from a single pml4.
820 * This is the top-level structure in 4-level page tables used on gen8+.
821 * Empty entries are always scratch pml4e.
822 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200823static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
824 struct i915_pml4 *pml4,
825 uint64_t start,
826 uint64_t length)
827{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200828 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200829 struct i915_page_directory_pointer *pdp;
830 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200831 gen8_ppgtt_pml4e_t *pml4e_vaddr;
832 gen8_ppgtt_pml4e_t scratch_pml4e =
833 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
834
835 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->dev));
Ben Widawsky459108b2013-11-02 21:07:23 -0700836
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200837 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
838 if (WARN_ON(!pml4->pdps[pml4e]))
839 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700840
Michał Winiarski2ce51792016-10-13 14:02:42 +0200841 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
842 __clear_bit(pml4e, pml4->used_pml4es);
843 pml4e_vaddr = kmap_px(pml4);
844 pml4e_vaddr[pml4e] = scratch_pml4e;
845 kunmap_px(ppgtt, pml4e_vaddr);
846 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700847 }
848}
849
Michel Thierryf9b5b782015-07-30 11:02:49 +0100850static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200851 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700852{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300853 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100854
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200855 if (USES_FULL_48BIT_PPGTT(vm->dev))
856 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
857 else
858 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100859}
860
861static void
862gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
863 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100864 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100865 uint64_t start,
866 enum i915_cache_level cache_level)
867{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300868 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000869 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100870 unsigned pdpe = gen8_pdpe_index(start);
871 unsigned pde = gen8_pde_index(start);
872 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700873
Chris Wilson6f1cc992013-12-31 15:50:31 +0000874 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700875
Michel Thierry3387d432015-08-03 09:52:47 +0100876 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000877 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100878 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100879 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300880 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000881 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800882
883 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100884 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200885 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000886 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300887 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000888 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000889 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100890 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
891 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800892 pde = 0;
893 }
894 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700895 }
896 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300897
898 if (pt_vaddr)
899 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700900}
901
Michel Thierryf9b5b782015-07-30 11:02:49 +0100902static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
903 struct sg_table *pages,
904 uint64_t start,
905 enum i915_cache_level cache_level,
906 u32 unused)
907{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300908 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100909 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100910
Michel Thierry3387d432015-08-03 09:52:47 +0100911 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100912
913 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
914 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
915 cache_level);
916 } else {
917 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000918 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100919 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
920
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000921 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100922 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
923 start, cache_level);
924 }
925 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100926}
927
Michel Thierryf37c0502015-06-10 17:46:39 +0100928static void gen8_free_page_tables(struct drm_device *dev,
929 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800930{
931 int i;
932
Mika Kuoppala567047b2015-06-25 18:35:12 +0300933 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800934 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800935
Michel Thierry33c88192015-04-08 12:13:33 +0100936 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000937 if (WARN_ON(!pd->page_table[i]))
938 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800939
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300940 free_pt(dev, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000941 pd->page_table[i] = NULL;
942 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000943}
944
Mika Kuoppala8776f022015-06-30 18:16:40 +0300945static int gen8_init_scratch(struct i915_address_space *vm)
946{
947 struct drm_device *dev = vm->dev;
Matthew Auld64c050d2016-04-27 13:19:25 +0100948 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300949
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100950 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100951 if (ret)
952 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300953
954 vm->scratch_pt = alloc_pt(dev);
955 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100956 ret = PTR_ERR(vm->scratch_pt);
957 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300958 }
959
960 vm->scratch_pd = alloc_pd(dev);
961 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100962 ret = PTR_ERR(vm->scratch_pd);
963 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300964 }
965
Michel Thierry69ab76f2015-07-29 17:23:55 +0100966 if (USES_FULL_48BIT_PPGTT(dev)) {
967 vm->scratch_pdp = alloc_pdp(dev);
968 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100969 ret = PTR_ERR(vm->scratch_pdp);
970 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100971 }
972 }
973
Mika Kuoppala8776f022015-06-30 18:16:40 +0300974 gen8_initialize_pt(vm, vm->scratch_pt);
975 gen8_initialize_pd(vm, vm->scratch_pd);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100976 if (USES_FULL_48BIT_PPGTT(dev))
977 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300978
979 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100980
981free_pd:
982 free_pd(dev, vm->scratch_pd);
983free_pt:
984 free_pt(dev, vm->scratch_pt);
985free_scratch_page:
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100986 cleanup_scratch_page(dev, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100987
988 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300989}
990
Zhiyuan Lv650da342015-08-28 15:41:18 +0800991static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
992{
993 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +0100994 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +0800995 int i;
996
Matthew Aulddf285642016-04-22 12:09:25 +0100997 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +0800998 u64 daddr = px_dma(&ppgtt->pml4);
999
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001000 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1001 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001002
1003 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1004 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1005 } else {
1006 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1007 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1008
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001009 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1010 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001011 }
1012
1013 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1014 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1015 }
1016
1017 I915_WRITE(vgtif_reg(g2v_notify), msg);
1018
1019 return 0;
1020}
1021
Mika Kuoppala8776f022015-06-30 18:16:40 +03001022static void gen8_free_scratch(struct i915_address_space *vm)
1023{
1024 struct drm_device *dev = vm->dev;
1025
Michel Thierry69ab76f2015-07-29 17:23:55 +01001026 if (USES_FULL_48BIT_PPGTT(dev))
1027 free_pdp(dev, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001028 free_pd(dev, vm->scratch_pd);
1029 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001030 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001031}
1032
Michel Thierry762d9932015-07-30 11:05:29 +01001033static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
1034 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001035{
1036 int i;
1037
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001038 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
1039 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001040 continue;
1041
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001042 gen8_free_page_tables(dev, pdp->page_directory[i]);
1043 free_pd(dev, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001044 }
Michel Thierry69876be2015-04-08 12:13:27 +01001045
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001046 free_pdp(dev, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001047}
1048
1049static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1050{
1051 int i;
1052
1053 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1054 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1055 continue;
1056
1057 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
1058 }
1059
1060 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
1061}
1062
1063static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1064{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001065 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001066
Chris Wilsonc0336662016-05-06 15:40:21 +01001067 if (intel_vgpu_active(to_i915(vm->dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001068 gen8_ppgtt_notify_vgt(ppgtt, false);
1069
Michel Thierry762d9932015-07-30 11:05:29 +01001070 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1071 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1072 else
1073 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001074
Mika Kuoppala8776f022015-06-30 18:16:40 +03001075 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001076}
1077
Michel Thierryd7b26332015-04-08 12:13:34 +01001078/**
1079 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001080 * @vm: Master vm structure.
1081 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001082 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001083 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001084 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1085 * caller to free on error.
1086 *
1087 * Allocate the required number of page tables. Extremely similar to
1088 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1089 * the page directory boundary (instead of the page directory pointer). That
1090 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1091 * possible, and likely that the caller will need to use multiple calls of this
1092 * function to achieve the appropriate allocation.
1093 *
1094 * Return: 0 if success; negative error code otherwise.
1095 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001096static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001097 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001098 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001099 uint64_t length,
1100 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001101{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001102 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001103 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001104 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001105
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001106 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001107 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001108 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001109 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001110 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001111 continue;
1112 }
1113
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001114 pt = alloc_pt(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001115 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001116 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001117
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001118 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001119 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001120 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001121 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001122 }
1123
1124 return 0;
1125
1126unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001127 for_each_set_bit(pde, new_pts, I915_PDES)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001128 free_pt(dev, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001129
1130 return -ENOMEM;
1131}
1132
Michel Thierryd7b26332015-04-08 12:13:34 +01001133/**
1134 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001135 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001136 * @pdp: Page directory pointer for this address range.
1137 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001138 * @length: Size of the allocations.
1139 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001140 * caller to free on error.
1141 *
1142 * Allocate the required number of page directories starting at the pde index of
1143 * @start, and ending at the pde index @start + @length. This function will skip
1144 * over already allocated page directories within the range, and only allocate
1145 * new ones, setting the appropriate pointer within the pdp as well as the
1146 * correct position in the bitmap @new_pds.
1147 *
1148 * The function will only allocate the pages within the range for a give page
1149 * directory pointer. In other words, if @start + @length straddles a virtually
1150 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1151 * required by the caller, This is not currently possible, and the BUG in the
1152 * code will prevent it.
1153 *
1154 * Return: 0 if success; negative error code otherwise.
1155 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001156static int
1157gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1158 struct i915_page_directory_pointer *pdp,
1159 uint64_t start,
1160 uint64_t length,
1161 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001162{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001163 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001164 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001165 uint32_t pdpe;
Michel Thierry6ac18502015-07-29 17:23:46 +01001166 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001167
Michel Thierry6ac18502015-07-29 17:23:46 +01001168 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001169
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001170 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001171 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001172 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001173
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001174 pd = alloc_pd(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001175 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001176 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001177
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001178 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001179 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001180 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001181 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001182 }
1183
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001184 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001185
1186unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001187 for_each_set_bit(pdpe, new_pds, pdpes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001188 free_pd(dev, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001189
1190 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001191}
1192
Michel Thierry762d9932015-07-30 11:05:29 +01001193/**
1194 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1195 * @vm: Master vm structure.
1196 * @pml4: Page map level 4 for this address range.
1197 * @start: Starting virtual address to begin allocations.
1198 * @length: Size of the allocations.
1199 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1200 * caller to free on error.
1201 *
1202 * Allocate the required number of page directory pointers. Extremely similar to
1203 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1204 * The main difference is here we are limited by the pml4 boundary (instead of
1205 * the page directory pointer).
1206 *
1207 * Return: 0 if success; negative error code otherwise.
1208 */
1209static int
1210gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1211 struct i915_pml4 *pml4,
1212 uint64_t start,
1213 uint64_t length,
1214 unsigned long *new_pdps)
1215{
1216 struct drm_device *dev = vm->dev;
1217 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001218 uint32_t pml4e;
1219
1220 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1221
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001222 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001223 if (!test_bit(pml4e, pml4->used_pml4es)) {
1224 pdp = alloc_pdp(dev);
1225 if (IS_ERR(pdp))
1226 goto unwind_out;
1227
Michel Thierry69ab76f2015-07-29 17:23:55 +01001228 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001229 pml4->pdps[pml4e] = pdp;
1230 __set_bit(pml4e, new_pdps);
1231 trace_i915_page_directory_pointer_entry_alloc(vm,
1232 pml4e,
1233 start,
1234 GEN8_PML4E_SHIFT);
1235 }
1236 }
1237
1238 return 0;
1239
1240unwind_out:
1241 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1242 free_pdp(dev, pml4->pdps[pml4e]);
1243
1244 return -ENOMEM;
1245}
1246
Michel Thierryd7b26332015-04-08 12:13:34 +01001247static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001248free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001249{
Michel Thierryd7b26332015-04-08 12:13:34 +01001250 kfree(new_pts);
1251 kfree(new_pds);
1252}
1253
1254/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1255 * of these are based on the number of PDPEs in the system.
1256 */
1257static
1258int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001259 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001260 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001261{
Michel Thierryd7b26332015-04-08 12:13:34 +01001262 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001263 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001264
Michał Winiarski3a41a052015-09-03 19:22:18 +02001265 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001266 if (!pds)
1267 return -ENOMEM;
1268
Michał Winiarski3a41a052015-09-03 19:22:18 +02001269 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1270 GFP_TEMPORARY);
1271 if (!pts)
1272 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001273
1274 *new_pds = pds;
1275 *new_pts = pts;
1276
1277 return 0;
1278
1279err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001280 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001281 return -ENOMEM;
1282}
1283
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001284/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1285 * the page table structures, we mark them dirty so that
1286 * context switching/execlist queuing code takes extra steps
1287 * to ensure that tlbs are flushed.
1288 */
1289static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1290{
1291 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1292}
1293
Michel Thierry762d9932015-07-30 11:05:29 +01001294static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1295 struct i915_page_directory_pointer *pdp,
1296 uint64_t start,
1297 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001298{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001299 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001300 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001301 struct drm_device *dev = vm->dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001302 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001303 const uint64_t orig_start = start;
1304 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001305 uint32_t pdpe;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001306 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001307 int ret;
1308
Michel Thierryd7b26332015-04-08 12:13:34 +01001309 /* Wrap is never okay since we can only represent 48b, and we don't
1310 * actually use the other side of the canonical address space.
1311 */
1312 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001313 return -ENODEV;
1314
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001315 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001316 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001317
Michel Thierry6ac18502015-07-29 17:23:46 +01001318 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001319 if (ret)
1320 return ret;
1321
Michel Thierryd7b26332015-04-08 12:13:34 +01001322 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001323 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1324 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001325 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001326 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001327 return ret;
1328 }
1329
1330 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001331 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001332 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001333 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001334 if (ret)
1335 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001336 }
1337
Michel Thierry33c88192015-04-08 12:13:33 +01001338 start = orig_start;
1339 length = orig_length;
1340
Michel Thierryd7b26332015-04-08 12:13:34 +01001341 /* Allocations have completed successfully, so set the bitmaps, and do
1342 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001343 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001344 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001345 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001346 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001347 uint64_t pd_start = start;
1348 uint32_t pde;
1349
Michel Thierryd7b26332015-04-08 12:13:34 +01001350 /* Every pd should be allocated, we just did that above. */
1351 WARN_ON(!pd);
1352
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001353 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001354 /* Same reasoning as pd */
1355 WARN_ON(!pt);
1356 WARN_ON(!pd_len);
1357 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1358
1359 /* Set our used ptes within the page table */
1360 bitmap_set(pt->used_ptes,
1361 gen8_pte_index(pd_start),
1362 gen8_pte_count(pd_start, pd_len));
1363
1364 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001365 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001366
1367 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001368 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1369 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001370 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1371 gen8_pte_index(start),
1372 gen8_pte_count(start, length),
1373 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001374
1375 /* NB: We haven't yet mapped ptes to pages. At this
1376 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001377 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001378
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001379 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001380 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001381 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001382 }
1383
Michał Winiarski3a41a052015-09-03 19:22:18 +02001384 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001385 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001386 return 0;
1387
1388err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001389 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001390 unsigned long temp;
1391
Michał Winiarski3a41a052015-09-03 19:22:18 +02001392 for_each_set_bit(temp, new_page_tables + pdpe *
1393 BITS_TO_LONGS(I915_PDES), I915_PDES)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001394 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001395 }
1396
Michel Thierry6ac18502015-07-29 17:23:46 +01001397 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001398 free_pd(dev, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001399
Michał Winiarski3a41a052015-09-03 19:22:18 +02001400 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001401 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001402 return ret;
1403}
1404
Michel Thierry762d9932015-07-30 11:05:29 +01001405static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1406 struct i915_pml4 *pml4,
1407 uint64_t start,
1408 uint64_t length)
1409{
1410 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001411 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001412 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001413 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001414 int ret = 0;
1415
1416 /* Do the pml4 allocations first, so we don't need to track the newly
1417 * allocated tables below the pdp */
1418 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1419
1420 /* The pagedirectory and pagetable allocations are done in the shared 3
1421 * and 4 level code. Just allocate the pdps.
1422 */
1423 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1424 new_pdps);
1425 if (ret)
1426 return ret;
1427
1428 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1429 "The allocation has spanned more than 512GB. "
1430 "It is highly likely this is incorrect.");
1431
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001432 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001433 WARN_ON(!pdp);
1434
1435 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1436 if (ret)
1437 goto err_out;
1438
1439 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1440 }
1441
1442 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1443 GEN8_PML4ES_PER_PML4);
1444
1445 return 0;
1446
1447err_out:
1448 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1449 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1450
1451 return ret;
1452}
1453
1454static int gen8_alloc_va_range(struct i915_address_space *vm,
1455 uint64_t start, uint64_t length)
1456{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001457 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001458
1459 if (USES_FULL_48BIT_PPGTT(vm->dev))
1460 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1461 else
1462 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1463}
1464
Michel Thierryea91e402015-07-29 17:23:57 +01001465static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1466 uint64_t start, uint64_t length,
1467 gen8_pte_t scratch_pte,
1468 struct seq_file *m)
1469{
1470 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001471 uint32_t pdpe;
1472
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001473 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001474 struct i915_page_table *pt;
1475 uint64_t pd_len = length;
1476 uint64_t pd_start = start;
1477 uint32_t pde;
1478
1479 if (!test_bit(pdpe, pdp->used_pdpes))
1480 continue;
1481
1482 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001483 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001484 uint32_t pte;
1485 gen8_pte_t *pt_vaddr;
1486
1487 if (!test_bit(pde, pd->used_pdes))
1488 continue;
1489
1490 pt_vaddr = kmap_px(pt);
1491 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1492 uint64_t va =
1493 (pdpe << GEN8_PDPE_SHIFT) |
1494 (pde << GEN8_PDE_SHIFT) |
1495 (pte << GEN8_PTE_SHIFT);
1496 int i;
1497 bool found = false;
1498
1499 for (i = 0; i < 4; i++)
1500 if (pt_vaddr[pte + i] != scratch_pte)
1501 found = true;
1502 if (!found)
1503 continue;
1504
1505 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1506 for (i = 0; i < 4; i++) {
1507 if (pt_vaddr[pte + i] != scratch_pte)
1508 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1509 else
1510 seq_puts(m, " SCRATCH ");
1511 }
1512 seq_puts(m, "\n");
1513 }
1514 /* don't use kunmap_px, it could trigger
1515 * an unnecessary flush.
1516 */
1517 kunmap_atomic(pt_vaddr);
1518 }
1519 }
1520}
1521
1522static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1523{
1524 struct i915_address_space *vm = &ppgtt->base;
1525 uint64_t start = ppgtt->base.start;
1526 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001527 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001528 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001529
1530 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1531 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1532 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001533 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001534 struct i915_pml4 *pml4 = &ppgtt->pml4;
1535 struct i915_page_directory_pointer *pdp;
1536
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001537 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001538 if (!test_bit(pml4e, pml4->used_pml4es))
1539 continue;
1540
1541 seq_printf(m, " PML4E #%llu\n", pml4e);
1542 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1543 }
1544 }
1545}
1546
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001547static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1548{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001549 unsigned long *new_page_dirs, *new_page_tables;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001550 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1551 int ret;
1552
1553 /* We allocate temp bitmap for page tables for no gain
1554 * but as this is for init only, lets keep the things simple
1555 */
1556 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1557 if (ret)
1558 return ret;
1559
1560 /* Allocate for all pdps regardless of how the ppgtt
1561 * was defined.
1562 */
1563 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1564 0, 1ULL << 32,
1565 new_page_dirs);
1566 if (!ret)
1567 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1568
Michał Winiarski3a41a052015-09-03 19:22:18 +02001569 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001570
1571 return ret;
1572}
1573
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001574/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001575 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1576 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1577 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1578 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001579 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001580 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001581static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001582{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001583 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001584
Mika Kuoppala8776f022015-06-30 18:16:40 +03001585 ret = gen8_init_scratch(&ppgtt->base);
1586 if (ret)
1587 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001588
Michel Thierryd7b26332015-04-08 12:13:34 +01001589 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001590 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001591 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001592 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001593 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001594 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1595 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001596 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001597
Michel Thierry762d9932015-07-30 11:05:29 +01001598 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1599 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1600 if (ret)
1601 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001602
Michel Thierry69ab76f2015-07-29 17:23:55 +01001603 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1604
Michel Thierry762d9932015-07-30 11:05:29 +01001605 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001606 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001607 } else {
Michel Thierry25f50332015-08-07 17:40:19 +01001608 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001609 if (ret)
1610 goto free_scratch;
1611
1612 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001613 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001614 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1615 0, 0,
1616 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001617
Chris Wilsonc0336662016-05-06 15:40:21 +01001618 if (intel_vgpu_active(to_i915(ppgtt->base.dev))) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001619 ret = gen8_preallocate_top_level_pdps(ppgtt);
1620 if (ret)
1621 goto free_scratch;
1622 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001623 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001624
Chris Wilsonc0336662016-05-06 15:40:21 +01001625 if (intel_vgpu_active(to_i915(ppgtt->base.dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001626 gen8_ppgtt_notify_vgt(ppgtt, true);
1627
Michel Thierryd7b26332015-04-08 12:13:34 +01001628 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001629
1630free_scratch:
1631 gen8_free_scratch(&ppgtt->base);
1632 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001633}
1634
Ben Widawsky87d60b62013-12-06 14:11:29 -08001635static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1636{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001637 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001638 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001639 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001640 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001641 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001642 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001643
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001644 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001645 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001646
Dave Gordon731f74c2016-06-24 19:37:46 +01001647 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001648 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001649 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001650 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001651 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001652 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1653
1654 if (pd_entry != expected)
1655 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1656 pde,
1657 pd_entry,
1658 expected);
1659 seq_printf(m, "\tPDE: %x\n", pd_entry);
1660
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001661 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1662
Michel Thierry07749ef2015-03-16 16:00:54 +00001663 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001664 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001665 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001666 (pte * PAGE_SIZE);
1667 int i;
1668 bool found = false;
1669 for (i = 0; i < 4; i++)
1670 if (pt_vaddr[pte + i] != scratch_pte)
1671 found = true;
1672 if (!found)
1673 continue;
1674
1675 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1676 for (i = 0; i < 4; i++) {
1677 if (pt_vaddr[pte + i] != scratch_pte)
1678 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1679 else
1680 seq_puts(m, " SCRATCH ");
1681 }
1682 seq_puts(m, "\n");
1683 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001684 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001685 }
1686}
1687
Ben Widawsky678d96f2015-03-16 16:00:56 +00001688/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001689static void gen6_write_pde(struct i915_page_directory *pd,
1690 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001691{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001692 /* Caller needs to make sure the write completes if necessary */
1693 struct i915_hw_ppgtt *ppgtt =
1694 container_of(pd, struct i915_hw_ppgtt, pd);
1695 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001696
Mika Kuoppala567047b2015-06-25 18:35:12 +03001697 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001698 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001699
Ben Widawsky678d96f2015-03-16 16:00:56 +00001700 writel(pd_entry, ppgtt->pd_addr + pde);
1701}
Ben Widawsky61973492013-04-08 18:43:54 -07001702
Ben Widawsky678d96f2015-03-16 16:00:56 +00001703/* Write all the page tables found in the ppgtt structure to incrementing page
1704 * directories. */
1705static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001706 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001707 uint32_t start, uint32_t length)
1708{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001709 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001710 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001711 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001712
Dave Gordon731f74c2016-06-24 19:37:46 +01001713 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001714 gen6_write_pde(pd, pde, pt);
1715
1716 /* Make sure write is complete before other code can use this page
1717 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001718 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001719}
1720
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001721static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001722{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001723 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001724
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001725 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001726}
Ben Widawsky61973492013-04-08 18:43:54 -07001727
Ben Widawsky90252e52013-12-06 14:11:12 -08001728static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001729 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001730{
Chris Wilson7e37f882016-08-02 22:50:21 +01001731 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001732 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001733 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001734
Ben Widawsky90252e52013-12-06 14:11:12 -08001735 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001736 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001737 if (ret)
1738 return ret;
1739
John Harrison5fb9de12015-05-29 17:44:07 +01001740 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001741 if (ret)
1742 return ret;
1743
Chris Wilsonb5321f32016-08-02 22:50:18 +01001744 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1745 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1746 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1747 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1748 intel_ring_emit(ring, get_pd_offset(ppgtt));
1749 intel_ring_emit(ring, MI_NOOP);
1750 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001751
1752 return 0;
1753}
1754
Ben Widawsky48a10382013-12-06 14:11:11 -08001755static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001756 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001757{
Chris Wilson7e37f882016-08-02 22:50:21 +01001758 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001759 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001760 int ret;
1761
Ben Widawsky48a10382013-12-06 14:11:11 -08001762 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001763 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001764 if (ret)
1765 return ret;
1766
John Harrison5fb9de12015-05-29 17:44:07 +01001767 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001768 if (ret)
1769 return ret;
1770
Chris Wilsonb5321f32016-08-02 22:50:18 +01001771 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1772 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1773 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1774 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1775 intel_ring_emit(ring, get_pd_offset(ppgtt));
1776 intel_ring_emit(ring, MI_NOOP);
1777 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001778
Ben Widawsky90252e52013-12-06 14:11:12 -08001779 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001780 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001781 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001782 if (ret)
1783 return ret;
1784 }
1785
Ben Widawsky48a10382013-12-06 14:11:11 -08001786 return 0;
1787}
1788
Ben Widawskyeeb94882013-12-06 14:11:10 -08001789static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001790 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001791{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001792 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001793 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001794
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001795 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1796 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001797 return 0;
1798}
1799
Daniel Vetter82460d92014-08-06 20:19:53 +02001800static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001801{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001802 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001803 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301804 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001805
Akash Goel3b3f1652016-10-13 22:44:48 +05301806 for_each_engine(engine, dev_priv, id) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001807 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001808 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001809 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001810 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001811}
1812
Daniel Vetter82460d92014-08-06 20:19:53 +02001813static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001814{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001815 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001816 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001817 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301818 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001820 ecobits = I915_READ(GAC_ECO_BITS);
1821 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1822
1823 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001824 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001825 ecochk |= ECOCHK_PPGTT_WB_HSW;
1826 } else {
1827 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1828 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1829 }
1830 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001831
Akash Goel3b3f1652016-10-13 22:44:48 +05301832 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001833 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001834 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001835 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001836 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001837}
1838
Daniel Vetter82460d92014-08-06 20:19:53 +02001839static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001840{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001841 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001842 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001843
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001844 ecobits = I915_READ(GAC_ECO_BITS);
1845 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1846 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001847
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001848 gab_ctl = I915_READ(GAB_CTL);
1849 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001850
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001851 ecochk = I915_READ(GAM_ECOCHK);
1852 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001853
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001854 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001855}
1856
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001857/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001858static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001859 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001860 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001861{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001862 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001863 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001864 unsigned first_entry = start >> PAGE_SHIFT;
1865 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001866 unsigned act_pt = first_entry / GEN6_PTES;
1867 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001868 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001869
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001870 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001871 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001872
Daniel Vetter7bddb012012-02-09 17:15:47 +01001873 while (num_entries) {
1874 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001875 if (last_pte > GEN6_PTES)
1876 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001877
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001878 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001879
1880 for (i = first_pte; i < last_pte; i++)
1881 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001882
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001883 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001884
Daniel Vetter7bddb012012-02-09 17:15:47 +01001885 num_entries -= last_pte - first_pte;
1886 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001887 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001888 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001889}
1890
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001891static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001892 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001893 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301894 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001895{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001896 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001897 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001898 unsigned act_pt = first_entry / GEN6_PTES;
1899 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001900 gen6_pte_t *pt_vaddr = NULL;
1901 struct sgt_iter sgt_iter;
1902 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001903
Dave Gordon85d12252016-05-20 11:54:06 +01001904 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001905 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001906 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001907
Chris Wilsoncc797142013-12-31 15:50:30 +00001908 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001909 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301910
Michel Thierry07749ef2015-03-16 16:00:54 +00001911 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001912 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001913 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001914 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001915 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001916 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001917 }
Dave Gordon85d12252016-05-20 11:54:06 +01001918
Chris Wilsoncc797142013-12-31 15:50:30 +00001919 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001920 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001921}
1922
Ben Widawsky678d96f2015-03-16 16:00:56 +00001923static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001924 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001925{
Michel Thierry4933d512015-03-24 15:46:22 +00001926 DECLARE_BITMAP(new_page_tables, I915_PDES);
1927 struct drm_device *dev = vm->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001928 struct drm_i915_private *dev_priv = to_i915(dev);
1929 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001930 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001931 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001932 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001933 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001934 int ret;
1935
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001936 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1937 return -ENODEV;
1938
1939 start = start_save = start_in;
1940 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001941
1942 bitmap_zero(new_page_tables, I915_PDES);
1943
1944 /* The allocation is done in two stages so that we can bail out with
1945 * minimal amount of pain. The first stage finds new page tables that
1946 * need allocation. The second stage marks use ptes within the page
1947 * tables.
1948 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001949 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001950 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001951 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1952 continue;
1953 }
1954
1955 /* We've already allocated a page table */
1956 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1957
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001958 pt = alloc_pt(dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001959 if (IS_ERR(pt)) {
1960 ret = PTR_ERR(pt);
1961 goto unwind_out;
1962 }
1963
1964 gen6_initialize_pt(vm, pt);
1965
1966 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001967 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001968 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001969 }
1970
1971 start = start_save;
1972 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001973
Dave Gordon731f74c2016-06-24 19:37:46 +01001974 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001975 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1976
1977 bitmap_zero(tmp_bitmap, GEN6_PTES);
1978 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1979 gen6_pte_count(start, length));
1980
Mika Kuoppala966082c2015-06-25 18:35:19 +03001981 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001982 gen6_write_pde(&ppgtt->pd, pde, pt);
1983
Michel Thierry72744cb2015-03-24 15:46:23 +00001984 trace_i915_page_table_entry_map(vm, pde, pt,
1985 gen6_pte_index(start),
1986 gen6_pte_count(start, length),
1987 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001988 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001989 GEN6_PTES);
1990 }
1991
Michel Thierry4933d512015-03-24 15:46:22 +00001992 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1993
1994 /* Make sure write is complete before other code can use this page
1995 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001996 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001997
Ben Widawsky563222a2015-03-19 12:53:28 +00001998 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001999 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002000
2001unwind_out:
2002 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002003 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002004
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002005 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03002006 free_pt(vm->dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002007 }
2008
2009 mark_tlbs_dirty(ppgtt);
2010 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002011}
2012
Mika Kuoppala8776f022015-06-30 18:16:40 +03002013static int gen6_init_scratch(struct i915_address_space *vm)
2014{
2015 struct drm_device *dev = vm->dev;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002016 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002017
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002018 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002019 if (ret)
2020 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002021
2022 vm->scratch_pt = alloc_pt(dev);
2023 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002024 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002025 return PTR_ERR(vm->scratch_pt);
2026 }
2027
2028 gen6_initialize_pt(vm, vm->scratch_pt);
2029
2030 return 0;
2031}
2032
2033static void gen6_free_scratch(struct i915_address_space *vm)
2034{
2035 struct drm_device *dev = vm->dev;
2036
2037 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002038 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002039}
2040
Daniel Vetter061dd492015-04-14 17:35:13 +02002041static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002042{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002043 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002044 struct i915_page_directory *pd = &ppgtt->pd;
2045 struct drm_device *dev = vm->dev;
Michel Thierry09942c62015-04-08 12:13:30 +01002046 struct i915_page_table *pt;
2047 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002048
Daniel Vetter061dd492015-04-14 17:35:13 +02002049 drm_mm_remove_node(&ppgtt->node);
2050
Dave Gordon731f74c2016-06-24 19:37:46 +01002051 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002052 if (pt != vm->scratch_pt)
Dave Gordon731f74c2016-06-24 19:37:46 +01002053 free_pt(dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002054
Mika Kuoppala8776f022015-06-30 18:16:40 +03002055 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002056}
2057
Ben Widawskyb1465202014-02-19 22:05:49 -08002058static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002059{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002060 struct i915_address_space *vm = &ppgtt->base;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002061 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002062 struct drm_i915_private *dev_priv = to_i915(dev);
2063 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002064 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002065 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002066
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002067 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2068 * allocator works in address space sizes, so it's multiplied by page
2069 * size. We allocate at the top of the GTT to avoid fragmentation.
2070 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002071 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002072
Mika Kuoppala8776f022015-06-30 18:16:40 +03002073 ret = gen6_init_scratch(vm);
2074 if (ret)
2075 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002076
Ben Widawskye3cc1992013-12-06 14:11:08 -08002077alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002078 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002079 &ppgtt->node, GEN6_PD_SIZE,
2080 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002081 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002082 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002083 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002084 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002085 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002086 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002087 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002088 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002089 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002090 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002091
2092 retried = true;
2093 goto alloc;
2094 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002095
Ben Widawskyc8c26622015-01-22 17:01:25 +00002096 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002097 goto err_out;
2098
Ben Widawskyc8c26622015-01-22 17:01:25 +00002099
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002100 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002101 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002102
Ben Widawskyc8c26622015-01-22 17:01:25 +00002103 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002104
2105err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002106 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002107 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002108}
2109
Ben Widawskyb1465202014-02-19 22:05:49 -08002110static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2111{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002112 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002113}
2114
Michel Thierry4933d512015-03-24 15:46:22 +00002115static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2116 uint64_t start, uint64_t length)
2117{
Michel Thierryec565b32015-04-08 12:13:23 +01002118 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002119 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002120
Dave Gordon731f74c2016-06-24 19:37:46 +01002121 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002122 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002123}
2124
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002125static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002126{
2127 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002128 struct drm_i915_private *dev_priv = to_i915(dev);
2129 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002130 int ret;
2131
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002132 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002133 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002134 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002135 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002136 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002137 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002138 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002139 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002140 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002141
2142 ret = gen6_ppgtt_alloc(ppgtt);
2143 if (ret)
2144 return ret;
2145
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002146 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002147 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2148 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002149 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2150 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002151 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002152 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002153 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002154 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002155
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002156 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002157 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002159 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002160 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002161
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002162 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002163
Ben Widawsky678d96f2015-03-16 16:00:56 +00002164 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2165
Thierry Reding440fd522015-01-23 09:05:06 +01002166 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002167 ppgtt->node.size >> 20,
2168 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002169
Daniel Vetterfa76da32014-08-06 20:19:54 +02002170 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002171 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002172
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002173 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002174}
2175
Chris Wilson2bfa9962016-08-04 07:52:25 +01002176static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2177 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002178{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002179 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002180
Chris Wilson2bfa9962016-08-04 07:52:25 +01002181 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002182 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002183 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002184 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002185}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002186
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002187static void i915_address_space_init(struct i915_address_space *vm,
2188 struct drm_i915_private *dev_priv)
2189{
2190 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002191 INIT_LIST_HEAD(&vm->active_list);
2192 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002193 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002194 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2195}
2196
Tim Gored5165eb2016-02-04 11:49:34 +00002197static void gtt_write_workarounds(struct drm_device *dev)
2198{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002199 struct drm_i915_private *dev_priv = to_i915(dev);
Tim Gored5165eb2016-02-04 11:49:34 +00002200
2201 /* This function is for gtt related workarounds. This function is
2202 * called on driver load and after a GPU reset, so you can place
2203 * workarounds here even if they get overwritten by GPU reset.
2204 */
2205 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002206 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002207 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002208 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002209 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002210 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002211 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002212 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002213 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2214}
2215
Chris Wilson2bfa9962016-08-04 07:52:25 +01002216static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2217 struct drm_i915_private *dev_priv,
2218 struct drm_i915_file_private *file_priv)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002219{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002220 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002221
Chris Wilson2bfa9962016-08-04 07:52:25 +01002222 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002223 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002224 kref_init(&ppgtt->ref);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002225 i915_address_space_init(&ppgtt->base, dev_priv);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002226 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002227 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002228
2229 return ret;
2230}
2231
Daniel Vetter82460d92014-08-06 20:19:53 +02002232int i915_ppgtt_init_hw(struct drm_device *dev)
2233{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002234 struct drm_i915_private *dev_priv = to_i915(dev);
2235
Tim Gored5165eb2016-02-04 11:49:34 +00002236 gtt_write_workarounds(dev);
2237
Thomas Daniel671b50132014-08-20 16:24:50 +01002238 /* In the case of execlists, PPGTT is enabled by the context descriptor
2239 * and the PDPs are contained within the context itself. We don't
2240 * need to do anything here. */
2241 if (i915.enable_execlists)
2242 return 0;
2243
Daniel Vetter82460d92014-08-06 20:19:53 +02002244 if (!USES_PPGTT(dev))
2245 return 0;
2246
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002247 if (IS_GEN6(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002248 gen6_ppgtt_enable(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002249 else if (IS_GEN7(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002250 gen7_ppgtt_enable(dev);
2251 else if (INTEL_INFO(dev)->gen >= 8)
2252 gen8_ppgtt_enable(dev);
2253 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01002254 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02002255
John Harrison4ad2fd82015-06-18 13:11:20 +01002256 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002257}
John Harrison4ad2fd82015-06-18 13:11:20 +01002258
Daniel Vetter4d884702014-08-06 15:04:47 +02002259struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002260i915_ppgtt_create(struct drm_i915_private *dev_priv,
2261 struct drm_i915_file_private *fpriv)
Daniel Vetter4d884702014-08-06 15:04:47 +02002262{
2263 struct i915_hw_ppgtt *ppgtt;
2264 int ret;
2265
2266 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2267 if (!ppgtt)
2268 return ERR_PTR(-ENOMEM);
2269
Chris Wilson2bfa9962016-08-04 07:52:25 +01002270 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv);
Daniel Vetter4d884702014-08-06 15:04:47 +02002271 if (ret) {
2272 kfree(ppgtt);
2273 return ERR_PTR(ret);
2274 }
2275
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002276 trace_i915_ppgtt_create(&ppgtt->base);
2277
Daniel Vetter4d884702014-08-06 15:04:47 +02002278 return ppgtt;
2279}
2280
Daniel Vetteree960be2014-08-06 15:04:45 +02002281void i915_ppgtt_release(struct kref *kref)
2282{
2283 struct i915_hw_ppgtt *ppgtt =
2284 container_of(kref, struct i915_hw_ppgtt, ref);
2285
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002286 trace_i915_ppgtt_release(&ppgtt->base);
2287
Chris Wilson50e046b2016-08-04 07:52:46 +01002288 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002289 WARN_ON(!list_empty(&ppgtt->base.active_list));
2290 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002291 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002292
Daniel Vetter19dd1202014-08-06 15:04:55 +02002293 list_del(&ppgtt->base.global_link);
2294 drm_mm_takedown(&ppgtt->base.mm);
2295
Daniel Vetteree960be2014-08-06 15:04:45 +02002296 ppgtt->base.cleanup(&ppgtt->base);
2297 kfree(ppgtt);
2298}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002299
Ben Widawskya81cc002013-01-18 12:30:31 -08002300/* Certain Gen5 chipsets require require idling the GPU before
2301 * unmapping anything from the GTT when VT-d is enabled.
2302 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002303static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002304{
2305#ifdef CONFIG_INTEL_IOMMU
2306 /* Query intel_iommu to see if we need the workaround. Presumably that
2307 * was loaded first.
2308 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002309 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002310 return true;
2311#endif
2312 return false;
2313}
2314
Chris Wilsondc979972016-05-10 14:10:04 +01002315void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002316{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002317 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302318 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002319
Chris Wilsondc979972016-05-10 14:10:04 +01002320 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002321 return;
2322
Akash Goel3b3f1652016-10-13 22:44:48 +05302323 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002324 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002325 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002326 if (fault_reg & RING_FAULT_VALID) {
2327 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002328 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002329 "\tAddress space: %s\n"
2330 "\tSource ID: %d\n"
2331 "\tType: %d\n",
2332 fault_reg & PAGE_MASK,
2333 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2334 RING_FAULT_SRCID(fault_reg),
2335 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002336 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002337 fault_reg & ~RING_FAULT_VALID);
2338 }
2339 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302340
2341 /* Engine specific init may not have been done till this point. */
2342 if (dev_priv->engine[RCS])
2343 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002344}
2345
Chris Wilson91e56492014-09-25 10:13:12 +01002346static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2347{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002348 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002349 intel_gtt_chipset_flush();
2350 } else {
2351 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2352 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2353 }
2354}
2355
Ben Widawsky828c7902013-10-16 09:21:30 -07002356void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2357{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002358 struct drm_i915_private *dev_priv = to_i915(dev);
2359 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002360
2361 /* Don't bother messing with faults pre GEN6 as we have little
2362 * documentation supporting that it's a good idea.
2363 */
2364 if (INTEL_INFO(dev)->gen < 6)
2365 return;
2366
Chris Wilsondc979972016-05-10 14:10:04 +01002367 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002368
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002369 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002370
2371 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002372}
2373
Chris Wilson03ac84f2016-10-28 13:58:36 +01002374int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2375 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002376{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002377 if (dma_map_sg(&obj->base.dev->pdev->dev,
2378 pages->sgl, pages->nents,
2379 PCI_DMA_BIDIRECTIONAL))
2380 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01002381
Chris Wilson03ac84f2016-10-28 13:58:36 +01002382 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002383}
2384
Daniel Vetter2c642b02015-04-14 17:35:26 +02002385static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002386{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002387 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002388}
2389
Chris Wilsond6473f52016-06-10 14:22:59 +05302390static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2391 dma_addr_t addr,
2392 uint64_t offset,
2393 enum i915_cache_level level,
2394 u32 unused)
2395{
2396 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2397 gen8_pte_t __iomem *pte =
2398 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2399 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302400
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002401 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302402
2403 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2404 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302405}
2406
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002407static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2408 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002409 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302410 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002411{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002412 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002413 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002414 struct sgt_iter sgt_iter;
2415 gen8_pte_t __iomem *gtt_entries;
2416 gen8_pte_t gtt_entry;
2417 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002418 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002419
Dave Gordon85d12252016-05-20 11:54:06 +01002420 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2421
2422 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002423 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002424 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002425 }
2426
2427 /*
2428 * XXX: This serves as a posting read to make sure that the PTE has
2429 * actually been updated. There is some concern that even though
2430 * registers and PTEs are within the same BAR that they are potentially
2431 * of NUMA access patterns. Therefore, even with the way we assume
2432 * hardware should work, we must keep this posting read for paranoia.
2433 */
2434 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002435 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002436
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002437 /* This next bit makes the above posting read even more important. We
2438 * want to flush the TLBs only after we're certain all the PTE updates
2439 * have finished.
2440 */
2441 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2442 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002443}
2444
Chris Wilsonc1403302015-11-18 15:19:39 +00002445struct insert_entries {
2446 struct i915_address_space *vm;
2447 struct sg_table *st;
2448 uint64_t start;
2449 enum i915_cache_level level;
2450 u32 flags;
2451};
2452
2453static int gen8_ggtt_insert_entries__cb(void *_arg)
2454{
2455 struct insert_entries *arg = _arg;
2456 gen8_ggtt_insert_entries(arg->vm, arg->st,
2457 arg->start, arg->level, arg->flags);
2458 return 0;
2459}
2460
2461static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2462 struct sg_table *st,
2463 uint64_t start,
2464 enum i915_cache_level level,
2465 u32 flags)
2466{
2467 struct insert_entries arg = { vm, st, start, level, flags };
2468 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2469}
2470
Chris Wilsond6473f52016-06-10 14:22:59 +05302471static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2472 dma_addr_t addr,
2473 uint64_t offset,
2474 enum i915_cache_level level,
2475 u32 flags)
2476{
2477 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2478 gen6_pte_t __iomem *pte =
2479 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2480 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302481
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002482 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302483
2484 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2485 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302486}
2487
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002488/*
2489 * Binds an object into the global gtt with the specified cache level. The object
2490 * will be accessible to the GPU via commands whose operands reference offsets
2491 * within the global GTT as well as accessible by the GPU through the GMADR
2492 * mapped BAR (dev_priv->mm.gtt->gtt).
2493 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002494static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002495 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002496 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302497 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002498{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002499 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002500 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002501 struct sgt_iter sgt_iter;
2502 gen6_pte_t __iomem *gtt_entries;
2503 gen6_pte_t gtt_entry;
2504 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002505 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002506
Dave Gordon85d12252016-05-20 11:54:06 +01002507 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2508
2509 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002510 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002511 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002512 }
2513
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002514 /* XXX: This serves as a posting read to make sure that the PTE has
2515 * actually been updated. There is some concern that even though
2516 * registers and PTEs are within the same BAR that they are potentially
2517 * of NUMA access patterns. Therefore, even with the way we assume
2518 * hardware should work, we must keep this posting read for paranoia.
2519 */
Dave Gordon85d12252016-05-20 11:54:06 +01002520 if (i != 0)
2521 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002522
2523 /* This next bit makes the above posting read even more important. We
2524 * want to flush the TLBs only after we're certain all the PTE updates
2525 * have finished.
2526 */
2527 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2528 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002529}
2530
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002531static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002532 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002533{
2534}
2535
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002536static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002537 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002538{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002539 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002540 unsigned first_entry = start >> PAGE_SHIFT;
2541 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002542 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002543 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2544 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002545 int i;
2546
2547 if (WARN(num_entries > max_entries,
2548 "First entry = %d; Num entries = %d (max=%d)\n",
2549 first_entry, num_entries, max_entries))
2550 num_entries = max_entries;
2551
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002552 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002553 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002554 for (i = 0; i < num_entries; i++)
2555 gen8_set_pte(&gtt_base[i], scratch_pte);
2556 readl(gtt_base);
2557}
2558
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002559static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002560 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002561 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002562{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002563 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002564 unsigned first_entry = start >> PAGE_SHIFT;
2565 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002566 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002567 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2568 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002569 int i;
2570
2571 if (WARN(num_entries > max_entries,
2572 "First entry = %d; Num entries = %d (max=%d)\n",
2573 first_entry, num_entries, max_entries))
2574 num_entries = max_entries;
2575
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002576 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002577 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002578
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002579 for (i = 0; i < num_entries; i++)
2580 iowrite32(scratch_pte, &gtt_base[i]);
2581 readl(gtt_base);
2582}
2583
Chris Wilsond6473f52016-06-10 14:22:59 +05302584static void i915_ggtt_insert_page(struct i915_address_space *vm,
2585 dma_addr_t addr,
2586 uint64_t offset,
2587 enum i915_cache_level cache_level,
2588 u32 unused)
2589{
Chris Wilsond6473f52016-06-10 14:22:59 +05302590 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2591 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302592
2593 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302594}
2595
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002596static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2597 struct sg_table *pages,
2598 uint64_t start,
2599 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002600{
2601 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2602 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2603
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002604 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002605
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002606}
2607
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002608static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002609 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002610 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002611{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002612 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002613}
2614
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002615static int ggtt_bind_vma(struct i915_vma *vma,
2616 enum i915_cache_level cache_level,
2617 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002618{
Chris Wilson9c870d02016-10-24 13:42:15 +01002619 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Daniel Vetter0a878712015-10-15 14:23:01 +02002620 struct drm_i915_gem_object *obj = vma->obj;
2621 u32 pte_flags = 0;
2622 int ret;
2623
2624 ret = i915_get_ggtt_vma_pages(vma);
2625 if (ret)
2626 return ret;
2627
2628 /* Currently applicable only to VLV */
2629 if (obj->gt_ro)
2630 pte_flags |= PTE_READ_ONLY;
2631
Chris Wilson9c870d02016-10-24 13:42:15 +01002632 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002633 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002634 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002635 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002636
2637 /*
2638 * Without aliasing PPGTT there's no difference between
2639 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2640 * upgrade to both bound if we bind either to avoid double-binding.
2641 */
Chris Wilson3272db52016-08-04 16:32:32 +01002642 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002643
2644 return 0;
2645}
2646
2647static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2648 enum i915_cache_level cache_level,
2649 u32 flags)
2650{
Chris Wilson9c870d02016-10-24 13:42:15 +01002651 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Chris Wilson321d1782015-11-20 10:27:18 +00002652 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002653 int ret;
2654
2655 ret = i915_get_ggtt_vma_pages(vma);
2656 if (ret)
2657 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002658
Akash Goel24f3a8c2014-06-17 10:59:42 +05302659 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002660 pte_flags = 0;
2661 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002662 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302663
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002664
Chris Wilson3272db52016-08-04 16:32:32 +01002665 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002666 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002667 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002668 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002669 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002670 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002671 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002672
Chris Wilson3272db52016-08-04 16:32:32 +01002673 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002674 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002675 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002676 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002677 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002678 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002679
2680 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002681}
2682
2683static void ggtt_unbind_vma(struct i915_vma *vma)
2684{
Chris Wilson9c870d02016-10-24 13:42:15 +01002685 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2686 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002687 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002688
Chris Wilson9c870d02016-10-24 13:42:15 +01002689 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2690 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002691 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002692 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002693 intel_runtime_pm_put(i915);
2694 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002695
Chris Wilson3272db52016-08-04 16:32:32 +01002696 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002697 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002698 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002699}
2700
Chris Wilson03ac84f2016-10-28 13:58:36 +01002701void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2702 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002703{
David Weinehall52a05c32016-08-22 13:32:44 +03002704 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2705 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002706 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002707
Chris Wilson307dc252016-08-05 10:14:12 +01002708 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002709 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002710 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2711 /* Wait a bit, in hopes it avoids the hang */
2712 udelay(10);
2713 }
2714 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002715
Chris Wilson03ac84f2016-10-28 13:58:36 +01002716 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002717}
Daniel Vetter644ec022012-03-26 09:45:40 +02002718
Chris Wilson42d6ab42012-07-26 11:49:32 +01002719static void i915_gtt_color_adjust(struct drm_mm_node *node,
2720 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002721 u64 *start,
2722 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002723{
2724 if (node->color != color)
2725 *start += 4096;
2726
Chris Wilson2a1d7752016-07-26 12:01:51 +01002727 node = list_first_entry_or_null(&node->node_list,
2728 struct drm_mm_node,
2729 node_list);
2730 if (node && node->allocated && node->color != color)
2731 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002732}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002733
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002734int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002735{
Ben Widawskye78891c2013-01-25 16:41:04 -08002736 /* Let GEM Manage all of the aperture.
2737 *
2738 * However, leave one page at the end still bound to the scratch page.
2739 * There are a number of places where the hardware apparently prefetches
2740 * past the end of the object, and we've seen multiple hangs with the
2741 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2742 * aperture. One page should be enough to keep any prefetching inside
2743 * of the aperture.
2744 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002745 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002746 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002747 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002748 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002749 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002750
Zhi Wangb02d22a2016-06-16 08:06:59 -04002751 ret = intel_vgt_balloon(dev_priv);
2752 if (ret)
2753 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002754
Chris Wilson95374d72016-10-12 10:05:20 +01002755 /* Reserve a mappable slot for our lockless error capture */
2756 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2757 &ggtt->error_capture,
2758 4096, 0, -1,
2759 0, ggtt->mappable_end,
2760 0, 0);
2761 if (ret)
2762 return ret;
2763
Chris Wilsoned2f3452012-11-15 11:32:19 +00002764 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002765 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002766 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2767 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002768 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002769 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002770 }
2771
2772 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002773 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002774 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002775
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002776 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002777 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002778 if (!ppgtt) {
2779 ret = -ENOMEM;
2780 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002781 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002782
Chris Wilson95374d72016-10-12 10:05:20 +01002783 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2784 if (ret)
2785 goto err_ppgtt;
2786
2787 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002788 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2789 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002790 if (ret)
2791 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002792 }
2793
2794 ppgtt->base.clear_range(&ppgtt->base,
2795 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002796 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002797
Daniel Vetterfa76da32014-08-06 20:19:54 +02002798 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002799 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2800 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002801 }
2802
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002803 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002804
2805err_ppgtt_cleanup:
2806 ppgtt->base.cleanup(&ppgtt->base);
2807err_ppgtt:
2808 kfree(ppgtt);
2809err:
2810 drm_mm_remove_node(&ggtt->error_capture);
2811 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002812}
2813
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002814/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002815 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002816 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002817 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002818void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002819{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002820 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002821
Daniel Vetter70e32542014-08-06 15:04:57 +02002822 if (dev_priv->mm.aliasing_ppgtt) {
2823 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002824 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002825 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002826 }
2827
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002828 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002829
Chris Wilson95374d72016-10-12 10:05:20 +01002830 if (drm_mm_node_allocated(&ggtt->error_capture))
2831 drm_mm_remove_node(&ggtt->error_capture);
2832
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002833 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002834 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002835
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002836 drm_mm_takedown(&ggtt->base.mm);
2837 list_del(&ggtt->base.global_link);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002838 }
2839
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002840 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002841
2842 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002843 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002844}
Daniel Vetter70e32542014-08-06 15:04:57 +02002845
Daniel Vetter2c642b02015-04-14 17:35:26 +02002846static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002847{
2848 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2849 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2850 return snb_gmch_ctl << 20;
2851}
2852
Daniel Vetter2c642b02015-04-14 17:35:26 +02002853static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002854{
2855 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2856 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2857 if (bdw_gmch_ctl)
2858 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002859
2860#ifdef CONFIG_X86_32
2861 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2862 if (bdw_gmch_ctl > 4)
2863 bdw_gmch_ctl = 4;
2864#endif
2865
Ben Widawsky9459d252013-11-03 16:53:55 -08002866 return bdw_gmch_ctl << 20;
2867}
2868
Daniel Vetter2c642b02015-04-14 17:35:26 +02002869static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002870{
2871 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2872 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2873
2874 if (gmch_ctrl)
2875 return 1 << (20 + gmch_ctrl);
2876
2877 return 0;
2878}
2879
Daniel Vetter2c642b02015-04-14 17:35:26 +02002880static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002881{
2882 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2883 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2884 return snb_gmch_ctl << 25; /* 32 MB units */
2885}
2886
Daniel Vetter2c642b02015-04-14 17:35:26 +02002887static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002888{
2889 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2890 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2891 return bdw_gmch_ctl << 25; /* 32 MB units */
2892}
2893
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002894static size_t chv_get_stolen_size(u16 gmch_ctrl)
2895{
2896 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2897 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2898
2899 /*
2900 * 0x0 to 0x10: 32MB increments starting at 0MB
2901 * 0x11 to 0x16: 4MB increments starting at 8MB
2902 * 0x17 to 0x1d: 4MB increments start at 36MB
2903 */
2904 if (gmch_ctrl < 0x11)
2905 return gmch_ctrl << 25;
2906 else if (gmch_ctrl < 0x17)
2907 return (gmch_ctrl - 0x11 + 2) << 22;
2908 else
2909 return (gmch_ctrl - 0x17 + 9) << 22;
2910}
2911
Damien Lespiau66375012014-01-09 18:02:46 +00002912static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2913{
2914 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2915 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2916
2917 if (gen9_gmch_ctl < 0xf0)
2918 return gen9_gmch_ctl << 25; /* 32 MB units */
2919 else
2920 /* 4MB increments starting at 0xf0 for 4MB */
2921 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2922}
2923
Chris Wilson34c998b2016-08-04 07:52:24 +01002924static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002925{
Chris Wilson34c998b2016-08-04 07:52:24 +01002926 struct pci_dev *pdev = ggtt->base.dev->pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002927 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002928 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002929
2930 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002931 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002932
Imre Deak2a073f892015-03-27 13:07:33 +02002933 /*
2934 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2935 * dropped. For WC mappings in general we have 64 byte burst writes
2936 * when the WC buffer is flushed, so we can't use it, but have to
2937 * resort to an uncached mapping. The WC issue is easily caught by the
2938 * readback check when writing GTT PTE entries.
2939 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002940 if (IS_BROXTON(to_i915(ggtt->base.dev)))
Chris Wilson34c998b2016-08-04 07:52:24 +01002941 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002942 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002943 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002944 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002945 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002946 return -ENOMEM;
2947 }
2948
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002949 ret = setup_scratch_page(ggtt->base.dev,
2950 &ggtt->base.scratch_page,
2951 GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002952 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002953 DRM_ERROR("Scratch setup failed\n");
2954 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002955 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002956 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002957 }
2958
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002959 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002960}
2961
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002962/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2963 * bits. When using advanced contexts each context stores its own PAT, but
2964 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002965static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002966{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002967 uint64_t pat;
2968
2969 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2970 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2971 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2972 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2973 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2974 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2975 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2976 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2977
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002978 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002979 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2980 * so RTL will always use the value corresponding to
2981 * pat_sel = 000".
2982 * So let's disable cache for GGTT to avoid screen corruptions.
2983 * MOCS still can be used though.
2984 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2985 * before this patch, i.e. the same uncached + snooping access
2986 * like on gen6/7 seems to be in effect.
2987 * - So this just fixes blitter/render access. Again it looks
2988 * like it's not just uncached access, but uncached + snooping.
2989 * So we can still hold onto all our assumptions wrt cpu
2990 * clflushing on LLC machines.
2991 */
2992 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2993
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002994 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2995 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002996 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2997 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002998}
2999
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003000static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3001{
3002 uint64_t pat;
3003
3004 /*
3005 * Map WB on BDW to snooped on CHV.
3006 *
3007 * Only the snoop bit has meaning for CHV, the rest is
3008 * ignored.
3009 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003010 * The hardware will never snoop for certain types of accesses:
3011 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3012 * - PPGTT page tables
3013 * - some other special cycles
3014 *
3015 * As with BDW, we also need to consider the following for GT accesses:
3016 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3017 * so RTL will always use the value corresponding to
3018 * pat_sel = 000".
3019 * Which means we must set the snoop bit in PAT entry 0
3020 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003021 */
3022 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3023 GEN8_PPAT(1, 0) |
3024 GEN8_PPAT(2, 0) |
3025 GEN8_PPAT(3, 0) |
3026 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3027 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3028 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3029 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3030
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003031 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3032 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003033}
3034
Chris Wilson34c998b2016-08-04 07:52:24 +01003035static void gen6_gmch_remove(struct i915_address_space *vm)
3036{
3037 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3038
3039 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01003040 cleanup_scratch_page(vm->dev, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003041}
3042
Joonas Lahtinend507d732016-03-18 10:42:58 +02003043static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003044{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003045 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3046 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003047 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003048 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003049
3050 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003051 ggtt->mappable_base = pci_resource_start(pdev, 2);
3052 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003053
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003054 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3055 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003056
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003057 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003058
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003059 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003060 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003061 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003062 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003063 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003064 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003065 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003066 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003067 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003068 }
Ben Widawsky63340132013-11-04 19:32:22 -08003069
Chris Wilson34c998b2016-08-04 07:52:24 +01003070 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003071
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003072 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003073 chv_setup_private_ppat(dev_priv);
3074 else
3075 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003076
Chris Wilson34c998b2016-08-04 07:52:24 +01003077 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003078 ggtt->base.bind_vma = ggtt_bind_vma;
3079 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303080 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003081 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003082 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003083 ggtt->base.clear_range = gen8_ggtt_clear_range;
3084
3085 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3086 if (IS_CHERRYVIEW(dev_priv))
3087 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3088
Chris Wilson34c998b2016-08-04 07:52:24 +01003089 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003090}
3091
Joonas Lahtinend507d732016-03-18 10:42:58 +02003092static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003093{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003094 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3095 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003096 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003097 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003098
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003099 ggtt->mappable_base = pci_resource_start(pdev, 2);
3100 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003101
Ben Widawskybaa09f52013-01-24 13:49:57 -08003102 /* 64/512MB is the current min/max we actually know of, but this is just
3103 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003104 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003105 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003106 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003107 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003108 }
3109
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003110 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3111 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3112 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003113
Joonas Lahtinend507d732016-03-18 10:42:58 +02003114 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003115
Chris Wilson34c998b2016-08-04 07:52:24 +01003116 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3117 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003118
Joonas Lahtinend507d732016-03-18 10:42:58 +02003119 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303120 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003121 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3122 ggtt->base.bind_vma = ggtt_bind_vma;
3123 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003124 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003125
Chris Wilson34c998b2016-08-04 07:52:24 +01003126 if (HAS_EDRAM(dev_priv))
3127 ggtt->base.pte_encode = iris_pte_encode;
3128 else if (IS_HASWELL(dev_priv))
3129 ggtt->base.pte_encode = hsw_pte_encode;
3130 else if (IS_VALLEYVIEW(dev_priv))
3131 ggtt->base.pte_encode = byt_pte_encode;
3132 else if (INTEL_GEN(dev_priv) >= 7)
3133 ggtt->base.pte_encode = ivb_pte_encode;
3134 else
3135 ggtt->base.pte_encode = snb_pte_encode;
3136
3137 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003138}
3139
Chris Wilson34c998b2016-08-04 07:52:24 +01003140static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003141{
Chris Wilson34c998b2016-08-04 07:52:24 +01003142 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003143}
3144
Joonas Lahtinend507d732016-03-18 10:42:58 +02003145static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003146{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003147 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003148 int ret;
3149
Chris Wilson91c8a322016-07-05 10:40:23 +01003150 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003151 if (!ret) {
3152 DRM_ERROR("failed to set up gmch\n");
3153 return -EIO;
3154 }
3155
Joonas Lahtinend507d732016-03-18 10:42:58 +02003156 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3157 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003158
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003159 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303160 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003161 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3162 ggtt->base.clear_range = i915_ggtt_clear_range;
3163 ggtt->base.bind_vma = ggtt_bind_vma;
3164 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003165 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003166
Joonas Lahtinend507d732016-03-18 10:42:58 +02003167 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003168 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3169
Ben Widawskybaa09f52013-01-24 13:49:57 -08003170 return 0;
3171}
3172
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003173/**
Chris Wilson0088e522016-08-04 07:52:21 +01003174 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003175 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003176 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003177int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003178{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003179 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003180 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003181
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003182 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003183
Chris Wilson34c998b2016-08-04 07:52:24 +01003184 if (INTEL_GEN(dev_priv) <= 5)
3185 ret = i915_gmch_probe(ggtt);
3186 else if (INTEL_GEN(dev_priv) < 8)
3187 ret = gen6_gmch_probe(ggtt);
3188 else
3189 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003190 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003191 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003192
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003193 if ((ggtt->base.total - 1) >> 32) {
3194 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003195 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003196 ggtt->base.total >> 20);
3197 ggtt->base.total = 1ULL << 32;
3198 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3199 }
3200
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003201 if (ggtt->mappable_end > ggtt->base.total) {
3202 DRM_ERROR("mappable aperture extends past end of GGTT,"
3203 " aperture=%llx, total=%llx\n",
3204 ggtt->mappable_end, ggtt->base.total);
3205 ggtt->mappable_end = ggtt->base.total;
3206 }
3207
Ben Widawskybaa09f52013-01-24 13:49:57 -08003208 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003209 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003210 ggtt->base.total >> 20);
3211 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3212 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003213#ifdef CONFIG_INTEL_IOMMU
3214 if (intel_iommu_gfx_mapped)
3215 DRM_INFO("VT-d active for gfx access\n");
3216#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003217
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003218 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003219}
3220
3221/**
3222 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003223 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003224 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003225int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003226{
Chris Wilson0088e522016-08-04 07:52:21 +01003227 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3228 int ret;
3229
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003230 INIT_LIST_HEAD(&dev_priv->vm_list);
3231
3232 /* Subtract the guard page before address space initialization to
3233 * shrink the range used by drm_mm.
3234 */
3235 ggtt->base.total -= PAGE_SIZE;
3236 i915_address_space_init(&ggtt->base, dev_priv);
3237 ggtt->base.total += PAGE_SIZE;
3238 if (!HAS_LLC(dev_priv))
3239 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
3240
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003241 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3242 dev_priv->ggtt.mappable_base,
3243 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003244 ret = -EIO;
3245 goto out_gtt_cleanup;
3246 }
3247
3248 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3249
Chris Wilson0088e522016-08-04 07:52:21 +01003250 /*
3251 * Initialise stolen early so that we may reserve preallocated
3252 * objects for the BIOS to KMS transition.
3253 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003254 ret = i915_gem_init_stolen(&dev_priv->drm);
Chris Wilson0088e522016-08-04 07:52:21 +01003255 if (ret)
3256 goto out_gtt_cleanup;
3257
3258 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003259
3260out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003261 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003262 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003263}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003264
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003265int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003266{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003267 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003268 return -EIO;
3269
3270 return 0;
3271}
3272
Daniel Vetterfa423312015-04-14 17:35:23 +02003273void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3274{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003275 struct drm_i915_private *dev_priv = to_i915(dev);
3276 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003277 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003278
Chris Wilsondc979972016-05-10 14:10:04 +01003279 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003280
3281 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003282 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003283
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003284 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3285
3286 /* clflush objects bound into the GGTT and rebind them. */
3287 list_for_each_entry_safe(obj, on,
3288 &dev_priv->mm.bound_list, global_list) {
3289 bool ggtt_bound = false;
3290 struct i915_vma *vma;
3291
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003292 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003293 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003294 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003295
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003296 if (!i915_vma_unbind(vma))
3297 continue;
3298
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003299 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3300 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003301 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003302 }
3303
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003304 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003305 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003306 }
3307
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003308 ggtt->base.closed = false;
3309
Daniel Vetterfa423312015-04-14 17:35:23 +02003310 if (INTEL_INFO(dev)->gen >= 8) {
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01003311 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003312 chv_setup_private_ppat(dev_priv);
3313 else
3314 bdw_setup_private_ppat(dev_priv);
3315
3316 return;
3317 }
3318
3319 if (USES_PPGTT(dev)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003320 struct i915_address_space *vm;
3321
Daniel Vetterfa423312015-04-14 17:35:23 +02003322 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3323 /* TODO: Perhaps it shouldn't be gen6 specific */
3324
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003325 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003326
Chris Wilson2bfa9962016-08-04 07:52:25 +01003327 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003328 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003329 else
3330 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003331
3332 gen6_write_page_range(dev_priv, &ppgtt->pd,
3333 0, ppgtt->base.total);
3334 }
3335 }
3336
3337 i915_ggtt_flush(dev_priv);
3338}
3339
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003340static void
3341i915_vma_retire(struct i915_gem_active *active,
3342 struct drm_i915_gem_request *rq)
3343{
3344 const unsigned int idx = rq->engine->id;
3345 struct i915_vma *vma =
3346 container_of(active, struct i915_vma, last_read[idx]);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003347 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003348
3349 GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
3350
3351 i915_vma_clear_active(vma, idx);
3352 if (i915_vma_is_active(vma))
3353 return;
3354
3355 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Chris Wilson3272db52016-08-04 16:32:32 +01003356 if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003357 WARN_ON(i915_vma_unbind(vma));
Chris Wilsond07f0e52016-10-28 13:58:44 +01003358
3359 GEM_BUG_ON(!i915_gem_object_is_active(obj));
3360 if (--obj->active_count)
3361 return;
3362
3363 /* Bump our place on the bound list to keep it roughly in LRU order
3364 * so that we don't steal from recently used but inactive objects
3365 * (unless we are forced to ofc!)
3366 */
3367 if (obj->bind_count)
3368 list_move_tail(&obj->global_list, &rq->i915->mm.bound_list);
3369
3370 obj->mm.dirty = true; /* be paranoid */
3371
3372 if (i915_gem_object_has_active_reference(obj)) {
3373 i915_gem_object_clear_active_reference(obj);
3374 i915_gem_object_put(obj);
3375 }
3376}
3377
3378static void
3379i915_ggtt_retire__write(struct i915_gem_active *active,
3380 struct drm_i915_gem_request *request)
3381{
3382 struct i915_vma *vma =
3383 container_of(active, struct i915_vma, last_write);
3384
3385 intel_fb_obj_flush(vma->obj, true, ORIGIN_CS);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003386}
3387
3388void i915_vma_destroy(struct i915_vma *vma)
3389{
3390 GEM_BUG_ON(vma->node.allocated);
3391 GEM_BUG_ON(i915_vma_is_active(vma));
Chris Wilson3272db52016-08-04 16:32:32 +01003392 GEM_BUG_ON(!i915_vma_is_closed(vma));
Chris Wilson49ef5292016-08-18 17:17:00 +01003393 GEM_BUG_ON(vma->fence);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003394
3395 list_del(&vma->vm_link);
Chris Wilson3272db52016-08-04 16:32:32 +01003396 if (!i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003397 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
3398
3399 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
3400}
3401
3402void i915_vma_close(struct i915_vma *vma)
3403{
Chris Wilson3272db52016-08-04 16:32:32 +01003404 GEM_BUG_ON(i915_vma_is_closed(vma));
3405 vma->flags |= I915_VMA_CLOSED;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003406
3407 list_del_init(&vma->obj_link);
Chris Wilson20dfbde2016-08-04 16:32:30 +01003408 if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003409 WARN_ON(i915_vma_unbind(vma));
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003410}
3411
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003412static struct i915_vma *
Chris Wilson058d88c2016-08-15 10:49:06 +01003413__i915_vma_create(struct drm_i915_gem_object *obj,
3414 struct i915_address_space *vm,
3415 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003416{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003417 struct i915_vma *vma;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003418 int i;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003419
Chris Wilson50e046b2016-08-04 07:52:46 +01003420 GEM_BUG_ON(vm->closed);
3421
Chris Wilsone20d2ab2015-04-07 16:20:58 +01003422 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003423 if (vma == NULL)
3424 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003425
Ben Widawsky6f65e292013-12-06 14:10:56 -08003426 INIT_LIST_HEAD(&vma->exec_list);
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003427 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
3428 init_request_active(&vma->last_read[i], i915_vma_retire);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003429 init_request_active(&vma->last_write,
3430 i915_is_ggtt(vm) ? i915_ggtt_retire__write : NULL);
Chris Wilson49ef5292016-08-18 17:17:00 +01003431 init_request_active(&vma->last_fence, NULL);
Chris Wilson50e046b2016-08-04 07:52:46 +01003432 list_add(&vma->vm_link, &vm->unbound_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003433 vma->vm = vm;
3434 vma->obj = obj;
Chris Wilsonde180032016-08-04 16:32:29 +01003435 vma->size = obj->base.size;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003436
Chris Wilson058d88c2016-08-15 10:49:06 +01003437 if (view) {
Chris Wilsonde180032016-08-04 16:32:29 +01003438 vma->ggtt_view = *view;
3439 if (view->type == I915_GGTT_VIEW_PARTIAL) {
3440 vma->size = view->params.partial.size;
3441 vma->size <<= PAGE_SHIFT;
3442 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3443 vma->size =
3444 intel_rotation_info_size(&view->params.rotated);
3445 vma->size <<= PAGE_SHIFT;
3446 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003447 }
3448
3449 if (i915_is_ggtt(vm)) {
3450 vma->flags |= I915_VMA_GGTT;
Chris Wilsonde180032016-08-04 16:32:29 +01003451 } else {
Chris Wilson596c5922016-02-26 11:03:20 +00003452 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Chris Wilsonde180032016-08-04 16:32:29 +01003453 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08003454
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003455 list_add_tail(&vma->obj_link, &obj->vma_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003456 return vma;
3457}
3458
Chris Wilson058d88c2016-08-15 10:49:06 +01003459static inline bool vma_matches(struct i915_vma *vma,
3460 struct i915_address_space *vm,
3461 const struct i915_ggtt_view *view)
3462{
3463 if (vma->vm != vm)
3464 return false;
3465
3466 if (!i915_vma_is_ggtt(vma))
3467 return true;
3468
3469 if (!view)
3470 return vma->ggtt_view.type == 0;
3471
3472 if (vma->ggtt_view.type != view->type)
3473 return false;
3474
3475 return memcmp(&vma->ggtt_view.params,
3476 &view->params,
3477 sizeof(view->params)) == 0;
3478}
3479
Ben Widawsky6f65e292013-12-06 14:10:56 -08003480struct i915_vma *
Chris Wilson81a8aa42016-08-15 10:48:48 +01003481i915_vma_create(struct drm_i915_gem_object *obj,
3482 struct i915_address_space *vm,
3483 const struct i915_ggtt_view *view)
3484{
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003485 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson81a8aa42016-08-15 10:48:48 +01003486 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Chris Wilson058d88c2016-08-15 10:49:06 +01003487 GEM_BUG_ON(i915_gem_obj_to_vma(obj, vm, view));
Chris Wilson81a8aa42016-08-15 10:48:48 +01003488
Chris Wilson058d88c2016-08-15 10:49:06 +01003489 return __i915_vma_create(obj, vm, view);
3490}
3491
3492struct i915_vma *
3493i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3494 struct i915_address_space *vm,
3495 const struct i915_ggtt_view *view)
3496{
3497 struct i915_vma *vma;
3498
3499 list_for_each_entry_reverse(vma, &obj->vma_list, obj_link)
3500 if (vma_matches(vma, vm, view))
3501 return vma;
3502
3503 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003504}
3505
3506struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003507i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003508 struct i915_address_space *vm,
3509 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003510{
3511 struct i915_vma *vma;
3512
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003513 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003514 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3515
3516 vma = i915_gem_obj_to_vma(obj, vm, view);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003517 if (!vma)
Chris Wilson058d88c2016-08-15 10:49:06 +01003518 vma = __i915_vma_create(obj, vm, view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003519
Chris Wilson3272db52016-08-04 16:32:32 +01003520 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003521 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003522}
3523
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003524static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003525rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003526 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003527 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003528 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003529{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003530 unsigned int column, row;
3531 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003532
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003533 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003534 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003535 for (row = 0; row < height; row++) {
3536 st->nents++;
3537 /* We don't need the pages, but need to initialize
3538 * the entries so the sg list can be happily traversed.
3539 * The only thing we need are DMA addresses.
3540 */
3541 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003542 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003543 sg_dma_len(sg) = PAGE_SIZE;
3544 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003545 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003546 }
3547 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003548
3549 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003550}
3551
3552static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003553intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003554 struct drm_i915_gem_object *obj)
3555{
Dave Gordon85d12252016-05-20 11:54:06 +01003556 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003557 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003558 struct sgt_iter sgt_iter;
3559 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003560 unsigned long i;
3561 dma_addr_t *page_addr_list;
3562 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003563 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003564 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003565
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003566 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003567 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003568 sizeof(dma_addr_t),
3569 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003570 if (!page_addr_list)
3571 return ERR_PTR(ret);
3572
3573 /* Allocate target SG list. */
3574 st = kmalloc(sizeof(*st), GFP_KERNEL);
3575 if (!st)
3576 goto err_st_alloc;
3577
Ville Syrjälä6687c902015-09-15 13:16:41 +03003578 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003579 if (ret)
3580 goto err_sg_alloc;
3581
3582 /* Populate source page list from the object. */
3583 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003584 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003585 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003586
Dave Gordon85d12252016-05-20 11:54:06 +01003587 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003588 st->nents = 0;
3589 sg = st->sgl;
3590
Ville Syrjälä6687c902015-09-15 13:16:41 +03003591 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3592 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3593 rot_info->plane[i].width, rot_info->plane[i].height,
3594 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003595 }
3596
Ville Syrjälä6687c902015-09-15 13:16:41 +03003597 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3598 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003599
3600 drm_free_large(page_addr_list);
3601
3602 return st;
3603
3604err_sg_alloc:
3605 kfree(st);
3606err_st_alloc:
3607 drm_free_large(page_addr_list);
3608
Ville Syrjälä6687c902015-09-15 13:16:41 +03003609 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3610 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3611
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003612 return ERR_PTR(ret);
3613}
3614
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003615static struct sg_table *
3616intel_partial_pages(const struct i915_ggtt_view *view,
3617 struct drm_i915_gem_object *obj)
3618{
3619 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003620 struct scatterlist *sg, *iter;
3621 unsigned int count = view->params.partial.size;
3622 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003623 int ret = -ENOMEM;
3624
3625 st = kmalloc(sizeof(*st), GFP_KERNEL);
3626 if (!st)
3627 goto err_st_alloc;
3628
Chris Wilsond2a84a72016-10-28 13:58:34 +01003629 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003630 if (ret)
3631 goto err_sg_alloc;
3632
Chris Wilsond2a84a72016-10-28 13:58:34 +01003633 iter = i915_gem_object_get_sg(obj,
3634 view->params.partial.offset,
3635 &offset);
3636 GEM_BUG_ON(!iter);
3637
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003638 sg = st->sgl;
3639 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003640 do {
3641 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003642
Chris Wilsond2a84a72016-10-28 13:58:34 +01003643 len = min(iter->length - (offset << PAGE_SHIFT),
3644 count << PAGE_SHIFT);
3645 sg_set_page(sg, NULL, len, 0);
3646 sg_dma_address(sg) =
3647 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3648 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003649
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003650 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003651 count -= len >> PAGE_SHIFT;
3652 if (count == 0) {
3653 sg_mark_end(sg);
3654 return st;
3655 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003656
Chris Wilsond2a84a72016-10-28 13:58:34 +01003657 sg = __sg_next(sg);
3658 iter = __sg_next(iter);
3659 offset = 0;
3660 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003661
3662err_sg_alloc:
3663 kfree(st);
3664err_st_alloc:
3665 return ERR_PTR(ret);
3666}
3667
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003668static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003669i915_get_ggtt_vma_pages(struct i915_vma *vma)
3670{
3671 int ret = 0;
3672
Chris Wilson247177d2016-08-15 10:48:47 +01003673 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003674 return 0;
3675
3676 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003677 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003678 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003679 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003680 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003681 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003682 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003683 else
3684 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3685 vma->ggtt_view.type);
3686
Chris Wilson247177d2016-08-15 10:48:47 +01003687 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003688 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003689 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003690 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003691 } else if (IS_ERR(vma->pages)) {
3692 ret = PTR_ERR(vma->pages);
3693 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003694 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3695 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003696 }
3697
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003698 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003699}
3700
3701/**
3702 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3703 * @vma: VMA to map
3704 * @cache_level: mapping cache level
3705 * @flags: flags like global or local mapping
3706 *
3707 * DMA addresses are taken from the scatter-gather table of this object (or of
3708 * this VMA in case of non-default GGTT views) and PTE entries set up.
3709 * Note that DMA addresses are also the only part of the SG table we care about.
3710 */
3711int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3712 u32 flags)
3713{
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003714 u32 bind_flags;
Chris Wilson3272db52016-08-04 16:32:32 +01003715 u32 vma_flags;
3716 int ret;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003717
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003718 if (WARN_ON(flags == 0))
3719 return -EINVAL;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003720
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003721 bind_flags = 0;
Daniel Vetter08755462015-04-20 09:04:05 -07003722 if (flags & PIN_GLOBAL)
Chris Wilson3272db52016-08-04 16:32:32 +01003723 bind_flags |= I915_VMA_GLOBAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003724 if (flags & PIN_USER)
Chris Wilson3272db52016-08-04 16:32:32 +01003725 bind_flags |= I915_VMA_LOCAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003726
Chris Wilson3272db52016-08-04 16:32:32 +01003727 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
Daniel Vetter08755462015-04-20 09:04:05 -07003728 if (flags & PIN_UPDATE)
Chris Wilson3272db52016-08-04 16:32:32 +01003729 bind_flags |= vma_flags;
Daniel Vetter08755462015-04-20 09:04:05 -07003730 else
Chris Wilson3272db52016-08-04 16:32:32 +01003731 bind_flags &= ~vma_flags;
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003732 if (bind_flags == 0)
3733 return 0;
3734
Chris Wilson3272db52016-08-04 16:32:32 +01003735 if (vma_flags == 0 && vma->vm->allocate_va_range) {
Chris Wilson596c5922016-02-26 11:03:20 +00003736 trace_i915_va_alloc(vma);
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003737 ret = vma->vm->allocate_va_range(vma->vm,
3738 vma->node.start,
3739 vma->node.size);
3740 if (ret)
3741 return ret;
3742 }
3743
3744 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003745 if (ret)
3746 return ret;
Daniel Vetter08755462015-04-20 09:04:05 -07003747
Chris Wilson3272db52016-08-04 16:32:32 +01003748 vma->flags |= bind_flags;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003749 return 0;
3750}
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003751
Chris Wilson8ef85612016-04-28 09:56:39 +01003752void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
3753{
3754 void __iomem *ptr;
3755
Chris Wilsone5cdb222016-08-15 10:48:56 +01003756 /* Access through the GTT requires the device to be awake. */
3757 assert_rpm_wakelock_held(to_i915(vma->vm->dev));
3758
Chris Wilson8ef85612016-04-28 09:56:39 +01003759 lockdep_assert_held(&vma->vm->dev->struct_mutex);
Chris Wilson05a20d02016-08-18 17:16:55 +01003760 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma)))
Chris Wilson406ea8d2016-07-20 13:31:55 +01003761 return IO_ERR_PTR(-ENODEV);
Chris Wilson8ef85612016-04-28 09:56:39 +01003762
Chris Wilson3272db52016-08-04 16:32:32 +01003763 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
3764 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
Chris Wilson8ef85612016-04-28 09:56:39 +01003765
3766 ptr = vma->iomap;
3767 if (ptr == NULL) {
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003768 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
Chris Wilson8ef85612016-04-28 09:56:39 +01003769 vma->node.start,
3770 vma->node.size);
3771 if (ptr == NULL)
Chris Wilson406ea8d2016-07-20 13:31:55 +01003772 return IO_ERR_PTR(-ENOMEM);
Chris Wilson8ef85612016-04-28 09:56:39 +01003773
3774 vma->iomap = ptr;
3775 }
3776
Chris Wilson20dfbde2016-08-04 16:32:30 +01003777 __i915_vma_pin(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +01003778 return ptr;
3779}
Chris Wilson19880c42016-08-15 10:49:05 +01003780
3781void i915_vma_unpin_and_release(struct i915_vma **p_vma)
3782{
3783 struct i915_vma *vma;
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003784 struct drm_i915_gem_object *obj;
Chris Wilson19880c42016-08-15 10:49:05 +01003785
3786 vma = fetch_and_zero(p_vma);
3787 if (!vma)
3788 return;
3789
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003790 obj = vma->obj;
3791
Chris Wilson19880c42016-08-15 10:49:05 +01003792 i915_vma_unpin(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003793 i915_vma_close(vma);
3794
3795 __i915_gem_object_release_unless_active(obj);
Chris Wilson19880c42016-08-15 10:49:05 +01003796}