blob: dc279ca61974ae9c3ca9a5847dbad1e3474cbe2b [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 num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200720 unsigned int pte = gen8_pte_index(start);
721 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200722 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
Mika Kuoppala37c63932016-11-01 15:27:36 +0200729 GEM_BUG_ON(pte_end > GEN8_PTES);
730
731 bitmap_clear(pt->used_ptes, pte, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000732
Michał Winiarski2ce51792016-10-13 14:02:42 +0200733 if (bitmap_empty(pt->used_ptes, GEN8_PTES)) {
734 free_pt(vm->dev, pt);
735 return true;
736 }
737
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200738 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000739
Mika Kuoppala37c63932016-11-01 15:27:36 +0200740 while (pte < pte_end)
741 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000742
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200743 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200744
745 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200746}
747
Michał Winiarski2ce51792016-10-13 14:02:42 +0200748/* Removes entries from a single page dir, releasing it if it's empty.
749 * Caller can use the return value to update higher-level entries
750 */
751static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200752 struct i915_page_directory *pd,
753 uint64_t start,
754 uint64_t length)
755{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200756 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200757 struct i915_page_table *pt;
758 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200759 gen8_pde_t *pde_vaddr;
760 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
761 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200762
763 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000764 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100765 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000766
Michał Winiarski2ce51792016-10-13 14:02:42 +0200767 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
768 __clear_bit(pde, pd->used_pdes);
769 pde_vaddr = kmap_px(pd);
770 pde_vaddr[pde] = scratch_pde;
771 kunmap_px(ppgtt, pde_vaddr);
772 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200773 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200774
775 if (bitmap_empty(pd->used_pdes, I915_PDES)) {
776 free_pd(vm->dev, pd);
777 return true;
778 }
779
780 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200781}
Ben Widawsky06fda602015-02-24 16:22:36 +0000782
Michał Winiarski2ce51792016-10-13 14:02:42 +0200783/* Removes entries from a single page dir pointer, releasing it if it's empty.
784 * Caller can use the return value to update higher-level entries
785 */
786static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200787 struct i915_page_directory_pointer *pdp,
788 uint64_t start,
789 uint64_t length)
790{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200791 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200792 struct i915_page_directory *pd;
793 uint64_t pdpe;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200794 gen8_ppgtt_pdpe_t *pdpe_vaddr;
795 gen8_ppgtt_pdpe_t scratch_pdpe =
796 gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200797
798 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
799 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100800 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000801
Michał Winiarski2ce51792016-10-13 14:02:42 +0200802 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
803 __clear_bit(pdpe, pdp->used_pdpes);
804 if (USES_FULL_48BIT_PPGTT(vm->dev)) {
805 pdpe_vaddr = kmap_px(pdp);
806 pdpe_vaddr[pdpe] = scratch_pdpe;
807 kunmap_px(ppgtt, pdpe_vaddr);
808 }
809 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200810 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200811
812 if (USES_FULL_48BIT_PPGTT(vm->dev) &&
813 bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(vm->dev))) {
814 free_pdp(vm->dev, pdp);
815 return true;
816 }
817
818 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200819}
Ben Widawsky459108b2013-11-02 21:07:23 -0700820
Michał Winiarski2ce51792016-10-13 14:02:42 +0200821/* Removes entries from a single pml4.
822 * This is the top-level structure in 4-level page tables used on gen8+.
823 * Empty entries are always scratch pml4e.
824 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200825static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
826 struct i915_pml4 *pml4,
827 uint64_t start,
828 uint64_t length)
829{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200830 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200831 struct i915_page_directory_pointer *pdp;
832 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200833 gen8_ppgtt_pml4e_t *pml4e_vaddr;
834 gen8_ppgtt_pml4e_t scratch_pml4e =
835 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
836
837 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->dev));
Ben Widawsky459108b2013-11-02 21:07:23 -0700838
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200839 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
840 if (WARN_ON(!pml4->pdps[pml4e]))
841 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700842
Michał Winiarski2ce51792016-10-13 14:02:42 +0200843 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
844 __clear_bit(pml4e, pml4->used_pml4es);
845 pml4e_vaddr = kmap_px(pml4);
846 pml4e_vaddr[pml4e] = scratch_pml4e;
847 kunmap_px(ppgtt, pml4e_vaddr);
848 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700849 }
850}
851
Michel Thierryf9b5b782015-07-30 11:02:49 +0100852static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200853 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700854{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300855 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100856
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200857 if (USES_FULL_48BIT_PPGTT(vm->dev))
858 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
859 else
860 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100861}
862
863static void
864gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
865 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100866 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100867 uint64_t start,
868 enum i915_cache_level cache_level)
869{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300870 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000871 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100872 unsigned pdpe = gen8_pdpe_index(start);
873 unsigned pde = gen8_pde_index(start);
874 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700875
Chris Wilson6f1cc992013-12-31 15:50:31 +0000876 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700877
Michel Thierry3387d432015-08-03 09:52:47 +0100878 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000879 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100880 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100881 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300882 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000883 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800884
885 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100886 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200887 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000888 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300889 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000890 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000891 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100892 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
893 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800894 pde = 0;
895 }
896 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700897 }
898 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300899
900 if (pt_vaddr)
901 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700902}
903
Michel Thierryf9b5b782015-07-30 11:02:49 +0100904static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
905 struct sg_table *pages,
906 uint64_t start,
907 enum i915_cache_level cache_level,
908 u32 unused)
909{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300910 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100911 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100912
Michel Thierry3387d432015-08-03 09:52:47 +0100913 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100914
915 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
916 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
917 cache_level);
918 } else {
919 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000920 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100921 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
922
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000923 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100924 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
925 start, cache_level);
926 }
927 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100928}
929
Michel Thierryf37c0502015-06-10 17:46:39 +0100930static void gen8_free_page_tables(struct drm_device *dev,
931 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800932{
933 int i;
934
Mika Kuoppala567047b2015-06-25 18:35:12 +0300935 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800936 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800937
Michel Thierry33c88192015-04-08 12:13:33 +0100938 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000939 if (WARN_ON(!pd->page_table[i]))
940 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800941
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300942 free_pt(dev, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000943 pd->page_table[i] = NULL;
944 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000945}
946
Mika Kuoppala8776f022015-06-30 18:16:40 +0300947static int gen8_init_scratch(struct i915_address_space *vm)
948{
949 struct drm_device *dev = vm->dev;
Matthew Auld64c050d2016-04-27 13:19:25 +0100950 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300951
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100952 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100953 if (ret)
954 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300955
956 vm->scratch_pt = alloc_pt(dev);
957 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100958 ret = PTR_ERR(vm->scratch_pt);
959 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300960 }
961
962 vm->scratch_pd = alloc_pd(dev);
963 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100964 ret = PTR_ERR(vm->scratch_pd);
965 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966 }
967
Michel Thierry69ab76f2015-07-29 17:23:55 +0100968 if (USES_FULL_48BIT_PPGTT(dev)) {
969 vm->scratch_pdp = alloc_pdp(dev);
970 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100971 ret = PTR_ERR(vm->scratch_pdp);
972 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100973 }
974 }
975
Mika Kuoppala8776f022015-06-30 18:16:40 +0300976 gen8_initialize_pt(vm, vm->scratch_pt);
977 gen8_initialize_pd(vm, vm->scratch_pd);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100978 if (USES_FULL_48BIT_PPGTT(dev))
979 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300980
981 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100982
983free_pd:
984 free_pd(dev, vm->scratch_pd);
985free_pt:
986 free_pt(dev, vm->scratch_pt);
987free_scratch_page:
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100988 cleanup_scratch_page(dev, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100989
990 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300991}
992
Zhiyuan Lv650da342015-08-28 15:41:18 +0800993static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
994{
995 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +0100996 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +0800997 int i;
998
Matthew Aulddf285642016-04-22 12:09:25 +0100999 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001000 u64 daddr = px_dma(&ppgtt->pml4);
1001
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001002 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1003 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001004
1005 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1006 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1007 } else {
1008 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1009 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1010
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001011 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1012 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001013 }
1014
1015 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1016 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1017 }
1018
1019 I915_WRITE(vgtif_reg(g2v_notify), msg);
1020
1021 return 0;
1022}
1023
Mika Kuoppala8776f022015-06-30 18:16:40 +03001024static void gen8_free_scratch(struct i915_address_space *vm)
1025{
1026 struct drm_device *dev = vm->dev;
1027
Michel Thierry69ab76f2015-07-29 17:23:55 +01001028 if (USES_FULL_48BIT_PPGTT(dev))
1029 free_pdp(dev, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001030 free_pd(dev, vm->scratch_pd);
1031 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001032 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001033}
1034
Michel Thierry762d9932015-07-30 11:05:29 +01001035static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
1036 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001037{
1038 int i;
1039
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001040 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
1041 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001042 continue;
1043
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001044 gen8_free_page_tables(dev, pdp->page_directory[i]);
1045 free_pd(dev, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001046 }
Michel Thierry69876be2015-04-08 12:13:27 +01001047
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001048 free_pdp(dev, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001049}
1050
1051static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1052{
1053 int i;
1054
1055 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1056 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1057 continue;
1058
1059 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
1060 }
1061
1062 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
1063}
1064
1065static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1066{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001067 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001068
Chris Wilsonc0336662016-05-06 15:40:21 +01001069 if (intel_vgpu_active(to_i915(vm->dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001070 gen8_ppgtt_notify_vgt(ppgtt, false);
1071
Michel Thierry762d9932015-07-30 11:05:29 +01001072 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1073 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1074 else
1075 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001076
Mika Kuoppala8776f022015-06-30 18:16:40 +03001077 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001078}
1079
Michel Thierryd7b26332015-04-08 12:13:34 +01001080/**
1081 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001082 * @vm: Master vm structure.
1083 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001084 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001085 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001086 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1087 * caller to free on error.
1088 *
1089 * Allocate the required number of page tables. Extremely similar to
1090 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1091 * the page directory boundary (instead of the page directory pointer). That
1092 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1093 * possible, and likely that the caller will need to use multiple calls of this
1094 * function to achieve the appropriate allocation.
1095 *
1096 * Return: 0 if success; negative error code otherwise.
1097 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001098static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001099 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001100 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001101 uint64_t length,
1102 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001103{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001104 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001105 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001106 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001107
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001108 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001109 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001110 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001111 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001112 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001113 continue;
1114 }
1115
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001116 pt = alloc_pt(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001117 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001118 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001119
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001120 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001121 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001122 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001123 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001124 }
1125
1126 return 0;
1127
1128unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001129 for_each_set_bit(pde, new_pts, I915_PDES)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001130 free_pt(dev, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001131
1132 return -ENOMEM;
1133}
1134
Michel Thierryd7b26332015-04-08 12:13:34 +01001135/**
1136 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001137 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001138 * @pdp: Page directory pointer for this address range.
1139 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001140 * @length: Size of the allocations.
1141 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001142 * caller to free on error.
1143 *
1144 * Allocate the required number of page directories starting at the pde index of
1145 * @start, and ending at the pde index @start + @length. This function will skip
1146 * over already allocated page directories within the range, and only allocate
1147 * new ones, setting the appropriate pointer within the pdp as well as the
1148 * correct position in the bitmap @new_pds.
1149 *
1150 * The function will only allocate the pages within the range for a give page
1151 * directory pointer. In other words, if @start + @length straddles a virtually
1152 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1153 * required by the caller, This is not currently possible, and the BUG in the
1154 * code will prevent it.
1155 *
1156 * Return: 0 if success; negative error code otherwise.
1157 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001158static int
1159gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1160 struct i915_page_directory_pointer *pdp,
1161 uint64_t start,
1162 uint64_t length,
1163 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001164{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001165 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001166 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001167 uint32_t pdpe;
Michel Thierry6ac18502015-07-29 17:23:46 +01001168 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001169
Michel Thierry6ac18502015-07-29 17:23:46 +01001170 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001171
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001172 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001173 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001174 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001175
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001176 pd = alloc_pd(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001177 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001178 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001179
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001180 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001181 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001182 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001183 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001184 }
1185
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001186 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001187
1188unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001189 for_each_set_bit(pdpe, new_pds, pdpes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001190 free_pd(dev, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001191
1192 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001193}
1194
Michel Thierry762d9932015-07-30 11:05:29 +01001195/**
1196 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1197 * @vm: Master vm structure.
1198 * @pml4: Page map level 4 for this address range.
1199 * @start: Starting virtual address to begin allocations.
1200 * @length: Size of the allocations.
1201 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1202 * caller to free on error.
1203 *
1204 * Allocate the required number of page directory pointers. Extremely similar to
1205 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1206 * The main difference is here we are limited by the pml4 boundary (instead of
1207 * the page directory pointer).
1208 *
1209 * Return: 0 if success; negative error code otherwise.
1210 */
1211static int
1212gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1213 struct i915_pml4 *pml4,
1214 uint64_t start,
1215 uint64_t length,
1216 unsigned long *new_pdps)
1217{
1218 struct drm_device *dev = vm->dev;
1219 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001220 uint32_t pml4e;
1221
1222 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1223
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001224 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001225 if (!test_bit(pml4e, pml4->used_pml4es)) {
1226 pdp = alloc_pdp(dev);
1227 if (IS_ERR(pdp))
1228 goto unwind_out;
1229
Michel Thierry69ab76f2015-07-29 17:23:55 +01001230 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001231 pml4->pdps[pml4e] = pdp;
1232 __set_bit(pml4e, new_pdps);
1233 trace_i915_page_directory_pointer_entry_alloc(vm,
1234 pml4e,
1235 start,
1236 GEN8_PML4E_SHIFT);
1237 }
1238 }
1239
1240 return 0;
1241
1242unwind_out:
1243 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1244 free_pdp(dev, pml4->pdps[pml4e]);
1245
1246 return -ENOMEM;
1247}
1248
Michel Thierryd7b26332015-04-08 12:13:34 +01001249static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001250free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001251{
Michel Thierryd7b26332015-04-08 12:13:34 +01001252 kfree(new_pts);
1253 kfree(new_pds);
1254}
1255
1256/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1257 * of these are based on the number of PDPEs in the system.
1258 */
1259static
1260int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001261 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001262 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001263{
Michel Thierryd7b26332015-04-08 12:13:34 +01001264 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001265 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001266
Michał Winiarski3a41a052015-09-03 19:22:18 +02001267 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001268 if (!pds)
1269 return -ENOMEM;
1270
Michał Winiarski3a41a052015-09-03 19:22:18 +02001271 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1272 GFP_TEMPORARY);
1273 if (!pts)
1274 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001275
1276 *new_pds = pds;
1277 *new_pts = pts;
1278
1279 return 0;
1280
1281err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001282 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001283 return -ENOMEM;
1284}
1285
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001286/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1287 * the page table structures, we mark them dirty so that
1288 * context switching/execlist queuing code takes extra steps
1289 * to ensure that tlbs are flushed.
1290 */
1291static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1292{
1293 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1294}
1295
Michel Thierry762d9932015-07-30 11:05:29 +01001296static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1297 struct i915_page_directory_pointer *pdp,
1298 uint64_t start,
1299 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001300{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001301 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001302 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001303 struct drm_device *dev = vm->dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001304 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001305 const uint64_t orig_start = start;
1306 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001307 uint32_t pdpe;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001308 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001309 int ret;
1310
Michel Thierryd7b26332015-04-08 12:13:34 +01001311 /* Wrap is never okay since we can only represent 48b, and we don't
1312 * actually use the other side of the canonical address space.
1313 */
1314 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001315 return -ENODEV;
1316
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001317 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001318 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001319
Michel Thierry6ac18502015-07-29 17:23:46 +01001320 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001321 if (ret)
1322 return ret;
1323
Michel Thierryd7b26332015-04-08 12:13:34 +01001324 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001325 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1326 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001327 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001328 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001329 return ret;
1330 }
1331
1332 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001333 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001334 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001335 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001336 if (ret)
1337 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001338 }
1339
Michel Thierry33c88192015-04-08 12:13:33 +01001340 start = orig_start;
1341 length = orig_length;
1342
Michel Thierryd7b26332015-04-08 12:13:34 +01001343 /* Allocations have completed successfully, so set the bitmaps, and do
1344 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001345 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001346 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001347 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001348 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001349 uint64_t pd_start = start;
1350 uint32_t pde;
1351
Michel Thierryd7b26332015-04-08 12:13:34 +01001352 /* Every pd should be allocated, we just did that above. */
1353 WARN_ON(!pd);
1354
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001355 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001356 /* Same reasoning as pd */
1357 WARN_ON(!pt);
1358 WARN_ON(!pd_len);
1359 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1360
1361 /* Set our used ptes within the page table */
1362 bitmap_set(pt->used_ptes,
1363 gen8_pte_index(pd_start),
1364 gen8_pte_count(pd_start, pd_len));
1365
1366 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001367 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001368
1369 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001370 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1371 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001372 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1373 gen8_pte_index(start),
1374 gen8_pte_count(start, length),
1375 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001376
1377 /* NB: We haven't yet mapped ptes to pages. At this
1378 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001379 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001380
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001381 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001382 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001383 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001384 }
1385
Michał Winiarski3a41a052015-09-03 19:22:18 +02001386 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001387 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001388 return 0;
1389
1390err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001391 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001392 unsigned long temp;
1393
Michał Winiarski3a41a052015-09-03 19:22:18 +02001394 for_each_set_bit(temp, new_page_tables + pdpe *
1395 BITS_TO_LONGS(I915_PDES), I915_PDES)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001396 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001397 }
1398
Michel Thierry6ac18502015-07-29 17:23:46 +01001399 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001400 free_pd(dev, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001401
Michał Winiarski3a41a052015-09-03 19:22:18 +02001402 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001403 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001404 return ret;
1405}
1406
Michel Thierry762d9932015-07-30 11:05:29 +01001407static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1408 struct i915_pml4 *pml4,
1409 uint64_t start,
1410 uint64_t length)
1411{
1412 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001413 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001414 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001415 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001416 int ret = 0;
1417
1418 /* Do the pml4 allocations first, so we don't need to track the newly
1419 * allocated tables below the pdp */
1420 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1421
1422 /* The pagedirectory and pagetable allocations are done in the shared 3
1423 * and 4 level code. Just allocate the pdps.
1424 */
1425 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1426 new_pdps);
1427 if (ret)
1428 return ret;
1429
1430 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1431 "The allocation has spanned more than 512GB. "
1432 "It is highly likely this is incorrect.");
1433
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001434 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001435 WARN_ON(!pdp);
1436
1437 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1438 if (ret)
1439 goto err_out;
1440
1441 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1442 }
1443
1444 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1445 GEN8_PML4ES_PER_PML4);
1446
1447 return 0;
1448
1449err_out:
1450 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1451 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1452
1453 return ret;
1454}
1455
1456static int gen8_alloc_va_range(struct i915_address_space *vm,
1457 uint64_t start, uint64_t length)
1458{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001459 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001460
1461 if (USES_FULL_48BIT_PPGTT(vm->dev))
1462 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1463 else
1464 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1465}
1466
Michel Thierryea91e402015-07-29 17:23:57 +01001467static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1468 uint64_t start, uint64_t length,
1469 gen8_pte_t scratch_pte,
1470 struct seq_file *m)
1471{
1472 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001473 uint32_t pdpe;
1474
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001475 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001476 struct i915_page_table *pt;
1477 uint64_t pd_len = length;
1478 uint64_t pd_start = start;
1479 uint32_t pde;
1480
1481 if (!test_bit(pdpe, pdp->used_pdpes))
1482 continue;
1483
1484 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001485 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001486 uint32_t pte;
1487 gen8_pte_t *pt_vaddr;
1488
1489 if (!test_bit(pde, pd->used_pdes))
1490 continue;
1491
1492 pt_vaddr = kmap_px(pt);
1493 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1494 uint64_t va =
1495 (pdpe << GEN8_PDPE_SHIFT) |
1496 (pde << GEN8_PDE_SHIFT) |
1497 (pte << GEN8_PTE_SHIFT);
1498 int i;
1499 bool found = false;
1500
1501 for (i = 0; i < 4; i++)
1502 if (pt_vaddr[pte + i] != scratch_pte)
1503 found = true;
1504 if (!found)
1505 continue;
1506
1507 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1508 for (i = 0; i < 4; i++) {
1509 if (pt_vaddr[pte + i] != scratch_pte)
1510 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1511 else
1512 seq_puts(m, " SCRATCH ");
1513 }
1514 seq_puts(m, "\n");
1515 }
1516 /* don't use kunmap_px, it could trigger
1517 * an unnecessary flush.
1518 */
1519 kunmap_atomic(pt_vaddr);
1520 }
1521 }
1522}
1523
1524static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1525{
1526 struct i915_address_space *vm = &ppgtt->base;
1527 uint64_t start = ppgtt->base.start;
1528 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001529 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001530 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001531
1532 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1533 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1534 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001535 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001536 struct i915_pml4 *pml4 = &ppgtt->pml4;
1537 struct i915_page_directory_pointer *pdp;
1538
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001539 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001540 if (!test_bit(pml4e, pml4->used_pml4es))
1541 continue;
1542
1543 seq_printf(m, " PML4E #%llu\n", pml4e);
1544 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1545 }
1546 }
1547}
1548
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001549static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1550{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001551 unsigned long *new_page_dirs, *new_page_tables;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001552 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1553 int ret;
1554
1555 /* We allocate temp bitmap for page tables for no gain
1556 * but as this is for init only, lets keep the things simple
1557 */
1558 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1559 if (ret)
1560 return ret;
1561
1562 /* Allocate for all pdps regardless of how the ppgtt
1563 * was defined.
1564 */
1565 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1566 0, 1ULL << 32,
1567 new_page_dirs);
1568 if (!ret)
1569 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1570
Michał Winiarski3a41a052015-09-03 19:22:18 +02001571 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001572
1573 return ret;
1574}
1575
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001576/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001577 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1578 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1579 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1580 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001581 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001582 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001583static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001584{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001585 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001586
Mika Kuoppala8776f022015-06-30 18:16:40 +03001587 ret = gen8_init_scratch(&ppgtt->base);
1588 if (ret)
1589 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001590
Michel Thierryd7b26332015-04-08 12:13:34 +01001591 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001592 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001593 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001594 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001595 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001596 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1597 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001598 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001599
Michel Thierry762d9932015-07-30 11:05:29 +01001600 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1601 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1602 if (ret)
1603 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001604
Michel Thierry69ab76f2015-07-29 17:23:55 +01001605 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1606
Michel Thierry762d9932015-07-30 11:05:29 +01001607 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001608 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001609 } else {
Michel Thierry25f50332015-08-07 17:40:19 +01001610 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001611 if (ret)
1612 goto free_scratch;
1613
1614 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001615 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001616 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1617 0, 0,
1618 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001619
Chris Wilsonc0336662016-05-06 15:40:21 +01001620 if (intel_vgpu_active(to_i915(ppgtt->base.dev))) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001621 ret = gen8_preallocate_top_level_pdps(ppgtt);
1622 if (ret)
1623 goto free_scratch;
1624 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001625 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001626
Chris Wilsonc0336662016-05-06 15:40:21 +01001627 if (intel_vgpu_active(to_i915(ppgtt->base.dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001628 gen8_ppgtt_notify_vgt(ppgtt, true);
1629
Michel Thierryd7b26332015-04-08 12:13:34 +01001630 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001631
1632free_scratch:
1633 gen8_free_scratch(&ppgtt->base);
1634 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001635}
1636
Ben Widawsky87d60b62013-12-06 14:11:29 -08001637static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1638{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001639 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001640 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001641 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001642 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001643 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001644 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001645
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001646 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001647 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001648
Dave Gordon731f74c2016-06-24 19:37:46 +01001649 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001650 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001651 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001652 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001653 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001654 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1655
1656 if (pd_entry != expected)
1657 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1658 pde,
1659 pd_entry,
1660 expected);
1661 seq_printf(m, "\tPDE: %x\n", pd_entry);
1662
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001663 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1664
Michel Thierry07749ef2015-03-16 16:00:54 +00001665 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001666 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001667 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001668 (pte * PAGE_SIZE);
1669 int i;
1670 bool found = false;
1671 for (i = 0; i < 4; i++)
1672 if (pt_vaddr[pte + i] != scratch_pte)
1673 found = true;
1674 if (!found)
1675 continue;
1676
1677 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1678 for (i = 0; i < 4; i++) {
1679 if (pt_vaddr[pte + i] != scratch_pte)
1680 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1681 else
1682 seq_puts(m, " SCRATCH ");
1683 }
1684 seq_puts(m, "\n");
1685 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001686 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001687 }
1688}
1689
Ben Widawsky678d96f2015-03-16 16:00:56 +00001690/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001691static void gen6_write_pde(struct i915_page_directory *pd,
1692 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001693{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001694 /* Caller needs to make sure the write completes if necessary */
1695 struct i915_hw_ppgtt *ppgtt =
1696 container_of(pd, struct i915_hw_ppgtt, pd);
1697 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001698
Mika Kuoppala567047b2015-06-25 18:35:12 +03001699 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001700 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001701
Ben Widawsky678d96f2015-03-16 16:00:56 +00001702 writel(pd_entry, ppgtt->pd_addr + pde);
1703}
Ben Widawsky61973492013-04-08 18:43:54 -07001704
Ben Widawsky678d96f2015-03-16 16:00:56 +00001705/* Write all the page tables found in the ppgtt structure to incrementing page
1706 * directories. */
1707static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001708 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001709 uint32_t start, uint32_t length)
1710{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001711 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001712 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001713 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001714
Dave Gordon731f74c2016-06-24 19:37:46 +01001715 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001716 gen6_write_pde(pd, pde, pt);
1717
1718 /* Make sure write is complete before other code can use this page
1719 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001720 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001721}
1722
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001723static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001724{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001725 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001726
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001727 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001728}
Ben Widawsky61973492013-04-08 18:43:54 -07001729
Ben Widawsky90252e52013-12-06 14:11:12 -08001730static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001731 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001732{
Chris Wilson7e37f882016-08-02 22:50:21 +01001733 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001734 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001735 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001736
Ben Widawsky90252e52013-12-06 14:11:12 -08001737 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001738 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001739 if (ret)
1740 return ret;
1741
John Harrison5fb9de12015-05-29 17:44:07 +01001742 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001743 if (ret)
1744 return ret;
1745
Chris Wilsonb5321f32016-08-02 22:50:18 +01001746 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1747 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1748 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1749 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1750 intel_ring_emit(ring, get_pd_offset(ppgtt));
1751 intel_ring_emit(ring, MI_NOOP);
1752 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001753
1754 return 0;
1755}
1756
Ben Widawsky48a10382013-12-06 14:11:11 -08001757static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001758 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001759{
Chris Wilson7e37f882016-08-02 22:50:21 +01001760 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001761 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001762 int ret;
1763
Ben Widawsky48a10382013-12-06 14:11:11 -08001764 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001765 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001766 if (ret)
1767 return ret;
1768
John Harrison5fb9de12015-05-29 17:44:07 +01001769 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001770 if (ret)
1771 return ret;
1772
Chris Wilsonb5321f32016-08-02 22:50:18 +01001773 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1774 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1775 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1776 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1777 intel_ring_emit(ring, get_pd_offset(ppgtt));
1778 intel_ring_emit(ring, MI_NOOP);
1779 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001780
Ben Widawsky90252e52013-12-06 14:11:12 -08001781 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001782 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001783 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001784 if (ret)
1785 return ret;
1786 }
1787
Ben Widawsky48a10382013-12-06 14:11:11 -08001788 return 0;
1789}
1790
Ben Widawskyeeb94882013-12-06 14:11:10 -08001791static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001792 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001793{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001794 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001795 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001796
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001797 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1798 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001799 return 0;
1800}
1801
Daniel Vetter82460d92014-08-06 20:19:53 +02001802static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001803{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001804 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001805 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301806 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001807
Akash Goel3b3f1652016-10-13 22:44:48 +05301808 for_each_engine(engine, dev_priv, id) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001809 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001810 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001811 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001812 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001813}
1814
Daniel Vetter82460d92014-08-06 20:19:53 +02001815static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001816{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001817 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001818 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301820 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001821
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001822 ecobits = I915_READ(GAC_ECO_BITS);
1823 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1824
1825 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001826 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001827 ecochk |= ECOCHK_PPGTT_WB_HSW;
1828 } else {
1829 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1830 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1831 }
1832 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001833
Akash Goel3b3f1652016-10-13 22:44:48 +05301834 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001835 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001836 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001837 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001838 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001839}
1840
Daniel Vetter82460d92014-08-06 20:19:53 +02001841static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001842{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001843 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001844 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001845
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001846 ecobits = I915_READ(GAC_ECO_BITS);
1847 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1848 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001849
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001850 gab_ctl = I915_READ(GAB_CTL);
1851 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001852
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001853 ecochk = I915_READ(GAM_ECOCHK);
1854 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001855
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001856 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001857}
1858
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001859/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001860static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001861 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001862 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001863{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001864 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001865 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001866 unsigned first_entry = start >> PAGE_SHIFT;
1867 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001868 unsigned act_pt = first_entry / GEN6_PTES;
1869 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001870 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001871
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001872 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001873 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001874
Daniel Vetter7bddb012012-02-09 17:15:47 +01001875 while (num_entries) {
1876 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001877 if (last_pte > GEN6_PTES)
1878 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001879
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001880 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001881
1882 for (i = first_pte; i < last_pte; i++)
1883 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001884
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001885 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001886
Daniel Vetter7bddb012012-02-09 17:15:47 +01001887 num_entries -= last_pte - first_pte;
1888 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001889 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001890 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001891}
1892
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001893static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001894 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001895 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301896 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001897{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001898 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001899 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001900 unsigned act_pt = first_entry / GEN6_PTES;
1901 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001902 gen6_pte_t *pt_vaddr = NULL;
1903 struct sgt_iter sgt_iter;
1904 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001905
Dave Gordon85d12252016-05-20 11:54:06 +01001906 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001907 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001908 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001909
Chris Wilsoncc797142013-12-31 15:50:30 +00001910 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001911 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301912
Michel Thierry07749ef2015-03-16 16:00:54 +00001913 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001914 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001915 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001916 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001917 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001918 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001919 }
Dave Gordon85d12252016-05-20 11:54:06 +01001920
Chris Wilsoncc797142013-12-31 15:50:30 +00001921 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001922 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001923}
1924
Ben Widawsky678d96f2015-03-16 16:00:56 +00001925static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001926 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001927{
Michel Thierry4933d512015-03-24 15:46:22 +00001928 DECLARE_BITMAP(new_page_tables, I915_PDES);
1929 struct drm_device *dev = vm->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001930 struct drm_i915_private *dev_priv = to_i915(dev);
1931 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001932 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001933 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001934 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001935 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001936 int ret;
1937
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001938 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1939 return -ENODEV;
1940
1941 start = start_save = start_in;
1942 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001943
1944 bitmap_zero(new_page_tables, I915_PDES);
1945
1946 /* The allocation is done in two stages so that we can bail out with
1947 * minimal amount of pain. The first stage finds new page tables that
1948 * need allocation. The second stage marks use ptes within the page
1949 * tables.
1950 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001951 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001952 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001953 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1954 continue;
1955 }
1956
1957 /* We've already allocated a page table */
1958 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1959
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001960 pt = alloc_pt(dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001961 if (IS_ERR(pt)) {
1962 ret = PTR_ERR(pt);
1963 goto unwind_out;
1964 }
1965
1966 gen6_initialize_pt(vm, pt);
1967
1968 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001969 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001970 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001971 }
1972
1973 start = start_save;
1974 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001975
Dave Gordon731f74c2016-06-24 19:37:46 +01001976 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001977 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1978
1979 bitmap_zero(tmp_bitmap, GEN6_PTES);
1980 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1981 gen6_pte_count(start, length));
1982
Mika Kuoppala966082c2015-06-25 18:35:19 +03001983 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001984 gen6_write_pde(&ppgtt->pd, pde, pt);
1985
Michel Thierry72744cb2015-03-24 15:46:23 +00001986 trace_i915_page_table_entry_map(vm, pde, pt,
1987 gen6_pte_index(start),
1988 gen6_pte_count(start, length),
1989 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001990 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001991 GEN6_PTES);
1992 }
1993
Michel Thierry4933d512015-03-24 15:46:22 +00001994 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1995
1996 /* Make sure write is complete before other code can use this page
1997 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001998 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001999
Ben Widawsky563222a2015-03-19 12:53:28 +00002000 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002001 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002002
2003unwind_out:
2004 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002005 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002006
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002007 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03002008 free_pt(vm->dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002009 }
2010
2011 mark_tlbs_dirty(ppgtt);
2012 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002013}
2014
Mika Kuoppala8776f022015-06-30 18:16:40 +03002015static int gen6_init_scratch(struct i915_address_space *vm)
2016{
2017 struct drm_device *dev = vm->dev;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002018 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002019
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002020 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002021 if (ret)
2022 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002023
2024 vm->scratch_pt = alloc_pt(dev);
2025 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002026 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002027 return PTR_ERR(vm->scratch_pt);
2028 }
2029
2030 gen6_initialize_pt(vm, vm->scratch_pt);
2031
2032 return 0;
2033}
2034
2035static void gen6_free_scratch(struct i915_address_space *vm)
2036{
2037 struct drm_device *dev = vm->dev;
2038
2039 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002040 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002041}
2042
Daniel Vetter061dd492015-04-14 17:35:13 +02002043static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002044{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002045 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002046 struct i915_page_directory *pd = &ppgtt->pd;
2047 struct drm_device *dev = vm->dev;
Michel Thierry09942c62015-04-08 12:13:30 +01002048 struct i915_page_table *pt;
2049 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002050
Daniel Vetter061dd492015-04-14 17:35:13 +02002051 drm_mm_remove_node(&ppgtt->node);
2052
Dave Gordon731f74c2016-06-24 19:37:46 +01002053 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002054 if (pt != vm->scratch_pt)
Dave Gordon731f74c2016-06-24 19:37:46 +01002055 free_pt(dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002056
Mika Kuoppala8776f022015-06-30 18:16:40 +03002057 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002058}
2059
Ben Widawskyb1465202014-02-19 22:05:49 -08002060static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002061{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002062 struct i915_address_space *vm = &ppgtt->base;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002063 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002064 struct drm_i915_private *dev_priv = to_i915(dev);
2065 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002066 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002067 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002068
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002069 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2070 * allocator works in address space sizes, so it's multiplied by page
2071 * size. We allocate at the top of the GTT to avoid fragmentation.
2072 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002073 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002074
Mika Kuoppala8776f022015-06-30 18:16:40 +03002075 ret = gen6_init_scratch(vm);
2076 if (ret)
2077 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002078
Ben Widawskye3cc1992013-12-06 14:11:08 -08002079alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002080 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002081 &ppgtt->node, GEN6_PD_SIZE,
2082 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002083 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002084 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002085 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002086 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002087 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002088 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002089 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002090 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002091 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002092 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002093
2094 retried = true;
2095 goto alloc;
2096 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002097
Ben Widawskyc8c26622015-01-22 17:01:25 +00002098 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002099 goto err_out;
2100
Ben Widawskyc8c26622015-01-22 17:01:25 +00002101
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002102 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002103 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002104
Ben Widawskyc8c26622015-01-22 17:01:25 +00002105 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002106
2107err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002108 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002109 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002110}
2111
Ben Widawskyb1465202014-02-19 22:05:49 -08002112static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2113{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002114 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002115}
2116
Michel Thierry4933d512015-03-24 15:46:22 +00002117static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2118 uint64_t start, uint64_t length)
2119{
Michel Thierryec565b32015-04-08 12:13:23 +01002120 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002121 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002122
Dave Gordon731f74c2016-06-24 19:37:46 +01002123 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002124 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002125}
2126
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002127static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002128{
2129 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002130 struct drm_i915_private *dev_priv = to_i915(dev);
2131 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002132 int ret;
2133
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002134 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002135 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002136 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002137 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002138 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002139 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002140 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002141 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002142 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002143
2144 ret = gen6_ppgtt_alloc(ppgtt);
2145 if (ret)
2146 return ret;
2147
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002148 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002149 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2150 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002151 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2152 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002153 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f6f2013-11-25 09:54:34 -08002154 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002155 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002156 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002157
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002158 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002159 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002160
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002161 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002162 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002163
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002164 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002165
Ben Widawsky678d96f2015-03-16 16:00:56 +00002166 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2167
Thierry Reding440fd522015-01-23 09:05:06 +01002168 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002169 ppgtt->node.size >> 20,
2170 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002171
Daniel Vetterfa76da32014-08-06 20:19:54 +02002172 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002173 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002174
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002175 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002176}
2177
Chris Wilson2bfa9962016-08-04 07:52:25 +01002178static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2179 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002180{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002181 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002182
Chris Wilson2bfa9962016-08-04 07:52:25 +01002183 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002184 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002185 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002186 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002187}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002188
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002189static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002190 struct drm_i915_private *dev_priv,
2191 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002192{
Chris Wilson80b204b2016-10-28 13:58:58 +01002193 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002194 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002195 INIT_LIST_HEAD(&vm->active_list);
2196 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002197 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002198 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2199}
2200
Tim Gored5165eb2016-02-04 11:49:34 +00002201static void gtt_write_workarounds(struct drm_device *dev)
2202{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002203 struct drm_i915_private *dev_priv = to_i915(dev);
Tim Gored5165eb2016-02-04 11:49:34 +00002204
2205 /* This function is for gtt related workarounds. This function is
2206 * called on driver load and after a GPU reset, so you can place
2207 * workarounds here even if they get overwritten by GPU reset.
2208 */
2209 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002210 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002211 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002212 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002213 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002214 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002215 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002216 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002217 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2218}
2219
Chris Wilson2bfa9962016-08-04 07:52:25 +01002220static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2221 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002222 struct drm_i915_file_private *file_priv,
2223 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002224{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002225 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002226
Chris Wilson2bfa9962016-08-04 07:52:25 +01002227 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002228 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002229 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002230 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002231 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002232 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002233
2234 return ret;
2235}
2236
Daniel Vetter82460d92014-08-06 20:19:53 +02002237int i915_ppgtt_init_hw(struct drm_device *dev)
2238{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002239 struct drm_i915_private *dev_priv = to_i915(dev);
2240
Tim Gored5165eb2016-02-04 11:49:34 +00002241 gtt_write_workarounds(dev);
2242
Thomas Daniel671b50132014-08-20 16:24:50 +01002243 /* In the case of execlists, PPGTT is enabled by the context descriptor
2244 * and the PDPs are contained within the context itself. We don't
2245 * need to do anything here. */
2246 if (i915.enable_execlists)
2247 return 0;
2248
Daniel Vetter82460d92014-08-06 20:19:53 +02002249 if (!USES_PPGTT(dev))
2250 return 0;
2251
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002252 if (IS_GEN6(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002253 gen6_ppgtt_enable(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002254 else if (IS_GEN7(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002255 gen7_ppgtt_enable(dev);
2256 else if (INTEL_INFO(dev)->gen >= 8)
2257 gen8_ppgtt_enable(dev);
2258 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01002259 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02002260
John Harrison4ad2fd82015-06-18 13:11:20 +01002261 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002262}
John Harrison4ad2fd82015-06-18 13:11:20 +01002263
Daniel Vetter4d884702014-08-06 15:04:47 +02002264struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002265i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002266 struct drm_i915_file_private *fpriv,
2267 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002268{
2269 struct i915_hw_ppgtt *ppgtt;
2270 int ret;
2271
2272 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2273 if (!ppgtt)
2274 return ERR_PTR(-ENOMEM);
2275
Chris Wilson80b204b2016-10-28 13:58:58 +01002276 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002277 if (ret) {
2278 kfree(ppgtt);
2279 return ERR_PTR(ret);
2280 }
2281
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002282 trace_i915_ppgtt_create(&ppgtt->base);
2283
Daniel Vetter4d884702014-08-06 15:04:47 +02002284 return ppgtt;
2285}
2286
Daniel Vetteree960be2014-08-06 15:04:45 +02002287void i915_ppgtt_release(struct kref *kref)
2288{
2289 struct i915_hw_ppgtt *ppgtt =
2290 container_of(kref, struct i915_hw_ppgtt, ref);
2291
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002292 trace_i915_ppgtt_release(&ppgtt->base);
2293
Chris Wilson50e046b2016-08-04 07:52:46 +01002294 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002295 WARN_ON(!list_empty(&ppgtt->base.active_list));
2296 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002297 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002298
Chris Wilson80b204b2016-10-28 13:58:58 +01002299 i915_gem_timeline_fini(&ppgtt->base.timeline);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002300 list_del(&ppgtt->base.global_link);
2301 drm_mm_takedown(&ppgtt->base.mm);
2302
Daniel Vetteree960be2014-08-06 15:04:45 +02002303 ppgtt->base.cleanup(&ppgtt->base);
2304 kfree(ppgtt);
2305}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002306
Ben Widawskya81cc002013-01-18 12:30:31 -08002307/* Certain Gen5 chipsets require require idling the GPU before
2308 * unmapping anything from the GTT when VT-d is enabled.
2309 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002310static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002311{
2312#ifdef CONFIG_INTEL_IOMMU
2313 /* Query intel_iommu to see if we need the workaround. Presumably that
2314 * was loaded first.
2315 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002316 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002317 return true;
2318#endif
2319 return false;
2320}
2321
Chris Wilsondc979972016-05-10 14:10:04 +01002322void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002323{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002324 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302325 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002326
Chris Wilsondc979972016-05-10 14:10:04 +01002327 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002328 return;
2329
Akash Goel3b3f1652016-10-13 22:44:48 +05302330 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002331 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002332 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002333 if (fault_reg & RING_FAULT_VALID) {
2334 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002335 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002336 "\tAddress space: %s\n"
2337 "\tSource ID: %d\n"
2338 "\tType: %d\n",
2339 fault_reg & PAGE_MASK,
2340 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2341 RING_FAULT_SRCID(fault_reg),
2342 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002343 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002344 fault_reg & ~RING_FAULT_VALID);
2345 }
2346 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302347
2348 /* Engine specific init may not have been done till this point. */
2349 if (dev_priv->engine[RCS])
2350 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002351}
2352
Chris Wilson91e56492014-09-25 10:13:12 +01002353static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2354{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002355 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002356 intel_gtt_chipset_flush();
2357 } else {
2358 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2359 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2360 }
2361}
2362
Ben Widawsky828c7902013-10-16 09:21:30 -07002363void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2364{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002365 struct drm_i915_private *dev_priv = to_i915(dev);
2366 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002367
2368 /* Don't bother messing with faults pre GEN6 as we have little
2369 * documentation supporting that it's a good idea.
2370 */
2371 if (INTEL_INFO(dev)->gen < 6)
2372 return;
2373
Chris Wilsondc979972016-05-10 14:10:04 +01002374 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002375
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002376 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002377
2378 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002379}
2380
Chris Wilson03ac84f2016-10-28 13:58:36 +01002381int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2382 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002383{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002384 if (dma_map_sg(&obj->base.dev->pdev->dev,
2385 pages->sgl, pages->nents,
2386 PCI_DMA_BIDIRECTIONAL))
2387 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01002388
Chris Wilson03ac84f2016-10-28 13:58:36 +01002389 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002390}
2391
Daniel Vetter2c642b02015-04-14 17:35:26 +02002392static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002393{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002394 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002395}
2396
Chris Wilsond6473f52016-06-10 14:22:59 +05302397static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2398 dma_addr_t addr,
2399 uint64_t offset,
2400 enum i915_cache_level level,
2401 u32 unused)
2402{
2403 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2404 gen8_pte_t __iomem *pte =
2405 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2406 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302407
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002408 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302409
2410 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2411 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302412}
2413
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002414static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2415 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002416 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302417 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002418{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002419 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002420 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002421 struct sgt_iter sgt_iter;
2422 gen8_pte_t __iomem *gtt_entries;
2423 gen8_pte_t gtt_entry;
2424 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002425 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002426
Dave Gordon85d12252016-05-20 11:54:06 +01002427 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2428
2429 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002430 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002431 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002432 }
2433
2434 /*
2435 * XXX: This serves as a posting read to make sure that the PTE has
2436 * actually been updated. There is some concern that even though
2437 * registers and PTEs are within the same BAR that they are potentially
2438 * of NUMA access patterns. Therefore, even with the way we assume
2439 * hardware should work, we must keep this posting read for paranoia.
2440 */
2441 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002442 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002443
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002444 /* This next bit makes the above posting read even more important. We
2445 * want to flush the TLBs only after we're certain all the PTE updates
2446 * have finished.
2447 */
2448 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2449 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002450}
2451
Chris Wilsonc1403302015-11-18 15:19:39 +00002452struct insert_entries {
2453 struct i915_address_space *vm;
2454 struct sg_table *st;
2455 uint64_t start;
2456 enum i915_cache_level level;
2457 u32 flags;
2458};
2459
2460static int gen8_ggtt_insert_entries__cb(void *_arg)
2461{
2462 struct insert_entries *arg = _arg;
2463 gen8_ggtt_insert_entries(arg->vm, arg->st,
2464 arg->start, arg->level, arg->flags);
2465 return 0;
2466}
2467
2468static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2469 struct sg_table *st,
2470 uint64_t start,
2471 enum i915_cache_level level,
2472 u32 flags)
2473{
2474 struct insert_entries arg = { vm, st, start, level, flags };
2475 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2476}
2477
Chris Wilsond6473f52016-06-10 14:22:59 +05302478static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2479 dma_addr_t addr,
2480 uint64_t offset,
2481 enum i915_cache_level level,
2482 u32 flags)
2483{
2484 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2485 gen6_pte_t __iomem *pte =
2486 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2487 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302488
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002489 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302490
2491 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2492 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302493}
2494
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002495/*
2496 * Binds an object into the global gtt with the specified cache level. The object
2497 * will be accessible to the GPU via commands whose operands reference offsets
2498 * within the global GTT as well as accessible by the GPU through the GMADR
2499 * mapped BAR (dev_priv->mm.gtt->gtt).
2500 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002501static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002502 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002503 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302504 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002505{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002506 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002507 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002508 struct sgt_iter sgt_iter;
2509 gen6_pte_t __iomem *gtt_entries;
2510 gen6_pte_t gtt_entry;
2511 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002512 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002513
Dave Gordon85d12252016-05-20 11:54:06 +01002514 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2515
2516 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002517 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002518 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002519 }
2520
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002521 /* XXX: This serves as a posting read to make sure that the PTE has
2522 * actually been updated. There is some concern that even though
2523 * registers and PTEs are within the same BAR that they are potentially
2524 * of NUMA access patterns. Therefore, even with the way we assume
2525 * hardware should work, we must keep this posting read for paranoia.
2526 */
Dave Gordon85d12252016-05-20 11:54:06 +01002527 if (i != 0)
2528 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002529
2530 /* This next bit makes the above posting read even more important. We
2531 * want to flush the TLBs only after we're certain all the PTE updates
2532 * have finished.
2533 */
2534 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2535 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002536}
2537
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002538static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002539 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002540{
2541}
2542
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002543static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002544 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002545{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002546 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002547 unsigned first_entry = start >> PAGE_SHIFT;
2548 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002549 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002550 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2551 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002552 int i;
2553
2554 if (WARN(num_entries > max_entries,
2555 "First entry = %d; Num entries = %d (max=%d)\n",
2556 first_entry, num_entries, max_entries))
2557 num_entries = max_entries;
2558
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002559 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002560 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002561 for (i = 0; i < num_entries; i++)
2562 gen8_set_pte(&gtt_base[i], scratch_pte);
2563 readl(gtt_base);
2564}
2565
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002566static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002567 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002568 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002569{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002570 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002571 unsigned first_entry = start >> PAGE_SHIFT;
2572 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002573 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002574 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2575 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002576 int i;
2577
2578 if (WARN(num_entries > max_entries,
2579 "First entry = %d; Num entries = %d (max=%d)\n",
2580 first_entry, num_entries, max_entries))
2581 num_entries = max_entries;
2582
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002583 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002584 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002585
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002586 for (i = 0; i < num_entries; i++)
2587 iowrite32(scratch_pte, &gtt_base[i]);
2588 readl(gtt_base);
2589}
2590
Chris Wilsond6473f52016-06-10 14:22:59 +05302591static void i915_ggtt_insert_page(struct i915_address_space *vm,
2592 dma_addr_t addr,
2593 uint64_t offset,
2594 enum i915_cache_level cache_level,
2595 u32 unused)
2596{
Chris Wilsond6473f52016-06-10 14:22:59 +05302597 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2598 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302599
2600 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302601}
2602
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002603static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2604 struct sg_table *pages,
2605 uint64_t start,
2606 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002607{
2608 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2609 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2610
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002611 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002612
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002613}
2614
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002615static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002616 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002617 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002618{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002619 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002620}
2621
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002622static int ggtt_bind_vma(struct i915_vma *vma,
2623 enum i915_cache_level cache_level,
2624 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002625{
Chris Wilson9c870d02016-10-24 13:42:15 +01002626 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Daniel Vetter0a878712015-10-15 14:23:01 +02002627 struct drm_i915_gem_object *obj = vma->obj;
2628 u32 pte_flags = 0;
2629 int ret;
2630
2631 ret = i915_get_ggtt_vma_pages(vma);
2632 if (ret)
2633 return ret;
2634
2635 /* Currently applicable only to VLV */
2636 if (obj->gt_ro)
2637 pte_flags |= PTE_READ_ONLY;
2638
Chris Wilson9c870d02016-10-24 13:42:15 +01002639 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002640 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002641 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002642 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002643
2644 /*
2645 * Without aliasing PPGTT there's no difference between
2646 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2647 * upgrade to both bound if we bind either to avoid double-binding.
2648 */
Chris Wilson3272db52016-08-04 16:32:32 +01002649 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002650
2651 return 0;
2652}
2653
2654static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2655 enum i915_cache_level cache_level,
2656 u32 flags)
2657{
Chris Wilson9c870d02016-10-24 13:42:15 +01002658 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Chris Wilson321d1782015-11-20 10:27:18 +00002659 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002660 int ret;
2661
2662 ret = i915_get_ggtt_vma_pages(vma);
2663 if (ret)
2664 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002665
Akash Goel24f3a8c2014-06-17 10:59:42 +05302666 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002667 pte_flags = 0;
2668 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002669 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302670
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002671
Chris Wilson3272db52016-08-04 16:32:32 +01002672 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002673 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002674 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002675 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002676 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002677 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002678 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002679
Chris Wilson3272db52016-08-04 16:32:32 +01002680 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002681 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002682 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002683 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002684 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002685 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002686
2687 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002688}
2689
2690static void ggtt_unbind_vma(struct i915_vma *vma)
2691{
Chris Wilson9c870d02016-10-24 13:42:15 +01002692 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2693 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002694 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002695
Chris Wilson9c870d02016-10-24 13:42:15 +01002696 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2697 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002698 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002699 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002700 intel_runtime_pm_put(i915);
2701 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002702
Chris Wilson3272db52016-08-04 16:32:32 +01002703 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002704 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002705 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002706}
2707
Chris Wilson03ac84f2016-10-28 13:58:36 +01002708void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2709 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002710{
David Weinehall52a05c32016-08-22 13:32:44 +03002711 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2712 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002713 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002714
Chris Wilson307dc252016-08-05 10:14:12 +01002715 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002716 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002717 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2718 /* Wait a bit, in hopes it avoids the hang */
2719 udelay(10);
2720 }
2721 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002722
Chris Wilson03ac84f2016-10-28 13:58:36 +01002723 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002724}
Daniel Vetter644ec022012-03-26 09:45:40 +02002725
Chris Wilson42d6ab42012-07-26 11:49:32 +01002726static void i915_gtt_color_adjust(struct drm_mm_node *node,
2727 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002728 u64 *start,
2729 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002730{
2731 if (node->color != color)
2732 *start += 4096;
2733
Chris Wilson2a1d7752016-07-26 12:01:51 +01002734 node = list_first_entry_or_null(&node->node_list,
2735 struct drm_mm_node,
2736 node_list);
2737 if (node && node->allocated && node->color != color)
2738 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002739}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002740
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002741int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002742{
Ben Widawskye78891c2013-01-25 16:41:04 -08002743 /* Let GEM Manage all of the aperture.
2744 *
2745 * However, leave one page at the end still bound to the scratch page.
2746 * There are a number of places where the hardware apparently prefetches
2747 * past the end of the object, and we've seen multiple hangs with the
2748 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2749 * aperture. One page should be enough to keep any prefetching inside
2750 * of the aperture.
2751 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002752 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002753 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002754 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002755 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002756 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002757
Zhi Wangb02d22a2016-06-16 08:06:59 -04002758 ret = intel_vgt_balloon(dev_priv);
2759 if (ret)
2760 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002761
Chris Wilson95374d72016-10-12 10:05:20 +01002762 /* Reserve a mappable slot for our lockless error capture */
2763 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2764 &ggtt->error_capture,
2765 4096, 0, -1,
2766 0, ggtt->mappable_end,
2767 0, 0);
2768 if (ret)
2769 return ret;
2770
Chris Wilsoned2f3452012-11-15 11:32:19 +00002771 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002772 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002773 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2774 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002775 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002776 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002777 }
2778
2779 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002780 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002781 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002782
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002783 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002784 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002785 if (!ppgtt) {
2786 ret = -ENOMEM;
2787 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002788 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002789
Chris Wilson95374d72016-10-12 10:05:20 +01002790 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2791 if (ret)
2792 goto err_ppgtt;
2793
2794 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002795 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2796 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002797 if (ret)
2798 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002799 }
2800
2801 ppgtt->base.clear_range(&ppgtt->base,
2802 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002803 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002804
Daniel Vetterfa76da32014-08-06 20:19:54 +02002805 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002806 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2807 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002808 }
2809
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002810 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002811
2812err_ppgtt_cleanup:
2813 ppgtt->base.cleanup(&ppgtt->base);
2814err_ppgtt:
2815 kfree(ppgtt);
2816err:
2817 drm_mm_remove_node(&ggtt->error_capture);
2818 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002819}
2820
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002821/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002822 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002823 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002824 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002825void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002826{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002827 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002828
Daniel Vetter70e32542014-08-06 15:04:57 +02002829 if (dev_priv->mm.aliasing_ppgtt) {
2830 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002831 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002832 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002833 }
2834
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002835 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002836
Chris Wilson95374d72016-10-12 10:05:20 +01002837 if (drm_mm_node_allocated(&ggtt->error_capture))
2838 drm_mm_remove_node(&ggtt->error_capture);
2839
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002840 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002841 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002842
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002843 drm_mm_takedown(&ggtt->base.mm);
2844 list_del(&ggtt->base.global_link);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002845 }
2846
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002847 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002848
2849 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002850 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002851}
Daniel Vetter70e32542014-08-06 15:04:57 +02002852
Daniel Vetter2c642b02015-04-14 17:35:26 +02002853static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002854{
2855 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2856 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2857 return snb_gmch_ctl << 20;
2858}
2859
Daniel Vetter2c642b02015-04-14 17:35:26 +02002860static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002861{
2862 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2863 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2864 if (bdw_gmch_ctl)
2865 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002866
2867#ifdef CONFIG_X86_32
2868 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2869 if (bdw_gmch_ctl > 4)
2870 bdw_gmch_ctl = 4;
2871#endif
2872
Ben Widawsky9459d252013-11-03 16:53:55 -08002873 return bdw_gmch_ctl << 20;
2874}
2875
Daniel Vetter2c642b02015-04-14 17:35:26 +02002876static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002877{
2878 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2879 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2880
2881 if (gmch_ctrl)
2882 return 1 << (20 + gmch_ctrl);
2883
2884 return 0;
2885}
2886
Daniel Vetter2c642b02015-04-14 17:35:26 +02002887static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002888{
2889 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2890 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2891 return snb_gmch_ctl << 25; /* 32 MB units */
2892}
2893
Daniel Vetter2c642b02015-04-14 17:35:26 +02002894static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002895{
2896 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2897 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2898 return bdw_gmch_ctl << 25; /* 32 MB units */
2899}
2900
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002901static size_t chv_get_stolen_size(u16 gmch_ctrl)
2902{
2903 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2904 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2905
2906 /*
2907 * 0x0 to 0x10: 32MB increments starting at 0MB
2908 * 0x11 to 0x16: 4MB increments starting at 8MB
2909 * 0x17 to 0x1d: 4MB increments start at 36MB
2910 */
2911 if (gmch_ctrl < 0x11)
2912 return gmch_ctrl << 25;
2913 else if (gmch_ctrl < 0x17)
2914 return (gmch_ctrl - 0x11 + 2) << 22;
2915 else
2916 return (gmch_ctrl - 0x17 + 9) << 22;
2917}
2918
Damien Lespiau66375012014-01-09 18:02:46 +00002919static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2920{
2921 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2922 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2923
2924 if (gen9_gmch_ctl < 0xf0)
2925 return gen9_gmch_ctl << 25; /* 32 MB units */
2926 else
2927 /* 4MB increments starting at 0xf0 for 4MB */
2928 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2929}
2930
Chris Wilson34c998b2016-08-04 07:52:24 +01002931static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002932{
Chris Wilson34c998b2016-08-04 07:52:24 +01002933 struct pci_dev *pdev = ggtt->base.dev->pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002934 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002935 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002936
2937 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002938 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002939
Imre Deak2a073f892015-03-27 13:07:33 +02002940 /*
2941 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2942 * dropped. For WC mappings in general we have 64 byte burst writes
2943 * when the WC buffer is flushed, so we can't use it, but have to
2944 * resort to an uncached mapping. The WC issue is easily caught by the
2945 * readback check when writing GTT PTE entries.
2946 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002947 if (IS_BROXTON(to_i915(ggtt->base.dev)))
Chris Wilson34c998b2016-08-04 07:52:24 +01002948 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002949 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002950 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002951 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002952 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002953 return -ENOMEM;
2954 }
2955
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002956 ret = setup_scratch_page(ggtt->base.dev,
2957 &ggtt->base.scratch_page,
2958 GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002959 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002960 DRM_ERROR("Scratch setup failed\n");
2961 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002962 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002963 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002964 }
2965
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002966 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002967}
2968
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002969/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2970 * bits. When using advanced contexts each context stores its own PAT, but
2971 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002972static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002973{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002974 uint64_t pat;
2975
2976 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2977 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2978 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2979 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2980 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2981 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2982 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2983 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2984
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002985 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002986 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2987 * so RTL will always use the value corresponding to
2988 * pat_sel = 000".
2989 * So let's disable cache for GGTT to avoid screen corruptions.
2990 * MOCS still can be used though.
2991 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2992 * before this patch, i.e. the same uncached + snooping access
2993 * like on gen6/7 seems to be in effect.
2994 * - So this just fixes blitter/render access. Again it looks
2995 * like it's not just uncached access, but uncached + snooping.
2996 * So we can still hold onto all our assumptions wrt cpu
2997 * clflushing on LLC machines.
2998 */
2999 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3000
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003001 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3002 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003003 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3004 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003005}
3006
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003007static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3008{
3009 uint64_t pat;
3010
3011 /*
3012 * Map WB on BDW to snooped on CHV.
3013 *
3014 * Only the snoop bit has meaning for CHV, the rest is
3015 * ignored.
3016 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003017 * The hardware will never snoop for certain types of accesses:
3018 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3019 * - PPGTT page tables
3020 * - some other special cycles
3021 *
3022 * As with BDW, we also need to consider the following for GT accesses:
3023 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3024 * so RTL will always use the value corresponding to
3025 * pat_sel = 000".
3026 * Which means we must set the snoop bit in PAT entry 0
3027 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003028 */
3029 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3030 GEN8_PPAT(1, 0) |
3031 GEN8_PPAT(2, 0) |
3032 GEN8_PPAT(3, 0) |
3033 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3034 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3035 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3036 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3037
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003038 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3039 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003040}
3041
Chris Wilson34c998b2016-08-04 07:52:24 +01003042static void gen6_gmch_remove(struct i915_address_space *vm)
3043{
3044 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3045
3046 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01003047 cleanup_scratch_page(vm->dev, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003048}
3049
Joonas Lahtinend507d732016-03-18 10:42:58 +02003050static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003051{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003052 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3053 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003054 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003055 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003056
3057 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003058 ggtt->mappable_base = pci_resource_start(pdev, 2);
3059 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003060
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003061 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3062 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003063
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003064 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003065
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003066 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003067 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003068 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003069 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003070 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003071 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003072 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003073 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003074 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003075 }
Ben Widawsky63340132013-11-04 19:32:22 -08003076
Chris Wilson34c998b2016-08-04 07:52:24 +01003077 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003078
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003079 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003080 chv_setup_private_ppat(dev_priv);
3081 else
3082 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003083
Chris Wilson34c998b2016-08-04 07:52:24 +01003084 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003085 ggtt->base.bind_vma = ggtt_bind_vma;
3086 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303087 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003088 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003089 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003090 ggtt->base.clear_range = gen8_ggtt_clear_range;
3091
3092 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3093 if (IS_CHERRYVIEW(dev_priv))
3094 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3095
Chris Wilson34c998b2016-08-04 07:52:24 +01003096 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003097}
3098
Joonas Lahtinend507d732016-03-18 10:42:58 +02003099static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003100{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003101 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3102 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003103 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003104 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003105
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003106 ggtt->mappable_base = pci_resource_start(pdev, 2);
3107 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003108
Ben Widawskybaa09f52013-01-24 13:49:57 -08003109 /* 64/512MB is the current min/max we actually know of, but this is just
3110 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003111 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003112 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003113 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003114 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003115 }
3116
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003117 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3118 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3119 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003120
Joonas Lahtinend507d732016-03-18 10:42:58 +02003121 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003122
Chris Wilson34c998b2016-08-04 07:52:24 +01003123 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3124 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003125
Joonas Lahtinend507d732016-03-18 10:42:58 +02003126 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303127 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003128 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3129 ggtt->base.bind_vma = ggtt_bind_vma;
3130 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003131 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003132
Chris Wilson34c998b2016-08-04 07:52:24 +01003133 if (HAS_EDRAM(dev_priv))
3134 ggtt->base.pte_encode = iris_pte_encode;
3135 else if (IS_HASWELL(dev_priv))
3136 ggtt->base.pte_encode = hsw_pte_encode;
3137 else if (IS_VALLEYVIEW(dev_priv))
3138 ggtt->base.pte_encode = byt_pte_encode;
3139 else if (INTEL_GEN(dev_priv) >= 7)
3140 ggtt->base.pte_encode = ivb_pte_encode;
3141 else
3142 ggtt->base.pte_encode = snb_pte_encode;
3143
3144 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003145}
3146
Chris Wilson34c998b2016-08-04 07:52:24 +01003147static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003148{
Chris Wilson34c998b2016-08-04 07:52:24 +01003149 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003150}
3151
Joonas Lahtinend507d732016-03-18 10:42:58 +02003152static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003153{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003154 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003155 int ret;
3156
Chris Wilson91c8a322016-07-05 10:40:23 +01003157 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003158 if (!ret) {
3159 DRM_ERROR("failed to set up gmch\n");
3160 return -EIO;
3161 }
3162
Joonas Lahtinend507d732016-03-18 10:42:58 +02003163 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3164 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003165
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003166 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303167 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003168 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3169 ggtt->base.clear_range = i915_ggtt_clear_range;
3170 ggtt->base.bind_vma = ggtt_bind_vma;
3171 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003172 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003173
Joonas Lahtinend507d732016-03-18 10:42:58 +02003174 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003175 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3176
Ben Widawskybaa09f52013-01-24 13:49:57 -08003177 return 0;
3178}
3179
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003180/**
Chris Wilson0088e522016-08-04 07:52:21 +01003181 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003182 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003183 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003184int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003185{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003186 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003187 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003188
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003189 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003190
Chris Wilson34c998b2016-08-04 07:52:24 +01003191 if (INTEL_GEN(dev_priv) <= 5)
3192 ret = i915_gmch_probe(ggtt);
3193 else if (INTEL_GEN(dev_priv) < 8)
3194 ret = gen6_gmch_probe(ggtt);
3195 else
3196 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003197 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003198 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003199
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003200 if ((ggtt->base.total - 1) >> 32) {
3201 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003202 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003203 ggtt->base.total >> 20);
3204 ggtt->base.total = 1ULL << 32;
3205 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3206 }
3207
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003208 if (ggtt->mappable_end > ggtt->base.total) {
3209 DRM_ERROR("mappable aperture extends past end of GGTT,"
3210 " aperture=%llx, total=%llx\n",
3211 ggtt->mappable_end, ggtt->base.total);
3212 ggtt->mappable_end = ggtt->base.total;
3213 }
3214
Ben Widawskybaa09f52013-01-24 13:49:57 -08003215 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003216 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003217 ggtt->base.total >> 20);
3218 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3219 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003220#ifdef CONFIG_INTEL_IOMMU
3221 if (intel_iommu_gfx_mapped)
3222 DRM_INFO("VT-d active for gfx access\n");
3223#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003224
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003225 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003226}
3227
3228/**
3229 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003230 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003231 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003232int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003233{
Chris Wilson0088e522016-08-04 07:52:21 +01003234 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3235 int ret;
3236
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003237 INIT_LIST_HEAD(&dev_priv->vm_list);
3238
3239 /* Subtract the guard page before address space initialization to
3240 * shrink the range used by drm_mm.
3241 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003242 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003243 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003244 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003245 ggtt->base.total += PAGE_SIZE;
3246 if (!HAS_LLC(dev_priv))
3247 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003248 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003249
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003250 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3251 dev_priv->ggtt.mappable_base,
3252 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003253 ret = -EIO;
3254 goto out_gtt_cleanup;
3255 }
3256
3257 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3258
Chris Wilson0088e522016-08-04 07:52:21 +01003259 /*
3260 * Initialise stolen early so that we may reserve preallocated
3261 * objects for the BIOS to KMS transition.
3262 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003263 ret = i915_gem_init_stolen(&dev_priv->drm);
Chris Wilson0088e522016-08-04 07:52:21 +01003264 if (ret)
3265 goto out_gtt_cleanup;
3266
3267 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003268
3269out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003270 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003271 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003272}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003273
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003274int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003275{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003276 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003277 return -EIO;
3278
3279 return 0;
3280}
3281
Daniel Vetterfa423312015-04-14 17:35:23 +02003282void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3283{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003284 struct drm_i915_private *dev_priv = to_i915(dev);
3285 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003286 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003287
Chris Wilsondc979972016-05-10 14:10:04 +01003288 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003289
3290 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003291 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003292
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003293 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3294
3295 /* clflush objects bound into the GGTT and rebind them. */
3296 list_for_each_entry_safe(obj, on,
3297 &dev_priv->mm.bound_list, global_list) {
3298 bool ggtt_bound = false;
3299 struct i915_vma *vma;
3300
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003301 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003302 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003303 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003304
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003305 if (!i915_vma_unbind(vma))
3306 continue;
3307
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003308 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3309 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003310 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003311 }
3312
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003313 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003314 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003315 }
3316
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003317 ggtt->base.closed = false;
3318
Daniel Vetterfa423312015-04-14 17:35:23 +02003319 if (INTEL_INFO(dev)->gen >= 8) {
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01003320 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003321 chv_setup_private_ppat(dev_priv);
3322 else
3323 bdw_setup_private_ppat(dev_priv);
3324
3325 return;
3326 }
3327
3328 if (USES_PPGTT(dev)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003329 struct i915_address_space *vm;
3330
Daniel Vetterfa423312015-04-14 17:35:23 +02003331 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3332 /* TODO: Perhaps it shouldn't be gen6 specific */
3333
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003334 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003335
Chris Wilson2bfa9962016-08-04 07:52:25 +01003336 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003337 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003338 else
3339 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003340
3341 gen6_write_page_range(dev_priv, &ppgtt->pd,
3342 0, ppgtt->base.total);
3343 }
3344 }
3345
3346 i915_ggtt_flush(dev_priv);
3347}
3348
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003349static void
3350i915_vma_retire(struct i915_gem_active *active,
3351 struct drm_i915_gem_request *rq)
3352{
3353 const unsigned int idx = rq->engine->id;
3354 struct i915_vma *vma =
3355 container_of(active, struct i915_vma, last_read[idx]);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003356 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003357
3358 GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
3359
3360 i915_vma_clear_active(vma, idx);
3361 if (i915_vma_is_active(vma))
3362 return;
3363
3364 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Chris Wilson3272db52016-08-04 16:32:32 +01003365 if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003366 WARN_ON(i915_vma_unbind(vma));
Chris Wilsond07f0e52016-10-28 13:58:44 +01003367
3368 GEM_BUG_ON(!i915_gem_object_is_active(obj));
3369 if (--obj->active_count)
3370 return;
3371
3372 /* Bump our place on the bound list to keep it roughly in LRU order
3373 * so that we don't steal from recently used but inactive objects
3374 * (unless we are forced to ofc!)
3375 */
3376 if (obj->bind_count)
3377 list_move_tail(&obj->global_list, &rq->i915->mm.bound_list);
3378
3379 obj->mm.dirty = true; /* be paranoid */
3380
3381 if (i915_gem_object_has_active_reference(obj)) {
3382 i915_gem_object_clear_active_reference(obj);
3383 i915_gem_object_put(obj);
3384 }
3385}
3386
3387static void
3388i915_ggtt_retire__write(struct i915_gem_active *active,
3389 struct drm_i915_gem_request *request)
3390{
3391 struct i915_vma *vma =
3392 container_of(active, struct i915_vma, last_write);
3393
3394 intel_fb_obj_flush(vma->obj, true, ORIGIN_CS);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003395}
3396
3397void i915_vma_destroy(struct i915_vma *vma)
3398{
3399 GEM_BUG_ON(vma->node.allocated);
3400 GEM_BUG_ON(i915_vma_is_active(vma));
Chris Wilson3272db52016-08-04 16:32:32 +01003401 GEM_BUG_ON(!i915_vma_is_closed(vma));
Chris Wilson49ef5292016-08-18 17:17:00 +01003402 GEM_BUG_ON(vma->fence);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003403
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003404 rb_erase(&vma->obj_node, &vma->obj->vma_tree);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003405 list_del(&vma->vm_link);
Chris Wilson3272db52016-08-04 16:32:32 +01003406 if (!i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003407 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
3408
3409 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
3410}
3411
3412void i915_vma_close(struct i915_vma *vma)
3413{
Chris Wilson3272db52016-08-04 16:32:32 +01003414 GEM_BUG_ON(i915_vma_is_closed(vma));
3415 vma->flags |= I915_VMA_CLOSED;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003416
3417 list_del_init(&vma->obj_link);
Chris Wilson20dfbde2016-08-04 16:32:30 +01003418 if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003419 WARN_ON(i915_vma_unbind(vma));
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003420}
3421
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003422static inline long vma_compare(struct i915_vma *vma,
3423 struct i915_address_space *vm,
3424 const struct i915_ggtt_view *view)
3425{
3426 GEM_BUG_ON(view && !i915_vma_is_ggtt(vma));
3427
3428 if (vma->vm != vm)
3429 return vma->vm - vm;
3430
3431 if (!view)
3432 return vma->ggtt_view.type;
3433
3434 if (vma->ggtt_view.type != view->type)
3435 return vma->ggtt_view.type - view->type;
3436
3437 return memcmp(&vma->ggtt_view.params,
3438 &view->params,
3439 sizeof(view->params));
3440}
3441
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003442static struct i915_vma *
Chris Wilson058d88c2016-08-15 10:49:06 +01003443__i915_vma_create(struct drm_i915_gem_object *obj,
3444 struct i915_address_space *vm,
3445 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003446{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003447 struct i915_vma *vma;
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003448 struct rb_node *rb, **p;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003449 int i;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003450
Chris Wilson50e046b2016-08-04 07:52:46 +01003451 GEM_BUG_ON(vm->closed);
3452
Chris Wilsone20d2ab2015-04-07 16:20:58 +01003453 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003454 if (vma == NULL)
3455 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003456
Ben Widawsky6f65e292013-12-06 14:10:56 -08003457 INIT_LIST_HEAD(&vma->exec_list);
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003458 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
3459 init_request_active(&vma->last_read[i], i915_vma_retire);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003460 init_request_active(&vma->last_write,
3461 i915_is_ggtt(vm) ? i915_ggtt_retire__write : NULL);
Chris Wilson49ef5292016-08-18 17:17:00 +01003462 init_request_active(&vma->last_fence, NULL);
Chris Wilson50e046b2016-08-04 07:52:46 +01003463 list_add(&vma->vm_link, &vm->unbound_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003464 vma->vm = vm;
3465 vma->obj = obj;
Chris Wilsonde180032016-08-04 16:32:29 +01003466 vma->size = obj->base.size;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003467
Chris Wilson058d88c2016-08-15 10:49:06 +01003468 if (view) {
Chris Wilsonde180032016-08-04 16:32:29 +01003469 vma->ggtt_view = *view;
3470 if (view->type == I915_GGTT_VIEW_PARTIAL) {
3471 vma->size = view->params.partial.size;
3472 vma->size <<= PAGE_SHIFT;
3473 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3474 vma->size =
3475 intel_rotation_info_size(&view->params.rotated);
3476 vma->size <<= PAGE_SHIFT;
3477 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003478 }
3479
3480 if (i915_is_ggtt(vm)) {
3481 vma->flags |= I915_VMA_GGTT;
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003482 list_add(&vma->obj_link, &obj->vma_list);
Chris Wilsonde180032016-08-04 16:32:29 +01003483 } else {
Chris Wilson596c5922016-02-26 11:03:20 +00003484 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003485 list_add_tail(&vma->obj_link, &obj->vma_list);
Chris Wilsonde180032016-08-04 16:32:29 +01003486 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08003487
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003488 rb = NULL;
3489 p = &obj->vma_tree.rb_node;
3490 while (*p) {
3491 struct i915_vma *pos;
3492
3493 rb = *p;
3494 pos = rb_entry(rb, struct i915_vma, obj_node);
3495 if (vma_compare(pos, vm, view) < 0)
3496 p = &rb->rb_right;
3497 else
3498 p = &rb->rb_left;
3499 }
3500 rb_link_node(&vma->obj_node, rb, p);
3501 rb_insert_color(&vma->obj_node, &obj->vma_tree);
3502
Ben Widawsky6f65e292013-12-06 14:10:56 -08003503 return vma;
3504}
3505
3506struct i915_vma *
Chris Wilson81a8aa42016-08-15 10:48:48 +01003507i915_vma_create(struct drm_i915_gem_object *obj,
3508 struct i915_address_space *vm,
3509 const struct i915_ggtt_view *view)
3510{
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003511 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson81a8aa42016-08-15 10:48:48 +01003512 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Chris Wilson058d88c2016-08-15 10:49:06 +01003513 GEM_BUG_ON(i915_gem_obj_to_vma(obj, vm, view));
Chris Wilson81a8aa42016-08-15 10:48:48 +01003514
Chris Wilson058d88c2016-08-15 10:49:06 +01003515 return __i915_vma_create(obj, vm, view);
3516}
3517
3518struct i915_vma *
3519i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3520 struct i915_address_space *vm,
3521 const struct i915_ggtt_view *view)
3522{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003523 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003524
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003525 rb = obj->vma_tree.rb_node;
3526 while (rb) {
3527 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3528 long cmp;
3529
3530 cmp = vma_compare(vma, vm, view);
3531 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003532 return vma;
3533
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003534 if (cmp < 0)
3535 rb = rb->rb_right;
3536 else
3537 rb = rb->rb_left;
3538 }
3539
Chris Wilson058d88c2016-08-15 10:49:06 +01003540 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003541}
3542
3543struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003544i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003545 struct i915_address_space *vm,
3546 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003547{
3548 struct i915_vma *vma;
3549
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003550 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003551 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3552
3553 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003554 if (!vma) {
Chris Wilson058d88c2016-08-15 10:49:06 +01003555 vma = __i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003556 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3557 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003558
Chris Wilson3272db52016-08-04 16:32:32 +01003559 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003560 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003561}
3562
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003563static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003564rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003565 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003566 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003567 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003568{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003569 unsigned int column, row;
3570 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003571
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003572 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003573 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003574 for (row = 0; row < height; row++) {
3575 st->nents++;
3576 /* We don't need the pages, but need to initialize
3577 * the entries so the sg list can be happily traversed.
3578 * The only thing we need are DMA addresses.
3579 */
3580 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003581 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003582 sg_dma_len(sg) = PAGE_SIZE;
3583 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003584 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003585 }
3586 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003587
3588 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003589}
3590
3591static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003592intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003593 struct drm_i915_gem_object *obj)
3594{
Dave Gordon85d12252016-05-20 11:54:06 +01003595 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003596 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003597 struct sgt_iter sgt_iter;
3598 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003599 unsigned long i;
3600 dma_addr_t *page_addr_list;
3601 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003602 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003603 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003604
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003605 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003606 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003607 sizeof(dma_addr_t),
3608 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003609 if (!page_addr_list)
3610 return ERR_PTR(ret);
3611
3612 /* Allocate target SG list. */
3613 st = kmalloc(sizeof(*st), GFP_KERNEL);
3614 if (!st)
3615 goto err_st_alloc;
3616
Ville Syrjälä6687c902015-09-15 13:16:41 +03003617 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003618 if (ret)
3619 goto err_sg_alloc;
3620
3621 /* Populate source page list from the object. */
3622 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003623 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003624 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003625
Dave Gordon85d12252016-05-20 11:54:06 +01003626 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003627 st->nents = 0;
3628 sg = st->sgl;
3629
Ville Syrjälä6687c902015-09-15 13:16:41 +03003630 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3631 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3632 rot_info->plane[i].width, rot_info->plane[i].height,
3633 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003634 }
3635
Ville Syrjälä6687c902015-09-15 13:16:41 +03003636 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3637 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003638
3639 drm_free_large(page_addr_list);
3640
3641 return st;
3642
3643err_sg_alloc:
3644 kfree(st);
3645err_st_alloc:
3646 drm_free_large(page_addr_list);
3647
Ville Syrjälä6687c902015-09-15 13:16:41 +03003648 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3649 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3650
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003651 return ERR_PTR(ret);
3652}
3653
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003654static struct sg_table *
3655intel_partial_pages(const struct i915_ggtt_view *view,
3656 struct drm_i915_gem_object *obj)
3657{
3658 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003659 struct scatterlist *sg, *iter;
3660 unsigned int count = view->params.partial.size;
3661 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003662 int ret = -ENOMEM;
3663
3664 st = kmalloc(sizeof(*st), GFP_KERNEL);
3665 if (!st)
3666 goto err_st_alloc;
3667
Chris Wilsond2a84a72016-10-28 13:58:34 +01003668 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003669 if (ret)
3670 goto err_sg_alloc;
3671
Chris Wilsond2a84a72016-10-28 13:58:34 +01003672 iter = i915_gem_object_get_sg(obj,
3673 view->params.partial.offset,
3674 &offset);
3675 GEM_BUG_ON(!iter);
3676
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003677 sg = st->sgl;
3678 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003679 do {
3680 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003681
Chris Wilsond2a84a72016-10-28 13:58:34 +01003682 len = min(iter->length - (offset << PAGE_SHIFT),
3683 count << PAGE_SHIFT);
3684 sg_set_page(sg, NULL, len, 0);
3685 sg_dma_address(sg) =
3686 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3687 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003688
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003689 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003690 count -= len >> PAGE_SHIFT;
3691 if (count == 0) {
3692 sg_mark_end(sg);
3693 return st;
3694 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003695
Chris Wilsond2a84a72016-10-28 13:58:34 +01003696 sg = __sg_next(sg);
3697 iter = __sg_next(iter);
3698 offset = 0;
3699 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003700
3701err_sg_alloc:
3702 kfree(st);
3703err_st_alloc:
3704 return ERR_PTR(ret);
3705}
3706
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003707static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003708i915_get_ggtt_vma_pages(struct i915_vma *vma)
3709{
3710 int ret = 0;
3711
Chris Wilson247177d2016-08-15 10:48:47 +01003712 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003713 return 0;
3714
3715 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003716 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003717 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003718 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003719 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003720 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003721 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003722 else
3723 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3724 vma->ggtt_view.type);
3725
Chris Wilson247177d2016-08-15 10:48:47 +01003726 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003727 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003728 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003729 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003730 } else if (IS_ERR(vma->pages)) {
3731 ret = PTR_ERR(vma->pages);
3732 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003733 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3734 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003735 }
3736
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003737 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003738}
3739
3740/**
3741 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3742 * @vma: VMA to map
3743 * @cache_level: mapping cache level
3744 * @flags: flags like global or local mapping
3745 *
3746 * DMA addresses are taken from the scatter-gather table of this object (or of
3747 * this VMA in case of non-default GGTT views) and PTE entries set up.
3748 * Note that DMA addresses are also the only part of the SG table we care about.
3749 */
3750int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3751 u32 flags)
3752{
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003753 u32 bind_flags;
Chris Wilson3272db52016-08-04 16:32:32 +01003754 u32 vma_flags;
3755 int ret;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003756
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003757 if (WARN_ON(flags == 0))
3758 return -EINVAL;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003759
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003760 bind_flags = 0;
Daniel Vetter08755462015-04-20 09:04:05 -07003761 if (flags & PIN_GLOBAL)
Chris Wilson3272db52016-08-04 16:32:32 +01003762 bind_flags |= I915_VMA_GLOBAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003763 if (flags & PIN_USER)
Chris Wilson3272db52016-08-04 16:32:32 +01003764 bind_flags |= I915_VMA_LOCAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003765
Chris Wilson3272db52016-08-04 16:32:32 +01003766 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
Daniel Vetter08755462015-04-20 09:04:05 -07003767 if (flags & PIN_UPDATE)
Chris Wilson3272db52016-08-04 16:32:32 +01003768 bind_flags |= vma_flags;
Daniel Vetter08755462015-04-20 09:04:05 -07003769 else
Chris Wilson3272db52016-08-04 16:32:32 +01003770 bind_flags &= ~vma_flags;
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003771 if (bind_flags == 0)
3772 return 0;
3773
Chris Wilson3272db52016-08-04 16:32:32 +01003774 if (vma_flags == 0 && vma->vm->allocate_va_range) {
Chris Wilson596c5922016-02-26 11:03:20 +00003775 trace_i915_va_alloc(vma);
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003776 ret = vma->vm->allocate_va_range(vma->vm,
3777 vma->node.start,
3778 vma->node.size);
3779 if (ret)
3780 return ret;
3781 }
3782
3783 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003784 if (ret)
3785 return ret;
Daniel Vetter08755462015-04-20 09:04:05 -07003786
Chris Wilson3272db52016-08-04 16:32:32 +01003787 vma->flags |= bind_flags;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003788 return 0;
3789}
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003790
Chris Wilson8ef85612016-04-28 09:56:39 +01003791void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
3792{
3793 void __iomem *ptr;
3794
Chris Wilsone5cdb222016-08-15 10:48:56 +01003795 /* Access through the GTT requires the device to be awake. */
3796 assert_rpm_wakelock_held(to_i915(vma->vm->dev));
3797
Chris Wilson8ef85612016-04-28 09:56:39 +01003798 lockdep_assert_held(&vma->vm->dev->struct_mutex);
Chris Wilson05a20d02016-08-18 17:16:55 +01003799 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma)))
Chris Wilson406ea8d2016-07-20 13:31:55 +01003800 return IO_ERR_PTR(-ENODEV);
Chris Wilson8ef85612016-04-28 09:56:39 +01003801
Chris Wilson3272db52016-08-04 16:32:32 +01003802 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
3803 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
Chris Wilson8ef85612016-04-28 09:56:39 +01003804
3805 ptr = vma->iomap;
3806 if (ptr == NULL) {
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003807 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
Chris Wilson8ef85612016-04-28 09:56:39 +01003808 vma->node.start,
3809 vma->node.size);
3810 if (ptr == NULL)
Chris Wilson406ea8d2016-07-20 13:31:55 +01003811 return IO_ERR_PTR(-ENOMEM);
Chris Wilson8ef85612016-04-28 09:56:39 +01003812
3813 vma->iomap = ptr;
3814 }
3815
Chris Wilson20dfbde2016-08-04 16:32:30 +01003816 __i915_vma_pin(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +01003817 return ptr;
3818}
Chris Wilson19880c42016-08-15 10:49:05 +01003819
3820void i915_vma_unpin_and_release(struct i915_vma **p_vma)
3821{
3822 struct i915_vma *vma;
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003823 struct drm_i915_gem_object *obj;
Chris Wilson19880c42016-08-15 10:49:05 +01003824
3825 vma = fetch_and_zero(p_vma);
3826 if (!vma)
3827 return;
3828
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003829 obj = vma->obj;
3830
Chris Wilson19880c42016-08-15 10:49:05 +01003831 i915_vma_unpin(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003832 i915_vma_close(vma);
3833
3834 __i915_gem_object_release_unless_active(obj);
Chris Wilson19880c42016-08-15 10:49:05 +01003835}