blob: 062fb0ad75dac88e9642a8fd4bc567ed3baacb60 [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"
34
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010035#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
36
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000037/**
38 * DOC: Global GTT views
39 *
40 * Background and previous state
41 *
42 * Historically objects could exists (be bound) in global GTT space only as
43 * singular instances with a view representing all of the object's backing pages
44 * in a linear fashion. This view will be called a normal view.
45 *
46 * To support multiple views of the same object, where the number of mapped
47 * pages is not equal to the backing store, or where the layout of the pages
48 * is not linear, concept of a GGTT view was added.
49 *
50 * One example of an alternative view is a stereo display driven by a single
51 * image. In this case we would have a framebuffer looking like this
52 * (2x2 pages):
53 *
54 * 12
55 * 34
56 *
57 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
58 * rendering. In contrast, fed to the display engine would be an alternative
59 * view which could look something like this:
60 *
61 * 1212
62 * 3434
63 *
64 * In this example both the size and layout of pages in the alternative view is
65 * different from the normal view.
66 *
67 * Implementation and usage
68 *
69 * GGTT views are implemented using VMAs and are distinguished via enum
70 * i915_ggtt_view_type and struct i915_ggtt_view.
71 *
72 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020073 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
74 * renaming in large amounts of code. They take the struct i915_ggtt_view
75 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000076 *
77 * As a helper for callers which are only interested in the normal view,
78 * globally const i915_ggtt_view_normal singleton instance exists. All old core
79 * GEM API functions, the ones not taking the view parameter, are operating on,
80 * or with the normal GGTT view.
81 *
82 * Code wanting to add or use a new GGTT view needs to:
83 *
84 * 1. Add a new enum with a suitable name.
85 * 2. Extend the metadata in the i915_ggtt_view structure if required.
86 * 3. Add support to i915_get_vma_pages().
87 *
88 * New views are required to build a scatter-gather table from within the
89 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
90 * exists for the lifetime of an VMA.
91 *
92 * Core API is designed to have copy semantics which means that passed in
93 * struct i915_ggtt_view does not need to be persistent (left around after
94 * calling the core API functions).
95 *
96 */
97
Chris Wilsonce7fda22016-04-28 09:56:38 +010098static inline struct i915_ggtt *
99i915_vm_to_ggtt(struct i915_address_space *vm)
100{
101 GEM_BUG_ON(!i915_is_ggtt(vm));
102 return container_of(vm, struct i915_ggtt, base);
103}
104
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200105static int
106i915_get_ggtt_vma_pages(struct i915_vma *vma);
107
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200108const struct i915_ggtt_view i915_ggtt_view_normal = {
109 .type = I915_GGTT_VIEW_NORMAL,
110};
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200111const struct i915_ggtt_view i915_ggtt_view_rotated = {
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200112 .type = I915_GGTT_VIEW_ROTATED,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200113};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000114
Chris Wilsonc0336662016-05-06 15:40:21 +0100115int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
116 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200117{
Chris Wilson1893a712014-09-19 11:56:27 +0100118 bool has_aliasing_ppgtt;
119 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100120 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100121
Chris Wilsonc0336662016-05-06 15:40:21 +0100122 has_aliasing_ppgtt = INTEL_GEN(dev_priv) >= 6;
123 has_full_ppgtt = INTEL_GEN(dev_priv) >= 7;
124 has_full_48bit_ppgtt =
125 IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9;
Chris Wilson1893a712014-09-19 11:56:27 +0100126
Zhi Wange320d402016-09-06 12:04:12 +0800127 if (intel_vgpu_active(dev_priv)) {
128 /* emulation is too hard */
129 has_full_ppgtt = false;
130 has_full_48bit_ppgtt = false;
131 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800132
Chris Wilson0e4ca102016-04-29 13:18:22 +0100133 if (!has_aliasing_ppgtt)
134 return 0;
135
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000136 /*
137 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
138 * execlists, the sole mechanism available to submit work.
139 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100140 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200141 return 0;
142
143 if (enable_ppgtt == 1)
144 return 1;
145
Chris Wilson1893a712014-09-19 11:56:27 +0100146 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200147 return 2;
148
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100149 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
150 return 3;
151
Daniel Vetter93a25a92014-03-06 09:40:43 +0100152#ifdef CONFIG_INTEL_IOMMU
153 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100154 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100155 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200156 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100157 }
158#endif
159
Jesse Barnes62942ed2014-06-13 09:28:33 -0700160 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100161 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700162 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
163 return 0;
164 }
165
Zhi Wange320d402016-09-06 12:04:12 +0800166 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100167 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000168 else
169 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100170}
171
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200172static int ppgtt_bind_vma(struct i915_vma *vma,
173 enum i915_cache_level cache_level,
174 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200175{
176 u32 pte_flags = 0;
177
Chris Wilson247177d2016-08-15 10:48:47 +0100178 vma->pages = vma->obj->pages;
179
Daniel Vetter47552652015-04-14 17:35:24 +0200180 /* Currently applicable only to VLV */
181 if (vma->obj->gt_ro)
182 pte_flags |= PTE_READ_ONLY;
183
Chris Wilson247177d2016-08-15 10:48:47 +0100184 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200185 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200186
187 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200188}
189
190static void ppgtt_unbind_vma(struct i915_vma *vma)
191{
192 vma->vm->clear_range(vma->vm,
193 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200194 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200195}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800196
Daniel Vetter2c642b02015-04-14 17:35:26 +0200197static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200198 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700199{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200200 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700201 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300202
203 switch (level) {
204 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800205 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300206 break;
207 case I915_CACHE_WT:
208 pte |= PPAT_DISPLAY_ELLC_INDEX;
209 break;
210 default:
211 pte |= PPAT_CACHED_INDEX;
212 break;
213 }
214
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700215 return pte;
216}
217
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300218static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
219 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800220{
Michel Thierry07749ef2015-03-16 16:00:54 +0000221 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800222 pde |= addr;
223 if (level != I915_CACHE_NONE)
224 pde |= PPAT_CACHED_PDE_INDEX;
225 else
226 pde |= PPAT_UNCACHED_INDEX;
227 return pde;
228}
229
Michel Thierry762d9932015-07-30 11:05:29 +0100230#define gen8_pdpe_encode gen8_pde_encode
231#define gen8_pml4e_encode gen8_pde_encode
232
Michel Thierry07749ef2015-03-16 16:00:54 +0000233static gen6_pte_t snb_pte_encode(dma_addr_t addr,
234 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200235 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700236{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200237 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700238 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700239
240 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100241 case I915_CACHE_L3_LLC:
242 case I915_CACHE_LLC:
243 pte |= GEN6_PTE_CACHE_LLC;
244 break;
245 case I915_CACHE_NONE:
246 pte |= GEN6_PTE_UNCACHED;
247 break;
248 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100249 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100250 }
251
252 return pte;
253}
254
Michel Thierry07749ef2015-03-16 16:00:54 +0000255static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
256 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200257 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100258{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200259 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100260 pte |= GEN6_PTE_ADDR_ENCODE(addr);
261
262 switch (level) {
263 case I915_CACHE_L3_LLC:
264 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700265 break;
266 case I915_CACHE_LLC:
267 pte |= GEN6_PTE_CACHE_LLC;
268 break;
269 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700270 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700271 break;
272 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100273 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700274 }
275
Ben Widawsky54d12522012-09-24 16:44:32 -0700276 return pte;
277}
278
Michel Thierry07749ef2015-03-16 16:00:54 +0000279static gen6_pte_t byt_pte_encode(dma_addr_t addr,
280 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200281 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700282{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200283 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700284 pte |= GEN6_PTE_ADDR_ENCODE(addr);
285
Akash Goel24f3a8c2014-06-17 10:59:42 +0530286 if (!(flags & PTE_READ_ONLY))
287 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700288
289 if (level != I915_CACHE_NONE)
290 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
291
292 return pte;
293}
294
Michel Thierry07749ef2015-03-16 16:00:54 +0000295static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
296 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200297 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700298{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200299 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700300 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700301
302 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700303 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700304
305 return pte;
306}
307
Michel Thierry07749ef2015-03-16 16:00:54 +0000308static gen6_pte_t iris_pte_encode(dma_addr_t addr,
309 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200310 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700311{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200312 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700313 pte |= HSW_PTE_ADDR_ENCODE(addr);
314
Chris Wilson651d7942013-08-08 14:41:10 +0100315 switch (level) {
316 case I915_CACHE_NONE:
317 break;
318 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000319 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100320 break;
321 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000322 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100323 break;
324 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700325
326 return pte;
327}
328
Mika Kuoppalac114f762015-06-25 18:35:13 +0300329static int __setup_page_dma(struct drm_device *dev,
330 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000331{
David Weinehallc49d13e2016-08-22 13:32:42 +0300332 struct device *kdev = &dev->pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000333
Mika Kuoppalac114f762015-06-25 18:35:13 +0300334 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300335 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000336 return -ENOMEM;
337
David Weinehallc49d13e2016-08-22 13:32:42 +0300338 p->daddr = dma_map_page(kdev,
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300339 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
340
David Weinehallc49d13e2016-08-22 13:32:42 +0300341 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300342 __free_page(p->page);
343 return -EINVAL;
344 }
345
Michel Thierry1266cdb2015-03-24 17:06:33 +0000346 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000347}
348
Mika Kuoppalac114f762015-06-25 18:35:13 +0300349static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
350{
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100351 return __setup_page_dma(dev, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300352}
353
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300354static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
355{
David Weinehall52a05c32016-08-22 13:32:44 +0300356 struct pci_dev *pdev = dev->pdev;
357
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300358 if (WARN_ON(!p->page))
359 return;
360
David Weinehall52a05c32016-08-22 13:32:44 +0300361 dma_unmap_page(&pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300362 __free_page(p->page);
363 memset(p, 0, sizeof(*p));
364}
365
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300366static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300367{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300368 return kmap_atomic(p->page);
369}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300370
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300371/* We use the flushing unmap only with ppgtt structures:
372 * page directories, page tables and scratch pages.
373 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100374static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300375{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300376 /* There are only few exceptions for gen >=6. chv and bxt.
377 * And we are not sure about the latter so play safe for now.
378 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100379 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300380 drm_clflush_virt_range(vaddr, PAGE_SIZE);
381
382 kunmap_atomic(vaddr);
383}
384
Mika Kuoppala567047b2015-06-25 18:35:12 +0300385#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100386#define kunmap_px(ppgtt, vaddr) \
387 kunmap_page_dma(to_i915((ppgtt)->base.dev), (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300388
Mika Kuoppala567047b2015-06-25 18:35:12 +0300389#define setup_px(dev, px) setup_page_dma((dev), px_base(px))
390#define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100391#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
392#define fill32_px(dev_priv, px, v) \
393 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300394
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100395static void fill_page_dma(struct drm_i915_private *dev_priv,
396 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300397{
398 int i;
399 uint64_t * const vaddr = kmap_page_dma(p);
400
401 for (i = 0; i < 512; i++)
402 vaddr[i] = val;
403
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100404 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300405}
406
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100407static void fill_page_dma_32(struct drm_i915_private *dev_priv,
408 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300409{
410 uint64_t v = val32;
411
412 v = v << 32 | val32;
413
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100414 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300415}
416
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100417static int
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100418setup_scratch_page(struct drm_device *dev,
419 struct i915_page_dma *scratch,
420 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300421{
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100422 return __setup_page_dma(dev, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300423}
424
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100425static void cleanup_scratch_page(struct drm_device *dev,
426 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300427{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100428 cleanup_page_dma(dev, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300429}
430
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300431static struct i915_page_table *alloc_pt(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000432{
Michel Thierryec565b32015-04-08 12:13:23 +0100433 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000434 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
435 GEN8_PTES : GEN6_PTES;
436 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000437
438 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
439 if (!pt)
440 return ERR_PTR(-ENOMEM);
441
Ben Widawsky678d96f2015-03-16 16:00:56 +0000442 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
443 GFP_KERNEL);
444
445 if (!pt->used_ptes)
446 goto fail_bitmap;
447
Mika Kuoppala567047b2015-06-25 18:35:12 +0300448 ret = setup_px(dev, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000449 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300450 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000451
452 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000453
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300454fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000455 kfree(pt->used_ptes);
456fail_bitmap:
457 kfree(pt);
458
459 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000460}
461
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300462static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000463{
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300464 cleanup_px(dev, pt);
465 kfree(pt->used_ptes);
466 kfree(pt);
467}
468
469static void gen8_initialize_pt(struct i915_address_space *vm,
470 struct i915_page_table *pt)
471{
472 gen8_pte_t scratch_pte;
473
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100474 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200475 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300476
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100477 fill_px(to_i915(vm->dev), pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300478}
479
480static void gen6_initialize_pt(struct i915_address_space *vm,
481 struct i915_page_table *pt)
482{
483 gen6_pte_t scratch_pte;
484
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100485 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300486
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100487 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200488 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300489
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100490 fill32_px(to_i915(vm->dev), pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000491}
492
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300493static struct i915_page_directory *alloc_pd(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000494{
Michel Thierryec565b32015-04-08 12:13:23 +0100495 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100496 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000497
498 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
499 if (!pd)
500 return ERR_PTR(-ENOMEM);
501
Michel Thierry33c88192015-04-08 12:13:33 +0100502 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
503 sizeof(*pd->used_pdes), GFP_KERNEL);
504 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300505 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100506
Mika Kuoppala567047b2015-06-25 18:35:12 +0300507 ret = setup_px(dev, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100508 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300509 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100510
Ben Widawsky06fda602015-02-24 16:22:36 +0000511 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100512
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300513fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100514 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300515fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100516 kfree(pd);
517
518 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000519}
520
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300521static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
522{
523 if (px_page(pd)) {
524 cleanup_px(dev, pd);
525 kfree(pd->used_pdes);
526 kfree(pd);
527 }
528}
529
530static void gen8_initialize_pd(struct i915_address_space *vm,
531 struct i915_page_directory *pd)
532{
533 gen8_pde_t scratch_pde;
534
535 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
536
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100537 fill_px(to_i915(vm->dev), pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300538}
539
Michel Thierry6ac18502015-07-29 17:23:46 +0100540static int __pdp_init(struct drm_device *dev,
541 struct i915_page_directory_pointer *pdp)
542{
543 size_t pdpes = I915_PDPES_PER_PDP(dev);
544
545 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
546 sizeof(unsigned long),
547 GFP_KERNEL);
548 if (!pdp->used_pdpes)
549 return -ENOMEM;
550
551 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
552 GFP_KERNEL);
553 if (!pdp->page_directory) {
554 kfree(pdp->used_pdpes);
555 /* the PDP might be the statically allocated top level. Keep it
556 * as clean as possible */
557 pdp->used_pdpes = NULL;
558 return -ENOMEM;
559 }
560
561 return 0;
562}
563
564static void __pdp_fini(struct i915_page_directory_pointer *pdp)
565{
566 kfree(pdp->used_pdpes);
567 kfree(pdp->page_directory);
568 pdp->page_directory = NULL;
569}
570
Michel Thierry762d9932015-07-30 11:05:29 +0100571static struct
572i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
573{
574 struct i915_page_directory_pointer *pdp;
575 int ret = -ENOMEM;
576
577 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
578
579 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
580 if (!pdp)
581 return ERR_PTR(-ENOMEM);
582
583 ret = __pdp_init(dev, pdp);
584 if (ret)
585 goto fail_bitmap;
586
587 ret = setup_px(dev, pdp);
588 if (ret)
589 goto fail_page_m;
590
591 return pdp;
592
593fail_page_m:
594 __pdp_fini(pdp);
595fail_bitmap:
596 kfree(pdp);
597
598 return ERR_PTR(ret);
599}
600
Michel Thierry6ac18502015-07-29 17:23:46 +0100601static void free_pdp(struct drm_device *dev,
602 struct i915_page_directory_pointer *pdp)
603{
604 __pdp_fini(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100605 if (USES_FULL_48BIT_PPGTT(dev)) {
606 cleanup_px(dev, pdp);
607 kfree(pdp);
608 }
609}
610
Michel Thierry69ab76f2015-07-29 17:23:55 +0100611static void gen8_initialize_pdp(struct i915_address_space *vm,
612 struct i915_page_directory_pointer *pdp)
613{
614 gen8_ppgtt_pdpe_t scratch_pdpe;
615
616 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
617
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100618 fill_px(to_i915(vm->dev), pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100619}
620
621static void gen8_initialize_pml4(struct i915_address_space *vm,
622 struct i915_pml4 *pml4)
623{
624 gen8_ppgtt_pml4e_t scratch_pml4e;
625
626 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
627 I915_CACHE_LLC);
628
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100629 fill_px(to_i915(vm->dev), pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100630}
631
Michel Thierry762d9932015-07-30 11:05:29 +0100632static void
633gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
634 struct i915_page_directory_pointer *pdp,
635 struct i915_page_directory *pd,
636 int index)
637{
638 gen8_ppgtt_pdpe_t *page_directorypo;
639
640 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
641 return;
642
643 page_directorypo = kmap_px(pdp);
644 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
645 kunmap_px(ppgtt, page_directorypo);
646}
647
648static void
649gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
650 struct i915_pml4 *pml4,
651 struct i915_page_directory_pointer *pdp,
652 int index)
653{
654 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
655
656 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
657 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
658 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100659}
660
Ben Widawsky94e409c2013-11-04 22:29:36 -0800661/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100662static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100663 unsigned entry,
664 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800665{
Chris Wilson7e37f882016-08-02 22:50:21 +0100666 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000667 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800668 int ret;
669
670 BUG_ON(entry >= 4);
671
John Harrison5fb9de12015-05-29 17:44:07 +0100672 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800673 if (ret)
674 return ret;
675
Chris Wilsonb5321f32016-08-02 22:50:18 +0100676 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
677 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
678 intel_ring_emit(ring, upper_32_bits(addr));
679 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
680 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
681 intel_ring_emit(ring, lower_32_bits(addr));
682 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800683
684 return 0;
685}
686
Michel Thierry2dba3232015-07-30 11:06:23 +0100687static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
688 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800689{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800690 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800691
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100692 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300693 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
694
John Harrisone85b26d2015-05-29 17:43:56 +0100695 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800696 if (ret)
697 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800698 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800699
Ben Widawskyeeb94882013-12-06 14:11:10 -0800700 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800701}
702
Michel Thierry2dba3232015-07-30 11:06:23 +0100703static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
704 struct drm_i915_gem_request *req)
705{
706 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
707}
708
Michał Winiarski2ce51792016-10-13 14:02:42 +0200709/* Removes entries from a single page table, releasing it if it's empty.
710 * Caller can use the return value to update higher-level entries.
711 */
712static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200713 struct i915_page_table *pt,
714 uint64_t start,
715 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700716{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300717 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200718 unsigned int pte_start = gen8_pte_index(start);
719 unsigned int num_entries = gen8_pte_count(start, length);
720 uint64_t pte;
721 gen8_pte_t *pt_vaddr;
722 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
723 I915_CACHE_LLC);
724
725 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200726 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700727
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200728 bitmap_clear(pt->used_ptes, pte_start, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000729
Michał Winiarski2ce51792016-10-13 14:02:42 +0200730 if (bitmap_empty(pt->used_ptes, GEN8_PTES)) {
731 free_pt(vm->dev, pt);
732 return true;
733 }
734
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200735 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000736
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200737 for (pte = pte_start; pte < num_entries; pte++)
738 pt_vaddr[pte] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000739
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200740 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200741
742 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200743}
744
Michał Winiarski2ce51792016-10-13 14:02:42 +0200745/* Removes entries from a single page dir, releasing it if it's empty.
746 * Caller can use the return value to update higher-level entries
747 */
748static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200749 struct i915_page_directory *pd,
750 uint64_t start,
751 uint64_t length)
752{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200753 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200754 struct i915_page_table *pt;
755 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200756 gen8_pde_t *pde_vaddr;
757 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
758 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200759
760 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000761 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100762 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000763
Michał Winiarski2ce51792016-10-13 14:02:42 +0200764 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
765 __clear_bit(pde, pd->used_pdes);
766 pde_vaddr = kmap_px(pd);
767 pde_vaddr[pde] = scratch_pde;
768 kunmap_px(ppgtt, pde_vaddr);
769 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200770 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200771
772 if (bitmap_empty(pd->used_pdes, I915_PDES)) {
773 free_pd(vm->dev, pd);
774 return true;
775 }
776
777 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200778}
Ben Widawsky06fda602015-02-24 16:22:36 +0000779
Michał Winiarski2ce51792016-10-13 14:02:42 +0200780/* Removes entries from a single page dir pointer, releasing it if it's empty.
781 * Caller can use the return value to update higher-level entries
782 */
783static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200784 struct i915_page_directory_pointer *pdp,
785 uint64_t start,
786 uint64_t length)
787{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200788 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200789 struct i915_page_directory *pd;
790 uint64_t pdpe;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200791 gen8_ppgtt_pdpe_t *pdpe_vaddr;
792 gen8_ppgtt_pdpe_t scratch_pdpe =
793 gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200794
795 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
796 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100797 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000798
Michał Winiarski2ce51792016-10-13 14:02:42 +0200799 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
800 __clear_bit(pdpe, pdp->used_pdpes);
801 if (USES_FULL_48BIT_PPGTT(vm->dev)) {
802 pdpe_vaddr = kmap_px(pdp);
803 pdpe_vaddr[pdpe] = scratch_pdpe;
804 kunmap_px(ppgtt, pdpe_vaddr);
805 }
806 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200807 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200808
809 if (USES_FULL_48BIT_PPGTT(vm->dev) &&
810 bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(vm->dev))) {
811 free_pdp(vm->dev, pdp);
812 return true;
813 }
814
815 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200816}
Ben Widawsky459108b2013-11-02 21:07:23 -0700817
Michał Winiarski2ce51792016-10-13 14:02:42 +0200818/* Removes entries from a single pml4.
819 * This is the top-level structure in 4-level page tables used on gen8+.
820 * Empty entries are always scratch pml4e.
821 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200822static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
823 struct i915_pml4 *pml4,
824 uint64_t start,
825 uint64_t length)
826{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200827 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200828 struct i915_page_directory_pointer *pdp;
829 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200830 gen8_ppgtt_pml4e_t *pml4e_vaddr;
831 gen8_ppgtt_pml4e_t scratch_pml4e =
832 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
833
834 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->dev));
Ben Widawsky459108b2013-11-02 21:07:23 -0700835
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200836 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
837 if (WARN_ON(!pml4->pdps[pml4e]))
838 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700839
Michał Winiarski2ce51792016-10-13 14:02:42 +0200840 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
841 __clear_bit(pml4e, pml4->used_pml4es);
842 pml4e_vaddr = kmap_px(pml4);
843 pml4e_vaddr[pml4e] = scratch_pml4e;
844 kunmap_px(ppgtt, pml4e_vaddr);
845 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700846 }
847}
848
Michel Thierryf9b5b782015-07-30 11:02:49 +0100849static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200850 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700851{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300852 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100853
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200854 if (USES_FULL_48BIT_PPGTT(vm->dev))
855 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
856 else
857 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100858}
859
860static void
861gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
862 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100863 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100864 uint64_t start,
865 enum i915_cache_level cache_level)
866{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300867 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000868 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100869 unsigned pdpe = gen8_pdpe_index(start);
870 unsigned pde = gen8_pde_index(start);
871 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700872
Chris Wilson6f1cc992013-12-31 15:50:31 +0000873 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700874
Michel Thierry3387d432015-08-03 09:52:47 +0100875 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000876 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100877 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100878 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300879 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000880 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800881
882 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100883 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200884 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000885 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300886 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000887 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000888 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100889 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
890 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800891 pde = 0;
892 }
893 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700894 }
895 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300896
897 if (pt_vaddr)
898 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700899}
900
Michel Thierryf9b5b782015-07-30 11:02:49 +0100901static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
902 struct sg_table *pages,
903 uint64_t start,
904 enum i915_cache_level cache_level,
905 u32 unused)
906{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300907 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100908 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100909
Michel Thierry3387d432015-08-03 09:52:47 +0100910 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100911
912 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
913 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
914 cache_level);
915 } else {
916 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000917 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100918 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
919
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000920 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100921 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
922 start, cache_level);
923 }
924 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100925}
926
Michel Thierryf37c0502015-06-10 17:46:39 +0100927static void gen8_free_page_tables(struct drm_device *dev,
928 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800929{
930 int i;
931
Mika Kuoppala567047b2015-06-25 18:35:12 +0300932 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800933 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800934
Michel Thierry33c88192015-04-08 12:13:33 +0100935 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000936 if (WARN_ON(!pd->page_table[i]))
937 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800938
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300939 free_pt(dev, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000940 pd->page_table[i] = NULL;
941 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000942}
943
Mika Kuoppala8776f022015-06-30 18:16:40 +0300944static int gen8_init_scratch(struct i915_address_space *vm)
945{
946 struct drm_device *dev = vm->dev;
Matthew Auld64c050d2016-04-27 13:19:25 +0100947 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300948
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100949 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100950 if (ret)
951 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300952
953 vm->scratch_pt = alloc_pt(dev);
954 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100955 ret = PTR_ERR(vm->scratch_pt);
956 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300957 }
958
959 vm->scratch_pd = alloc_pd(dev);
960 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100961 ret = PTR_ERR(vm->scratch_pd);
962 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300963 }
964
Michel Thierry69ab76f2015-07-29 17:23:55 +0100965 if (USES_FULL_48BIT_PPGTT(dev)) {
966 vm->scratch_pdp = alloc_pdp(dev);
967 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100968 ret = PTR_ERR(vm->scratch_pdp);
969 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100970 }
971 }
972
Mika Kuoppala8776f022015-06-30 18:16:40 +0300973 gen8_initialize_pt(vm, vm->scratch_pt);
974 gen8_initialize_pd(vm, vm->scratch_pd);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100975 if (USES_FULL_48BIT_PPGTT(dev))
976 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300977
978 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100979
980free_pd:
981 free_pd(dev, vm->scratch_pd);
982free_pt:
983 free_pt(dev, vm->scratch_pt);
984free_scratch_page:
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100985 cleanup_scratch_page(dev, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100986
987 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300988}
989
Zhiyuan Lv650da342015-08-28 15:41:18 +0800990static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
991{
992 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +0100993 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +0800994 int i;
995
Matthew Aulddf285642016-04-22 12:09:25 +0100996 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +0800997 u64 daddr = px_dma(&ppgtt->pml4);
998
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200999 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1000 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001001
1002 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1003 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1004 } else {
1005 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1006 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1007
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001008 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1009 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001010 }
1011
1012 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1013 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1014 }
1015
1016 I915_WRITE(vgtif_reg(g2v_notify), msg);
1017
1018 return 0;
1019}
1020
Mika Kuoppala8776f022015-06-30 18:16:40 +03001021static void gen8_free_scratch(struct i915_address_space *vm)
1022{
1023 struct drm_device *dev = vm->dev;
1024
Michel Thierry69ab76f2015-07-29 17:23:55 +01001025 if (USES_FULL_48BIT_PPGTT(dev))
1026 free_pdp(dev, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001027 free_pd(dev, vm->scratch_pd);
1028 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001029 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001030}
1031
Michel Thierry762d9932015-07-30 11:05:29 +01001032static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
1033 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001034{
1035 int i;
1036
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001037 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
1038 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001039 continue;
1040
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001041 gen8_free_page_tables(dev, pdp->page_directory[i]);
1042 free_pd(dev, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001043 }
Michel Thierry69876be2015-04-08 12:13:27 +01001044
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001045 free_pdp(dev, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001046}
1047
1048static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1049{
1050 int i;
1051
1052 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1053 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1054 continue;
1055
1056 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
1057 }
1058
1059 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
1060}
1061
1062static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1063{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001064 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001065
Chris Wilsonc0336662016-05-06 15:40:21 +01001066 if (intel_vgpu_active(to_i915(vm->dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001067 gen8_ppgtt_notify_vgt(ppgtt, false);
1068
Michel Thierry762d9932015-07-30 11:05:29 +01001069 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1070 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1071 else
1072 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001073
Mika Kuoppala8776f022015-06-30 18:16:40 +03001074 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001075}
1076
Michel Thierryd7b26332015-04-08 12:13:34 +01001077/**
1078 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001079 * @vm: Master vm structure.
1080 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001081 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001082 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001083 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1084 * caller to free on error.
1085 *
1086 * Allocate the required number of page tables. Extremely similar to
1087 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1088 * the page directory boundary (instead of the page directory pointer). That
1089 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1090 * possible, and likely that the caller will need to use multiple calls of this
1091 * function to achieve the appropriate allocation.
1092 *
1093 * Return: 0 if success; negative error code otherwise.
1094 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001095static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001096 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001097 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001098 uint64_t length,
1099 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001100{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001101 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001102 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001103 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001104
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001105 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001106 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001107 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001108 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001109 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001110 continue;
1111 }
1112
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001113 pt = alloc_pt(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001114 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001115 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001116
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001117 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001118 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001119 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001120 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001121 }
1122
1123 return 0;
1124
1125unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001126 for_each_set_bit(pde, new_pts, I915_PDES)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001127 free_pt(dev, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001128
1129 return -ENOMEM;
1130}
1131
Michel Thierryd7b26332015-04-08 12:13:34 +01001132/**
1133 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001134 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001135 * @pdp: Page directory pointer for this address range.
1136 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001137 * @length: Size of the allocations.
1138 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001139 * caller to free on error.
1140 *
1141 * Allocate the required number of page directories starting at the pde index of
1142 * @start, and ending at the pde index @start + @length. This function will skip
1143 * over already allocated page directories within the range, and only allocate
1144 * new ones, setting the appropriate pointer within the pdp as well as the
1145 * correct position in the bitmap @new_pds.
1146 *
1147 * The function will only allocate the pages within the range for a give page
1148 * directory pointer. In other words, if @start + @length straddles a virtually
1149 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1150 * required by the caller, This is not currently possible, and the BUG in the
1151 * code will prevent it.
1152 *
1153 * Return: 0 if success; negative error code otherwise.
1154 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001155static int
1156gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1157 struct i915_page_directory_pointer *pdp,
1158 uint64_t start,
1159 uint64_t length,
1160 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001161{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001162 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001163 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001164 uint32_t pdpe;
Michel Thierry6ac18502015-07-29 17:23:46 +01001165 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001166
Michel Thierry6ac18502015-07-29 17:23:46 +01001167 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001168
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001169 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001170 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001171 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001172
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001173 pd = alloc_pd(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001174 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001175 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001176
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001177 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001178 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001179 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001180 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001181 }
1182
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001183 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001184
1185unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001186 for_each_set_bit(pdpe, new_pds, pdpes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001187 free_pd(dev, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001188
1189 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001190}
1191
Michel Thierry762d9932015-07-30 11:05:29 +01001192/**
1193 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1194 * @vm: Master vm structure.
1195 * @pml4: Page map level 4 for this address range.
1196 * @start: Starting virtual address to begin allocations.
1197 * @length: Size of the allocations.
1198 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1199 * caller to free on error.
1200 *
1201 * Allocate the required number of page directory pointers. Extremely similar to
1202 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1203 * The main difference is here we are limited by the pml4 boundary (instead of
1204 * the page directory pointer).
1205 *
1206 * Return: 0 if success; negative error code otherwise.
1207 */
1208static int
1209gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1210 struct i915_pml4 *pml4,
1211 uint64_t start,
1212 uint64_t length,
1213 unsigned long *new_pdps)
1214{
1215 struct drm_device *dev = vm->dev;
1216 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001217 uint32_t pml4e;
1218
1219 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1220
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001221 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001222 if (!test_bit(pml4e, pml4->used_pml4es)) {
1223 pdp = alloc_pdp(dev);
1224 if (IS_ERR(pdp))
1225 goto unwind_out;
1226
Michel Thierry69ab76f2015-07-29 17:23:55 +01001227 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001228 pml4->pdps[pml4e] = pdp;
1229 __set_bit(pml4e, new_pdps);
1230 trace_i915_page_directory_pointer_entry_alloc(vm,
1231 pml4e,
1232 start,
1233 GEN8_PML4E_SHIFT);
1234 }
1235 }
1236
1237 return 0;
1238
1239unwind_out:
1240 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1241 free_pdp(dev, pml4->pdps[pml4e]);
1242
1243 return -ENOMEM;
1244}
1245
Michel Thierryd7b26332015-04-08 12:13:34 +01001246static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001247free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001248{
Michel Thierryd7b26332015-04-08 12:13:34 +01001249 kfree(new_pts);
1250 kfree(new_pds);
1251}
1252
1253/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1254 * of these are based on the number of PDPEs in the system.
1255 */
1256static
1257int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001258 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001259 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001260{
Michel Thierryd7b26332015-04-08 12:13:34 +01001261 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001262 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001263
Michał Winiarski3a41a052015-09-03 19:22:18 +02001264 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001265 if (!pds)
1266 return -ENOMEM;
1267
Michał Winiarski3a41a052015-09-03 19:22:18 +02001268 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1269 GFP_TEMPORARY);
1270 if (!pts)
1271 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001272
1273 *new_pds = pds;
1274 *new_pts = pts;
1275
1276 return 0;
1277
1278err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001279 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001280 return -ENOMEM;
1281}
1282
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001283/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1284 * the page table structures, we mark them dirty so that
1285 * context switching/execlist queuing code takes extra steps
1286 * to ensure that tlbs are flushed.
1287 */
1288static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1289{
1290 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1291}
1292
Michel Thierry762d9932015-07-30 11:05:29 +01001293static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1294 struct i915_page_directory_pointer *pdp,
1295 uint64_t start,
1296 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001297{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001298 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001299 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001300 struct drm_device *dev = vm->dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001301 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001302 const uint64_t orig_start = start;
1303 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001304 uint32_t pdpe;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001305 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001306 int ret;
1307
Michel Thierryd7b26332015-04-08 12:13:34 +01001308 /* Wrap is never okay since we can only represent 48b, and we don't
1309 * actually use the other side of the canonical address space.
1310 */
1311 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001312 return -ENODEV;
1313
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001314 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001315 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001316
Michel Thierry6ac18502015-07-29 17:23:46 +01001317 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001318 if (ret)
1319 return ret;
1320
Michel Thierryd7b26332015-04-08 12:13:34 +01001321 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001322 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1323 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001324 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001325 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001326 return ret;
1327 }
1328
1329 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001330 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001331 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001332 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001333 if (ret)
1334 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001335 }
1336
Michel Thierry33c88192015-04-08 12:13:33 +01001337 start = orig_start;
1338 length = orig_length;
1339
Michel Thierryd7b26332015-04-08 12:13:34 +01001340 /* Allocations have completed successfully, so set the bitmaps, and do
1341 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001342 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001343 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001344 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001345 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001346 uint64_t pd_start = start;
1347 uint32_t pde;
1348
Michel Thierryd7b26332015-04-08 12:13:34 +01001349 /* Every pd should be allocated, we just did that above. */
1350 WARN_ON(!pd);
1351
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001352 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001353 /* Same reasoning as pd */
1354 WARN_ON(!pt);
1355 WARN_ON(!pd_len);
1356 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1357
1358 /* Set our used ptes within the page table */
1359 bitmap_set(pt->used_ptes,
1360 gen8_pte_index(pd_start),
1361 gen8_pte_count(pd_start, pd_len));
1362
1363 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001364 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001365
1366 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001367 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1368 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001369 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1370 gen8_pte_index(start),
1371 gen8_pte_count(start, length),
1372 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001373
1374 /* NB: We haven't yet mapped ptes to pages. At this
1375 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001376 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001377
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001378 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001379 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001380 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001381 }
1382
Michał Winiarski3a41a052015-09-03 19:22:18 +02001383 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001384 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001385 return 0;
1386
1387err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001388 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001389 unsigned long temp;
1390
Michał Winiarski3a41a052015-09-03 19:22:18 +02001391 for_each_set_bit(temp, new_page_tables + pdpe *
1392 BITS_TO_LONGS(I915_PDES), I915_PDES)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001393 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001394 }
1395
Michel Thierry6ac18502015-07-29 17:23:46 +01001396 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001397 free_pd(dev, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001398
Michał Winiarski3a41a052015-09-03 19:22:18 +02001399 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001400 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001401 return ret;
1402}
1403
Michel Thierry762d9932015-07-30 11:05:29 +01001404static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1405 struct i915_pml4 *pml4,
1406 uint64_t start,
1407 uint64_t length)
1408{
1409 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001410 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001411 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001412 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001413 int ret = 0;
1414
1415 /* Do the pml4 allocations first, so we don't need to track the newly
1416 * allocated tables below the pdp */
1417 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1418
1419 /* The pagedirectory and pagetable allocations are done in the shared 3
1420 * and 4 level code. Just allocate the pdps.
1421 */
1422 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1423 new_pdps);
1424 if (ret)
1425 return ret;
1426
1427 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1428 "The allocation has spanned more than 512GB. "
1429 "It is highly likely this is incorrect.");
1430
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001431 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001432 WARN_ON(!pdp);
1433
1434 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1435 if (ret)
1436 goto err_out;
1437
1438 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1439 }
1440
1441 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1442 GEN8_PML4ES_PER_PML4);
1443
1444 return 0;
1445
1446err_out:
1447 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1448 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1449
1450 return ret;
1451}
1452
1453static int gen8_alloc_va_range(struct i915_address_space *vm,
1454 uint64_t start, uint64_t length)
1455{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001456 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001457
1458 if (USES_FULL_48BIT_PPGTT(vm->dev))
1459 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1460 else
1461 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1462}
1463
Michel Thierryea91e402015-07-29 17:23:57 +01001464static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1465 uint64_t start, uint64_t length,
1466 gen8_pte_t scratch_pte,
1467 struct seq_file *m)
1468{
1469 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001470 uint32_t pdpe;
1471
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001472 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001473 struct i915_page_table *pt;
1474 uint64_t pd_len = length;
1475 uint64_t pd_start = start;
1476 uint32_t pde;
1477
1478 if (!test_bit(pdpe, pdp->used_pdpes))
1479 continue;
1480
1481 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001482 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001483 uint32_t pte;
1484 gen8_pte_t *pt_vaddr;
1485
1486 if (!test_bit(pde, pd->used_pdes))
1487 continue;
1488
1489 pt_vaddr = kmap_px(pt);
1490 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1491 uint64_t va =
1492 (pdpe << GEN8_PDPE_SHIFT) |
1493 (pde << GEN8_PDE_SHIFT) |
1494 (pte << GEN8_PTE_SHIFT);
1495 int i;
1496 bool found = false;
1497
1498 for (i = 0; i < 4; i++)
1499 if (pt_vaddr[pte + i] != scratch_pte)
1500 found = true;
1501 if (!found)
1502 continue;
1503
1504 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1505 for (i = 0; i < 4; i++) {
1506 if (pt_vaddr[pte + i] != scratch_pte)
1507 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1508 else
1509 seq_puts(m, " SCRATCH ");
1510 }
1511 seq_puts(m, "\n");
1512 }
1513 /* don't use kunmap_px, it could trigger
1514 * an unnecessary flush.
1515 */
1516 kunmap_atomic(pt_vaddr);
1517 }
1518 }
1519}
1520
1521static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1522{
1523 struct i915_address_space *vm = &ppgtt->base;
1524 uint64_t start = ppgtt->base.start;
1525 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001526 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001527 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001528
1529 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1530 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1531 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001532 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001533 struct i915_pml4 *pml4 = &ppgtt->pml4;
1534 struct i915_page_directory_pointer *pdp;
1535
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001536 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001537 if (!test_bit(pml4e, pml4->used_pml4es))
1538 continue;
1539
1540 seq_printf(m, " PML4E #%llu\n", pml4e);
1541 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1542 }
1543 }
1544}
1545
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001546static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1547{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001548 unsigned long *new_page_dirs, *new_page_tables;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001549 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1550 int ret;
1551
1552 /* We allocate temp bitmap for page tables for no gain
1553 * but as this is for init only, lets keep the things simple
1554 */
1555 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1556 if (ret)
1557 return ret;
1558
1559 /* Allocate for all pdps regardless of how the ppgtt
1560 * was defined.
1561 */
1562 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1563 0, 1ULL << 32,
1564 new_page_dirs);
1565 if (!ret)
1566 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1567
Michał Winiarski3a41a052015-09-03 19:22:18 +02001568 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001569
1570 return ret;
1571}
1572
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001573/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001574 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1575 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1576 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1577 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001578 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001579 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001580static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001581{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001582 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001583
Mika Kuoppala8776f022015-06-30 18:16:40 +03001584 ret = gen8_init_scratch(&ppgtt->base);
1585 if (ret)
1586 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001587
Michel Thierryd7b26332015-04-08 12:13:34 +01001588 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001589 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001590 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001591 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001592 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001593 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1594 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001595 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001596
Michel Thierry762d9932015-07-30 11:05:29 +01001597 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1598 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1599 if (ret)
1600 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001601
Michel Thierry69ab76f2015-07-29 17:23:55 +01001602 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1603
Michel Thierry762d9932015-07-30 11:05:29 +01001604 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001605 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001606 } else {
Michel Thierry25f50332015-08-07 17:40:19 +01001607 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001608 if (ret)
1609 goto free_scratch;
1610
1611 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001612 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001613 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1614 0, 0,
1615 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001616
Chris Wilsonc0336662016-05-06 15:40:21 +01001617 if (intel_vgpu_active(to_i915(ppgtt->base.dev))) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001618 ret = gen8_preallocate_top_level_pdps(ppgtt);
1619 if (ret)
1620 goto free_scratch;
1621 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001622 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001623
Chris Wilsonc0336662016-05-06 15:40:21 +01001624 if (intel_vgpu_active(to_i915(ppgtt->base.dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001625 gen8_ppgtt_notify_vgt(ppgtt, true);
1626
Michel Thierryd7b26332015-04-08 12:13:34 +01001627 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001628
1629free_scratch:
1630 gen8_free_scratch(&ppgtt->base);
1631 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001632}
1633
Ben Widawsky87d60b62013-12-06 14:11:29 -08001634static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1635{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001636 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001637 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001638 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001639 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001640 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001641 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001642
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001643 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001644 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001645
Dave Gordon731f74c2016-06-24 19:37:46 +01001646 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001647 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001648 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001649 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001650 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001651 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1652
1653 if (pd_entry != expected)
1654 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1655 pde,
1656 pd_entry,
1657 expected);
1658 seq_printf(m, "\tPDE: %x\n", pd_entry);
1659
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001660 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1661
Michel Thierry07749ef2015-03-16 16:00:54 +00001662 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001663 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001664 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001665 (pte * PAGE_SIZE);
1666 int i;
1667 bool found = false;
1668 for (i = 0; i < 4; i++)
1669 if (pt_vaddr[pte + i] != scratch_pte)
1670 found = true;
1671 if (!found)
1672 continue;
1673
1674 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1675 for (i = 0; i < 4; i++) {
1676 if (pt_vaddr[pte + i] != scratch_pte)
1677 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1678 else
1679 seq_puts(m, " SCRATCH ");
1680 }
1681 seq_puts(m, "\n");
1682 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001683 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001684 }
1685}
1686
Ben Widawsky678d96f2015-03-16 16:00:56 +00001687/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001688static void gen6_write_pde(struct i915_page_directory *pd,
1689 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001690{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001691 /* Caller needs to make sure the write completes if necessary */
1692 struct i915_hw_ppgtt *ppgtt =
1693 container_of(pd, struct i915_hw_ppgtt, pd);
1694 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001695
Mika Kuoppala567047b2015-06-25 18:35:12 +03001696 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001697 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001698
Ben Widawsky678d96f2015-03-16 16:00:56 +00001699 writel(pd_entry, ppgtt->pd_addr + pde);
1700}
Ben Widawsky61973492013-04-08 18:43:54 -07001701
Ben Widawsky678d96f2015-03-16 16:00:56 +00001702/* Write all the page tables found in the ppgtt structure to incrementing page
1703 * directories. */
1704static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001705 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001706 uint32_t start, uint32_t length)
1707{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001708 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001709 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001710 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001711
Dave Gordon731f74c2016-06-24 19:37:46 +01001712 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001713 gen6_write_pde(pd, pde, pt);
1714
1715 /* Make sure write is complete before other code can use this page
1716 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001717 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001718}
1719
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001720static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001721{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001722 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001723
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001724 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001725}
Ben Widawsky61973492013-04-08 18:43:54 -07001726
Ben Widawsky90252e52013-12-06 14:11:12 -08001727static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001728 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001729{
Chris Wilson7e37f882016-08-02 22:50:21 +01001730 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001731 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001732 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001733
Ben Widawsky90252e52013-12-06 14:11:12 -08001734 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001735 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001736 if (ret)
1737 return ret;
1738
John Harrison5fb9de12015-05-29 17:44:07 +01001739 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001740 if (ret)
1741 return ret;
1742
Chris Wilsonb5321f32016-08-02 22:50:18 +01001743 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1744 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1745 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1746 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1747 intel_ring_emit(ring, get_pd_offset(ppgtt));
1748 intel_ring_emit(ring, MI_NOOP);
1749 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001750
1751 return 0;
1752}
1753
Ben Widawsky48a10382013-12-06 14:11:11 -08001754static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001755 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001756{
Chris Wilson7e37f882016-08-02 22:50:21 +01001757 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001758 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001759 int ret;
1760
Ben Widawsky48a10382013-12-06 14:11:11 -08001761 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001762 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001763 if (ret)
1764 return ret;
1765
John Harrison5fb9de12015-05-29 17:44:07 +01001766 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001767 if (ret)
1768 return ret;
1769
Chris Wilsonb5321f32016-08-02 22:50:18 +01001770 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1771 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1772 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1773 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1774 intel_ring_emit(ring, get_pd_offset(ppgtt));
1775 intel_ring_emit(ring, MI_NOOP);
1776 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001777
Ben Widawsky90252e52013-12-06 14:11:12 -08001778 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001779 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001780 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001781 if (ret)
1782 return ret;
1783 }
1784
Ben Widawsky48a10382013-12-06 14:11:11 -08001785 return 0;
1786}
1787
Ben Widawskyeeb94882013-12-06 14:11:10 -08001788static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001789 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001790{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001791 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001792 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001793
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001794 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1795 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001796 return 0;
1797}
1798
Daniel Vetter82460d92014-08-06 20:19:53 +02001799static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001800{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001801 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001802 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301803 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001804
Akash Goel3b3f1652016-10-13 22:44:48 +05301805 for_each_engine(engine, dev_priv, id) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001806 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001807 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001808 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001809 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001810}
1811
Daniel Vetter82460d92014-08-06 20:19:53 +02001812static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001813{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001814 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001815 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001816 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301817 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001818
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819 ecobits = I915_READ(GAC_ECO_BITS);
1820 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1821
1822 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001823 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001824 ecochk |= ECOCHK_PPGTT_WB_HSW;
1825 } else {
1826 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1827 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1828 }
1829 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001830
Akash Goel3b3f1652016-10-13 22:44:48 +05301831 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001832 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001833 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001834 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001835 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001836}
1837
Daniel Vetter82460d92014-08-06 20:19:53 +02001838static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001839{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001840 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001841 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001842
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001843 ecobits = I915_READ(GAC_ECO_BITS);
1844 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1845 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001846
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001847 gab_ctl = I915_READ(GAB_CTL);
1848 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001849
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001850 ecochk = I915_READ(GAM_ECOCHK);
1851 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001852
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001853 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001854}
1855
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001856/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001857static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001858 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001859 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001860{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001861 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001862 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001863 unsigned first_entry = start >> PAGE_SHIFT;
1864 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001865 unsigned act_pt = first_entry / GEN6_PTES;
1866 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001867 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001868
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001869 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001870 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001871
Daniel Vetter7bddb012012-02-09 17:15:47 +01001872 while (num_entries) {
1873 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001874 if (last_pte > GEN6_PTES)
1875 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001876
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001877 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001878
1879 for (i = first_pte; i < last_pte; i++)
1880 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001881
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001882 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001883
Daniel Vetter7bddb012012-02-09 17:15:47 +01001884 num_entries -= last_pte - first_pte;
1885 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001886 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001887 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001888}
1889
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001890static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001891 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001892 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301893 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001894{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001895 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001896 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001897 unsigned act_pt = first_entry / GEN6_PTES;
1898 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001899 gen6_pte_t *pt_vaddr = NULL;
1900 struct sgt_iter sgt_iter;
1901 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001902
Dave Gordon85d12252016-05-20 11:54:06 +01001903 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001904 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001905 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001906
Chris Wilsoncc797142013-12-31 15:50:30 +00001907 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001908 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301909
Michel Thierry07749ef2015-03-16 16:00:54 +00001910 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001911 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001912 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001913 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001914 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001915 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001916 }
Dave Gordon85d12252016-05-20 11:54:06 +01001917
Chris Wilsoncc797142013-12-31 15:50:30 +00001918 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001919 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001920}
1921
Ben Widawsky678d96f2015-03-16 16:00:56 +00001922static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001923 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001924{
Michel Thierry4933d512015-03-24 15:46:22 +00001925 DECLARE_BITMAP(new_page_tables, I915_PDES);
1926 struct drm_device *dev = vm->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001927 struct drm_i915_private *dev_priv = to_i915(dev);
1928 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001929 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001930 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001931 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001932 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001933 int ret;
1934
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001935 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1936 return -ENODEV;
1937
1938 start = start_save = start_in;
1939 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001940
1941 bitmap_zero(new_page_tables, I915_PDES);
1942
1943 /* The allocation is done in two stages so that we can bail out with
1944 * minimal amount of pain. The first stage finds new page tables that
1945 * need allocation. The second stage marks use ptes within the page
1946 * tables.
1947 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001948 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001949 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001950 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1951 continue;
1952 }
1953
1954 /* We've already allocated a page table */
1955 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1956
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001957 pt = alloc_pt(dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001958 if (IS_ERR(pt)) {
1959 ret = PTR_ERR(pt);
1960 goto unwind_out;
1961 }
1962
1963 gen6_initialize_pt(vm, pt);
1964
1965 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001966 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001967 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001968 }
1969
1970 start = start_save;
1971 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001972
Dave Gordon731f74c2016-06-24 19:37:46 +01001973 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001974 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1975
1976 bitmap_zero(tmp_bitmap, GEN6_PTES);
1977 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1978 gen6_pte_count(start, length));
1979
Mika Kuoppala966082c2015-06-25 18:35:19 +03001980 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001981 gen6_write_pde(&ppgtt->pd, pde, pt);
1982
Michel Thierry72744cb2015-03-24 15:46:23 +00001983 trace_i915_page_table_entry_map(vm, pde, pt,
1984 gen6_pte_index(start),
1985 gen6_pte_count(start, length),
1986 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001987 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001988 GEN6_PTES);
1989 }
1990
Michel Thierry4933d512015-03-24 15:46:22 +00001991 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1992
1993 /* Make sure write is complete before other code can use this page
1994 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001995 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001996
Ben Widawsky563222a2015-03-19 12:53:28 +00001997 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001998 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001999
2000unwind_out:
2001 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002002 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002003
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002004 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03002005 free_pt(vm->dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002006 }
2007
2008 mark_tlbs_dirty(ppgtt);
2009 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002010}
2011
Mika Kuoppala8776f022015-06-30 18:16:40 +03002012static int gen6_init_scratch(struct i915_address_space *vm)
2013{
2014 struct drm_device *dev = vm->dev;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002015 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002016
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002017 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002018 if (ret)
2019 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002020
2021 vm->scratch_pt = alloc_pt(dev);
2022 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002023 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002024 return PTR_ERR(vm->scratch_pt);
2025 }
2026
2027 gen6_initialize_pt(vm, vm->scratch_pt);
2028
2029 return 0;
2030}
2031
2032static void gen6_free_scratch(struct i915_address_space *vm)
2033{
2034 struct drm_device *dev = vm->dev;
2035
2036 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002037 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002038}
2039
Daniel Vetter061dd492015-04-14 17:35:13 +02002040static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002041{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002042 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002043 struct i915_page_directory *pd = &ppgtt->pd;
2044 struct drm_device *dev = vm->dev;
Michel Thierry09942c62015-04-08 12:13:30 +01002045 struct i915_page_table *pt;
2046 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002047
Daniel Vetter061dd492015-04-14 17:35:13 +02002048 drm_mm_remove_node(&ppgtt->node);
2049
Dave Gordon731f74c2016-06-24 19:37:46 +01002050 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002051 if (pt != vm->scratch_pt)
Dave Gordon731f74c2016-06-24 19:37:46 +01002052 free_pt(dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002053
Mika Kuoppala8776f022015-06-30 18:16:40 +03002054 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002055}
2056
Ben Widawskyb1465202014-02-19 22:05:49 -08002057static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002058{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002059 struct i915_address_space *vm = &ppgtt->base;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002060 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002061 struct drm_i915_private *dev_priv = to_i915(dev);
2062 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002063 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002064 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002065
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002066 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2067 * allocator works in address space sizes, so it's multiplied by page
2068 * size. We allocate at the top of the GTT to avoid fragmentation.
2069 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002070 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002071
Mika Kuoppala8776f022015-06-30 18:16:40 +03002072 ret = gen6_init_scratch(vm);
2073 if (ret)
2074 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002075
Ben Widawskye3cc1992013-12-06 14:11:08 -08002076alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002077 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002078 &ppgtt->node, GEN6_PD_SIZE,
2079 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002080 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002081 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002082 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002083 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002084 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002085 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002086 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002087 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002088 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002089 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002090
2091 retried = true;
2092 goto alloc;
2093 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002094
Ben Widawskyc8c26622015-01-22 17:01:25 +00002095 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002096 goto err_out;
2097
Ben Widawskyc8c26622015-01-22 17:01:25 +00002098
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002099 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002100 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002101
Ben Widawskyc8c26622015-01-22 17:01:25 +00002102 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002103
2104err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002105 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002106 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002107}
2108
Ben Widawskyb1465202014-02-19 22:05:49 -08002109static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2110{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002111 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002112}
2113
Michel Thierry4933d512015-03-24 15:46:22 +00002114static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2115 uint64_t start, uint64_t length)
2116{
Michel Thierryec565b32015-04-08 12:13:23 +01002117 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002118 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002119
Dave Gordon731f74c2016-06-24 19:37:46 +01002120 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002121 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002122}
2123
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002124static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002125{
2126 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002127 struct drm_i915_private *dev_priv = to_i915(dev);
2128 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002129 int ret;
2130
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002131 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002132 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002133 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002134 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002135 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002136 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002137 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002138 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002139 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002140
2141 ret = gen6_ppgtt_alloc(ppgtt);
2142 if (ret)
2143 return ret;
2144
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002145 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002146 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2147 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002148 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2149 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002150 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002151 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002152 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002153 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002154
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002155 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002156 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002157
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002158 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002159 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002160
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002161 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002162
Ben Widawsky678d96f2015-03-16 16:00:56 +00002163 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2164
Thierry Reding440fd522015-01-23 09:05:06 +01002165 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002166 ppgtt->node.size >> 20,
2167 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002168
Daniel Vetterfa76da32014-08-06 20:19:54 +02002169 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002170 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002171
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002172 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002173}
2174
Chris Wilson2bfa9962016-08-04 07:52:25 +01002175static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2176 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002177{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002178 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002179
Chris Wilson2bfa9962016-08-04 07:52:25 +01002180 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002181 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002182 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002183 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002184}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002185
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002186static void i915_address_space_init(struct i915_address_space *vm,
2187 struct drm_i915_private *dev_priv)
2188{
2189 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002190 INIT_LIST_HEAD(&vm->active_list);
2191 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002192 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002193 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2194}
2195
Tim Gored5165eb2016-02-04 11:49:34 +00002196static void gtt_write_workarounds(struct drm_device *dev)
2197{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002198 struct drm_i915_private *dev_priv = to_i915(dev);
Tim Gored5165eb2016-02-04 11:49:34 +00002199
2200 /* This function is for gtt related workarounds. This function is
2201 * called on driver load and after a GPU reset, so you can place
2202 * workarounds here even if they get overwritten by GPU reset.
2203 */
2204 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002205 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002206 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002207 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002208 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002209 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002210 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002211 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002212 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2213}
2214
Chris Wilson2bfa9962016-08-04 07:52:25 +01002215static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2216 struct drm_i915_private *dev_priv,
2217 struct drm_i915_file_private *file_priv)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002218{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002219 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002220
Chris Wilson2bfa9962016-08-04 07:52:25 +01002221 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002222 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002223 kref_init(&ppgtt->ref);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002224 i915_address_space_init(&ppgtt->base, dev_priv);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002225 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002226 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002227
2228 return ret;
2229}
2230
Daniel Vetter82460d92014-08-06 20:19:53 +02002231int i915_ppgtt_init_hw(struct drm_device *dev)
2232{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002233 struct drm_i915_private *dev_priv = to_i915(dev);
2234
Tim Gored5165eb2016-02-04 11:49:34 +00002235 gtt_write_workarounds(dev);
2236
Thomas Daniel671b50132014-08-20 16:24:50 +01002237 /* In the case of execlists, PPGTT is enabled by the context descriptor
2238 * and the PDPs are contained within the context itself. We don't
2239 * need to do anything here. */
2240 if (i915.enable_execlists)
2241 return 0;
2242
Daniel Vetter82460d92014-08-06 20:19:53 +02002243 if (!USES_PPGTT(dev))
2244 return 0;
2245
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002246 if (IS_GEN6(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002247 gen6_ppgtt_enable(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002248 else if (IS_GEN7(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002249 gen7_ppgtt_enable(dev);
2250 else if (INTEL_INFO(dev)->gen >= 8)
2251 gen8_ppgtt_enable(dev);
2252 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01002253 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02002254
John Harrison4ad2fd82015-06-18 13:11:20 +01002255 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002256}
John Harrison4ad2fd82015-06-18 13:11:20 +01002257
Daniel Vetter4d884702014-08-06 15:04:47 +02002258struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002259i915_ppgtt_create(struct drm_i915_private *dev_priv,
2260 struct drm_i915_file_private *fpriv)
Daniel Vetter4d884702014-08-06 15:04:47 +02002261{
2262 struct i915_hw_ppgtt *ppgtt;
2263 int ret;
2264
2265 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2266 if (!ppgtt)
2267 return ERR_PTR(-ENOMEM);
2268
Chris Wilson2bfa9962016-08-04 07:52:25 +01002269 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv);
Daniel Vetter4d884702014-08-06 15:04:47 +02002270 if (ret) {
2271 kfree(ppgtt);
2272 return ERR_PTR(ret);
2273 }
2274
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002275 trace_i915_ppgtt_create(&ppgtt->base);
2276
Daniel Vetter4d884702014-08-06 15:04:47 +02002277 return ppgtt;
2278}
2279
Daniel Vetteree960be2014-08-06 15:04:45 +02002280void i915_ppgtt_release(struct kref *kref)
2281{
2282 struct i915_hw_ppgtt *ppgtt =
2283 container_of(kref, struct i915_hw_ppgtt, ref);
2284
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002285 trace_i915_ppgtt_release(&ppgtt->base);
2286
Chris Wilson50e046b2016-08-04 07:52:46 +01002287 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002288 WARN_ON(!list_empty(&ppgtt->base.active_list));
2289 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002290 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002291
Daniel Vetter19dd1202014-08-06 15:04:55 +02002292 list_del(&ppgtt->base.global_link);
2293 drm_mm_takedown(&ppgtt->base.mm);
2294
Daniel Vetteree960be2014-08-06 15:04:45 +02002295 ppgtt->base.cleanup(&ppgtt->base);
2296 kfree(ppgtt);
2297}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002298
Ben Widawskya81cc002013-01-18 12:30:31 -08002299/* Certain Gen5 chipsets require require idling the GPU before
2300 * unmapping anything from the GTT when VT-d is enabled.
2301 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002302static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002303{
2304#ifdef CONFIG_INTEL_IOMMU
2305 /* Query intel_iommu to see if we need the workaround. Presumably that
2306 * was loaded first.
2307 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002308 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002309 return true;
2310#endif
2311 return false;
2312}
2313
Chris Wilsondc979972016-05-10 14:10:04 +01002314void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002315{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002316 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302317 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002318
Chris Wilsondc979972016-05-10 14:10:04 +01002319 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002320 return;
2321
Akash Goel3b3f1652016-10-13 22:44:48 +05302322 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002323 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002324 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002325 if (fault_reg & RING_FAULT_VALID) {
2326 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002327 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002328 "\tAddress space: %s\n"
2329 "\tSource ID: %d\n"
2330 "\tType: %d\n",
2331 fault_reg & PAGE_MASK,
2332 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2333 RING_FAULT_SRCID(fault_reg),
2334 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002335 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002336 fault_reg & ~RING_FAULT_VALID);
2337 }
2338 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302339
2340 /* Engine specific init may not have been done till this point. */
2341 if (dev_priv->engine[RCS])
2342 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002343}
2344
Chris Wilson91e56492014-09-25 10:13:12 +01002345static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2346{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002347 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002348 intel_gtt_chipset_flush();
2349 } else {
2350 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2351 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2352 }
2353}
2354
Ben Widawsky828c7902013-10-16 09:21:30 -07002355void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2356{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002357 struct drm_i915_private *dev_priv = to_i915(dev);
2358 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002359
2360 /* Don't bother messing with faults pre GEN6 as we have little
2361 * documentation supporting that it's a good idea.
2362 */
2363 if (INTEL_INFO(dev)->gen < 6)
2364 return;
2365
Chris Wilsondc979972016-05-10 14:10:04 +01002366 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002367
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002368 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002369
2370 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002371}
2372
Daniel Vetter74163902012-02-15 23:50:21 +01002373int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002374{
Chris Wilson9da3da62012-06-01 15:20:22 +01002375 if (!dma_map_sg(&obj->base.dev->pdev->dev,
2376 obj->pages->sgl, obj->pages->nents,
2377 PCI_DMA_BIDIRECTIONAL))
2378 return -ENOSPC;
2379
2380 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002381}
2382
Daniel Vetter2c642b02015-04-14 17:35:26 +02002383static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002384{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002385 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002386}
2387
Chris Wilsond6473f52016-06-10 14:22:59 +05302388static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2389 dma_addr_t addr,
2390 uint64_t offset,
2391 enum i915_cache_level level,
2392 u32 unused)
2393{
2394 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2395 gen8_pte_t __iomem *pte =
2396 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2397 (offset >> PAGE_SHIFT);
2398 int rpm_atomic_seq;
2399
2400 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2401
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002402 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302403
2404 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2405 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2406
2407 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2408}
2409
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002410static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2411 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002412 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302413 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002414{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002415 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002416 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002417 struct sgt_iter sgt_iter;
2418 gen8_pte_t __iomem *gtt_entries;
2419 gen8_pte_t gtt_entry;
2420 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002421 int rpm_atomic_seq;
Dave Gordon85d12252016-05-20 11:54:06 +01002422 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002423
2424 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002425
Dave Gordon85d12252016-05-20 11:54:06 +01002426 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2427
2428 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002429 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002430 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002431 }
2432
2433 /*
2434 * XXX: This serves as a posting read to make sure that the PTE has
2435 * actually been updated. There is some concern that even though
2436 * registers and PTEs are within the same BAR that they are potentially
2437 * of NUMA access patterns. Therefore, even with the way we assume
2438 * hardware should work, we must keep this posting read for paranoia.
2439 */
2440 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002441 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002442
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002443 /* This next bit makes the above posting read even more important. We
2444 * want to flush the TLBs only after we're certain all the PTE updates
2445 * have finished.
2446 */
2447 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2448 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Imre Deakbe694592015-12-15 20:10:38 +02002449
2450 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002451}
2452
Chris Wilsonc1403302015-11-18 15:19:39 +00002453struct insert_entries {
2454 struct i915_address_space *vm;
2455 struct sg_table *st;
2456 uint64_t start;
2457 enum i915_cache_level level;
2458 u32 flags;
2459};
2460
2461static int gen8_ggtt_insert_entries__cb(void *_arg)
2462{
2463 struct insert_entries *arg = _arg;
2464 gen8_ggtt_insert_entries(arg->vm, arg->st,
2465 arg->start, arg->level, arg->flags);
2466 return 0;
2467}
2468
2469static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2470 struct sg_table *st,
2471 uint64_t start,
2472 enum i915_cache_level level,
2473 u32 flags)
2474{
2475 struct insert_entries arg = { vm, st, start, level, flags };
2476 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2477}
2478
Chris Wilsond6473f52016-06-10 14:22:59 +05302479static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2480 dma_addr_t addr,
2481 uint64_t offset,
2482 enum i915_cache_level level,
2483 u32 flags)
2484{
2485 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2486 gen6_pte_t __iomem *pte =
2487 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2488 (offset >> PAGE_SHIFT);
2489 int rpm_atomic_seq;
2490
2491 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2492
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002493 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302494
2495 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2496 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2497
2498 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2499}
2500
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002501/*
2502 * Binds an object into the global gtt with the specified cache level. The object
2503 * will be accessible to the GPU via commands whose operands reference offsets
2504 * within the global GTT as well as accessible by the GPU through the GMADR
2505 * mapped BAR (dev_priv->mm.gtt->gtt).
2506 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002507static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002508 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002509 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302510 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002511{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002512 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002513 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002514 struct sgt_iter sgt_iter;
2515 gen6_pte_t __iomem *gtt_entries;
2516 gen6_pte_t gtt_entry;
2517 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002518 int rpm_atomic_seq;
Dave Gordon85d12252016-05-20 11:54:06 +01002519 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002520
2521 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002522
Dave Gordon85d12252016-05-20 11:54:06 +01002523 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2524
2525 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002526 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002527 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002528 }
2529
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002530 /* XXX: This serves as a posting read to make sure that the PTE has
2531 * actually been updated. There is some concern that even though
2532 * registers and PTEs are within the same BAR that they are potentially
2533 * of NUMA access patterns. Therefore, even with the way we assume
2534 * hardware should work, we must keep this posting read for paranoia.
2535 */
Dave Gordon85d12252016-05-20 11:54:06 +01002536 if (i != 0)
2537 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002538
2539 /* This next bit makes the above posting read even more important. We
2540 * want to flush the TLBs only after we're certain all the PTE updates
2541 * have finished.
2542 */
2543 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2544 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Imre Deakbe694592015-12-15 20:10:38 +02002545
2546 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002547}
2548
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002549static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002550 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002551{
2552}
2553
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002554static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002555 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002556{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002557 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002558 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002559 unsigned first_entry = start >> PAGE_SHIFT;
2560 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002561 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002562 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2563 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002564 int i;
Imre Deakbe694592015-12-15 20:10:38 +02002565 int rpm_atomic_seq;
2566
2567 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002568
2569 if (WARN(num_entries > max_entries,
2570 "First entry = %d; Num entries = %d (max=%d)\n",
2571 first_entry, num_entries, max_entries))
2572 num_entries = max_entries;
2573
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002574 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002575 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002576 for (i = 0; i < num_entries; i++)
2577 gen8_set_pte(&gtt_base[i], scratch_pte);
2578 readl(gtt_base);
Imre Deakbe694592015-12-15 20:10:38 +02002579
2580 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002581}
2582
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002583static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002584 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002585 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002586{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002587 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002588 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002589 unsigned first_entry = start >> PAGE_SHIFT;
2590 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002591 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002592 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2593 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002594 int i;
Imre Deakbe694592015-12-15 20:10:38 +02002595 int rpm_atomic_seq;
2596
2597 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002598
2599 if (WARN(num_entries > max_entries,
2600 "First entry = %d; Num entries = %d (max=%d)\n",
2601 first_entry, num_entries, max_entries))
2602 num_entries = max_entries;
2603
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002604 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002605 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002606
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002607 for (i = 0; i < num_entries; i++)
2608 iowrite32(scratch_pte, &gtt_base[i]);
2609 readl(gtt_base);
Imre Deakbe694592015-12-15 20:10:38 +02002610
2611 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002612}
2613
Chris Wilsond6473f52016-06-10 14:22:59 +05302614static void i915_ggtt_insert_page(struct i915_address_space *vm,
2615 dma_addr_t addr,
2616 uint64_t offset,
2617 enum i915_cache_level cache_level,
2618 u32 unused)
2619{
2620 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2621 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2622 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2623 int rpm_atomic_seq;
2624
2625 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2626
2627 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2628
2629 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2630}
2631
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002632static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2633 struct sg_table *pages,
2634 uint64_t start,
2635 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002636{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002637 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002638 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2639 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Imre Deakbe694592015-12-15 20:10:38 +02002640 int rpm_atomic_seq;
2641
2642 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002643
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002644 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002645
Imre Deakbe694592015-12-15 20:10:38 +02002646 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2647
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002648}
2649
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002650static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002651 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002652 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002653{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002654 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Ben Widawsky782f1492014-02-20 11:50:33 -08002655 unsigned first_entry = start >> PAGE_SHIFT;
2656 unsigned num_entries = length >> PAGE_SHIFT;
Imre Deakbe694592015-12-15 20:10:38 +02002657 int rpm_atomic_seq;
2658
2659 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2660
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002661 intel_gtt_clear_range(first_entry, num_entries);
Imre Deakbe694592015-12-15 20:10:38 +02002662
2663 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002664}
2665
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002666static int ggtt_bind_vma(struct i915_vma *vma,
2667 enum i915_cache_level cache_level,
2668 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002669{
Daniel Vetter0a878712015-10-15 14:23:01 +02002670 struct drm_i915_gem_object *obj = vma->obj;
2671 u32 pte_flags = 0;
2672 int ret;
2673
2674 ret = i915_get_ggtt_vma_pages(vma);
2675 if (ret)
2676 return ret;
2677
2678 /* Currently applicable only to VLV */
2679 if (obj->gt_ro)
2680 pte_flags |= PTE_READ_ONLY;
2681
Chris Wilson247177d2016-08-15 10:48:47 +01002682 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002683 cache_level, pte_flags);
2684
2685 /*
2686 * Without aliasing PPGTT there's no difference between
2687 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2688 * upgrade to both bound if we bind either to avoid double-binding.
2689 */
Chris Wilson3272db52016-08-04 16:32:32 +01002690 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002691
2692 return 0;
2693}
2694
2695static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2696 enum i915_cache_level cache_level,
2697 u32 flags)
2698{
Chris Wilson321d1782015-11-20 10:27:18 +00002699 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002700 int ret;
2701
2702 ret = i915_get_ggtt_vma_pages(vma);
2703 if (ret)
2704 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002705
Akash Goel24f3a8c2014-06-17 10:59:42 +05302706 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002707 pte_flags = 0;
2708 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002709 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302710
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002711
Chris Wilson3272db52016-08-04 16:32:32 +01002712 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson321d1782015-11-20 10:27:18 +00002713 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002714 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002715 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002716 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002717
Chris Wilson3272db52016-08-04 16:32:32 +01002718 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson321d1782015-11-20 10:27:18 +00002719 struct i915_hw_ppgtt *appgtt =
2720 to_i915(vma->vm->dev)->mm.aliasing_ppgtt;
2721 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002722 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002723 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002724 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002725
2726 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002727}
2728
2729static void ggtt_unbind_vma(struct i915_vma *vma)
2730{
Chris Wilsonde180032016-08-04 16:32:29 +01002731 struct i915_hw_ppgtt *appgtt = to_i915(vma->vm->dev)->mm.aliasing_ppgtt;
2732 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002733
Chris Wilson3272db52016-08-04 16:32:32 +01002734 if (vma->flags & I915_VMA_GLOBAL_BIND)
Ben Widawsky782f1492014-02-20 11:50:33 -08002735 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002736 vma->node.start, size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002737
Chris Wilson3272db52016-08-04 16:32:32 +01002738 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002739 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002740 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002741}
2742
2743void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2744{
David Weinehall52a05c32016-08-22 13:32:44 +03002745 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2746 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002747 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002748
Chris Wilson307dc252016-08-05 10:14:12 +01002749 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002750 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002751 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2752 /* Wait a bit, in hopes it avoids the hang */
2753 udelay(10);
2754 }
2755 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002756
David Weinehall52a05c32016-08-22 13:32:44 +03002757 dma_unmap_sg(kdev, obj->pages->sgl, obj->pages->nents,
Imre Deak5ec5b512015-07-08 19:18:59 +03002758 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002759}
Daniel Vetter644ec022012-03-26 09:45:40 +02002760
Chris Wilson42d6ab42012-07-26 11:49:32 +01002761static void i915_gtt_color_adjust(struct drm_mm_node *node,
2762 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002763 u64 *start,
2764 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002765{
2766 if (node->color != color)
2767 *start += 4096;
2768
Chris Wilson2a1d7752016-07-26 12:01:51 +01002769 node = list_first_entry_or_null(&node->node_list,
2770 struct drm_mm_node,
2771 node_list);
2772 if (node && node->allocated && node->color != color)
2773 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002774}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002775
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002776int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002777{
Ben Widawskye78891c2013-01-25 16:41:04 -08002778 /* Let GEM Manage all of the aperture.
2779 *
2780 * However, leave one page at the end still bound to the scratch page.
2781 * There are a number of places where the hardware apparently prefetches
2782 * past the end of the object, and we've seen multiple hangs with the
2783 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2784 * aperture. One page should be enough to keep any prefetching inside
2785 * of the aperture.
2786 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002787 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002788 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002789 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002790 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002791 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002792
Zhi Wangb02d22a2016-06-16 08:06:59 -04002793 ret = intel_vgt_balloon(dev_priv);
2794 if (ret)
2795 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002796
Chris Wilson95374d72016-10-12 10:05:20 +01002797 /* Reserve a mappable slot for our lockless error capture */
2798 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2799 &ggtt->error_capture,
2800 4096, 0, -1,
2801 0, ggtt->mappable_end,
2802 0, 0);
2803 if (ret)
2804 return ret;
2805
Chris Wilsoned2f3452012-11-15 11:32:19 +00002806 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002807 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002808 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2809 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002810 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002811 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002812 }
2813
2814 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002815 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002816 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002817
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002818 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002819 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002820 if (!ppgtt) {
2821 ret = -ENOMEM;
2822 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002823 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002824
Chris Wilson95374d72016-10-12 10:05:20 +01002825 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2826 if (ret)
2827 goto err_ppgtt;
2828
2829 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002830 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2831 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002832 if (ret)
2833 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002834 }
2835
2836 ppgtt->base.clear_range(&ppgtt->base,
2837 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002838 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002839
Daniel Vetterfa76da32014-08-06 20:19:54 +02002840 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002841 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2842 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002843 }
2844
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002845 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002846
2847err_ppgtt_cleanup:
2848 ppgtt->base.cleanup(&ppgtt->base);
2849err_ppgtt:
2850 kfree(ppgtt);
2851err:
2852 drm_mm_remove_node(&ggtt->error_capture);
2853 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002854}
2855
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002856/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002857 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002858 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002859 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002860void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002861{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002862 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002863
Daniel Vetter70e32542014-08-06 15:04:57 +02002864 if (dev_priv->mm.aliasing_ppgtt) {
2865 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002866 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002867 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002868 }
2869
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002870 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002871
Chris Wilson95374d72016-10-12 10:05:20 +01002872 if (drm_mm_node_allocated(&ggtt->error_capture))
2873 drm_mm_remove_node(&ggtt->error_capture);
2874
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002875 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002876 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002877
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002878 drm_mm_takedown(&ggtt->base.mm);
2879 list_del(&ggtt->base.global_link);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002880 }
2881
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002882 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002883
2884 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002885 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002886}
Daniel Vetter70e32542014-08-06 15:04:57 +02002887
Daniel Vetter2c642b02015-04-14 17:35:26 +02002888static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002889{
2890 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2891 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2892 return snb_gmch_ctl << 20;
2893}
2894
Daniel Vetter2c642b02015-04-14 17:35:26 +02002895static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002896{
2897 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2898 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2899 if (bdw_gmch_ctl)
2900 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002901
2902#ifdef CONFIG_X86_32
2903 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2904 if (bdw_gmch_ctl > 4)
2905 bdw_gmch_ctl = 4;
2906#endif
2907
Ben Widawsky9459d252013-11-03 16:53:55 -08002908 return bdw_gmch_ctl << 20;
2909}
2910
Daniel Vetter2c642b02015-04-14 17:35:26 +02002911static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002912{
2913 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2914 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2915
2916 if (gmch_ctrl)
2917 return 1 << (20 + gmch_ctrl);
2918
2919 return 0;
2920}
2921
Daniel Vetter2c642b02015-04-14 17:35:26 +02002922static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002923{
2924 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2925 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2926 return snb_gmch_ctl << 25; /* 32 MB units */
2927}
2928
Daniel Vetter2c642b02015-04-14 17:35:26 +02002929static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002930{
2931 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2932 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2933 return bdw_gmch_ctl << 25; /* 32 MB units */
2934}
2935
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002936static size_t chv_get_stolen_size(u16 gmch_ctrl)
2937{
2938 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2939 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2940
2941 /*
2942 * 0x0 to 0x10: 32MB increments starting at 0MB
2943 * 0x11 to 0x16: 4MB increments starting at 8MB
2944 * 0x17 to 0x1d: 4MB increments start at 36MB
2945 */
2946 if (gmch_ctrl < 0x11)
2947 return gmch_ctrl << 25;
2948 else if (gmch_ctrl < 0x17)
2949 return (gmch_ctrl - 0x11 + 2) << 22;
2950 else
2951 return (gmch_ctrl - 0x17 + 9) << 22;
2952}
2953
Damien Lespiau66375012014-01-09 18:02:46 +00002954static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2955{
2956 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2957 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2958
2959 if (gen9_gmch_ctl < 0xf0)
2960 return gen9_gmch_ctl << 25; /* 32 MB units */
2961 else
2962 /* 4MB increments starting at 0xf0 for 4MB */
2963 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2964}
2965
Chris Wilson34c998b2016-08-04 07:52:24 +01002966static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002967{
Chris Wilson34c998b2016-08-04 07:52:24 +01002968 struct pci_dev *pdev = ggtt->base.dev->pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002969 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002970 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002971
2972 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002973 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002974
Imre Deak2a073f892015-03-27 13:07:33 +02002975 /*
2976 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2977 * dropped. For WC mappings in general we have 64 byte burst writes
2978 * when the WC buffer is flushed, so we can't use it, but have to
2979 * resort to an uncached mapping. The WC issue is easily caught by the
2980 * readback check when writing GTT PTE entries.
2981 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002982 if (IS_BROXTON(to_i915(ggtt->base.dev)))
Chris Wilson34c998b2016-08-04 07:52:24 +01002983 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002984 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002985 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002986 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002987 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002988 return -ENOMEM;
2989 }
2990
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002991 ret = setup_scratch_page(ggtt->base.dev,
2992 &ggtt->base.scratch_page,
2993 GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002994 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002995 DRM_ERROR("Scratch setup failed\n");
2996 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002997 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002998 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002999 }
3000
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03003001 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08003002}
3003
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003004/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3005 * bits. When using advanced contexts each context stores its own PAT, but
3006 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003007static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003008{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003009 uint64_t pat;
3010
3011 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3012 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3013 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3014 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3015 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3016 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3017 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3018 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3019
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03003020 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08003021 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3022 * so RTL will always use the value corresponding to
3023 * pat_sel = 000".
3024 * So let's disable cache for GGTT to avoid screen corruptions.
3025 * MOCS still can be used though.
3026 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3027 * before this patch, i.e. the same uncached + snooping access
3028 * like on gen6/7 seems to be in effect.
3029 * - So this just fixes blitter/render access. Again it looks
3030 * like it's not just uncached access, but uncached + snooping.
3031 * So we can still hold onto all our assumptions wrt cpu
3032 * clflushing on LLC machines.
3033 */
3034 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3035
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003036 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3037 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003038 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3039 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003040}
3041
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003042static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3043{
3044 uint64_t pat;
3045
3046 /*
3047 * Map WB on BDW to snooped on CHV.
3048 *
3049 * Only the snoop bit has meaning for CHV, the rest is
3050 * ignored.
3051 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003052 * The hardware will never snoop for certain types of accesses:
3053 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3054 * - PPGTT page tables
3055 * - some other special cycles
3056 *
3057 * As with BDW, we also need to consider the following for GT accesses:
3058 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3059 * so RTL will always use the value corresponding to
3060 * pat_sel = 000".
3061 * Which means we must set the snoop bit in PAT entry 0
3062 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003063 */
3064 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3065 GEN8_PPAT(1, 0) |
3066 GEN8_PPAT(2, 0) |
3067 GEN8_PPAT(3, 0) |
3068 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3069 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3070 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3071 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3072
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003073 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3074 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003075}
3076
Chris Wilson34c998b2016-08-04 07:52:24 +01003077static void gen6_gmch_remove(struct i915_address_space *vm)
3078{
3079 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3080
3081 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01003082 cleanup_scratch_page(vm->dev, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003083}
3084
Joonas Lahtinend507d732016-03-18 10:42:58 +02003085static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003086{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003087 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3088 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003089 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003090 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003091
3092 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003093 ggtt->mappable_base = pci_resource_start(pdev, 2);
3094 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003095
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003096 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3097 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003098
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003099 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003100
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003101 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003102 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003103 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003104 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003105 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003106 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003107 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003108 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003109 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003110 }
Ben Widawsky63340132013-11-04 19:32:22 -08003111
Chris Wilson34c998b2016-08-04 07:52:24 +01003112 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003113
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003114 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003115 chv_setup_private_ppat(dev_priv);
3116 else
3117 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003118
Chris Wilson34c998b2016-08-04 07:52:24 +01003119 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003120 ggtt->base.bind_vma = ggtt_bind_vma;
3121 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303122 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003123 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003124 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003125 ggtt->base.clear_range = gen8_ggtt_clear_range;
3126
3127 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3128 if (IS_CHERRYVIEW(dev_priv))
3129 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3130
Chris Wilson34c998b2016-08-04 07:52:24 +01003131 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003132}
3133
Joonas Lahtinend507d732016-03-18 10:42:58 +02003134static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003135{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003136 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3137 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003138 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003139 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003140
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003141 ggtt->mappable_base = pci_resource_start(pdev, 2);
3142 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003143
Ben Widawskybaa09f52013-01-24 13:49:57 -08003144 /* 64/512MB is the current min/max we actually know of, but this is just
3145 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003146 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003147 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003148 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003149 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003150 }
3151
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003152 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3153 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3154 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003155
Joonas Lahtinend507d732016-03-18 10:42:58 +02003156 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003157
Chris Wilson34c998b2016-08-04 07:52:24 +01003158 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3159 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003160
Joonas Lahtinend507d732016-03-18 10:42:58 +02003161 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303162 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003163 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3164 ggtt->base.bind_vma = ggtt_bind_vma;
3165 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003166 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003167
Chris Wilson34c998b2016-08-04 07:52:24 +01003168 if (HAS_EDRAM(dev_priv))
3169 ggtt->base.pte_encode = iris_pte_encode;
3170 else if (IS_HASWELL(dev_priv))
3171 ggtt->base.pte_encode = hsw_pte_encode;
3172 else if (IS_VALLEYVIEW(dev_priv))
3173 ggtt->base.pte_encode = byt_pte_encode;
3174 else if (INTEL_GEN(dev_priv) >= 7)
3175 ggtt->base.pte_encode = ivb_pte_encode;
3176 else
3177 ggtt->base.pte_encode = snb_pte_encode;
3178
3179 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003180}
3181
Chris Wilson34c998b2016-08-04 07:52:24 +01003182static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003183{
Chris Wilson34c998b2016-08-04 07:52:24 +01003184 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003185}
3186
Joonas Lahtinend507d732016-03-18 10:42:58 +02003187static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003188{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003189 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003190 int ret;
3191
Chris Wilson91c8a322016-07-05 10:40:23 +01003192 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003193 if (!ret) {
3194 DRM_ERROR("failed to set up gmch\n");
3195 return -EIO;
3196 }
3197
Joonas Lahtinend507d732016-03-18 10:42:58 +02003198 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3199 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003200
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003201 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303202 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003203 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3204 ggtt->base.clear_range = i915_ggtt_clear_range;
3205 ggtt->base.bind_vma = ggtt_bind_vma;
3206 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003207 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003208
Joonas Lahtinend507d732016-03-18 10:42:58 +02003209 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003210 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3211
Ben Widawskybaa09f52013-01-24 13:49:57 -08003212 return 0;
3213}
3214
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003215/**
Chris Wilson0088e522016-08-04 07:52:21 +01003216 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003217 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003218 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003219int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003220{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003221 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003222 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003223
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003224 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003225
Chris Wilson34c998b2016-08-04 07:52:24 +01003226 if (INTEL_GEN(dev_priv) <= 5)
3227 ret = i915_gmch_probe(ggtt);
3228 else if (INTEL_GEN(dev_priv) < 8)
3229 ret = gen6_gmch_probe(ggtt);
3230 else
3231 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003232 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003233 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003234
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003235 if ((ggtt->base.total - 1) >> 32) {
3236 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003237 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003238 ggtt->base.total >> 20);
3239 ggtt->base.total = 1ULL << 32;
3240 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3241 }
3242
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003243 if (ggtt->mappable_end > ggtt->base.total) {
3244 DRM_ERROR("mappable aperture extends past end of GGTT,"
3245 " aperture=%llx, total=%llx\n",
3246 ggtt->mappable_end, ggtt->base.total);
3247 ggtt->mappable_end = ggtt->base.total;
3248 }
3249
Ben Widawskybaa09f52013-01-24 13:49:57 -08003250 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003251 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003252 ggtt->base.total >> 20);
3253 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3254 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003255#ifdef CONFIG_INTEL_IOMMU
3256 if (intel_iommu_gfx_mapped)
3257 DRM_INFO("VT-d active for gfx access\n");
3258#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003259
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003260 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003261}
3262
3263/**
3264 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003265 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003266 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003267int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003268{
Chris Wilson0088e522016-08-04 07:52:21 +01003269 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3270 int ret;
3271
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003272 INIT_LIST_HEAD(&dev_priv->vm_list);
3273
3274 /* Subtract the guard page before address space initialization to
3275 * shrink the range used by drm_mm.
3276 */
3277 ggtt->base.total -= PAGE_SIZE;
3278 i915_address_space_init(&ggtt->base, dev_priv);
3279 ggtt->base.total += PAGE_SIZE;
3280 if (!HAS_LLC(dev_priv))
3281 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
3282
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003283 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3284 dev_priv->ggtt.mappable_base,
3285 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003286 ret = -EIO;
3287 goto out_gtt_cleanup;
3288 }
3289
3290 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3291
Chris Wilson0088e522016-08-04 07:52:21 +01003292 /*
3293 * Initialise stolen early so that we may reserve preallocated
3294 * objects for the BIOS to KMS transition.
3295 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003296 ret = i915_gem_init_stolen(&dev_priv->drm);
Chris Wilson0088e522016-08-04 07:52:21 +01003297 if (ret)
3298 goto out_gtt_cleanup;
3299
3300 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003301
3302out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003303 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003304 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003305}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003306
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003307int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003308{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003309 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003310 return -EIO;
3311
3312 return 0;
3313}
3314
Daniel Vetterfa423312015-04-14 17:35:23 +02003315void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3316{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003317 struct drm_i915_private *dev_priv = to_i915(dev);
3318 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003319 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003320
Chris Wilsondc979972016-05-10 14:10:04 +01003321 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003322
3323 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003324 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003325
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003326 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3327
3328 /* clflush objects bound into the GGTT and rebind them. */
3329 list_for_each_entry_safe(obj, on,
3330 &dev_priv->mm.bound_list, global_list) {
3331 bool ggtt_bound = false;
3332 struct i915_vma *vma;
3333
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003334 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003335 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003336 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003337
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003338 if (!i915_vma_unbind(vma))
3339 continue;
3340
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003341 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3342 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003343 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003344 }
3345
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003346 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003347 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003348 }
3349
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003350 ggtt->base.closed = false;
3351
Daniel Vetterfa423312015-04-14 17:35:23 +02003352 if (INTEL_INFO(dev)->gen >= 8) {
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01003353 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003354 chv_setup_private_ppat(dev_priv);
3355 else
3356 bdw_setup_private_ppat(dev_priv);
3357
3358 return;
3359 }
3360
3361 if (USES_PPGTT(dev)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003362 struct i915_address_space *vm;
3363
Daniel Vetterfa423312015-04-14 17:35:23 +02003364 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3365 /* TODO: Perhaps it shouldn't be gen6 specific */
3366
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003367 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003368
Chris Wilson2bfa9962016-08-04 07:52:25 +01003369 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003370 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003371 else
3372 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003373
3374 gen6_write_page_range(dev_priv, &ppgtt->pd,
3375 0, ppgtt->base.total);
3376 }
3377 }
3378
3379 i915_ggtt_flush(dev_priv);
3380}
3381
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003382static void
3383i915_vma_retire(struct i915_gem_active *active,
3384 struct drm_i915_gem_request *rq)
3385{
3386 const unsigned int idx = rq->engine->id;
3387 struct i915_vma *vma =
3388 container_of(active, struct i915_vma, last_read[idx]);
3389
3390 GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
3391
3392 i915_vma_clear_active(vma, idx);
3393 if (i915_vma_is_active(vma))
3394 return;
3395
3396 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Chris Wilson3272db52016-08-04 16:32:32 +01003397 if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003398 WARN_ON(i915_vma_unbind(vma));
3399}
3400
3401void i915_vma_destroy(struct i915_vma *vma)
3402{
3403 GEM_BUG_ON(vma->node.allocated);
3404 GEM_BUG_ON(i915_vma_is_active(vma));
Chris Wilson3272db52016-08-04 16:32:32 +01003405 GEM_BUG_ON(!i915_vma_is_closed(vma));
Chris Wilson49ef5292016-08-18 17:17:00 +01003406 GEM_BUG_ON(vma->fence);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003407
3408 list_del(&vma->vm_link);
Chris Wilson3272db52016-08-04 16:32:32 +01003409 if (!i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003410 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
3411
3412 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
3413}
3414
3415void i915_vma_close(struct i915_vma *vma)
3416{
Chris Wilson3272db52016-08-04 16:32:32 +01003417 GEM_BUG_ON(i915_vma_is_closed(vma));
3418 vma->flags |= I915_VMA_CLOSED;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003419
3420 list_del_init(&vma->obj_link);
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
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003425static struct i915_vma *
Chris Wilson058d88c2016-08-15 10:49:06 +01003426__i915_vma_create(struct drm_i915_gem_object *obj,
3427 struct i915_address_space *vm,
3428 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003429{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003430 struct i915_vma *vma;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003431 int i;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003432
Chris Wilson50e046b2016-08-04 07:52:46 +01003433 GEM_BUG_ON(vm->closed);
3434
Chris Wilsone20d2ab2015-04-07 16:20:58 +01003435 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003436 if (vma == NULL)
3437 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003438
Ben Widawsky6f65e292013-12-06 14:10:56 -08003439 INIT_LIST_HEAD(&vma->exec_list);
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003440 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
3441 init_request_active(&vma->last_read[i], i915_vma_retire);
Chris Wilson49ef5292016-08-18 17:17:00 +01003442 init_request_active(&vma->last_fence, NULL);
Chris Wilson50e046b2016-08-04 07:52:46 +01003443 list_add(&vma->vm_link, &vm->unbound_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003444 vma->vm = vm;
3445 vma->obj = obj;
Chris Wilsonde180032016-08-04 16:32:29 +01003446 vma->size = obj->base.size;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003447
Chris Wilson058d88c2016-08-15 10:49:06 +01003448 if (view) {
Chris Wilsonde180032016-08-04 16:32:29 +01003449 vma->ggtt_view = *view;
3450 if (view->type == I915_GGTT_VIEW_PARTIAL) {
3451 vma->size = view->params.partial.size;
3452 vma->size <<= PAGE_SHIFT;
3453 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3454 vma->size =
3455 intel_rotation_info_size(&view->params.rotated);
3456 vma->size <<= PAGE_SHIFT;
3457 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003458 }
3459
3460 if (i915_is_ggtt(vm)) {
3461 vma->flags |= I915_VMA_GGTT;
Chris Wilsonde180032016-08-04 16:32:29 +01003462 } else {
Chris Wilson596c5922016-02-26 11:03:20 +00003463 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Chris Wilsonde180032016-08-04 16:32:29 +01003464 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08003465
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003466 list_add_tail(&vma->obj_link, &obj->vma_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003467 return vma;
3468}
3469
Chris Wilson058d88c2016-08-15 10:49:06 +01003470static inline bool vma_matches(struct i915_vma *vma,
3471 struct i915_address_space *vm,
3472 const struct i915_ggtt_view *view)
3473{
3474 if (vma->vm != vm)
3475 return false;
3476
3477 if (!i915_vma_is_ggtt(vma))
3478 return true;
3479
3480 if (!view)
3481 return vma->ggtt_view.type == 0;
3482
3483 if (vma->ggtt_view.type != view->type)
3484 return false;
3485
3486 return memcmp(&vma->ggtt_view.params,
3487 &view->params,
3488 sizeof(view->params)) == 0;
3489}
3490
Ben Widawsky6f65e292013-12-06 14:10:56 -08003491struct i915_vma *
Chris Wilson81a8aa42016-08-15 10:48:48 +01003492i915_vma_create(struct drm_i915_gem_object *obj,
3493 struct i915_address_space *vm,
3494 const struct i915_ggtt_view *view)
3495{
3496 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Chris Wilson058d88c2016-08-15 10:49:06 +01003497 GEM_BUG_ON(i915_gem_obj_to_vma(obj, vm, view));
Chris Wilson81a8aa42016-08-15 10:48:48 +01003498
Chris Wilson058d88c2016-08-15 10:49:06 +01003499 return __i915_vma_create(obj, vm, view);
3500}
3501
3502struct i915_vma *
3503i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3504 struct i915_address_space *vm,
3505 const struct i915_ggtt_view *view)
3506{
3507 struct i915_vma *vma;
3508
3509 list_for_each_entry_reverse(vma, &obj->vma_list, obj_link)
3510 if (vma_matches(vma, vm, view))
3511 return vma;
3512
3513 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003514}
3515
3516struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003517i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003518 struct i915_address_space *vm,
3519 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003520{
3521 struct i915_vma *vma;
3522
Chris Wilson058d88c2016-08-15 10:49:06 +01003523 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3524
3525 vma = i915_gem_obj_to_vma(obj, vm, view);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003526 if (!vma)
Chris Wilson058d88c2016-08-15 10:49:06 +01003527 vma = __i915_vma_create(obj, vm, view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003528
Chris Wilson3272db52016-08-04 16:32:32 +01003529 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003530 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003531}
3532
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003533static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003534rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003535 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003536 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003537 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003538{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003539 unsigned int column, row;
3540 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003541
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003542 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003543 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003544 for (row = 0; row < height; row++) {
3545 st->nents++;
3546 /* We don't need the pages, but need to initialize
3547 * the entries so the sg list can be happily traversed.
3548 * The only thing we need are DMA addresses.
3549 */
3550 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003551 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003552 sg_dma_len(sg) = PAGE_SIZE;
3553 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003554 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003555 }
3556 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003557
3558 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003559}
3560
3561static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003562intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003563 struct drm_i915_gem_object *obj)
3564{
Dave Gordon85d12252016-05-20 11:54:06 +01003565 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003566 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003567 struct sgt_iter sgt_iter;
3568 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003569 unsigned long i;
3570 dma_addr_t *page_addr_list;
3571 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003572 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003573 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003574
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003575 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003576 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003577 sizeof(dma_addr_t),
3578 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003579 if (!page_addr_list)
3580 return ERR_PTR(ret);
3581
3582 /* Allocate target SG list. */
3583 st = kmalloc(sizeof(*st), GFP_KERNEL);
3584 if (!st)
3585 goto err_st_alloc;
3586
Ville Syrjälä6687c902015-09-15 13:16:41 +03003587 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003588 if (ret)
3589 goto err_sg_alloc;
3590
3591 /* Populate source page list from the object. */
3592 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +01003593 for_each_sgt_dma(dma_addr, sgt_iter, obj->pages)
3594 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003595
Dave Gordon85d12252016-05-20 11:54:06 +01003596 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003597 st->nents = 0;
3598 sg = st->sgl;
3599
Ville Syrjälä6687c902015-09-15 13:16:41 +03003600 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3601 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3602 rot_info->plane[i].width, rot_info->plane[i].height,
3603 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003604 }
3605
Ville Syrjälä6687c902015-09-15 13:16:41 +03003606 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3607 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003608
3609 drm_free_large(page_addr_list);
3610
3611 return st;
3612
3613err_sg_alloc:
3614 kfree(st);
3615err_st_alloc:
3616 drm_free_large(page_addr_list);
3617
Ville Syrjälä6687c902015-09-15 13:16:41 +03003618 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3619 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3620
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003621 return ERR_PTR(ret);
3622}
3623
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003624static struct sg_table *
3625intel_partial_pages(const struct i915_ggtt_view *view,
3626 struct drm_i915_gem_object *obj)
3627{
3628 struct sg_table *st;
3629 struct scatterlist *sg;
3630 struct sg_page_iter obj_sg_iter;
3631 int ret = -ENOMEM;
3632
3633 st = kmalloc(sizeof(*st), GFP_KERNEL);
3634 if (!st)
3635 goto err_st_alloc;
3636
3637 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
3638 if (ret)
3639 goto err_sg_alloc;
3640
3641 sg = st->sgl;
3642 st->nents = 0;
3643 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
3644 view->params.partial.offset)
3645 {
3646 if (st->nents >= view->params.partial.size)
3647 break;
3648
3649 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3650 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
3651 sg_dma_len(sg) = PAGE_SIZE;
3652
3653 sg = sg_next(sg);
3654 st->nents++;
3655 }
3656
3657 return st;
3658
3659err_sg_alloc:
3660 kfree(st);
3661err_st_alloc:
3662 return ERR_PTR(ret);
3663}
3664
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003665static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003666i915_get_ggtt_vma_pages(struct i915_vma *vma)
3667{
3668 int ret = 0;
3669
Chris Wilson247177d2016-08-15 10:48:47 +01003670 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003671 return 0;
3672
3673 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003674 vma->pages = vma->obj->pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003675 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003676 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003677 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003678 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003679 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003680 else
3681 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3682 vma->ggtt_view.type);
3683
Chris Wilson247177d2016-08-15 10:48:47 +01003684 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003685 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003686 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003687 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003688 } else if (IS_ERR(vma->pages)) {
3689 ret = PTR_ERR(vma->pages);
3690 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003691 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3692 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003693 }
3694
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003695 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003696}
3697
3698/**
3699 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3700 * @vma: VMA to map
3701 * @cache_level: mapping cache level
3702 * @flags: flags like global or local mapping
3703 *
3704 * DMA addresses are taken from the scatter-gather table of this object (or of
3705 * this VMA in case of non-default GGTT views) and PTE entries set up.
3706 * Note that DMA addresses are also the only part of the SG table we care about.
3707 */
3708int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3709 u32 flags)
3710{
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003711 u32 bind_flags;
Chris Wilson3272db52016-08-04 16:32:32 +01003712 u32 vma_flags;
3713 int ret;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003714
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003715 if (WARN_ON(flags == 0))
3716 return -EINVAL;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003717
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003718 bind_flags = 0;
Daniel Vetter08755462015-04-20 09:04:05 -07003719 if (flags & PIN_GLOBAL)
Chris Wilson3272db52016-08-04 16:32:32 +01003720 bind_flags |= I915_VMA_GLOBAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003721 if (flags & PIN_USER)
Chris Wilson3272db52016-08-04 16:32:32 +01003722 bind_flags |= I915_VMA_LOCAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003723
Chris Wilson3272db52016-08-04 16:32:32 +01003724 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
Daniel Vetter08755462015-04-20 09:04:05 -07003725 if (flags & PIN_UPDATE)
Chris Wilson3272db52016-08-04 16:32:32 +01003726 bind_flags |= vma_flags;
Daniel Vetter08755462015-04-20 09:04:05 -07003727 else
Chris Wilson3272db52016-08-04 16:32:32 +01003728 bind_flags &= ~vma_flags;
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003729 if (bind_flags == 0)
3730 return 0;
3731
Chris Wilson3272db52016-08-04 16:32:32 +01003732 if (vma_flags == 0 && vma->vm->allocate_va_range) {
Chris Wilson596c5922016-02-26 11:03:20 +00003733 trace_i915_va_alloc(vma);
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003734 ret = vma->vm->allocate_va_range(vma->vm,
3735 vma->node.start,
3736 vma->node.size);
3737 if (ret)
3738 return ret;
3739 }
3740
3741 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003742 if (ret)
3743 return ret;
Daniel Vetter08755462015-04-20 09:04:05 -07003744
Chris Wilson3272db52016-08-04 16:32:32 +01003745 vma->flags |= bind_flags;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003746 return 0;
3747}
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003748
Chris Wilson8ef85612016-04-28 09:56:39 +01003749void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
3750{
3751 void __iomem *ptr;
3752
Chris Wilsone5cdb222016-08-15 10:48:56 +01003753 /* Access through the GTT requires the device to be awake. */
3754 assert_rpm_wakelock_held(to_i915(vma->vm->dev));
3755
Chris Wilson8ef85612016-04-28 09:56:39 +01003756 lockdep_assert_held(&vma->vm->dev->struct_mutex);
Chris Wilson05a20d02016-08-18 17:16:55 +01003757 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma)))
Chris Wilson406ea8d2016-07-20 13:31:55 +01003758 return IO_ERR_PTR(-ENODEV);
Chris Wilson8ef85612016-04-28 09:56:39 +01003759
Chris Wilson3272db52016-08-04 16:32:32 +01003760 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
3761 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
Chris Wilson8ef85612016-04-28 09:56:39 +01003762
3763 ptr = vma->iomap;
3764 if (ptr == NULL) {
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003765 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
Chris Wilson8ef85612016-04-28 09:56:39 +01003766 vma->node.start,
3767 vma->node.size);
3768 if (ptr == NULL)
Chris Wilson406ea8d2016-07-20 13:31:55 +01003769 return IO_ERR_PTR(-ENOMEM);
Chris Wilson8ef85612016-04-28 09:56:39 +01003770
3771 vma->iomap = ptr;
3772 }
3773
Chris Wilson20dfbde2016-08-04 16:32:30 +01003774 __i915_vma_pin(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +01003775 return ptr;
3776}
Chris Wilson19880c42016-08-15 10:49:05 +01003777
3778void i915_vma_unpin_and_release(struct i915_vma **p_vma)
3779{
3780 struct i915_vma *vma;
3781
3782 vma = fetch_and_zero(p_vma);
3783 if (!vma)
3784 return;
3785
3786 i915_vma_unpin(vma);
3787 i915_vma_put(vma);
3788}