blob: a5fafa3d4fc80c36d8bc48f26bf71412e635de68 [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
Mika Kuoppalafce93752016-10-31 17:24:46 +0200710/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
711 * the page table structures, we mark them dirty so that
712 * context switching/execlist queuing code takes extra steps
713 * to ensure that tlbs are flushed.
714 */
715static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
716{
717 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
718}
719
Michał Winiarski2ce51792016-10-13 14:02:42 +0200720/* Removes entries from a single page table, releasing it if it's empty.
721 * Caller can use the return value to update higher-level entries.
722 */
723static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200724 struct i915_page_table *pt,
725 uint64_t start,
726 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700727{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300728 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200729 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200730 unsigned int pte = gen8_pte_index(start);
731 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200732 gen8_pte_t *pt_vaddr;
733 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
734 I915_CACHE_LLC);
735
736 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200737 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700738
Mika Kuoppala37c63932016-11-01 15:27:36 +0200739 GEM_BUG_ON(pte_end > GEN8_PTES);
740
741 bitmap_clear(pt->used_ptes, pte, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000742
Michał Winiarski2ce51792016-10-13 14:02:42 +0200743 if (bitmap_empty(pt->used_ptes, GEN8_PTES)) {
744 free_pt(vm->dev, pt);
745 return true;
746 }
747
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200748 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000749
Mika Kuoppala37c63932016-11-01 15:27:36 +0200750 while (pte < pte_end)
751 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000752
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200753 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200754
755 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200756}
757
Michał Winiarski2ce51792016-10-13 14:02:42 +0200758/* Removes entries from a single page dir, releasing it if it's empty.
759 * Caller can use the return value to update higher-level entries
760 */
761static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200762 struct i915_page_directory *pd,
763 uint64_t start,
764 uint64_t length)
765{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200766 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200767 struct i915_page_table *pt;
768 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200769 gen8_pde_t *pde_vaddr;
770 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
771 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200772
773 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000774 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100775 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000776
Michał Winiarski2ce51792016-10-13 14:02:42 +0200777 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
778 __clear_bit(pde, pd->used_pdes);
779 pde_vaddr = kmap_px(pd);
780 pde_vaddr[pde] = scratch_pde;
781 kunmap_px(ppgtt, pde_vaddr);
782 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200783 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200784
785 if (bitmap_empty(pd->used_pdes, I915_PDES)) {
786 free_pd(vm->dev, pd);
787 return true;
788 }
789
790 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200791}
Ben Widawsky06fda602015-02-24 16:22:36 +0000792
Michał Winiarski2ce51792016-10-13 14:02:42 +0200793/* Removes entries from a single page dir pointer, releasing it if it's empty.
794 * Caller can use the return value to update higher-level entries
795 */
796static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200797 struct i915_page_directory_pointer *pdp,
798 uint64_t start,
799 uint64_t length)
800{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200801 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200802 struct i915_page_directory *pd;
803 uint64_t pdpe;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200804 gen8_ppgtt_pdpe_t *pdpe_vaddr;
805 gen8_ppgtt_pdpe_t scratch_pdpe =
806 gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200807
808 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
809 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100810 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000811
Michał Winiarski2ce51792016-10-13 14:02:42 +0200812 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
813 __clear_bit(pdpe, pdp->used_pdpes);
814 if (USES_FULL_48BIT_PPGTT(vm->dev)) {
815 pdpe_vaddr = kmap_px(pdp);
816 pdpe_vaddr[pdpe] = scratch_pdpe;
817 kunmap_px(ppgtt, pdpe_vaddr);
818 }
819 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200820 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200821
Mika Kuoppalafce93752016-10-31 17:24:46 +0200822 mark_tlbs_dirty(ppgtt);
823
Michał Winiarski2ce51792016-10-13 14:02:42 +0200824 if (USES_FULL_48BIT_PPGTT(vm->dev) &&
825 bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(vm->dev))) {
826 free_pdp(vm->dev, pdp);
827 return true;
828 }
829
830 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200831}
Ben Widawsky459108b2013-11-02 21:07:23 -0700832
Michał Winiarski2ce51792016-10-13 14:02:42 +0200833/* Removes entries from a single pml4.
834 * This is the top-level structure in 4-level page tables used on gen8+.
835 * Empty entries are always scratch pml4e.
836 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200837static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
838 struct i915_pml4 *pml4,
839 uint64_t start,
840 uint64_t length)
841{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200842 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200843 struct i915_page_directory_pointer *pdp;
844 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200845 gen8_ppgtt_pml4e_t *pml4e_vaddr;
846 gen8_ppgtt_pml4e_t scratch_pml4e =
847 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
848
849 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->dev));
Ben Widawsky459108b2013-11-02 21:07:23 -0700850
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200851 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
852 if (WARN_ON(!pml4->pdps[pml4e]))
853 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700854
Michał Winiarski2ce51792016-10-13 14:02:42 +0200855 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
856 __clear_bit(pml4e, pml4->used_pml4es);
857 pml4e_vaddr = kmap_px(pml4);
858 pml4e_vaddr[pml4e] = scratch_pml4e;
859 kunmap_px(ppgtt, pml4e_vaddr);
860 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700861 }
862}
863
Michel Thierryf9b5b782015-07-30 11:02:49 +0100864static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200865 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700866{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300867 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100868
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200869 if (USES_FULL_48BIT_PPGTT(vm->dev))
870 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
871 else
872 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100873}
874
875static void
876gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
877 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100878 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100879 uint64_t start,
880 enum i915_cache_level cache_level)
881{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300882 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000883 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100884 unsigned pdpe = gen8_pdpe_index(start);
885 unsigned pde = gen8_pde_index(start);
886 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700887
Chris Wilson6f1cc992013-12-31 15:50:31 +0000888 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700889
Michel Thierry3387d432015-08-03 09:52:47 +0100890 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000891 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100892 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100893 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300894 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000895 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800896
897 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100898 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200899 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000900 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300901 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000902 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000903 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100904 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
905 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800906 pde = 0;
907 }
908 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700909 }
910 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300911
912 if (pt_vaddr)
913 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700914}
915
Michel Thierryf9b5b782015-07-30 11:02:49 +0100916static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
917 struct sg_table *pages,
918 uint64_t start,
919 enum i915_cache_level cache_level,
920 u32 unused)
921{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300922 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100923 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100924
Michel Thierry3387d432015-08-03 09:52:47 +0100925 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100926
927 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
928 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
929 cache_level);
930 } else {
931 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000932 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100933 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
934
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000935 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100936 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
937 start, cache_level);
938 }
939 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100940}
941
Michel Thierryf37c0502015-06-10 17:46:39 +0100942static void gen8_free_page_tables(struct drm_device *dev,
943 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800944{
945 int i;
946
Mika Kuoppala567047b2015-06-25 18:35:12 +0300947 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800948 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800949
Michel Thierry33c88192015-04-08 12:13:33 +0100950 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000951 if (WARN_ON(!pd->page_table[i]))
952 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800953
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300954 free_pt(dev, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000955 pd->page_table[i] = NULL;
956 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000957}
958
Mika Kuoppala8776f022015-06-30 18:16:40 +0300959static int gen8_init_scratch(struct i915_address_space *vm)
960{
961 struct drm_device *dev = vm->dev;
Matthew Auld64c050d2016-04-27 13:19:25 +0100962 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300963
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100964 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100965 if (ret)
966 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300967
968 vm->scratch_pt = alloc_pt(dev);
969 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100970 ret = PTR_ERR(vm->scratch_pt);
971 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300972 }
973
974 vm->scratch_pd = alloc_pd(dev);
975 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100976 ret = PTR_ERR(vm->scratch_pd);
977 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300978 }
979
Michel Thierry69ab76f2015-07-29 17:23:55 +0100980 if (USES_FULL_48BIT_PPGTT(dev)) {
981 vm->scratch_pdp = alloc_pdp(dev);
982 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100983 ret = PTR_ERR(vm->scratch_pdp);
984 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100985 }
986 }
987
Mika Kuoppala8776f022015-06-30 18:16:40 +0300988 gen8_initialize_pt(vm, vm->scratch_pt);
989 gen8_initialize_pd(vm, vm->scratch_pd);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100990 if (USES_FULL_48BIT_PPGTT(dev))
991 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300992
993 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100994
995free_pd:
996 free_pd(dev, vm->scratch_pd);
997free_pt:
998 free_pt(dev, vm->scratch_pt);
999free_scratch_page:
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001000 cleanup_scratch_page(dev, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +01001001
1002 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001003}
1004
Zhiyuan Lv650da342015-08-28 15:41:18 +08001005static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1006{
1007 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +01001008 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +08001009 int i;
1010
Matthew Aulddf285642016-04-22 12:09:25 +01001011 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001012 u64 daddr = px_dma(&ppgtt->pml4);
1013
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001014 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1015 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001016
1017 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1018 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1019 } else {
1020 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1021 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1022
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001023 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1024 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001025 }
1026
1027 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1028 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1029 }
1030
1031 I915_WRITE(vgtif_reg(g2v_notify), msg);
1032
1033 return 0;
1034}
1035
Mika Kuoppala8776f022015-06-30 18:16:40 +03001036static void gen8_free_scratch(struct i915_address_space *vm)
1037{
1038 struct drm_device *dev = vm->dev;
1039
Michel Thierry69ab76f2015-07-29 17:23:55 +01001040 if (USES_FULL_48BIT_PPGTT(dev))
1041 free_pdp(dev, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001042 free_pd(dev, vm->scratch_pd);
1043 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001044 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001045}
1046
Michel Thierry762d9932015-07-30 11:05:29 +01001047static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
1048 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001049{
1050 int i;
1051
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001052 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
1053 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001054 continue;
1055
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001056 gen8_free_page_tables(dev, pdp->page_directory[i]);
1057 free_pd(dev, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001058 }
Michel Thierry69876be2015-04-08 12:13:27 +01001059
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001060 free_pdp(dev, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001061}
1062
1063static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1064{
1065 int i;
1066
1067 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1068 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1069 continue;
1070
1071 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
1072 }
1073
1074 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
1075}
1076
1077static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1078{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001079 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001080
Chris Wilsonc0336662016-05-06 15:40:21 +01001081 if (intel_vgpu_active(to_i915(vm->dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001082 gen8_ppgtt_notify_vgt(ppgtt, false);
1083
Michel Thierry762d9932015-07-30 11:05:29 +01001084 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1085 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1086 else
1087 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001088
Mika Kuoppala8776f022015-06-30 18:16:40 +03001089 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001090}
1091
Michel Thierryd7b26332015-04-08 12:13:34 +01001092/**
1093 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001094 * @vm: Master vm structure.
1095 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001096 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001097 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001098 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1099 * caller to free on error.
1100 *
1101 * Allocate the required number of page tables. Extremely similar to
1102 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1103 * the page directory boundary (instead of the page directory pointer). That
1104 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1105 * possible, and likely that the caller will need to use multiple calls of this
1106 * function to achieve the appropriate allocation.
1107 *
1108 * Return: 0 if success; negative error code otherwise.
1109 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001110static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001111 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001112 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001113 uint64_t length,
1114 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001115{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001116 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001117 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001118 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001119
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001120 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001121 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001122 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001123 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001124 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001125 continue;
1126 }
1127
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001128 pt = alloc_pt(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001129 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001130 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001131
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001132 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001133 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001134 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001135 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001136 }
1137
1138 return 0;
1139
1140unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001141 for_each_set_bit(pde, new_pts, I915_PDES)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001142 free_pt(dev, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001143
1144 return -ENOMEM;
1145}
1146
Michel Thierryd7b26332015-04-08 12:13:34 +01001147/**
1148 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001149 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001150 * @pdp: Page directory pointer for this address range.
1151 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001152 * @length: Size of the allocations.
1153 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001154 * caller to free on error.
1155 *
1156 * Allocate the required number of page directories starting at the pde index of
1157 * @start, and ending at the pde index @start + @length. This function will skip
1158 * over already allocated page directories within the range, and only allocate
1159 * new ones, setting the appropriate pointer within the pdp as well as the
1160 * correct position in the bitmap @new_pds.
1161 *
1162 * The function will only allocate the pages within the range for a give page
1163 * directory pointer. In other words, if @start + @length straddles a virtually
1164 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1165 * required by the caller, This is not currently possible, and the BUG in the
1166 * code will prevent it.
1167 *
1168 * Return: 0 if success; negative error code otherwise.
1169 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001170static int
1171gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1172 struct i915_page_directory_pointer *pdp,
1173 uint64_t start,
1174 uint64_t length,
1175 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001176{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001177 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001178 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001179 uint32_t pdpe;
Michel Thierry6ac18502015-07-29 17:23:46 +01001180 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001181
Michel Thierry6ac18502015-07-29 17:23:46 +01001182 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001183
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001184 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001185 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001186 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001187
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001188 pd = alloc_pd(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001189 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001190 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001191
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001192 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001193 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001194 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001195 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001196 }
1197
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001198 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001199
1200unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001201 for_each_set_bit(pdpe, new_pds, pdpes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001202 free_pd(dev, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001203
1204 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001205}
1206
Michel Thierry762d9932015-07-30 11:05:29 +01001207/**
1208 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1209 * @vm: Master vm structure.
1210 * @pml4: Page map level 4 for this address range.
1211 * @start: Starting virtual address to begin allocations.
1212 * @length: Size of the allocations.
1213 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1214 * caller to free on error.
1215 *
1216 * Allocate the required number of page directory pointers. Extremely similar to
1217 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1218 * The main difference is here we are limited by the pml4 boundary (instead of
1219 * the page directory pointer).
1220 *
1221 * Return: 0 if success; negative error code otherwise.
1222 */
1223static int
1224gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1225 struct i915_pml4 *pml4,
1226 uint64_t start,
1227 uint64_t length,
1228 unsigned long *new_pdps)
1229{
1230 struct drm_device *dev = vm->dev;
1231 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001232 uint32_t pml4e;
1233
1234 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1235
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001236 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001237 if (!test_bit(pml4e, pml4->used_pml4es)) {
1238 pdp = alloc_pdp(dev);
1239 if (IS_ERR(pdp))
1240 goto unwind_out;
1241
Michel Thierry69ab76f2015-07-29 17:23:55 +01001242 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001243 pml4->pdps[pml4e] = pdp;
1244 __set_bit(pml4e, new_pdps);
1245 trace_i915_page_directory_pointer_entry_alloc(vm,
1246 pml4e,
1247 start,
1248 GEN8_PML4E_SHIFT);
1249 }
1250 }
1251
1252 return 0;
1253
1254unwind_out:
1255 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1256 free_pdp(dev, pml4->pdps[pml4e]);
1257
1258 return -ENOMEM;
1259}
1260
Michel Thierryd7b26332015-04-08 12:13:34 +01001261static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001262free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001263{
Michel Thierryd7b26332015-04-08 12:13:34 +01001264 kfree(new_pts);
1265 kfree(new_pds);
1266}
1267
1268/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1269 * of these are based on the number of PDPEs in the system.
1270 */
1271static
1272int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001273 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001274 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001275{
Michel Thierryd7b26332015-04-08 12:13:34 +01001276 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001277 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001278
Michał Winiarski3a41a052015-09-03 19:22:18 +02001279 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001280 if (!pds)
1281 return -ENOMEM;
1282
Michał Winiarski3a41a052015-09-03 19:22:18 +02001283 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1284 GFP_TEMPORARY);
1285 if (!pts)
1286 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001287
1288 *new_pds = pds;
1289 *new_pts = pts;
1290
1291 return 0;
1292
1293err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001294 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001295 return -ENOMEM;
1296}
1297
Michel Thierry762d9932015-07-30 11:05:29 +01001298static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1299 struct i915_page_directory_pointer *pdp,
1300 uint64_t start,
1301 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001302{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001303 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001304 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001305 struct drm_device *dev = vm->dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001306 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001307 const uint64_t orig_start = start;
1308 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001309 uint32_t pdpe;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001310 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001311 int ret;
1312
Michel Thierryd7b26332015-04-08 12:13:34 +01001313 /* Wrap is never okay since we can only represent 48b, and we don't
1314 * actually use the other side of the canonical address space.
1315 */
1316 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001317 return -ENODEV;
1318
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001319 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001320 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001321
Michel Thierry6ac18502015-07-29 17:23:46 +01001322 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001323 if (ret)
1324 return ret;
1325
Michel Thierryd7b26332015-04-08 12:13:34 +01001326 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001327 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1328 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001329 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001330 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001331 return ret;
1332 }
1333
1334 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001335 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001336 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001337 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001338 if (ret)
1339 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001340 }
1341
Michel Thierry33c88192015-04-08 12:13:33 +01001342 start = orig_start;
1343 length = orig_length;
1344
Michel Thierryd7b26332015-04-08 12:13:34 +01001345 /* Allocations have completed successfully, so set the bitmaps, and do
1346 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001347 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001348 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001349 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001350 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001351 uint64_t pd_start = start;
1352 uint32_t pde;
1353
Michel Thierryd7b26332015-04-08 12:13:34 +01001354 /* Every pd should be allocated, we just did that above. */
1355 WARN_ON(!pd);
1356
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001357 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001358 /* Same reasoning as pd */
1359 WARN_ON(!pt);
1360 WARN_ON(!pd_len);
1361 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1362
1363 /* Set our used ptes within the page table */
1364 bitmap_set(pt->used_ptes,
1365 gen8_pte_index(pd_start),
1366 gen8_pte_count(pd_start, pd_len));
1367
1368 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001369 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001370
1371 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001372 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1373 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001374 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1375 gen8_pte_index(start),
1376 gen8_pte_count(start, length),
1377 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001378
1379 /* NB: We haven't yet mapped ptes to pages. At this
1380 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001381 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001382
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001383 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001384 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001385 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001386 }
1387
Michał Winiarski3a41a052015-09-03 19:22:18 +02001388 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001389 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001390 return 0;
1391
1392err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001393 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001394 unsigned long temp;
1395
Michał Winiarski3a41a052015-09-03 19:22:18 +02001396 for_each_set_bit(temp, new_page_tables + pdpe *
1397 BITS_TO_LONGS(I915_PDES), I915_PDES)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001398 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001399 }
1400
Michel Thierry6ac18502015-07-29 17:23:46 +01001401 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001402 free_pd(dev, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001403
Michał Winiarski3a41a052015-09-03 19:22:18 +02001404 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001405 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001406 return ret;
1407}
1408
Michel Thierry762d9932015-07-30 11:05:29 +01001409static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1410 struct i915_pml4 *pml4,
1411 uint64_t start,
1412 uint64_t length)
1413{
1414 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001415 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001416 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001417 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001418 int ret = 0;
1419
1420 /* Do the pml4 allocations first, so we don't need to track the newly
1421 * allocated tables below the pdp */
1422 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1423
1424 /* The pagedirectory and pagetable allocations are done in the shared 3
1425 * and 4 level code. Just allocate the pdps.
1426 */
1427 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1428 new_pdps);
1429 if (ret)
1430 return ret;
1431
1432 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1433 "The allocation has spanned more than 512GB. "
1434 "It is highly likely this is incorrect.");
1435
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001436 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001437 WARN_ON(!pdp);
1438
1439 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1440 if (ret)
1441 goto err_out;
1442
1443 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1444 }
1445
1446 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1447 GEN8_PML4ES_PER_PML4);
1448
1449 return 0;
1450
1451err_out:
1452 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1453 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1454
1455 return ret;
1456}
1457
1458static int gen8_alloc_va_range(struct i915_address_space *vm,
1459 uint64_t start, uint64_t length)
1460{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001461 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001462
1463 if (USES_FULL_48BIT_PPGTT(vm->dev))
1464 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1465 else
1466 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1467}
1468
Michel Thierryea91e402015-07-29 17:23:57 +01001469static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1470 uint64_t start, uint64_t length,
1471 gen8_pte_t scratch_pte,
1472 struct seq_file *m)
1473{
1474 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001475 uint32_t pdpe;
1476
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001477 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001478 struct i915_page_table *pt;
1479 uint64_t pd_len = length;
1480 uint64_t pd_start = start;
1481 uint32_t pde;
1482
1483 if (!test_bit(pdpe, pdp->used_pdpes))
1484 continue;
1485
1486 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001487 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001488 uint32_t pte;
1489 gen8_pte_t *pt_vaddr;
1490
1491 if (!test_bit(pde, pd->used_pdes))
1492 continue;
1493
1494 pt_vaddr = kmap_px(pt);
1495 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1496 uint64_t va =
1497 (pdpe << GEN8_PDPE_SHIFT) |
1498 (pde << GEN8_PDE_SHIFT) |
1499 (pte << GEN8_PTE_SHIFT);
1500 int i;
1501 bool found = false;
1502
1503 for (i = 0; i < 4; i++)
1504 if (pt_vaddr[pte + i] != scratch_pte)
1505 found = true;
1506 if (!found)
1507 continue;
1508
1509 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1510 for (i = 0; i < 4; i++) {
1511 if (pt_vaddr[pte + i] != scratch_pte)
1512 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1513 else
1514 seq_puts(m, " SCRATCH ");
1515 }
1516 seq_puts(m, "\n");
1517 }
1518 /* don't use kunmap_px, it could trigger
1519 * an unnecessary flush.
1520 */
1521 kunmap_atomic(pt_vaddr);
1522 }
1523 }
1524}
1525
1526static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1527{
1528 struct i915_address_space *vm = &ppgtt->base;
1529 uint64_t start = ppgtt->base.start;
1530 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001531 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001532 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001533
1534 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1535 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1536 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001537 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001538 struct i915_pml4 *pml4 = &ppgtt->pml4;
1539 struct i915_page_directory_pointer *pdp;
1540
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001541 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001542 if (!test_bit(pml4e, pml4->used_pml4es))
1543 continue;
1544
1545 seq_printf(m, " PML4E #%llu\n", pml4e);
1546 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1547 }
1548 }
1549}
1550
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001551static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1552{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001553 unsigned long *new_page_dirs, *new_page_tables;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001554 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1555 int ret;
1556
1557 /* We allocate temp bitmap for page tables for no gain
1558 * but as this is for init only, lets keep the things simple
1559 */
1560 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1561 if (ret)
1562 return ret;
1563
1564 /* Allocate for all pdps regardless of how the ppgtt
1565 * was defined.
1566 */
1567 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1568 0, 1ULL << 32,
1569 new_page_dirs);
1570 if (!ret)
1571 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1572
Michał Winiarski3a41a052015-09-03 19:22:18 +02001573 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001574
1575 return ret;
1576}
1577
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001578/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001579 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1580 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1581 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1582 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001583 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001584 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001585static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001586{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001587 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001588
Mika Kuoppala8776f022015-06-30 18:16:40 +03001589 ret = gen8_init_scratch(&ppgtt->base);
1590 if (ret)
1591 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001592
Michel Thierryd7b26332015-04-08 12:13:34 +01001593 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001594 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001595 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001596 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001597 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001598 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1599 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001600 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001601
Michel Thierry762d9932015-07-30 11:05:29 +01001602 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1603 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1604 if (ret)
1605 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001606
Michel Thierry69ab76f2015-07-29 17:23:55 +01001607 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1608
Michel Thierry762d9932015-07-30 11:05:29 +01001609 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001610 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001611 } else {
Michel Thierry25f50332015-08-07 17:40:19 +01001612 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001613 if (ret)
1614 goto free_scratch;
1615
1616 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001617 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001618 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1619 0, 0,
1620 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001621
Chris Wilsonc0336662016-05-06 15:40:21 +01001622 if (intel_vgpu_active(to_i915(ppgtt->base.dev))) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001623 ret = gen8_preallocate_top_level_pdps(ppgtt);
1624 if (ret)
1625 goto free_scratch;
1626 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001627 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001628
Chris Wilsonc0336662016-05-06 15:40:21 +01001629 if (intel_vgpu_active(to_i915(ppgtt->base.dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001630 gen8_ppgtt_notify_vgt(ppgtt, true);
1631
Michel Thierryd7b26332015-04-08 12:13:34 +01001632 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001633
1634free_scratch:
1635 gen8_free_scratch(&ppgtt->base);
1636 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001637}
1638
Ben Widawsky87d60b62013-12-06 14:11:29 -08001639static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1640{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001641 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001642 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001643 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001644 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001645 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001646 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001647
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001648 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001649 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001650
Dave Gordon731f74c2016-06-24 19:37:46 +01001651 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001652 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001653 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001654 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001655 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001656 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1657
1658 if (pd_entry != expected)
1659 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1660 pde,
1661 pd_entry,
1662 expected);
1663 seq_printf(m, "\tPDE: %x\n", pd_entry);
1664
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001665 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1666
Michel Thierry07749ef2015-03-16 16:00:54 +00001667 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001668 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001669 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001670 (pte * PAGE_SIZE);
1671 int i;
1672 bool found = false;
1673 for (i = 0; i < 4; i++)
1674 if (pt_vaddr[pte + i] != scratch_pte)
1675 found = true;
1676 if (!found)
1677 continue;
1678
1679 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1680 for (i = 0; i < 4; i++) {
1681 if (pt_vaddr[pte + i] != scratch_pte)
1682 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1683 else
1684 seq_puts(m, " SCRATCH ");
1685 }
1686 seq_puts(m, "\n");
1687 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001688 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001689 }
1690}
1691
Ben Widawsky678d96f2015-03-16 16:00:56 +00001692/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001693static void gen6_write_pde(struct i915_page_directory *pd,
1694 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001695{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001696 /* Caller needs to make sure the write completes if necessary */
1697 struct i915_hw_ppgtt *ppgtt =
1698 container_of(pd, struct i915_hw_ppgtt, pd);
1699 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001700
Mika Kuoppala567047b2015-06-25 18:35:12 +03001701 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001702 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001703
Ben Widawsky678d96f2015-03-16 16:00:56 +00001704 writel(pd_entry, ppgtt->pd_addr + pde);
1705}
Ben Widawsky61973492013-04-08 18:43:54 -07001706
Ben Widawsky678d96f2015-03-16 16:00:56 +00001707/* Write all the page tables found in the ppgtt structure to incrementing page
1708 * directories. */
1709static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001710 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001711 uint32_t start, uint32_t length)
1712{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001713 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001714 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001715 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001716
Dave Gordon731f74c2016-06-24 19:37:46 +01001717 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001718 gen6_write_pde(pd, pde, pt);
1719
1720 /* Make sure write is complete before other code can use this page
1721 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001722 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001723}
1724
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001725static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001726{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001727 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001728
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001729 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001730}
Ben Widawsky61973492013-04-08 18:43:54 -07001731
Ben Widawsky90252e52013-12-06 14:11:12 -08001732static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001733 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001734{
Chris Wilson7e37f882016-08-02 22:50:21 +01001735 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001736 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001737 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001738
Ben Widawsky90252e52013-12-06 14:11:12 -08001739 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001740 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001741 if (ret)
1742 return ret;
1743
John Harrison5fb9de12015-05-29 17:44:07 +01001744 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001745 if (ret)
1746 return ret;
1747
Chris Wilsonb5321f32016-08-02 22:50:18 +01001748 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1749 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1750 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1751 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1752 intel_ring_emit(ring, get_pd_offset(ppgtt));
1753 intel_ring_emit(ring, MI_NOOP);
1754 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001755
1756 return 0;
1757}
1758
Ben Widawsky48a10382013-12-06 14:11:11 -08001759static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001760 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001761{
Chris Wilson7e37f882016-08-02 22:50:21 +01001762 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001763 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001764 int ret;
1765
Ben Widawsky48a10382013-12-06 14:11:11 -08001766 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001767 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001768 if (ret)
1769 return ret;
1770
John Harrison5fb9de12015-05-29 17:44:07 +01001771 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001772 if (ret)
1773 return ret;
1774
Chris Wilsonb5321f32016-08-02 22:50:18 +01001775 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1776 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1777 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1778 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1779 intel_ring_emit(ring, get_pd_offset(ppgtt));
1780 intel_ring_emit(ring, MI_NOOP);
1781 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001782
Ben Widawsky90252e52013-12-06 14:11:12 -08001783 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001784 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001785 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001786 if (ret)
1787 return ret;
1788 }
1789
Ben Widawsky48a10382013-12-06 14:11:11 -08001790 return 0;
1791}
1792
Ben Widawskyeeb94882013-12-06 14:11:10 -08001793static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001794 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001795{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001796 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001797 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001798
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001799 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1800 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001801 return 0;
1802}
1803
Daniel Vetter82460d92014-08-06 20:19:53 +02001804static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001805{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001806 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001807 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301808 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001809
Akash Goel3b3f1652016-10-13 22:44:48 +05301810 for_each_engine(engine, dev_priv, id) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001811 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001812 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001813 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001814 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001815}
1816
Daniel Vetter82460d92014-08-06 20:19:53 +02001817static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001818{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001819 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001820 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001821 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301822 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001823
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001824 ecobits = I915_READ(GAC_ECO_BITS);
1825 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1826
1827 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001828 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001829 ecochk |= ECOCHK_PPGTT_WB_HSW;
1830 } else {
1831 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1832 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1833 }
1834 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001835
Akash Goel3b3f1652016-10-13 22:44:48 +05301836 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001837 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001838 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001839 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001840 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001841}
1842
Daniel Vetter82460d92014-08-06 20:19:53 +02001843static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001844{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001845 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001846 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001847
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001848 ecobits = I915_READ(GAC_ECO_BITS);
1849 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1850 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001851
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001852 gab_ctl = I915_READ(GAB_CTL);
1853 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001854
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001855 ecochk = I915_READ(GAM_ECOCHK);
1856 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001857
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001858 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001859}
1860
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001861/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001862static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001863 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001864 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001865{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001866 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001867 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001868 unsigned first_entry = start >> PAGE_SHIFT;
1869 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001870 unsigned act_pt = first_entry / GEN6_PTES;
1871 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001872 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001873
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001874 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001875 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001876
Daniel Vetter7bddb012012-02-09 17:15:47 +01001877 while (num_entries) {
1878 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001879 if (last_pte > GEN6_PTES)
1880 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001881
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001882 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001883
1884 for (i = first_pte; i < last_pte; i++)
1885 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001886
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001887 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001888
Daniel Vetter7bddb012012-02-09 17:15:47 +01001889 num_entries -= last_pte - first_pte;
1890 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001891 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001892 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001893}
1894
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001895static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001896 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001897 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301898 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001899{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001900 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001901 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001902 unsigned act_pt = first_entry / GEN6_PTES;
1903 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001904 gen6_pte_t *pt_vaddr = NULL;
1905 struct sgt_iter sgt_iter;
1906 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001907
Dave Gordon85d12252016-05-20 11:54:06 +01001908 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001909 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001910 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001911
Chris Wilsoncc797142013-12-31 15:50:30 +00001912 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001913 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301914
Michel Thierry07749ef2015-03-16 16:00:54 +00001915 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001916 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001917 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001918 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001919 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001920 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001921 }
Dave Gordon85d12252016-05-20 11:54:06 +01001922
Chris Wilsoncc797142013-12-31 15:50:30 +00001923 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001924 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001925}
1926
Ben Widawsky678d96f2015-03-16 16:00:56 +00001927static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001928 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001929{
Michel Thierry4933d512015-03-24 15:46:22 +00001930 DECLARE_BITMAP(new_page_tables, I915_PDES);
1931 struct drm_device *dev = vm->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001932 struct drm_i915_private *dev_priv = to_i915(dev);
1933 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001934 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001935 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001936 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001937 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001938 int ret;
1939
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001940 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1941 return -ENODEV;
1942
1943 start = start_save = start_in;
1944 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001945
1946 bitmap_zero(new_page_tables, I915_PDES);
1947
1948 /* The allocation is done in two stages so that we can bail out with
1949 * minimal amount of pain. The first stage finds new page tables that
1950 * need allocation. The second stage marks use ptes within the page
1951 * tables.
1952 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001953 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001954 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001955 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1956 continue;
1957 }
1958
1959 /* We've already allocated a page table */
1960 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1961
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001962 pt = alloc_pt(dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001963 if (IS_ERR(pt)) {
1964 ret = PTR_ERR(pt);
1965 goto unwind_out;
1966 }
1967
1968 gen6_initialize_pt(vm, pt);
1969
1970 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001971 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001972 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001973 }
1974
1975 start = start_save;
1976 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001977
Dave Gordon731f74c2016-06-24 19:37:46 +01001978 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001979 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1980
1981 bitmap_zero(tmp_bitmap, GEN6_PTES);
1982 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1983 gen6_pte_count(start, length));
1984
Mika Kuoppala966082c2015-06-25 18:35:19 +03001985 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001986 gen6_write_pde(&ppgtt->pd, pde, pt);
1987
Michel Thierry72744cb2015-03-24 15:46:23 +00001988 trace_i915_page_table_entry_map(vm, pde, pt,
1989 gen6_pte_index(start),
1990 gen6_pte_count(start, length),
1991 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001992 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001993 GEN6_PTES);
1994 }
1995
Michel Thierry4933d512015-03-24 15:46:22 +00001996 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1997
1998 /* Make sure write is complete before other code can use this page
1999 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002000 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00002001
Ben Widawsky563222a2015-03-19 12:53:28 +00002002 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002003 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002004
2005unwind_out:
2006 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002007 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002008
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002009 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03002010 free_pt(vm->dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002011 }
2012
2013 mark_tlbs_dirty(ppgtt);
2014 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002015}
2016
Mika Kuoppala8776f022015-06-30 18:16:40 +03002017static int gen6_init_scratch(struct i915_address_space *vm)
2018{
2019 struct drm_device *dev = vm->dev;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002020 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002021
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002022 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002023 if (ret)
2024 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002025
2026 vm->scratch_pt = alloc_pt(dev);
2027 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002028 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002029 return PTR_ERR(vm->scratch_pt);
2030 }
2031
2032 gen6_initialize_pt(vm, vm->scratch_pt);
2033
2034 return 0;
2035}
2036
2037static void gen6_free_scratch(struct i915_address_space *vm)
2038{
2039 struct drm_device *dev = vm->dev;
2040
2041 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002042 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002043}
2044
Daniel Vetter061dd492015-04-14 17:35:13 +02002045static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002046{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002047 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002048 struct i915_page_directory *pd = &ppgtt->pd;
2049 struct drm_device *dev = vm->dev;
Michel Thierry09942c62015-04-08 12:13:30 +01002050 struct i915_page_table *pt;
2051 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002052
Daniel Vetter061dd492015-04-14 17:35:13 +02002053 drm_mm_remove_node(&ppgtt->node);
2054
Dave Gordon731f74c2016-06-24 19:37:46 +01002055 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002056 if (pt != vm->scratch_pt)
Dave Gordon731f74c2016-06-24 19:37:46 +01002057 free_pt(dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002058
Mika Kuoppala8776f022015-06-30 18:16:40 +03002059 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002060}
2061
Ben Widawskyb1465202014-02-19 22:05:49 -08002062static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002063{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002064 struct i915_address_space *vm = &ppgtt->base;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002065 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002066 struct drm_i915_private *dev_priv = to_i915(dev);
2067 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002068 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002069 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002070
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002071 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2072 * allocator works in address space sizes, so it's multiplied by page
2073 * size. We allocate at the top of the GTT to avoid fragmentation.
2074 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002075 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002076
Mika Kuoppala8776f022015-06-30 18:16:40 +03002077 ret = gen6_init_scratch(vm);
2078 if (ret)
2079 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002080
Ben Widawskye3cc1992013-12-06 14:11:08 -08002081alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002082 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002083 &ppgtt->node, GEN6_PD_SIZE,
2084 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002085 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002086 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002087 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002088 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002089 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002090 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002091 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002092 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002093 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002094 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002095
2096 retried = true;
2097 goto alloc;
2098 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002099
Ben Widawskyc8c26622015-01-22 17:01:25 +00002100 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002101 goto err_out;
2102
Ben Widawskyc8c26622015-01-22 17:01:25 +00002103
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002104 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002105 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002106
Ben Widawskyc8c26622015-01-22 17:01:25 +00002107 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002108
2109err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002110 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002111 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002112}
2113
Ben Widawskyb1465202014-02-19 22:05:49 -08002114static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2115{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002116 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002117}
2118
Michel Thierry4933d512015-03-24 15:46:22 +00002119static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2120 uint64_t start, uint64_t length)
2121{
Michel Thierryec565b32015-04-08 12:13:23 +01002122 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002123 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002124
Dave Gordon731f74c2016-06-24 19:37:46 +01002125 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002126 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002127}
2128
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002129static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002130{
2131 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002132 struct drm_i915_private *dev_priv = to_i915(dev);
2133 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002134 int ret;
2135
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002136 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002137 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002138 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002139 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002140 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002141 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002142 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002143 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002144 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002145
2146 ret = gen6_ppgtt_alloc(ppgtt);
2147 if (ret)
2148 return ret;
2149
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002150 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002151 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2152 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002153 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2154 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002155 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002156 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002157 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002158 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002159
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002160 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002161 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002162
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002163 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002164 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002165
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002166 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002167
Ben Widawsky678d96f2015-03-16 16:00:56 +00002168 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2169
Thierry Reding440fd522015-01-23 09:05:06 +01002170 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002171 ppgtt->node.size >> 20,
2172 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002173
Daniel Vetterfa76da32014-08-06 20:19:54 +02002174 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002175 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002176
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002177 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002178}
2179
Chris Wilson2bfa9962016-08-04 07:52:25 +01002180static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2181 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002182{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002183 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002184
Chris Wilson2bfa9962016-08-04 07:52:25 +01002185 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002186 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002187 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002188 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002189}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002190
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002191static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002192 struct drm_i915_private *dev_priv,
2193 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002194{
Chris Wilson80b204b2016-10-28 13:58:58 +01002195 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002196 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002197 INIT_LIST_HEAD(&vm->active_list);
2198 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002199 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002200 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2201}
2202
Tim Gored5165eb2016-02-04 11:49:34 +00002203static void gtt_write_workarounds(struct drm_device *dev)
2204{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002205 struct drm_i915_private *dev_priv = to_i915(dev);
Tim Gored5165eb2016-02-04 11:49:34 +00002206
2207 /* This function is for gtt related workarounds. This function is
2208 * called on driver load and after a GPU reset, so you can place
2209 * workarounds here even if they get overwritten by GPU reset.
2210 */
2211 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002212 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002213 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002214 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002215 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002216 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002217 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002218 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002219 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2220}
2221
Chris Wilson2bfa9962016-08-04 07:52:25 +01002222static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2223 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002224 struct drm_i915_file_private *file_priv,
2225 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002226{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002227 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002228
Chris Wilson2bfa9962016-08-04 07:52:25 +01002229 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002230 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002231 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002232 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002233 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002234 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002235
2236 return ret;
2237}
2238
Daniel Vetter82460d92014-08-06 20:19:53 +02002239int i915_ppgtt_init_hw(struct drm_device *dev)
2240{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002241 struct drm_i915_private *dev_priv = to_i915(dev);
2242
Tim Gored5165eb2016-02-04 11:49:34 +00002243 gtt_write_workarounds(dev);
2244
Thomas Daniel671b50132014-08-20 16:24:50 +01002245 /* In the case of execlists, PPGTT is enabled by the context descriptor
2246 * and the PDPs are contained within the context itself. We don't
2247 * need to do anything here. */
2248 if (i915.enable_execlists)
2249 return 0;
2250
Daniel Vetter82460d92014-08-06 20:19:53 +02002251 if (!USES_PPGTT(dev))
2252 return 0;
2253
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002254 if (IS_GEN6(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002255 gen6_ppgtt_enable(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002256 else if (IS_GEN7(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002257 gen7_ppgtt_enable(dev);
2258 else if (INTEL_INFO(dev)->gen >= 8)
2259 gen8_ppgtt_enable(dev);
2260 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01002261 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02002262
John Harrison4ad2fd82015-06-18 13:11:20 +01002263 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002264}
John Harrison4ad2fd82015-06-18 13:11:20 +01002265
Daniel Vetter4d884702014-08-06 15:04:47 +02002266struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002267i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002268 struct drm_i915_file_private *fpriv,
2269 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002270{
2271 struct i915_hw_ppgtt *ppgtt;
2272 int ret;
2273
2274 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2275 if (!ppgtt)
2276 return ERR_PTR(-ENOMEM);
2277
Chris Wilson80b204b2016-10-28 13:58:58 +01002278 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002279 if (ret) {
2280 kfree(ppgtt);
2281 return ERR_PTR(ret);
2282 }
2283
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002284 trace_i915_ppgtt_create(&ppgtt->base);
2285
Daniel Vetter4d884702014-08-06 15:04:47 +02002286 return ppgtt;
2287}
2288
Daniel Vetteree960be2014-08-06 15:04:45 +02002289void i915_ppgtt_release(struct kref *kref)
2290{
2291 struct i915_hw_ppgtt *ppgtt =
2292 container_of(kref, struct i915_hw_ppgtt, ref);
2293
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002294 trace_i915_ppgtt_release(&ppgtt->base);
2295
Chris Wilson50e046b2016-08-04 07:52:46 +01002296 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002297 WARN_ON(!list_empty(&ppgtt->base.active_list));
2298 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002299 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002300
Chris Wilson80b204b2016-10-28 13:58:58 +01002301 i915_gem_timeline_fini(&ppgtt->base.timeline);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002302 list_del(&ppgtt->base.global_link);
2303 drm_mm_takedown(&ppgtt->base.mm);
2304
Daniel Vetteree960be2014-08-06 15:04:45 +02002305 ppgtt->base.cleanup(&ppgtt->base);
2306 kfree(ppgtt);
2307}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002308
Ben Widawskya81cc002013-01-18 12:30:31 -08002309/* Certain Gen5 chipsets require require idling the GPU before
2310 * unmapping anything from the GTT when VT-d is enabled.
2311 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002312static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002313{
2314#ifdef CONFIG_INTEL_IOMMU
2315 /* Query intel_iommu to see if we need the workaround. Presumably that
2316 * was loaded first.
2317 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002318 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002319 return true;
2320#endif
2321 return false;
2322}
2323
Chris Wilsondc979972016-05-10 14:10:04 +01002324void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002325{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002326 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302327 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002328
Chris Wilsondc979972016-05-10 14:10:04 +01002329 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002330 return;
2331
Akash Goel3b3f1652016-10-13 22:44:48 +05302332 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002333 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002334 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002335 if (fault_reg & RING_FAULT_VALID) {
2336 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002337 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002338 "\tAddress space: %s\n"
2339 "\tSource ID: %d\n"
2340 "\tType: %d\n",
2341 fault_reg & PAGE_MASK,
2342 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2343 RING_FAULT_SRCID(fault_reg),
2344 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002345 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002346 fault_reg & ~RING_FAULT_VALID);
2347 }
2348 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302349
2350 /* Engine specific init may not have been done till this point. */
2351 if (dev_priv->engine[RCS])
2352 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002353}
2354
Chris Wilson91e56492014-09-25 10:13:12 +01002355static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2356{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002357 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002358 intel_gtt_chipset_flush();
2359 } else {
2360 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2361 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2362 }
2363}
2364
Ben Widawsky828c7902013-10-16 09:21:30 -07002365void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2366{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002367 struct drm_i915_private *dev_priv = to_i915(dev);
2368 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002369
2370 /* Don't bother messing with faults pre GEN6 as we have little
2371 * documentation supporting that it's a good idea.
2372 */
2373 if (INTEL_INFO(dev)->gen < 6)
2374 return;
2375
Chris Wilsondc979972016-05-10 14:10:04 +01002376 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002377
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002378 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002379
2380 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002381}
2382
Chris Wilson03ac84f2016-10-28 13:58:36 +01002383int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2384 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002385{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002386 if (dma_map_sg(&obj->base.dev->pdev->dev,
2387 pages->sgl, pages->nents,
2388 PCI_DMA_BIDIRECTIONAL))
2389 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01002390
Chris Wilson03ac84f2016-10-28 13:58:36 +01002391 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002392}
2393
Daniel Vetter2c642b02015-04-14 17:35:26 +02002394static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002395{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002396 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002397}
2398
Chris Wilsond6473f52016-06-10 14:22:59 +05302399static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2400 dma_addr_t addr,
2401 uint64_t offset,
2402 enum i915_cache_level level,
2403 u32 unused)
2404{
2405 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2406 gen8_pte_t __iomem *pte =
2407 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2408 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302409
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002410 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302411
2412 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2413 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302414}
2415
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002416static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2417 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002418 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302419 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002420{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002421 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002422 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002423 struct sgt_iter sgt_iter;
2424 gen8_pte_t __iomem *gtt_entries;
2425 gen8_pte_t gtt_entry;
2426 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002427 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002428
Dave Gordon85d12252016-05-20 11:54:06 +01002429 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2430
2431 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002432 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002433 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002434 }
2435
2436 /*
2437 * XXX: This serves as a posting read to make sure that the PTE has
2438 * actually been updated. There is some concern that even though
2439 * registers and PTEs are within the same BAR that they are potentially
2440 * of NUMA access patterns. Therefore, even with the way we assume
2441 * hardware should work, we must keep this posting read for paranoia.
2442 */
2443 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002444 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002445
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002446 /* This next bit makes the above posting read even more important. We
2447 * want to flush the TLBs only after we're certain all the PTE updates
2448 * have finished.
2449 */
2450 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2451 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002452}
2453
Chris Wilsonc1403302015-11-18 15:19:39 +00002454struct insert_entries {
2455 struct i915_address_space *vm;
2456 struct sg_table *st;
2457 uint64_t start;
2458 enum i915_cache_level level;
2459 u32 flags;
2460};
2461
2462static int gen8_ggtt_insert_entries__cb(void *_arg)
2463{
2464 struct insert_entries *arg = _arg;
2465 gen8_ggtt_insert_entries(arg->vm, arg->st,
2466 arg->start, arg->level, arg->flags);
2467 return 0;
2468}
2469
2470static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2471 struct sg_table *st,
2472 uint64_t start,
2473 enum i915_cache_level level,
2474 u32 flags)
2475{
2476 struct insert_entries arg = { vm, st, start, level, flags };
2477 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2478}
2479
Chris Wilsond6473f52016-06-10 14:22:59 +05302480static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2481 dma_addr_t addr,
2482 uint64_t offset,
2483 enum i915_cache_level level,
2484 u32 flags)
2485{
2486 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2487 gen6_pte_t __iomem *pte =
2488 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2489 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302490
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002491 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302492
2493 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2494 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302495}
2496
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002497/*
2498 * Binds an object into the global gtt with the specified cache level. The object
2499 * will be accessible to the GPU via commands whose operands reference offsets
2500 * within the global GTT as well as accessible by the GPU through the GMADR
2501 * mapped BAR (dev_priv->mm.gtt->gtt).
2502 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002503static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002504 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002505 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302506 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002507{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002508 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002509 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002510 struct sgt_iter sgt_iter;
2511 gen6_pte_t __iomem *gtt_entries;
2512 gen6_pte_t gtt_entry;
2513 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002514 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002515
Dave Gordon85d12252016-05-20 11:54:06 +01002516 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2517
2518 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002519 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002520 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002521 }
2522
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002523 /* XXX: This serves as a posting read to make sure that the PTE has
2524 * actually been updated. There is some concern that even though
2525 * registers and PTEs are within the same BAR that they are potentially
2526 * of NUMA access patterns. Therefore, even with the way we assume
2527 * hardware should work, we must keep this posting read for paranoia.
2528 */
Dave Gordon85d12252016-05-20 11:54:06 +01002529 if (i != 0)
2530 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002531
2532 /* This next bit makes the above posting read even more important. We
2533 * want to flush the TLBs only after we're certain all the PTE updates
2534 * have finished.
2535 */
2536 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2537 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002538}
2539
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002540static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002541 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002542{
2543}
2544
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002545static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002546 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002547{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002548 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002549 unsigned first_entry = start >> PAGE_SHIFT;
2550 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002551 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002552 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2553 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002554 int i;
2555
2556 if (WARN(num_entries > max_entries,
2557 "First entry = %d; Num entries = %d (max=%d)\n",
2558 first_entry, num_entries, max_entries))
2559 num_entries = max_entries;
2560
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002561 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002562 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002563 for (i = 0; i < num_entries; i++)
2564 gen8_set_pte(&gtt_base[i], scratch_pte);
2565 readl(gtt_base);
2566}
2567
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002568static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002569 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002570 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002571{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002572 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002573 unsigned first_entry = start >> PAGE_SHIFT;
2574 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002575 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002576 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2577 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002578 int i;
2579
2580 if (WARN(num_entries > max_entries,
2581 "First entry = %d; Num entries = %d (max=%d)\n",
2582 first_entry, num_entries, max_entries))
2583 num_entries = max_entries;
2584
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002585 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002586 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002587
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002588 for (i = 0; i < num_entries; i++)
2589 iowrite32(scratch_pte, &gtt_base[i]);
2590 readl(gtt_base);
2591}
2592
Chris Wilsond6473f52016-06-10 14:22:59 +05302593static void i915_ggtt_insert_page(struct i915_address_space *vm,
2594 dma_addr_t addr,
2595 uint64_t offset,
2596 enum i915_cache_level cache_level,
2597 u32 unused)
2598{
Chris Wilsond6473f52016-06-10 14:22:59 +05302599 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2600 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302601
2602 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302603}
2604
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002605static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2606 struct sg_table *pages,
2607 uint64_t start,
2608 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002609{
2610 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2611 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2612
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002613 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002614
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002615}
2616
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002617static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002618 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002619 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002620{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002621 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002622}
2623
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002624static int ggtt_bind_vma(struct i915_vma *vma,
2625 enum i915_cache_level cache_level,
2626 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002627{
Chris Wilson9c870d02016-10-24 13:42:15 +01002628 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Daniel Vetter0a878712015-10-15 14:23:01 +02002629 struct drm_i915_gem_object *obj = vma->obj;
2630 u32 pte_flags = 0;
2631 int ret;
2632
2633 ret = i915_get_ggtt_vma_pages(vma);
2634 if (ret)
2635 return ret;
2636
2637 /* Currently applicable only to VLV */
2638 if (obj->gt_ro)
2639 pte_flags |= PTE_READ_ONLY;
2640
Chris Wilson9c870d02016-10-24 13:42:15 +01002641 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002642 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002643 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002644 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002645
2646 /*
2647 * Without aliasing PPGTT there's no difference between
2648 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2649 * upgrade to both bound if we bind either to avoid double-binding.
2650 */
Chris Wilson3272db52016-08-04 16:32:32 +01002651 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002652
2653 return 0;
2654}
2655
2656static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2657 enum i915_cache_level cache_level,
2658 u32 flags)
2659{
Chris Wilson9c870d02016-10-24 13:42:15 +01002660 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Chris Wilson321d1782015-11-20 10:27:18 +00002661 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002662 int ret;
2663
2664 ret = i915_get_ggtt_vma_pages(vma);
2665 if (ret)
2666 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002667
Akash Goel24f3a8c2014-06-17 10:59:42 +05302668 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002669 pte_flags = 0;
2670 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002671 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302672
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002673
Chris Wilson3272db52016-08-04 16:32:32 +01002674 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002675 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002676 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002677 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002678 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002679 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002680 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002681
Chris Wilson3272db52016-08-04 16:32:32 +01002682 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002683 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002684 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002685 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002686 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002687 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002688
2689 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002690}
2691
2692static void ggtt_unbind_vma(struct i915_vma *vma)
2693{
Chris Wilson9c870d02016-10-24 13:42:15 +01002694 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2695 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002696 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002697
Chris Wilson9c870d02016-10-24 13:42:15 +01002698 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2699 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002700 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002701 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002702 intel_runtime_pm_put(i915);
2703 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002704
Chris Wilson3272db52016-08-04 16:32:32 +01002705 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002706 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002707 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002708}
2709
Chris Wilson03ac84f2016-10-28 13:58:36 +01002710void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2711 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002712{
David Weinehall52a05c32016-08-22 13:32:44 +03002713 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2714 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002715 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002716
Chris Wilson307dc252016-08-05 10:14:12 +01002717 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002718 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002719 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2720 /* Wait a bit, in hopes it avoids the hang */
2721 udelay(10);
2722 }
2723 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002724
Chris Wilson03ac84f2016-10-28 13:58:36 +01002725 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002726}
Daniel Vetter644ec022012-03-26 09:45:40 +02002727
Chris Wilson42d6ab42012-07-26 11:49:32 +01002728static void i915_gtt_color_adjust(struct drm_mm_node *node,
2729 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002730 u64 *start,
2731 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002732{
2733 if (node->color != color)
2734 *start += 4096;
2735
Chris Wilson2a1d7752016-07-26 12:01:51 +01002736 node = list_first_entry_or_null(&node->node_list,
2737 struct drm_mm_node,
2738 node_list);
2739 if (node && node->allocated && node->color != color)
2740 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002741}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002742
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002743int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002744{
Ben Widawskye78891c2013-01-25 16:41:04 -08002745 /* Let GEM Manage all of the aperture.
2746 *
2747 * However, leave one page at the end still bound to the scratch page.
2748 * There are a number of places where the hardware apparently prefetches
2749 * past the end of the object, and we've seen multiple hangs with the
2750 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2751 * aperture. One page should be enough to keep any prefetching inside
2752 * of the aperture.
2753 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002754 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002755 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002756 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002757 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002758 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002759
Zhi Wangb02d22a2016-06-16 08:06:59 -04002760 ret = intel_vgt_balloon(dev_priv);
2761 if (ret)
2762 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002763
Chris Wilson95374d72016-10-12 10:05:20 +01002764 /* Reserve a mappable slot for our lockless error capture */
2765 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2766 &ggtt->error_capture,
2767 4096, 0, -1,
2768 0, ggtt->mappable_end,
2769 0, 0);
2770 if (ret)
2771 return ret;
2772
Chris Wilsoned2f3452012-11-15 11:32:19 +00002773 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002774 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002775 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2776 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002777 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002778 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002779 }
2780
2781 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002782 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002783 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002784
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002785 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002786 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002787 if (!ppgtt) {
2788 ret = -ENOMEM;
2789 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002790 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002791
Chris Wilson95374d72016-10-12 10:05:20 +01002792 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2793 if (ret)
2794 goto err_ppgtt;
2795
2796 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002797 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2798 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002799 if (ret)
2800 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002801 }
2802
2803 ppgtt->base.clear_range(&ppgtt->base,
2804 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002805 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002806
Daniel Vetterfa76da32014-08-06 20:19:54 +02002807 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002808 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2809 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002810 }
2811
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002812 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002813
2814err_ppgtt_cleanup:
2815 ppgtt->base.cleanup(&ppgtt->base);
2816err_ppgtt:
2817 kfree(ppgtt);
2818err:
2819 drm_mm_remove_node(&ggtt->error_capture);
2820 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002821}
2822
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002823/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002824 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002825 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002826 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002827void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002828{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002829 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002830
Daniel Vetter70e32542014-08-06 15:04:57 +02002831 if (dev_priv->mm.aliasing_ppgtt) {
2832 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002833 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002834 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002835 }
2836
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002837 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002838
Chris Wilson95374d72016-10-12 10:05:20 +01002839 if (drm_mm_node_allocated(&ggtt->error_capture))
2840 drm_mm_remove_node(&ggtt->error_capture);
2841
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002842 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002843 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002844
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002845 drm_mm_takedown(&ggtt->base.mm);
2846 list_del(&ggtt->base.global_link);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002847 }
2848
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002849 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002850
2851 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002852 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002853}
Daniel Vetter70e32542014-08-06 15:04:57 +02002854
Daniel Vetter2c642b02015-04-14 17:35:26 +02002855static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002856{
2857 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2858 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2859 return snb_gmch_ctl << 20;
2860}
2861
Daniel Vetter2c642b02015-04-14 17:35:26 +02002862static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002863{
2864 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2865 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2866 if (bdw_gmch_ctl)
2867 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002868
2869#ifdef CONFIG_X86_32
2870 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2871 if (bdw_gmch_ctl > 4)
2872 bdw_gmch_ctl = 4;
2873#endif
2874
Ben Widawsky9459d252013-11-03 16:53:55 -08002875 return bdw_gmch_ctl << 20;
2876}
2877
Daniel Vetter2c642b02015-04-14 17:35:26 +02002878static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002879{
2880 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2881 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2882
2883 if (gmch_ctrl)
2884 return 1 << (20 + gmch_ctrl);
2885
2886 return 0;
2887}
2888
Daniel Vetter2c642b02015-04-14 17:35:26 +02002889static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002890{
2891 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2892 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2893 return snb_gmch_ctl << 25; /* 32 MB units */
2894}
2895
Daniel Vetter2c642b02015-04-14 17:35:26 +02002896static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002897{
2898 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2899 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2900 return bdw_gmch_ctl << 25; /* 32 MB units */
2901}
2902
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002903static size_t chv_get_stolen_size(u16 gmch_ctrl)
2904{
2905 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2906 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2907
2908 /*
2909 * 0x0 to 0x10: 32MB increments starting at 0MB
2910 * 0x11 to 0x16: 4MB increments starting at 8MB
2911 * 0x17 to 0x1d: 4MB increments start at 36MB
2912 */
2913 if (gmch_ctrl < 0x11)
2914 return gmch_ctrl << 25;
2915 else if (gmch_ctrl < 0x17)
2916 return (gmch_ctrl - 0x11 + 2) << 22;
2917 else
2918 return (gmch_ctrl - 0x17 + 9) << 22;
2919}
2920
Damien Lespiau66375012014-01-09 18:02:46 +00002921static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2922{
2923 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2924 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2925
2926 if (gen9_gmch_ctl < 0xf0)
2927 return gen9_gmch_ctl << 25; /* 32 MB units */
2928 else
2929 /* 4MB increments starting at 0xf0 for 4MB */
2930 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2931}
2932
Chris Wilson34c998b2016-08-04 07:52:24 +01002933static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002934{
Chris Wilson34c998b2016-08-04 07:52:24 +01002935 struct pci_dev *pdev = ggtt->base.dev->pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002936 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002937 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002938
2939 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002940 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002941
Imre Deak2a073f892015-03-27 13:07:33 +02002942 /*
2943 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2944 * dropped. For WC mappings in general we have 64 byte burst writes
2945 * when the WC buffer is flushed, so we can't use it, but have to
2946 * resort to an uncached mapping. The WC issue is easily caught by the
2947 * readback check when writing GTT PTE entries.
2948 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002949 if (IS_BROXTON(to_i915(ggtt->base.dev)))
Chris Wilson34c998b2016-08-04 07:52:24 +01002950 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002951 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002952 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002953 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002954 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002955 return -ENOMEM;
2956 }
2957
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002958 ret = setup_scratch_page(ggtt->base.dev,
2959 &ggtt->base.scratch_page,
2960 GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002961 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002962 DRM_ERROR("Scratch setup failed\n");
2963 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002964 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002965 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002966 }
2967
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002968 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002969}
2970
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002971/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2972 * bits. When using advanced contexts each context stores its own PAT, but
2973 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002974static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002975{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002976 uint64_t pat;
2977
2978 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2979 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2980 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2981 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2982 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2983 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2984 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2985 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2986
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002987 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002988 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2989 * so RTL will always use the value corresponding to
2990 * pat_sel = 000".
2991 * So let's disable cache for GGTT to avoid screen corruptions.
2992 * MOCS still can be used though.
2993 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2994 * before this patch, i.e. the same uncached + snooping access
2995 * like on gen6/7 seems to be in effect.
2996 * - So this just fixes blitter/render access. Again it looks
2997 * like it's not just uncached access, but uncached + snooping.
2998 * So we can still hold onto all our assumptions wrt cpu
2999 * clflushing on LLC machines.
3000 */
3001 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3002
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003003 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3004 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003005 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3006 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003007}
3008
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003009static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3010{
3011 uint64_t pat;
3012
3013 /*
3014 * Map WB on BDW to snooped on CHV.
3015 *
3016 * Only the snoop bit has meaning for CHV, the rest is
3017 * ignored.
3018 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003019 * The hardware will never snoop for certain types of accesses:
3020 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3021 * - PPGTT page tables
3022 * - some other special cycles
3023 *
3024 * As with BDW, we also need to consider the following for GT accesses:
3025 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3026 * so RTL will always use the value corresponding to
3027 * pat_sel = 000".
3028 * Which means we must set the snoop bit in PAT entry 0
3029 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003030 */
3031 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3032 GEN8_PPAT(1, 0) |
3033 GEN8_PPAT(2, 0) |
3034 GEN8_PPAT(3, 0) |
3035 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3036 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3037 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3038 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3039
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003040 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3041 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003042}
3043
Chris Wilson34c998b2016-08-04 07:52:24 +01003044static void gen6_gmch_remove(struct i915_address_space *vm)
3045{
3046 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3047
3048 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01003049 cleanup_scratch_page(vm->dev, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003050}
3051
Joonas Lahtinend507d732016-03-18 10:42:58 +02003052static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003053{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003054 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3055 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003056 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003057 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003058
3059 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003060 ggtt->mappable_base = pci_resource_start(pdev, 2);
3061 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003062
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003063 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3064 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003065
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003066 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003067
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003068 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003069 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003070 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003071 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003072 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003073 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003074 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003075 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003076 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003077 }
Ben Widawsky63340132013-11-04 19:32:22 -08003078
Chris Wilson34c998b2016-08-04 07:52:24 +01003079 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003080
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003081 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003082 chv_setup_private_ppat(dev_priv);
3083 else
3084 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003085
Chris Wilson34c998b2016-08-04 07:52:24 +01003086 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003087 ggtt->base.bind_vma = ggtt_bind_vma;
3088 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303089 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003090 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003091 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003092 ggtt->base.clear_range = gen8_ggtt_clear_range;
3093
3094 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3095 if (IS_CHERRYVIEW(dev_priv))
3096 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3097
Chris Wilson34c998b2016-08-04 07:52:24 +01003098 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003099}
3100
Joonas Lahtinend507d732016-03-18 10:42:58 +02003101static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003102{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003103 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3104 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003105 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003106 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003107
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003108 ggtt->mappable_base = pci_resource_start(pdev, 2);
3109 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003110
Ben Widawskybaa09f52013-01-24 13:49:57 -08003111 /* 64/512MB is the current min/max we actually know of, but this is just
3112 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003113 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003114 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003115 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003116 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003117 }
3118
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003119 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3120 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3121 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003122
Joonas Lahtinend507d732016-03-18 10:42:58 +02003123 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003124
Chris Wilson34c998b2016-08-04 07:52:24 +01003125 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3126 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003127
Joonas Lahtinend507d732016-03-18 10:42:58 +02003128 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303129 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003130 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3131 ggtt->base.bind_vma = ggtt_bind_vma;
3132 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003133 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003134
Chris Wilson34c998b2016-08-04 07:52:24 +01003135 if (HAS_EDRAM(dev_priv))
3136 ggtt->base.pte_encode = iris_pte_encode;
3137 else if (IS_HASWELL(dev_priv))
3138 ggtt->base.pte_encode = hsw_pte_encode;
3139 else if (IS_VALLEYVIEW(dev_priv))
3140 ggtt->base.pte_encode = byt_pte_encode;
3141 else if (INTEL_GEN(dev_priv) >= 7)
3142 ggtt->base.pte_encode = ivb_pte_encode;
3143 else
3144 ggtt->base.pte_encode = snb_pte_encode;
3145
3146 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003147}
3148
Chris Wilson34c998b2016-08-04 07:52:24 +01003149static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003150{
Chris Wilson34c998b2016-08-04 07:52:24 +01003151 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003152}
3153
Joonas Lahtinend507d732016-03-18 10:42:58 +02003154static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003155{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003156 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003157 int ret;
3158
Chris Wilson91c8a322016-07-05 10:40:23 +01003159 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003160 if (!ret) {
3161 DRM_ERROR("failed to set up gmch\n");
3162 return -EIO;
3163 }
3164
Joonas Lahtinend507d732016-03-18 10:42:58 +02003165 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3166 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003167
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003168 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303169 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003170 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3171 ggtt->base.clear_range = i915_ggtt_clear_range;
3172 ggtt->base.bind_vma = ggtt_bind_vma;
3173 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003174 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003175
Joonas Lahtinend507d732016-03-18 10:42:58 +02003176 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003177 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3178
Ben Widawskybaa09f52013-01-24 13:49:57 -08003179 return 0;
3180}
3181
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003182/**
Chris Wilson0088e522016-08-04 07:52:21 +01003183 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003184 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003185 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003186int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003187{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003188 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003189 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003190
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003191 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003192
Chris Wilson34c998b2016-08-04 07:52:24 +01003193 if (INTEL_GEN(dev_priv) <= 5)
3194 ret = i915_gmch_probe(ggtt);
3195 else if (INTEL_GEN(dev_priv) < 8)
3196 ret = gen6_gmch_probe(ggtt);
3197 else
3198 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003199 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003200 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003201
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003202 if ((ggtt->base.total - 1) >> 32) {
3203 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003204 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003205 ggtt->base.total >> 20);
3206 ggtt->base.total = 1ULL << 32;
3207 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3208 }
3209
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003210 if (ggtt->mappable_end > ggtt->base.total) {
3211 DRM_ERROR("mappable aperture extends past end of GGTT,"
3212 " aperture=%llx, total=%llx\n",
3213 ggtt->mappable_end, ggtt->base.total);
3214 ggtt->mappable_end = ggtt->base.total;
3215 }
3216
Ben Widawskybaa09f52013-01-24 13:49:57 -08003217 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003218 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003219 ggtt->base.total >> 20);
3220 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3221 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003222#ifdef CONFIG_INTEL_IOMMU
3223 if (intel_iommu_gfx_mapped)
3224 DRM_INFO("VT-d active for gfx access\n");
3225#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003226
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003227 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003228}
3229
3230/**
3231 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003232 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003233 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003234int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003235{
Chris Wilson0088e522016-08-04 07:52:21 +01003236 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3237 int ret;
3238
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003239 INIT_LIST_HEAD(&dev_priv->vm_list);
3240
3241 /* Subtract the guard page before address space initialization to
3242 * shrink the range used by drm_mm.
3243 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003244 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003245 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003246 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003247 ggtt->base.total += PAGE_SIZE;
3248 if (!HAS_LLC(dev_priv))
3249 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003250 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003251
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003252 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3253 dev_priv->ggtt.mappable_base,
3254 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003255 ret = -EIO;
3256 goto out_gtt_cleanup;
3257 }
3258
3259 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3260
Chris Wilson0088e522016-08-04 07:52:21 +01003261 /*
3262 * Initialise stolen early so that we may reserve preallocated
3263 * objects for the BIOS to KMS transition.
3264 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003265 ret = i915_gem_init_stolen(&dev_priv->drm);
Chris Wilson0088e522016-08-04 07:52:21 +01003266 if (ret)
3267 goto out_gtt_cleanup;
3268
3269 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003270
3271out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003272 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003273 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003274}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003275
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003276int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003277{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003278 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003279 return -EIO;
3280
3281 return 0;
3282}
3283
Daniel Vetterfa423312015-04-14 17:35:23 +02003284void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3285{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003286 struct drm_i915_private *dev_priv = to_i915(dev);
3287 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003288 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003289
Chris Wilsondc979972016-05-10 14:10:04 +01003290 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003291
3292 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003293 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003294
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003295 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3296
3297 /* clflush objects bound into the GGTT and rebind them. */
3298 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003299 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003300 bool ggtt_bound = false;
3301 struct i915_vma *vma;
3302
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003303 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003304 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003305 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003306
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003307 if (!i915_vma_unbind(vma))
3308 continue;
3309
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003310 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3311 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003312 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003313 }
3314
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003315 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003316 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003317 }
3318
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003319 ggtt->base.closed = false;
3320
Daniel Vetterfa423312015-04-14 17:35:23 +02003321 if (INTEL_INFO(dev)->gen >= 8) {
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01003322 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003323 chv_setup_private_ppat(dev_priv);
3324 else
3325 bdw_setup_private_ppat(dev_priv);
3326
3327 return;
3328 }
3329
3330 if (USES_PPGTT(dev)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003331 struct i915_address_space *vm;
3332
Daniel Vetterfa423312015-04-14 17:35:23 +02003333 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3334 /* TODO: Perhaps it shouldn't be gen6 specific */
3335
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003336 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003337
Chris Wilson2bfa9962016-08-04 07:52:25 +01003338 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003339 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003340 else
3341 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003342
3343 gen6_write_page_range(dev_priv, &ppgtt->pd,
3344 0, ppgtt->base.total);
3345 }
3346 }
3347
3348 i915_ggtt_flush(dev_priv);
3349}
3350
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003351static void
3352i915_vma_retire(struct i915_gem_active *active,
3353 struct drm_i915_gem_request *rq)
3354{
3355 const unsigned int idx = rq->engine->id;
3356 struct i915_vma *vma =
3357 container_of(active, struct i915_vma, last_read[idx]);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003358 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003359
3360 GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
3361
3362 i915_vma_clear_active(vma, idx);
3363 if (i915_vma_is_active(vma))
3364 return;
3365
3366 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Chris Wilson3272db52016-08-04 16:32:32 +01003367 if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003368 WARN_ON(i915_vma_unbind(vma));
Chris Wilsond07f0e52016-10-28 13:58:44 +01003369
3370 GEM_BUG_ON(!i915_gem_object_is_active(obj));
3371 if (--obj->active_count)
3372 return;
3373
3374 /* Bump our place on the bound list to keep it roughly in LRU order
3375 * so that we don't steal from recently used but inactive objects
3376 * (unless we are forced to ofc!)
3377 */
3378 if (obj->bind_count)
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003379 list_move_tail(&obj->global_link, &rq->i915->mm.bound_list);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003380
3381 obj->mm.dirty = true; /* be paranoid */
3382
3383 if (i915_gem_object_has_active_reference(obj)) {
3384 i915_gem_object_clear_active_reference(obj);
3385 i915_gem_object_put(obj);
3386 }
3387}
3388
3389static void
3390i915_ggtt_retire__write(struct i915_gem_active *active,
3391 struct drm_i915_gem_request *request)
3392{
3393 struct i915_vma *vma =
3394 container_of(active, struct i915_vma, last_write);
3395
3396 intel_fb_obj_flush(vma->obj, true, ORIGIN_CS);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003397}
3398
3399void i915_vma_destroy(struct i915_vma *vma)
3400{
3401 GEM_BUG_ON(vma->node.allocated);
3402 GEM_BUG_ON(i915_vma_is_active(vma));
Chris Wilson3272db52016-08-04 16:32:32 +01003403 GEM_BUG_ON(!i915_vma_is_closed(vma));
Chris Wilson49ef5292016-08-18 17:17:00 +01003404 GEM_BUG_ON(vma->fence);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003405
3406 list_del(&vma->vm_link);
Chris Wilson3272db52016-08-04 16:32:32 +01003407 if (!i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003408 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
3409
3410 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
3411}
3412
3413void i915_vma_close(struct i915_vma *vma)
3414{
Chris Wilson3272db52016-08-04 16:32:32 +01003415 GEM_BUG_ON(i915_vma_is_closed(vma));
3416 vma->flags |= I915_VMA_CLOSED;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003417
Chris Wilsondfd28122016-11-04 16:12:41 +00003418 list_del(&vma->obj_link);
3419 rb_erase(&vma->obj_node, &vma->obj->vma_tree);
3420
Chris Wilson20dfbde2016-08-04 16:32:30 +01003421 if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003422 WARN_ON(i915_vma_unbind(vma));
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003423}
3424
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003425static inline long vma_compare(struct i915_vma *vma,
3426 struct i915_address_space *vm,
3427 const struct i915_ggtt_view *view)
3428{
Chris Wilsona44342a2016-11-03 20:08:52 +00003429 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003430
3431 if (vma->vm != vm)
3432 return vma->vm - vm;
3433
3434 if (!view)
3435 return vma->ggtt_view.type;
3436
3437 if (vma->ggtt_view.type != view->type)
3438 return vma->ggtt_view.type - view->type;
3439
3440 return memcmp(&vma->ggtt_view.params,
3441 &view->params,
3442 sizeof(view->params));
3443}
3444
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003445static struct i915_vma *
Chris Wilson058d88c2016-08-15 10:49:06 +01003446__i915_vma_create(struct drm_i915_gem_object *obj,
3447 struct i915_address_space *vm,
3448 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003449{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003450 struct i915_vma *vma;
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003451 struct rb_node *rb, **p;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003452 int i;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003453
Chris Wilson50e046b2016-08-04 07:52:46 +01003454 GEM_BUG_ON(vm->closed);
3455
Chris Wilsone20d2ab2015-04-07 16:20:58 +01003456 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003457 if (vma == NULL)
3458 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003459
Ben Widawsky6f65e292013-12-06 14:10:56 -08003460 INIT_LIST_HEAD(&vma->exec_list);
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003461 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
3462 init_request_active(&vma->last_read[i], i915_vma_retire);
Chris Wilsond07f0e52016-10-28 13:58:44 +01003463 init_request_active(&vma->last_write,
3464 i915_is_ggtt(vm) ? i915_ggtt_retire__write : NULL);
Chris Wilson49ef5292016-08-18 17:17:00 +01003465 init_request_active(&vma->last_fence, NULL);
Chris Wilson50e046b2016-08-04 07:52:46 +01003466 list_add(&vma->vm_link, &vm->unbound_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003467 vma->vm = vm;
3468 vma->obj = obj;
Chris Wilsonde180032016-08-04 16:32:29 +01003469 vma->size = obj->base.size;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003470
Chris Wilson058d88c2016-08-15 10:49:06 +01003471 if (view) {
Chris Wilsonde180032016-08-04 16:32:29 +01003472 vma->ggtt_view = *view;
3473 if (view->type == I915_GGTT_VIEW_PARTIAL) {
3474 vma->size = view->params.partial.size;
3475 vma->size <<= PAGE_SHIFT;
3476 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3477 vma->size =
3478 intel_rotation_info_size(&view->params.rotated);
3479 vma->size <<= PAGE_SHIFT;
3480 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003481 }
3482
3483 if (i915_is_ggtt(vm)) {
3484 vma->flags |= I915_VMA_GGTT;
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003485 list_add(&vma->obj_link, &obj->vma_list);
Chris Wilsonde180032016-08-04 16:32:29 +01003486 } else {
Chris Wilson596c5922016-02-26 11:03:20 +00003487 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003488 list_add_tail(&vma->obj_link, &obj->vma_list);
Chris Wilsonde180032016-08-04 16:32:29 +01003489 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08003490
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003491 rb = NULL;
3492 p = &obj->vma_tree.rb_node;
3493 while (*p) {
3494 struct i915_vma *pos;
3495
3496 rb = *p;
3497 pos = rb_entry(rb, struct i915_vma, obj_node);
3498 if (vma_compare(pos, vm, view) < 0)
3499 p = &rb->rb_right;
3500 else
3501 p = &rb->rb_left;
3502 }
3503 rb_link_node(&vma->obj_node, rb, p);
3504 rb_insert_color(&vma->obj_node, &obj->vma_tree);
3505
Ben Widawsky6f65e292013-12-06 14:10:56 -08003506 return vma;
3507}
3508
3509struct i915_vma *
Chris Wilson81a8aa42016-08-15 10:48:48 +01003510i915_vma_create(struct drm_i915_gem_object *obj,
3511 struct i915_address_space *vm,
3512 const struct i915_ggtt_view *view)
3513{
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003514 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson81a8aa42016-08-15 10:48:48 +01003515 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Chris Wilson058d88c2016-08-15 10:49:06 +01003516 GEM_BUG_ON(i915_gem_obj_to_vma(obj, vm, view));
Chris Wilson81a8aa42016-08-15 10:48:48 +01003517
Chris Wilson058d88c2016-08-15 10:49:06 +01003518 return __i915_vma_create(obj, vm, view);
3519}
3520
3521struct i915_vma *
3522i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3523 struct i915_address_space *vm,
3524 const struct i915_ggtt_view *view)
3525{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003526 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003527
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003528 rb = obj->vma_tree.rb_node;
3529 while (rb) {
3530 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3531 long cmp;
3532
3533 cmp = vma_compare(vma, vm, view);
3534 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003535 return vma;
3536
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003537 if (cmp < 0)
3538 rb = rb->rb_right;
3539 else
3540 rb = rb->rb_left;
3541 }
3542
Chris Wilson058d88c2016-08-15 10:49:06 +01003543 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003544}
3545
3546struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003547i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003548 struct i915_address_space *vm,
3549 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003550{
3551 struct i915_vma *vma;
3552
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003553 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003554 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3555
3556 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003557 if (!vma) {
Chris Wilson058d88c2016-08-15 10:49:06 +01003558 vma = __i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003559 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3560 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003561
Chris Wilson3272db52016-08-04 16:32:32 +01003562 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003563 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003564}
3565
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003566static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003567rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003568 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003569 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003570 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003571{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003572 unsigned int column, row;
3573 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003574
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003575 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003576 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003577 for (row = 0; row < height; row++) {
3578 st->nents++;
3579 /* We don't need the pages, but need to initialize
3580 * the entries so the sg list can be happily traversed.
3581 * The only thing we need are DMA addresses.
3582 */
3583 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003584 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003585 sg_dma_len(sg) = PAGE_SIZE;
3586 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003587 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003588 }
3589 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003590
3591 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003592}
3593
3594static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003595intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003596 struct drm_i915_gem_object *obj)
3597{
Dave Gordon85d12252016-05-20 11:54:06 +01003598 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003599 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003600 struct sgt_iter sgt_iter;
3601 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003602 unsigned long i;
3603 dma_addr_t *page_addr_list;
3604 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003605 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003606 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003607
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003608 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003609 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003610 sizeof(dma_addr_t),
3611 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003612 if (!page_addr_list)
3613 return ERR_PTR(ret);
3614
3615 /* Allocate target SG list. */
3616 st = kmalloc(sizeof(*st), GFP_KERNEL);
3617 if (!st)
3618 goto err_st_alloc;
3619
Ville Syrjälä6687c902015-09-15 13:16:41 +03003620 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003621 if (ret)
3622 goto err_sg_alloc;
3623
3624 /* Populate source page list from the object. */
3625 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003626 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003627 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003628
Dave Gordon85d12252016-05-20 11:54:06 +01003629 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003630 st->nents = 0;
3631 sg = st->sgl;
3632
Ville Syrjälä6687c902015-09-15 13:16:41 +03003633 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3634 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3635 rot_info->plane[i].width, rot_info->plane[i].height,
3636 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003637 }
3638
Ville Syrjälä6687c902015-09-15 13:16:41 +03003639 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3640 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003641
3642 drm_free_large(page_addr_list);
3643
3644 return st;
3645
3646err_sg_alloc:
3647 kfree(st);
3648err_st_alloc:
3649 drm_free_large(page_addr_list);
3650
Ville Syrjälä6687c902015-09-15 13:16:41 +03003651 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3652 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3653
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003654 return ERR_PTR(ret);
3655}
3656
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003657static struct sg_table *
3658intel_partial_pages(const struct i915_ggtt_view *view,
3659 struct drm_i915_gem_object *obj)
3660{
3661 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003662 struct scatterlist *sg, *iter;
3663 unsigned int count = view->params.partial.size;
3664 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003665 int ret = -ENOMEM;
3666
3667 st = kmalloc(sizeof(*st), GFP_KERNEL);
3668 if (!st)
3669 goto err_st_alloc;
3670
Chris Wilsond2a84a72016-10-28 13:58:34 +01003671 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003672 if (ret)
3673 goto err_sg_alloc;
3674
Chris Wilsond2a84a72016-10-28 13:58:34 +01003675 iter = i915_gem_object_get_sg(obj,
3676 view->params.partial.offset,
3677 &offset);
3678 GEM_BUG_ON(!iter);
3679
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003680 sg = st->sgl;
3681 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003682 do {
3683 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003684
Chris Wilsond2a84a72016-10-28 13:58:34 +01003685 len = min(iter->length - (offset << PAGE_SHIFT),
3686 count << PAGE_SHIFT);
3687 sg_set_page(sg, NULL, len, 0);
3688 sg_dma_address(sg) =
3689 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3690 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003691
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003692 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003693 count -= len >> PAGE_SHIFT;
3694 if (count == 0) {
3695 sg_mark_end(sg);
3696 return st;
3697 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003698
Chris Wilsond2a84a72016-10-28 13:58:34 +01003699 sg = __sg_next(sg);
3700 iter = __sg_next(iter);
3701 offset = 0;
3702 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003703
3704err_sg_alloc:
3705 kfree(st);
3706err_st_alloc:
3707 return ERR_PTR(ret);
3708}
3709
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003710static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003711i915_get_ggtt_vma_pages(struct i915_vma *vma)
3712{
3713 int ret = 0;
3714
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003715 /* The vma->pages are only valid within the lifespan of the borrowed
3716 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3717 * must be the vma->pages. A simple rule is that vma->pages must only
3718 * be accessed when the obj->mm.pages are pinned.
3719 */
3720 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3721
Chris Wilson247177d2016-08-15 10:48:47 +01003722 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003723 return 0;
3724
3725 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003726 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003727 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003728 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003729 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003730 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003731 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003732 else
3733 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3734 vma->ggtt_view.type);
3735
Chris Wilson247177d2016-08-15 10:48:47 +01003736 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003737 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003738 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003739 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003740 } else if (IS_ERR(vma->pages)) {
3741 ret = PTR_ERR(vma->pages);
3742 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003743 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3744 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003745 }
3746
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003747 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003748}
3749
3750/**
3751 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3752 * @vma: VMA to map
3753 * @cache_level: mapping cache level
3754 * @flags: flags like global or local mapping
3755 *
3756 * DMA addresses are taken from the scatter-gather table of this object (or of
3757 * this VMA in case of non-default GGTT views) and PTE entries set up.
3758 * Note that DMA addresses are also the only part of the SG table we care about.
3759 */
3760int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3761 u32 flags)
3762{
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003763 u32 bind_flags;
Chris Wilson3272db52016-08-04 16:32:32 +01003764 u32 vma_flags;
3765 int ret;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003766
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003767 if (WARN_ON(flags == 0))
3768 return -EINVAL;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003769
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003770 bind_flags = 0;
Daniel Vetter08755462015-04-20 09:04:05 -07003771 if (flags & PIN_GLOBAL)
Chris Wilson3272db52016-08-04 16:32:32 +01003772 bind_flags |= I915_VMA_GLOBAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003773 if (flags & PIN_USER)
Chris Wilson3272db52016-08-04 16:32:32 +01003774 bind_flags |= I915_VMA_LOCAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003775
Chris Wilson3272db52016-08-04 16:32:32 +01003776 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
Daniel Vetter08755462015-04-20 09:04:05 -07003777 if (flags & PIN_UPDATE)
Chris Wilson3272db52016-08-04 16:32:32 +01003778 bind_flags |= vma_flags;
Daniel Vetter08755462015-04-20 09:04:05 -07003779 else
Chris Wilson3272db52016-08-04 16:32:32 +01003780 bind_flags &= ~vma_flags;
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003781 if (bind_flags == 0)
3782 return 0;
3783
Chris Wilson3272db52016-08-04 16:32:32 +01003784 if (vma_flags == 0 && vma->vm->allocate_va_range) {
Chris Wilson596c5922016-02-26 11:03:20 +00003785 trace_i915_va_alloc(vma);
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003786 ret = vma->vm->allocate_va_range(vma->vm,
3787 vma->node.start,
3788 vma->node.size);
3789 if (ret)
3790 return ret;
3791 }
3792
3793 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003794 if (ret)
3795 return ret;
Daniel Vetter08755462015-04-20 09:04:05 -07003796
Chris Wilson3272db52016-08-04 16:32:32 +01003797 vma->flags |= bind_flags;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003798 return 0;
3799}
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003800
Chris Wilson8ef85612016-04-28 09:56:39 +01003801void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
3802{
3803 void __iomem *ptr;
3804
Chris Wilsone5cdb222016-08-15 10:48:56 +01003805 /* Access through the GTT requires the device to be awake. */
3806 assert_rpm_wakelock_held(to_i915(vma->vm->dev));
3807
Chris Wilson8ef85612016-04-28 09:56:39 +01003808 lockdep_assert_held(&vma->vm->dev->struct_mutex);
Chris Wilson05a20d02016-08-18 17:16:55 +01003809 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma)))
Chris Wilson406ea8d2016-07-20 13:31:55 +01003810 return IO_ERR_PTR(-ENODEV);
Chris Wilson8ef85612016-04-28 09:56:39 +01003811
Chris Wilson3272db52016-08-04 16:32:32 +01003812 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
3813 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
Chris Wilson8ef85612016-04-28 09:56:39 +01003814
3815 ptr = vma->iomap;
3816 if (ptr == NULL) {
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003817 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
Chris Wilson8ef85612016-04-28 09:56:39 +01003818 vma->node.start,
3819 vma->node.size);
3820 if (ptr == NULL)
Chris Wilson406ea8d2016-07-20 13:31:55 +01003821 return IO_ERR_PTR(-ENOMEM);
Chris Wilson8ef85612016-04-28 09:56:39 +01003822
3823 vma->iomap = ptr;
3824 }
3825
Chris Wilson20dfbde2016-08-04 16:32:30 +01003826 __i915_vma_pin(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +01003827 return ptr;
3828}
Chris Wilson19880c42016-08-15 10:49:05 +01003829
3830void i915_vma_unpin_and_release(struct i915_vma **p_vma)
3831{
3832 struct i915_vma *vma;
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003833 struct drm_i915_gem_object *obj;
Chris Wilson19880c42016-08-15 10:49:05 +01003834
3835 vma = fetch_and_zero(p_vma);
3836 if (!vma)
3837 return;
3838
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003839 obj = vma->obj;
3840
Chris Wilson19880c42016-08-15 10:49:05 +01003841 i915_vma_unpin(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003842 i915_vma_close(vma);
3843
3844 __i915_gem_object_release_unless_active(obj);
Chris Wilson19880c42016-08-15 10:49:05 +01003845}