Mon Aug 27 06:08:25 2018 UTC ()
some more definitions necessary

ifdef out all the i915 gtt virtual memory stuff that errors,
unless it looks exactly like the old code and then merge the
netbsd ifdefs for it.

we don't want to use their ALIGN(, which has more arguments,
use the old alt function.
merge in the old intel_acpi.c code.
don't duplicate DECLARE_BITMAP

Author: coypu <coypu@sdf.org>
Committer: Taylor R Campbell <riastradh@NetBSD.org>


(riastradh)
diff -r1.11 -r1.12 src/sys/external/bsd/common/include/linux/kernel.h
diff -r1.5 -r1.6 src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_gtt.c
diff -r1.5 -r1.6 src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_stolen.c
diff -r1.3 -r1.4 src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_gtt.h
diff -r1.6 -r1.7 src/sys/external/bsd/drm2/dist/drm/i915/i915_gpu_error.c
diff -r1.2 -r1.3 src/sys/external/bsd/drm2/include/linux/hashtable.h

cvs diff -r1.11 -r1.12 src/sys/external/bsd/common/include/linux/kernel.h (switch to unified diff)

--- src/sys/external/bsd/common/include/linux/kernel.h 2018/08/27 06:07:20 1.11
+++ src/sys/external/bsd/common/include/linux/kernel.h 2018/08/27 06:08:25 1.12
@@ -1,177 +1,181 @@ @@ -1,177 +1,181 @@
1/* $NetBSD: kernel.h,v 1.11 2018/08/27 06:07:20 riastradh Exp $ */ 1/* $NetBSD: kernel.h,v 1.12 2018/08/27 06:08:25 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell. 8 * by Taylor R. Campbell.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#ifndef _LINUX_KERNEL_H_ 32#ifndef _LINUX_KERNEL_H_
33#define _LINUX_KERNEL_H_ 33#define _LINUX_KERNEL_H_
34 34
35#include <sys/cdefs.h> 35#include <sys/cdefs.h>
36#include <sys/types.h> 36#include <sys/types.h>
37#include <sys/param.h> 37#include <sys/param.h>
38#include <sys/systm.h> 38#include <sys/systm.h>
39 39
40#include <lib/libkern/libkern.h> 40#include <lib/libkern/libkern.h>
41#include <linux/printk.h> 41#include <linux/printk.h>
42 42
 43#define U16_MAX UINT16_MAX
 44#define U32_MAX UINT32_MAX
 45#define U64_MAX UINT64_MAX
 46
43#define oops_in_progress (panicstr != NULL) 47#define oops_in_progress (panicstr != NULL)
44 48
45#define IS_ENABLED(option) (option) 49#define IS_ENABLED(option) (option)
46#define IS_BUILTIN(option) (1) /* Probably... */ 50#define IS_BUILTIN(option) (1) /* Probably... */
47 51
48#define __printf __printflike 52#define __printf __printflike
49#define __user 53#define __user
50#define __must_check /* __attribute__((warn_unused_result)), if GCC */ 54#define __must_check /* __attribute__((warn_unused_result)), if GCC */
51#define __always_unused __unused 55#define __always_unused __unused
52 56
53#define barrier() __insn_barrier() 57#define barrier() __insn_barrier()
54#define likely(X) __predict_true(X) 58#define likely(X) __predict_true(X)
55#define unlikely(X) __predict_false(X) 59#define unlikely(X) __predict_false(X)
56 60
57/* 61/*
58 * XXX Linux kludge to work around GCC uninitialized variable warning. 62 * XXX Linux kludge to work around GCC uninitialized variable warning.
59 * Linux does `x = x', which is bollocks. 63 * Linux does `x = x', which is bollocks.
60 */ 64 */
61#define uninitialized_var(x) x = 0 65#define uninitialized_var(x) x = 0
62 66
63/* XXX These will multiply evaluate their arguments. */ 67/* XXX These will multiply evaluate their arguments. */
64#define min(X, Y) MIN(X, Y) 68#define min(X, Y) MIN(X, Y)
65#define max(X, Y) MAX(X, Y) 69#define max(X, Y) MAX(X, Y)
66 70
67#define max_t(T, X, Y) MAX(X, Y) 71#define max_t(T, X, Y) MAX(X, Y)
68#define min_t(T, X, Y) MIN(X, Y) 72#define min_t(T, X, Y) MIN(X, Y)
69 73
70#define clamp_t(T, X, MIN, MAX) min_t(T, max_t(T, X, MIN), MAX) 74#define clamp_t(T, X, MIN, MAX) min_t(T, max_t(T, X, MIN), MAX)
71#define clamp(X, MN, MX) MIN(MAX(X, MN), MX) 75#define clamp(X, MN, MX) MIN(MAX(X, MN), MX)
72 76
73/* 77/*
74 * Rounding to nearest. 78 * Rounding to nearest.
75 */ 79 */
76#define DIV_ROUND_CLOSEST(N, D) \ 80#define DIV_ROUND_CLOSEST(N, D) \
77 ((0 < (N)) ? (((N) + ((D) / 2)) / (D)) \ 81 ((0 < (N)) ? (((N) + ((D) / 2)) / (D)) \
78 : (((N) - ((D) / 2)) / (D))) 82 : (((N) - ((D) / 2)) / (D)))
79 83
80/* 84/*
81 * Rounding to what may or may not be powers of two. 85 * Rounding to what may or may not be powers of two.
82 */ 86 */
83#define DIV_ROUND_UP(X, N) (((X) + (N) - 1) / (N)) 87#define DIV_ROUND_UP(X, N) (((X) + (N) - 1) / (N))
84#define DIV_ROUND_UP_ULL(X, N) DIV_ROUND_UP((unsigned long long)(X), (N)) 88#define DIV_ROUND_UP_ULL(X, N) DIV_ROUND_UP((unsigned long long)(X), (N))
85 89
86/* 90/*
87 * Rounding to powers of two -- carefully avoiding multiple evaluation 91 * Rounding to powers of two -- carefully avoiding multiple evaluation
88 * of arguments and pitfalls with C integer arithmetic rules. 92 * of arguments and pitfalls with C integer arithmetic rules.
89 */ 93 */
90#define round_up(X, N) ((((X) - 1) | ((N) - 1)) + 1) 94#define round_up(X, N) ((((X) - 1) | ((N) - 1)) + 1)
91#define round_down(X, N) ((X) & ~(uintmax_t)((N) - 1)) 95#define round_down(X, N) ((X) & ~(uintmax_t)((N) - 1))
92 96
93#define IS_ALIGNED(X, N) (((X) & ((N) - 1)) == 0) 97#define IS_ALIGNED(X, N) (((X) & ((N) - 1)) == 0)
94 98
95/* 99/*
96 * These select 32-bit halves of what may be 32- or 64-bit quantities, 100 * These select 32-bit halves of what may be 32- or 64-bit quantities,
97 * for which straight 32-bit shifts may be undefined behaviour (and do 101 * for which straight 32-bit shifts may be undefined behaviour (and do
98 * the wrong thing on most machines: return the input unshifted by 102 * the wrong thing on most machines: return the input unshifted by
99 * ignoring the upper bits of the shift count). 103 * ignoring the upper bits of the shift count).
100 */ 104 */
101#define upper_32_bits(X) ((uint32_t) (((X) >> 16) >> 16)) 105#define upper_32_bits(X) ((uint32_t) (((X) >> 16) >> 16))
102#define lower_32_bits(X) ((uint32_t) ((X) & 0xffffffffUL)) 106#define lower_32_bits(X) ((uint32_t) ((X) & 0xffffffffUL))
103 107
104#define ARRAY_SIZE(ARRAY) __arraycount(ARRAY) 108#define ARRAY_SIZE(ARRAY) __arraycount(ARRAY)
105 109
106#define swap(X, Y) do \ 110#define swap(X, Y) do \
107{ \ 111{ \
108 /* XXX Kludge for type-safety. */ \ 112 /* XXX Kludge for type-safety. */ \
109 if (&(X) != &(Y)) { \ 113 if (&(X) != &(Y)) { \
110 CTASSERT(sizeof(X) == sizeof(Y)); \ 114 CTASSERT(sizeof(X) == sizeof(Y)); \
111 /* XXX Can't do this much better without typeof. */ \ 115 /* XXX Can't do this much better without typeof. */ \
112 char __swap_tmp[sizeof(X)]; \ 116 char __swap_tmp[sizeof(X)]; \
113 (void)memcpy(__swap_tmp, &(X), sizeof(X)); \ 117 (void)memcpy(__swap_tmp, &(X), sizeof(X)); \
114 (void)memcpy(&(X), &(Y), sizeof(X)); \ 118 (void)memcpy(&(X), &(Y), sizeof(X)); \
115 (void)memcpy(&(Y), __swap_tmp, sizeof(X)); \ 119 (void)memcpy(&(Y), __swap_tmp, sizeof(X)); \
116 } \ 120 } \
117} while (0) 121} while (0)
118 122
119static inline int64_t 123static inline int64_t
120abs64(int64_t x) 124abs64(int64_t x)
121{ 125{
122 return (x < 0? (-x) : x); 126 return (x < 0? (-x) : x);
123} 127}
124 128
125static inline uintmax_t 129static inline uintmax_t
126mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 130mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
127{ 131{
128 uintmax_t q = (x / divisor); 132 uintmax_t q = (x / divisor);
129 uintmax_t r = (x % divisor); 133 uintmax_t r = (x % divisor);
130 134
131 return ((q * multiplier) + ((r * multiplier) / divisor)); 135 return ((q * multiplier) + ((r * multiplier) / divisor));
132} 136}
133 137
134static int panic_timeout __unused = 0; 138static int panic_timeout __unused = 0;
135 139
136static inline int 140static inline int
137vscnprintf(char *buf, size_t size, const char *fmt, va_list va) 141vscnprintf(char *buf, size_t size, const char *fmt, va_list va)
138{ 142{
139 int ret; 143 int ret;
140 144
141 ret = vsnprintf(buf, size, fmt, va); 145 ret = vsnprintf(buf, size, fmt, va);
142 if (__predict_false(ret < 0)) 146 if (__predict_false(ret < 0))
143 return ret; 147 return ret;
144 if (__predict_false(size == 0)) 148 if (__predict_false(size == 0))
145 return 0; 149 return 0;
146 if (__predict_false(size <= ret)) 150 if (__predict_false(size <= ret))
147 return (size - 1); 151 return (size - 1);
148 152
149 return ret; 153 return ret;
150} 154}
151 155
152static inline int 156static inline int
153scnprintf(char *buf, size_t size, const char *fmt, ...) 157scnprintf(char *buf, size_t size, const char *fmt, ...)
154{ 158{
155 va_list va; 159 va_list va;
156 int ret; 160 int ret;
157 161
158 va_start(va, fmt); 162 va_start(va, fmt);
159 ret = vscnprintf(buf, size, fmt, va); 163 ret = vscnprintf(buf, size, fmt, va);
160 va_end(va); 164 va_end(va);
161 165
162 return ret; 166 return ret;
163} 167}
164 168
165static inline int 169static inline int
166kstrtol(const char *s, unsigned base, long *vp) 170kstrtol(const char *s, unsigned base, long *vp)
167{ 171{
168 long long v; 172 long long v;
169 173
170 v = strtoll(s, NULL, base); 174 v = strtoll(s, NULL, base);
171 if (v < LONG_MIN || LONG_MAX < v) 175 if (v < LONG_MIN || LONG_MAX < v)
172 return -ERANGE; 176 return -ERANGE;
173 *vp = v; 177 *vp = v;
174 return 0; 178 return 0;
175} 179}
176 180
177#endif /* _LINUX_KERNEL_H_ */ 181#endif /* _LINUX_KERNEL_H_ */

cvs diff -r1.5 -r1.6 src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_gtt.c (switch to unified diff)

--- src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_gtt.c 2018/08/27 04:58:23 1.5
+++ src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_gtt.c 2018/08/27 06:08:25 1.6
@@ -1,3847 +1,3857 @@ @@ -1,3847 +1,3857 @@
1/* $NetBSD: i915_gem_gtt.c,v 1.5 2018/08/27 04:58:23 riastradh Exp $ */ 1/* $NetBSD: i915_gem_gtt.c,v 1.6 2018/08/27 06:08:25 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright © 2010 Daniel Vetter 4 * Copyright © 2010 Daniel Vetter
5 * Copyright © 2011-2014 Intel Corporation 5 * Copyright © 2011-2014 Intel Corporation
6 * 6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a 7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"), 8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation 9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the 11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions: 12 * Software is furnished to do so, subject to the following conditions:
13 * 13 *
14 * The above copyright notice and this permission notice (including the next 14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the 15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software. 16 * Software.
17 * 17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * IN THE SOFTWARE. 24 * IN THE SOFTWARE.
25 * 25 *
26 */ 26 */
27 27
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: i915_gem_gtt.c,v 1.5 2018/08/27 04:58:23 riastradh Exp $"); 29__KERNEL_RCSID(0, "$NetBSD: i915_gem_gtt.c,v 1.6 2018/08/27 06:08:25 riastradh Exp $");
30 30
31#include <linux/err.h> 31#include <linux/err.h>
32#include <linux/seq_file.h> 32#include <linux/seq_file.h>
33#include <drm/drmP.h> 33#include <drm/drmP.h>
34#include <drm/i915_drm.h> 34#include <drm/i915_drm.h>
35#include "i915_drv.h" 35#include "i915_drv.h"
36#include "i915_vgpu.h" 36#include "i915_vgpu.h"
37#include "i915_trace.h" 37#include "i915_trace.h"
38#include "intel_drv.h" 38#include "intel_drv.h"
39 39
40#ifdef __NetBSD__ 40#ifdef __NetBSD__
41#include <x86/machdep.h> 41#include <x86/machdep.h>
42#include <x86/pte.h> 42#include <x86/pte.h>
43#define _PAGE_PRESENT PG_V /* 0x01 PTE is present / valid */ 43#define _PAGE_PRESENT PG_V /* 0x01 PTE is present / valid */
44#define _PAGE_RW PG_RW /* 0x02 read/write */ 44#define _PAGE_RW PG_RW /* 0x02 read/write */
45#define _PAGE_PWT PG_WT /* 0x08 write-through */ 45#define _PAGE_PWT PG_WT /* 0x08 write-through */
46#define _PAGE_PCD PG_N /* 0x10 page cache disabled / non-cacheable */ 46#define _PAGE_PCD PG_N /* 0x10 page cache disabled / non-cacheable */
47#define _PAGE_PAT PG_PAT /* 0x80 page attribute table on PTE */ 47#define _PAGE_PAT PG_PAT /* 0x80 page attribute table on PTE */
48#endif 48#endif
49 49
50/** 50/**
51 * DOC: Global GTT views 51 * DOC: Global GTT views
52 * 52 *
53 * Background and previous state 53 * Background and previous state
54 * 54 *
55 * Historically objects could exists (be bound) in global GTT space only as 55 * Historically objects could exists (be bound) in global GTT space only as
56 * singular instances with a view representing all of the object's backing pages 56 * singular instances with a view representing all of the object's backing pages
57 * in a linear fashion. This view will be called a normal view. 57 * in a linear fashion. This view will be called a normal view.
58 * 58 *
59 * To support multiple views of the same object, where the number of mapped 59 * To support multiple views of the same object, where the number of mapped
60 * pages is not equal to the backing store, or where the layout of the pages 60 * pages is not equal to the backing store, or where the layout of the pages
61 * is not linear, concept of a GGTT view was added. 61 * is not linear, concept of a GGTT view was added.
62 * 62 *
63 * One example of an alternative view is a stereo display driven by a single 63 * One example of an alternative view is a stereo display driven by a single
64 * image. In this case we would have a framebuffer looking like this 64 * image. In this case we would have a framebuffer looking like this
65 * (2x2 pages): 65 * (2x2 pages):
66 * 66 *
67 * 12 67 * 12
68 * 34 68 * 34
69 * 69 *
70 * Above would represent a normal GGTT view as normally mapped for GPU or CPU 70 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
71 * rendering. In contrast, fed to the display engine would be an alternative 71 * rendering. In contrast, fed to the display engine would be an alternative
72 * view which could look something like this: 72 * view which could look something like this:
73 * 73 *
74 * 1212 74 * 1212
75 * 3434 75 * 3434
76 * 76 *
77 * In this example both the size and layout of pages in the alternative view is 77 * In this example both the size and layout of pages in the alternative view is
78 * different from the normal view. 78 * different from the normal view.
79 * 79 *
80 * Implementation and usage 80 * Implementation and usage
81 * 81 *
82 * GGTT views are implemented using VMAs and are distinguished via enum 82 * GGTT views are implemented using VMAs and are distinguished via enum
83 * i915_ggtt_view_type and struct i915_ggtt_view. 83 * i915_ggtt_view_type and struct i915_ggtt_view.
84 * 84 *
85 * A new flavour of core GEM functions which work with GGTT bound objects were 85 * A new flavour of core GEM functions which work with GGTT bound objects were
86 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid 86 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
87 * renaming in large amounts of code. They take the struct i915_ggtt_view 87 * renaming in large amounts of code. They take the struct i915_ggtt_view
88 * parameter encapsulating all metadata required to implement a view. 88 * parameter encapsulating all metadata required to implement a view.
89 * 89 *
90 * As a helper for callers which are only interested in the normal view, 90 * As a helper for callers which are only interested in the normal view,
91 * globally const i915_ggtt_view_normal singleton instance exists. All old core 91 * globally const i915_ggtt_view_normal singleton instance exists. All old core
92 * GEM API functions, the ones not taking the view parameter, are operating on, 92 * GEM API functions, the ones not taking the view parameter, are operating on,
93 * or with the normal GGTT view. 93 * or with the normal GGTT view.
94 * 94 *
95 * Code wanting to add or use a new GGTT view needs to: 95 * Code wanting to add or use a new GGTT view needs to:
96 * 96 *
97 * 1. Add a new enum with a suitable name. 97 * 1. Add a new enum with a suitable name.
98 * 2. Extend the metadata in the i915_ggtt_view structure if required. 98 * 2. Extend the metadata in the i915_ggtt_view structure if required.
99 * 3. Add support to i915_get_vma_pages(). 99 * 3. Add support to i915_get_vma_pages().
100 * 100 *
101 * New views are required to build a scatter-gather table from within the 101 * New views are required to build a scatter-gather table from within the
102 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and 102 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
103 * exists for the lifetime of an VMA. 103 * exists for the lifetime of an VMA.
104 * 104 *
105 * Core API is designed to have copy semantics which means that passed in 105 * Core API is designed to have copy semantics which means that passed in
106 * struct i915_ggtt_view does not need to be persistent (left around after 106 * struct i915_ggtt_view does not need to be persistent (left around after
107 * calling the core API functions). 107 * calling the core API functions).
108 * 108 *
109 */ 109 */
110 110
111static int 111static int
112i915_get_ggtt_vma_pages(struct i915_vma *vma); 112i915_get_ggtt_vma_pages(struct i915_vma *vma);
113 113
114const struct i915_ggtt_view i915_ggtt_view_normal; 114const struct i915_ggtt_view i915_ggtt_view_normal;
115const struct i915_ggtt_view i915_ggtt_view_rotated = { 115const struct i915_ggtt_view i915_ggtt_view_rotated = {
116 .type = I915_GGTT_VIEW_ROTATED 116 .type = I915_GGTT_VIEW_ROTATED
117}; 117};
118 118
119static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt) 119static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
120{ 120{
121 bool has_aliasing_ppgtt; 121 bool has_aliasing_ppgtt;
122 bool has_full_ppgtt; 122 bool has_full_ppgtt;
123 123
124 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6; 124 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
125 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7; 125 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
126 126
127 if (intel_vgpu_active(dev)) 127 if (intel_vgpu_active(dev))
128 has_full_ppgtt = false; /* emulation is too hard */ 128 has_full_ppgtt = false; /* emulation is too hard */
129 129
130 /* 130 /*
131 * We don't allow disabling PPGTT for gen9+ as it's a requirement for 131 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
132 * execlists, the sole mechanism available to submit work. 132 * execlists, the sole mechanism available to submit work.
133 */ 133 */
134 if (INTEL_INFO(dev)->gen < 9 && 134 if (INTEL_INFO(dev)->gen < 9 &&
135 (enable_ppgtt == 0 || !has_aliasing_ppgtt)) 135 (enable_ppgtt == 0 || !has_aliasing_ppgtt))
136 return 0; 136 return 0;
137 137
138 if (enable_ppgtt == 1) 138 if (enable_ppgtt == 1)
139 return 1; 139 return 1;
140 140
141 if (enable_ppgtt == 2 && has_full_ppgtt) 141 if (enable_ppgtt == 2 && has_full_ppgtt)
142 return 2; 142 return 2;
143 143
144#ifdef CONFIG_INTEL_IOMMU 144#ifdef CONFIG_INTEL_IOMMU
145 /* Disable ppgtt on SNB if VT-d is on. */ 145 /* Disable ppgtt on SNB if VT-d is on. */
146 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) { 146 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
147 DRM_INFO("Disabling PPGTT because VT-d is on\n"); 147 DRM_INFO("Disabling PPGTT because VT-d is on\n");
148 return 0; 148 return 0;
149 } 149 }
150#endif 150#endif
151 151
152 /* Early VLV doesn't have this */ 152 /* Early VLV doesn't have this */
153 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && 153 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
154 dev->pdev->revision < 0xb) { 154 dev->pdev->revision < 0xb) {
155 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n"); 155 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
156 return 0; 156 return 0;
157 } 157 }
158 158
159 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists) 159 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
160 return 2; 160 return 2;
161 else 161 else
162 return has_aliasing_ppgtt ? 1 : 0; 162 return has_aliasing_ppgtt ? 1 : 0;
163} 163}
164 164
165static int ppgtt_bind_vma(struct i915_vma *vma, 165static int ppgtt_bind_vma(struct i915_vma *vma,
166 enum i915_cache_level cache_level, 166 enum i915_cache_level cache_level,
167 u32 unused) 167 u32 unused)
168{ 168{
169 u32 pte_flags = 0; 169 u32 pte_flags = 0;
170 170
171 /* Currently applicable only to VLV */ 171 /* Currently applicable only to VLV */
172 if (vma->obj->gt_ro) 172 if (vma->obj->gt_ro)
173 pte_flags |= PTE_READ_ONLY; 173 pte_flags |= PTE_READ_ONLY;
174 174
175 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start, 175 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
176 cache_level, pte_flags); 176 cache_level, pte_flags);
177 177
178 return 0; 178 return 0;
179} 179}
180 180
181static void ppgtt_unbind_vma(struct i915_vma *vma) 181static void ppgtt_unbind_vma(struct i915_vma *vma)
182{ 182{
183 vma->vm->clear_range(vma->vm, 183 vma->vm->clear_range(vma->vm,
184 vma->node.start, 184 vma->node.start,
185 vma->obj->base.size, 185 vma->obj->base.size,
186 true); 186 true);
187} 187}
188 188
189static gen8_pte_t gen8_pte_encode(dma_addr_t addr, 189static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
190 enum i915_cache_level level, 190 enum i915_cache_level level,
191 bool valid) 191 bool valid)
192{ 192{
193 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0; 193 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
194 pte |= addr; 194 pte |= addr;
195 195
196 switch (level) { 196 switch (level) {
197 case I915_CACHE_NONE: 197 case I915_CACHE_NONE:
198 pte |= PPAT_UNCACHED_INDEX; 198 pte |= PPAT_UNCACHED_INDEX;
199 break; 199 break;
200 case I915_CACHE_WT: 200 case I915_CACHE_WT:
201 pte |= PPAT_DISPLAY_ELLC_INDEX; 201 pte |= PPAT_DISPLAY_ELLC_INDEX;
202 break; 202 break;
203 default: 203 default:
204 pte |= PPAT_CACHED_INDEX; 204 pte |= PPAT_CACHED_INDEX;
205 break; 205 break;
206 } 206 }
207 207
208 return pte; 208 return pte;
209} 209}
210 210
211static gen8_pde_t gen8_pde_encode(const dma_addr_t addr, 211static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
212 const enum i915_cache_level level) 212 const enum i915_cache_level level)
213{ 213{
214 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW; 214 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
215 pde |= addr; 215 pde |= addr;
216 if (level != I915_CACHE_NONE) 216 if (level != I915_CACHE_NONE)
217 pde |= PPAT_CACHED_PDE_INDEX; 217 pde |= PPAT_CACHED_PDE_INDEX;
218 else 218 else
219 pde |= PPAT_UNCACHED_INDEX; 219 pde |= PPAT_UNCACHED_INDEX;
220 return pde; 220 return pde;
221} 221}
222 222
223#define gen8_pdpe_encode gen8_pde_encode 223#define gen8_pdpe_encode gen8_pde_encode
224#define gen8_pml4e_encode gen8_pde_encode 224#define gen8_pml4e_encode gen8_pde_encode
225 225
226static gen6_pte_t snb_pte_encode(dma_addr_t addr, 226static gen6_pte_t snb_pte_encode(dma_addr_t addr,
227 enum i915_cache_level level, 227 enum i915_cache_level level,
228 bool valid, u32 unused) 228 bool valid, u32 unused)
229{ 229{
230 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 230 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
231 pte |= GEN6_PTE_ADDR_ENCODE(addr); 231 pte |= GEN6_PTE_ADDR_ENCODE(addr);
232 232
233 switch (level) { 233 switch (level) {
234 case I915_CACHE_L3_LLC: 234 case I915_CACHE_L3_LLC:
235 case I915_CACHE_LLC: 235 case I915_CACHE_LLC:
236 pte |= GEN6_PTE_CACHE_LLC; 236 pte |= GEN6_PTE_CACHE_LLC;
237 break; 237 break;
238 case I915_CACHE_NONE: 238 case I915_CACHE_NONE:
239 pte |= GEN6_PTE_UNCACHED; 239 pte |= GEN6_PTE_UNCACHED;
240 break; 240 break;
241 default: 241 default:
242 MISSING_CASE(level); 242 MISSING_CASE(level);
243 } 243 }
244 244
245 return pte; 245 return pte;
246} 246}
247 247
248static gen6_pte_t ivb_pte_encode(dma_addr_t addr, 248static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
249 enum i915_cache_level level, 249 enum i915_cache_level level,
250 bool valid, u32 unused) 250 bool valid, u32 unused)
251{ 251{
252 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 252 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
253 pte |= GEN6_PTE_ADDR_ENCODE(addr); 253 pte |= GEN6_PTE_ADDR_ENCODE(addr);
254 254
255 switch (level) { 255 switch (level) {
256 case I915_CACHE_L3_LLC: 256 case I915_CACHE_L3_LLC:
257 pte |= GEN7_PTE_CACHE_L3_LLC; 257 pte |= GEN7_PTE_CACHE_L3_LLC;
258 break; 258 break;
259 case I915_CACHE_LLC: 259 case I915_CACHE_LLC:
260 pte |= GEN6_PTE_CACHE_LLC; 260 pte |= GEN6_PTE_CACHE_LLC;
261 break; 261 break;
262 case I915_CACHE_NONE: 262 case I915_CACHE_NONE:
263 pte |= GEN6_PTE_UNCACHED; 263 pte |= GEN6_PTE_UNCACHED;
264 break; 264 break;
265 default: 265 default:
266 MISSING_CASE(level); 266 MISSING_CASE(level);
267 } 267 }
268 268
269 return pte; 269 return pte;
270} 270}
271 271
272static gen6_pte_t byt_pte_encode(dma_addr_t addr, 272static gen6_pte_t byt_pte_encode(dma_addr_t addr,
273 enum i915_cache_level level, 273 enum i915_cache_level level,
274 bool valid, u32 flags) 274 bool valid, u32 flags)
275{ 275{
276 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 276 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
277 pte |= GEN6_PTE_ADDR_ENCODE(addr); 277 pte |= GEN6_PTE_ADDR_ENCODE(addr);
278 278
279 if (!(flags & PTE_READ_ONLY)) 279 if (!(flags & PTE_READ_ONLY))
280 pte |= BYT_PTE_WRITEABLE; 280 pte |= BYT_PTE_WRITEABLE;
281 281
282 if (level != I915_CACHE_NONE) 282 if (level != I915_CACHE_NONE)
283 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES; 283 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
284 284
285 return pte; 285 return pte;
286} 286}
287 287
288static gen6_pte_t hsw_pte_encode(dma_addr_t addr, 288static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
289 enum i915_cache_level level, 289 enum i915_cache_level level,
290 bool valid, u32 unused) 290 bool valid, u32 unused)
291{ 291{
292 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 292 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
293 pte |= HSW_PTE_ADDR_ENCODE(addr); 293 pte |= HSW_PTE_ADDR_ENCODE(addr);
294 294
295 if (level != I915_CACHE_NONE) 295 if (level != I915_CACHE_NONE)
296 pte |= HSW_WB_LLC_AGE3; 296 pte |= HSW_WB_LLC_AGE3;
297 297
298 return pte; 298 return pte;
299} 299}
300 300
301static gen6_pte_t iris_pte_encode(dma_addr_t addr, 301static gen6_pte_t iris_pte_encode(dma_addr_t addr,
302 enum i915_cache_level level, 302 enum i915_cache_level level,
303 bool valid, u32 unused) 303 bool valid, u32 unused)
304{ 304{
305 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 305 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
306 pte |= HSW_PTE_ADDR_ENCODE(addr); 306 pte |= HSW_PTE_ADDR_ENCODE(addr);
307 307
308 switch (level) { 308 switch (level) {
309 case I915_CACHE_NONE: 309 case I915_CACHE_NONE:
310 break; 310 break;
311 case I915_CACHE_WT: 311 case I915_CACHE_WT:
312 pte |= HSW_WT_ELLC_LLC_AGE3; 312 pte |= HSW_WT_ELLC_LLC_AGE3;
313 break; 313 break;
314 default: 314 default:
315 pte |= HSW_WB_ELLC_LLC_AGE3; 315 pte |= HSW_WB_ELLC_LLC_AGE3;
316 break; 316 break;
317 } 317 }
318 318
319 return pte; 319 return pte;
320} 320}
321 321
322static int __setup_page_dma(struct drm_device *dev, 322static int __setup_page_dma(struct drm_device *dev,
323 struct i915_page_dma *p, gfp_t flags) 323 struct i915_page_dma *p, gfp_t flags)
324{ 324{
325#ifdef __NetBSD__ 325#ifdef __NetBSD__
326 int error; 326 int error;
327 int nseg = 1; 327 int nseg = 1;
328 328
329 error = bus_dmamem_alloc(dev->dmat, PAGE_SIZE, PAGE_SIZE, PAGE_SIZE, 329 error = bus_dmamem_alloc(dev->dmat, PAGE_SIZE, PAGE_SIZE, PAGE_SIZE,
330 &p->seg, nseg, &nseg, BUS_DMA_WAITOK); 330 &p->seg, nseg, &nseg, BUS_DMA_WAITOK);
331 if (error) 331 if (error)
332fail0: return -error; /* XXX errno NetBSD->Linux */ 332fail0: return -error; /* XXX errno NetBSD->Linux */
333 KASSERT(nseg == 1); 333 KASSERT(nseg == 1);
334 error = bus_dmamap_create(dev->dmat, PAGE_SIZE, 1, PAGE_SIZE, 334 error = bus_dmamap_create(dev->dmat, PAGE_SIZE, 1, PAGE_SIZE,
335 PAGE_SIZE, BUS_DMA_WAITOK, &p->map); 335 PAGE_SIZE, BUS_DMA_WAITOK, &p->map);
336 if (error) { 336 if (error) {
337fail1: bus_dmamem_free(dev->dmat, &p->seg, 1); 337fail1: bus_dmamem_free(dev->dmat, &p->seg, 1);
338 goto fail0; 338 goto fail0;
339 } 339 }
340 error = bus_dmamap_load_raw(dev->dmat, p->map, &p->seg, 1, PAGE_SIZE, 340 error = bus_dmamap_load_raw(dev->dmat, p->map, &p->seg, 1, PAGE_SIZE,
341 BUS_DMA_WAITOK); 341 BUS_DMA_WAITOK);
342 if (error) { 342 if (error) {
343fail2: __unused 343fail2: __unused
344 bus_dmamap_destroy(dev->dmat, p->map); 344 bus_dmamap_destroy(dev->dmat, p->map);
345 goto fail1; 345 goto fail1;
346 } 346 }
347#else 347#else
348 struct device *device = &dev->pdev->dev; 348 struct device *device = &dev->pdev->dev;
349 349
350 p->page = alloc_page(flags); 350 p->page = alloc_page(flags);
351 if (!p->page) 351 if (!p->page)
352 return -ENOMEM; 352 return -ENOMEM;
353 353
354 p->daddr = dma_map_page(device, 354 p->daddr = dma_map_page(device,
355 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL); 355 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
356 356
357 if (dma_mapping_error(device, p->daddr)) { 357 if (dma_mapping_error(device, p->daddr)) {
358 __free_page(p->page); 358 __free_page(p->page);
359 return -EINVAL; 359 return -EINVAL;
360 } 360 }
361#endif 361#endif
362 362
363 return 0; 363 return 0;
364} 364}
365 365
366static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p) 366static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
367{ 367{
368 return __setup_page_dma(dev, p, GFP_KERNEL); 368 return __setup_page_dma(dev, p, GFP_KERNEL);
369} 369}
370 370
371static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p) 371static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
372{ 372{
373#ifdef __NetBSD__ 373#ifdef __NetBSD__
374 if (WARN_ON(!p->map)) 374 if (WARN_ON(!p->map))
375 return; 375 return;
376 376
377 bus_dmamap_unload(dev->dmat, p->map); 377 bus_dmamap_unload(dev->dmat, p->map);
378 bus_dmamap_destroy(dev->dmat, p->dmap); 378 bus_dmamap_destroy(dev->dmat, p->dmap);
379 bus_dmamem_free(dev->dmat, &p->seg, 1); 379 bus_dmamem_free(dev->dmat, &p->seg, 1);
380#else 380#else
381 if (WARN_ON(!p->page)) 381 if (WARN_ON(!p->page))
382 return; 382 return;
383 383
384 dma_unmap_page(&dev->pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL); 384 dma_unmap_page(&dev->pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
385 __free_page(p->page); 385 __free_page(p->page);
386 memset(p, 0, sizeof(*p)); 386 memset(p, 0, sizeof(*p));
387#endif 387#endif
388} 388}
389 389
390static void *kmap_page_dma(struct i915_page_dma *p) 390static void *kmap_page_dma(struct i915_page_dma *p)
391{ 391{
392#ifdef __NetBSD__ 392#ifdef __NetBSD__
393 return kmap_atomic(PHYS_TO_VM_PAGE(p->seg.ds_addr)); 393 return kmap_atomic(PHYS_TO_VM_PAGE(p->seg.ds_addr));
394#else 394#else
395 return kmap_atomic(p->page); 395 return kmap_atomic(p->page);
396#endif 396#endif
397} 397}
398 398
399/* We use the flushing unmap only with ppgtt structures: 399/* We use the flushing unmap only with ppgtt structures:
400 * page directories, page tables and scratch pages. 400 * page directories, page tables and scratch pages.
401 */ 401 */
402static void kunmap_page_dma(struct drm_device *dev, void *vaddr) 402static void kunmap_page_dma(struct drm_device *dev, void *vaddr)
403{ 403{
404 /* There are only few exceptions for gen >=6. chv and bxt. 404 /* There are only few exceptions for gen >=6. chv and bxt.
405 * And we are not sure about the latter so play safe for now. 405 * And we are not sure about the latter so play safe for now.
406 */ 406 */
407 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) 407 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
408 drm_clflush_virt_range(vaddr, PAGE_SIZE); 408 drm_clflush_virt_range(vaddr, PAGE_SIZE);
409 409
410 kunmap_atomic(vaddr); 410 kunmap_atomic(vaddr);
411} 411}
412 412
413#define kmap_px(px) kmap_page_dma(px_base(px)) 413#define kmap_px(px) kmap_page_dma(px_base(px))
414#define kunmap_px(ppgtt, vaddr) kunmap_page_dma((ppgtt)->base.dev, (vaddr)) 414#define kunmap_px(ppgtt, vaddr) kunmap_page_dma((ppgtt)->base.dev, (vaddr))
415 415
416#define setup_px(dev, px) setup_page_dma((dev), px_base(px)) 416#define setup_px(dev, px) setup_page_dma((dev), px_base(px))
417#define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px)) 417#define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
418#define fill_px(dev, px, v) fill_page_dma((dev), px_base(px), (v)) 418#define fill_px(dev, px, v) fill_page_dma((dev), px_base(px), (v))
419#define fill32_px(dev, px, v) fill_page_dma_32((dev), px_base(px), (v)) 419#define fill32_px(dev, px, v) fill_page_dma_32((dev), px_base(px), (v))
420 420
421static void fill_page_dma(struct drm_device *dev, struct i915_page_dma *p, 421static void fill_page_dma(struct drm_device *dev, struct i915_page_dma *p,
422 const uint64_t val) 422 const uint64_t val)
423{ 423{
424 int i; 424 int i;
425 uint64_t * const vaddr = kmap_page_dma(p); 425 uint64_t * const vaddr = kmap_page_dma(p);
426 426
427 for (i = 0; i < 512; i++) 427 for (i = 0; i < 512; i++)
428 vaddr[i] = val; 428 vaddr[i] = val;
429 429
430 kunmap_page_dma(dev, vaddr); 430 kunmap_page_dma(dev, vaddr);
431} 431}
432 432
433static void fill_page_dma_32(struct drm_device *dev, struct i915_page_dma *p, 433static void fill_page_dma_32(struct drm_device *dev, struct i915_page_dma *p,
434 const uint32_t val32) 434 const uint32_t val32)
435{ 435{
436 uint64_t v = val32; 436 uint64_t v = val32;
437 437
438 v = v << 32 | val32; 438 v = v << 32 | val32;
439 439
440 fill_page_dma(dev, p, v); 440 fill_page_dma(dev, p, v);
441} 441}
442 442
443static struct i915_page_scratch *alloc_scratch_page(struct drm_device *dev) 443static struct i915_page_scratch *alloc_scratch_page(struct drm_device *dev)
444{ 444{
445 struct i915_page_scratch *sp; 445 struct i915_page_scratch *sp;
446 int ret; 446 int ret;
447 447
448 sp = kzalloc(sizeof(*sp), GFP_KERNEL); 448 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
449 if (sp == NULL) 449 if (sp == NULL)
450 return ERR_PTR(-ENOMEM); 450 return ERR_PTR(-ENOMEM);
451 451
452 ret = __setup_page_dma(dev, px_base(sp), GFP_DMA32 | __GFP_ZERO); 452 ret = __setup_page_dma(dev, px_base(sp), GFP_DMA32 | __GFP_ZERO);
453 if (ret) { 453 if (ret) {
454 kfree(sp); 454 kfree(sp);
455 return ERR_PTR(ret); 455 return ERR_PTR(ret);
456 } 456 }
457 457
458 set_pages_uc(px_page(sp), 1); 458 set_pages_uc(px_page(sp), 1);
459 459
460 return sp; 460 return sp;
461} 461}
462 462
463static void free_scratch_page(struct drm_device *dev, 463static void free_scratch_page(struct drm_device *dev,
464 struct i915_page_scratch *sp) 464 struct i915_page_scratch *sp)
465{ 465{
466 set_pages_wb(px_page(sp), 1); 466 set_pages_wb(px_page(sp), 1);
467 467
468 cleanup_px(dev, sp); 468 cleanup_px(dev, sp);
469 kfree(sp); 469 kfree(sp);
470} 470}
471 471
472static struct i915_page_table *alloc_pt(struct drm_device *dev) 472static struct i915_page_table *alloc_pt(struct drm_device *dev)
473{ 473{
474 struct i915_page_table *pt; 474 struct i915_page_table *pt;
475 const size_t count = INTEL_INFO(dev)->gen >= 8 ? 475 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
476 GEN8_PTES : GEN6_PTES; 476 GEN8_PTES : GEN6_PTES;
477 int ret = -ENOMEM; 477 int ret = -ENOMEM;
478 478
479 pt = kzalloc(sizeof(*pt), GFP_KERNEL); 479 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
480 if (!pt) 480 if (!pt)
481 return ERR_PTR(-ENOMEM); 481 return ERR_PTR(-ENOMEM);
482 482
483 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes), 483 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
484 GFP_KERNEL); 484 GFP_KERNEL);
485 485
486 if (!pt->used_ptes) 486 if (!pt->used_ptes)
487 goto fail_bitmap; 487 goto fail_bitmap;
488 488
489 ret = setup_px(dev, pt); 489 ret = setup_px(dev, pt);
490 if (ret) 490 if (ret)
491 goto fail_page_m; 491 goto fail_page_m;
492 492
493 return pt; 493 return pt;
494 494
495fail_page_m: 495fail_page_m:
496 kfree(pt->used_ptes); 496 kfree(pt->used_ptes);
497fail_bitmap: 497fail_bitmap:
498 kfree(pt); 498 kfree(pt);
499 499
500 return ERR_PTR(ret); 500 return ERR_PTR(ret);
501} 501}
502 502
503static void free_pt(struct drm_device *dev, struct i915_page_table *pt) 503static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
504{ 504{
505 cleanup_px(dev, pt); 505 cleanup_px(dev, pt);
506 kfree(pt->used_ptes); 506 kfree(pt->used_ptes);
507 kfree(pt); 507 kfree(pt);
508} 508}
509 509
510static void gen8_initialize_pt(struct i915_address_space *vm, 510static void gen8_initialize_pt(struct i915_address_space *vm,
511 struct i915_page_table *pt) 511 struct i915_page_table *pt)
512{ 512{
513 gen8_pte_t scratch_pte; 513 gen8_pte_t scratch_pte;
514 514
515 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page), 515 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
516 I915_CACHE_LLC, true); 516 I915_CACHE_LLC, true);
517 517
518 fill_px(vm->dev, pt, scratch_pte); 518 fill_px(vm->dev, pt, scratch_pte);
519} 519}
520 520
521static void gen6_initialize_pt(struct i915_address_space *vm, 521static void gen6_initialize_pt(struct i915_address_space *vm,
522 struct i915_page_table *pt) 522 struct i915_page_table *pt)
523{ 523{
524 gen6_pte_t scratch_pte; 524 gen6_pte_t scratch_pte;
525 525
526 WARN_ON(px_dma(vm->scratch_page) == 0); 526 WARN_ON(px_dma(vm->scratch_page) == 0);
527 527
528 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page), 528 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
529 I915_CACHE_LLC, true, 0); 529 I915_CACHE_LLC, true, 0);
530 530
531 fill32_px(vm->dev, pt, scratch_pte); 531 fill32_px(vm->dev, pt, scratch_pte);
532} 532}
533 533
534static struct i915_page_directory *alloc_pd(struct drm_device *dev) 534static struct i915_page_directory *alloc_pd(struct drm_device *dev)
535{ 535{
536 struct i915_page_directory *pd; 536 struct i915_page_directory *pd;
537 int ret = -ENOMEM; 537 int ret = -ENOMEM;
538 538
539 pd = kzalloc(sizeof(*pd), GFP_KERNEL); 539 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
540 if (!pd) 540 if (!pd)
541 return ERR_PTR(-ENOMEM); 541 return ERR_PTR(-ENOMEM);
542 542
543 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES), 543 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
544 sizeof(*pd->used_pdes), GFP_KERNEL); 544 sizeof(*pd->used_pdes), GFP_KERNEL);
545 if (!pd->used_pdes) 545 if (!pd->used_pdes)
546 goto fail_bitmap; 546 goto fail_bitmap;
547 547
548 ret = setup_px(dev, pd); 548 ret = setup_px(dev, pd);
549 if (ret) 549 if (ret)
550 goto fail_page_m; 550 goto fail_page_m;
551 551
552 return pd; 552 return pd;
553 553
554fail_page_m: 554fail_page_m:
555 kfree(pd->used_pdes); 555 kfree(pd->used_pdes);
556fail_bitmap: 556fail_bitmap:
557 kfree(pd); 557 kfree(pd);
558 558
559 return ERR_PTR(ret); 559 return ERR_PTR(ret);
560} 560}
561 561
562static void free_pd(struct drm_device *dev, struct i915_page_directory *pd) 562static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
563{ 563{
564 if (px_page(pd)) { 564 if (px_page(pd)) {
565 cleanup_px(dev, pd); 565 cleanup_px(dev, pd);
566 kfree(pd->used_pdes); 566 kfree(pd->used_pdes);
567 kfree(pd); 567 kfree(pd);
568 } 568 }
569} 569}
570 570
571static void gen8_initialize_pd(struct i915_address_space *vm, 571static void gen8_initialize_pd(struct i915_address_space *vm,
572 struct i915_page_directory *pd) 572 struct i915_page_directory *pd)
573{ 573{
574 gen8_pde_t scratch_pde; 574 gen8_pde_t scratch_pde;
575 575
576 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC); 576 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
577 577
578 fill_px(vm->dev, pd, scratch_pde); 578 fill_px(vm->dev, pd, scratch_pde);
579} 579}
580 580
581static int __pdp_init(struct drm_device *dev, 581static int __pdp_init(struct drm_device *dev,
582 struct i915_page_directory_pointer *pdp) 582 struct i915_page_directory_pointer *pdp)
583{ 583{
584 size_t pdpes = I915_PDPES_PER_PDP(dev); 584 size_t pdpes = I915_PDPES_PER_PDP(dev);
585 585
586 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes), 586 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
587 sizeof(unsigned long), 587 sizeof(unsigned long),
588 GFP_KERNEL); 588 GFP_KERNEL);
589 if (!pdp->used_pdpes) 589 if (!pdp->used_pdpes)
590 return -ENOMEM; 590 return -ENOMEM;
591 591
592 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory), 592 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
593 GFP_KERNEL); 593 GFP_KERNEL);
594 if (!pdp->page_directory) { 594 if (!pdp->page_directory) {
595 kfree(pdp->used_pdpes); 595 kfree(pdp->used_pdpes);
596 /* the PDP might be the statically allocated top level. Keep it 596 /* the PDP might be the statically allocated top level. Keep it
597 * as clean as possible */ 597 * as clean as possible */
598 pdp->used_pdpes = NULL; 598 pdp->used_pdpes = NULL;
599 return -ENOMEM; 599 return -ENOMEM;
600 } 600 }
601 601
602 return 0; 602 return 0;
603} 603}
604 604
605static void __pdp_fini(struct i915_page_directory_pointer *pdp) 605static void __pdp_fini(struct i915_page_directory_pointer *pdp)
606{ 606{
607 kfree(pdp->used_pdpes); 607 kfree(pdp->used_pdpes);
608 kfree(pdp->page_directory); 608 kfree(pdp->page_directory);
609 pdp->page_directory = NULL; 609 pdp->page_directory = NULL;
610} 610}
611 611
612static struct 612static struct
613i915_page_directory_pointer *alloc_pdp(struct drm_device *dev) 613i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
614{ 614{
615 struct i915_page_directory_pointer *pdp; 615 struct i915_page_directory_pointer *pdp;
616 int ret = -ENOMEM; 616 int ret = -ENOMEM;
617 617
618 WARN_ON(!USES_FULL_48BIT_PPGTT(dev)); 618 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
619 619
620 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL); 620 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
621 if (!pdp) 621 if (!pdp)
622 return ERR_PTR(-ENOMEM); 622 return ERR_PTR(-ENOMEM);
623 623
624 ret = __pdp_init(dev, pdp); 624 ret = __pdp_init(dev, pdp);
625 if (ret) 625 if (ret)
626 goto fail_bitmap; 626 goto fail_bitmap;
627 627
628 ret = setup_px(dev, pdp); 628 ret = setup_px(dev, pdp);
629 if (ret) 629 if (ret)
630 goto fail_page_m; 630 goto fail_page_m;
631 631
632 return pdp; 632 return pdp;
633 633
634fail_page_m: 634fail_page_m:
635 __pdp_fini(pdp); 635 __pdp_fini(pdp);
636fail_bitmap: 636fail_bitmap:
637 kfree(pdp); 637 kfree(pdp);
638 638
639 return ERR_PTR(ret); 639 return ERR_PTR(ret);
640} 640}
641 641
642static void free_pdp(struct drm_device *dev, 642static void free_pdp(struct drm_device *dev,
643 struct i915_page_directory_pointer *pdp) 643 struct i915_page_directory_pointer *pdp)
644{ 644{
645 __pdp_fini(pdp); 645 __pdp_fini(pdp);
646 if (USES_FULL_48BIT_PPGTT(dev)) { 646 if (USES_FULL_48BIT_PPGTT(dev)) {
647 cleanup_px(dev, pdp); 647 cleanup_px(dev, pdp);
648 kfree(pdp); 648 kfree(pdp);
649 } 649 }
650} 650}
651 651
652static void gen8_initialize_pdp(struct i915_address_space *vm, 652static void gen8_initialize_pdp(struct i915_address_space *vm,
653 struct i915_page_directory_pointer *pdp) 653 struct i915_page_directory_pointer *pdp)
654{ 654{
655 gen8_ppgtt_pdpe_t scratch_pdpe; 655 gen8_ppgtt_pdpe_t scratch_pdpe;
656 656
657 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC); 657 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
658 658
659 fill_px(vm->dev, pdp, scratch_pdpe); 659 fill_px(vm->dev, pdp, scratch_pdpe);
660} 660}
661 661
662static void gen8_initialize_pml4(struct i915_address_space *vm, 662static void gen8_initialize_pml4(struct i915_address_space *vm,
663 struct i915_pml4 *pml4) 663 struct i915_pml4 *pml4)
664{ 664{
665 gen8_ppgtt_pml4e_t scratch_pml4e; 665 gen8_ppgtt_pml4e_t scratch_pml4e;
666 666
667 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp), 667 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
668 I915_CACHE_LLC); 668 I915_CACHE_LLC);
669 669
670 fill_px(vm->dev, pml4, scratch_pml4e); 670 fill_px(vm->dev, pml4, scratch_pml4e);
671} 671}
672 672
673static void 673static void
674gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt, 674gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
675 struct i915_page_directory_pointer *pdp, 675 struct i915_page_directory_pointer *pdp,
676 struct i915_page_directory *pd, 676 struct i915_page_directory *pd,
677 int index) 677 int index)
678{ 678{
679 gen8_ppgtt_pdpe_t *page_directorypo; 679 gen8_ppgtt_pdpe_t *page_directorypo;
680 680
681 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) 681 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
682 return; 682 return;
683 683
684 page_directorypo = kmap_px(pdp); 684 page_directorypo = kmap_px(pdp);
685 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC); 685 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
686 kunmap_px(ppgtt, page_directorypo); 686 kunmap_px(ppgtt, page_directorypo);
687} 687}
688 688
689static void 689static void
690gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt, 690gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
691 struct i915_pml4 *pml4, 691 struct i915_pml4 *pml4,
692 struct i915_page_directory_pointer *pdp, 692 struct i915_page_directory_pointer *pdp,
693 int index) 693 int index)
694{ 694{
695 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4); 695 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
696 696
697 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev)); 697 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
698 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC); 698 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
699 kunmap_px(ppgtt, pagemap); 699 kunmap_px(ppgtt, pagemap);
700} 700}
701 701
702/* Broadwell Page Directory Pointer Descriptors */ 702/* Broadwell Page Directory Pointer Descriptors */
703static int gen8_write_pdp(struct drm_i915_gem_request *req, 703static int gen8_write_pdp(struct drm_i915_gem_request *req,
704 unsigned entry, 704 unsigned entry,
705 dma_addr_t addr) 705 dma_addr_t addr)
706{ 706{
707 struct intel_engine_cs *ring = req->ring; 707 struct intel_engine_cs *ring = req->ring;
708 int ret; 708 int ret;
709 709
710 BUG_ON(entry >= 4); 710 BUG_ON(entry >= 4);
711 711
712 ret = intel_ring_begin(req, 6); 712 ret = intel_ring_begin(req, 6);
713 if (ret) 713 if (ret)
714 return ret; 714 return ret;
715 715
716 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); 716 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
717 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry)); 717 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
718 intel_ring_emit(ring, upper_32_bits(addr)); 718 intel_ring_emit(ring, upper_32_bits(addr));
719 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); 719 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
720 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry)); 720 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
721 intel_ring_emit(ring, lower_32_bits(addr)); 721 intel_ring_emit(ring, lower_32_bits(addr));
722 intel_ring_advance(ring); 722 intel_ring_advance(ring);
723 723
724 return 0; 724 return 0;
725} 725}
726 726
727static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt, 727static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
728 struct drm_i915_gem_request *req) 728 struct drm_i915_gem_request *req)
729{ 729{
730 int i, ret; 730 int i, ret;
731 731
732 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) { 732 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
733 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i); 733 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
734 734
735 ret = gen8_write_pdp(req, i, pd_daddr); 735 ret = gen8_write_pdp(req, i, pd_daddr);
736 if (ret) 736 if (ret)
737 return ret; 737 return ret;
738 } 738 }
739 739
740 return 0; 740 return 0;
741} 741}
742 742
743static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt, 743static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
744 struct drm_i915_gem_request *req) 744 struct drm_i915_gem_request *req)
745{ 745{
746 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4)); 746 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
747} 747}
748 748
749static void gen8_ppgtt_clear_pte_range(struct i915_address_space *vm, 749static void gen8_ppgtt_clear_pte_range(struct i915_address_space *vm,
750 struct i915_page_directory_pointer *pdp, 750 struct i915_page_directory_pointer *pdp,
751 uint64_t start, 751 uint64_t start,
752 uint64_t length, 752 uint64_t length,
753 gen8_pte_t scratch_pte) 753 gen8_pte_t scratch_pte)
754{ 754{
755 struct i915_hw_ppgtt *ppgtt = 755 struct i915_hw_ppgtt *ppgtt =
756 container_of(vm, struct i915_hw_ppgtt, base); 756 container_of(vm, struct i915_hw_ppgtt, base);
757 gen8_pte_t *pt_vaddr; 757 gen8_pte_t *pt_vaddr;
758 unsigned pdpe = gen8_pdpe_index(start); 758 unsigned pdpe = gen8_pdpe_index(start);
759 unsigned pde = gen8_pde_index(start); 759 unsigned pde = gen8_pde_index(start);
760 unsigned pte = gen8_pte_index(start); 760 unsigned pte = gen8_pte_index(start);
761 unsigned num_entries = length >> PAGE_SHIFT; 761 unsigned num_entries = length >> PAGE_SHIFT;
762 unsigned last_pte, i; 762 unsigned last_pte, i;
763 763
764 if (WARN_ON(!pdp)) 764 if (WARN_ON(!pdp))
765 return; 765 return;
766 766
767 while (num_entries) { 767 while (num_entries) {
768 struct i915_page_directory *pd; 768 struct i915_page_directory *pd;
769 struct i915_page_table *pt; 769 struct i915_page_table *pt;
770 770
771 if (WARN_ON(!pdp->page_directory[pdpe])) 771 if (WARN_ON(!pdp->page_directory[pdpe]))
772 break; 772 break;
773 773
774 pd = pdp->page_directory[pdpe]; 774 pd = pdp->page_directory[pdpe];
775 775
776 if (WARN_ON(!pd->page_table[pde])) 776 if (WARN_ON(!pd->page_table[pde]))
777 break; 777 break;
778 778
779 pt = pd->page_table[pde]; 779 pt = pd->page_table[pde];
780 780
781 if (WARN_ON(!px_page(pt))) 781 if (WARN_ON(!px_page(pt)))
782 break; 782 break;
783 783
784 last_pte = pte + num_entries; 784 last_pte = pte + num_entries;
785 if (last_pte > GEN8_PTES) 785 if (last_pte > GEN8_PTES)
786 last_pte = GEN8_PTES; 786 last_pte = GEN8_PTES;
787 787
788 pt_vaddr = kmap_px(pt); 788 pt_vaddr = kmap_px(pt);
789 789
790 for (i = pte; i < last_pte; i++) { 790 for (i = pte; i < last_pte; i++) {
791 pt_vaddr[i] = scratch_pte; 791 pt_vaddr[i] = scratch_pte;
792 num_entries--; 792 num_entries--;
793 } 793 }
794 794
795 kunmap_px(ppgtt, pt); 795 kunmap_px(ppgtt, pt);
796 796
797 pte = 0; 797 pte = 0;
798 if (++pde == I915_PDES) { 798 if (++pde == I915_PDES) {
799 if (++pdpe == I915_PDPES_PER_PDP(vm->dev)) 799 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
800 break; 800 break;
801 pde = 0; 801 pde = 0;
802 } 802 }
803 } 803 }
804} 804}
805 805
806static void gen8_ppgtt_clear_range(struct i915_address_space *vm, 806static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
807 uint64_t start, 807 uint64_t start,
808 uint64_t length, 808 uint64_t length,
809 bool use_scratch) 809 bool use_scratch)
810{ 810{
811 struct i915_hw_ppgtt *ppgtt = 811 struct i915_hw_ppgtt *ppgtt =
812 container_of(vm, struct i915_hw_ppgtt, base); 812 container_of(vm, struct i915_hw_ppgtt, base);
813 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page), 813 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
814 I915_CACHE_LLC, use_scratch); 814 I915_CACHE_LLC, use_scratch);
815 815
816 if (!USES_FULL_48BIT_PPGTT(vm->dev)) { 816 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
817 gen8_ppgtt_clear_pte_range(vm, &ppgtt->pdp, start, length, 817 gen8_ppgtt_clear_pte_range(vm, &ppgtt->pdp, start, length,
818 scratch_pte); 818 scratch_pte);
819 } else { 819 } else {
820 uint64_t templ4, pml4e; 820 uint64_t templ4, pml4e;
821 struct i915_page_directory_pointer *pdp; 821 struct i915_page_directory_pointer *pdp;
822 822
823 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, templ4, pml4e) { 823 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, templ4, pml4e) {
824 gen8_ppgtt_clear_pte_range(vm, pdp, start, length, 824 gen8_ppgtt_clear_pte_range(vm, pdp, start, length,
825 scratch_pte); 825 scratch_pte);
826 } 826 }
827 } 827 }
828} 828}
829 829
830static void 830static void
831gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm, 831gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
832 struct i915_page_directory_pointer *pdp, 832 struct i915_page_directory_pointer *pdp,
833 struct sg_page_iter *sg_iter, 833 struct sg_page_iter *sg_iter,
834 uint64_t start, 834 uint64_t start,
835 enum i915_cache_level cache_level) 835 enum i915_cache_level cache_level)
836{ 836{
837 struct i915_hw_ppgtt *ppgtt = 837 struct i915_hw_ppgtt *ppgtt =
838 container_of(vm, struct i915_hw_ppgtt, base); 838 container_of(vm, struct i915_hw_ppgtt, base);
839 gen8_pte_t *pt_vaddr; 839 gen8_pte_t *pt_vaddr;
840 unsigned pdpe = gen8_pdpe_index(start); 840 unsigned pdpe = gen8_pdpe_index(start);
841 unsigned pde = gen8_pde_index(start); 841 unsigned pde = gen8_pde_index(start);
842 unsigned pte = gen8_pte_index(start); 842 unsigned pte = gen8_pte_index(start);
843 843
844 pt_vaddr = NULL; 844 pt_vaddr = NULL;
845 845
846 while (__sg_page_iter_next(sg_iter)) { 846 while (__sg_page_iter_next(sg_iter)) {
847 if (pt_vaddr == NULL) { 847 if (pt_vaddr == NULL) {
848 struct i915_page_directory *pd = pdp->page_directory[pdpe]; 848 struct i915_page_directory *pd = pdp->page_directory[pdpe];
849 struct i915_page_table *pt = pd->page_table[pde]; 849 struct i915_page_table *pt = pd->page_table[pde];
850 pt_vaddr = kmap_px(pt); 850 pt_vaddr = kmap_px(pt);
851 } 851 }
852 852
853 pt_vaddr[pte] = 853 pt_vaddr[pte] =
854 gen8_pte_encode(sg_page_iter_dma_address(sg_iter), 854 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
855 cache_level, true); 855 cache_level, true);
856 if (++pte == GEN8_PTES) { 856 if (++pte == GEN8_PTES) {
857 kunmap_px(ppgtt, pt_vaddr); 857 kunmap_px(ppgtt, pt_vaddr);
858 pt_vaddr = NULL; 858 pt_vaddr = NULL;
859 if (++pde == I915_PDES) { 859 if (++pde == I915_PDES) {
860 if (++pdpe == I915_PDPES_PER_PDP(vm->dev)) 860 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
861 break; 861 break;
862 pde = 0; 862 pde = 0;
863 } 863 }
864 pte = 0; 864 pte = 0;
865 } 865 }
866 } 866 }
867 867
868 if (pt_vaddr) 868 if (pt_vaddr)
869 kunmap_px(ppgtt, pt_vaddr); 869 kunmap_px(ppgtt, pt_vaddr);
870} 870}
871 871
872static void gen8_ppgtt_insert_entries(struct i915_address_space *vm, 872static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
873 struct sg_table *pages, 873 struct sg_table *pages,
874 uint64_t start, 874 uint64_t start,
875 enum i915_cache_level cache_level, 875 enum i915_cache_level cache_level,
876 u32 unused) 876 u32 unused)
877{ 877{
878 struct i915_hw_ppgtt *ppgtt = 878 struct i915_hw_ppgtt *ppgtt =
879 container_of(vm, struct i915_hw_ppgtt, base); 879 container_of(vm, struct i915_hw_ppgtt, base);
880 struct sg_page_iter sg_iter; 880 struct sg_page_iter sg_iter;
881 881
882 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0); 882 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
883 883
884 if (!USES_FULL_48BIT_PPGTT(vm->dev)) { 884 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
885 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start, 885 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
886 cache_level); 886 cache_level);
887 } else { 887 } else {
888 struct i915_page_directory_pointer *pdp; 888 struct i915_page_directory_pointer *pdp;
889 uint64_t templ4, pml4e; 889 uint64_t templ4, pml4e;
890 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT; 890 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
891 891
892 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, templ4, pml4e) { 892 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, templ4, pml4e) {
893 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter, 893 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
894 start, cache_level); 894 start, cache_level);
895 } 895 }
896 } 896 }
897} 897}
898 898
899static void gen8_free_page_tables(struct drm_device *dev, 899static void gen8_free_page_tables(struct drm_device *dev,
900 struct i915_page_directory *pd) 900 struct i915_page_directory *pd)
901{ 901{
902 int i; 902 int i;
903 903
904 if (!px_page(pd)) 904 if (!px_page(pd))
905 return; 905 return;
906 906
907 for_each_set_bit(i, pd->used_pdes, I915_PDES) { 907 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
908 if (WARN_ON(!pd->page_table[i])) 908 if (WARN_ON(!pd->page_table[i]))
909 continue; 909 continue;
910 910
911 free_pt(dev, pd->page_table[i]); 911 free_pt(dev, pd->page_table[i]);
912 pd->page_table[i] = NULL; 912 pd->page_table[i] = NULL;
913 } 913 }
914} 914}
915 915
916static int gen8_init_scratch(struct i915_address_space *vm) 916static int gen8_init_scratch(struct i915_address_space *vm)
917{ 917{
918 struct drm_device *dev = vm->dev; 918 struct drm_device *dev = vm->dev;
919 919
920 vm->scratch_page = alloc_scratch_page(dev); 920 vm->scratch_page = alloc_scratch_page(dev);
921 if (IS_ERR(vm->scratch_page)) 921 if (IS_ERR(vm->scratch_page))
922 return PTR_ERR(vm->scratch_page); 922 return PTR_ERR(vm->scratch_page);
923 923
924 vm->scratch_pt = alloc_pt(dev); 924 vm->scratch_pt = alloc_pt(dev);
925 if (IS_ERR(vm->scratch_pt)) { 925 if (IS_ERR(vm->scratch_pt)) {
926 free_scratch_page(dev, vm->scratch_page); 926 free_scratch_page(dev, vm->scratch_page);
927 return PTR_ERR(vm->scratch_pt); 927 return PTR_ERR(vm->scratch_pt);
928 } 928 }
929 929
930 vm->scratch_pd = alloc_pd(dev); 930 vm->scratch_pd = alloc_pd(dev);
931 if (IS_ERR(vm->scratch_pd)) { 931 if (IS_ERR(vm->scratch_pd)) {
932 free_pt(dev, vm->scratch_pt); 932 free_pt(dev, vm->scratch_pt);
933 free_scratch_page(dev, vm->scratch_page); 933 free_scratch_page(dev, vm->scratch_page);
934 return PTR_ERR(vm->scratch_pd); 934 return PTR_ERR(vm->scratch_pd);
935 } 935 }
936 936
937 if (USES_FULL_48BIT_PPGTT(dev)) { 937 if (USES_FULL_48BIT_PPGTT(dev)) {
938 vm->scratch_pdp = alloc_pdp(dev); 938 vm->scratch_pdp = alloc_pdp(dev);
939 if (IS_ERR(vm->scratch_pdp)) { 939 if (IS_ERR(vm->scratch_pdp)) {
940 free_pd(dev, vm->scratch_pd); 940 free_pd(dev, vm->scratch_pd);
941 free_pt(dev, vm->scratch_pt); 941 free_pt(dev, vm->scratch_pt);
942 free_scratch_page(dev, vm->scratch_page); 942 free_scratch_page(dev, vm->scratch_page);
943 return PTR_ERR(vm->scratch_pdp); 943 return PTR_ERR(vm->scratch_pdp);
944 } 944 }
945 } 945 }
946 946
947 gen8_initialize_pt(vm, vm->scratch_pt); 947 gen8_initialize_pt(vm, vm->scratch_pt);
948 gen8_initialize_pd(vm, vm->scratch_pd); 948 gen8_initialize_pd(vm, vm->scratch_pd);
949 if (USES_FULL_48BIT_PPGTT(dev)) 949 if (USES_FULL_48BIT_PPGTT(dev))
950 gen8_initialize_pdp(vm, vm->scratch_pdp); 950 gen8_initialize_pdp(vm, vm->scratch_pdp);
951 951
952 return 0; 952 return 0;
953} 953}
954 954
955static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create) 955static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
956{ 956{
957 enum vgt_g2v_type msg; 957 enum vgt_g2v_type msg;
958 struct drm_device *dev = ppgtt->base.dev; 958 struct drm_device *dev = ppgtt->base.dev;
959 struct drm_i915_private *dev_priv = dev->dev_private; 959 struct drm_i915_private *dev_priv = dev->dev_private;
960 unsigned int offset = vgtif_reg(pdp0_lo); 960 unsigned int offset = vgtif_reg(pdp0_lo);
961 int i; 961 int i;
962 962
963 if (USES_FULL_48BIT_PPGTT(dev)) { 963 if (USES_FULL_48BIT_PPGTT(dev)) {
964 u64 daddr = px_dma(&ppgtt->pml4); 964 u64 daddr = px_dma(&ppgtt->pml4);
965 965
966 I915_WRITE(offset, lower_32_bits(daddr)); 966 I915_WRITE(offset, lower_32_bits(daddr));
967 I915_WRITE(offset + 4, upper_32_bits(daddr)); 967 I915_WRITE(offset + 4, upper_32_bits(daddr));
968 968
969 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE : 969 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
970 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY); 970 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
971 } else { 971 } else {
972 for (i = 0; i < GEN8_LEGACY_PDPES; i++) { 972 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
973 u64 daddr = i915_page_dir_dma_addr(ppgtt, i); 973 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
974 974
975 I915_WRITE(offset, lower_32_bits(daddr)); 975 I915_WRITE(offset, lower_32_bits(daddr));
976 I915_WRITE(offset + 4, upper_32_bits(daddr)); 976 I915_WRITE(offset + 4, upper_32_bits(daddr));
977 977
978 offset += 8; 978 offset += 8;
979 } 979 }
980 980
981 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE : 981 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
982 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY); 982 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
983 } 983 }
984 984
985 I915_WRITE(vgtif_reg(g2v_notify), msg); 985 I915_WRITE(vgtif_reg(g2v_notify), msg);
986 986
987 return 0; 987 return 0;
988} 988}
989 989
990static void gen8_free_scratch(struct i915_address_space *vm) 990static void gen8_free_scratch(struct i915_address_space *vm)
991{ 991{
992 struct drm_device *dev = vm->dev; 992 struct drm_device *dev = vm->dev;
993 993
994 if (USES_FULL_48BIT_PPGTT(dev)) 994 if (USES_FULL_48BIT_PPGTT(dev))
995 free_pdp(dev, vm->scratch_pdp); 995 free_pdp(dev, vm->scratch_pdp);
996 free_pd(dev, vm->scratch_pd); 996 free_pd(dev, vm->scratch_pd);
997 free_pt(dev, vm->scratch_pt); 997 free_pt(dev, vm->scratch_pt);
998 free_scratch_page(dev, vm->scratch_page); 998 free_scratch_page(dev, vm->scratch_page);
999} 999}
1000 1000
1001static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev, 1001static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
1002 struct i915_page_directory_pointer *pdp) 1002 struct i915_page_directory_pointer *pdp)
1003{ 1003{
1004 int i; 1004 int i;
1005 1005
1006 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) { 1006 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
1007 if (WARN_ON(!pdp->page_directory[i])) 1007 if (WARN_ON(!pdp->page_directory[i]))
1008 continue; 1008 continue;
1009 1009
1010 gen8_free_page_tables(dev, pdp->page_directory[i]); 1010 gen8_free_page_tables(dev, pdp->page_directory[i]);
1011 free_pd(dev, pdp->page_directory[i]); 1011 free_pd(dev, pdp->page_directory[i]);
1012 } 1012 }
1013 1013
1014 free_pdp(dev, pdp); 1014 free_pdp(dev, pdp);
1015} 1015}
1016 1016
1017static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt) 1017static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1018{ 1018{
1019 int i; 1019 int i;
1020 1020
1021 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) { 1021 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1022 if (WARN_ON(!ppgtt->pml4.pdps[i])) 1022 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1023 continue; 1023 continue;
1024 1024
1025 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]); 1025 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
1026 } 1026 }
1027 1027
1028 cleanup_px(ppgtt->base.dev, &ppgtt->pml4); 1028 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
1029} 1029}
1030 1030
1031static void gen8_ppgtt_cleanup(struct i915_address_space *vm) 1031static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1032{ 1032{
1033 struct i915_hw_ppgtt *ppgtt = 1033 struct i915_hw_ppgtt *ppgtt =
1034 container_of(vm, struct i915_hw_ppgtt, base); 1034 container_of(vm, struct i915_hw_ppgtt, base);
1035 1035
1036 if (intel_vgpu_active(vm->dev)) 1036 if (intel_vgpu_active(vm->dev))
1037 gen8_ppgtt_notify_vgt(ppgtt, false); 1037 gen8_ppgtt_notify_vgt(ppgtt, false);
1038 1038
1039 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) 1039 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1040 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp); 1040 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1041 else 1041 else
1042 gen8_ppgtt_cleanup_4lvl(ppgtt); 1042 gen8_ppgtt_cleanup_4lvl(ppgtt);
1043 1043
1044 gen8_free_scratch(vm); 1044 gen8_free_scratch(vm);
1045} 1045}
1046 1046
1047/** 1047/**
1048 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range. 1048 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
1049 * @vm: Master vm structure. 1049 * @vm: Master vm structure.
1050 * @pd: Page directory for this address range. 1050 * @pd: Page directory for this address range.
1051 * @start: Starting virtual address to begin allocations. 1051 * @start: Starting virtual address to begin allocations.
1052 * @length: Size of the allocations. 1052 * @length: Size of the allocations.
1053 * @new_pts: Bitmap set by function with new allocations. Likely used by the 1053 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1054 * caller to free on error. 1054 * caller to free on error.
1055 * 1055 *
1056 * Allocate the required number of page tables. Extremely similar to 1056 * Allocate the required number of page tables. Extremely similar to
1057 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by 1057 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1058 * the page directory boundary (instead of the page directory pointer). That 1058 * the page directory boundary (instead of the page directory pointer). That
1059 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is 1059 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1060 * possible, and likely that the caller will need to use multiple calls of this 1060 * possible, and likely that the caller will need to use multiple calls of this
1061 * function to achieve the appropriate allocation. 1061 * function to achieve the appropriate allocation.
1062 * 1062 *
1063 * Return: 0 if success; negative error code otherwise. 1063 * Return: 0 if success; negative error code otherwise.
1064 */ 1064 */
1065static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm, 1065static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
1066 struct i915_page_directory *pd, 1066 struct i915_page_directory *pd,
1067 uint64_t start, 1067 uint64_t start,
1068 uint64_t length, 1068 uint64_t length,
1069 unsigned long *new_pts) 1069 unsigned long *new_pts)
1070{ 1070{
1071 struct drm_device *dev = vm->dev; 1071 struct drm_device *dev = vm->dev;
1072 struct i915_page_table *pt; 1072 struct i915_page_table *pt;
1073 uint64_t temp; 1073 uint64_t temp;
1074 uint32_t pde; 1074 uint32_t pde;
1075 1075
1076 gen8_for_each_pde(pt, pd, start, length, temp, pde) { 1076 gen8_for_each_pde(pt, pd, start, length, temp, pde) {
1077 /* Don't reallocate page tables */ 1077 /* Don't reallocate page tables */
1078 if (test_bit(pde, pd->used_pdes)) { 1078 if (test_bit(pde, pd->used_pdes)) {
1079 /* Scratch is never allocated this way */ 1079 /* Scratch is never allocated this way */
1080 WARN_ON(pt == vm->scratch_pt); 1080 WARN_ON(pt == vm->scratch_pt);
1081 continue; 1081 continue;
1082 } 1082 }
1083 1083
1084 pt = alloc_pt(dev); 1084 pt = alloc_pt(dev);
1085 if (IS_ERR(pt)) 1085 if (IS_ERR(pt))
1086 goto unwind_out; 1086 goto unwind_out;
1087 1087
1088 gen8_initialize_pt(vm, pt); 1088 gen8_initialize_pt(vm, pt);
1089 pd->page_table[pde] = pt; 1089 pd->page_table[pde] = pt;
1090 __set_bit(pde, new_pts); 1090 __set_bit(pde, new_pts);
1091 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT); 1091 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
1092 } 1092 }
1093 1093
1094 return 0; 1094 return 0;
1095 1095
1096unwind_out: 1096unwind_out:
1097 for_each_set_bit(pde, new_pts, I915_PDES) 1097 for_each_set_bit(pde, new_pts, I915_PDES)
1098 free_pt(dev, pd->page_table[pde]); 1098 free_pt(dev, pd->page_table[pde]);
1099 1099
1100 return -ENOMEM; 1100 return -ENOMEM;
1101} 1101}
1102 1102
1103/** 1103/**
1104 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range. 1104 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
1105 * @vm: Master vm structure. 1105 * @vm: Master vm structure.
1106 * @pdp: Page directory pointer for this address range. 1106 * @pdp: Page directory pointer for this address range.
1107 * @start: Starting virtual address to begin allocations. 1107 * @start: Starting virtual address to begin allocations.
1108 * @length: Size of the allocations. 1108 * @length: Size of the allocations.
1109 * @new_pds: Bitmap set by function with new allocations. Likely used by the 1109 * @new_pds: Bitmap set by function with new allocations. Likely used by the
1110 * caller to free on error. 1110 * caller to free on error.
1111 * 1111 *
1112 * Allocate the required number of page directories starting at the pde index of 1112 * Allocate the required number of page directories starting at the pde index of
1113 * @start, and ending at the pde index @start + @length. This function will skip 1113 * @start, and ending at the pde index @start + @length. This function will skip
1114 * over already allocated page directories within the range, and only allocate 1114 * over already allocated page directories within the range, and only allocate
1115 * new ones, setting the appropriate pointer within the pdp as well as the 1115 * new ones, setting the appropriate pointer within the pdp as well as the
1116 * correct position in the bitmap @new_pds. 1116 * correct position in the bitmap @new_pds.
1117 * 1117 *
1118 * The function will only allocate the pages within the range for a give page 1118 * The function will only allocate the pages within the range for a give page
1119 * directory pointer. In other words, if @start + @length straddles a virtually 1119 * directory pointer. In other words, if @start + @length straddles a virtually
1120 * addressed PDP boundary (512GB for 4k pages), there will be more allocations 1120 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1121 * required by the caller, This is not currently possible, and the BUG in the 1121 * required by the caller, This is not currently possible, and the BUG in the
1122 * code will prevent it. 1122 * code will prevent it.
1123 * 1123 *
1124 * Return: 0 if success; negative error code otherwise. 1124 * Return: 0 if success; negative error code otherwise.
1125 */ 1125 */
1126static int 1126static int
1127gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm, 1127gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1128 struct i915_page_directory_pointer *pdp, 1128 struct i915_page_directory_pointer *pdp,
1129 uint64_t start, 1129 uint64_t start,
1130 uint64_t length, 1130 uint64_t length,
1131 unsigned long *new_pds) 1131 unsigned long *new_pds)
1132{ 1132{
1133 struct drm_device *dev = vm->dev; 1133 struct drm_device *dev = vm->dev;
1134 struct i915_page_directory *pd; 1134 struct i915_page_directory *pd;
1135 uint64_t temp; 1135 uint64_t temp;
1136 uint32_t pdpe; 1136 uint32_t pdpe;
1137 uint32_t pdpes = I915_PDPES_PER_PDP(dev); 1137 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1138 1138
1139 WARN_ON(!bitmap_empty(new_pds, pdpes)); 1139 WARN_ON(!bitmap_empty(new_pds, pdpes));
1140 1140
1141 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) { 1141 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
1142 if (test_bit(pdpe, pdp->used_pdpes)) 1142 if (test_bit(pdpe, pdp->used_pdpes))
1143 continue; 1143 continue;
1144 1144
1145 pd = alloc_pd(dev); 1145 pd = alloc_pd(dev);
1146 if (IS_ERR(pd)) 1146 if (IS_ERR(pd))
1147 goto unwind_out; 1147 goto unwind_out;
1148 1148
1149 gen8_initialize_pd(vm, pd); 1149 gen8_initialize_pd(vm, pd);
1150 pdp->page_directory[pdpe] = pd; 1150 pdp->page_directory[pdpe] = pd;
1151 __set_bit(pdpe, new_pds); 1151 __set_bit(pdpe, new_pds);
1152 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT); 1152 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
1153 } 1153 }
1154 1154
1155 return 0; 1155 return 0;
1156 1156
1157unwind_out: 1157unwind_out:
1158 for_each_set_bit(pdpe, new_pds, pdpes) 1158 for_each_set_bit(pdpe, new_pds, pdpes)
1159 free_pd(dev, pdp->page_directory[pdpe]); 1159 free_pd(dev, pdp->page_directory[pdpe]);
1160 1160
1161 return -ENOMEM; 1161 return -ENOMEM;
1162} 1162}
1163 1163
1164/** 1164/**
1165 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range. 1165 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1166 * @vm: Master vm structure. 1166 * @vm: Master vm structure.
1167 * @pml4: Page map level 4 for this address range. 1167 * @pml4: Page map level 4 for this address range.
1168 * @start: Starting virtual address to begin allocations. 1168 * @start: Starting virtual address to begin allocations.
1169 * @length: Size of the allocations. 1169 * @length: Size of the allocations.
1170 * @new_pdps: Bitmap set by function with new allocations. Likely used by the 1170 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1171 * caller to free on error. 1171 * caller to free on error.
1172 * 1172 *
1173 * Allocate the required number of page directory pointers. Extremely similar to 1173 * Allocate the required number of page directory pointers. Extremely similar to
1174 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs(). 1174 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1175 * The main difference is here we are limited by the pml4 boundary (instead of 1175 * The main difference is here we are limited by the pml4 boundary (instead of
1176 * the page directory pointer). 1176 * the page directory pointer).
1177 * 1177 *
1178 * Return: 0 if success; negative error code otherwise. 1178 * Return: 0 if success; negative error code otherwise.
1179 */ 1179 */
1180static int 1180static int
1181gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm, 1181gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1182 struct i915_pml4 *pml4, 1182 struct i915_pml4 *pml4,
1183 uint64_t start, 1183 uint64_t start,
1184 uint64_t length, 1184 uint64_t length,
1185 unsigned long *new_pdps) 1185 unsigned long *new_pdps)
1186{ 1186{
1187 struct drm_device *dev = vm->dev; 1187 struct drm_device *dev = vm->dev;
1188 struct i915_page_directory_pointer *pdp; 1188 struct i915_page_directory_pointer *pdp;
1189 uint64_t temp; 1189 uint64_t temp;
1190 uint32_t pml4e; 1190 uint32_t pml4e;
1191 1191
1192 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4)); 1192 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1193 1193
1194 gen8_for_each_pml4e(pdp, pml4, start, length, temp, pml4e) { 1194 gen8_for_each_pml4e(pdp, pml4, start, length, temp, pml4e) {
1195 if (!test_bit(pml4e, pml4->used_pml4es)) { 1195 if (!test_bit(pml4e, pml4->used_pml4es)) {
1196 pdp = alloc_pdp(dev); 1196 pdp = alloc_pdp(dev);
1197 if (IS_ERR(pdp)) 1197 if (IS_ERR(pdp))
1198 goto unwind_out; 1198 goto unwind_out;
1199 1199
1200 gen8_initialize_pdp(vm, pdp); 1200 gen8_initialize_pdp(vm, pdp);
1201 pml4->pdps[pml4e] = pdp; 1201 pml4->pdps[pml4e] = pdp;
1202 __set_bit(pml4e, new_pdps); 1202 __set_bit(pml4e, new_pdps);
1203 trace_i915_page_directory_pointer_entry_alloc(vm, 1203 trace_i915_page_directory_pointer_entry_alloc(vm,
1204 pml4e, 1204 pml4e,
1205 start, 1205 start,
1206 GEN8_PML4E_SHIFT); 1206 GEN8_PML4E_SHIFT);
1207 } 1207 }
1208 } 1208 }
1209 1209
1210 return 0; 1210 return 0;
1211 1211
1212unwind_out: 1212unwind_out:
1213 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4) 1213 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1214 free_pdp(dev, pml4->pdps[pml4e]); 1214 free_pdp(dev, pml4->pdps[pml4e]);
1215 1215
1216 return -ENOMEM; 1216 return -ENOMEM;
1217} 1217}
1218 1218
1219static void 1219static void
1220free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts) 1220free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
1221{ 1221{
1222 kfree(new_pts); 1222 kfree(new_pts);
1223 kfree(new_pds); 1223 kfree(new_pds);
1224} 1224}
1225 1225
1226/* Fills in the page directory bitmap, and the array of page tables bitmap. Both 1226/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1227 * of these are based on the number of PDPEs in the system. 1227 * of these are based on the number of PDPEs in the system.
1228 */ 1228 */
1229static 1229static
1230int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds, 1230int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
1231 unsigned long **new_pts, 1231 unsigned long **new_pts,
1232 uint32_t pdpes) 1232 uint32_t pdpes)
1233{ 1233{
1234 unsigned long *pds; 1234 unsigned long *pds;
1235 unsigned long *pts; 1235 unsigned long *pts;
1236 1236
1237 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY); 1237 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
1238 if (!pds) 1238 if (!pds)
1239 return -ENOMEM; 1239 return -ENOMEM;
1240 1240
1241 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long), 1241 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1242 GFP_TEMPORARY); 1242 GFP_TEMPORARY);
1243 if (!pts) 1243 if (!pts)
1244 goto err_out; 1244 goto err_out;
1245 1245
1246 *new_pds = pds; 1246 *new_pds = pds;
1247 *new_pts = pts; 1247 *new_pts = pts;
1248 1248
1249 return 0; 1249 return 0;
1250 1250
1251err_out: 1251err_out:
1252 free_gen8_temp_bitmaps(pds, pts); 1252 free_gen8_temp_bitmaps(pds, pts);
1253 return -ENOMEM; 1253 return -ENOMEM;
1254} 1254}
1255 1255
1256/* PDE TLBs are a pain to invalidate on GEN8+. When we modify 1256/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1257 * the page table structures, we mark them dirty so that 1257 * the page table structures, we mark them dirty so that
1258 * context switching/execlist queuing code takes extra steps 1258 * context switching/execlist queuing code takes extra steps
1259 * to ensure that tlbs are flushed. 1259 * to ensure that tlbs are flushed.
1260 */ 1260 */
1261static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt) 1261static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1262{ 1262{
1263 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask; 1263 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1264} 1264}
1265 1265
1266static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm, 1266static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1267 struct i915_page_directory_pointer *pdp, 1267 struct i915_page_directory_pointer *pdp,
1268 uint64_t start, 1268 uint64_t start,
1269 uint64_t length) 1269 uint64_t length)
1270{ 1270{
1271 struct i915_hw_ppgtt *ppgtt = 1271 struct i915_hw_ppgtt *ppgtt =
1272 container_of(vm, struct i915_hw_ppgtt, base); 1272 container_of(vm, struct i915_hw_ppgtt, base);
1273 unsigned long *new_page_dirs, *new_page_tables; 1273 unsigned long *new_page_dirs, *new_page_tables;
1274 struct drm_device *dev = vm->dev; 1274 struct drm_device *dev = vm->dev;
1275 struct i915_page_directory *pd; 1275 struct i915_page_directory *pd;
1276 const uint64_t orig_start = start; 1276 const uint64_t orig_start = start;
1277 const uint64_t orig_length = length; 1277 const uint64_t orig_length = length;
1278 uint64_t temp; 1278 uint64_t temp;
1279 uint32_t pdpe; 1279 uint32_t pdpe;
1280 uint32_t pdpes = I915_PDPES_PER_PDP(dev); 1280 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1281 int ret; 1281 int ret;
1282 1282
1283 /* Wrap is never okay since we can only represent 48b, and we don't 1283 /* Wrap is never okay since we can only represent 48b, and we don't
1284 * actually use the other side of the canonical address space. 1284 * actually use the other side of the canonical address space.
1285 */ 1285 */
1286 if (WARN_ON(start + length < start)) 1286 if (WARN_ON(start + length < start))
1287 return -ENODEV; 1287 return -ENODEV;
1288 1288
1289 if (WARN_ON(start + length > vm->total)) 1289 if (WARN_ON(start + length > vm->total))
1290 return -ENODEV; 1290 return -ENODEV;
1291 1291
1292 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes); 1292 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1293 if (ret) 1293 if (ret)
1294 return ret; 1294 return ret;
1295 1295
1296 /* Do the allocations first so we can easily bail out */ 1296 /* Do the allocations first so we can easily bail out */
1297 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length, 1297 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1298 new_page_dirs); 1298 new_page_dirs);
1299 if (ret) { 1299 if (ret) {
1300 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables); 1300 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1301 return ret; 1301 return ret;
1302 } 1302 }
1303 1303
1304 /* For every page directory referenced, allocate page tables */ 1304 /* For every page directory referenced, allocate page tables */
1305 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) { 1305 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
1306 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length, 1306 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
1307 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES)); 1307 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
1308 if (ret) 1308 if (ret)
1309 goto err_out; 1309 goto err_out;
1310 } 1310 }
1311 1311
1312 start = orig_start; 1312 start = orig_start;
1313 length = orig_length; 1313 length = orig_length;
1314 1314
1315 /* Allocations have completed successfully, so set the bitmaps, and do 1315 /* Allocations have completed successfully, so set the bitmaps, and do
1316 * the mappings. */ 1316 * the mappings. */
1317 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) { 1317 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
1318 gen8_pde_t *const page_directory = kmap_px(pd); 1318 gen8_pde_t *const page_directory = kmap_px(pd);
1319 struct i915_page_table *pt; 1319 struct i915_page_table *pt;
1320 uint64_t pd_len = length; 1320 uint64_t pd_len = length;
1321 uint64_t pd_start = start; 1321 uint64_t pd_start = start;
1322 uint32_t pde; 1322 uint32_t pde;
1323 1323
1324 /* Every pd should be allocated, we just did that above. */ 1324 /* Every pd should be allocated, we just did that above. */
1325 WARN_ON(!pd); 1325 WARN_ON(!pd);
1326 1326
1327 gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) { 1327 gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
1328 /* Same reasoning as pd */ 1328 /* Same reasoning as pd */
1329 WARN_ON(!pt); 1329 WARN_ON(!pt);
1330 WARN_ON(!pd_len); 1330 WARN_ON(!pd_len);
1331 WARN_ON(!gen8_pte_count(pd_start, pd_len)); 1331 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1332 1332
1333 /* Set our used ptes within the page table */ 1333 /* Set our used ptes within the page table */
1334 bitmap_set(pt->used_ptes, 1334 bitmap_set(pt->used_ptes,
1335 gen8_pte_index(pd_start), 1335 gen8_pte_index(pd_start),
1336 gen8_pte_count(pd_start, pd_len)); 1336 gen8_pte_count(pd_start, pd_len));
1337 1337
1338 /* Our pde is now pointing to the pagetable, pt */ 1338 /* Our pde is now pointing to the pagetable, pt */
1339 __set_bit(pde, pd->used_pdes); 1339 __set_bit(pde, pd->used_pdes);
1340 1340
1341 /* Map the PDE to the page table */ 1341 /* Map the PDE to the page table */
1342 page_directory[pde] = gen8_pde_encode(px_dma(pt), 1342 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1343 I915_CACHE_LLC); 1343 I915_CACHE_LLC);
1344 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt, 1344 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1345 gen8_pte_index(start), 1345 gen8_pte_index(start),
1346 gen8_pte_count(start, length), 1346 gen8_pte_count(start, length),
1347 GEN8_PTES); 1347 GEN8_PTES);
1348 1348
1349 /* NB: We haven't yet mapped ptes to pages. At this 1349 /* NB: We haven't yet mapped ptes to pages. At this
1350 * point we're still relying on insert_entries() */ 1350 * point we're still relying on insert_entries() */
1351 } 1351 }
1352 1352
1353 kunmap_px(ppgtt, page_directory); 1353 kunmap_px(ppgtt, page_directory);
1354 __set_bit(pdpe, pdp->used_pdpes); 1354 __set_bit(pdpe, pdp->used_pdpes);
1355 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe); 1355 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
1356 } 1356 }
1357 1357
1358 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables); 1358 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1359 mark_tlbs_dirty(ppgtt); 1359 mark_tlbs_dirty(ppgtt);
1360 return 0; 1360 return 0;
1361 1361
1362err_out: 1362err_out:
1363 while (pdpe--) { 1363 while (pdpe--) {
1364 for_each_set_bit(temp, new_page_tables + pdpe * 1364 for_each_set_bit(temp, new_page_tables + pdpe *
1365 BITS_TO_LONGS(I915_PDES), I915_PDES) 1365 BITS_TO_LONGS(I915_PDES), I915_PDES)
1366 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]); 1366 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
1367 } 1367 }
1368 1368
1369 for_each_set_bit(pdpe, new_page_dirs, pdpes) 1369 for_each_set_bit(pdpe, new_page_dirs, pdpes)
1370 free_pd(dev, pdp->page_directory[pdpe]); 1370 free_pd(dev, pdp->page_directory[pdpe]);
1371 1371
1372 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables); 1372 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1373 mark_tlbs_dirty(ppgtt); 1373 mark_tlbs_dirty(ppgtt);
1374 return ret; 1374 return ret;
1375} 1375}
1376 1376
1377static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm, 1377static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1378 struct i915_pml4 *pml4, 1378 struct i915_pml4 *pml4,
1379 uint64_t start, 1379 uint64_t start,
1380 uint64_t length) 1380 uint64_t length)
1381{ 1381{
1382 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4); 1382 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
1383 struct i915_hw_ppgtt *ppgtt = 1383 struct i915_hw_ppgtt *ppgtt =
1384 container_of(vm, struct i915_hw_ppgtt, base); 1384 container_of(vm, struct i915_hw_ppgtt, base);
1385 struct i915_page_directory_pointer *pdp; 1385 struct i915_page_directory_pointer *pdp;
1386 uint64_t temp, pml4e; 1386 uint64_t temp, pml4e;
1387 int ret = 0; 1387 int ret = 0;
1388 1388
1389 /* Do the pml4 allocations first, so we don't need to track the newly 1389 /* Do the pml4 allocations first, so we don't need to track the newly
1390 * allocated tables below the pdp */ 1390 * allocated tables below the pdp */
1391 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4); 1391 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1392 1392
1393 /* The pagedirectory and pagetable allocations are done in the shared 3 1393 /* The pagedirectory and pagetable allocations are done in the shared 3
1394 * and 4 level code. Just allocate the pdps. 1394 * and 4 level code. Just allocate the pdps.
1395 */ 1395 */
1396 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length, 1396 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1397 new_pdps); 1397 new_pdps);
1398 if (ret) 1398 if (ret)
1399 return ret; 1399 return ret;
1400 1400
1401 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2, 1401 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1402 "The allocation has spanned more than 512GB. " 1402 "The allocation has spanned more than 512GB. "
1403 "It is highly likely this is incorrect."); 1403 "It is highly likely this is incorrect.");
1404 1404
1405 gen8_for_each_pml4e(pdp, pml4, start, length, temp, pml4e) { 1405 gen8_for_each_pml4e(pdp, pml4, start, length, temp, pml4e) {
1406 WARN_ON(!pdp); 1406 WARN_ON(!pdp);
1407 1407
1408 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length); 1408 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1409 if (ret) 1409 if (ret)
1410 goto err_out; 1410 goto err_out;
1411 1411
1412 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e); 1412 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1413 } 1413 }
1414 1414
1415 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es, 1415 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1416 GEN8_PML4ES_PER_PML4); 1416 GEN8_PML4ES_PER_PML4);
1417 1417
1418 return 0; 1418 return 0;
1419 1419
1420err_out: 1420err_out:
1421 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4) 1421 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1422 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]); 1422 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1423 1423
1424 return ret; 1424 return ret;
1425} 1425}
1426 1426
1427static int gen8_alloc_va_range(struct i915_address_space *vm, 1427static int gen8_alloc_va_range(struct i915_address_space *vm,
1428 uint64_t start, uint64_t length) 1428 uint64_t start, uint64_t length)
1429{ 1429{
1430 struct i915_hw_ppgtt *ppgtt = 1430 struct i915_hw_ppgtt *ppgtt =
1431 container_of(vm, struct i915_hw_ppgtt, base); 1431 container_of(vm, struct i915_hw_ppgtt, base);
1432 1432
1433 if (USES_FULL_48BIT_PPGTT(vm->dev)) 1433 if (USES_FULL_48BIT_PPGTT(vm->dev))
1434 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length); 1434 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1435 else 1435 else
1436 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length); 1436 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1437} 1437}
1438 1438
1439static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp, 1439static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1440 uint64_t start, uint64_t length, 1440 uint64_t start, uint64_t length,
1441 gen8_pte_t scratch_pte, 1441 gen8_pte_t scratch_pte,
1442 struct seq_file *m) 1442 struct seq_file *m)
1443{ 1443{
1444 struct i915_page_directory *pd; 1444 struct i915_page_directory *pd;
1445 uint64_t temp; 1445 uint64_t temp;
1446 uint32_t pdpe; 1446 uint32_t pdpe;
1447 1447
1448 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) { 1448 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
1449 struct i915_page_table *pt; 1449 struct i915_page_table *pt;
1450 uint64_t pd_len = length; 1450 uint64_t pd_len = length;
1451 uint64_t pd_start = start; 1451 uint64_t pd_start = start;
1452 uint32_t pde; 1452 uint32_t pde;
1453 1453
1454 if (!test_bit(pdpe, pdp->used_pdpes)) 1454 if (!test_bit(pdpe, pdp->used_pdpes))
1455 continue; 1455 continue;
1456 1456
1457 seq_printf(m, "\tPDPE #%d\n", pdpe); 1457 seq_printf(m, "\tPDPE #%d\n", pdpe);
1458 gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) { 1458 gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
1459 uint32_t pte; 1459 uint32_t pte;
1460 gen8_pte_t *pt_vaddr; 1460 gen8_pte_t *pt_vaddr;
1461 1461
1462 if (!test_bit(pde, pd->used_pdes)) 1462 if (!test_bit(pde, pd->used_pdes))
1463 continue; 1463 continue;
1464 1464
1465 pt_vaddr = kmap_px(pt); 1465 pt_vaddr = kmap_px(pt);
1466 for (pte = 0; pte < GEN8_PTES; pte += 4) { 1466 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1467 uint64_t va = 1467 uint64_t va =
1468 (pdpe << GEN8_PDPE_SHIFT) | 1468 (pdpe << GEN8_PDPE_SHIFT) |
1469 (pde << GEN8_PDE_SHIFT) | 1469 (pde << GEN8_PDE_SHIFT) |
1470 (pte << GEN8_PTE_SHIFT); 1470 (pte << GEN8_PTE_SHIFT);
1471 int i; 1471 int i;
1472 bool found = false; 1472 bool found = false;
1473 1473
1474 for (i = 0; i < 4; i++) 1474 for (i = 0; i < 4; i++)
1475 if (pt_vaddr[pte + i] != scratch_pte) 1475 if (pt_vaddr[pte + i] != scratch_pte)
1476 found = true; 1476 found = true;
1477 if (!found) 1477 if (!found)
1478 continue; 1478 continue;
1479 1479
1480 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte); 1480 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1481 for (i = 0; i < 4; i++) { 1481 for (i = 0; i < 4; i++) {
1482 if (pt_vaddr[pte + i] != scratch_pte) 1482 if (pt_vaddr[pte + i] != scratch_pte)
1483 seq_printf(m, " %llx", pt_vaddr[pte + i]); 1483 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1484 else 1484 else
1485 seq_puts(m, " SCRATCH "); 1485 seq_puts(m, " SCRATCH ");
1486 } 1486 }
1487 seq_puts(m, "\n"); 1487 seq_puts(m, "\n");
1488 } 1488 }
1489 /* don't use kunmap_px, it could trigger 1489 /* don't use kunmap_px, it could trigger
1490 * an unnecessary flush. 1490 * an unnecessary flush.
1491 */ 1491 */
1492 kunmap_atomic(pt_vaddr); 1492 kunmap_atomic(pt_vaddr);
1493 } 1493 }
1494 } 1494 }
1495} 1495}
1496 1496
1497static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m) 1497static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1498{ 1498{
1499 struct i915_address_space *vm = &ppgtt->base; 1499 struct i915_address_space *vm = &ppgtt->base;
1500 uint64_t start = ppgtt->base.start; 1500 uint64_t start = ppgtt->base.start;
1501 uint64_t length = ppgtt->base.total; 1501 uint64_t length = ppgtt->base.total;
1502 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page), 1502 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
1503 I915_CACHE_LLC, true); 1503 I915_CACHE_LLC, true);
1504 1504
1505 if (!USES_FULL_48BIT_PPGTT(vm->dev)) { 1505 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1506 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m); 1506 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1507 } else { 1507 } else {
1508 uint64_t templ4, pml4e; 1508 uint64_t templ4, pml4e;
1509 struct i915_pml4 *pml4 = &ppgtt->pml4; 1509 struct i915_pml4 *pml4 = &ppgtt->pml4;
1510 struct i915_page_directory_pointer *pdp; 1510 struct i915_page_directory_pointer *pdp;
1511 1511
1512 gen8_for_each_pml4e(pdp, pml4, start, length, templ4, pml4e) { 1512 gen8_for_each_pml4e(pdp, pml4, start, length, templ4, pml4e) {
1513 if (!test_bit(pml4e, pml4->used_pml4es)) 1513 if (!test_bit(pml4e, pml4->used_pml4es))
1514 continue; 1514 continue;
1515 1515
1516 seq_printf(m, " PML4E #%llu\n", pml4e); 1516 seq_printf(m, " PML4E #%llu\n", pml4e);
1517 gen8_dump_pdp(pdp, start, length, scratch_pte, m); 1517 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1518 } 1518 }
1519 } 1519 }
1520} 1520}
1521 1521
1522static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt) 1522static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1523{ 1523{
1524 unsigned long *new_page_dirs, *new_page_tables; 1524 unsigned long *new_page_dirs, *new_page_tables;
1525 uint32_t pdpes = I915_PDPES_PER_PDP(dev); 1525 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1526 int ret; 1526 int ret;
1527 1527
1528 /* We allocate temp bitmap for page tables for no gain 1528 /* We allocate temp bitmap for page tables for no gain
1529 * but as this is for init only, lets keep the things simple 1529 * but as this is for init only, lets keep the things simple
1530 */ 1530 */
1531 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes); 1531 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1532 if (ret) 1532 if (ret)
1533 return ret; 1533 return ret;
1534 1534
1535 /* Allocate for all pdps regardless of how the ppgtt 1535 /* Allocate for all pdps regardless of how the ppgtt
1536 * was defined. 1536 * was defined.
1537 */ 1537 */
1538 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp, 1538 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1539 0, 1ULL << 32, 1539 0, 1ULL << 32,
1540 new_page_dirs); 1540 new_page_dirs);
1541 if (!ret) 1541 if (!ret)
1542 *ppgtt->pdp.used_pdpes = *new_page_dirs; 1542 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1543 1543
1544 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables); 1544 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1545 1545
1546 return ret; 1546 return ret;
1547} 1547}
1548 1548
1549/* 1549/*
1550 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers 1550 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1551 * with a net effect resembling a 2-level page table in normal x86 terms. Each 1551 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1552 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address 1552 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1553 * space. 1553 * space.
1554 * 1554 *
1555 */ 1555 */
1556static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt) 1556static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1557{ 1557{
1558 int ret; 1558 int ret;
1559 1559
1560 ret = gen8_init_scratch(&ppgtt->base); 1560 ret = gen8_init_scratch(&ppgtt->base);
1561 if (ret) 1561 if (ret)
1562 return ret; 1562 return ret;
1563 1563
1564 ppgtt->base.start = 0; 1564 ppgtt->base.start = 0;
1565 ppgtt->base.cleanup = gen8_ppgtt_cleanup; 1565 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
1566 ppgtt->base.allocate_va_range = gen8_alloc_va_range; 1566 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
1567 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries; 1567 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
1568 ppgtt->base.clear_range = gen8_ppgtt_clear_range; 1568 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
1569 ppgtt->base.unbind_vma = ppgtt_unbind_vma; 1569 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1570 ppgtt->base.bind_vma = ppgtt_bind_vma; 1570 ppgtt->base.bind_vma = ppgtt_bind_vma;
1571 ppgtt->debug_dump = gen8_dump_ppgtt; 1571 ppgtt->debug_dump = gen8_dump_ppgtt;
1572 1572
1573 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) { 1573 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1574 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4); 1574 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1575 if (ret) 1575 if (ret)
1576 goto free_scratch; 1576 goto free_scratch;
1577 1577
1578 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4); 1578 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1579 1579
1580 ppgtt->base.total = 1ULL << 48; 1580 ppgtt->base.total = 1ULL << 48;
1581 ppgtt->switch_mm = gen8_48b_mm_switch; 1581 ppgtt->switch_mm = gen8_48b_mm_switch;
1582 } else { 1582 } else {
1583 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp); 1583 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
1584 if (ret) 1584 if (ret)
1585 goto free_scratch; 1585 goto free_scratch;
1586 1586
1587 ppgtt->base.total = 1ULL << 32; 1587 ppgtt->base.total = 1ULL << 32;
1588 ppgtt->switch_mm = gen8_legacy_mm_switch; 1588 ppgtt->switch_mm = gen8_legacy_mm_switch;
1589 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base, 1589 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1590 0, 0, 1590 0, 0,
1591 GEN8_PML4E_SHIFT); 1591 GEN8_PML4E_SHIFT);
1592 1592
1593 if (intel_vgpu_active(ppgtt->base.dev)) { 1593 if (intel_vgpu_active(ppgtt->base.dev)) {
1594 ret = gen8_preallocate_top_level_pdps(ppgtt); 1594 ret = gen8_preallocate_top_level_pdps(ppgtt);
1595 if (ret) 1595 if (ret)
1596 goto free_scratch; 1596 goto free_scratch;
1597 } 1597 }
1598 } 1598 }
1599 1599
1600 if (intel_vgpu_active(ppgtt->base.dev)) 1600 if (intel_vgpu_active(ppgtt->base.dev))
1601 gen8_ppgtt_notify_vgt(ppgtt, true); 1601 gen8_ppgtt_notify_vgt(ppgtt, true);
1602 1602
1603 return 0; 1603 return 0;
1604 1604
1605free_scratch: 1605free_scratch:
1606 gen8_free_scratch(&ppgtt->base); 1606 gen8_free_scratch(&ppgtt->base);
1607 return ret; 1607 return ret;
1608} 1608}
1609#endif 1609#endif
1610 1610
1611#ifndef __NetBSD__ 1611#ifndef __NetBSD__
1612static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m) 1612static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1613{ 1613{
1614 struct i915_address_space *vm = &ppgtt->base; 1614 struct i915_address_space *vm = &ppgtt->base;
1615 struct i915_page_table *unused; 1615 struct i915_page_table *unused;
1616 gen6_pte_t scratch_pte; 1616 gen6_pte_t scratch_pte;
1617 uint32_t pd_entry; 1617 uint32_t pd_entry;
1618 uint32_t pte, pde, temp; 1618 uint32_t pte, pde, temp;
1619 uint32_t start = ppgtt->base.start, length = ppgtt->base.total; 1619 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
1620 1620
1621 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page), 1621 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1622 I915_CACHE_LLC, true, 0); 1622 I915_CACHE_LLC, true, 0);
1623 1623
1624 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) { 1624 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
1625 u32 expected; 1625 u32 expected;
1626 gen6_pte_t *pt_vaddr; 1626 gen6_pte_t *pt_vaddr;
1627 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]); 1627 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
1628 pd_entry = readl(ppgtt->pd_addr + pde); 1628 pd_entry = readl(ppgtt->pd_addr + pde);
1629 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID); 1629 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1630 1630
1631 if (pd_entry != expected) 1631 if (pd_entry != expected)
1632 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n", 1632 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1633 pde, 1633 pde,
1634 pd_entry, 1634 pd_entry,
1635 expected); 1635 expected);
1636 seq_printf(m, "\tPDE: %x\n", pd_entry); 1636 seq_printf(m, "\tPDE: %x\n", pd_entry);
1637 1637
1638 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]); 1638 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1639 1639
1640 for (pte = 0; pte < GEN6_PTES; pte+=4) { 1640 for (pte = 0; pte < GEN6_PTES; pte+=4) {
1641 unsigned long va = 1641 unsigned long va =
1642 (pde * PAGE_SIZE * GEN6_PTES) + 1642 (pde * PAGE_SIZE * GEN6_PTES) +
1643 (pte * PAGE_SIZE); 1643 (pte * PAGE_SIZE);
1644 int i; 1644 int i;
1645 bool found = false; 1645 bool found = false;
1646 for (i = 0; i < 4; i++) 1646 for (i = 0; i < 4; i++)
1647 if (pt_vaddr[pte + i] != scratch_pte) 1647 if (pt_vaddr[pte + i] != scratch_pte)
1648 found = true; 1648 found = true;
1649 if (!found) 1649 if (!found)
1650 continue; 1650 continue;
1651 1651
1652 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte); 1652 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1653 for (i = 0; i < 4; i++) { 1653 for (i = 0; i < 4; i++) {
1654 if (pt_vaddr[pte + i] != scratch_pte) 1654 if (pt_vaddr[pte + i] != scratch_pte)
1655 seq_printf(m, " %08x", pt_vaddr[pte + i]); 1655 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1656 else 1656 else
1657 seq_puts(m, " SCRATCH "); 1657 seq_puts(m, " SCRATCH ");
1658 } 1658 }
1659 seq_puts(m, "\n"); 1659 seq_puts(m, "\n");
1660 } 1660 }
1661 kunmap_px(ppgtt, pt_vaddr); 1661 kunmap_px(ppgtt, pt_vaddr);
1662 } 1662 }
1663} 1663}
1664#endif 1664#endif
1665 1665
1666/* Write pde (index) from the page directory @pd to the page table @pt */ 1666/* Write pde (index) from the page directory @pd to the page table @pt */
1667static void gen6_write_pde(struct i915_page_directory *pd, 1667static void gen6_write_pde(struct i915_page_directory *pd,
1668 const int pde, struct i915_page_table *pt) 1668 const int pde, struct i915_page_table *pt)
1669{ 1669{
1670 /* Caller needs to make sure the write completes if necessary */ 1670 /* Caller needs to make sure the write completes if necessary */
1671 struct i915_hw_ppgtt *ppgtt = 1671 struct i915_hw_ppgtt *ppgtt =
1672 container_of(pd, struct i915_hw_ppgtt, pd); 1672 container_of(pd, struct i915_hw_ppgtt, pd);
1673#ifdef __NetBSD__ 1673#ifdef __NetBSD__
1674 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private; 1674 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
1675 const bus_space_tag_t bst = dev_priv->gtt.bst; 1675 const bus_space_tag_t bst = dev_priv->gtt.bst;
1676 const bus_space_handle_t bsh = dev_priv->gtt.bsh; 1676 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
1677 const bus_addr_t pd_base = ppgtt->pd.base.ggtt_offset; 1677 const bus_addr_t pd_base = ppgtt->pd.base.ggtt_offset;
1678#endif 1678#endif
1679 u32 pd_entry; 1679 u32 pd_entry;
1680 1680
1681 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt)); 1681 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
1682 pd_entry |= GEN6_PDE_VALID; 1682 pd_entry |= GEN6_PDE_VALID;
1683 1683
1684#ifdef __NetBSD__ 1684#ifdef __NetBSD__
1685 bus_space_write_4(bst, bsh, pd_base + pde, pd_entry); 1685 bus_space_write_4(bst, bsh, pd_base + pde, pd_entry);
1686#else 1686#else
1687 writel(pd_entry, ppgtt->pd_addr + pde); 1687 writel(pd_entry, ppgtt->pd_addr + pde);
1688#endif 1688#endif
1689 
1690} 1689}
1691 1690
1692/* Write all the page tables found in the ppgtt structure to incrementing page 1691/* Write all the page tables found in the ppgtt structure to incrementing page
1693 * directories. */ 1692 * directories. */
1694static void gen6_write_page_range(struct drm_i915_private *dev_priv, 1693static void gen6_write_page_range(struct drm_i915_private *dev_priv,
1695 struct i915_page_directory *pd, 1694 struct i915_page_directory *pd,
1696 uint32_t start, uint32_t length) 1695 uint32_t start, uint32_t length)
1697{ 1696{
1698 struct i915_page_table *pt; 1697 struct i915_page_table *pt;
1699 uint32_t pde, temp; 1698 uint32_t pde, temp;
1700 1699
1701 gen6_for_each_pde(pt, pd, start, length, temp, pde) 1700 gen6_for_each_pde(pt, pd, start, length, temp, pde)
1702 gen6_write_pde(pd, pde, pt); 1701 gen6_write_pde(pd, pde, pt);
1703 1702
1704 /* Make sure write is complete before other code can use this page 1703 /* Make sure write is complete before other code can use this page
1705 * table. Also require for WC mapped PTEs */ 1704 * table. Also require for WC mapped PTEs */
1706#ifdef __NetBSD__ 1705#ifdef __NetBSD__
1707 bus_space_read_4(dev_priv->gtt.bst, dev_priv->gtt.bsh, 0); 1706 bus_space_read_4(dev_priv->gtt.bst, dev_priv->gtt.bsh, 0);
1708#else 1707#else
1709 readl(dev_priv->gtt.gsm); 1708 readl(dev_priv->gtt.gsm);
1710#endif 1709#endif
1711} 1710}
1712 1711
1713static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt) 1712static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
1714{ 1713{
1715 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f); 1714 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1716 1715
1717 return (ppgtt->pd.base.ggtt_offset / 64) << 16; 1716 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
1718} 1717}
1719 1718
1720static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt, 1719static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
1721 struct drm_i915_gem_request *req) 1720 struct drm_i915_gem_request *req)
1722{ 1721{
1723 struct intel_engine_cs *ring = req->ring; 1722 struct intel_engine_cs *ring = req->ring;
1724 int ret; 1723 int ret;
1725 1724
1726 /* NB: TLBs must be flushed and invalidated before a switch */ 1725 /* NB: TLBs must be flushed and invalidated before a switch */
1727 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); 1726 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1728 if (ret) 1727 if (ret)
1729 return ret; 1728 return ret;
1730 1729
1731 ret = intel_ring_begin(req, 6); 1730 ret = intel_ring_begin(req, 6);
1732 if (ret) 1731 if (ret)
1733 return ret; 1732 return ret;
1734 1733
1735 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2)); 1734 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1736 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring)); 1735 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1737 intel_ring_emit(ring, PP_DIR_DCLV_2G); 1736 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1738 intel_ring_emit(ring, RING_PP_DIR_BASE(ring)); 1737 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1739 intel_ring_emit(ring, get_pd_offset(ppgtt)); 1738 intel_ring_emit(ring, get_pd_offset(ppgtt));
1740 intel_ring_emit(ring, MI_NOOP); 1739 intel_ring_emit(ring, MI_NOOP);
1741 intel_ring_advance(ring); 1740 intel_ring_advance(ring);
1742 1741
1743 return 0; 1742 return 0;
1744} 1743}
1745 1744
1746static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt, 1745static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
1747 struct drm_i915_gem_request *req) 1746 struct drm_i915_gem_request *req)
1748{ 1747{
1749 struct intel_engine_cs *ring = req->ring; 1748 struct intel_engine_cs *ring = req->ring;
1750 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev); 1749 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1751 1750
1752 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); 1751 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1753 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt)); 1752 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1754 return 0; 1753 return 0;
1755} 1754}
1756 1755
1757static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt, 1756static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
1758 struct drm_i915_gem_request *req) 1757 struct drm_i915_gem_request *req)
1759{ 1758{
1760 struct intel_engine_cs *ring = req->ring; 1759 struct intel_engine_cs *ring = req->ring;
1761 int ret; 1760 int ret;
1762 1761
1763 /* NB: TLBs must be flushed and invalidated before a switch */ 1762 /* NB: TLBs must be flushed and invalidated before a switch */
1764 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); 1763 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1765 if (ret) 1764 if (ret)
1766 return ret; 1765 return ret;
1767 1766
1768 ret = intel_ring_begin(req, 6); 1767 ret = intel_ring_begin(req, 6);
1769 if (ret) 1768 if (ret)
1770 return ret; 1769 return ret;
1771 1770
1772 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2)); 1771 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1773 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring)); 1772 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1774 intel_ring_emit(ring, PP_DIR_DCLV_2G); 1773 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1775 intel_ring_emit(ring, RING_PP_DIR_BASE(ring)); 1774 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1776 intel_ring_emit(ring, get_pd_offset(ppgtt)); 1775 intel_ring_emit(ring, get_pd_offset(ppgtt));
1777 intel_ring_emit(ring, MI_NOOP); 1776 intel_ring_emit(ring, MI_NOOP);
1778 intel_ring_advance(ring); 1777 intel_ring_advance(ring);
1779 1778
1780 /* XXX: RCS is the only one to auto invalidate the TLBs? */ 1779 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1781 if (ring->id != RCS) { 1780 if (ring->id != RCS) {
1782 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); 1781 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1783 if (ret) 1782 if (ret)
1784 return ret; 1783 return ret;
1785 } 1784 }
1786 1785
1787 return 0; 1786 return 0;
1788} 1787}
1789 1788
1790static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt, 1789static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
1791 struct drm_i915_gem_request *req) 1790 struct drm_i915_gem_request *req)
1792{ 1791{
1793 struct intel_engine_cs *ring = req->ring; 1792 struct intel_engine_cs *ring = req->ring;
1794 struct drm_device *dev = ppgtt->base.dev; 1793 struct drm_device *dev = ppgtt->base.dev;
1795 struct drm_i915_private *dev_priv = dev->dev_private; 1794 struct drm_i915_private *dev_priv = dev->dev_private;
1796 1795
1797 1796
1798 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); 1797 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1799 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt)); 1798 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1800 1799
1801 POSTING_READ(RING_PP_DIR_DCLV(ring)); 1800 POSTING_READ(RING_PP_DIR_DCLV(ring));
1802 1801
1803 return 0; 1802 return 0;
1804} 1803}
1805 1804
1806static void gen8_ppgtt_enable(struct drm_device *dev) 1805static void gen8_ppgtt_enable(struct drm_device *dev)
1807{ 1806{
1808 struct drm_i915_private *dev_priv = dev->dev_private; 1807 struct drm_i915_private *dev_priv = dev->dev_private;
1809 struct intel_engine_cs *ring; 1808 struct intel_engine_cs *ring;
1810 int j; 1809 int j;
1811 1810
1812 for_each_ring(ring, dev_priv, j) { 1811 for_each_ring(ring, dev_priv, j) {
1813 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0; 1812 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
1814 I915_WRITE(RING_MODE_GEN7(ring), 1813 I915_WRITE(RING_MODE_GEN7(ring),
1815 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level)); 1814 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
1816 } 1815 }
1817} 1816}
1818 1817
1819static void gen7_ppgtt_enable(struct drm_device *dev) 1818static void gen7_ppgtt_enable(struct drm_device *dev)
1820{ 1819{
1821 struct drm_i915_private *dev_priv = dev->dev_private; 1820 struct drm_i915_private *dev_priv = dev->dev_private;
1822 struct intel_engine_cs *ring; 1821 struct intel_engine_cs *ring;
1823 uint32_t ecochk, ecobits; 1822 uint32_t ecochk, ecobits;
1824 int i; 1823 int i;
1825 1824
1826 ecobits = I915_READ(GAC_ECO_BITS); 1825 ecobits = I915_READ(GAC_ECO_BITS);
1827 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); 1826 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1828 1827
1829 ecochk = I915_READ(GAM_ECOCHK); 1828 ecochk = I915_READ(GAM_ECOCHK);
1830 if (IS_HASWELL(dev)) { 1829 if (IS_HASWELL(dev)) {
1831 ecochk |= ECOCHK_PPGTT_WB_HSW; 1830 ecochk |= ECOCHK_PPGTT_WB_HSW;
1832 } else { 1831 } else {
1833 ecochk |= ECOCHK_PPGTT_LLC_IVB; 1832 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1834 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB; 1833 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1835 } 1834 }
1836 I915_WRITE(GAM_ECOCHK, ecochk); 1835 I915_WRITE(GAM_ECOCHK, ecochk);
1837 1836
1838 for_each_ring(ring, dev_priv, i) { 1837 for_each_ring(ring, dev_priv, i) {
1839 /* GFX_MODE is per-ring on gen7+ */ 1838 /* GFX_MODE is per-ring on gen7+ */
1840 I915_WRITE(RING_MODE_GEN7(ring), 1839 I915_WRITE(RING_MODE_GEN7(ring),
1841 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); 1840 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1842 } 1841 }
1843} 1842}
1844 1843
1845static void gen6_ppgtt_enable(struct drm_device *dev) 1844static void gen6_ppgtt_enable(struct drm_device *dev)
1846{ 1845{
1847 struct drm_i915_private *dev_priv = dev->dev_private; 1846 struct drm_i915_private *dev_priv = dev->dev_private;
1848 uint32_t ecochk, gab_ctl, ecobits; 1847 uint32_t ecochk, gab_ctl, ecobits;
1849 1848
1850 ecobits = I915_READ(GAC_ECO_BITS); 1849 ecobits = I915_READ(GAC_ECO_BITS);
1851 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT | 1850 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1852 ECOBITS_PPGTT_CACHE64B); 1851 ECOBITS_PPGTT_CACHE64B);
1853 1852
1854 gab_ctl = I915_READ(GAB_CTL); 1853 gab_ctl = I915_READ(GAB_CTL);
1855 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); 1854 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1856 1855
1857 ecochk = I915_READ(GAM_ECOCHK); 1856 ecochk = I915_READ(GAM_ECOCHK);
1858 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B); 1857 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1859 1858
1860 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); 1859 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1861} 1860}
1862 1861
1863/* PPGTT support for Sandybdrige/Gen6 and later */ 1862/* PPGTT support for Sandybdrige/Gen6 and later */
1864static void gen6_ppgtt_clear_range(struct i915_address_space *vm, 1863static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1865 uint64_t start, 1864 uint64_t start,
1866 uint64_t length, 1865 uint64_t length,
1867 bool use_scratch) 1866 bool use_scratch)
1868{ 1867{
1869 struct i915_hw_ppgtt *ppgtt = 1868 struct i915_hw_ppgtt *ppgtt =
1870 container_of(vm, struct i915_hw_ppgtt, base); 1869 container_of(vm, struct i915_hw_ppgtt, base);
1871 gen6_pte_t *pt_vaddr, scratch_pte; 1870 gen6_pte_t *pt_vaddr, scratch_pte;
1872 unsigned first_entry = start >> PAGE_SHIFT; 1871 unsigned first_entry = start >> PAGE_SHIFT;
1873 unsigned num_entries = length >> PAGE_SHIFT; 1872 unsigned num_entries = length >> PAGE_SHIFT;
1874 unsigned act_pt = first_entry / GEN6_PTES; 1873 unsigned act_pt = first_entry / GEN6_PTES;
1875 unsigned first_pte = first_entry % GEN6_PTES; 1874 unsigned first_pte = first_entry % GEN6_PTES;
1876 unsigned last_pte, i; 1875 unsigned last_pte, i;
1877 1876
1878 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page), 1877 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1879 I915_CACHE_LLC, true, 0); 1878 I915_CACHE_LLC, true, 0);
1880 1879
1881 while (num_entries) { 1880 while (num_entries) {
1882 last_pte = first_pte + num_entries; 1881 last_pte = first_pte + num_entries;
1883 if (last_pte > GEN6_PTES) 1882 if (last_pte > GEN6_PTES)
1884 last_pte = GEN6_PTES; 1883 last_pte = GEN6_PTES;
1885 1884
1886 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]); 1885 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1887 1886
1888 for (i = first_pte; i < last_pte; i++) 1887 for (i = first_pte; i < last_pte; i++)
1889 pt_vaddr[i] = scratch_pte; 1888 pt_vaddr[i] = scratch_pte;
1890 1889
1891 kunmap_px(ppgtt, pt_vaddr); 1890 kunmap_px(ppgtt, pt_vaddr);
1892 1891
1893 num_entries -= last_pte - first_pte; 1892 num_entries -= last_pte - first_pte;
1894 first_pte = 0; 1893 first_pte = 0;
1895 act_pt++; 1894 act_pt++;
1896 } 1895 }
1897} 1896}
1898 1897
1899#ifdef __NetBSD__ 1898#ifdef __NetBSD__
1900static void 1899static void
1901gen6_ppgtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap, 1900gen6_ppgtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
1902 uint64_t start, enum i915_cache_level cache_level) 1901 uint64_t start, enum i915_cache_level cache_level)
1903{ 1902{
1904 struct i915_hw_ppgtt *ppgtt = 1903 struct i915_hw_ppgtt *ppgtt =
1905 container_of(vm, struct i915_hw_ppgtt, base); 1904 container_of(vm, struct i915_hw_ppgtt, base);
1906 gen6_gtt_pte_t *pt_vaddr; 1905 gen6_gtt_pte_t *pt_vaddr;
1907 unsigned first_entry = start >> PAGE_SHIFT; 1906 unsigned first_entry = start >> PAGE_SHIFT;
1908 unsigned act_pt = first_entry / GEN6_PTES; 1907 unsigned act_pt = first_entry / GEN6_PTES;
1909 unsigned act_pte = first_entry % GEN6_PTES; 1908 unsigned act_pte = first_entry % GEN6_PTES;
1910 unsigned seg; 1909 unsigned seg;
1911 int ret; 1910 int ret;
1912 1911
1913 pt_vaddr = NULL; 1912 pt_vaddr = NULL;
1914 KASSERT(0 < dmamap->dm_nsegs); 1913 KASSERT(0 < dmamap->dm_nsegs);
1915 for (seg = 0; seg < dmamap->dm_nsegs; seg++) { 1914 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
1916 KASSERT(dmamap->dm_segs[seg].ds_len == PAGE_SIZE); 1915 KASSERT(dmamap->dm_segs[seg].ds_len == PAGE_SIZE);
1917 if (pt_vaddr == NULL) 1916 if (pt_vaddr == NULL)
1918 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]); 1917 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1919 1918
1920 pt_vaddr[act_pte] = 1919 pt_vaddr[act_pte] =
1921 vm->pte_encode(dmamap->dm_segs[seg].ds_addr, 1920 vm->pte_encode(dmamap->dm_segs[seg].ds_addr,
1922 cache_level, true, flags); 1921 cache_level, true, flags);
1923 1922
1924 if (++act_pte == GEN6_PTES) { 1923 if (++act_pte == GEN6_PTES) {
1925 kunmap_px(ppgtt, pt_vaddr); 1924 kunmap_px(ppgtt, pt_vaddr);
1926 pt_vaddr = NULL; 1925 pt_vaddr = NULL;
1927 act_pt++; 1926 act_pt++;
1928 act_pte = 0; 1927 act_pte = 0;
1929 } 1928 }
1930 } 1929 }
1931 if (pt_vaddr) 1930 if (pt_vaddr)
1932 kunmap_px(ppgtt, pt_vaddr); 1931 kunmap_px(ppgtt, pt_vaddr);
1933} 1932}
1934#else 1933#else
1935static void gen6_ppgtt_insert_entries(struct i915_address_space *vm, 1934static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1936 struct sg_table *pages, 1935 struct sg_table *pages,
1937 uint64_t start, 1936 uint64_t start,
1938 enum i915_cache_level cache_level, u32 flags) 1937 enum i915_cache_level cache_level, u32 flags)
1939{ 1938{
1940 struct i915_hw_ppgtt *ppgtt = 1939 struct i915_hw_ppgtt *ppgtt =
1941 container_of(vm, struct i915_hw_ppgtt, base); 1940 container_of(vm, struct i915_hw_ppgtt, base);
1942 gen6_pte_t *pt_vaddr; 1941 gen6_pte_t *pt_vaddr;
1943 unsigned first_entry = start >> PAGE_SHIFT; 1942 unsigned first_entry = start >> PAGE_SHIFT;
1944 unsigned act_pt = first_entry / GEN6_PTES; 1943 unsigned act_pt = first_entry / GEN6_PTES;
1945 unsigned act_pte = first_entry % GEN6_PTES; 1944 unsigned act_pte = first_entry % GEN6_PTES;
1946 struct sg_page_iter sg_iter; 1945 struct sg_page_iter sg_iter;
1947 1946
1948 pt_vaddr = NULL; 1947 pt_vaddr = NULL;
1949 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { 1948 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
1950 if (pt_vaddr == NULL) 1949 if (pt_vaddr == NULL)
1951 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]); 1950 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1952 1951
1953 pt_vaddr[act_pte] = 1952 pt_vaddr[act_pte] =
1954 vm->pte_encode(sg_page_iter_dma_address(&sg_iter), 1953 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
1955 cache_level, true, flags); 1954 cache_level, true, flags);
1956 1955
1957 if (++act_pte == GEN6_PTES) { 1956 if (++act_pte == GEN6_PTES) {
1958 kunmap_px(ppgtt, pt_vaddr); 1957 kunmap_px(ppgtt, pt_vaddr);
1959 pt_vaddr = NULL; 1958 pt_vaddr = NULL;
1960 act_pt++; 1959 act_pt++;
1961 act_pte = 0; 1960 act_pte = 0;
1962 } 1961 }
1963 } 1962 }
1964 if (pt_vaddr) 1963 if (pt_vaddr)
1965 kunmap_px(ppgtt, pt_vaddr); 1964 kunmap_px(ppgtt, pt_vaddr);
1966} 1965}
1967#endif 1966#endif
1968 1967
1969static int gen6_alloc_va_range(struct i915_address_space *vm, 1968static int gen6_alloc_va_range(struct i915_address_space *vm,
1970 uint64_t start_in, uint64_t length_in) 1969 uint64_t start_in, uint64_t length_in)
1971{ 1970{
1972 DECLARE_BITMAP(new_page_tables, I915_PDES); 1971 DECLARE_BITMAP(new_page_tables, I915_PDES);
1973 struct drm_device *dev = vm->dev; 1972 struct drm_device *dev = vm->dev;
1974 struct drm_i915_private *dev_priv = dev->dev_private; 1973 struct drm_i915_private *dev_priv = dev->dev_private;
1975 struct i915_hw_ppgtt *ppgtt = 1974 struct i915_hw_ppgtt *ppgtt =
1976 container_of(vm, struct i915_hw_ppgtt, base); 1975 container_of(vm, struct i915_hw_ppgtt, base);
1977 struct i915_page_table *pt; 1976 struct i915_page_table *pt;
1978 uint32_t start, length, start_save, length_save; 1977 uint32_t start, length, start_save, length_save;
1979 uint32_t pde, temp; 1978 uint32_t pde, temp;
1980 int ret; 1979 int ret;
1981 1980
1982 if (WARN_ON(start_in + length_in > ppgtt->base.total)) 1981 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1983 return -ENODEV; 1982 return -ENODEV;
1984 1983
1985 start = start_save = start_in; 1984 start = start_save = start_in;
1986 length = length_save = length_in; 1985 length = length_save = length_in;
1987 1986
1988 bitmap_zero(new_page_tables, I915_PDES); 1987 bitmap_zero(new_page_tables, I915_PDES);
1989 1988
1990 /* The allocation is done in two stages so that we can bail out with 1989 /* The allocation is done in two stages so that we can bail out with
1991 * minimal amount of pain. The first stage finds new page tables that 1990 * minimal amount of pain. The first stage finds new page tables that
1992 * need allocation. The second stage marks use ptes within the page 1991 * need allocation. The second stage marks use ptes within the page
1993 * tables. 1992 * tables.
1994 */ 1993 */
1995 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) { 1994 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1996 if (pt != vm->scratch_pt) { 1995 if (pt != vm->scratch_pt) {
1997 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES)); 1996 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1998 continue; 1997 continue;
1999 } 1998 }
2000 1999
2001 /* We've already allocated a page table */ 2000 /* We've already allocated a page table */
2002 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES)); 2001 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
2003 2002
2004 pt = alloc_pt(dev); 2003 pt = alloc_pt(dev);
2005 if (IS_ERR(pt)) { 2004 if (IS_ERR(pt)) {
2006 ret = PTR_ERR(pt); 2005 ret = PTR_ERR(pt);
2007 goto unwind_out; 2006 goto unwind_out;
2008 } 2007 }
2009 2008
2010 gen6_initialize_pt(vm, pt); 2009 gen6_initialize_pt(vm, pt);
2011 2010
2012 ppgtt->pd.page_table[pde] = pt; 2011 ppgtt->pd.page_table[pde] = pt;
2013 __set_bit(pde, new_page_tables); 2012 __set_bit(pde, new_page_tables);
2014 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT); 2013 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
2015 } 2014 }
2016 2015
2017 start = start_save; 2016 start = start_save;
2018 length = length_save; 2017 length = length_save;
2019 2018
2020 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) { 2019 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
2021 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES); 2020 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
2022 2021
2023 bitmap_zero(tmp_bitmap, GEN6_PTES); 2022 bitmap_zero(tmp_bitmap, GEN6_PTES);
2024 bitmap_set(tmp_bitmap, gen6_pte_index(start), 2023 bitmap_set(tmp_bitmap, gen6_pte_index(start),
2025 gen6_pte_count(start, length)); 2024 gen6_pte_count(start, length));
2026 2025
2027 if (__test_and_clear_bit(pde, new_page_tables)) 2026 if (__test_and_clear_bit(pde, new_page_tables))
2028 gen6_write_pde(&ppgtt->pd, pde, pt); 2027 gen6_write_pde(&ppgtt->pd, pde, pt);
2029 2028
2030 trace_i915_page_table_entry_map(vm, pde, pt, 2029 trace_i915_page_table_entry_map(vm, pde, pt,
2031 gen6_pte_index(start), 2030 gen6_pte_index(start),
2032 gen6_pte_count(start, length), 2031 gen6_pte_count(start, length),
2033 GEN6_PTES); 2032 GEN6_PTES);
2034 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes, 2033 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
2035 GEN6_PTES); 2034 GEN6_PTES);
2036 } 2035 }
2037 2036
2038 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES)); 2037 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
2039 2038
2040 /* Make sure write is complete before other code can use this page 2039 /* Make sure write is complete before other code can use this page
2041 * table. Also require for WC mapped PTEs */ 2040 * table. Also require for WC mapped PTEs */
2042#ifdef __NetBSD__ 2041#ifdef __NetBSD__
2043 bus_space_read_4(dev_priv->gtt.bst, dev_priv->gtt.bsh, 0); 2042 bus_space_read_4(dev_priv->gtt.bst, dev_priv->gtt.bsh, 0);
2044#else 2043#else
2045 readl(dev_priv->gtt.gsm); 2044 readl(dev_priv->gtt.gsm);
2046#endif 2045#endif
2047 2046
2048 mark_tlbs_dirty(ppgtt); 2047 mark_tlbs_dirty(ppgtt);
2049 return 0; 2048 return 0;
2050 2049
2051unwind_out: 2050unwind_out:
2052 for_each_set_bit(pde, new_page_tables, I915_PDES) { 2051 for_each_set_bit(pde, new_page_tables, I915_PDES) {
2053 struct i915_page_table *pt = ppgtt->pd.page_table[pde]; 2052 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
2054 2053
2055 ppgtt->pd.page_table[pde] = vm->scratch_pt; 2054 ppgtt->pd.page_table[pde] = vm->scratch_pt;
2056 free_pt(vm->dev, pt); 2055 free_pt(vm->dev, pt);
2057 } 2056 }
2058 2057
2059 mark_tlbs_dirty(ppgtt); 2058 mark_tlbs_dirty(ppgtt);
2060 return ret; 2059 return ret;
2061} 2060}
2062 2061
2063static int gen6_init_scratch(struct i915_address_space *vm) 2062static int gen6_init_scratch(struct i915_address_space *vm)
2064{ 2063{
2065 struct drm_device *dev = vm->dev; 2064 struct drm_device *dev = vm->dev;
2066 2065
2067 vm->scratch_page = alloc_scratch_page(dev); 2066 vm->scratch_page = alloc_scratch_page(dev);
2068 if (IS_ERR(vm->scratch_page)) 2067 if (IS_ERR(vm->scratch_page))
2069 return PTR_ERR(vm->scratch_page); 2068 return PTR_ERR(vm->scratch_page);
2070 2069
2071 vm->scratch_pt = alloc_pt(dev); 2070 vm->scratch_pt = alloc_pt(dev);
2072 if (IS_ERR(vm->scratch_pt)) { 2071 if (IS_ERR(vm->scratch_pt)) {
2073 free_scratch_page(dev, vm->scratch_page); 2072 free_scratch_page(dev, vm->scratch_page);
2074 return PTR_ERR(vm->scratch_pt); 2073 return PTR_ERR(vm->scratch_pt);
2075 } 2074 }
2076 2075
2077 gen6_initialize_pt(vm, vm->scratch_pt); 2076 gen6_initialize_pt(vm, vm->scratch_pt);
2078 2077
2079 return 0; 2078 return 0;
2080} 2079}
2081 2080
2082static void gen6_free_scratch(struct i915_address_space *vm) 2081static void gen6_free_scratch(struct i915_address_space *vm)
2083{ 2082{
2084 struct drm_device *dev = vm->dev; 2083 struct drm_device *dev = vm->dev;
2085 2084
2086 free_pt(dev, vm->scratch_pt); 2085 free_pt(dev, vm->scratch_pt);
2087 free_scratch_page(dev, vm->scratch_page); 2086 free_scratch_page(dev, vm->scratch_page);
2088} 2087}
2089 2088
2090static void gen6_ppgtt_cleanup(struct i915_address_space *vm) 2089static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
2091{ 2090{
2092 struct i915_hw_ppgtt *ppgtt = 2091 struct i915_hw_ppgtt *ppgtt =
2093 container_of(vm, struct i915_hw_ppgtt, base); 2092 container_of(vm, struct i915_hw_ppgtt, base);
2094 struct i915_page_table *pt; 2093 struct i915_page_table *pt;
2095 uint32_t pde; 2094 uint32_t pde;
2096 2095
2097 drm_mm_remove_node(&ppgtt->node); 2096 drm_mm_remove_node(&ppgtt->node);
2098 2097
2099 gen6_for_all_pdes(pt, ppgtt, pde) { 2098 gen6_for_all_pdes(pt, ppgtt, pde) {
2100 if (pt != vm->scratch_pt) 2099 if (pt != vm->scratch_pt)
2101 free_pt(ppgtt->base.dev, pt); 2100 free_pt(ppgtt->base.dev, pt);
2102 } 2101 }
2103 2102
2104 gen6_free_scratch(vm); 2103 gen6_free_scratch(vm);
2105} 2104}
2106 2105
2107static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt) 2106static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
2108{ 2107{
2109 struct i915_address_space *vm = &ppgtt->base; 2108 struct i915_address_space *vm = &ppgtt->base;
2110 struct drm_device *dev = ppgtt->base.dev; 2109 struct drm_device *dev = ppgtt->base.dev;
2111 struct drm_i915_private *dev_priv = dev->dev_private; 2110 struct drm_i915_private *dev_priv = dev->dev_private;
2112 bool retried = false; 2111 bool retried = false;
2113 int ret; 2112 int ret;
2114 2113
2115 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The 2114 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2116 * allocator works in address space sizes, so it's multiplied by page 2115 * allocator works in address space sizes, so it's multiplied by page
2117 * size. We allocate at the top of the GTT to avoid fragmentation. 2116 * size. We allocate at the top of the GTT to avoid fragmentation.
2118 */ 2117 */
2119 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm)); 2118 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
2120 2119
2121 ret = gen6_init_scratch(vm); 2120 ret = gen6_init_scratch(vm);
2122 if (ret) 2121 if (ret)
2123 return ret; 2122 return ret;
2124 2123
2125alloc: 2124alloc:
2126 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm, 2125 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
2127 &ppgtt->node, GEN6_PD_SIZE, 2126 &ppgtt->node, GEN6_PD_SIZE,
2128 GEN6_PD_ALIGN, 0, 2127 GEN6_PD_ALIGN, 0,
2129 0, dev_priv->gtt.base.total, 2128 0, dev_priv->gtt.base.total,
2130 DRM_MM_TOPDOWN); 2129 DRM_MM_TOPDOWN);
2131 if (ret == -ENOSPC && !retried) { 2130 if (ret == -ENOSPC && !retried) {
2132 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base, 2131 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
2133 GEN6_PD_SIZE, GEN6_PD_ALIGN, 2132 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2134 I915_CACHE_NONE, 2133 I915_CACHE_NONE,
2135 0, dev_priv->gtt.base.total, 2134 0, dev_priv->gtt.base.total,
2136 0); 2135 0);
2137 if (ret) 2136 if (ret)
2138 goto err_out; 2137 goto err_out;
2139 2138
2140 retried = true; 2139 retried = true;
2141 goto alloc; 2140 goto alloc;
2142 } 2141 }
2143 2142
2144 if (ret) 2143 if (ret)
2145 goto err_out; 2144 goto err_out;
2146 2145
2147 2146
2148 if (ppgtt->node.start < dev_priv->gtt.mappable_end) 2147 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
2149 DRM_DEBUG("Forced to use aperture for PDEs\n"); 2148 DRM_DEBUG("Forced to use aperture for PDEs\n");
2150 2149
2151 return 0; 2150 return 0;
2152 2151
2153err_out: 2152err_out:
2154 gen6_free_scratch(vm); 2153 gen6_free_scratch(vm);
2155 return ret; 2154 return ret;
2156} 2155}
2157 2156
2158static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt) 2157static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2159{ 2158{
2160 return gen6_ppgtt_allocate_page_directories(ppgtt); 2159 return gen6_ppgtt_allocate_page_directories(ppgtt);
2161} 2160}
2162 2161
2163static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt, 2162static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2164 uint64_t start, uint64_t length) 2163 uint64_t start, uint64_t length)
2165{ 2164{
2166 struct i915_page_table *unused; 2165 struct i915_page_table *unused;
2167 uint32_t pde, temp; 2166 uint32_t pde, temp;
2168 2167
2169 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) 2168 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
2170 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt; 2169 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
2171} 2170}
2172#endif 2171#endif
2173 2172
2174static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt) 2173static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
2175{ 2174{
2176 struct drm_device *dev = ppgtt->base.dev; 2175 struct drm_device *dev = ppgtt->base.dev;
2177 struct drm_i915_private *dev_priv = dev->dev_private; 2176 struct drm_i915_private *dev_priv = dev->dev_private;
2178 int ret; 2177 int ret;
2179 2178
2180 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode; 2179 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
2181 if (IS_GEN6(dev)) { 2180 if (IS_GEN6(dev)) {
2182 ppgtt->switch_mm = gen6_mm_switch; 2181 ppgtt->switch_mm = gen6_mm_switch;
2183 } else if (IS_HASWELL(dev)) { 2182 } else if (IS_HASWELL(dev)) {
2184 ppgtt->switch_mm = hsw_mm_switch; 2183 ppgtt->switch_mm = hsw_mm_switch;
2185 } else if (IS_GEN7(dev)) { 2184 } else if (IS_GEN7(dev)) {
2186 ppgtt->switch_mm = gen7_mm_switch; 2185 ppgtt->switch_mm = gen7_mm_switch;
2187 } else 2186 } else
2188 BUG(); 2187 BUG();
2189 2188
2190 if (intel_vgpu_active(dev)) 2189 if (intel_vgpu_active(dev))
2191 ppgtt->switch_mm = vgpu_mm_switch; 2190 ppgtt->switch_mm = vgpu_mm_switch;
2192 2191
2193 ret = gen6_ppgtt_alloc(ppgtt); 2192 ret = gen6_ppgtt_alloc(ppgtt);
2194 if (ret) 2193 if (ret)
2195 return ret; 2194 return ret;
2196 2195
2197 ppgtt->base.allocate_va_range = gen6_alloc_va_range; 2196 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
2198 ppgtt->base.clear_range = gen6_ppgtt_clear_range; 2197 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2199 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries; 2198 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
2200 ppgtt->base.unbind_vma = ppgtt_unbind_vma; 2199 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2201 ppgtt->base.bind_vma = ppgtt_bind_vma; 2200 ppgtt->base.bind_vma = ppgtt_bind_vma;
2202 ppgtt->base.cleanup = gen6_ppgtt_cleanup; 2201 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
2203 ppgtt->base.start = 0; 2202 ppgtt->base.start = 0;
2204 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE; 2203 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
2205#ifndef __NetBSD__ 2204#ifndef __NetBSD__
2206 ppgtt->debug_dump = gen6_dump_ppgtt; 2205 ppgtt->debug_dump = gen6_dump_ppgtt;
2207#endif 2206#endif
2208 2207
2209 ppgtt->pd.base.ggtt_offset = 2208 ppgtt->pd.base.ggtt_offset =
2210 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t); 2209 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
2211 2210
2212 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm + 2211 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
2213 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t); 2212 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
2214 2213
2215 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total); 2214 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
2216 2215
2217 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total); 2216 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2218 2217
2219 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n", 2218 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
2220 ppgtt->node.size >> 20, 2219 ppgtt->node.size >> 20,
2221 ppgtt->node.start / PAGE_SIZE); 2220 ppgtt->node.start / PAGE_SIZE);
2222 2221
2223 DRM_DEBUG("Adding PPGTT at offset %x\n", 2222 DRM_DEBUG("Adding PPGTT at offset %x\n",
2224 ppgtt->pd.base.ggtt_offset << 10); 2223 ppgtt->pd.base.ggtt_offset << 10);
2225 2224
2226 return 0; 2225 return 0;
2227} 2226}
2228 2227
2229static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt) 2228static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
2230{ 2229{
2231 ppgtt->base.dev = dev; 2230 ppgtt->base.dev = dev;
2232 2231
2233 if (INTEL_INFO(dev)->gen < 8) 2232 if (INTEL_INFO(dev)->gen < 8)
2234 return gen6_ppgtt_init(ppgtt); 2233 return gen6_ppgtt_init(ppgtt);
2235 else 2234 else
2236 return gen8_ppgtt_init(ppgtt); 2235 return gen8_ppgtt_init(ppgtt);
2237} 2236}
2238 2237
2239static void i915_address_space_init(struct i915_address_space *vm, 2238static void i915_address_space_init(struct i915_address_space *vm,
2240 struct drm_i915_private *dev_priv) 2239 struct drm_i915_private *dev_priv)
2241{ 2240{
2242 drm_mm_init(&vm->mm, vm->start, vm->total); 2241 drm_mm_init(&vm->mm, vm->start, vm->total);
2243 vm->dev = dev_priv->dev; 2242 vm->dev = dev_priv->dev;
2244 INIT_LIST_HEAD(&vm->active_list); 2243 INIT_LIST_HEAD(&vm->active_list);
2245 INIT_LIST_HEAD(&vm->inactive_list); 2244 INIT_LIST_HEAD(&vm->inactive_list);
2246 list_add_tail(&vm->global_link, &dev_priv->vm_list); 2245 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2247} 2246}
2248 2247
2249int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt) 2248int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
2250{ 2249{
2251 struct drm_i915_private *dev_priv = dev->dev_private; 2250 struct drm_i915_private *dev_priv = dev->dev_private;
2252 int ret = 0; 2251 int ret = 0;
2253 2252
2254 ret = __hw_ppgtt_init(dev, ppgtt); 2253 ret = __hw_ppgtt_init(dev, ppgtt);
2255 if (ret == 0) { 2254 if (ret == 0) {
2256 kref_init(&ppgtt->ref); 2255 kref_init(&ppgtt->ref);
2257 i915_address_space_init(&ppgtt->base, dev_priv); 2256 i915_address_space_init(&ppgtt->base, dev_priv);
2258 } 2257 }
2259 2258
2260 return ret; 2259 return ret;
2261} 2260}
2262 2261
2263int i915_ppgtt_init_hw(struct drm_device *dev) 2262int i915_ppgtt_init_hw(struct drm_device *dev)
2264{ 2263{
2265 /* In the case of execlists, PPGTT is enabled by the context descriptor 2264 /* In the case of execlists, PPGTT is enabled by the context descriptor
2266 * and the PDPs are contained within the context itself. We don't 2265 * and the PDPs are contained within the context itself. We don't
2267 * need to do anything here. */ 2266 * need to do anything here. */
2268 if (i915.enable_execlists) 2267 if (i915.enable_execlists)
2269 return 0; 2268 return 0;
2270 2269
2271 if (!USES_PPGTT(dev)) 2270 if (!USES_PPGTT(dev))
2272 return 0; 2271 return 0;
2273 2272
2274 if (IS_GEN6(dev)) 2273 if (IS_GEN6(dev))
2275 gen6_ppgtt_enable(dev); 2274 gen6_ppgtt_enable(dev);
2276 else if (IS_GEN7(dev)) 2275 else if (IS_GEN7(dev))
2277 gen7_ppgtt_enable(dev); 2276 gen7_ppgtt_enable(dev);
2278 else if (INTEL_INFO(dev)->gen >= 8) 2277 else if (INTEL_INFO(dev)->gen >= 8)
2279 gen8_ppgtt_enable(dev); 2278 gen8_ppgtt_enable(dev);
2280 else 2279 else
2281 MISSING_CASE(INTEL_INFO(dev)->gen); 2280 MISSING_CASE(INTEL_INFO(dev)->gen);
2282 2281
2283 return 0; 2282 return 0;
2284} 2283}
2285 2284
2286int i915_ppgtt_init_ring(struct drm_i915_gem_request *req) 2285int i915_ppgtt_init_ring(struct drm_i915_gem_request *req)
2287{ 2286{
2288 struct drm_i915_private *dev_priv = req->ring->dev->dev_private; 2287 struct drm_i915_private *dev_priv = req->ring->dev->dev_private;
2289 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; 2288 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2290 2289
2291 if (i915.enable_execlists) 2290 if (i915.enable_execlists)
2292 return 0; 2291 return 0;
2293 2292
2294 if (!ppgtt) 2293 if (!ppgtt)
2295 return 0; 2294 return 0;
2296 2295
2297 return ppgtt->switch_mm(ppgtt, req); 2296 return ppgtt->switch_mm(ppgtt, req);
2298} 2297}
2299 2298
2300struct i915_hw_ppgtt * 2299struct i915_hw_ppgtt *
2301i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv) 2300i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
2302{ 2301{
2303 struct i915_hw_ppgtt *ppgtt; 2302 struct i915_hw_ppgtt *ppgtt;
2304 int ret; 2303 int ret;
2305 2304
2306 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); 2305 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2307 if (!ppgtt) 2306 if (!ppgtt)
2308 return ERR_PTR(-ENOMEM); 2307 return ERR_PTR(-ENOMEM);
2309 2308
2310 ret = i915_ppgtt_init(dev, ppgtt); 2309 ret = i915_ppgtt_init(dev, ppgtt);
2311 if (ret) { 2310 if (ret) {
2312 kfree(ppgtt); 2311 kfree(ppgtt);
2313 return ERR_PTR(ret); 2312 return ERR_PTR(ret);
2314 } 2313 }
2315 2314
2316 ppgtt->file_priv = fpriv; 2315 ppgtt->file_priv = fpriv;
2317 2316
2318 trace_i915_ppgtt_create(&ppgtt->base); 2317 trace_i915_ppgtt_create(&ppgtt->base);
2319 2318
2320 return ppgtt; 2319 return ppgtt;
2321} 2320}
2322 2321
2323void i915_ppgtt_release(struct kref *kref) 2322void i915_ppgtt_release(struct kref *kref)
2324{ 2323{
2325 struct i915_hw_ppgtt *ppgtt = 2324 struct i915_hw_ppgtt *ppgtt =
2326 container_of(kref, struct i915_hw_ppgtt, ref); 2325 container_of(kref, struct i915_hw_ppgtt, ref);
2327 2326
2328 trace_i915_ppgtt_release(&ppgtt->base); 2327 trace_i915_ppgtt_release(&ppgtt->base);
2329 2328
2330 /* vmas should already be unbound */ 2329 /* vmas should already be unbound */
2331 WARN_ON(!list_empty(&ppgtt->base.active_list)); 2330 WARN_ON(!list_empty(&ppgtt->base.active_list));
2332 WARN_ON(!list_empty(&ppgtt->base.inactive_list)); 2331 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
2333 2332
2334 list_del(&ppgtt->base.global_link); 2333 list_del(&ppgtt->base.global_link);
2335 drm_mm_takedown(&ppgtt->base.mm); 2334 drm_mm_takedown(&ppgtt->base.mm);
2336 2335
2337 ppgtt->base.cleanup(&ppgtt->base); 2336 ppgtt->base.cleanup(&ppgtt->base);
2338 kfree(ppgtt); 2337 kfree(ppgtt);
2339} 2338}
2340 2339
2341extern int intel_iommu_gfx_mapped; 2340extern int intel_iommu_gfx_mapped;
2342/* Certain Gen5 chipsets require require idling the GPU before 2341/* Certain Gen5 chipsets require require idling the GPU before
2343 * unmapping anything from the GTT when VT-d is enabled. 2342 * unmapping anything from the GTT when VT-d is enabled.
2344 */ 2343 */
2345static bool needs_idle_maps(struct drm_device *dev) 2344static bool needs_idle_maps(struct drm_device *dev)
2346{ 2345{
2347#ifdef CONFIG_INTEL_IOMMU 2346#ifdef CONFIG_INTEL_IOMMU
2348 /* Query intel_iommu to see if we need the workaround. Presumably that 2347 /* Query intel_iommu to see if we need the workaround. Presumably that
2349 * was loaded first. 2348 * was loaded first.
2350 */ 2349 */
2351 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped) 2350 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
2352 return true; 2351 return true;
2353#endif 2352#endif
2354 return false; 2353 return false;
2355} 2354}
2356 2355
2357static bool do_idling(struct drm_i915_private *dev_priv) 2356static bool do_idling(struct drm_i915_private *dev_priv)
2358{ 2357{
2359 bool ret = dev_priv->mm.interruptible; 2358 bool ret = dev_priv->mm.interruptible;
2360 2359
2361 if (unlikely(dev_priv->gtt.do_idle_maps)) { 2360 if (unlikely(dev_priv->gtt.do_idle_maps)) {
2362 dev_priv->mm.interruptible = false; 2361 dev_priv->mm.interruptible = false;
2363 if (i915_gpu_idle(dev_priv->dev)) { 2362 if (i915_gpu_idle(dev_priv->dev)) {
2364 DRM_ERROR("Couldn't idle GPU\n"); 2363 DRM_ERROR("Couldn't idle GPU\n");
2365 /* Wait a bit, in hopes it avoids the hang */ 2364 /* Wait a bit, in hopes it avoids the hang */
2366 udelay(10); 2365 udelay(10);
2367 } 2366 }
2368 } 2367 }
2369 2368
2370 return ret; 2369 return ret;
2371} 2370}
2372 2371
2373static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) 2372static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
2374{ 2373{
2375 if (unlikely(dev_priv->gtt.do_idle_maps)) 2374 if (unlikely(dev_priv->gtt.do_idle_maps))
2376 dev_priv->mm.interruptible = interruptible; 2375 dev_priv->mm.interruptible = interruptible;
2377} 2376}
2378 2377
2379void i915_check_and_clear_faults(struct drm_device *dev) 2378void i915_check_and_clear_faults(struct drm_device *dev)
2380{ 2379{
2381 struct drm_i915_private *dev_priv = dev->dev_private; 2380 struct drm_i915_private *dev_priv = dev->dev_private;
2382 struct intel_engine_cs *ring; 2381 struct intel_engine_cs *ring;
2383 int i; 2382 int i;
2384 2383
2385 if (INTEL_INFO(dev)->gen < 6) 2384 if (INTEL_INFO(dev)->gen < 6)
2386 return; 2385 return;
2387 2386
2388 for_each_ring(ring, dev_priv, i) { 2387 for_each_ring(ring, dev_priv, i) {
2389 u32 fault_reg; 2388 u32 fault_reg;
2390 fault_reg = I915_READ(RING_FAULT_REG(ring)); 2389 fault_reg = I915_READ(RING_FAULT_REG(ring));
2391 if (fault_reg & RING_FAULT_VALID) { 2390 if (fault_reg & RING_FAULT_VALID) {
2392 DRM_DEBUG_DRIVER("Unexpected fault\n" 2391 DRM_DEBUG_DRIVER("Unexpected fault\n"
2393 "\tAddr: 0x%08"PRIx32"\n" 2392 "\tAddr: 0x%08"PRIx32"\n"
2394 "\tAddress space: %s\n" 2393 "\tAddress space: %s\n"
2395 "\tSource ID: %d\n" 2394 "\tSource ID: %d\n"
2396 "\tType: %d\n", 2395 "\tType: %d\n",
2397 fault_reg & PAGE_MASK, 2396 fault_reg & PAGE_MASK,
2398 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT", 2397 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2399 RING_FAULT_SRCID(fault_reg), 2398 RING_FAULT_SRCID(fault_reg),
2400 RING_FAULT_FAULT_TYPE(fault_reg)); 2399 RING_FAULT_FAULT_TYPE(fault_reg));
2401 I915_WRITE(RING_FAULT_REG(ring), 2400 I915_WRITE(RING_FAULT_REG(ring),
2402 fault_reg & ~RING_FAULT_VALID); 2401 fault_reg & ~RING_FAULT_VALID);
2403 } 2402 }
2404 } 2403 }
2405 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS])); 2404 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
2406} 2405}
2407 2406
2408static void i915_ggtt_flush(struct drm_i915_private *dev_priv) 2407static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2409{ 2408{
2410 if (INTEL_INFO(dev_priv->dev)->gen < 6) { 2409 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
2411 intel_gtt_chipset_flush(); 2410 intel_gtt_chipset_flush();
2412 } else { 2411 } else {
2413 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 2412 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2414 POSTING_READ(GFX_FLSH_CNTL_GEN6); 2413 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2415 } 2414 }
2416} 2415}
2417 2416
2418void i915_gem_suspend_gtt_mappings(struct drm_device *dev) 2417void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2419{ 2418{
2420 struct drm_i915_private *dev_priv = dev->dev_private; 2419 struct drm_i915_private *dev_priv = dev->dev_private;
2421 2420
2422 /* Don't bother messing with faults pre GEN6 as we have little 2421 /* Don't bother messing with faults pre GEN6 as we have little
2423 * documentation supporting that it's a good idea. 2422 * documentation supporting that it's a good idea.
2424 */ 2423 */
2425 if (INTEL_INFO(dev)->gen < 6) 2424 if (INTEL_INFO(dev)->gen < 6)
2426 return; 2425 return;
2427 2426
2428 i915_check_and_clear_faults(dev); 2427 i915_check_and_clear_faults(dev);
2429 2428
2430 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, 2429 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
2431 dev_priv->gtt.base.start, 2430 dev_priv->gtt.base.start,
2432 dev_priv->gtt.base.total, 2431 dev_priv->gtt.base.total,
2433 true); 2432 true);
2434 2433
2435 i915_ggtt_flush(dev_priv); 2434 i915_ggtt_flush(dev_priv);
2436} 2435}
2437 2436
2438int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) 2437int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
2439{ 2438{
2440#ifdef __NetBSD__ 2439#ifdef __NetBSD__
2441 KASSERT(0 < obj->base.size); 2440 KASSERT(0 < obj->base.size);
2442 /* XXX errno NetBSD->Linux */ 2441 /* XXX errno NetBSD->Linux */
2443 return -bus_dmamap_load_raw(obj->base.dev->dmat, obj->igo_dmamap, 2442 return -bus_dmamap_load_raw(obj->base.dev->dmat, obj->igo_dmamap,
2444 obj->pages, obj->igo_nsegs, obj->base.size, BUS_DMA_NOWAIT); 2443 obj->pages, obj->igo_nsegs, obj->base.size, BUS_DMA_NOWAIT);
2445#else 2444#else
2446 if (!dma_map_sg(&obj->base.dev->pdev->dev, 2445 if (!dma_map_sg(&obj->base.dev->pdev->dev,
2447 obj->pages->sgl, obj->pages->nents, 2446 obj->pages->sgl, obj->pages->nents,
2448 PCI_DMA_BIDIRECTIONAL)) 2447 PCI_DMA_BIDIRECTIONAL))
2449 return -ENOSPC; 2448 return -ENOSPC;
2450 2449
2451 return 0; 2450 return 0;
2452#endif 2451#endif
2453} 2452}
2454 2453
2455#ifdef __NetBSD__ 2454#ifdef __NetBSD__
2456static uint64_t 2455static uint64_t
2457gen8_get_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i) 2456gen8_get_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i)
2458{ 2457{
2459 CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */ 2458 CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */
2460 CTASSERT(sizeof(gen8_gtt_pte_t) == 8); 2459 CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
2461#ifdef _LP64 /* XXX How to detect bus_space_read_8? */ 2460#ifdef _LP64 /* XXX How to detect bus_space_read_8? */
2462 return bus_space_read_8(bst, bsh, 8*i); 2461 return bus_space_read_8(bst, bsh, 8*i);
2463#else 2462#else
2464 /* 2463 /*
2465 * XXX I'm not sure this case can actually happen in practice: 2464 * XXX I'm not sure this case can actually happen in practice:
2466 * 32-bit gen8 chipsets? 2465 * 32-bit gen8 chipsets?
2467 */ 2466 */
2468 return bus_space_read_4(bst, bsh, 8*i) | 2467 return bus_space_read_4(bst, bsh, 8*i) |
2469 ((uint64_t)bus_space_read_4(bst, bsh, 8*i + 4) << 32); 2468 ((uint64_t)bus_space_read_4(bst, bsh, 8*i + 4) << 32);
2470#endif 2469#endif
2471} 2470}
2472 2471
2473static inline void 2472static inline void
2474gen8_set_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i, 2473gen8_set_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i,
2475 gen8_gtt_pte_t pte) 2474 gen8_gtt_pte_t pte)
2476{ 2475{
2477 CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */ 2476 CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */
2478 CTASSERT(sizeof(gen8_gtt_pte_t) == 8); 2477 CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
2479#ifdef _LP64 /* XXX How to detect bus_space_write_8? */ 2478#ifdef _LP64 /* XXX How to detect bus_space_write_8? */
2480 bus_space_write_8(bst, bsh, 8*i, pte); 2479 bus_space_write_8(bst, bsh, 8*i, pte);
2481#else 2480#else
2482 bus_space_write_4(bst, bsh, 8*i, (uint32_t)pte); 2481 bus_space_write_4(bst, bsh, 8*i, (uint32_t)pte);
2483 bus_space_write_4(bst, bsh, 8*i + 4, (uint32_t)(pte >> 32)); 2482 bus_space_write_4(bst, bsh, 8*i + 4, (uint32_t)(pte >> 32));
2484#endif 2483#endif
2485} 2484}
2486#else 2485#else
2487static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte) 2486static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2488{ 2487{
2489#ifdef writeq 2488#ifdef writeq
2490 writeq(pte, addr); 2489 writeq(pte, addr);
2491#else 2490#else
2492 iowrite32((u32)pte, addr); 2491 iowrite32((u32)pte, addr);
2493 iowrite32(pte >> 32, addr + 4); 2492 iowrite32(pte >> 32, addr + 4);
2494#endif 2493#endif
2495} 2494}
2496#endif 2495#endif
2497 2496
2498#ifdef __NetBSD__ 2497#ifdef __NetBSD__
2499static void 2498static void
2500gen8_ggtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap, 2499gen8_ggtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
2501 uint64_t start, enum i915_cache_level level) 2500 uint64_t start, enum i915_cache_level level)
2502{ 2501{
2503 struct drm_i915_private *dev_priv = vm->dev->dev_private; 2502 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2504 unsigned first_entry = start >> PAGE_SHIFT; 2503 unsigned first_entry = start >> PAGE_SHIFT;
2505 const bus_space_tag_t bst = dev_priv->gtt.bst; 2504 const bus_space_tag_t bst = dev_priv->gtt.bst;
2506 const bus_space_handle_t bsh = dev_priv->gtt.bsh; 2505 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
2507 unsigned i; 2506 unsigned i;
2508 2507
2509 KASSERT(0 < dmamap->dm_nsegs); 2508 KASSERT(0 < dmamap->dm_nsegs);
2510 for (i = 0; i < dmamap->dm_nsegs; i++) { 2509 for (i = 0; i < dmamap->dm_nsegs; i++) {
2511 KASSERT(dmamap->dm_segs[i].ds_len == PAGE_SIZE); 2510 KASSERT(dmamap->dm_segs[i].ds_len == PAGE_SIZE);
2512 gen8_set_pte(bst, bsh, first_entry + i, 2511 gen8_set_pte(bst, bsh, first_entry + i,
2513 gen8_pte_encode(dmamap->dm_segs[i].ds_addr, level, true)); 2512 gen8_pte_encode(dmamap->dm_segs[i].ds_addr, level, true));
2514 } 2513 }
2515 if (0 < i) { 2514 if (0 < i) {
2516 /* Posting read. */ 2515 /* Posting read. */
2517 WARN_ON(gen8_get_pte(bst, bsh, (first_entry + i - 1)) 2516 WARN_ON(gen8_get_pte(bst, bsh, (first_entry + i - 1))
2518 != gen8_pte_encode(dmamap->dm_segs[i - 1].ds_addr, level, 2517 != gen8_pte_encode(dmamap->dm_segs[i - 1].ds_addr, level,
2519 true)); 2518 true));
2520 } 2519 }
2521 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 2520 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2522 POSTING_READ(GFX_FLSH_CNTL_GEN6); 2521 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2523} 2522}
2524#else 2523#else
2525static void gen8_ggtt_insert_entries(struct i915_address_space *vm, 2524static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2526 struct sg_table *st, 2525 struct sg_table *st,
2527 uint64_t start, 2526 uint64_t start,
2528 enum i915_cache_level level, u32 unused) 2527 enum i915_cache_level level, u32 unused)
2529{ 2528{
2530 struct drm_i915_private *dev_priv = vm->dev->dev_private; 2529 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2531 unsigned first_entry = start >> PAGE_SHIFT; 2530 unsigned first_entry = start >> PAGE_SHIFT;
2532 gen8_pte_t __iomem *gtt_entries = 2531 gen8_pte_t __iomem *gtt_entries =
2533 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; 2532 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
2534 int i = 0; 2533 int i = 0;
2535 struct sg_page_iter sg_iter; 2534 struct sg_page_iter sg_iter;
2536 dma_addr_t addr = 0; /* shut up gcc */ 2535 dma_addr_t addr = 0; /* shut up gcc */
2537 2536
2538 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { 2537 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
2539 addr = sg_dma_address(sg_iter.sg) + 2538 addr = sg_dma_address(sg_iter.sg) +
2540 (sg_iter.sg_pgoffset << PAGE_SHIFT); 2539 (sg_iter.sg_pgoffset << PAGE_SHIFT);
2541 gen8_set_pte(&gtt_entries[i], 2540 gen8_set_pte(&gtt_entries[i],
2542 gen8_pte_encode(addr, level, true)); 2541 gen8_pte_encode(addr, level, true));
2543 i++; 2542 i++;
2544 } 2543 }
2545 2544
2546 /* 2545 /*
2547 * XXX: This serves as a posting read to make sure that the PTE has 2546 * XXX: This serves as a posting read to make sure that the PTE has
2548 * actually been updated. There is some concern that even though 2547 * actually been updated. There is some concern that even though
2549 * registers and PTEs are within the same BAR that they are potentially 2548 * registers and PTEs are within the same BAR that they are potentially
2550 * of NUMA access patterns. Therefore, even with the way we assume 2549 * of NUMA access patterns. Therefore, even with the way we assume
2551 * hardware should work, we must keep this posting read for paranoia. 2550 * hardware should work, we must keep this posting read for paranoia.
2552 */ 2551 */
2553 if (i != 0) 2552 if (i != 0)
2554 WARN_ON(readq(&gtt_entries[i-1]) 2553 WARN_ON(readq(&gtt_entries[i-1])
2555 != gen8_pte_encode(addr, level, true)); 2554 != gen8_pte_encode(addr, level, true));
2556 2555
2557 /* This next bit makes the above posting read even more important. We 2556 /* This next bit makes the above posting read even more important. We
2558 * want to flush the TLBs only after we're certain all the PTE updates 2557 * want to flush the TLBs only after we're certain all the PTE updates
2559 * have finished. 2558 * have finished.
2560 */ 2559 */
2561 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 2560 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2562 POSTING_READ(GFX_FLSH_CNTL_GEN6); 2561 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2563} 2562}
2564#endif 2563#endif
2565 2564
2566/* 2565/*
2567 * Binds an object into the global gtt with the specified cache level. The object 2566 * Binds an object into the global gtt with the specified cache level. The object
2568 * will be accessible to the GPU via commands whose operands reference offsets 2567 * will be accessible to the GPU via commands whose operands reference offsets
2569 * within the global GTT as well as accessible by the GPU through the GMADR 2568 * within the global GTT as well as accessible by the GPU through the GMADR
2570 * mapped BAR (dev_priv->mm.gtt->gtt). 2569 * mapped BAR (dev_priv->mm.gtt->gtt).
2571 */ 2570 */
2572#ifdef __NetBSD__ 2571#ifdef __NetBSD__
2573static void 2572static void
2574gen6_ggtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap, 2573gen6_ggtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
2575 uint64_t start, enum i915_cache_level level) 2574 uint64_t start, enum i915_cache_level level)
2576{ 2575{
2577 struct drm_i915_private *dev_priv = vm->dev->dev_private; 2576 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2578 unsigned first_entry = start >> PAGE_SHIFT; 2577 unsigned first_entry = start >> PAGE_SHIFT;
2579 const bus_space_tag_t bst = dev_priv->gtt.bst; 2578 const bus_space_tag_t bst = dev_priv->gtt.bst;
2580 const bus_space_handle_t bsh = dev_priv->gtt.bsh; 2579 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
2581 unsigned i; 2580 unsigned i;
2582 2581
2583 KASSERT(0 < dmamap->dm_nsegs); 2582 KASSERT(0 < dmamap->dm_nsegs);
2584 for (i = 0; i < dmamap->dm_nsegs; i++) { 2583 for (i = 0; i < dmamap->dm_nsegs; i++) {
2585 KASSERT(dmamap->dm_segs[i].ds_len == PAGE_SIZE); 2584 KASSERT(dmamap->dm_segs[i].ds_len == PAGE_SIZE);
2586 CTASSERT(sizeof(gen6_gtt_pte_t) == 4); 2585 CTASSERT(sizeof(gen6_gtt_pte_t) == 4);
2587 bus_space_write_4(bst, bsh, 4*(first_entry + i), 2586 bus_space_write_4(bst, bsh, 4*(first_entry + i),
2588 vm->pte_encode(dmamap->dm_segs[i].ds_addr, level, true)); 2587 vm->pte_encode(dmamap->dm_segs[i].ds_addr, level, true));
2589 } 2588 }
2590 if (0 < i) { 2589 if (0 < i) {
2591 /* Posting read. */ 2590 /* Posting read. */
2592 WARN_ON(bus_space_read_4(bst, bsh, 4*(first_entry + i - 1)) 2591 WARN_ON(bus_space_read_4(bst, bsh, 4*(first_entry + i - 1))
2593 != vm->pte_encode(dmamap->dm_segs[i - 1].ds_addr, level, 2592 != vm->pte_encode(dmamap->dm_segs[i - 1].ds_addr, level,
2594 true)); 2593 true));
2595 } 2594 }
2596 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 2595 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2597 POSTING_READ(GFX_FLSH_CNTL_GEN6); 2596 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2598} 2597}
2599#else 2598#else
2600static void gen6_ggtt_insert_entries(struct i915_address_space *vm, 2599static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2601 struct sg_table *st, 2600 struct sg_table *st,
2602 uint64_t start, 2601 uint64_t start,
2603 enum i915_cache_level level, u32 flags) 2602 enum i915_cache_level level, u32 flags)
2604{ 2603{
2605 struct drm_i915_private *dev_priv = vm->dev->dev_private; 2604 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2606 unsigned first_entry = start >> PAGE_SHIFT; 2605 unsigned first_entry = start >> PAGE_SHIFT;
2607 gen6_pte_t __iomem *gtt_entries = 2606 gen6_pte_t __iomem *gtt_entries =
2608 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; 2607 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
2609 int i = 0; 2608 int i = 0;
2610 struct sg_page_iter sg_iter; 2609 struct sg_page_iter sg_iter;
2611 dma_addr_t addr = 0; 2610 dma_addr_t addr = 0;
2612 2611
2613 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { 2612 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
2614 addr = sg_page_iter_dma_address(&sg_iter); 2613 addr = sg_page_iter_dma_address(&sg_iter);
2615 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]); 2614 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
2616 i++; 2615 i++;
2617 } 2616 }
2618 2617
2619 /* XXX: This serves as a posting read to make sure that the PTE has 2618 /* XXX: This serves as a posting read to make sure that the PTE has
2620 * actually been updated. There is some concern that even though 2619 * actually been updated. There is some concern that even though
2621 * registers and PTEs are within the same BAR that they are potentially 2620 * registers and PTEs are within the same BAR that they are potentially
2622 * of NUMA access patterns. Therefore, even with the way we assume 2621 * of NUMA access patterns. Therefore, even with the way we assume
2623 * hardware should work, we must keep this posting read for paranoia. 2622 * hardware should work, we must keep this posting read for paranoia.
2624 */ 2623 */
2625 if (i != 0) { 2624 if (i != 0) {
2626 unsigned long gtt = readl(&gtt_entries[i-1]); 2625 unsigned long gtt = readl(&gtt_entries[i-1]);
2627 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags)); 2626 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
2628 } 2627 }
2629 2628
2630 /* This next bit makes the above posting read even more important. We 2629 /* This next bit makes the above posting read even more important. We
2631 * want to flush the TLBs only after we're certain all the PTE updates 2630 * want to flush the TLBs only after we're certain all the PTE updates
2632 * have finished. 2631 * have finished.
2633 */ 2632 */
2634 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 2633 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2635 POSTING_READ(GFX_FLSH_CNTL_GEN6); 2634 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2636} 2635}
2637#endif 2636#endif
2638 2637
2639static void gen8_ggtt_clear_range(struct i915_address_space *vm, 2638static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2640 uint64_t start, 2639 uint64_t start,
2641 uint64_t length, 2640 uint64_t length,
2642 bool use_scratch) 2641 bool use_scratch)
2643{ 2642{
2644 struct drm_i915_private *dev_priv = vm->dev->dev_private; 2643 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2645 unsigned first_entry = start >> PAGE_SHIFT; 2644 unsigned first_entry = start >> PAGE_SHIFT;
2646 unsigned num_entries = length >> PAGE_SHIFT; 2645 unsigned num_entries = length >> PAGE_SHIFT;
2647#ifdef __NetBSD__ 2646#ifdef __NetBSD__
2648 const bus_space_tag_t bst = dev_priv->gtt.bst; 2647 const bus_space_tag_t bst = dev_priv->gtt.bst;
2649 const bus_space_handle_t bsh = dev_priv->gtt.bsh; 2648 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
2650 gen8_pte_t scratch_pte; 2649 gen8_pte_t scratch_pte;
2651#else 2650#else
2652 gen8_pte_t scratch_pte, __iomem *gtt_base = 2651 gen8_pte_t scratch_pte, __iomem *gtt_base =
2653 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; 2652 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
2654#endif 2653#endif
2655 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; 2654 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
2656 int i; 2655 int i;
2657 2656
2658 if (WARN(num_entries > max_entries, 2657 if (WARN(num_entries > max_entries,
2659 "First entry = %d; Num entries = %d (max=%d)\n", 2658 "First entry = %d; Num entries = %d (max=%d)\n",
2660 first_entry, num_entries, max_entries)) 2659 first_entry, num_entries, max_entries))
2661 num_entries = max_entries; 2660 num_entries = max_entries;
2662 2661
2663 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page), 2662 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
2664 I915_CACHE_LLC, 2663 I915_CACHE_LLC,
2665 use_scratch); 2664 use_scratch);
2666#ifdef __NetBSD__ 2665#ifdef __NetBSD__
2667 CTASSERT(sizeof(gen8_gtt_pte_t) == 8); 2666 CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
2668 for (i = 0; i < num_entries; i++) 2667 for (i = 0; i < num_entries; i++)
2669 gen8_set_pte(bst, bsh, first_entry + i, scratch_pte); 2668 gen8_set_pte(bst, bsh, first_entry + i, scratch_pte);
2670 (void)gen8_get_pte(bst, bsh, first_entry); 2669 (void)gen8_get_pte(bst, bsh, first_entry);
2671#else 2670#else
2672 for (i = 0; i < num_entries; i++) 2671 for (i = 0; i < num_entries; i++)
2673 gen8_set_pte(&gtt_base[i], scratch_pte); 2672 gen8_set_pte(&gtt_base[i], scratch_pte);
2674 readl(gtt_base); 2673 readl(gtt_base);
2675#endif 2674#endif
2676} 2675}
2677 2676
2678static void gen6_ggtt_clear_range(struct i915_address_space *vm, 2677static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2679 uint64_t start, 2678 uint64_t start,
2680 uint64_t length, 2679 uint64_t length,
2681 bool use_scratch) 2680 bool use_scratch)
2682{ 2681{
2683 struct drm_i915_private *dev_priv = vm->dev->dev_private; 2682 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2684 unsigned first_entry = start >> PAGE_SHIFT; 2683 unsigned first_entry = start >> PAGE_SHIFT;
2685 unsigned num_entries = length >> PAGE_SHIFT; 2684 unsigned num_entries = length >> PAGE_SHIFT;
2686#ifdef __NetBSD__ 2685#ifdef __NetBSD__
2687 const bus_space_tag_t bst = dev_priv->gtt.bst; 2686 const bus_space_tag_t bst = dev_priv->gtt.bst;
2688 const bus_space_handle_t bsh = dev_priv->gtt.bsh; 2687 const bus_space_handle_t bsh = dev_priv->gtt.bsh;
2689 gen8_pte_t scratch_pte; 2688 gen8_pte_t scratch_pte;
2690#else 2689#else
2691 gen6_pte_t scratch_pte, __iomem *gtt_base = 2690 gen6_pte_t scratch_pte, __iomem *gtt_base =
2692 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; 2691 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
2693#endif 2692#endif
2694 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; 2693 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
2695 int i; 2694 int i;
2696 2695
2697 if (WARN(num_entries > max_entries, 2696 if (WARN(num_entries > max_entries,
2698 "First entry = %d; Num entries = %d (max=%d)\n", 2697 "First entry = %d; Num entries = %d (max=%d)\n",
2699 first_entry, num_entries, max_entries)) 2698 first_entry, num_entries, max_entries))
2700 num_entries = max_entries; 2699 num_entries = max_entries;
2701 2700
2702 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page), 2701 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
2703 I915_CACHE_LLC, use_scratch, 0); 2702 I915_CACHE_LLC, use_scratch, 0);
2704 2703
2705#ifdef __NetBSD__ 2704#ifdef __NetBSD__
2706 CTASSERT(sizeof(gen6_gtt_pte_t) == 4); 2705 CTASSERT(sizeof(gen6_gtt_pte_t) == 4);
2707 for (i = 0; i < num_entries; i++) 2706 for (i = 0; i < num_entries; i++)
2708 bus_space_write_4(bst, bsh, 4*(first_entry + i), scratch_pte); 2707 bus_space_write_4(bst, bsh, 4*(first_entry + i), scratch_pte);
2709 (void)bus_space_read_4(bst, bsh, 4*first_entry); 2708 (void)bus_space_read_4(bst, bsh, 4*first_entry);
2710#else 2709#else
2711 for (i = 0; i < num_entries; i++) 2710 for (i = 0; i < num_entries; i++)
2712 iowrite32(scratch_pte, &gtt_base[i]); 2711 iowrite32(scratch_pte, &gtt_base[i]);
2713 readl(gtt_base); 2712 readl(gtt_base);
2714#endif 2713#endif
2715} 2714}
2716 2715
2717static void i915_ggtt_insert_entries(struct i915_address_space *vm, 2716static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2718#ifdef __NetBSD__ 2717#ifdef __NetBSD__
2719 bus_dmamap_t pages, 2718 bus_dmamap_t pages,
2720#else 2719#else
2721 struct sg_table *pages, 2720 struct sg_table *pages,
2722#endif 2721#endif
2723 uint64_t start, 2722 uint64_t start,
2724 enum i915_cache_level cache_level, u32 unused) 2723 enum i915_cache_level cache_level, u32 unused)
2725{ 2724{
2726 unsigned int flags = (cache_level == I915_CACHE_NONE) ? 2725 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2727 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY; 2726 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2728 2727
2729 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags); 2728 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
2730} 2729}
2731 2730
2732static void i915_ggtt_clear_range(struct i915_address_space *vm, 2731static void i915_ggtt_clear_range(struct i915_address_space *vm,
2733 uint64_t start, 2732 uint64_t start,
2734 uint64_t length, 2733 uint64_t length,
2735 bool unused) 2734 bool unused)
2736{ 2735{
2737 unsigned first_entry = start >> PAGE_SHIFT; 2736 unsigned first_entry = start >> PAGE_SHIFT;
2738 unsigned num_entries = length >> PAGE_SHIFT; 2737 unsigned num_entries = length >> PAGE_SHIFT;
2739 intel_gtt_clear_range(first_entry, num_entries); 2738 intel_gtt_clear_range(first_entry, num_entries);
2740} 2739}
2741 2740
2742static int ggtt_bind_vma(struct i915_vma *vma, 2741static int ggtt_bind_vma(struct i915_vma *vma,
2743 enum i915_cache_level cache_level, 2742 enum i915_cache_level cache_level,
2744 u32 flags) 2743 u32 flags)
2745{ 2744{
2746 struct drm_i915_gem_object *obj = vma->obj; 2745 struct drm_i915_gem_object *obj = vma->obj;
2747 u32 pte_flags = 0; 2746 u32 pte_flags = 0;
2748 int ret; 2747 int ret;
2749 2748
2750 ret = i915_get_ggtt_vma_pages(vma); 2749 ret = i915_get_ggtt_vma_pages(vma);
2751 if (ret) 2750 if (ret)
2752 return ret; 2751 return ret;
2753 2752
2754 /* Currently applicable only to VLV */ 2753 /* Currently applicable only to VLV */
2755 if (obj->gt_ro) 2754 if (obj->gt_ro)
2756 pte_flags |= PTE_READ_ONLY; 2755 pte_flags |= PTE_READ_ONLY;
2757 2756
2758 vma->vm->insert_entries(vma->vm, vma->ggtt_view.pages, 2757 vma->vm->insert_entries(vma->vm, vma->ggtt_view.pages,
2759 vma->node.start, 2758 vma->node.start,
2760 cache_level, pte_flags); 2759 cache_level, pte_flags);
2761 2760
2762 /* 2761 /*
2763 * Without aliasing PPGTT there's no difference between 2762 * Without aliasing PPGTT there's no difference between
2764 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally 2763 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2765 * upgrade to both bound if we bind either to avoid double-binding. 2764 * upgrade to both bound if we bind either to avoid double-binding.
2766 */ 2765 */
2767 vma->bound |= GLOBAL_BIND | LOCAL_BIND; 2766 vma->bound |= GLOBAL_BIND | LOCAL_BIND;
2768 2767
2769 return 0; 2768 return 0;
2770} 2769}
2771 2770
2772static int aliasing_gtt_bind_vma(struct i915_vma *vma, 2771static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2773 enum i915_cache_level cache_level, 2772 enum i915_cache_level cache_level,
2774 u32 flags) 2773 u32 flags)
2775{ 2774{
2776 struct drm_device *dev = vma->vm->dev; 2775 struct drm_device *dev = vma->vm->dev;
2777 struct drm_i915_private *dev_priv = dev->dev_private; 2776 struct drm_i915_private *dev_priv = dev->dev_private;
2778 struct drm_i915_gem_object *obj = vma->obj; 2777 struct drm_i915_gem_object *obj = vma->obj;
2779#ifdef __NetBSD__ 2778#ifdef __NetBSD__
2780 bus_dmamap_t pages = obj->pages; 2779 bus_dmamap_t pages = obj->pages;
2781#else 2780#else
2782 struct sg_table *pages = obj->pages; 2781 struct sg_table *pages = obj->pages;
2783#endif 2782#endif
2784 u32 pte_flags = 0; 2783 u32 pte_flags = 0;
2785 int ret; 2784 int ret;
2786 2785
2787 ret = i915_get_ggtt_vma_pages(vma); 2786 ret = i915_get_ggtt_vma_pages(vma);
2788 if (ret) 2787 if (ret)
2789 return ret; 2788 return ret;
2790 pages = vma->ggtt_view.pages; 2789 pages = vma->ggtt_view.pages;
2791 2790
2792 /* Currently applicable only to VLV */ 2791 /* Currently applicable only to VLV */
2793 if (obj->gt_ro) 2792 if (obj->gt_ro)
2794 pte_flags |= PTE_READ_ONLY; 2793 pte_flags |= PTE_READ_ONLY;
2795 2794
2796 2795
2797 if (flags & GLOBAL_BIND) { 2796 if (flags & GLOBAL_BIND) {
2798 vma->vm->insert_entries(vma->vm, pages, 2797 vma->vm->insert_entries(vma->vm, pages,
2799 vma->node.start, 2798 vma->node.start,
2800 cache_level, pte_flags); 2799 cache_level, pte_flags);
2801 } 2800 }
2802 2801
2803 if (flags & LOCAL_BIND) { 2802 if (flags & LOCAL_BIND) {
2804 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt; 2803 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2805 appgtt->base.insert_entries(&appgtt->base, pages, 2804 appgtt->base.insert_entries(&appgtt->base, pages,
2806 vma->node.start, 2805 vma->node.start,
2807 cache_level, pte_flags); 2806 cache_level, pte_flags);
2808 } 2807 }
2809 2808
2810 return 0; 2809 return 0;
2811} 2810}
2812 2811
2813static void ggtt_unbind_vma(struct i915_vma *vma) 2812static void ggtt_unbind_vma(struct i915_vma *vma)
2814{ 2813{
2815 struct drm_device *dev = vma->vm->dev; 2814 struct drm_device *dev = vma->vm->dev;
2816 struct drm_i915_private *dev_priv = dev->dev_private; 2815 struct drm_i915_private *dev_priv = dev->dev_private;
2817 struct drm_i915_gem_object *obj = vma->obj; 2816 struct drm_i915_gem_object *obj = vma->obj;
2818 const uint64_t size = min_t(uint64_t, 2817 const uint64_t size = min_t(uint64_t,
2819 obj->base.size, 2818 obj->base.size,
2820 vma->node.size); 2819 vma->node.size);
2821 2820
2822 if (vma->bound & GLOBAL_BIND) { 2821 if (vma->bound & GLOBAL_BIND) {
2823 vma->vm->clear_range(vma->vm, 2822 vma->vm->clear_range(vma->vm,
2824 vma->node.start, 2823 vma->node.start,
2825 size, 2824 size,
2826 true); 2825 true);
2827 } 2826 }
2828 2827
2829 if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) { 2828 if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
2830 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt; 2829 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2831 2830
2832 appgtt->base.clear_range(&appgtt->base, 2831 appgtt->base.clear_range(&appgtt->base,
2833 vma->node.start, 2832 vma->node.start,
2834 size, 2833 size,
2835 true); 2834 true);
2836 } 2835 }
2837} 2836}
2838 2837
2839void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) 2838void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2840{ 2839{
2841 struct drm_device *dev = obj->base.dev; 2840 struct drm_device *dev = obj->base.dev;
2842 struct drm_i915_private *dev_priv = dev->dev_private; 2841 struct drm_i915_private *dev_priv = dev->dev_private;
2843 bool interruptible; 2842 bool interruptible;
2844 2843
2845 interruptible = do_idling(dev_priv); 2844 interruptible = do_idling(dev_priv);
2846 2845
2847#ifdef __NetBSD__ 2846#ifdef __NetBSD__
2848 bus_dmamap_unload(dev->dmat, obj->igo_dmamap); 2847 bus_dmamap_unload(dev->dmat, obj->igo_dmamap);
2849#else 2848#else
2850 dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents, 2849 dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
2851 PCI_DMA_BIDIRECTIONAL); 2850 PCI_DMA_BIDIRECTIONAL);
2852#endif 2851#endif
2853 2852
2854 undo_idling(dev_priv, interruptible); 2853 undo_idling(dev_priv, interruptible);
2855} 2854}
2856 2855
2857static void i915_gtt_color_adjust(struct drm_mm_node *node, 2856static void i915_gtt_color_adjust(struct drm_mm_node *node,
2858 unsigned long color, 2857 unsigned long color,
2859 u64 *start, 2858 u64 *start,
2860 u64 *end) 2859 u64 *end)
2861{ 2860{
2862 if (node->color != color) 2861 if (node->color != color)
2863 *start += 4096; 2862 *start += 4096;
2864 2863
2865 if (!list_empty(&node->node_list)) { 2864 if (!list_empty(&node->node_list)) {
2866 node = list_entry(node->node_list.next, 2865 node = list_entry(node->node_list.next,
2867 struct drm_mm_node, 2866 struct drm_mm_node,
2868 node_list); 2867 node_list);
2869 if (node->allocated && node->color != color) 2868 if (node->allocated && node->color != color)
2870 *end -= 4096; 2869 *end -= 4096;
2871 } 2870 }
2872} 2871}
2873 2872
2874static int i915_gem_setup_global_gtt(struct drm_device *dev, 2873static int i915_gem_setup_global_gtt(struct drm_device *dev,
2875 u64 start, 2874 u64 start,
2876 u64 mappable_end, 2875 u64 mappable_end,
2877 u64 end) 2876 u64 end)
2878{ 2877{
2879 /* Let GEM Manage all of the aperture. 2878 /* Let GEM Manage all of the aperture.
2880 * 2879 *
2881 * However, leave one page at the end still bound to the scratch page. 2880 * However, leave one page at the end still bound to the scratch page.
2882 * There are a number of places where the hardware apparently prefetches 2881 * There are a number of places where the hardware apparently prefetches
2883 * past the end of the object, and we've seen multiple hangs with the 2882 * past the end of the object, and we've seen multiple hangs with the
2884 * GPU head pointer stuck in a batchbuffer bound at the last page of the 2883 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2885 * aperture. One page should be enough to keep any prefetching inside 2884 * aperture. One page should be enough to keep any prefetching inside
2886 * of the aperture. 2885 * of the aperture.
2887 */ 2886 */
2888 struct drm_i915_private *dev_priv = dev->dev_private; 2887 struct drm_i915_private *dev_priv = dev->dev_private;
2889 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base; 2888 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
2890 struct drm_mm_node *entry; 2889 struct drm_mm_node *entry;
2891 struct drm_i915_gem_object *obj; 2890 struct drm_i915_gem_object *obj;
2892 unsigned long hole_start, hole_end; 2891 unsigned long hole_start, hole_end;
2893 int ret; 2892 int ret;
2894 2893
2895 BUG_ON(mappable_end > end); 2894 BUG_ON(mappable_end > end);
2896 2895
2897 ggtt_vm->start = start; 2896 ggtt_vm->start = start;
2898 2897
2899 /* Subtract the guard page before address space initialization to 2898 /* Subtract the guard page before address space initialization to
2900 * shrink the range used by drm_mm */ 2899 * shrink the range used by drm_mm */
2901 ggtt_vm->total = end - start - PAGE_SIZE; 2900 ggtt_vm->total = end - start - PAGE_SIZE;
2902 i915_address_space_init(ggtt_vm, dev_priv); 2901 i915_address_space_init(ggtt_vm, dev_priv);
2903 ggtt_vm->total += PAGE_SIZE; 2902 ggtt_vm->total += PAGE_SIZE;
2904 2903
2905 if (intel_vgpu_active(dev)) { 2904 if (intel_vgpu_active(dev)) {
2906 ret = intel_vgt_balloon(dev); 2905 ret = intel_vgt_balloon(dev);
2907 if (ret) 2906 if (ret)
2908 return ret; 2907 return ret;
2909 } 2908 }
2910 2909
2911 if (!HAS_LLC(dev)) 2910 if (!HAS_LLC(dev))
2912 ggtt_vm->mm.color_adjust = i915_gtt_color_adjust; 2911 ggtt_vm->mm.color_adjust = i915_gtt_color_adjust;
2913 2912
2914 /* Mark any preallocated objects as occupied */ 2913 /* Mark any preallocated objects as occupied */
2915 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { 2914 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
2916 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm); 2915 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
2917 2916
2918 DRM_DEBUG_KMS("reserving preallocated space: %llx + %zx\n", 2917 DRM_DEBUG_KMS("reserving preallocated space: %llx + %zx\n",
2919 i915_gem_obj_ggtt_offset(obj), obj->base.size); 2918 i915_gem_obj_ggtt_offset(obj), obj->base.size);
2920 2919
2921 WARN_ON(i915_gem_obj_ggtt_bound(obj)); 2920 WARN_ON(i915_gem_obj_ggtt_bound(obj));
2922 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node); 2921 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
2923 if (ret) { 2922 if (ret) {
2924 DRM_DEBUG_KMS("Reservation failed: %i\n", ret); 2923 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2925 return ret; 2924 return ret;
2926 } 2925 }
2927 vma->bound |= GLOBAL_BIND; 2926 vma->bound |= GLOBAL_BIND;
2928 __i915_vma_set_map_and_fenceable(vma); 2927 __i915_vma_set_map_and_fenceable(vma);
2929 list_add_tail(&vma->mm_list, &ggtt_vm->inactive_list); 2928 list_add_tail(&vma->mm_list, &ggtt_vm->inactive_list);
2930 } 2929 }
2931 2930
2932 /* Clear any non-preallocated blocks */ 2931 /* Clear any non-preallocated blocks */
2933 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) { 2932 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
2934 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n", 2933 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2935 hole_start, hole_end); 2934 hole_start, hole_end);
2936 ggtt_vm->clear_range(ggtt_vm, hole_start, 2935 ggtt_vm->clear_range(ggtt_vm, hole_start,
2937 hole_end - hole_start, true); 2936 hole_end - hole_start, true);
2938 } 2937 }
2939 2938
2940 /* And finally clear the reserved guard page */ 2939 /* And finally clear the reserved guard page */
2941 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true); 2940 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
2942 2941
2943 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) { 2942 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2944 struct i915_hw_ppgtt *ppgtt; 2943 struct i915_hw_ppgtt *ppgtt;
2945 2944
2946 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); 2945 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2947 if (!ppgtt) 2946 if (!ppgtt)
2948 return -ENOMEM; 2947 return -ENOMEM;
2949 2948
2950 ret = __hw_ppgtt_init(dev, ppgtt); 2949 ret = __hw_ppgtt_init(dev, ppgtt);
2951 if (ret) { 2950 if (ret) {
2952 ppgtt->base.cleanup(&ppgtt->base); 2951 ppgtt->base.cleanup(&ppgtt->base);
2953 kfree(ppgtt); 2952 kfree(ppgtt);
2954 return ret; 2953 return ret;
2955 } 2954 }
2956 2955
2957 if (ppgtt->base.allocate_va_range) 2956 if (ppgtt->base.allocate_va_range)
2958 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0, 2957 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2959 ppgtt->base.total); 2958 ppgtt->base.total);
2960 if (ret) { 2959 if (ret) {
2961 ppgtt->base.cleanup(&ppgtt->base); 2960 ppgtt->base.cleanup(&ppgtt->base);
2962 kfree(ppgtt); 2961 kfree(ppgtt);
2963 return ret; 2962 return ret;
2964 } 2963 }
2965 2964
2966 ppgtt->base.clear_range(&ppgtt->base, 2965 ppgtt->base.clear_range(&ppgtt->base,
2967 ppgtt->base.start, 2966 ppgtt->base.start,
2968 ppgtt->base.total, 2967 ppgtt->base.total,
2969 true); 2968 true);
2970 2969
2971 dev_priv->mm.aliasing_ppgtt = ppgtt; 2970 dev_priv->mm.aliasing_ppgtt = ppgtt;
2972 WARN_ON(dev_priv->gtt.base.bind_vma != ggtt_bind_vma); 2971 WARN_ON(dev_priv->gtt.base.bind_vma != ggtt_bind_vma);
2973 dev_priv->gtt.base.bind_vma = aliasing_gtt_bind_vma; 2972 dev_priv->gtt.base.bind_vma = aliasing_gtt_bind_vma;
2974 } 2973 }
2975 2974
2976 return 0; 2975 return 0;
2977} 2976}
2978 2977
2979void i915_gem_init_global_gtt(struct drm_device *dev) 2978void i915_gem_init_global_gtt(struct drm_device *dev)
2980{ 2979{
2981 struct drm_i915_private *dev_priv = dev->dev_private; 2980 struct drm_i915_private *dev_priv = dev->dev_private;
2982 u64 gtt_size, mappable_size; 2981 u64 gtt_size, mappable_size;
2983 2982
2984 gtt_size = dev_priv->gtt.base.total; 2983 gtt_size = dev_priv->gtt.base.total;
2985 mappable_size = dev_priv->gtt.mappable_end; 2984 mappable_size = dev_priv->gtt.mappable_end;
2986 2985
2987 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); 2986 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
2988} 2987}
2989 2988
2990void i915_global_gtt_cleanup(struct drm_device *dev) 2989void i915_global_gtt_cleanup(struct drm_device *dev)
2991{ 2990{
2992 struct drm_i915_private *dev_priv = dev->dev_private; 2991 struct drm_i915_private *dev_priv = dev->dev_private;
2993 struct i915_address_space *vm = &dev_priv->gtt.base; 2992 struct i915_address_space *vm = &dev_priv->gtt.base;
2994 2993
2995 if (dev_priv->mm.aliasing_ppgtt) { 2994 if (dev_priv->mm.aliasing_ppgtt) {
2996 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; 2995 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2997 2996
2998 ppgtt->base.cleanup(&ppgtt->base); 2997 ppgtt->base.cleanup(&ppgtt->base);
2999 kfree(ppgtt); 2998 kfree(ppgtt);
3000 } 2999 }
3001 3000
3002 if (drm_mm_initialized(&vm->mm)) { 3001 if (drm_mm_initialized(&vm->mm)) {
3003 if (intel_vgpu_active(dev)) 3002 if (intel_vgpu_active(dev))
3004 intel_vgt_deballoon(); 3003 intel_vgt_deballoon();
3005 3004
3006 drm_mm_takedown(&vm->mm); 3005 drm_mm_takedown(&vm->mm);
3007 list_del(&vm->global_link); 3006 list_del(&vm->global_link);
3008 } 3007 }
3009 3008
3010 vm->cleanup(vm); 3009 vm->cleanup(vm);
3011} 3010}
3012 3011
3013static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl) 3012static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
3014{ 3013{
3015 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT; 3014 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
3016 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK; 3015 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
3017 return snb_gmch_ctl << 20; 3016 return snb_gmch_ctl << 20;
3018} 3017}
3019 3018
3020static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl) 3019static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
3021{ 3020{
3022 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT; 3021 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
3023 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK; 3022 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
3024 if (bdw_gmch_ctl) 3023 if (bdw_gmch_ctl)
3025 bdw_gmch_ctl = 1 << bdw_gmch_ctl; 3024 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
3026 3025
3027#ifdef CONFIG_X86_32 3026#ifdef CONFIG_X86_32
3028 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */ 3027 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
3029 if (bdw_gmch_ctl > 4) 3028 if (bdw_gmch_ctl > 4)
3030 bdw_gmch_ctl = 4; 3029 bdw_gmch_ctl = 4;
3031#endif 3030#endif
3032 3031
3033 return bdw_gmch_ctl << 20; 3032 return bdw_gmch_ctl << 20;
3034} 3033}
3035 3034
3036static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl) 3035static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
3037{ 3036{
3038 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT; 3037 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
3039 gmch_ctrl &= SNB_GMCH_GGMS_MASK; 3038 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
3040 3039
3041 if (gmch_ctrl) 3040 if (gmch_ctrl)
3042 return 1 << (20 + gmch_ctrl); 3041 return 1 << (20 + gmch_ctrl);
3043 3042
3044 return 0; 3043 return 0;
3045} 3044}
3046 3045
3047static size_t gen6_get_stolen_size(u16 snb_gmch_ctl) 3046static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
3048{ 3047{
3049 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT; 3048 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
3050 snb_gmch_ctl &= SNB_GMCH_GMS_MASK; 3049 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
3051 return snb_gmch_ctl << 25; /* 32 MB units */ 3050 return snb_gmch_ctl << 25; /* 32 MB units */
3052} 3051}
3053 3052
3054static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl) 3053static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
3055{ 3054{
3056 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; 3055 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
3057 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK; 3056 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
3058 return bdw_gmch_ctl << 25; /* 32 MB units */ 3057 return bdw_gmch_ctl << 25; /* 32 MB units */
3059} 3058}
3060 3059
3061static size_t chv_get_stolen_size(u16 gmch_ctrl) 3060static size_t chv_get_stolen_size(u16 gmch_ctrl)
3062{ 3061{
3063 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT; 3062 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
3064 gmch_ctrl &= SNB_GMCH_GMS_MASK; 3063 gmch_ctrl &= SNB_GMCH_GMS_MASK;
3065 3064
3066 /* 3065 /*
3067 * 0x0 to 0x10: 32MB increments starting at 0MB 3066 * 0x0 to 0x10: 32MB increments starting at 0MB
3068 * 0x11 to 0x16: 4MB increments starting at 8MB 3067 * 0x11 to 0x16: 4MB increments starting at 8MB
3069 * 0x17 to 0x1d: 4MB increments start at 36MB 3068 * 0x17 to 0x1d: 4MB increments start at 36MB
3070 */ 3069 */
3071 if (gmch_ctrl < 0x11) 3070 if (gmch_ctrl < 0x11)
3072 return gmch_ctrl << 25; 3071 return gmch_ctrl << 25;
3073 else if (gmch_ctrl < 0x17) 3072 else if (gmch_ctrl < 0x17)
3074 return (gmch_ctrl - 0x11 + 2) << 22; 3073 return (gmch_ctrl - 0x11 + 2) << 22;
3075 else 3074 else
3076 return (gmch_ctrl - 0x17 + 9) << 22; 3075 return (gmch_ctrl - 0x17 + 9) << 22;
3077} 3076}
3078 3077
3079static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl) 3078static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
3080{ 3079{
3081 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; 3080 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
3082 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK; 3081 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
3083 3082
3084 if (gen9_gmch_ctl < 0xf0) 3083 if (gen9_gmch_ctl < 0xf0)
3085 return gen9_gmch_ctl << 25; /* 32 MB units */ 3084 return gen9_gmch_ctl << 25; /* 32 MB units */
3086 else 3085 else
3087 /* 4MB increments starting at 0xf0 for 4MB */ 3086 /* 4MB increments starting at 0xf0 for 4MB */
3088 return (gen9_gmch_ctl - 0xf0 + 1) << 22; 3087 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
3089} 3088}
3090 3089
3091static int ggtt_probe_common(struct drm_device *dev, 3090static int ggtt_probe_common(struct drm_device *dev,
3092 size_t gtt_size) 3091 size_t gtt_size)
3093{ 3092{
3094 struct drm_i915_private *dev_priv = dev->dev_private; 3093 struct drm_i915_private *dev_priv = dev->dev_private;
3095 struct i915_page_scratch *scratch_page; 3094 struct i915_page_scratch *scratch_page;
3096 phys_addr_t gtt_phys_addr; 3095 phys_addr_t gtt_phys_addr;
3097 3096
3098 /* For Modern GENs the PTEs and register space are split in the BAR */ 3097 /* For Modern GENs the PTEs and register space are split in the BAR */
3099 gtt_phys_addr = pci_resource_start(dev->pdev, 0) + 3098 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
3100 (pci_resource_len(dev->pdev, 0) / 2); 3099 (pci_resource_len(dev->pdev, 0) / 2);
3101 3100
3102#ifdef __NetBSD__ 3101#ifdef __NetBSD__
3103 dev_priv->gtt.bst = dev->pdev->pd_pa.pa_memt; 3102 dev_priv->gtt.bst = dev->pdev->pd_pa.pa_memt;
3104 /* XXX errno NetBSD->Linux */ 3103 /* XXX errno NetBSD->Linux */
3105 ret = -bus_space_map(dev_priv->gtt.bst, gtt_phys_addr, gtt_size, 3104 ret = -bus_space_map(dev_priv->gtt.bst, gtt_phys_addr, gtt_size,
3106 IS_PROXTON(dev) ? 0 : BUS_SPACE_MAP_PREFETCHABLE, 3105 IS_PROXTON(dev) ? 0 : BUS_SPACE_MAP_PREFETCHABLE,
3107 &dev_priv->gtt.bsh); 3106 &dev_priv->gtt.bsh);
3108 if (ret) { 3107 if (ret) {
3109 DRM_ERROR("Failed to map the graphics translation table: %d\n", 3108 DRM_ERROR("Failed to map the graphics translation table: %d\n",
3110 ret); 3109 ret);
3111 return ret; 3110 return ret;
3112 } 3111 }
3113 dev_priv->gtt.size = gtt_size; 3112 dev_priv->gtt.size = gtt_size;
3114#else 3113#else
3115 /* 3114 /*
3116 * On BXT writes larger than 64 bit to the GTT pagetable range will be 3115 * On BXT writes larger than 64 bit to the GTT pagetable range will be
3117 * dropped. For WC mappings in general we have 64 byte burst writes 3116 * dropped. For WC mappings in general we have 64 byte burst writes
3118 * when the WC buffer is flushed, so we can't use it, but have to 3117 * when the WC buffer is flushed, so we can't use it, but have to
3119 * resort to an uncached mapping. The WC issue is easily caught by the 3118 * resort to an uncached mapping. The WC issue is easily caught by the
3120 * readback check when writing GTT PTE entries. 3119 * readback check when writing GTT PTE entries.
3121 */ 3120 */
3122 if (IS_BROXTON(dev)) 3121 if (IS_BROXTON(dev))
3123 dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size); 3122 dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size);
3124 else 3123 else
3125 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size); 3124 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
3126 if (!dev_priv->gtt.gsm) { 3125 if (!dev_priv->gtt.gsm) {
3127 DRM_ERROR("Failed to map the gtt page table\n"); 3126 DRM_ERROR("Failed to map the gtt page table\n");
3128 return -ENOMEM; 3127 return -ENOMEM;
3129 } 3128 }
3130#endif 3129#endif
3131 3130
3132 scratch_page = alloc_scratch_page(dev); 3131 scratch_page = alloc_scratch_page(dev);
3133 if (IS_ERR(scratch_page)) { 3132 if (IS_ERR(scratch_page)) {
3134 DRM_ERROR("Scratch setup failed\n"); 3133 DRM_ERROR("Scratch setup failed\n");
3135 /* iounmap will also get called at remove, but meh */ 3134 /* iounmap will also get called at remove, but meh */
3136#ifdef __NetBSD__ 3135#ifdef __NetBSD__
3137 bus_space_unmap(dev_priv->gtt.bst, dev_priv->gtt.bsh, 3136 bus_space_unmap(dev_priv->gtt.bst, dev_priv->gtt.bsh,
3138 dev_priv->gtt.size); 3137 dev_priv->gtt.size);
3139#else 3138#else
3140 iounmap(dev_priv->gtt.gsm); 3139 iounmap(dev_priv->gtt.gsm);
3141#endif 3140#endif
3142 return PTR_ERR(scratch_page); 3141 return PTR_ERR(scratch_page);
3143 } 3142 }
3144 3143
3145 dev_priv->gtt.base.scratch_page = scratch_page; 3144 dev_priv->gtt.base.scratch_page = scratch_page;
3146 3145
3147 return 0; 3146 return 0;
3148} 3147}
3149 3148
3150/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability 3149/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3151 * bits. When using advanced contexts each context stores its own PAT, but 3150 * bits. When using advanced contexts each context stores its own PAT, but
3152 * writing this data shouldn't be harmful even in those cases. */ 3151 * writing this data shouldn't be harmful even in those cases. */
3153static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv) 3152static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
3154{ 3153{
3155 uint64_t pat; 3154 uint64_t pat;
3156 3155
3157 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */ 3156 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3158 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */ 3157 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3159 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */ 3158 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3160 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */ 3159 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3161 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) | 3160 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3162 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) | 3161 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3163 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) | 3162 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3164 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3)); 3163 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3165 3164
3166 if (!USES_PPGTT(dev_priv->dev)) 3165 if (!USES_PPGTT(dev_priv->dev))
3167 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry, 3166 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3168 * so RTL will always use the value corresponding to 3167 * so RTL will always use the value corresponding to
3169 * pat_sel = 000". 3168 * pat_sel = 000".
3170 * So let's disable cache for GGTT to avoid screen corruptions. 3169 * So let's disable cache for GGTT to avoid screen corruptions.
3171 * MOCS still can be used though. 3170 * MOCS still can be used though.
3172 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work 3171 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3173 * before this patch, i.e. the same uncached + snooping access 3172 * before this patch, i.e. the same uncached + snooping access
3174 * like on gen6/7 seems to be in effect. 3173 * like on gen6/7 seems to be in effect.
3175 * - So this just fixes blitter/render access. Again it looks 3174 * - So this just fixes blitter/render access. Again it looks
3176 * like it's not just uncached access, but uncached + snooping. 3175 * like it's not just uncached access, but uncached + snooping.
3177 * So we can still hold onto all our assumptions wrt cpu 3176 * So we can still hold onto all our assumptions wrt cpu
3178 * clflushing on LLC machines. 3177 * clflushing on LLC machines.
3179 */ 3178 */
3180 pat = GEN8_PPAT(0, GEN8_PPAT_UC); 3179 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3181 3180
3182 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b 3181 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3183 * write would work. */ 3182 * write would work. */
3184 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat); 3183 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3185 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32); 3184 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3186} 3185}
3187 3186
3188static void chv_setup_private_ppat(struct drm_i915_private *dev_priv) 3187static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3189{ 3188{
3190 uint64_t pat; 3189 uint64_t pat;
3191 3190
3192 /* 3191 /*
3193 * Map WB on BDW to snooped on CHV. 3192 * Map WB on BDW to snooped on CHV.
3194 * 3193 *
3195 * Only the snoop bit has meaning for CHV, the rest is 3194 * Only the snoop bit has meaning for CHV, the rest is
3196 * ignored. 3195 * ignored.
3197 * 3196 *
3198 * The hardware will never snoop for certain types of accesses: 3197 * The hardware will never snoop for certain types of accesses:
3199 * - CPU GTT (GMADR->GGTT->no snoop->memory) 3198 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3200 * - PPGTT page tables 3199 * - PPGTT page tables
3201 * - some other special cycles 3200 * - some other special cycles
3202 * 3201 *
3203 * As with BDW, we also need to consider the following for GT accesses: 3202 * As with BDW, we also need to consider the following for GT accesses:
3204 * "For GGTT, there is NO pat_sel[2:0] from the entry, 3203 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3205 * so RTL will always use the value corresponding to 3204 * so RTL will always use the value corresponding to
3206 * pat_sel = 000". 3205 * pat_sel = 000".
3207 * Which means we must set the snoop bit in PAT entry 0 3206 * Which means we must set the snoop bit in PAT entry 0
3208 * in order to keep the global status page working. 3207 * in order to keep the global status page working.
3209 */ 3208 */
3210 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) | 3209 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3211 GEN8_PPAT(1, 0) | 3210 GEN8_PPAT(1, 0) |
3212 GEN8_PPAT(2, 0) | 3211 GEN8_PPAT(2, 0) |
3213 GEN8_PPAT(3, 0) | 3212 GEN8_PPAT(3, 0) |
3214 GEN8_PPAT(4, CHV_PPAT_SNOOP) | 3213 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3215 GEN8_PPAT(5, CHV_PPAT_SNOOP) | 3214 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3216 GEN8_PPAT(6, CHV_PPAT_SNOOP) | 3215 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3217 GEN8_PPAT(7, CHV_PPAT_SNOOP); 3216 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3218 3217
3219 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat); 3218 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3220 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32); 3219 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3221} 3220}
3222 3221
3223static int gen8_gmch_probe(struct drm_device *dev, 3222static int gen8_gmch_probe(struct drm_device *dev,
3224 u64 *gtt_total, 3223 u64 *gtt_total,
3225 size_t *stolen, 3224 size_t *stolen,
3226 phys_addr_t *mappable_base, 3225 phys_addr_t *mappable_base,
3227 u64 *mappable_end) 3226 u64 *mappable_end)
3228{ 3227{
3229 struct drm_i915_private *dev_priv = dev->dev_private; 3228 struct drm_i915_private *dev_priv = dev->dev_private;
3230 u64 gtt_size; 3229 u64 gtt_size;
3231 u16 snb_gmch_ctl; 3230 u16 snb_gmch_ctl;
3232 int ret; 3231 int ret;
3233 3232
3234 /* TODO: We're not aware of mappable constraints on gen8 yet */ 3233 /* TODO: We're not aware of mappable constraints on gen8 yet */
3235 *mappable_base = pci_resource_start(dev->pdev, 2); 3234 *mappable_base = pci_resource_start(dev->pdev, 2);
3236 *mappable_end = pci_resource_len(dev->pdev, 2); 3235 *mappable_end = pci_resource_len(dev->pdev, 2);
3237 3236
3238#ifndef __NetBSD__ 3237#ifndef __NetBSD__
3239 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39))) 3238 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
3240 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39)); 3239 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
3241#endif 3240#endif
3242 3241
3243 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); 3242 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3244 3243
3245 if (INTEL_INFO(dev)->gen >= 9) { 3244 if (INTEL_INFO(dev)->gen >= 9) {
3246 *stolen = gen9_get_stolen_size(snb_gmch_ctl); 3245 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
3247 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); 3246 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
3248 } else if (IS_CHERRYVIEW(dev)) { 3247 } else if (IS_CHERRYVIEW(dev)) {
3249 *stolen = chv_get_stolen_size(snb_gmch_ctl); 3248 *stolen = chv_get_stolen_size(snb_gmch_ctl);
3250 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl); 3249 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
3251 } else { 3250 } else {
3252 *stolen = gen8_get_stolen_size(snb_gmch_ctl); 3251 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
3253 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); 3252 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
3254 } 3253 }
3255 3254
3256 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT; 3255 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
3257 3256
3258 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) 3257 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3259 chv_setup_private_ppat(dev_priv); 3258 chv_setup_private_ppat(dev_priv);
3260 else 3259 else
3261 bdw_setup_private_ppat(dev_priv); 3260 bdw_setup_private_ppat(dev_priv);
3262 3261
3263 ret = ggtt_probe_common(dev, gtt_size); 3262 ret = ggtt_probe_common(dev, gtt_size);
3264 3263
3265 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range; 3264 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
3266 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries; 3265 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
3267 dev_priv->gtt.base.bind_vma = ggtt_bind_vma; 3266 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
3268 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma; 3267 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
3269 3268
3270 /* XXX 39-bit addresses? Really? See pci_set_dma_mask above... */ 3269 /* XXX 39-bit addresses? Really? See pci_set_dma_mask above... */
3271 dev_priv->gtt.max_paddr = __BITS(38, 0); 3270 dev_priv->gtt.max_paddr = __BITS(38, 0);
3272 3271
3273 return ret; 3272 return ret;
3274} 3273}
3275 3274
3276static int gen6_gmch_probe(struct drm_device *dev, 3275static int gen6_gmch_probe(struct drm_device *dev,
3277 u64 *gtt_total, 3276 u64 *gtt_total,
3278 size_t *stolen, 3277 size_t *stolen,
3279 phys_addr_t *mappable_base, 3278 phys_addr_t *mappable_base,
3280 u64 *mappable_end) 3279 u64 *mappable_end)
3281{ 3280{
3282 struct drm_i915_private *dev_priv = dev->dev_private; 3281 struct drm_i915_private *dev_priv = dev->dev_private;
3283 unsigned int gtt_size; 3282 unsigned int gtt_size;
3284 u16 snb_gmch_ctl; 3283 u16 snb_gmch_ctl;
3285 int ret; 3284 int ret;
3286 3285
3287 *mappable_base = pci_resource_start(dev->pdev, 2); 3286 *mappable_base = pci_resource_start(dev->pdev, 2);
3288 *mappable_end = pci_resource_len(dev->pdev, 2); 3287 *mappable_end = pci_resource_len(dev->pdev, 2);
3289 3288
3290 /* 64/512MB is the current min/max we actually know of, but this is just 3289 /* 64/512MB is the current min/max we actually know of, but this is just
3291 * a coarse sanity check. 3290 * a coarse sanity check.
3292 */ 3291 */
3293 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) { 3292 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
3294 DRM_ERROR("Unknown GMADR size (%llx)\n", 3293 DRM_ERROR("Unknown GMADR size (%llx)\n",
3295 dev_priv->gtt.mappable_end); 3294 dev_priv->gtt.mappable_end);
3296 return -ENXIO; 3295 return -ENXIO;
3297 } 3296 }
3298 3297
3299#ifndef __NetBSD__ 3298#ifndef __NetBSD__
3300 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40))) 3299 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
3301 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40)); 3300 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
3302#endif 3301#endif
3303 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); 3302 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3304 3303
3305 *stolen = gen6_get_stolen_size(snb_gmch_ctl); 3304 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
3306 3305
3307 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl); 3306 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
3308 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT; 3307 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
3309 3308
3310 ret = ggtt_probe_common(dev, gtt_size); 3309 ret = ggtt_probe_common(dev, gtt_size);
3311 3310
3312 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range; 3311 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
3313 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries; 3312 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
3314 dev_priv->gtt.base.bind_vma = ggtt_bind_vma; 3313 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
3315 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma; 3314 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
3316 3315
3317 dev_priv->gtt.max_paddr = __BITS(39, 0); 3316 dev_priv->gtt.max_paddr = __BITS(39, 0);
3318 3317
3319 return ret; 3318 return ret;
3320} 3319}
3321 3320
3322static void gen6_gmch_remove(struct i915_address_space *vm) 3321static void gen6_gmch_remove(struct i915_address_space *vm)
3323{ 3322{
3324 
3325 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base); 3323 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
3326 3324
3327#ifdef __NetBSD__ 3325#ifdef __NetBSD__
3328 bus_space_unmap(gtt->bst, gtt->bsh, gtt->size); 3326 bus_space_unmap(gtt->bst, gtt->bsh, gtt->size);
3329#else 3327#else
3330 iounmap(gtt->gsm); 3328 iounmap(gtt->gsm);
3331#endif 3329#endif
3332 free_scratch_page(vm->dev, vm->scratch_page); 3330 free_scratch_page(vm->dev, vm->scratch_page);
3333} 3331}
3334 3332
3335static int i915_gmch_probe(struct drm_device *dev, 3333static int i915_gmch_probe(struct drm_device *dev,
3336 u64 *gtt_total, 3334 u64 *gtt_total,
3337 size_t *stolen, 3335 size_t *stolen,
3338 phys_addr_t *mappable_base, 3336 phys_addr_t *mappable_base,
3339 u64 *mappable_end) 3337 u64 *mappable_end)
3340{ 3338{
3341 struct drm_i915_private *dev_priv = dev->dev_private; 3339 struct drm_i915_private *dev_priv = dev->dev_private;
3342 int ret; 3340 int ret;
3343 3341
3344 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL); 3342 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
3345 if (!ret) { 3343 if (!ret) {
3346 DRM_ERROR("failed to set up gmch\n"); 3344 DRM_ERROR("failed to set up gmch\n");
3347 return -EIO; 3345 return -EIO;
3348 } 3346 }
3349 3347
3350 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end); 3348 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
3351 3349
3352 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev); 3350 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
3353 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries; 3351 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
3354 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range; 3352 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
3355 dev_priv->gtt.base.bind_vma = ggtt_bind_vma; 3353 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
3356 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma; 3354 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
3357 3355
3358 if (unlikely(dev_priv->gtt.do_idle_maps)) 3356 if (unlikely(dev_priv->gtt.do_idle_maps))
3359 DRM_INFO("applying Ironlake quirks for intel_iommu\n"); 3357 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3360 3358
3361 if (INTEL_INFO(dev)->gen <= 2) 3359 if (INTEL_INFO(dev)->gen <= 2)
3362 dev_priv->gtt.max_paddr = __BITS(29, 0); 3360 dev_priv->gtt.max_paddr = __BITS(29, 0);
3363 else if ((INTEL_INFO(dev)->gen <= 3) || 3361 else if ((INTEL_INFO(dev)->gen <= 3) ||
3364 IS_BROADWATER(dev) || IS_CRESTLINE(dev)) 3362 IS_BROADWATER(dev) || IS_CRESTLINE(dev))
3365 dev_priv->gtt.max_paddr = __BITS(31, 0); 3363 dev_priv->gtt.max_paddr = __BITS(31, 0);
3366 else if (INTEL_INFO(dev)->gen <= 5) 3364 else if (INTEL_INFO(dev)->gen <= 5)
3367 dev_priv->gtt.max_paddr = __BITS(35, 0); 3365 dev_priv->gtt.max_paddr = __BITS(35, 0);
3368 else 3366 else
3369 dev_priv->gtt.max_paddr = __BITS(39, 0); 3367 dev_priv->gtt.max_paddr = __BITS(39, 0);
3370 3368
3371 return 0; 3369 return 0;
3372} 3370}
3373 3371
3374static void i915_gmch_remove(struct i915_address_space *vm) 3372static void i915_gmch_remove(struct i915_address_space *vm)
3375{ 3373{
3376 intel_gmch_remove(); 3374 intel_gmch_remove();
3377} 3375}
3378 3376
3379int i915_gem_gtt_init(struct drm_device *dev) 3377int i915_gem_gtt_init(struct drm_device *dev)
3380{ 3378{
3381 struct drm_i915_private *dev_priv = dev->dev_private; 3379 struct drm_i915_private *dev_priv = dev->dev_private;
3382 struct i915_gtt *gtt = &dev_priv->gtt; 3380 struct i915_gtt *gtt = &dev_priv->gtt;
3383 int ret; 3381 int ret;
3384 3382
3385 if (INTEL_INFO(dev)->gen <= 5) { 3383 if (INTEL_INFO(dev)->gen <= 5) {
3386 gtt->gtt_probe = i915_gmch_probe; 3384 gtt->gtt_probe = i915_gmch_probe;
3387 gtt->base.cleanup = i915_gmch_remove; 3385 gtt->base.cleanup = i915_gmch_remove;
3388 } else if (INTEL_INFO(dev)->gen < 8) { 3386 } else if (INTEL_INFO(dev)->gen < 8) {
3389 gtt->gtt_probe = gen6_gmch_probe; 3387 gtt->gtt_probe = gen6_gmch_probe;
3390 gtt->base.cleanup = gen6_gmch_remove; 3388 gtt->base.cleanup = gen6_gmch_remove;
3391 if (IS_HASWELL(dev) && dev_priv->ellc_size) 3389 if (IS_HASWELL(dev) && dev_priv->ellc_size)
3392 gtt->base.pte_encode = iris_pte_encode; 3390 gtt->base.pte_encode = iris_pte_encode;
3393 else if (IS_HASWELL(dev)) 3391 else if (IS_HASWELL(dev))
3394 gtt->base.pte_encode = hsw_pte_encode; 3392 gtt->base.pte_encode = hsw_pte_encode;
3395 else if (IS_VALLEYVIEW(dev)) 3393 else if (IS_VALLEYVIEW(dev))
3396 gtt->base.pte_encode = byt_pte_encode; 3394 gtt->base.pte_encode = byt_pte_encode;
3397 else if (INTEL_INFO(dev)->gen >= 7) 3395 else if (INTEL_INFO(dev)->gen >= 7)
3398 gtt->base.pte_encode = ivb_pte_encode; 3396 gtt->base.pte_encode = ivb_pte_encode;
3399 else 3397 else
3400 gtt->base.pte_encode = snb_pte_encode; 3398 gtt->base.pte_encode = snb_pte_encode;
3401 } else { 3399 } else {
3402 dev_priv->gtt.gtt_probe = gen8_gmch_probe; 3400 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
3403 dev_priv->gtt.base.cleanup = gen6_gmch_remove; 3401 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
3404 } 3402 }
3405 3403
3406 gtt->base.dev = dev; 3404 gtt->base.dev = dev;
3407 3405
3408 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size, 3406 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
3409 &gtt->mappable_base, &gtt->mappable_end); 3407 &gtt->mappable_base, &gtt->mappable_end);
3410 if (ret) 3408 if (ret)
3411 return ret; 3409 return ret;
3412 3410
3413#ifdef __NetBSD__ 3411#ifdef __NetBSD__
3414 dev_priv->gtt.pgfl = x86_select_freelist(dev_priv->gtt.max_paddr); 3412 dev_priv->gtt.pgfl = x86_select_freelist(dev_priv->gtt.max_paddr);
3415 ret = drm_limit_dma_space(dev, 0, dev_priv->gtt.max_paddr); 3413 ret = drm_limit_dma_space(dev, 0, dev_priv->gtt.max_paddr);
3416 if (ret) { 3414 if (ret) {
3417 DRM_ERROR("Unable to limit DMA paddr allocations: %d!\n", ret); 3415 DRM_ERROR("Unable to limit DMA paddr allocations: %d!\n", ret);
3418 gtt->base.cleanup(&gtt->base); 3416 gtt->base.cleanup(&gtt->base);
3419 return ret; 3417 return ret;
3420 } 3418 }
3421#endif 3419#endif
3422 3420
3423 /* GMADR is the PCI mmio aperture into the global GTT. */ 3421 /* GMADR is the PCI mmio aperture into the global GTT. */
3424 DRM_INFO("Memory usable by graphics device = %lluM\n", 3422 DRM_INFO("Memory usable by graphics device = %lluM\n",
3425 gtt->base.total >> 20); 3423 gtt->base.total >> 20);
3426 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", gtt->mappable_end >> 20); 3424 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", gtt->mappable_end >> 20);
3427 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20); 3425 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
3428#ifdef CONFIG_INTEL_IOMMU 3426#ifdef CONFIG_INTEL_IOMMU
3429 if (intel_iommu_gfx_mapped) 3427 if (intel_iommu_gfx_mapped)
3430 DRM_INFO("VT-d active for gfx access\n"); 3428 DRM_INFO("VT-d active for gfx access\n");
3431#endif 3429#endif
3432 /* 3430 /*
3433 * i915.enable_ppgtt is read-only, so do an early pass to validate the 3431 * i915.enable_ppgtt is read-only, so do an early pass to validate the
3434 * user's requested state against the hardware/driver capabilities. We 3432 * user's requested state against the hardware/driver capabilities. We
3435 * do this now so that we can print out any log messages once rather 3433 * do this now so that we can print out any log messages once rather
3436 * than every time we check intel_enable_ppgtt(). 3434 * than every time we check intel_enable_ppgtt().
3437 */ 3435 */
3438 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt); 3436 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
3439 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt); 3437 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
3440 3438
3441 return 0; 3439 return 0;
3442} 3440}
3443 3441
3444void i915_gem_restore_gtt_mappings(struct drm_device *dev) 3442void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3445{ 3443{
3446 struct drm_i915_private *dev_priv = dev->dev_private; 3444 struct drm_i915_private *dev_priv = dev->dev_private;
3447 struct drm_i915_gem_object *obj; 3445 struct drm_i915_gem_object *obj;
3448 struct i915_address_space *vm; 3446 struct i915_address_space *vm;
3449 struct i915_vma *vma; 3447 struct i915_vma *vma;
3450 bool flush; 3448 bool flush;
3451 3449
3452 i915_check_and_clear_faults(dev); 3450 i915_check_and_clear_faults(dev);
3453 3451
3454 /* First fill our portion of the GTT with scratch pages */ 3452 /* First fill our portion of the GTT with scratch pages */
3455 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, 3453 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
3456 dev_priv->gtt.base.start, 3454 dev_priv->gtt.base.start,
3457 dev_priv->gtt.base.total, 3455 dev_priv->gtt.base.total,
3458 true); 3456 true);
3459 3457
3460 /* Cache flush objects bound into GGTT and rebind them. */ 3458 /* Cache flush objects bound into GGTT and rebind them. */
3461 vm = &dev_priv->gtt.base; 3459 vm = &dev_priv->gtt.base;
3462 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { 3460 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
3463 flush = false; 3461 flush = false;
3464 list_for_each_entry(vma, &obj->vma_list, vma_link) { 3462 list_for_each_entry(vma, &obj->vma_list, vma_link) {
3465 if (vma->vm != vm) 3463 if (vma->vm != vm)
3466 continue; 3464 continue;
3467 3465
3468 WARN_ON(i915_vma_bind(vma, obj->cache_level, 3466 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3469 PIN_UPDATE)); 3467 PIN_UPDATE));
3470 3468
3471 flush = true; 3469 flush = true;
3472 } 3470 }
3473 3471
3474 if (flush) 3472 if (flush)
3475 i915_gem_clflush_object(obj, obj->pin_display); 3473 i915_gem_clflush_object(obj, obj->pin_display);
3476 } 3474 }
3477 3475
3478 if (INTEL_INFO(dev)->gen >= 8) { 3476 if (INTEL_INFO(dev)->gen >= 8) {
3479 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) 3477 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3480 chv_setup_private_ppat(dev_priv); 3478 chv_setup_private_ppat(dev_priv);
3481 else 3479 else
3482 bdw_setup_private_ppat(dev_priv); 3480 bdw_setup_private_ppat(dev_priv);
3483 3481
3484 return; 3482 return;
3485 } 3483 }
3486 3484
3487 if (USES_PPGTT(dev)) { 3485 if (USES_PPGTT(dev)) {
3488 list_for_each_entry(vm, &dev_priv->vm_list, global_link) { 3486 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3489 /* TODO: Perhaps it shouldn't be gen6 specific */ 3487 /* TODO: Perhaps it shouldn't be gen6 specific */
3490 3488
3491 struct i915_hw_ppgtt *ppgtt = 3489 struct i915_hw_ppgtt *ppgtt =
3492 container_of(vm, struct i915_hw_ppgtt, 3490 container_of(vm, struct i915_hw_ppgtt,
3493 base); 3491 base);
3494 3492
3495 if (i915_is_ggtt(vm)) 3493 if (i915_is_ggtt(vm))
3496 ppgtt = dev_priv->mm.aliasing_ppgtt; 3494 ppgtt = dev_priv->mm.aliasing_ppgtt;
3497 3495
3498 gen6_write_page_range(dev_priv, &ppgtt->pd, 3496 gen6_write_page_range(dev_priv, &ppgtt->pd,
3499 0, ppgtt->base.total); 3497 0, ppgtt->base.total);
3500 } 3498 }
3501 } 3499 }
3502 3500
3503 i915_ggtt_flush(dev_priv); 3501 i915_ggtt_flush(dev_priv);
3504} 3502}
3505 3503
3506static struct i915_vma * 3504static struct i915_vma *
3507__i915_gem_vma_create(struct drm_i915_gem_object *obj, 3505__i915_gem_vma_create(struct drm_i915_gem_object *obj,
3508 struct i915_address_space *vm, 3506 struct i915_address_space *vm,
3509 const struct i915_ggtt_view *ggtt_view) 3507 const struct i915_ggtt_view *ggtt_view)
3510{ 3508{
3511 struct i915_vma *vma; 3509 struct i915_vma *vma;
3512 3510
3513 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view)) 3511 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
3514 return ERR_PTR(-EINVAL); 3512 return ERR_PTR(-EINVAL);
3515 3513
3516 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL); 3514 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
3517 if (vma == NULL) 3515 if (vma == NULL)
3518 return ERR_PTR(-ENOMEM); 3516 return ERR_PTR(-ENOMEM);
3519 3517
3520 INIT_LIST_HEAD(&vma->vma_link); 3518 INIT_LIST_HEAD(&vma->vma_link);
3521 INIT_LIST_HEAD(&vma->mm_list); 3519 INIT_LIST_HEAD(&vma->mm_list);
3522 INIT_LIST_HEAD(&vma->exec_list); 3520 INIT_LIST_HEAD(&vma->exec_list);
3523 vma->vm = vm; 3521 vma->vm = vm;
3524 vma->obj = obj; 3522 vma->obj = obj;
3525 3523
3526 if (i915_is_ggtt(vm)) 3524 if (i915_is_ggtt(vm))
3527 vma->ggtt_view = *ggtt_view; 3525 vma->ggtt_view = *ggtt_view;
3528 3526
3529 list_add_tail(&vma->vma_link, &obj->vma_list); 3527 list_add_tail(&vma->vma_link, &obj->vma_list);
3530 if (!i915_is_ggtt(vm)) 3528 if (!i915_is_ggtt(vm))
3531 i915_ppgtt_get(i915_vm_to_ppgtt(vm)); 3529 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
3532 3530
3533 return vma; 3531 return vma;
3534} 3532}
3535 3533
3536struct i915_vma * 3534struct i915_vma *
3537i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj, 3535i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
3538 struct i915_address_space *vm) 3536 struct i915_address_space *vm)
3539{ 3537{
3540 struct i915_vma *vma; 3538 struct i915_vma *vma;
3541 3539
3542 vma = i915_gem_obj_to_vma(obj, vm); 3540 vma = i915_gem_obj_to_vma(obj, vm);
3543 if (!vma) 3541 if (!vma)
3544 vma = __i915_gem_vma_create(obj, vm, 3542 vma = __i915_gem_vma_create(obj, vm,
3545 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL); 3543 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
3546 3544
3547 return vma; 3545 return vma;
3548} 3546}
3549 3547
3550struct i915_vma * 3548struct i915_vma *
3551i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj, 3549i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
3552 const struct i915_ggtt_view *view) 3550 const struct i915_ggtt_view *view)
3553{ 3551{
3554 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj); 3552 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
3555 struct i915_vma *vma; 3553 struct i915_vma *vma;
3556 3554
3557 if (WARN_ON(!view)) 3555 if (WARN_ON(!view))
3558 return ERR_PTR(-EINVAL); 3556 return ERR_PTR(-EINVAL);
3559 3557
3560 vma = i915_gem_obj_to_ggtt_view(obj, view); 3558 vma = i915_gem_obj_to_ggtt_view(obj, view);
3561 3559
3562 if (IS_ERR(vma)) 3560 if (IS_ERR(vma))
3563 return vma; 3561 return vma;
3564 3562
3565 if (!vma) 3563 if (!vma)
3566 vma = __i915_gem_vma_create(obj, ggtt, view); 3564 vma = __i915_gem_vma_create(obj, ggtt, view);
3567 3565
3568 return vma; 3566 return vma;
3569 3567
3570} 3568}
3571 3569
3572static struct scatterlist * 3570static struct scatterlist *
3573rotate_pages(dma_addr_t *in, unsigned int offset, 3571rotate_pages(dma_addr_t *in, unsigned int offset,
3574 unsigned int width, unsigned int height, 3572 unsigned int width, unsigned int height,
3575 struct sg_table *st, struct scatterlist *sg) 3573 struct sg_table *st, struct scatterlist *sg)
3576{ 3574{
 3575#ifdef __NetBSD__
 3576 panic("XXX");
 3577#else
3577 unsigned int column, row; 3578 unsigned int column, row;
3578 unsigned int src_idx; 3579 unsigned int src_idx;
3579 3580
3580 if (!sg) { 3581 if (!sg) {
3581 st->nents = 0; 3582 st->nents = 0;
3582 sg = st->sgl; 3583 sg = st->sgl;
3583 } 3584 }
3584 3585
3585 for (column = 0; column < width; column++) { 3586 for (column = 0; column < width; column++) {
3586 src_idx = width * (height - 1) + column; 3587 src_idx = width * (height - 1) + column;
3587 for (row = 0; row < height; row++) { 3588 for (row = 0; row < height; row++) {
3588 st->nents++; 3589 st->nents++;
3589 /* We don't need the pages, but need to initialize 3590 /* We don't need the pages, but need to initialize
3590 * the entries so the sg list can be happily traversed. 3591 * the entries so the sg list can be happily traversed.
3591 * The only thing we need are DMA addresses. 3592 * The only thing we need are DMA addresses.
3592 */ 3593 */
3593 sg_set_page(sg, NULL, PAGE_SIZE, 0); 3594 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3594 sg_dma_address(sg) = in[offset + src_idx]; 3595 sg_dma_address(sg) = in[offset + src_idx];
3595 sg_dma_len(sg) = PAGE_SIZE; 3596 sg_dma_len(sg) = PAGE_SIZE;
3596 sg = sg_next(sg); 3597 sg = sg_next(sg);
3597 src_idx -= width; 3598 src_idx -= width;
3598 } 3599 }
3599 } 3600 }
3600 3601
3601 return sg; 3602 return sg;
 3603#endif
3602} 3604}
3603 3605
3604static struct sg_table * 3606static struct sg_table *
3605intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view, 3607intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
3606 struct drm_i915_gem_object *obj) 3608 struct drm_i915_gem_object *obj)
3607{ 3609{
 3610#ifdef __NetBSD__
 3611 panic("XXX");
 3612#else
3608 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info; 3613 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
3609 unsigned int size_pages = rot_info->size >> PAGE_SHIFT; 3614 unsigned int size_pages = rot_info->size >> PAGE_SHIFT;
3610 unsigned int size_pages_uv; 3615 unsigned int size_pages_uv;
3611 struct sg_page_iter sg_iter; 3616 struct sg_page_iter sg_iter;
3612 unsigned long i; 3617 unsigned long i;
3613 dma_addr_t *page_addr_list; 3618 dma_addr_t *page_addr_list;
3614 struct sg_table *st; 3619 struct sg_table *st;
3615 unsigned int uv_start_page; 3620 unsigned int uv_start_page;
3616 struct scatterlist *sg; 3621 struct scatterlist *sg;
3617 int ret = -ENOMEM; 3622 int ret = -ENOMEM;
3618 3623
3619 /* Allocate a temporary list of source pages for random access. */ 3624 /* Allocate a temporary list of source pages for random access. */
3620 page_addr_list = drm_malloc_ab(obj->base.size / PAGE_SIZE, 3625 page_addr_list = drm_malloc_ab(obj->base.size / PAGE_SIZE,
3621 sizeof(dma_addr_t)); 3626 sizeof(dma_addr_t));
3622 if (!page_addr_list) 3627 if (!page_addr_list)
3623 return ERR_PTR(ret); 3628 return ERR_PTR(ret);
3624 3629
3625 /* Account for UV plane with NV12. */ 3630 /* Account for UV plane with NV12. */
3626 if (rot_info->pixel_format == DRM_FORMAT_NV12) 3631 if (rot_info->pixel_format == DRM_FORMAT_NV12)
3627 size_pages_uv = rot_info->size_uv >> PAGE_SHIFT; 3632 size_pages_uv = rot_info->size_uv >> PAGE_SHIFT;
3628 else 3633 else
3629 size_pages_uv = 0; 3634 size_pages_uv = 0;
3630 3635
3631 /* Allocate target SG list. */ 3636 /* Allocate target SG list. */
3632 st = kmalloc(sizeof(*st), GFP_KERNEL); 3637 st = kmalloc(sizeof(*st), GFP_KERNEL);
3633 if (!st) 3638 if (!st)
3634 goto err_st_alloc; 3639 goto err_st_alloc;
3635 3640
3636 ret = sg_alloc_table(st, size_pages + size_pages_uv, GFP_KERNEL); 3641 ret = sg_alloc_table(st, size_pages + size_pages_uv, GFP_KERNEL);
3637 if (ret) 3642 if (ret)
3638 goto err_sg_alloc; 3643 goto err_sg_alloc;
3639 3644
3640 /* Populate source page list from the object. */ 3645 /* Populate source page list from the object. */
3641 i = 0; 3646 i = 0;
3642 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) { 3647 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
3643 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter); 3648 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
3644 i++; 3649 i++;
3645 } 3650 }
3646 3651
3647 /* Rotate the pages. */ 3652 /* Rotate the pages. */
3648 sg = rotate_pages(page_addr_list, 0, 3653 sg = rotate_pages(page_addr_list, 0,
3649 rot_info->width_pages, rot_info->height_pages, 3654 rot_info->width_pages, rot_info->height_pages,
3650 st, NULL); 3655 st, NULL);
3651 3656
3652 /* Append the UV plane if NV12. */ 3657 /* Append the UV plane if NV12. */
3653 if (rot_info->pixel_format == DRM_FORMAT_NV12) { 3658 if (rot_info->pixel_format == DRM_FORMAT_NV12) {
3654 uv_start_page = size_pages; 3659 uv_start_page = size_pages;
3655 3660
3656 /* Check for tile-row un-alignment. */ 3661 /* Check for tile-row un-alignment. */
3657 if (offset_in_page(rot_info->uv_offset)) 3662 if (offset_in_page(rot_info->uv_offset))
3658 uv_start_page--; 3663 uv_start_page--;
3659 3664
3660 rot_info->uv_start_page = uv_start_page; 3665 rot_info->uv_start_page = uv_start_page;
3661 3666
3662 rotate_pages(page_addr_list, uv_start_page, 3667 rotate_pages(page_addr_list, uv_start_page,
3663 rot_info->width_pages_uv, 3668 rot_info->width_pages_uv,
3664 rot_info->height_pages_uv, 3669 rot_info->height_pages_uv,
3665 st, sg); 3670 st, sg);
3666 } 3671 }
3667 3672
3668 DRM_DEBUG_KMS( 3673 DRM_DEBUG_KMS(
3669 "Created rotated page mapping for object size %zu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages (%u plane 0)).\n", 3674 "Created rotated page mapping for object size %zu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages (%u plane 0)).\n",
3670 obj->base.size, rot_info->pitch, rot_info->height, 3675 obj->base.size, rot_info->pitch, rot_info->height,
3671 rot_info->pixel_format, rot_info->width_pages, 3676 rot_info->pixel_format, rot_info->width_pages,
3672 rot_info->height_pages, size_pages + size_pages_uv, 3677 rot_info->height_pages, size_pages + size_pages_uv,
3673 size_pages); 3678 size_pages);
3674 3679
3675 drm_free_large(page_addr_list); 3680 drm_free_large(page_addr_list);
3676 3681
3677 return st; 3682 return st;
3678 3683
3679err_sg_alloc: 3684err_sg_alloc:
3680 kfree(st); 3685 kfree(st);
3681err_st_alloc: 3686err_st_alloc:
3682 drm_free_large(page_addr_list); 3687 drm_free_large(page_addr_list);
3683 3688
3684 DRM_DEBUG_KMS( 3689 DRM_DEBUG_KMS(
3685 "Failed to create rotated mapping for object size %zu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages (%u plane 0))\n", 3690 "Failed to create rotated mapping for object size %zu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages (%u plane 0))\n",
3686 obj->base.size, ret, rot_info->pitch, rot_info->height, 3691 obj->base.size, ret, rot_info->pitch, rot_info->height,
3687 rot_info->pixel_format, rot_info->width_pages, 3692 rot_info->pixel_format, rot_info->width_pages,
3688 rot_info->height_pages, size_pages + size_pages_uv, 3693 rot_info->height_pages, size_pages + size_pages_uv,
3689 size_pages); 3694 size_pages);
3690 return ERR_PTR(ret); 3695 return ERR_PTR(ret);
 3696#endif
3691} 3697}
3692 3698
3693static struct sg_table * 3699static struct sg_table *
3694intel_partial_pages(const struct i915_ggtt_view *view, 3700intel_partial_pages(const struct i915_ggtt_view *view,
3695 struct drm_i915_gem_object *obj) 3701 struct drm_i915_gem_object *obj)
3696{ 3702{
 3703#ifdef __NetBSD__
 3704 panic("XXX");
 3705#else
3697 struct sg_table *st; 3706 struct sg_table *st;
3698 struct scatterlist *sg; 3707 struct scatterlist *sg;
3699 struct sg_page_iter obj_sg_iter; 3708 struct sg_page_iter obj_sg_iter;
3700 int ret = -ENOMEM; 3709 int ret = -ENOMEM;
3701 3710
3702 st = kmalloc(sizeof(*st), GFP_KERNEL); 3711 st = kmalloc(sizeof(*st), GFP_KERNEL);
3703 if (!st) 3712 if (!st)
3704 goto err_st_alloc; 3713 goto err_st_alloc;
3705 3714
3706 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL); 3715 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
3707 if (ret) 3716 if (ret)
3708 goto err_sg_alloc; 3717 goto err_sg_alloc;
3709 3718
3710 sg = st->sgl; 3719 sg = st->sgl;
3711 st->nents = 0; 3720 st->nents = 0;
3712 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents, 3721 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
3713 view->params.partial.offset) 3722 view->params.partial.offset)
3714 { 3723 {
3715 if (st->nents >= view->params.partial.size) 3724 if (st->nents >= view->params.partial.size)
3716 break; 3725 break;
3717 3726
3718 sg_set_page(sg, NULL, PAGE_SIZE, 0); 3727 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3719 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter); 3728 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
3720 sg_dma_len(sg) = PAGE_SIZE; 3729 sg_dma_len(sg) = PAGE_SIZE;
3721 3730
3722 sg = sg_next(sg); 3731 sg = sg_next(sg);
3723 st->nents++; 3732 st->nents++;
3724 } 3733 }
3725 3734
3726 return st; 3735 return st;
3727 3736
3728err_sg_alloc: 3737err_sg_alloc:
3729 kfree(st); 3738 kfree(st);
3730err_st_alloc: 3739err_st_alloc:
3731 return ERR_PTR(ret); 3740 return ERR_PTR(ret);
 3741#endif
3732} 3742}
3733 3743
3734static int 3744static int
3735i915_get_ggtt_vma_pages(struct i915_vma *vma) 3745i915_get_ggtt_vma_pages(struct i915_vma *vma)
3736{ 3746{
3737 int ret = 0; 3747 int ret = 0;
3738 3748
3739 if (vma->ggtt_view.pages) 3749 if (vma->ggtt_view.pages)
3740 return 0; 3750 return 0;
3741 3751
3742 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) 3752 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
3743 vma->ggtt_view.pages = vma->obj->pages; 3753 vma->ggtt_view.pages = vma->obj->pages;
3744 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED) 3754 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
3745 vma->ggtt_view.pages = 3755 vma->ggtt_view.pages =
3746 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj); 3756 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
3747 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL) 3757 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
3748 vma->ggtt_view.pages = 3758 vma->ggtt_view.pages =
3749 intel_partial_pages(&vma->ggtt_view, vma->obj); 3759 intel_partial_pages(&vma->ggtt_view, vma->obj);
3750 else 3760 else
3751 WARN_ONCE(1, "GGTT view %u not implemented!\n", 3761 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3752 vma->ggtt_view.type); 3762 vma->ggtt_view.type);
3753 3763
3754 if (!vma->ggtt_view.pages) { 3764 if (!vma->ggtt_view.pages) {
3755 DRM_ERROR("Failed to get pages for GGTT view type %u!\n", 3765 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
3756 vma->ggtt_view.type); 3766 vma->ggtt_view.type);
3757 ret = -EINVAL; 3767 ret = -EINVAL;
3758 } else if (IS_ERR(vma->ggtt_view.pages)) { 3768 } else if (IS_ERR(vma->ggtt_view.pages)) {
3759 ret = PTR_ERR(vma->ggtt_view.pages); 3769 ret = PTR_ERR(vma->ggtt_view.pages);
3760 vma->ggtt_view.pages = NULL; 3770 vma->ggtt_view.pages = NULL;
3761 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n", 3771 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3762 vma->ggtt_view.type, ret); 3772 vma->ggtt_view.type, ret);
3763 } 3773 }
3764 3774
3765 return ret; 3775 return ret;
3766} 3776}
3767 3777
3768/** 3778/**
3769 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space. 3779 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3770 * @vma: VMA to map 3780 * @vma: VMA to map
3771 * @cache_level: mapping cache level 3781 * @cache_level: mapping cache level
3772 * @flags: flags like global or local mapping 3782 * @flags: flags like global or local mapping
3773 * 3783 *
3774 * DMA addresses are taken from the scatter-gather table of this object (or of 3784 * DMA addresses are taken from the scatter-gather table of this object (or of
3775 * this VMA in case of non-default GGTT views) and PTE entries set up. 3785 * this VMA in case of non-default GGTT views) and PTE entries set up.
3776 * Note that DMA addresses are also the only part of the SG table we care about. 3786 * Note that DMA addresses are also the only part of the SG table we care about.
3777 */ 3787 */
3778int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level, 3788int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3779 u32 flags) 3789 u32 flags)
3780{ 3790{
3781 int ret; 3791 int ret;
3782 u32 bind_flags; 3792 u32 bind_flags;
3783 3793
3784 if (WARN_ON(flags == 0)) 3794 if (WARN_ON(flags == 0))
3785 return -EINVAL; 3795 return -EINVAL;
3786 3796
3787 bind_flags = 0; 3797 bind_flags = 0;
3788 if (flags & PIN_GLOBAL) 3798 if (flags & PIN_GLOBAL)
3789 bind_flags |= GLOBAL_BIND; 3799 bind_flags |= GLOBAL_BIND;
3790 if (flags & PIN_USER) 3800 if (flags & PIN_USER)
3791 bind_flags |= LOCAL_BIND; 3801 bind_flags |= LOCAL_BIND;
3792 3802
3793 if (flags & PIN_UPDATE) 3803 if (flags & PIN_UPDATE)
3794 bind_flags |= vma->bound; 3804 bind_flags |= vma->bound;
3795 else 3805 else
3796 bind_flags &= ~vma->bound; 3806 bind_flags &= ~vma->bound;
3797 3807
3798 if (bind_flags == 0) 3808 if (bind_flags == 0)
3799 return 0; 3809 return 0;
3800 3810
3801 if (vma->bound == 0 && vma->vm->allocate_va_range) { 3811 if (vma->bound == 0 && vma->vm->allocate_va_range) {
3802 trace_i915_va_alloc(vma->vm, 3812 trace_i915_va_alloc(vma->vm,
3803 vma->node.start, 3813 vma->node.start,
3804 vma->node.size, 3814 vma->node.size,
3805 VM_TO_TRACE_NAME(vma->vm)); 3815 VM_TO_TRACE_NAME(vma->vm));
3806 3816
3807 /* XXX: i915_vma_pin() will fix this +- hack */ 3817 /* XXX: i915_vma_pin() will fix this +- hack */
3808 vma->pin_count++; 3818 vma->pin_count++;
3809 ret = vma->vm->allocate_va_range(vma->vm, 3819 ret = vma->vm->allocate_va_range(vma->vm,
3810 vma->node.start, 3820 vma->node.start,
3811 vma->node.size); 3821 vma->node.size);
3812 vma->pin_count--; 3822 vma->pin_count--;
3813 if (ret) 3823 if (ret)
3814 return ret; 3824 return ret;
3815 } 3825 }
3816 3826
3817 ret = vma->vm->bind_vma(vma, cache_level, bind_flags); 3827 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
3818 if (ret) 3828 if (ret)
3819 return ret; 3829 return ret;
3820 3830
3821 vma->bound |= bind_flags; 3831 vma->bound |= bind_flags;
3822 3832
3823 return 0; 3833 return 0;
3824} 3834}
3825 3835
3826/** 3836/**
3827 * i915_ggtt_view_size - Get the size of a GGTT view. 3837 * i915_ggtt_view_size - Get the size of a GGTT view.
3828 * @obj: Object the view is of. 3838 * @obj: Object the view is of.
3829 * @view: The view in question. 3839 * @view: The view in question.
3830 * 3840 *
3831 * @return The size of the GGTT view in bytes. 3841 * @return The size of the GGTT view in bytes.
3832 */ 3842 */
3833size_t 3843size_t
3834i915_ggtt_view_size(struct drm_i915_gem_object *obj, 3844i915_ggtt_view_size(struct drm_i915_gem_object *obj,
3835 const struct i915_ggtt_view *view) 3845 const struct i915_ggtt_view *view)
3836{ 3846{
3837 if (view->type == I915_GGTT_VIEW_NORMAL) { 3847 if (view->type == I915_GGTT_VIEW_NORMAL) {
3838 return obj->base.size; 3848 return obj->base.size;
3839 } else if (view->type == I915_GGTT_VIEW_ROTATED) { 3849 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3840 return view->rotation_info.size; 3850 return view->rotation_info.size;
3841 } else if (view->type == I915_GGTT_VIEW_PARTIAL) { 3851 } else if (view->type == I915_GGTT_VIEW_PARTIAL) {
3842 return view->params.partial.size << PAGE_SHIFT; 3852 return view->params.partial.size << PAGE_SHIFT;
3843 } else { 3853 } else {
3844 WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type); 3854 WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type);
3845 return obj->base.size; 3855 return obj->base.size;
3846 } 3856 }
3847} 3857}

cvs diff -r1.5 -r1.6 src/sys/external/bsd/drm2/dist/drm/i915/Attic/i915_gem_stolen.c (switch to unified diff)

--- src/sys/external/bsd/drm2/dist/drm/i915/Attic/i915_gem_stolen.c 2018/08/27 04:58:23 1.5
+++ src/sys/external/bsd/drm2/dist/drm/i915/Attic/i915_gem_stolen.c 2018/08/27 06:08:25 1.6
@@ -1,776 +1,784 @@ @@ -1,776 +1,784 @@
1/* $NetBSD: i915_gem_stolen.c,v 1.5 2018/08/27 04:58:23 riastradh Exp $ */ 1/* $NetBSD: i915_gem_stolen.c,v 1.6 2018/08/27 06:08:25 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright © 2008-2012 Intel Corporation 4 * Copyright © 2008-2012 Intel Corporation
5 * 5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a 6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"), 7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation 8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the 10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions: 11 * Software is furnished to do so, subject to the following conditions:
12 * 12 *
13 * The above copyright notice and this permission notice (including the next 13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the 14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software. 15 * Software.
16 * 16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE. 23 * IN THE SOFTWARE.
24 * 24 *
25 * Authors: 25 * Authors:
26 * Eric Anholt <eric@anholt.net> 26 * Eric Anholt <eric@anholt.net>
27 * Chris Wilson <chris@chris-wilson.co.uk> 27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * 28 *
29 */ 29 */
30 30
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32__KERNEL_RCSID(0, "$NetBSD: i915_gem_stolen.c,v 1.5 2018/08/27 04:58:23 riastradh Exp $"); 32__KERNEL_RCSID(0, "$NetBSD: i915_gem_stolen.c,v 1.6 2018/08/27 06:08:25 riastradh Exp $");
33 33
34#include <linux/printk.h> 34#include <linux/printk.h>
35#include <linux/err.h> 35#include <linux/err.h>
36#include <drm/drmP.h> 36#include <drm/drmP.h>
37#include <drm/i915_drm.h> 37#include <drm/i915_drm.h>
38#include "i915_drv.h" 38#include "i915_drv.h"
39 39
40#define KB(x) ((x) * 1024) 40#define KB(x) ((x) * 1024)
41#define MB(x) (KB(x) * 1024) 41#define MB(x) (KB(x) * 1024)
42 42
43/* 43/*
44 * The BIOS typically reserves some of the system's memory for the exclusive 44 * The BIOS typically reserves some of the system's memory for the exclusive
45 * use of the integrated graphics. This memory is no longer available for 45 * use of the integrated graphics. This memory is no longer available for
46 * use by the OS and so the user finds that his system has less memory 46 * use by the OS and so the user finds that his system has less memory
47 * available than he put in. We refer to this memory as stolen. 47 * available than he put in. We refer to this memory as stolen.
48 * 48 *
49 * The BIOS will allocate its framebuffer from the stolen memory. Our 49 * The BIOS will allocate its framebuffer from the stolen memory. Our
50 * goal is try to reuse that object for our own fbcon which must always 50 * goal is try to reuse that object for our own fbcon which must always
51 * be available for panics. Anything else we can reuse the stolen memory 51 * be available for panics. Anything else we can reuse the stolen memory
52 * for is a boon. 52 * for is a boon.
53 */ 53 */
54 54
55int i915_gem_stolen_insert_node_in_range(struct drm_i915_private *dev_priv, 55int i915_gem_stolen_insert_node_in_range(struct drm_i915_private *dev_priv,
56 struct drm_mm_node *node, u64 size, 56 struct drm_mm_node *node, u64 size,
57 unsigned alignment, u64 start, u64 end) 57 unsigned alignment, u64 start, u64 end)
58{ 58{
59 int ret; 59 int ret;
60 60
61 if (!drm_mm_initialized(&dev_priv->mm.stolen)) 61 if (!drm_mm_initialized(&dev_priv->mm.stolen))
62 return -ENODEV; 62 return -ENODEV;
63 63
64 /* See the comment at the drm_mm_init() call for more about this check. 64 /* See the comment at the drm_mm_init() call for more about this check.
65 * WaSkipStolenMemoryFirstPage:bdw,chv (incomplete) */ 65 * WaSkipStolenMemoryFirstPage:bdw,chv (incomplete) */
66 if (INTEL_INFO(dev_priv)->gen == 8 && start < 4096) 66 if (INTEL_INFO(dev_priv)->gen == 8 && start < 4096)
67 start = 4096; 67 start = 4096;
68 68
69 mutex_lock(&dev_priv->mm.stolen_lock); 69 mutex_lock(&dev_priv->mm.stolen_lock);
70 ret = drm_mm_insert_node_in_range(&dev_priv->mm.stolen, node, size, 70 ret = drm_mm_insert_node_in_range(&dev_priv->mm.stolen, node, size,
71 alignment, start, end, 71 alignment, start, end,
72 DRM_MM_SEARCH_DEFAULT); 72 DRM_MM_SEARCH_DEFAULT);
73 mutex_unlock(&dev_priv->mm.stolen_lock); 73 mutex_unlock(&dev_priv->mm.stolen_lock);
74 74
75 return ret; 75 return ret;
76} 76}
77 77
78int i915_gem_stolen_insert_node(struct drm_i915_private *dev_priv, 78int i915_gem_stolen_insert_node(struct drm_i915_private *dev_priv,
79 struct drm_mm_node *node, u64 size, 79 struct drm_mm_node *node, u64 size,
80 unsigned alignment) 80 unsigned alignment)
81{ 81{
82 return i915_gem_stolen_insert_node_in_range(dev_priv, node, size, 82 return i915_gem_stolen_insert_node_in_range(dev_priv, node, size,
83 alignment, 0, 83 alignment, 0,
84 dev_priv->gtt.stolen_usable_size); 84 dev_priv->gtt.stolen_usable_size);
85} 85}
86 86
87void i915_gem_stolen_remove_node(struct drm_i915_private *dev_priv, 87void i915_gem_stolen_remove_node(struct drm_i915_private *dev_priv,
88 struct drm_mm_node *node) 88 struct drm_mm_node *node)
89{ 89{
90 mutex_lock(&dev_priv->mm.stolen_lock); 90 mutex_lock(&dev_priv->mm.stolen_lock);
91 drm_mm_remove_node(node); 91 drm_mm_remove_node(node);
92 mutex_unlock(&dev_priv->mm.stolen_lock); 92 mutex_unlock(&dev_priv->mm.stolen_lock);
93} 93}
94 94
95static unsigned long i915_stolen_to_physical(struct drm_device *dev) 95static unsigned long i915_stolen_to_physical(struct drm_device *dev)
96{ 96{
97#ifndef __NetBSD__ 97#ifndef __NetBSD__
98 struct drm_i915_private *dev_priv = dev->dev_private; 98 struct drm_i915_private *dev_priv = dev->dev_private;
99 struct resource *r; 99 struct resource *r;
100#endif 100#endif
101 u32 base; 101 u32 base;
102 102
103 /* Almost universally we can find the Graphics Base of Stolen Memory 103 /* Almost universally we can find the Graphics Base of Stolen Memory
104 * at offset 0x5c in the igfx configuration space. On a few (desktop) 104 * at offset 0x5c in the igfx configuration space. On a few (desktop)
105 * machines this is also mirrored in the bridge device at different 105 * machines this is also mirrored in the bridge device at different
106 * locations, or in the MCHBAR. 106 * locations, or in the MCHBAR.
107 * 107 *
108 * On 865 we just check the TOUD register. 108 * On 865 we just check the TOUD register.
109 * 109 *
110 * On 830/845/85x the stolen memory base isn't available in any 110 * On 830/845/85x the stolen memory base isn't available in any
111 * register. We need to calculate it as TOM-TSEG_SIZE-stolen_size. 111 * register. We need to calculate it as TOM-TSEG_SIZE-stolen_size.
112 * 112 *
113 */ 113 */
114 base = 0; 114 base = 0;
115 if (INTEL_INFO(dev)->gen >= 3) { 115 if (INTEL_INFO(dev)->gen >= 3) {
116#ifndef __NetBSD__ /* XXX disable this for now */ 116#ifndef __NetBSD__ /* XXX disable this for now */
117 /* Read Graphics Base of Stolen Memory directly */ 117 /* Read Graphics Base of Stolen Memory directly */
118 pci_read_config_dword(dev->pdev, 0x5c, &base); 118 pci_read_config_dword(dev->pdev, 0x5c, &base);
119 base &= ~((1<<20) - 1); 119 base &= ~((1<<20) - 1);
120#endif 120#endif
121 } else if (IS_I865G(dev)) { 121 } else if (IS_I865G(dev)) {
122 u32 tseg_size = 0; 122 u32 tseg_size = 0;
123 u16 toud = 0; 123 u16 toud = 0;
124 u8 tmp; 124 u8 tmp;
125 125
126 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0), 126 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
127 I845_ESMRAMC, &tmp); 127 I845_ESMRAMC, &tmp);
128 128
129 if (tmp & TSEG_ENABLE) { 129 if (tmp & TSEG_ENABLE) {
130 switch (tmp & I845_TSEG_SIZE_MASK) { 130 switch (tmp & I845_TSEG_SIZE_MASK) {
131 case I845_TSEG_SIZE_512K: 131 case I845_TSEG_SIZE_512K:
132 tseg_size = KB(512); 132 tseg_size = KB(512);
133 break; 133 break;
134 case I845_TSEG_SIZE_1M: 134 case I845_TSEG_SIZE_1M:
135 tseg_size = MB(1); 135 tseg_size = MB(1);
136 break; 136 break;
137 } 137 }
138 } 138 }
139 139
140 pci_bus_read_config_word(dev->pdev->bus, PCI_DEVFN(0, 0), 140 pci_bus_read_config_word(dev->pdev->bus, PCI_DEVFN(0, 0),
141 I865_TOUD, &toud); 141 I865_TOUD, &toud);
142 142
143 base = (toud << 16) + tseg_size; 143 base = (toud << 16) + tseg_size;
144 } else if (IS_I85X(dev)) { 144 } else if (IS_I85X(dev)) {
145 u32 tseg_size = 0; 145 u32 tseg_size = 0;
146 u32 tom; 146 u32 tom;
147 u8 tmp; 147 u8 tmp;
148 148
149 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0), 149 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
150 I85X_ESMRAMC, &tmp); 150 I85X_ESMRAMC, &tmp);
151 151
152 if (tmp & TSEG_ENABLE) 152 if (tmp & TSEG_ENABLE)
153 tseg_size = MB(1); 153 tseg_size = MB(1);
154 154
155 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 1), 155 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 1),
156 I85X_DRB3, &tmp); 156 I85X_DRB3, &tmp);
157 tom = tmp * MB(32); 157 tom = tmp * MB(32);
158 158
159 base = tom - tseg_size - dev_priv->gtt.stolen_size; 159 base = tom - tseg_size - dev_priv->gtt.stolen_size;
160 } else if (IS_845G(dev)) { 160 } else if (IS_845G(dev)) {
161 u32 tseg_size = 0; 161 u32 tseg_size = 0;
162 u32 tom; 162 u32 tom;
163 u8 tmp; 163 u8 tmp;
164 164
165 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0), 165 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
166 I845_ESMRAMC, &tmp); 166 I845_ESMRAMC, &tmp);
167 167
168 if (tmp & TSEG_ENABLE) { 168 if (tmp & TSEG_ENABLE) {
169 switch (tmp & I845_TSEG_SIZE_MASK) { 169 switch (tmp & I845_TSEG_SIZE_MASK) {
170 case I845_TSEG_SIZE_512K: 170 case I845_TSEG_SIZE_512K:
171 tseg_size = KB(512); 171 tseg_size = KB(512);
172 break; 172 break;
173 case I845_TSEG_SIZE_1M: 173 case I845_TSEG_SIZE_1M:
174 tseg_size = MB(1); 174 tseg_size = MB(1);
175 break; 175 break;
176 } 176 }
177 } 177 }
178 178
179 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0), 179 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
180 I830_DRB3, &tmp); 180 I830_DRB3, &tmp);
181 tom = tmp * MB(32); 181 tom = tmp * MB(32);
182 182
183 base = tom - tseg_size - dev_priv->gtt.stolen_size; 183 base = tom - tseg_size - dev_priv->gtt.stolen_size;
184 } else if (IS_I830(dev)) { 184 } else if (IS_I830(dev)) {
185 u32 tseg_size = 0; 185 u32 tseg_size = 0;
186 u32 tom; 186 u32 tom;
187 u8 tmp; 187 u8 tmp;
188 188
189 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0), 189 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
190 I830_ESMRAMC, &tmp); 190 I830_ESMRAMC, &tmp);
191 191
192 if (tmp & TSEG_ENABLE) { 192 if (tmp & TSEG_ENABLE) {
193 if (tmp & I830_TSEG_SIZE_1M) 193 if (tmp & I830_TSEG_SIZE_1M)
194 tseg_size = MB(1); 194 tseg_size = MB(1);
195 else 195 else
196 tseg_size = KB(512); 196 tseg_size = KB(512);
197 } 197 }
198 198
199 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0), 199 pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
200 I830_DRB3, &tmp); 200 I830_DRB3, &tmp);
201 tom = tmp * MB(32); 201 tom = tmp * MB(32);
202 202
203 base = tom - tseg_size - dev_priv->gtt.stolen_size; 203 base = tom - tseg_size - dev_priv->gtt.stolen_size;
204 } 204 }
205 205
206 if (base == 0) 206 if (base == 0)
207 return 0; 207 return 0;
208 208
209 /* make sure we don't clobber the GTT if it's within stolen memory */ 209 /* make sure we don't clobber the GTT if it's within stolen memory */
210 if (INTEL_INFO(dev)->gen <= 4 && !IS_G33(dev) && !IS_G4X(dev)) { 210 if (INTEL_INFO(dev)->gen <= 4 && !IS_G33(dev) && !IS_G4X(dev)) {
211 struct { 211 struct {
212 u32 start, end; 212 u32 start, end;
213 } stolen[2] = { 213 } stolen[2] = {
214 { .start = base, .end = base + dev_priv->gtt.stolen_size, }, 214 { .start = base, .end = base + dev_priv->gtt.stolen_size, },
215 { .start = base, .end = base + dev_priv->gtt.stolen_size, }, 215 { .start = base, .end = base + dev_priv->gtt.stolen_size, },
216 }; 216 };
217 u64 gtt_start, gtt_end; 217 u64 gtt_start, gtt_end;
218 218
219 gtt_start = I915_READ(PGTBL_CTL); 219 gtt_start = I915_READ(PGTBL_CTL);
220 if (IS_GEN4(dev)) 220 if (IS_GEN4(dev))
221 gtt_start = (gtt_start & PGTBL_ADDRESS_LO_MASK) | 221 gtt_start = (gtt_start & PGTBL_ADDRESS_LO_MASK) |
222 (gtt_start & PGTBL_ADDRESS_HI_MASK) << 28; 222 (gtt_start & PGTBL_ADDRESS_HI_MASK) << 28;
223 else 223 else
224 gtt_start &= PGTBL_ADDRESS_LO_MASK; 224 gtt_start &= PGTBL_ADDRESS_LO_MASK;
225 gtt_end = gtt_start + gtt_total_entries(dev_priv->gtt) * 4; 225 gtt_end = gtt_start + gtt_total_entries(dev_priv->gtt) * 4;
226 226
227 if (gtt_start >= stolen[0].start && gtt_start < stolen[0].end) 227 if (gtt_start >= stolen[0].start && gtt_start < stolen[0].end)
228 stolen[0].end = gtt_start; 228 stolen[0].end = gtt_start;
229 if (gtt_end > stolen[1].start && gtt_end <= stolen[1].end) 229 if (gtt_end > stolen[1].start && gtt_end <= stolen[1].end)
230 stolen[1].start = gtt_end; 230 stolen[1].start = gtt_end;
231 231
232 /* pick the larger of the two chunks */ 232 /* pick the larger of the two chunks */
233 if (stolen[0].end - stolen[0].start > 233 if (stolen[0].end - stolen[0].start >
234 stolen[1].end - stolen[1].start) { 234 stolen[1].end - stolen[1].start) {
235 base = stolen[0].start; 235 base = stolen[0].start;
236 dev_priv->gtt.stolen_size = stolen[0].end - stolen[0].start; 236 dev_priv->gtt.stolen_size = stolen[0].end - stolen[0].start;
237 } else { 237 } else {
238 base = stolen[1].start; 238 base = stolen[1].start;
239 dev_priv->gtt.stolen_size = stolen[1].end - stolen[1].start; 239 dev_priv->gtt.stolen_size = stolen[1].end - stolen[1].start;
240 } 240 }
241 241
242 if (stolen[0].start != stolen[1].start || 242 if (stolen[0].start != stolen[1].start ||
243 stolen[0].end != stolen[1].end) { 243 stolen[0].end != stolen[1].end) {
244 DRM_DEBUG_KMS("GTT within stolen memory at 0x%llx-0x%llx\n", 244 DRM_DEBUG_KMS("GTT within stolen memory at 0x%llx-0x%llx\n",
245 (unsigned long long) gtt_start, 245 (unsigned long long) gtt_start,
246 (unsigned long long) gtt_end - 1); 246 (unsigned long long) gtt_end - 1);
247 DRM_DEBUG_KMS("Stolen memory adjusted to 0x%x-0x%x\n", 247 DRM_DEBUG_KMS("Stolen memory adjusted to 0x%x-0x%x\n",
248 base, base + (u32) dev_priv->gtt.stolen_size - 1); 248 base, base + (u32) dev_priv->gtt.stolen_size - 1);
249 } 249 }
250 } 250 }
251 251
252 252
253#ifndef __NetBSD__ /* XXX */ 253#ifndef __NetBSD__ /* XXX */
254 /* Verify that nothing else uses this physical address. Stolen 254 /* Verify that nothing else uses this physical address. Stolen
255 * memory should be reserved by the BIOS and hidden from the 255 * memory should be reserved by the BIOS and hidden from the
256 * kernel. So if the region is already marked as busy, something 256 * kernel. So if the region is already marked as busy, something
257 * is seriously wrong. 257 * is seriously wrong.
258 */ 258 */
259 r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size, 259 r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size,
260 "Graphics Stolen Memory"); 260 "Graphics Stolen Memory");
261 if (r == NULL) { 261 if (r == NULL) {
262 /* 262 /*
263 * One more attempt but this time requesting region from 263 * One more attempt but this time requesting region from
264 * base + 1, as we have seen that this resolves the region 264 * base + 1, as we have seen that this resolves the region
265 * conflict with the PCI Bus. 265 * conflict with the PCI Bus.
266 * This is a BIOS w/a: Some BIOS wrap stolen in the root 266 * This is a BIOS w/a: Some BIOS wrap stolen in the root
267 * PCI bus, but have an off-by-one error. Hence retry the 267 * PCI bus, but have an off-by-one error. Hence retry the
268 * reservation starting from 1 instead of 0. 268 * reservation starting from 1 instead of 0.
269 */ 269 */
270 r = devm_request_mem_region(dev->dev, base + 1, 270 r = devm_request_mem_region(dev->dev, base + 1,
271 dev_priv->gtt.stolen_size - 1, 271 dev_priv->gtt.stolen_size - 1,
272 "Graphics Stolen Memory"); 272 "Graphics Stolen Memory");
273 /* 273 /*
274 * GEN3 firmware likes to smash pci bridges into the stolen 274 * GEN3 firmware likes to smash pci bridges into the stolen
275 * range. Apparently this works. 275 * range. Apparently this works.
276 */ 276 */
277 if (r == NULL && !IS_GEN3(dev)) { 277 if (r == NULL && !IS_GEN3(dev)) {
278 DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n", 278 DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n",
279 base, base + (uint32_t)dev_priv->gtt.stolen_size); 279 base, base + (uint32_t)dev_priv->gtt.stolen_size);
280 base = 0; 280 base = 0;
281 } 281 }
282 } 282 }
283#endif 283#endif
284 284
285 return base; 285 return base;
286} 286}
287 287
288void i915_gem_cleanup_stolen(struct drm_device *dev) 288void i915_gem_cleanup_stolen(struct drm_device *dev)
289{ 289{
290 struct drm_i915_private *dev_priv = dev->dev_private; 290 struct drm_i915_private *dev_priv = dev->dev_private;
291 291
292 if (!drm_mm_initialized(&dev_priv->mm.stolen)) 292 if (!drm_mm_initialized(&dev_priv->mm.stolen))
293 return; 293 return;
294 294
295 drm_mm_takedown(&dev_priv->mm.stolen); 295 drm_mm_takedown(&dev_priv->mm.stolen);
296} 296}
297 297
298static void g4x_get_stolen_reserved(struct drm_i915_private *dev_priv, 298static void g4x_get_stolen_reserved(struct drm_i915_private *dev_priv,
299 unsigned long *base, unsigned long *size) 299 unsigned long *base, unsigned long *size)
300{ 300{
301 uint32_t reg_val = I915_READ(IS_GM45(dev_priv) ? 301 uint32_t reg_val = I915_READ(IS_GM45(dev_priv) ?
302 CTG_STOLEN_RESERVED : 302 CTG_STOLEN_RESERVED :
303 ELK_STOLEN_RESERVED); 303 ELK_STOLEN_RESERVED);
304 unsigned long stolen_top = dev_priv->mm.stolen_base + 304 unsigned long stolen_top = dev_priv->mm.stolen_base +
305 dev_priv->gtt.stolen_size; 305 dev_priv->gtt.stolen_size;
306 306
307 *base = (reg_val & G4X_STOLEN_RESERVED_ADDR2_MASK) << 16; 307 *base = (reg_val & G4X_STOLEN_RESERVED_ADDR2_MASK) << 16;
308 308
309 WARN_ON((reg_val & G4X_STOLEN_RESERVED_ADDR1_MASK) < *base); 309 WARN_ON((reg_val & G4X_STOLEN_RESERVED_ADDR1_MASK) < *base);
310 310
311 /* On these platforms, the register doesn't have a size field, so the 311 /* On these platforms, the register doesn't have a size field, so the
312 * size is the distance between the base and the top of the stolen 312 * size is the distance between the base and the top of the stolen
313 * memory. We also have the genuine case where base is zero and there's 313 * memory. We also have the genuine case where base is zero and there's
314 * nothing reserved. */ 314 * nothing reserved. */
315 if (*base == 0) 315 if (*base == 0)
316 *size = 0; 316 *size = 0;
317 else 317 else
318 *size = stolen_top - *base; 318 *size = stolen_top - *base;
319} 319}
320 320
321static void gen6_get_stolen_reserved(struct drm_i915_private *dev_priv, 321static void gen6_get_stolen_reserved(struct drm_i915_private *dev_priv,
322 unsigned long *base, unsigned long *size) 322 unsigned long *base, unsigned long *size)
323{ 323{
324 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED); 324 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
325 325
326 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK; 326 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
327 327
328 switch (reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK) { 328 switch (reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK) {
329 case GEN6_STOLEN_RESERVED_1M: 329 case GEN6_STOLEN_RESERVED_1M:
330 *size = 1024 * 1024; 330 *size = 1024 * 1024;
331 break; 331 break;
332 case GEN6_STOLEN_RESERVED_512K: 332 case GEN6_STOLEN_RESERVED_512K:
333 *size = 512 * 1024; 333 *size = 512 * 1024;
334 break; 334 break;
335 case GEN6_STOLEN_RESERVED_256K: 335 case GEN6_STOLEN_RESERVED_256K:
336 *size = 256 * 1024; 336 *size = 256 * 1024;
337 break; 337 break;
338 case GEN6_STOLEN_RESERVED_128K: 338 case GEN6_STOLEN_RESERVED_128K:
339 *size = 128 * 1024; 339 *size = 128 * 1024;
340 break; 340 break;
341 default: 341 default:
342 *size = 1024 * 1024; 342 *size = 1024 * 1024;
343 MISSING_CASE(reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK); 343 MISSING_CASE(reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK);
344 } 344 }
345} 345}
346 346
347static void gen7_get_stolen_reserved(struct drm_i915_private *dev_priv, 347static void gen7_get_stolen_reserved(struct drm_i915_private *dev_priv,
348 unsigned long *base, unsigned long *size) 348 unsigned long *base, unsigned long *size)
349{ 349{
350 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED); 350 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
351 351
352 *base = reg_val & GEN7_STOLEN_RESERVED_ADDR_MASK; 352 *base = reg_val & GEN7_STOLEN_RESERVED_ADDR_MASK;
353 353
354 switch (reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK) { 354 switch (reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK) {
355 case GEN7_STOLEN_RESERVED_1M: 355 case GEN7_STOLEN_RESERVED_1M:
356 *size = 1024 * 1024; 356 *size = 1024 * 1024;
357 break; 357 break;
358 case GEN7_STOLEN_RESERVED_256K: 358 case GEN7_STOLEN_RESERVED_256K:
359 *size = 256 * 1024; 359 *size = 256 * 1024;
360 break; 360 break;
361 default: 361 default:
362 *size = 1024 * 1024; 362 *size = 1024 * 1024;
363 MISSING_CASE(reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK); 363 MISSING_CASE(reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK);
364 } 364 }
365} 365}
366 366
367static void gen8_get_stolen_reserved(struct drm_i915_private *dev_priv, 367static void gen8_get_stolen_reserved(struct drm_i915_private *dev_priv,
368 unsigned long *base, unsigned long *size) 368 unsigned long *base, unsigned long *size)
369{ 369{
370 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED); 370 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
371 371
372 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK; 372 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
373 373
374 switch (reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK) { 374 switch (reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK) {
375 case GEN8_STOLEN_RESERVED_1M: 375 case GEN8_STOLEN_RESERVED_1M:
376 *size = 1024 * 1024; 376 *size = 1024 * 1024;
377 break; 377 break;
378 case GEN8_STOLEN_RESERVED_2M: 378 case GEN8_STOLEN_RESERVED_2M:
379 *size = 2 * 1024 * 1024; 379 *size = 2 * 1024 * 1024;
380 break; 380 break;
381 case GEN8_STOLEN_RESERVED_4M: 381 case GEN8_STOLEN_RESERVED_4M:
382 *size = 4 * 1024 * 1024; 382 *size = 4 * 1024 * 1024;
383 break; 383 break;
384 case GEN8_STOLEN_RESERVED_8M: 384 case GEN8_STOLEN_RESERVED_8M:
385 *size = 8 * 1024 * 1024; 385 *size = 8 * 1024 * 1024;
386 break; 386 break;
387 default: 387 default:
388 *size = 8 * 1024 * 1024; 388 *size = 8 * 1024 * 1024;
389 MISSING_CASE(reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK); 389 MISSING_CASE(reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK);
390 } 390 }
391} 391}
392 392
393static void bdw_get_stolen_reserved(struct drm_i915_private *dev_priv, 393static void bdw_get_stolen_reserved(struct drm_i915_private *dev_priv,
394 unsigned long *base, unsigned long *size) 394 unsigned long *base, unsigned long *size)
395{ 395{
396 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED); 396 uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
397 unsigned long stolen_top; 397 unsigned long stolen_top;
398 398
399 stolen_top = dev_priv->mm.stolen_base + dev_priv->gtt.stolen_size; 399 stolen_top = dev_priv->mm.stolen_base + dev_priv->gtt.stolen_size;
400 400
401 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK; 401 *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
402 402
403 /* On these platforms, the register doesn't have a size field, so the 403 /* On these platforms, the register doesn't have a size field, so the
404 * size is the distance between the base and the top of the stolen 404 * size is the distance between the base and the top of the stolen
405 * memory. We also have the genuine case where base is zero and there's 405 * memory. We also have the genuine case where base is zero and there's
406 * nothing reserved. */ 406 * nothing reserved. */
407 if (*base == 0) 407 if (*base == 0)
408 *size = 0; 408 *size = 0;
409 else 409 else
410 *size = stolen_top - *base; 410 *size = stolen_top - *base;
411} 411}
412 412
413int i915_gem_init_stolen(struct drm_device *dev) 413int i915_gem_init_stolen(struct drm_device *dev)
414{ 414{
415 struct drm_i915_private *dev_priv = dev->dev_private; 415 struct drm_i915_private *dev_priv = dev->dev_private;
416 unsigned long reserved_total, reserved_base = 0, reserved_size; 416 unsigned long reserved_total, reserved_base = 0, reserved_size;
417 unsigned long stolen_top; 417 unsigned long stolen_top;
418 418
 419#ifdef __NetBSD__
 420 linux_mutex_init(&dev_priv->mm.stolen_lock);
 421#else
419 mutex_init(&dev_priv->mm.stolen_lock); 422 mutex_init(&dev_priv->mm.stolen_lock);
 423#endif
420 424
421#ifdef CONFIG_INTEL_IOMMU 425#ifdef CONFIG_INTEL_IOMMU
422 if (intel_iommu_gfx_mapped && INTEL_INFO(dev)->gen < 8) { 426 if (intel_iommu_gfx_mapped && INTEL_INFO(dev)->gen < 8) {
423 DRM_INFO("DMAR active, disabling use of stolen memory\n"); 427 DRM_INFO("DMAR active, disabling use of stolen memory\n");
424 return 0; 428 return 0;
425 } 429 }
426#endif 430#endif
427 431
428 if (dev_priv->gtt.stolen_size == 0) 432 if (dev_priv->gtt.stolen_size == 0)
429 return 0; 433 return 0;
430 434
431 dev_priv->mm.stolen_base = i915_stolen_to_physical(dev); 435 dev_priv->mm.stolen_base = i915_stolen_to_physical(dev);
432 if (dev_priv->mm.stolen_base == 0) 436 if (dev_priv->mm.stolen_base == 0)
433 return 0; 437 return 0;
434 438
435 stolen_top = dev_priv->mm.stolen_base + dev_priv->gtt.stolen_size; 439 stolen_top = dev_priv->mm.stolen_base + dev_priv->gtt.stolen_size;
436 440
437 switch (INTEL_INFO(dev_priv)->gen) { 441 switch (INTEL_INFO(dev_priv)->gen) {
438 case 2: 442 case 2:
439 case 3: 443 case 3:
440 break; 444 break;
441 case 4: 445 case 4:
442 if (IS_G4X(dev)) 446 if (IS_G4X(dev))
443 g4x_get_stolen_reserved(dev_priv, &reserved_base, 447 g4x_get_stolen_reserved(dev_priv, &reserved_base,
444 &reserved_size); 448 &reserved_size);
445 break; 449 break;
446 case 5: 450 case 5:
447 /* Assume the gen6 maximum for the older platforms. */ 451 /* Assume the gen6 maximum for the older platforms. */
448 reserved_size = 1024 * 1024; 452 reserved_size = 1024 * 1024;
449 reserved_base = stolen_top - reserved_size; 453 reserved_base = stolen_top - reserved_size;
450 break; 454 break;
451 case 6: 455 case 6:
452 gen6_get_stolen_reserved(dev_priv, &reserved_base, 456 gen6_get_stolen_reserved(dev_priv, &reserved_base,
453 &reserved_size); 457 &reserved_size);
454 break; 458 break;
455 case 7: 459 case 7:
456 gen7_get_stolen_reserved(dev_priv, &reserved_base, 460 gen7_get_stolen_reserved(dev_priv, &reserved_base,
457 &reserved_size); 461 &reserved_size);
458 break; 462 break;
459 default: 463 default:
460 if (IS_BROADWELL(dev_priv) || IS_SKYLAKE(dev_priv)) 464 if (IS_BROADWELL(dev_priv) || IS_SKYLAKE(dev_priv))
461 bdw_get_stolen_reserved(dev_priv, &reserved_base, 465 bdw_get_stolen_reserved(dev_priv, &reserved_base,
462 &reserved_size); 466 &reserved_size);
463 else 467 else
464 gen8_get_stolen_reserved(dev_priv, &reserved_base, 468 gen8_get_stolen_reserved(dev_priv, &reserved_base,
465 &reserved_size); 469 &reserved_size);
466 break; 470 break;
467 } 471 }
468 472
469 /* It is possible for the reserved base to be zero, but the register 473 /* It is possible for the reserved base to be zero, but the register
470 * field for size doesn't have a zero option. */ 474 * field for size doesn't have a zero option. */
471 if (reserved_base == 0) { 475 if (reserved_base == 0) {
472 reserved_size = 0; 476 reserved_size = 0;
473 reserved_base = stolen_top; 477 reserved_base = stolen_top;
474 } 478 }
475 479
476 if (reserved_base < dev_priv->mm.stolen_base || 480 if (reserved_base < dev_priv->mm.stolen_base ||
477 reserved_base + reserved_size > stolen_top) { 481 reserved_base + reserved_size > stolen_top) {
478 DRM_DEBUG_KMS("Stolen reserved area [0x%08lx - 0x%08lx] outside stolen memory [0x%08lx - 0x%08lx]\n", 482 DRM_DEBUG_KMS("Stolen reserved area [0x%08lx - 0x%08lx] outside stolen memory [0x%08lx - 0x%08lx]\n",
479 reserved_base, reserved_base + reserved_size, 483 reserved_base, reserved_base + reserved_size,
480 dev_priv->mm.stolen_base, stolen_top); 484 dev_priv->mm.stolen_base, stolen_top);
481 return 0; 485 return 0;
482 } 486 }
483 487
484 /* It is possible for the reserved area to end before the end of stolen 488 /* It is possible for the reserved area to end before the end of stolen
485 * memory, so just consider the start. */ 489 * memory, so just consider the start. */
486 reserved_total = stolen_top - reserved_base; 490 reserved_total = stolen_top - reserved_base;
487 491
488 DRM_DEBUG_KMS("Memory reserved for graphics device: %zuK, usable: %luK\n", 492 DRM_DEBUG_KMS("Memory reserved for graphics device: %zuK, usable: %luK\n",
489 dev_priv->gtt.stolen_size >> 10, 493 dev_priv->gtt.stolen_size >> 10,
490 (dev_priv->gtt.stolen_size - reserved_total) >> 10); 494 (dev_priv->gtt.stolen_size - reserved_total) >> 10);
491 495
492 dev_priv->gtt.stolen_usable_size = dev_priv->gtt.stolen_size - 496 dev_priv->gtt.stolen_usable_size = dev_priv->gtt.stolen_size -
493 reserved_total; 497 reserved_total;
494 498
495 /* 499 /*
496 * Basic memrange allocator for stolen space. 500 * Basic memrange allocator for stolen space.
497 * 501 *
498 * TODO: Notice that some platforms require us to not use the first page 502 * TODO: Notice that some platforms require us to not use the first page
499 * of the stolen memory but their BIOSes may still put the framebuffer 503 * of the stolen memory but their BIOSes may still put the framebuffer
500 * on the first page. So we don't reserve this page for now because of 504 * on the first page. So we don't reserve this page for now because of
501 * that. Our current solution is to just prevent new nodes from being 505 * that. Our current solution is to just prevent new nodes from being
502 * inserted on the first page - see the check we have at 506 * inserted on the first page - see the check we have at
503 * i915_gem_stolen_insert_node_in_range(). We may want to fix the fbcon 507 * i915_gem_stolen_insert_node_in_range(). We may want to fix the fbcon
504 * problem later. 508 * problem later.
505 */ 509 */
506 drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_usable_size); 510 drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_usable_size);
507 511
508 return 0; 512 return 0;
509} 513}
510 514
511#ifndef __NetBSD__ 515#ifndef __NetBSD__
512static struct sg_table * 516static struct sg_table *
513i915_pages_create_for_stolen(struct drm_device *dev, 517i915_pages_create_for_stolen(struct drm_device *dev,
514 u32 offset, u32 size) 518 u32 offset, u32 size)
515{ 519{
 520#ifdef __NetBSD__
 521 panic("XXX");
 522#else
516 struct drm_i915_private *dev_priv = dev->dev_private; 523 struct drm_i915_private *dev_priv = dev->dev_private;
517 struct sg_table *st; 524 struct sg_table *st;
518 struct scatterlist *sg; 525 struct scatterlist *sg;
519 526
520 DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size); 527 DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size);
521 BUG_ON(offset > dev_priv->gtt.stolen_size - size); 528 BUG_ON(offset > dev_priv->gtt.stolen_size - size);
522 529
523 /* We hide that we have no struct page backing our stolen object 530 /* We hide that we have no struct page backing our stolen object
524 * by wrapping the contiguous physical allocation with a fake 531 * by wrapping the contiguous physical allocation with a fake
525 * dma mapping in a single scatterlist. 532 * dma mapping in a single scatterlist.
526 */ 533 */
527 534
528 st = kmalloc(sizeof(*st), GFP_KERNEL); 535 st = kmalloc(sizeof(*st), GFP_KERNEL);
529 if (st == NULL) 536 if (st == NULL)
530 return NULL; 537 return NULL;
531 538
532 if (sg_alloc_table(st, 1, GFP_KERNEL)) { 539 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
533 kfree(st); 540 kfree(st);
534 return NULL; 541 return NULL;
535 } 542 }
536 543
537 sg = st->sgl; 544 sg = st->sgl;
538 sg->offset = 0; 545 sg->offset = 0;
539 sg->length = size; 546 sg->length = size;
540 547
541 sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset; 548 sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
542 sg_dma_len(sg) = size; 549 sg_dma_len(sg) = size;
543 550
544 return st; 551 return st;
 552#endif
545} 553}
546#endif 554#endif
547 555
548static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj) 556static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
549{ 557{
550 BUG(); 558 BUG();
551 return -EINVAL; 559 return -EINVAL;
552} 560}
553 561
554static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj) 562static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj)
555{ 563{
556 /* Should only be called during free */ 564 /* Should only be called during free */
557#ifdef __NetBSD__ 565#ifdef __NetBSD__
558 bus_dmamap_unload(obj->base.dev->dmat, obj->igo_dmamap); 566 bus_dmamap_unload(obj->base.dev->dmat, obj->igo_dmamap);
559 bus_dmamap_destroy(obj->base.dev->dmat, obj->igo_dmamap); 567 bus_dmamap_destroy(obj->base.dev->dmat, obj->igo_dmamap);
560 kmem_free(obj->pages, (obj->igo_nsegs * sizeof(obj->pages[0]))); 568 kmem_free(obj->pages, (obj->igo_nsegs * sizeof(obj->pages[0])));
561#else 569#else
562 sg_free_table(obj->pages); 570 sg_free_table(obj->pages);
563 kfree(obj->pages); 571 kfree(obj->pages);
564#endif 572#endif
565} 573}
566 574
567 575
568static void 576static void
569i915_gem_object_release_stolen(struct drm_i915_gem_object *obj) 577i915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
570{ 578{
571 struct drm_i915_private *dev_priv = obj->base.dev->dev_private; 579 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
572 580
573 if (obj->stolen) { 581 if (obj->stolen) {
574 i915_gem_stolen_remove_node(dev_priv, obj->stolen); 582 i915_gem_stolen_remove_node(dev_priv, obj->stolen);
575 kfree(obj->stolen); 583 kfree(obj->stolen);
576 obj->stolen = NULL; 584 obj->stolen = NULL;
577 } 585 }
578} 586}
579static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = { 587static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
580 .get_pages = i915_gem_object_get_pages_stolen, 588 .get_pages = i915_gem_object_get_pages_stolen,
581 .put_pages = i915_gem_object_put_pages_stolen, 589 .put_pages = i915_gem_object_put_pages_stolen,
582 .release = i915_gem_object_release_stolen, 590 .release = i915_gem_object_release_stolen,
583}; 591};
584 592
585static struct drm_i915_gem_object * 593static struct drm_i915_gem_object *
586_i915_gem_object_create_stolen(struct drm_device *dev, 594_i915_gem_object_create_stolen(struct drm_device *dev,
587 struct drm_mm_node *stolen) 595 struct drm_mm_node *stolen)
588{ 596{
589 struct drm_i915_gem_object *obj; 597 struct drm_i915_gem_object *obj;
590#ifdef __NetBSD__ 598#ifdef __NetBSD__
591 struct drm_i915_private *const dev_priv = dev->dev_private; 599 struct drm_i915_private *const dev_priv = dev->dev_private;
592 unsigned i; 600 unsigned i;
593 int error; 601 int error;
594#endif 602#endif
595 603
596 obj = i915_gem_object_alloc(dev); 604 obj = i915_gem_object_alloc(dev);
597 if (obj == NULL) 605 if (obj == NULL)
598 return NULL; 606 return NULL;
599 607
600 drm_gem_private_object_init(dev, &obj->base, stolen->size); 608 drm_gem_private_object_init(dev, &obj->base, stolen->size);
601 i915_gem_object_init(obj, &i915_gem_object_stolen_ops); 609 i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
602 610
603#ifdef __NetBSD__ 611#ifdef __NetBSD__
604 TAILQ_INIT(&obj->igo_pageq); /* XXX Need to fill this... */ 612 TAILQ_INIT(&obj->igo_pageq); /* XXX Need to fill this... */
605 KASSERT((stolen->size % PAGE_SIZE) == 0); 613 KASSERT((stolen->size % PAGE_SIZE) == 0);
606 obj->igo_nsegs = (stolen->size / PAGE_SIZE); 614 obj->igo_nsegs = (stolen->size / PAGE_SIZE);
607 obj->pages = kmem_alloc((obj->igo_nsegs * sizeof(obj->pages[0])), 615 obj->pages = kmem_alloc((obj->igo_nsegs * sizeof(obj->pages[0])),
608 KM_SLEEP); 616 KM_SLEEP);
609 /* 617 /*
610 * x86 bus_dmamap_load_raw fails to respect the maxsegsz we 618 * x86 bus_dmamap_load_raw fails to respect the maxsegsz we
611 * pass to bus_dmamap_create, so we have to create page-sized 619 * pass to bus_dmamap_create, so we have to create page-sized
612 * segments to begin with. 620 * segments to begin with.
613 */ 621 */
614 for (i = 0; i < obj->igo_nsegs; i++) { 622 for (i = 0; i < obj->igo_nsegs; i++) {
615 obj->pages[i].ds_addr = (bus_addr_t)dev_priv->mm.stolen_base + 623 obj->pages[i].ds_addr = (bus_addr_t)dev_priv->mm.stolen_base +
616 stolen->start + (i*PAGE_SIZE); 624 stolen->start + (i*PAGE_SIZE);
617 obj->pages[i].ds_len = PAGE_SIZE; 625 obj->pages[i].ds_len = PAGE_SIZE;
618 } 626 }
619 error = bus_dmamap_create(dev->dmat, obj->base.size, obj->igo_nsegs, 627 error = bus_dmamap_create(dev->dmat, obj->base.size, obj->igo_nsegs,
620 PAGE_SIZE, 0, BUS_DMA_WAITOK, &obj->igo_dmamap); 628 PAGE_SIZE, 0, BUS_DMA_WAITOK, &obj->igo_dmamap);
621 if (error) { 629 if (error) {
622 DRM_ERROR("failed to create DMA map for stolen object: %d\n", 630 DRM_ERROR("failed to create DMA map for stolen object: %d\n",
623 error); 631 error);
624 kmem_free(obj->pages, sizeof(obj->pages[0])); 632 kmem_free(obj->pages, sizeof(obj->pages[0]));
625 obj->pages = NULL; 633 obj->pages = NULL;
626 goto cleanup; 634 goto cleanup;
627 } 635 }
628 error = bus_dmamap_load_raw(dev->dmat, obj->igo_dmamap, obj->pages, 636 error = bus_dmamap_load_raw(dev->dmat, obj->igo_dmamap, obj->pages,
629 obj->igo_nsegs, stolen->size, BUS_DMA_WAITOK); 637 obj->igo_nsegs, stolen->size, BUS_DMA_WAITOK);
630 if (error) { 638 if (error) {
631 DRM_ERROR("failed to load DMA map for stolen object: %d\n", 639 DRM_ERROR("failed to load DMA map for stolen object: %d\n",
632 error); 640 error);
633 bus_dmamap_destroy(dev->dmat, obj->igo_dmamap); 641 bus_dmamap_destroy(dev->dmat, obj->igo_dmamap);
634 kmem_free(obj->pages, sizeof(obj->pages[0])); 642 kmem_free(obj->pages, sizeof(obj->pages[0]));
635 obj->pages = NULL; 643 obj->pages = NULL;
636 goto cleanup; 644 goto cleanup;
637 } 645 }
638#else 646#else
639 obj->pages = i915_pages_create_for_stolen(dev, 647 obj->pages = i915_pages_create_for_stolen(dev,
640 stolen->start, stolen->size); 648 stolen->start, stolen->size);
641 if (obj->pages == NULL) 649 if (obj->pages == NULL)
642 goto cleanup; 650 goto cleanup;
643#endif 651#endif
644 652
645 i915_gem_object_pin_pages(obj); 653 i915_gem_object_pin_pages(obj);
646 obj->stolen = stolen; 654 obj->stolen = stolen;
647 655
648 obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT; 656 obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
649 obj->cache_level = HAS_LLC(dev) ? I915_CACHE_LLC : I915_CACHE_NONE; 657 obj->cache_level = HAS_LLC(dev) ? I915_CACHE_LLC : I915_CACHE_NONE;
650 658
651 return obj; 659 return obj;
652 660
653cleanup: 661cleanup:
654 i915_gem_object_free(obj); 662 i915_gem_object_free(obj);
655 return NULL; 663 return NULL;
656} 664}
657 665
658struct drm_i915_gem_object * 666struct drm_i915_gem_object *
659i915_gem_object_create_stolen(struct drm_device *dev, u32 size) 667i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
660{ 668{
661 struct drm_i915_private *dev_priv = dev->dev_private; 669 struct drm_i915_private *dev_priv = dev->dev_private;
662 struct drm_i915_gem_object *obj; 670 struct drm_i915_gem_object *obj;
663 struct drm_mm_node *stolen; 671 struct drm_mm_node *stolen;
664 int ret; 672 int ret;
665 673
666 if (!drm_mm_initialized(&dev_priv->mm.stolen)) 674 if (!drm_mm_initialized(&dev_priv->mm.stolen))
667 return NULL; 675 return NULL;
668 676
669 DRM_DEBUG_KMS("creating stolen object: size=%x\n", size); 677 DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
670 if (size == 0) 678 if (size == 0)
671 return NULL; 679 return NULL;
672 680
673 stolen = kzalloc(sizeof(*stolen), GFP_KERNEL); 681 stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
674 if (!stolen) 682 if (!stolen)
675 return NULL; 683 return NULL;
676 684
677 ret = i915_gem_stolen_insert_node(dev_priv, stolen, size, 4096); 685 ret = i915_gem_stolen_insert_node(dev_priv, stolen, size, 4096);
678 if (ret) { 686 if (ret) {
679 kfree(stolen); 687 kfree(stolen);
680 return NULL; 688 return NULL;
681 } 689 }
682 690
683 obj = _i915_gem_object_create_stolen(dev, stolen); 691 obj = _i915_gem_object_create_stolen(dev, stolen);
684 if (obj) 692 if (obj)
685 return obj; 693 return obj;
686 694
687 i915_gem_stolen_remove_node(dev_priv, stolen); 695 i915_gem_stolen_remove_node(dev_priv, stolen);
688 kfree(stolen); 696 kfree(stolen);
689 return NULL; 697 return NULL;
690} 698}
691 699
692struct drm_i915_gem_object * 700struct drm_i915_gem_object *
693i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev, 701i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev,
694 u32 stolen_offset, 702 u32 stolen_offset,
695 u32 gtt_offset, 703 u32 gtt_offset,
696 u32 size) 704 u32 size)
697{ 705{
698 struct drm_i915_private *dev_priv = dev->dev_private; 706 struct drm_i915_private *dev_priv = dev->dev_private;
699 struct i915_address_space *ggtt = &dev_priv->gtt.base; 707 struct i915_address_space *ggtt = &dev_priv->gtt.base;
700 struct drm_i915_gem_object *obj; 708 struct drm_i915_gem_object *obj;
701 struct drm_mm_node *stolen; 709 struct drm_mm_node *stolen;
702 struct i915_vma *vma; 710 struct i915_vma *vma;
703 int ret; 711 int ret;
704 712
705 if (!drm_mm_initialized(&dev_priv->mm.stolen)) 713 if (!drm_mm_initialized(&dev_priv->mm.stolen))
706 return NULL; 714 return NULL;
707 715
708 DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n", 716 DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
709 stolen_offset, gtt_offset, size); 717 stolen_offset, gtt_offset, size);
710 718
711 /* KISS and expect everything to be page-aligned */ 719 /* KISS and expect everything to be page-aligned */
712 if (WARN_ON(size == 0) || WARN_ON(size & 4095) || 720 if (WARN_ON(size == 0) || WARN_ON(size & 4095) ||
713 WARN_ON(stolen_offset & 4095)) 721 WARN_ON(stolen_offset & 4095))
714 return NULL; 722 return NULL;
715 723
716 stolen = kzalloc(sizeof(*stolen), GFP_KERNEL); 724 stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
717 if (!stolen) 725 if (!stolen)
718 return NULL; 726 return NULL;
719 727
720 stolen->start = stolen_offset; 728 stolen->start = stolen_offset;
721 stolen->size = size; 729 stolen->size = size;
722 mutex_lock(&dev_priv->mm.stolen_lock); 730 mutex_lock(&dev_priv->mm.stolen_lock);
723 ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen); 731 ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen);
724 mutex_unlock(&dev_priv->mm.stolen_lock); 732 mutex_unlock(&dev_priv->mm.stolen_lock);
725 if (ret) { 733 if (ret) {
726 DRM_DEBUG_KMS("failed to allocate stolen space\n"); 734 DRM_DEBUG_KMS("failed to allocate stolen space\n");
727 kfree(stolen); 735 kfree(stolen);
728 return NULL; 736 return NULL;
729 } 737 }
730 738
731 obj = _i915_gem_object_create_stolen(dev, stolen); 739 obj = _i915_gem_object_create_stolen(dev, stolen);
732 if (obj == NULL) { 740 if (obj == NULL) {
733 DRM_DEBUG_KMS("failed to allocate stolen object\n"); 741 DRM_DEBUG_KMS("failed to allocate stolen object\n");
734 i915_gem_stolen_remove_node(dev_priv, stolen); 742 i915_gem_stolen_remove_node(dev_priv, stolen);
735 kfree(stolen); 743 kfree(stolen);
736 return NULL; 744 return NULL;
737 } 745 }
738 746
739 /* Some objects just need physical mem from stolen space */ 747 /* Some objects just need physical mem from stolen space */
740 if (gtt_offset == I915_GTT_OFFSET_NONE) 748 if (gtt_offset == I915_GTT_OFFSET_NONE)
741 return obj; 749 return obj;
742 750
743 vma = i915_gem_obj_lookup_or_create_vma(obj, ggtt); 751 vma = i915_gem_obj_lookup_or_create_vma(obj, ggtt);
744 if (IS_ERR(vma)) { 752 if (IS_ERR(vma)) {
745 ret = PTR_ERR(vma); 753 ret = PTR_ERR(vma);
746 goto err; 754 goto err;
747 } 755 }
748 756
749 /* To simplify the initialisation sequence between KMS and GTT, 757 /* To simplify the initialisation sequence between KMS and GTT,
750 * we allow construction of the stolen object prior to 758 * we allow construction of the stolen object prior to
751 * setting up the GTT space. The actual reservation will occur 759 * setting up the GTT space. The actual reservation will occur
752 * later. 760 * later.
753 */ 761 */
754 vma->node.start = gtt_offset; 762 vma->node.start = gtt_offset;
755 vma->node.size = size; 763 vma->node.size = size;
756 if (drm_mm_initialized(&ggtt->mm)) { 764 if (drm_mm_initialized(&ggtt->mm)) {
757 ret = drm_mm_reserve_node(&ggtt->mm, &vma->node); 765 ret = drm_mm_reserve_node(&ggtt->mm, &vma->node);
758 if (ret) { 766 if (ret) {
759 DRM_DEBUG_KMS("failed to allocate stolen GTT space\n"); 767 DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
760 goto err; 768 goto err;
761 } 769 }
762 770
763 vma->bound |= GLOBAL_BIND; 771 vma->bound |= GLOBAL_BIND;
764 __i915_vma_set_map_and_fenceable(vma); 772 __i915_vma_set_map_and_fenceable(vma);
765 list_add_tail(&vma->mm_list, &ggtt->inactive_list); 773 list_add_tail(&vma->mm_list, &ggtt->inactive_list);
766 } 774 }
767 775
768 list_add_tail(&obj->global_list, &dev_priv->mm.bound_list); 776 list_add_tail(&obj->global_list, &dev_priv->mm.bound_list);
769 i915_gem_object_pin_pages(obj); 777 i915_gem_object_pin_pages(obj);
770 778
771 return obj; 779 return obj;
772 780
773err: 781err:
774 drm_gem_object_unreference(&obj->base); 782 drm_gem_object_unreference(&obj->base);
775 return NULL; 783 return NULL;
776} 784}

cvs diff -r1.3 -r1.4 src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_gtt.h (switch to unified diff)

--- src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_gtt.h 2018/08/27 05:35:11 1.3
+++ src/sys/external/bsd/drm2/dist/drm/i915/i915_gem_gtt.h 2018/08/27 06:08:25 1.4
@@ -1,611 +1,653 @@ @@ -1,611 +1,653 @@
1/* $NetBSD: i915_gem_gtt.h,v 1.3 2018/08/27 05:35:11 riastradh Exp $ */ 1/* $NetBSD: i915_gem_gtt.h,v 1.4 2018/08/27 06:08:25 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright © 2014 Intel Corporation 4 * Copyright © 2014 Intel Corporation
5 * 5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a 6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"), 7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation 8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the 10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions: 11 * Software is furnished to do so, subject to the following conditions:
12 * 12 *
13 * The above copyright notice and this permission notice (including the next 13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the 14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software. 15 * Software.
16 * 16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE. 23 * IN THE SOFTWARE.
24 * 24 *
25 * Please try to maintain the following order within this file unless it makes 25 * Please try to maintain the following order within this file unless it makes
26 * sense to do otherwise. From top to bottom: 26 * sense to do otherwise. From top to bottom:
27 * 1. typedefs 27 * 1. typedefs
28 * 2. #defines, and macros 28 * 2. #defines, and macros
29 * 3. structure definitions 29 * 3. structure definitions
30 * 4. function prototypes 30 * 4. function prototypes
31 * 31 *
32 * Within each section, please try to order by generation in ascending order, 32 * Within each section, please try to order by generation in ascending order,
33 * from top to bottom (ie. gen6 on the top, gen8 on the bottom). 33 * from top to bottom (ie. gen6 on the top, gen8 on the bottom).
34 */ 34 */
35 35
36#ifndef __I915_GEM_GTT_H__ 36#ifndef __I915_GEM_GTT_H__
37#define __I915_GEM_GTT_H__ 37#define __I915_GEM_GTT_H__
38 38
39struct drm_i915_file_private; 39struct drm_i915_file_private;
40 40
41typedef uint32_t gen6_pte_t; 41typedef uint32_t gen6_pte_t;
42typedef uint64_t gen8_pte_t; 42typedef uint64_t gen8_pte_t;
43typedef uint64_t gen8_pde_t; 43typedef uint64_t gen8_pde_t;
44typedef uint64_t gen8_ppgtt_pdpe_t; 44typedef uint64_t gen8_ppgtt_pdpe_t;
45typedef uint64_t gen8_ppgtt_pml4e_t; 45typedef uint64_t gen8_ppgtt_pml4e_t;
46 46
47#define gtt_total_entries(gtt) ((gtt).base.total >> PAGE_SHIFT) 47#define gtt_total_entries(gtt) ((gtt).base.total >> PAGE_SHIFT)
48 48
49 49
50/* gen6-hsw has bit 11-4 for physical addr bit 39-32 */ 50/* gen6-hsw has bit 11-4 for physical addr bit 39-32 */
51#define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0)) 51#define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0))
52#define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) 52#define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
53#define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) 53#define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
54#define GEN6_PTE_CACHE_LLC (2 << 1) 54#define GEN6_PTE_CACHE_LLC (2 << 1)
55#define GEN6_PTE_UNCACHED (1 << 1) 55#define GEN6_PTE_UNCACHED (1 << 1)
56#define GEN6_PTE_VALID (1 << 0) 56#define GEN6_PTE_VALID (1 << 0)
57 57
58#define I915_PTES(pte_len) (PAGE_SIZE / (pte_len)) 58#define I915_PTES(pte_len) (PAGE_SIZE / (pte_len))
59#define I915_PTE_MASK(pte_len) (I915_PTES(pte_len) - 1) 59#define I915_PTE_MASK(pte_len) (I915_PTES(pte_len) - 1)
60#define I915_PDES 512 60#define I915_PDES 512
61#define I915_PDE_MASK (I915_PDES - 1) 61#define I915_PDE_MASK (I915_PDES - 1)
62#define NUM_PTE(pde_shift) (1 << (pde_shift - PAGE_SHIFT)) 62#define NUM_PTE(pde_shift) (1 << (pde_shift - PAGE_SHIFT))
63 63
64#define GEN6_PTES I915_PTES(sizeof(gen6_pte_t)) 64#define GEN6_PTES I915_PTES(sizeof(gen6_pte_t))
65#define GEN6_PD_SIZE (I915_PDES * PAGE_SIZE) 65#define GEN6_PD_SIZE (I915_PDES * PAGE_SIZE)
66#define GEN6_PD_ALIGN (PAGE_SIZE * 16) 66#define GEN6_PD_ALIGN (PAGE_SIZE * 16)
67#define GEN6_PDE_SHIFT 22 67#define GEN6_PDE_SHIFT 22
68#define GEN6_PDE_VALID (1 << 0) 68#define GEN6_PDE_VALID (1 << 0)
69 69
70#define GEN7_PTE_CACHE_L3_LLC (3 << 1) 70#define GEN7_PTE_CACHE_L3_LLC (3 << 1)
71 71
72#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2) 72#define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
73#define BYT_PTE_WRITEABLE (1 << 1) 73#define BYT_PTE_WRITEABLE (1 << 1)
74 74
75/* Cacheability Control is a 4-bit value. The low three bits are stored in bits 75/* Cacheability Control is a 4-bit value. The low three bits are stored in bits
76 * 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE. 76 * 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
77 */ 77 */
78#define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \ 78#define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \
79 (((bits) & 0x8) << (11 - 3))) 79 (((bits) & 0x8) << (11 - 3)))
80#define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2) 80#define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2)
81#define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3) 81#define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3)
82#define HSW_WB_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x8) 82#define HSW_WB_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x8)
83#define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb) 83#define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb)
84#define HSW_WT_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x7) 84#define HSW_WT_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x7)
85#define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6) 85#define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6)
86#define HSW_PTE_UNCACHED (0) 86#define HSW_PTE_UNCACHED (0)
87#define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0)) 87#define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0))
88#define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr) 88#define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr)
89 89
90/* GEN8 legacy style address is defined as a 3 level page table: 90/* GEN8 legacy style address is defined as a 3 level page table:
91 * 31:30 | 29:21 | 20:12 | 11:0 91 * 31:30 | 29:21 | 20:12 | 11:0
92 * PDPE | PDE | PTE | offset 92 * PDPE | PDE | PTE | offset
93 * The difference as compared to normal x86 3 level page table is the PDPEs are 93 * The difference as compared to normal x86 3 level page table is the PDPEs are
94 * programmed via register. 94 * programmed via register.
95 * 95 *
96 * GEN8 48b legacy style address is defined as a 4 level page table: 96 * GEN8 48b legacy style address is defined as a 4 level page table:
97 * 47:39 | 38:30 | 29:21 | 20:12 | 11:0 97 * 47:39 | 38:30 | 29:21 | 20:12 | 11:0
98 * PML4E | PDPE | PDE | PTE | offset 98 * PML4E | PDPE | PDE | PTE | offset
99 */ 99 */
100#define GEN8_PML4ES_PER_PML4 512 100#define GEN8_PML4ES_PER_PML4 512
101#define GEN8_PML4E_SHIFT 39 101#define GEN8_PML4E_SHIFT 39
102#define GEN8_PML4E_MASK (GEN8_PML4ES_PER_PML4 - 1) 102#define GEN8_PML4E_MASK (GEN8_PML4ES_PER_PML4 - 1)
103#define GEN8_PDPE_SHIFT 30 103#define GEN8_PDPE_SHIFT 30
104/* NB: GEN8_PDPE_MASK is untrue for 32b platforms, but it has no impact on 32b page 104/* NB: GEN8_PDPE_MASK is untrue for 32b platforms, but it has no impact on 32b page
105 * tables */ 105 * tables */
106#define GEN8_PDPE_MASK 0x1ff 106#define GEN8_PDPE_MASK 0x1ff
107#define GEN8_PDE_SHIFT 21 107#define GEN8_PDE_SHIFT 21
108#define GEN8_PDE_MASK 0x1ff 108#define GEN8_PDE_MASK 0x1ff
109#define GEN8_PTE_SHIFT 12 109#define GEN8_PTE_SHIFT 12
110#define GEN8_PTE_MASK 0x1ff 110#define GEN8_PTE_MASK 0x1ff
111#define GEN8_LEGACY_PDPES 4 111#define GEN8_LEGACY_PDPES 4
112#define GEN8_PTES I915_PTES(sizeof(gen8_pte_t)) 112#define GEN8_PTES I915_PTES(sizeof(gen8_pte_t))
113 113
114#define I915_PDPES_PER_PDP(dev) (USES_FULL_48BIT_PPGTT(dev) ?\ 114#define I915_PDPES_PER_PDP(dev) (USES_FULL_48BIT_PPGTT(dev) ?\
115 GEN8_PML4ES_PER_PML4 : GEN8_LEGACY_PDPES) 115 GEN8_PML4ES_PER_PML4 : GEN8_LEGACY_PDPES)
116 116
117#define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD) 117#define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD)
118#define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */ 118#define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */
119#define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */ 119#define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */
120#define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */ 120#define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */
121 121
122#define CHV_PPAT_SNOOP (1<<6) 122#define CHV_PPAT_SNOOP (1<<6)
123#define GEN8_PPAT_AGE(x) (x<<4) 123#define GEN8_PPAT_AGE(x) (x<<4)
124#define GEN8_PPAT_LLCeLLC (3<<2) 124#define GEN8_PPAT_LLCeLLC (3<<2)
125#define GEN8_PPAT_LLCELLC (2<<2) 125#define GEN8_PPAT_LLCELLC (2<<2)
126#define GEN8_PPAT_LLC (1<<2) 126#define GEN8_PPAT_LLC (1<<2)
127#define GEN8_PPAT_WB (3<<0) 127#define GEN8_PPAT_WB (3<<0)
128#define GEN8_PPAT_WT (2<<0) 128#define GEN8_PPAT_WT (2<<0)
129#define GEN8_PPAT_WC (1<<0) 129#define GEN8_PPAT_WC (1<<0)
130#define GEN8_PPAT_UC (0<<0) 130#define GEN8_PPAT_UC (0<<0)
131#define GEN8_PPAT_ELLC_OVERRIDE (0<<2) 131#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
132#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8)) 132#define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
133 133
134enum i915_ggtt_view_type { 134enum i915_ggtt_view_type {
135 I915_GGTT_VIEW_NORMAL = 0, 135 I915_GGTT_VIEW_NORMAL = 0,
136 I915_GGTT_VIEW_ROTATED, 136 I915_GGTT_VIEW_ROTATED,
137 I915_GGTT_VIEW_PARTIAL, 137 I915_GGTT_VIEW_PARTIAL,
138}; 138};
139 139
140struct intel_rotation_info { 140struct intel_rotation_info {
141 unsigned int height; 141 unsigned int height;
142 unsigned int pitch; 142 unsigned int pitch;
143 unsigned int uv_offset; 143 unsigned int uv_offset;
144 uint32_t pixel_format; 144 uint32_t pixel_format;
145 uint64_t fb_modifier; 145 uint64_t fb_modifier;
146 unsigned int width_pages, height_pages; 146 unsigned int width_pages, height_pages;
147 uint64_t size; 147 uint64_t size;
148 unsigned int width_pages_uv, height_pages_uv; 148 unsigned int width_pages_uv, height_pages_uv;
149 uint64_t size_uv; 149 uint64_t size_uv;
150 unsigned int uv_start_page; 150 unsigned int uv_start_page;
151}; 151};
152 152
153struct i915_ggtt_view { 153struct i915_ggtt_view {
154 enum i915_ggtt_view_type type; 154 enum i915_ggtt_view_type type;
155 155
156 union { 156 union {
157 struct { 157 struct {
158 u64 offset; 158 u64 offset;
159 unsigned int size; 159 unsigned int size;
160 } partial; 160 } partial;
161 } params; 161 } params;
162 162
163#ifdef __NetBSD__ 163#ifdef __NetBSD__
164 bus_dmamap_t pages; 164 bus_dmamap_t pages;
165#else 165#else
166 struct sg_table *pages; 166 struct sg_table *pages;
167#endif 167#endif
168 168
169 union { 169 union {
170 struct intel_rotation_info rotation_info; 170 struct intel_rotation_info rotation_info;
171 }; 171 };
172}; 172};
173 173
174extern const struct i915_ggtt_view i915_ggtt_view_normal; 174extern const struct i915_ggtt_view i915_ggtt_view_normal;
175extern const struct i915_ggtt_view i915_ggtt_view_rotated; 175extern const struct i915_ggtt_view i915_ggtt_view_rotated;
176 176
177enum i915_cache_level; 177enum i915_cache_level;
178 178
179/** 179/**
180 * A VMA represents a GEM BO that is bound into an address space. Therefore, a 180 * A VMA represents a GEM BO that is bound into an address space. Therefore, a
181 * VMA's presence cannot be guaranteed before binding, or after unbinding the 181 * VMA's presence cannot be guaranteed before binding, or after unbinding the
182 * object into/from the address space. 182 * object into/from the address space.
183 * 183 *
184 * To make things as simple as possible (ie. no refcounting), a VMA's lifetime 184 * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
185 * will always be <= an objects lifetime. So object refcounting should cover us. 185 * will always be <= an objects lifetime. So object refcounting should cover us.
186 */ 186 */
187struct i915_vma { 187struct i915_vma {
188 struct drm_mm_node node; 188 struct drm_mm_node node;
189 struct drm_i915_gem_object *obj; 189 struct drm_i915_gem_object *obj;
190 struct i915_address_space *vm; 190 struct i915_address_space *vm;
191 191
192 /** Flags and address space this VMA is bound to */ 192 /** Flags and address space this VMA is bound to */
193#define GLOBAL_BIND (1<<0) 193#define GLOBAL_BIND (1<<0)
194#define LOCAL_BIND (1<<1) 194#define LOCAL_BIND (1<<1)
195 unsigned int bound : 4; 195 unsigned int bound : 4;
196 196
197 /** 197 /**
198 * Support different GGTT views into the same object. 198 * Support different GGTT views into the same object.
199 * This means there can be multiple VMA mappings per object and per VM. 199 * This means there can be multiple VMA mappings per object and per VM.
200 * i915_ggtt_view_type is used to distinguish between those entries. 200 * i915_ggtt_view_type is used to distinguish between those entries.
201 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also 201 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
202 * assumed in GEM functions which take no ggtt view parameter. 202 * assumed in GEM functions which take no ggtt view parameter.
203 */ 203 */
204 struct i915_ggtt_view ggtt_view; 204 struct i915_ggtt_view ggtt_view;
205 205
206 /** This object's place on the active/inactive lists */ 206 /** This object's place on the active/inactive lists */
207 struct list_head mm_list; 207 struct list_head mm_list;
208 208
209 struct list_head vma_link; /* Link in the object's VMA list */ 209 struct list_head vma_link; /* Link in the object's VMA list */
210 210
211 /** This vma's place in the batchbuffer or on the eviction list */ 211 /** This vma's place in the batchbuffer or on the eviction list */
212 struct list_head exec_list; 212 struct list_head exec_list;
213 213
214 /** 214 /**
215 * Used for performing relocations during execbuffer insertion. 215 * Used for performing relocations during execbuffer insertion.
216 */ 216 */
217 struct hlist_node exec_node; 217 struct hlist_node exec_node;
218 unsigned long exec_handle; 218 unsigned long exec_handle;
219 struct drm_i915_gem_exec_object2 *exec_entry; 219 struct drm_i915_gem_exec_object2 *exec_entry;
220 220
221 /** 221 /**
222 * How many users have pinned this object in GTT space. The following 222 * How many users have pinned this object in GTT space. The following
223 * users can each hold at most one reference: pwrite/pread, execbuffer 223 * users can each hold at most one reference: pwrite/pread, execbuffer
224 * (objects are not allowed multiple times for the same batchbuffer), 224 * (objects are not allowed multiple times for the same batchbuffer),
225 * and the framebuffer code. When switching/pageflipping, the 225 * and the framebuffer code. When switching/pageflipping, the
226 * framebuffer code has at most two buffers pinned per crtc. 226 * framebuffer code has at most two buffers pinned per crtc.
227 * 227 *
228 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3 228 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
229 * bits with absolutely no headroom. So use 4 bits. */ 229 * bits with absolutely no headroom. So use 4 bits. */
230 unsigned int pin_count:4; 230 unsigned int pin_count:4;
231#define DRM_I915_GEM_OBJECT_MAX_PIN_COUNT 0xf 231#define DRM_I915_GEM_OBJECT_MAX_PIN_COUNT 0xf
232}; 232};
233 233
234struct i915_page_dma { 234struct i915_page_dma {
235#ifdef __NetBSD__ 235#ifdef __NetBSD__
236 union { 236 union {
237 bus_dma_segment_t seg; 237 bus_dma_segment_t seg;
238 uint32_t ggtt_offset; 238 uint32_t ggtt_offset;
239 }; 239 };
240 bus_dmamap_t map; 240 bus_dmamap_t map;
241#else 241#else
242 struct page *page; 242 struct page *page;
243 union { 243 union {
244 dma_addr_t daddr; 244 dma_addr_t daddr;
245 245
246 /* For gen6/gen7 only. This is the offset in the GGTT 246 /* For gen6/gen7 only. This is the offset in the GGTT
247 * where the page directory entries for PPGTT begin 247 * where the page directory entries for PPGTT begin
248 */ 248 */
249 uint32_t ggtt_offset; 249 uint32_t ggtt_offset;
250 }; 250 };
251#endif 251#endif
252}; 252};
253 253
254#define px_base(px) (&(px)->base) 254#define px_base(px) (&(px)->base)
255#define px_page(px) (px_base(px)->page) 255#define px_page(px) (px_base(px)->page)
256#ifdef __NetBSD__ 256#ifdef __NetBSD__
257#define px_dma(x) (px_base(px)->map->dm_seg[0].ds_addr) 257#define px_dma(x) (px_base(px)->map->dm_seg[0].ds_addr)
258#else 258#else
259#define px_dma(px) (px_base(px)->daddr) 259#define px_dma(px) (px_base(px)->daddr)
260#endif 260#endif
261 261
262struct i915_page_scratch { 262struct i915_page_scratch {
263 struct i915_page_dma base; 263 struct i915_page_dma base;
264}; 264};
265 265
266struct i915_page_table { 266struct i915_page_table {
267 struct i915_page_dma base; 267 struct i915_page_dma base;
268 268
269 unsigned long *used_ptes; 269 unsigned long *used_ptes;
270}; 270};
271 271
272struct i915_page_directory { 272struct i915_page_directory {
273 struct i915_page_dma base; 273 struct i915_page_dma base;
274 274
275 unsigned long *used_pdes; 275 unsigned long *used_pdes;
276 struct i915_page_table *page_table[I915_PDES]; /* PDEs */ 276 struct i915_page_table *page_table[I915_PDES]; /* PDEs */
277}; 277};
278 278
279struct i915_page_directory_pointer { 279struct i915_page_directory_pointer {
280 struct i915_page_dma base; 280 struct i915_page_dma base;
281 281
282 unsigned long *used_pdpes; 282 unsigned long *used_pdpes;
283 struct i915_page_directory **page_directory; 283 struct i915_page_directory **page_directory;
284}; 284};
285 285
286struct i915_pml4 { 286struct i915_pml4 {
287 struct i915_page_dma base; 287 struct i915_page_dma base;
288 288
289 DECLARE_BITMAP(used_pml4es, GEN8_PML4ES_PER_PML4); 289 DECLARE_BITMAP(used_pml4es, GEN8_PML4ES_PER_PML4);
290 struct i915_page_directory_pointer *pdps[GEN8_PML4ES_PER_PML4]; 290 struct i915_page_directory_pointer *pdps[GEN8_PML4ES_PER_PML4];
291}; 291};
292 292
293struct i915_address_space { 293struct i915_address_space {
294 struct drm_mm mm; 294 struct drm_mm mm;
295 struct drm_device *dev; 295 struct drm_device *dev;
296 struct list_head global_link; 296 struct list_head global_link;
297 u64 start; /* Start offset always 0 for dri2 */ 297 u64 start; /* Start offset always 0 for dri2 */
298 u64 total; /* size addr space maps (ex. 2GB for ggtt) */ 298 u64 total; /* size addr space maps (ex. 2GB for ggtt) */
299 299
300 struct i915_page_scratch *scratch_page; 300 struct i915_page_scratch *scratch_page;
301 struct i915_page_table *scratch_pt; 301 struct i915_page_table *scratch_pt;
302 struct i915_page_directory *scratch_pd; 302 struct i915_page_directory *scratch_pd;
303 struct i915_page_directory_pointer *scratch_pdp; /* GEN8+ & 48b PPGTT */ 303 struct i915_page_directory_pointer *scratch_pdp; /* GEN8+ & 48b PPGTT */
304 304
305 /** 305 /**
306 * List of objects currently involved in rendering. 306 * List of objects currently involved in rendering.
307 * 307 *
308 * Includes buffers having the contents of their GPU caches 308 * Includes buffers having the contents of their GPU caches
309 * flushed, not necessarily primitives. last_read_req 309 * flushed, not necessarily primitives. last_read_req
310 * represents when the rendering involved will be completed. 310 * represents when the rendering involved will be completed.
311 * 311 *
312 * A reference is held on the buffer while on this list. 312 * A reference is held on the buffer while on this list.
313 */ 313 */
314 struct list_head active_list; 314 struct list_head active_list;
315 315
316 /** 316 /**
317 * LRU list of objects which are not in the ringbuffer and 317 * LRU list of objects which are not in the ringbuffer and
318 * are ready to unbind, but are still in the GTT. 318 * are ready to unbind, but are still in the GTT.
319 * 319 *
320 * last_read_req is NULL while an object is in this list. 320 * last_read_req is NULL while an object is in this list.
321 * 321 *
322 * A reference is not held on the buffer while on this list, 322 * A reference is not held on the buffer while on this list,
323 * as merely being GTT-bound shouldn't prevent its being 323 * as merely being GTT-bound shouldn't prevent its being
324 * freed, and we'll pull it off the list in the free path. 324 * freed, and we'll pull it off the list in the free path.
325 */ 325 */
326 struct list_head inactive_list; 326 struct list_head inactive_list;
327 327
328 /* FIXME: Need a more generic return type */ 328 /* FIXME: Need a more generic return type */
329 gen6_pte_t (*pte_encode)(dma_addr_t addr, 329 gen6_pte_t (*pte_encode)(dma_addr_t addr,
330 enum i915_cache_level level, 330 enum i915_cache_level level,
331 bool valid, u32 flags); /* Create a valid PTE */ 331 bool valid, u32 flags); /* Create a valid PTE */
332 /* flags for pte_encode */ 332 /* flags for pte_encode */
333#define PTE_READ_ONLY (1<<0) 333#define PTE_READ_ONLY (1<<0)
334 int (*allocate_va_range)(struct i915_address_space *vm, 334 int (*allocate_va_range)(struct i915_address_space *vm,
335 uint64_t start, 335 uint64_t start,
336 uint64_t length); 336 uint64_t length);
337 void (*clear_range)(struct i915_address_space *vm, 337 void (*clear_range)(struct i915_address_space *vm,
338 uint64_t start, 338 uint64_t start,
339 uint64_t length, 339 uint64_t length,
340 bool use_scratch); 340 bool use_scratch);
341 void (*insert_entries)(struct i915_address_space *vm, 341 void (*insert_entries)(struct i915_address_space *vm,
342#ifdef __NetBSD__ 342#ifdef __NetBSD__
343 bus_dmamap_t dmamap, 343 bus_dmamap_t dmamap,
344#else 344#else
345 struct sg_table *st, 345 struct sg_table *st,
346#endif 346#endif
347 uint64_t start, 347 uint64_t start,
348 enum i915_cache_level cache_level, u32 flags); 348 enum i915_cache_level cache_level, u32 flags);
349 void (*cleanup)(struct i915_address_space *vm); 349 void (*cleanup)(struct i915_address_space *vm);
350 /** Unmap an object from an address space. This usually consists of 350 /** Unmap an object from an address space. This usually consists of
351 * setting the valid PTE entries to a reserved scratch page. */ 351 * setting the valid PTE entries to a reserved scratch page. */
352 void (*unbind_vma)(struct i915_vma *vma); 352 void (*unbind_vma)(struct i915_vma *vma);
353 /* Map an object into an address space with the given cache flags. */ 353 /* Map an object into an address space with the given cache flags. */
354 int (*bind_vma)(struct i915_vma *vma, 354 int (*bind_vma)(struct i915_vma *vma,
355 enum i915_cache_level cache_level, 355 enum i915_cache_level cache_level,
356 u32 flags); 356 u32 flags);
357}; 357};
358 358
359/* The Graphics Translation Table is the way in which GEN hardware translates a 359/* The Graphics Translation Table is the way in which GEN hardware translates a
360 * Graphics Virtual Address into a Physical Address. In addition to the normal 360 * Graphics Virtual Address into a Physical Address. In addition to the normal
361 * collateral associated with any va->pa translations GEN hardware also has a 361 * collateral associated with any va->pa translations GEN hardware also has a
362 * portion of the GTT which can be mapped by the CPU and remain both coherent 362 * portion of the GTT which can be mapped by the CPU and remain both coherent
363 * and correct (in cases like swizzling). That region is referred to as GMADR in 363 * and correct (in cases like swizzling). That region is referred to as GMADR in
364 * the spec. 364 * the spec.
365 */ 365 */
366struct i915_gtt { 366struct i915_gtt {
367 struct i915_address_space base; 367 struct i915_address_space base;
368 368
369 size_t stolen_size; /* Total size of stolen memory */ 369 size_t stolen_size; /* Total size of stolen memory */
370 size_t stolen_usable_size; /* Total size minus BIOS reserved */ 370 size_t stolen_usable_size; /* Total size minus BIOS reserved */
371 u64 mappable_end; /* End offset that we can CPU map */ 371 u64 mappable_end; /* End offset that we can CPU map */
372 struct io_mapping *mappable; /* Mapping to our CPU mappable region */ 372 struct io_mapping *mappable; /* Mapping to our CPU mappable region */
373 phys_addr_t mappable_base; /* PA of our GMADR */ 373 phys_addr_t mappable_base; /* PA of our GMADR */
374 374
375 /** "Graphics Stolen Memory" holds the global PTEs */ 375 /** "Graphics Stolen Memory" holds the global PTEs */
376#ifdef __NetBSD__ 376#ifdef __NetBSD__
377 /* 377 /*
378 * This is not actually the `Graphics Stolen Memory'; it is the 378 * This is not actually the `Graphics Stolen Memory'; it is the
379 * graphics translation table, which we write to through the 379 * graphics translation table, which we write to through the
380 * GTTADR/GTTMMADR PCI BAR, and which is backed by `Graphics 380 * GTTADR/GTTMMADR PCI BAR, and which is backed by `Graphics
381 * GTT Stolen Memory'. That isn't the `Graphics Stolen Memory' 381 * GTT Stolen Memory'. That isn't the `Graphics Stolen Memory'
382 * either, although it is stolen from main memory. 382 * either, although it is stolen from main memory.
383 */ 383 */
384 bus_space_tag_t bst; 384 bus_space_tag_t bst;
385 bus_space_handle_t bsh; 385 bus_space_handle_t bsh;
386 bus_size_t size; 386 bus_size_t size;
387 387
388 /* Maximum physical address that can be wired into a GTT entry. */ 388 /* Maximum physical address that can be wired into a GTT entry. */
389 uint64_t max_paddr; 389 uint64_t max_paddr;
390 390
391 /* Page freelist for pages limited to the above maximum address. */ 391 /* Page freelist for pages limited to the above maximum address. */
392 int pgfl; 392 int pgfl;
393#else 393#else
394 void __iomem *gsm; 394 void __iomem *gsm;
395#endif 395#endif
396 396
397 bool do_idle_maps; 397 bool do_idle_maps;
398 398
399 int mtrr; 399 int mtrr;
400 400
401 /* global gtt ops */ 401 /* global gtt ops */
402 int (*gtt_probe)(struct drm_device *dev, u64 *gtt_total, 402 int (*gtt_probe)(struct drm_device *dev, u64 *gtt_total,
403 size_t *stolen, phys_addr_t *mappable_base, 403 size_t *stolen, phys_addr_t *mappable_base,
404 u64 *mappable_end); 404 u64 *mappable_end);
405}; 405};
406 406
407struct i915_hw_ppgtt { 407struct i915_hw_ppgtt {
408 struct i915_address_space base; 408 struct i915_address_space base;
409 struct kref ref; 409 struct kref ref;
410 struct drm_mm_node node; 410 struct drm_mm_node node;
411 unsigned long pd_dirty_rings; 411 unsigned long pd_dirty_rings;
412 union { 412 union {
413 struct i915_pml4 pml4; /* GEN8+ & 48b PPGTT */ 413 struct i915_pml4 pml4; /* GEN8+ & 48b PPGTT */
414 struct i915_page_directory_pointer pdp; /* GEN8+ */ 414 struct i915_page_directory_pointer pdp; /* GEN8+ */
415 struct i915_page_directory pd; /* GEN6-7 */ 415 struct i915_page_directory pd; /* GEN6-7 */
416 }; 416 };
417 417
418 struct drm_i915_file_private *file_priv; 418 struct drm_i915_file_private *file_priv;
419 419
420#ifndef __NetBSD__ 420#ifndef __NetBSD__
421 gen6_pte_t __iomem *pd_addr; 421 gen6_pte_t __iomem *pd_addr;
422#endif 422#endif
423 423
424 int (*enable)(struct i915_hw_ppgtt *ppgtt); 424 int (*enable)(struct i915_hw_ppgtt *ppgtt);
425 int (*switch_mm)(struct i915_hw_ppgtt *ppgtt, 425 int (*switch_mm)(struct i915_hw_ppgtt *ppgtt,
426 struct drm_i915_gem_request *req); 426 struct drm_i915_gem_request *req);
427 void (*debug_dump)(struct i915_hw_ppgtt *ppgtt, struct seq_file *m); 427 void (*debug_dump)(struct i915_hw_ppgtt *ppgtt, struct seq_file *m);
428}; 428};
429 429
430/* For each pde iterates over every pde between from start until start + length. 430/* For each pde iterates over every pde between from start until start + length.
431 * If start, and start+length are not perfectly divisible, the macro will round 431 * If start, and start+length are not perfectly divisible, the macro will round
432 * down, and up as needed. The macro modifies pde, start, and length. Dev is 432 * down, and up as needed. The macro modifies pde, start, and length. Dev is
433 * only used to differentiate shift values. Temp is temp. On gen6/7, start = 0, 433 * only used to differentiate shift values. Temp is temp. On gen6/7, start = 0,
434 * and length = 2G effectively iterates over every PDE in the system. 434 * and length = 2G effectively iterates over every PDE in the system.
435 * 435 *
436 * XXX: temp is not actually needed, but it saves doing the ALIGN operation. 436 * XXX: temp is not actually needed, but it saves doing the ALIGN operation.
437 */ 437 */
 438#ifdef __NetBSD__ /* ALIGN means something else */
 439#define gen6_for_each_pde(pt, pd, start, length, temp, iter) \
 440 for (iter = gen6_pde_index(start); \
 441 length > 0 && iter < I915_PDES ? \
 442 (pt = (pd)->page_table[iter]), 1 : 0; \
 443 iter++, \
 444 temp = round_up(start+1, 1 << GEN6_PDE_SHIFT) - start, \
 445 temp = min_t(unsigned, temp, length), \
 446 start += temp, length -= temp)
 447#else
438#define gen6_for_each_pde(pt, pd, start, length, temp, iter) \ 448#define gen6_for_each_pde(pt, pd, start, length, temp, iter) \
439 for (iter = gen6_pde_index(start); \ 449 for (iter = gen6_pde_index(start); \
440 length > 0 && iter < I915_PDES ? \ 450 length > 0 && iter < I915_PDES ? \
441 (pt = (pd)->page_table[iter]), 1 : 0; \ 451 (pt = (pd)->page_table[iter]), 1 : 0; \
442 iter++, \ 452 iter++, \
443 temp = ALIGN(start+1, 1 << GEN6_PDE_SHIFT) - start, \ 453 temp = ALIGN(start+1, 1 << GEN6_PDE_SHIFT) - start, \
444 temp = min_t(unsigned, temp, length), \ 454 temp = min_t(unsigned, temp, length), \
445 start += temp, length -= temp) 455 start += temp, length -= temp)
 456#endif
446 457
447#define gen6_for_all_pdes(pt, ppgtt, iter) \ 458#define gen6_for_all_pdes(pt, ppgtt, iter) \
448 for (iter = 0; \ 459 for (iter = 0; \
449 pt = ppgtt->pd.page_table[iter], iter < I915_PDES; \ 460 pt = ppgtt->pd.page_table[iter], iter < I915_PDES; \
450 iter++) 461 iter++)
451 462
452static inline uint32_t i915_pte_index(uint64_t address, uint32_t pde_shift) 463static inline uint32_t i915_pte_index(uint64_t address, uint32_t pde_shift)
453{ 464{
454 const uint32_t mask = NUM_PTE(pde_shift) - 1; 465 const uint32_t mask = NUM_PTE(pde_shift) - 1;
455 466
456 return (address >> PAGE_SHIFT) & mask; 467 return (address >> PAGE_SHIFT) & mask;
457} 468}
458 469
459/* Helper to counts the number of PTEs within the given length. This count 470/* Helper to counts the number of PTEs within the given length. This count
460 * does not cross a page table boundary, so the max value would be 471 * does not cross a page table boundary, so the max value would be
461 * GEN6_PTES for GEN6, and GEN8_PTES for GEN8. 472 * GEN6_PTES for GEN6, and GEN8_PTES for GEN8.
462*/ 473*/
463static inline uint32_t i915_pte_count(uint64_t addr, size_t length, 474static inline uint32_t i915_pte_count(uint64_t addr, size_t length,
464 uint32_t pde_shift) 475 uint32_t pde_shift)
465{ 476{
466 const uint64_t mask = ~((1 << pde_shift) - 1); 477 const uint64_t mask = ~((1 << pde_shift) - 1);
467 uint64_t end; 478 uint64_t end;
468 479
469 WARN_ON(length == 0); 480 WARN_ON(length == 0);
470 WARN_ON(offset_in_page(addr|length)); 481 WARN_ON(offset_in_page(addr|length));
471 482
472 end = addr + length; 483 end = addr + length;
473 484
474 if ((addr & mask) != (end & mask)) 485 if ((addr & mask) != (end & mask))
475 return NUM_PTE(pde_shift) - i915_pte_index(addr, pde_shift); 486 return NUM_PTE(pde_shift) - i915_pte_index(addr, pde_shift);
476 487
477 return i915_pte_index(end, pde_shift) - i915_pte_index(addr, pde_shift); 488 return i915_pte_index(end, pde_shift) - i915_pte_index(addr, pde_shift);
478} 489}
479 490
480static inline uint32_t i915_pde_index(uint64_t addr, uint32_t shift) 491static inline uint32_t i915_pde_index(uint64_t addr, uint32_t shift)
481{ 492{
482 return (addr >> shift) & I915_PDE_MASK; 493 return (addr >> shift) & I915_PDE_MASK;
483} 494}
484 495
485static inline uint32_t gen6_pte_index(uint32_t addr) 496static inline uint32_t gen6_pte_index(uint32_t addr)
486{ 497{
487 return i915_pte_index(addr, GEN6_PDE_SHIFT); 498 return i915_pte_index(addr, GEN6_PDE_SHIFT);
488} 499}
489 500
490static inline size_t gen6_pte_count(uint32_t addr, uint32_t length) 501static inline size_t gen6_pte_count(uint32_t addr, uint32_t length)
491{ 502{
492 return i915_pte_count(addr, length, GEN6_PDE_SHIFT); 503 return i915_pte_count(addr, length, GEN6_PDE_SHIFT);
493} 504}
494 505
495static inline uint32_t gen6_pde_index(uint32_t addr) 506static inline uint32_t gen6_pde_index(uint32_t addr)
496{ 507{
497 return i915_pde_index(addr, GEN6_PDE_SHIFT); 508 return i915_pde_index(addr, GEN6_PDE_SHIFT);
498} 509}
499 510
500/* Equivalent to the gen6 version, For each pde iterates over every pde 511/* Equivalent to the gen6 version, For each pde iterates over every pde
501 * between from start until start + length. On gen8+ it simply iterates 512 * between from start until start + length. On gen8+ it simply iterates
502 * over every page directory entry in a page directory. 513 * over every page directory entry in a page directory.
503 */ 514 */
 515#ifdef __NetBSD__ /* ALIGN means something else */
 516
 517#define gen8_for_each_pde(pt, pd, start, length, temp, iter) \
 518 for (iter = gen8_pde_index(start); \
 519 length > 0 && iter < I915_PDES ? \
 520 (pt = (pd)->page_table[iter]), 1 : 0; \
 521 iter++, \
 522 temp = round_up(start+1, 1 << GEN8_PDE_SHIFT) - start, \
 523 temp = min(temp, length), \
 524 start += temp, length -= temp)
 525#define gen8_for_each_pdpe(pd, pdp, start, length, temp, iter) \
 526 for (iter = gen8_pdpe_index(start); \
 527 length > 0 && (iter < I915_PDPES_PER_PDP(dev)) ? \
 528 (pd = (pdp)->page_directory[iter]), 1 : 0; \
 529 iter++, \
 530 temp = round_up(start+1, 1 << GEN8_PDPE_SHIFT) - start, \
 531 temp = min(temp, length), \
 532 start += temp, length -= temp)
 533
 534#define gen8_for_each_pml4e(pdp, pml4, start, length, temp, iter) \
 535 for (iter = gen8_pml4e_index(start); \
 536 length > 0 && iter < GEN8_PML4ES_PER_PML4 ? \
 537 (pdp = (pml4)->pdps[iter]), 1 : 0; \
 538 iter++, \
 539 temp = round_up(start+1, 1ULL << GEN8_PML4E_SHIFT) - start, \
 540 temp = min(temp, length), \
 541 start += temp, length -= temp)
 542
 543#else
 544
504#define gen8_for_each_pde(pt, pd, start, length, temp, iter) \ 545#define gen8_for_each_pde(pt, pd, start, length, temp, iter) \
505 for (iter = gen8_pde_index(start); \ 546 for (iter = gen8_pde_index(start); \
506 length > 0 && iter < I915_PDES ? \ 547 length > 0 && iter < I915_PDES ? \
507 (pt = (pd)->page_table[iter]), 1 : 0; \ 548 (pt = (pd)->page_table[iter]), 1 : 0; \
508 iter++, \ 549 iter++, \
509 temp = ALIGN(start+1, 1 << GEN8_PDE_SHIFT) - start, \ 550 temp = ALIGN(start+1, 1 << GEN8_PDE_SHIFT) - start, \
510 temp = min(temp, length), \ 551 temp = min(temp, length), \
511 start += temp, length -= temp) 552 start += temp, length -= temp)
512 553
513#define gen8_for_each_pdpe(pd, pdp, start, length, temp, iter) \ 554#define gen8_for_each_pdpe(pd, pdp, start, length, temp, iter) \
514 for (iter = gen8_pdpe_index(start); \ 555 for (iter = gen8_pdpe_index(start); \
515 length > 0 && (iter < I915_PDPES_PER_PDP(dev)) ? \ 556 length > 0 && (iter < I915_PDPES_PER_PDP(dev)) ? \
516 (pd = (pdp)->page_directory[iter]), 1 : 0; \ 557 (pd = (pdp)->page_directory[iter]), 1 : 0; \
517 iter++, \ 558 iter++, \
518 temp = ALIGN(start+1, 1 << GEN8_PDPE_SHIFT) - start, \ 559 temp = ALIGN(start+1, 1 << GEN8_PDPE_SHIFT) - start, \
519 temp = min(temp, length), \ 560 temp = min(temp, length), \
520 start += temp, length -= temp) 561 start += temp, length -= temp)
521 562
522#define gen8_for_each_pml4e(pdp, pml4, start, length, temp, iter) \ 563#define gen8_for_each_pml4e(pdp, pml4, start, length, temp, iter) \
523 for (iter = gen8_pml4e_index(start); \ 564 for (iter = gen8_pml4e_index(start); \
524 length > 0 && iter < GEN8_PML4ES_PER_PML4 ? \ 565 length > 0 && iter < GEN8_PML4ES_PER_PML4 ? \
525 (pdp = (pml4)->pdps[iter]), 1 : 0; \ 566 (pdp = (pml4)->pdps[iter]), 1 : 0; \
526 iter++, \ 567 iter++, \
527 temp = ALIGN(start+1, 1ULL << GEN8_PML4E_SHIFT) - start, \ 568 temp = ALIGN(start+1, 1ULL << GEN8_PML4E_SHIFT) - start, \
528 temp = min(temp, length), \ 569 temp = min(temp, length), \
529 start += temp, length -= temp) 570 start += temp, length -= temp)
 571#endif
530 572
531static inline uint32_t gen8_pte_index(uint64_t address) 573static inline uint32_t gen8_pte_index(uint64_t address)
532{ 574{
533 return i915_pte_index(address, GEN8_PDE_SHIFT); 575 return i915_pte_index(address, GEN8_PDE_SHIFT);
534} 576}
535 577
536static inline uint32_t gen8_pde_index(uint64_t address) 578static inline uint32_t gen8_pde_index(uint64_t address)
537{ 579{
538 return i915_pde_index(address, GEN8_PDE_SHIFT); 580 return i915_pde_index(address, GEN8_PDE_SHIFT);
539} 581}
540 582
541static inline uint32_t gen8_pdpe_index(uint64_t address) 583static inline uint32_t gen8_pdpe_index(uint64_t address)
542{ 584{
543 return (address >> GEN8_PDPE_SHIFT) & GEN8_PDPE_MASK; 585 return (address >> GEN8_PDPE_SHIFT) & GEN8_PDPE_MASK;
544} 586}
545 587
546static inline uint32_t gen8_pml4e_index(uint64_t address) 588static inline uint32_t gen8_pml4e_index(uint64_t address)
547{ 589{
548 return (address >> GEN8_PML4E_SHIFT) & GEN8_PML4E_MASK; 590 return (address >> GEN8_PML4E_SHIFT) & GEN8_PML4E_MASK;
549} 591}
550 592
551static inline size_t gen8_pte_count(uint64_t address, uint64_t length) 593static inline size_t gen8_pte_count(uint64_t address, uint64_t length)
552{ 594{
553 return i915_pte_count(address, length, GEN8_PDE_SHIFT); 595 return i915_pte_count(address, length, GEN8_PDE_SHIFT);
554} 596}
555 597
556static inline dma_addr_t 598static inline dma_addr_t
557i915_page_dir_dma_addr(const struct i915_hw_ppgtt *ppgtt, const unsigned n) 599i915_page_dir_dma_addr(const struct i915_hw_ppgtt *ppgtt, const unsigned n)
558{ 600{
559 return test_bit(n, ppgtt->pdp.used_pdpes) ? 601 return test_bit(n, ppgtt->pdp.used_pdpes) ?
560 px_dma(ppgtt->pdp.page_directory[n]) : 602 px_dma(ppgtt->pdp.page_directory[n]) :
561 px_dma(ppgtt->base.scratch_pd); 603 px_dma(ppgtt->base.scratch_pd);
562} 604}
563 605
564int i915_gem_gtt_init(struct drm_device *dev); 606int i915_gem_gtt_init(struct drm_device *dev);
565void i915_gem_init_global_gtt(struct drm_device *dev); 607void i915_gem_init_global_gtt(struct drm_device *dev);
566void i915_global_gtt_cleanup(struct drm_device *dev); 608void i915_global_gtt_cleanup(struct drm_device *dev);
567 609
568 610
569int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt); 611int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt);
570int i915_ppgtt_init_hw(struct drm_device *dev); 612int i915_ppgtt_init_hw(struct drm_device *dev);
571int i915_ppgtt_init_ring(struct drm_i915_gem_request *req); 613int i915_ppgtt_init_ring(struct drm_i915_gem_request *req);
572void i915_ppgtt_release(struct kref *kref); 614void i915_ppgtt_release(struct kref *kref);
573struct i915_hw_ppgtt *i915_ppgtt_create(struct drm_device *dev, 615struct i915_hw_ppgtt *i915_ppgtt_create(struct drm_device *dev,
574 struct drm_i915_file_private *fpriv); 616 struct drm_i915_file_private *fpriv);
575static inline void i915_ppgtt_get(struct i915_hw_ppgtt *ppgtt) 617static inline void i915_ppgtt_get(struct i915_hw_ppgtt *ppgtt)
576{ 618{
577 if (ppgtt) 619 if (ppgtt)
578 kref_get(&ppgtt->ref); 620 kref_get(&ppgtt->ref);
579} 621}
580static inline void i915_ppgtt_put(struct i915_hw_ppgtt *ppgtt) 622static inline void i915_ppgtt_put(struct i915_hw_ppgtt *ppgtt)
581{ 623{
582 if (ppgtt) 624 if (ppgtt)
583 kref_put(&ppgtt->ref, i915_ppgtt_release); 625 kref_put(&ppgtt->ref, i915_ppgtt_release);
584} 626}
585 627
586void i915_check_and_clear_faults(struct drm_device *dev); 628void i915_check_and_clear_faults(struct drm_device *dev);
587void i915_gem_suspend_gtt_mappings(struct drm_device *dev); 629void i915_gem_suspend_gtt_mappings(struct drm_device *dev);
588void i915_gem_restore_gtt_mappings(struct drm_device *dev); 630void i915_gem_restore_gtt_mappings(struct drm_device *dev);
589 631
590int __must_check i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj); 632int __must_check i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj);
591void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj); 633void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj);
592 634
593static inline bool 635static inline bool
594i915_ggtt_view_equal(const struct i915_ggtt_view *a, 636i915_ggtt_view_equal(const struct i915_ggtt_view *a,
595 const struct i915_ggtt_view *b) 637 const struct i915_ggtt_view *b)
596{ 638{
597 if (WARN_ON(!a || !b)) 639 if (WARN_ON(!a || !b))
598 return false; 640 return false;
599 641
600 if (a->type != b->type) 642 if (a->type != b->type)
601 return false; 643 return false;
602 if (a->type == I915_GGTT_VIEW_PARTIAL) 644 if (a->type == I915_GGTT_VIEW_PARTIAL)
603 return !memcmp(&a->params, &b->params, sizeof(a->params)); 645 return !memcmp(&a->params, &b->params, sizeof(a->params));
604 return true; 646 return true;
605} 647}
606 648
607size_t 649size_t
608i915_ggtt_view_size(struct drm_i915_gem_object *obj, 650i915_ggtt_view_size(struct drm_i915_gem_object *obj,
609 const struct i915_ggtt_view *view); 651 const struct i915_ggtt_view *view);
610 652
611#endif 653#endif

cvs diff -r1.6 -r1.7 src/sys/external/bsd/drm2/dist/drm/i915/i915_gpu_error.c (switch to unified diff)

--- src/sys/external/bsd/drm2/dist/drm/i915/i915_gpu_error.c 2018/08/27 05:57:29 1.6
+++ src/sys/external/bsd/drm2/dist/drm/i915/i915_gpu_error.c 2018/08/27 06:08:25 1.7
@@ -1,1357 +1,1357 @@ @@ -1,1357 +1,1357 @@
1/* $NetBSD: i915_gpu_error.c,v 1.6 2018/08/27 05:57:29 riastradh Exp $ */ 1/* $NetBSD: i915_gpu_error.c,v 1.7 2018/08/27 06:08:25 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2008 Intel Corporation 4 * Copyright (c) 2008 Intel Corporation
5 * 5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a 6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"), 7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation 8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the 10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions: 11 * Software is furnished to do so, subject to the following conditions:
12 * 12 *
13 * The above copyright notice and this permission notice (including the next 13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the 14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software. 15 * Software.
16 * 16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE. 23 * IN THE SOFTWARE.
24 * 24 *
25 * Authors: 25 * Authors:
26 * Eric Anholt <eric@anholt.net> 26 * Eric Anholt <eric@anholt.net>
27 * Keith Packard <keithp@keithp.com> 27 * Keith Packard <keithp@keithp.com>
28 * Mika Kuoppala <mika.kuoppala@intel.com> 28 * Mika Kuoppala <mika.kuoppala@intel.com>
29 * 29 *
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: i915_gpu_error.c,v 1.6 2018/08/27 05:57:29 riastradh Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: i915_gpu_error.c,v 1.7 2018/08/27 06:08:25 riastradh Exp $");
34 34
35#include <sys/param.h> 35#include <sys/param.h>
36 36
37#include <asm/io.h> 37#include <asm/io.h>
38#include <linux/irqflags.h> 38#include <linux/irqflags.h>
39#include "i915_drv.h" 39#include "i915_drv.h"
40 40
41static const char *ring_str(int ring) 41static const char *ring_str(int ring)
42{ 42{
43 switch (ring) { 43 switch (ring) {
44 case RCS: return "render"; 44 case RCS: return "render";
45 case VCS: return "bsd"; 45 case VCS: return "bsd";
46 case BCS: return "blt"; 46 case BCS: return "blt";
47 case VECS: return "vebox"; 47 case VECS: return "vebox";
48 case VCS2: return "bsd2"; 48 case VCS2: return "bsd2";
49 default: return ""; 49 default: return "";
50 } 50 }
51} 51}
52 52
53static const char *pin_flag(int pinned) 53static const char *pin_flag(int pinned)
54{ 54{
55 if (pinned > 0) 55 if (pinned > 0)
56 return " P"; 56 return " P";
57 else if (pinned < 0) 57 else if (pinned < 0)
58 return " p"; 58 return " p";
59 else 59 else
60 return ""; 60 return "";
61} 61}
62 62
63static const char *tiling_flag(int tiling) 63static const char *tiling_flag(int tiling)
64{ 64{
65 switch (tiling) { 65 switch (tiling) {
66 default: 66 default:
67 case I915_TILING_NONE: return ""; 67 case I915_TILING_NONE: return "";
68 case I915_TILING_X: return " X"; 68 case I915_TILING_X: return " X";
69 case I915_TILING_Y: return " Y"; 69 case I915_TILING_Y: return " Y";
70 } 70 }
71} 71}
72 72
73static const char *dirty_flag(int dirty) 73static const char *dirty_flag(int dirty)
74{ 74{
75 return dirty ? " dirty" : ""; 75 return dirty ? " dirty" : "";
76} 76}
77 77
78static const char *purgeable_flag(int purgeable) 78static const char *purgeable_flag(int purgeable)
79{ 79{
80 return purgeable ? " purgeable" : ""; 80 return purgeable ? " purgeable" : "";
81} 81}
82 82
83static bool __i915_error_ok(struct drm_i915_error_state_buf *e) 83static bool __i915_error_ok(struct drm_i915_error_state_buf *e)
84{ 84{
85 85
86 if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) { 86 if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) {
87 e->err = -ENOSPC; 87 e->err = -ENOSPC;
88 return false; 88 return false;
89 } 89 }
90 90
91 if (e->bytes == e->size - 1 || e->err) 91 if (e->bytes == e->size - 1 || e->err)
92 return false; 92 return false;
93 93
94 return true; 94 return true;
95} 95}
96 96
97static bool __i915_error_seek(struct drm_i915_error_state_buf *e, 97static bool __i915_error_seek(struct drm_i915_error_state_buf *e,
98 unsigned len) 98 unsigned len)
99{ 99{
100 if (e->pos + len <= e->start) { 100 if (e->pos + len <= e->start) {
101 e->pos += len; 101 e->pos += len;
102 return false; 102 return false;
103 } 103 }
104 104
105 /* First vsnprintf needs to fit in its entirety for memmove */ 105 /* First vsnprintf needs to fit in its entirety for memmove */
106 if (len >= e->size) { 106 if (len >= e->size) {
107 e->err = -EIO; 107 e->err = -EIO;
108 return false; 108 return false;
109 } 109 }
110 110
111 return true; 111 return true;
112} 112}
113 113
114static void __i915_error_advance(struct drm_i915_error_state_buf *e, 114static void __i915_error_advance(struct drm_i915_error_state_buf *e,
115 unsigned len) 115 unsigned len)
116{ 116{
117 /* If this is first printf in this window, adjust it so that 117 /* If this is first printf in this window, adjust it so that
118 * start position matches start of the buffer 118 * start position matches start of the buffer
119 */ 119 */
120 120
121 if (e->pos < e->start) { 121 if (e->pos < e->start) {
122 const size_t off = e->start - e->pos; 122 const size_t off = e->start - e->pos;
123 123
124 /* Should not happen but be paranoid */ 124 /* Should not happen but be paranoid */
125 if (off > len || e->bytes) { 125 if (off > len || e->bytes) {
126 e->err = -EIO; 126 e->err = -EIO;
127 return; 127 return;
128 } 128 }
129 129
130 memmove(e->buf, e->buf + off, len - off); 130 memmove(e->buf, e->buf + off, len - off);
131 e->bytes = len - off; 131 e->bytes = len - off;
132 e->pos = e->start; 132 e->pos = e->start;
133 return; 133 return;
134 } 134 }
135 135
136 e->bytes += len; 136 e->bytes += len;
137 e->pos += len; 137 e->pos += len;
138} 138}
139 139
140static void i915_error_vprintf(struct drm_i915_error_state_buf *e, 140static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
141 const char *f, va_list args) 141 const char *f, va_list args)
142{ 142{
143 unsigned len; 143 unsigned len;
144 144
145 if (!__i915_error_ok(e)) 145 if (!__i915_error_ok(e))
146 return; 146 return;
147 147
148 /* Seek the first printf which is hits start position */ 148 /* Seek the first printf which is hits start position */
149 if (e->pos < e->start) { 149 if (e->pos < e->start) {
150 va_list tmp; 150 va_list tmp;
151 151
152 va_copy(tmp, args); 152 va_copy(tmp, args);
153 len = vsnprintf(NULL, 0, f, tmp); 153 len = vsnprintf(NULL, 0, f, tmp);
154 va_end(tmp); 154 va_end(tmp);
155 155
156 if (!__i915_error_seek(e, len)) 156 if (!__i915_error_seek(e, len))
157 return; 157 return;
158 } 158 }
159 159
160 len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args); 160 len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args);
161 if (len >= e->size - e->bytes) 161 if (len >= e->size - e->bytes)
162 len = e->size - e->bytes - 1; 162 len = e->size - e->bytes - 1;
163 163
164 __i915_error_advance(e, len); 164 __i915_error_advance(e, len);
165} 165}
166 166
167static void i915_error_puts(struct drm_i915_error_state_buf *e, 167static void i915_error_puts(struct drm_i915_error_state_buf *e,
168 const char *str) 168 const char *str)
169{ 169{
170 unsigned len; 170 unsigned len;
171 171
172 if (!__i915_error_ok(e)) 172 if (!__i915_error_ok(e))
173 return; 173 return;
174 174
175 len = strlen(str); 175 len = strlen(str);
176 176
177 /* Seek the first printf which is hits start position */ 177 /* Seek the first printf which is hits start position */
178 if (e->pos < e->start) { 178 if (e->pos < e->start) {
179 if (!__i915_error_seek(e, len)) 179 if (!__i915_error_seek(e, len))
180 return; 180 return;
181 } 181 }
182 182
183 if (len >= e->size - e->bytes) 183 if (len >= e->size - e->bytes)
184 len = e->size - e->bytes - 1; 184 len = e->size - e->bytes - 1;
185 memcpy(e->buf + e->bytes, str, len); 185 memcpy(e->buf + e->bytes, str, len);
186 186
187 __i915_error_advance(e, len); 187 __i915_error_advance(e, len);
188} 188}
189 189
190#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__) 190#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
191#define err_puts(e, s) i915_error_puts(e, s) 191#define err_puts(e, s) i915_error_puts(e, s)
192 192
193static void print_error_buffers(struct drm_i915_error_state_buf *m, 193static void print_error_buffers(struct drm_i915_error_state_buf *m,
194 const char *name, 194 const char *name,
195 struct drm_i915_error_buffer *err, 195 struct drm_i915_error_buffer *err,
196 int count) 196 int count)
197{ 197{
198 int i; 198 int i;
199 199
200 err_printf(m, " %s [%d]:\n", name, count); 200 err_printf(m, " %s [%d]:\n", name, count);
201 201
202 while (count--) { 202 while (count--) {
203 err_printf(m, " %08x_%08x %8u %02x %02x [ ", 203 err_printf(m, " %08x_%08x %8u %02x %02x [ ",
204 upper_32_bits(err->gtt_offset), 204 upper_32_bits(err->gtt_offset),
205 lower_32_bits(err->gtt_offset), 205 lower_32_bits(err->gtt_offset),
206 err->size, 206 err->size,
207 err->read_domains, 207 err->read_domains,
208 err->write_domain); 208 err->write_domain);
209 for (i = 0; i < I915_NUM_RINGS; i++) 209 for (i = 0; i < I915_NUM_RINGS; i++)
210 err_printf(m, "%02x ", err->rseqno[i]); 210 err_printf(m, "%02x ", err->rseqno[i]);
211 211
212 err_printf(m, "] %02x", err->wseqno); 212 err_printf(m, "] %02x", err->wseqno);
213 err_puts(m, pin_flag(err->pinned)); 213 err_puts(m, pin_flag(err->pinned));
214 err_puts(m, tiling_flag(err->tiling)); 214 err_puts(m, tiling_flag(err->tiling));
215 err_puts(m, dirty_flag(err->dirty)); 215 err_puts(m, dirty_flag(err->dirty));
216 err_puts(m, purgeable_flag(err->purgeable)); 216 err_puts(m, purgeable_flag(err->purgeable));
217 err_puts(m, err->userptr ? " userptr" : ""); 217 err_puts(m, err->userptr ? " userptr" : "");
218 err_puts(m, err->ring != -1 ? " " : ""); 218 err_puts(m, err->ring != -1 ? " " : "");
219 err_puts(m, ring_str(err->ring)); 219 err_puts(m, ring_str(err->ring));
220 err_puts(m, i915_cache_level_str(m->i915, err->cache_level)); 220 err_puts(m, i915_cache_level_str(m->i915, err->cache_level));
221 221
222 if (err->name) 222 if (err->name)
223 err_printf(m, " (name: %d)", err->name); 223 err_printf(m, " (name: %d)", err->name);
224 if (err->fence_reg != I915_FENCE_REG_NONE) 224 if (err->fence_reg != I915_FENCE_REG_NONE)
225 err_printf(m, " (fence: %d)", err->fence_reg); 225 err_printf(m, " (fence: %d)", err->fence_reg);
226 226
227 err_puts(m, "\n"); 227 err_puts(m, "\n");
228 err++; 228 err++;
229 } 229 }
230} 230}
231 231
232static const char *hangcheck_action_to_str(enum intel_ring_hangcheck_action a) 232static const char *hangcheck_action_to_str(enum intel_ring_hangcheck_action a)
233{ 233{
234 switch (a) { 234 switch (a) {
235 case HANGCHECK_IDLE: 235 case HANGCHECK_IDLE:
236 return "idle"; 236 return "idle";
237 case HANGCHECK_WAIT: 237 case HANGCHECK_WAIT:
238 return "wait"; 238 return "wait";
239 case HANGCHECK_ACTIVE: 239 case HANGCHECK_ACTIVE:
240 return "active"; 240 return "active";
241 case HANGCHECK_ACTIVE_LOOP: 241 case HANGCHECK_ACTIVE_LOOP:
242 return "active (loop)"; 242 return "active (loop)";
243 case HANGCHECK_KICK: 243 case HANGCHECK_KICK:
244 return "kick"; 244 return "kick";
245 case HANGCHECK_HUNG: 245 case HANGCHECK_HUNG:
246 return "hung"; 246 return "hung";
247 } 247 }
248 248
249 return "unknown"; 249 return "unknown";
250} 250}
251 251
252static void i915_ring_error_state(struct drm_i915_error_state_buf *m, 252static void i915_ring_error_state(struct drm_i915_error_state_buf *m,
253 struct drm_device *dev, 253 struct drm_device *dev,
254 struct drm_i915_error_state *error, 254 struct drm_i915_error_state *error,
255 int ring_idx) 255 int ring_idx)
256{ 256{
257 struct drm_i915_error_ring *ring = &error->ring[ring_idx]; 257 struct drm_i915_error_ring *ring = &error->ring[ring_idx];
258 258
259 if (!ring->valid) 259 if (!ring->valid)
260 return; 260 return;
261 261
262 err_printf(m, "%s command stream:\n", ring_str(ring_idx)); 262 err_printf(m, "%s command stream:\n", ring_str(ring_idx));
263 err_printf(m, " START: 0x%08x\n", ring->start); 263 err_printf(m, " START: 0x%08x\n", ring->start);
264 err_printf(m, " HEAD: 0x%08x\n", ring->head); 264 err_printf(m, " HEAD: 0x%08x\n", ring->head);
265 err_printf(m, " TAIL: 0x%08x\n", ring->tail); 265 err_printf(m, " TAIL: 0x%08x\n", ring->tail);
266 err_printf(m, " CTL: 0x%08x\n", ring->ctl); 266 err_printf(m, " CTL: 0x%08x\n", ring->ctl);
267 err_printf(m, " HWS: 0x%08x\n", ring->hws); 267 err_printf(m, " HWS: 0x%08x\n", ring->hws);
268 err_printf(m, " ACTHD: 0x%08x %08x\n", (u32)(ring->acthd>>32), (u32)ring->acthd); 268 err_printf(m, " ACTHD: 0x%08x %08x\n", (u32)(ring->acthd>>32), (u32)ring->acthd);
269 err_printf(m, " IPEIR: 0x%08x\n", ring->ipeir); 269 err_printf(m, " IPEIR: 0x%08x\n", ring->ipeir);
270 err_printf(m, " IPEHR: 0x%08x\n", ring->ipehr); 270 err_printf(m, " IPEHR: 0x%08x\n", ring->ipehr);
271 err_printf(m, " INSTDONE: 0x%08x\n", ring->instdone); 271 err_printf(m, " INSTDONE: 0x%08x\n", ring->instdone);
272 if (INTEL_INFO(dev)->gen >= 4) { 272 if (INTEL_INFO(dev)->gen >= 4) {
273 err_printf(m, " BBADDR: 0x%08x %08x\n", (u32)(ring->bbaddr>>32), (u32)ring->bbaddr); 273 err_printf(m, " BBADDR: 0x%08x %08x\n", (u32)(ring->bbaddr>>32), (u32)ring->bbaddr);
274 err_printf(m, " BB_STATE: 0x%08x\n", ring->bbstate); 274 err_printf(m, " BB_STATE: 0x%08x\n", ring->bbstate);
275 err_printf(m, " INSTPS: 0x%08x\n", ring->instps); 275 err_printf(m, " INSTPS: 0x%08x\n", ring->instps);
276 } 276 }
277 err_printf(m, " INSTPM: 0x%08x\n", ring->instpm); 277 err_printf(m, " INSTPM: 0x%08x\n", ring->instpm);
278 err_printf(m, " FADDR: 0x%08x %08x\n", upper_32_bits(ring->faddr), 278 err_printf(m, " FADDR: 0x%08x %08x\n", upper_32_bits(ring->faddr),
279 lower_32_bits(ring->faddr)); 279 lower_32_bits(ring->faddr));
280 if (INTEL_INFO(dev)->gen >= 6) { 280 if (INTEL_INFO(dev)->gen >= 6) {
281 err_printf(m, " RC PSMI: 0x%08x\n", ring->rc_psmi); 281 err_printf(m, " RC PSMI: 0x%08x\n", ring->rc_psmi);
282 err_printf(m, " FAULT_REG: 0x%08x\n", ring->fault_reg); 282 err_printf(m, " FAULT_REG: 0x%08x\n", ring->fault_reg);
283 err_printf(m, " SYNC_0: 0x%08x [last synced 0x%08x]\n", 283 err_printf(m, " SYNC_0: 0x%08x [last synced 0x%08x]\n",
284 ring->semaphore_mboxes[0], 284 ring->semaphore_mboxes[0],
285 ring->semaphore_seqno[0]); 285 ring->semaphore_seqno[0]);
286 err_printf(m, " SYNC_1: 0x%08x [last synced 0x%08x]\n", 286 err_printf(m, " SYNC_1: 0x%08x [last synced 0x%08x]\n",
287 ring->semaphore_mboxes[1], 287 ring->semaphore_mboxes[1],
288 ring->semaphore_seqno[1]); 288 ring->semaphore_seqno[1]);
289 if (HAS_VEBOX(dev)) { 289 if (HAS_VEBOX(dev)) {
290 err_printf(m, " SYNC_2: 0x%08x [last synced 0x%08x]\n", 290 err_printf(m, " SYNC_2: 0x%08x [last synced 0x%08x]\n",
291 ring->semaphore_mboxes[2], 291 ring->semaphore_mboxes[2],
292 ring->semaphore_seqno[2]); 292 ring->semaphore_seqno[2]);
293 } 293 }
294 } 294 }
295 if (USES_PPGTT(dev)) { 295 if (USES_PPGTT(dev)) {
296 err_printf(m, " GFX_MODE: 0x%08x\n", ring->vm_info.gfx_mode); 296 err_printf(m, " GFX_MODE: 0x%08x\n", ring->vm_info.gfx_mode);
297 297
298 if (INTEL_INFO(dev)->gen >= 8) { 298 if (INTEL_INFO(dev)->gen >= 8) {
299 int i; 299 int i;
300 for (i = 0; i < 4; i++) 300 for (i = 0; i < 4; i++)
301 err_printf(m, " PDP%d: 0x%016"PRIx64"\n", 301 err_printf(m, " PDP%d: 0x%016"PRIx64"\n",
302 i, ring->vm_info.pdp[i]); 302 i, ring->vm_info.pdp[i]);
303 } else { 303 } else {
304 err_printf(m, " PP_DIR_BASE: 0x%08x\n", 304 err_printf(m, " PP_DIR_BASE: 0x%08x\n",
305 ring->vm_info.pp_dir_base); 305 ring->vm_info.pp_dir_base);
306 } 306 }
307 } 307 }
308 err_printf(m, " seqno: 0x%08x\n", ring->seqno); 308 err_printf(m, " seqno: 0x%08x\n", ring->seqno);
309 err_printf(m, " waiting: %s\n", yesno(ring->waiting)); 309 err_printf(m, " waiting: %s\n", yesno(ring->waiting));
310 err_printf(m, " ring->head: 0x%08x\n", ring->cpu_ring_head); 310 err_printf(m, " ring->head: 0x%08x\n", ring->cpu_ring_head);
311 err_printf(m, " ring->tail: 0x%08x\n", ring->cpu_ring_tail); 311 err_printf(m, " ring->tail: 0x%08x\n", ring->cpu_ring_tail);
312 err_printf(m, " hangcheck: %s [%d]\n", 312 err_printf(m, " hangcheck: %s [%d]\n",
313 hangcheck_action_to_str(ring->hangcheck_action), 313 hangcheck_action_to_str(ring->hangcheck_action),
314 ring->hangcheck_score); 314 ring->hangcheck_score);
315} 315}
316 316
317void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...) 317void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
318{ 318{
319 va_list args; 319 va_list args;
320 320
321 va_start(args, f); 321 va_start(args, f);
322 i915_error_vprintf(e, f, args); 322 i915_error_vprintf(e, f, args);
323 va_end(args); 323 va_end(args);
324} 324}
325 325
326static void print_error_obj(struct drm_i915_error_state_buf *m, 326static void print_error_obj(struct drm_i915_error_state_buf *m,
327 struct drm_i915_error_object *obj) 327 struct drm_i915_error_object *obj)
328{ 328{
329 int page, offset, elt; 329 int page, offset, elt;
330 330
331 for (page = offset = 0; page < obj->page_count; page++) { 331 for (page = offset = 0; page < obj->page_count; page++) {
332 for (elt = 0; elt < PAGE_SIZE/4; elt++) { 332 for (elt = 0; elt < PAGE_SIZE/4; elt++) {
333 err_printf(m, "%08x : %08x\n", offset, 333 err_printf(m, "%08x : %08x\n", offset,
334 obj->pages[page][elt]); 334 obj->pages[page][elt]);
335 offset += 4; 335 offset += 4;
336 } 336 }
337 } 337 }
338} 338}
339 339
340int i915_error_state_to_str(struct drm_i915_error_state_buf *m, 340int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
341 const struct i915_error_state_file_priv *error_priv) 341 const struct i915_error_state_file_priv *error_priv)
342{ 342{
343 struct drm_device *dev = error_priv->dev; 343 struct drm_device *dev = error_priv->dev;
344 struct drm_i915_private *dev_priv = dev->dev_private; 344 struct drm_i915_private *dev_priv = dev->dev_private;
345 struct drm_i915_error_state *error = error_priv->error; 345 struct drm_i915_error_state *error = error_priv->error;
346 struct drm_i915_error_object *obj; 346 struct drm_i915_error_object *obj;
347 int i, j, offset, elt; 347 int i, j, offset, elt;
348 int max_hangcheck_score; 348 int max_hangcheck_score;
349 349
350 if (!error) { 350 if (!error) {
351 err_printf(m, "no error state collected\n"); 351 err_printf(m, "no error state collected\n");
352 goto out; 352 goto out;
353 } 353 }
354 354
355 err_printf(m, "%s\n", error->error_msg); 355 err_printf(m, "%s\n", error->error_msg);
356 err_printf(m, "Time: %"PRIdMAX" s %ld us\n", (intmax_t)error->time.tv_sec, 356 err_printf(m, "Time: %"PRIdMAX" s %ld us\n", (intmax_t)error->time.tv_sec,
357 (long)error->time.tv_usec); 357 (long)error->time.tv_usec);
358 err_printf(m, "Kernel: " __NetBSD_Version__ "\n"); 358 err_printf(m, "Kernel: %d\n", __NetBSD_Version__);
359 err_printf(m, "Time: %ld s %ld us\n", error->time.tv_sec, 359 err_printf(m, "Time: %ld s %ld us\n", error->time.tv_sec,
360 error->time.tv_usec); 360 error->time.tv_usec);
361 max_hangcheck_score = 0; 361 max_hangcheck_score = 0;
362 for (i = 0; i < ARRAY_SIZE(error->ring); i++) { 362 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
363 if (error->ring[i].hangcheck_score > max_hangcheck_score) 363 if (error->ring[i].hangcheck_score > max_hangcheck_score)
364 max_hangcheck_score = error->ring[i].hangcheck_score; 364 max_hangcheck_score = error->ring[i].hangcheck_score;
365 } 365 }
366 for (i = 0; i < ARRAY_SIZE(error->ring); i++) { 366 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
367 if (error->ring[i].hangcheck_score == max_hangcheck_score && 367 if (error->ring[i].hangcheck_score == max_hangcheck_score &&
368 error->ring[i].pid != -1) { 368 error->ring[i].pid != -1) {
369 err_printf(m, "Active process (on ring %s): %s [%d]\n", 369 err_printf(m, "Active process (on ring %s): %s [%d]\n",
370 ring_str(i), 370 ring_str(i),
371 error->ring[i].comm, 371 error->ring[i].comm,
372 error->ring[i].pid); 372 error->ring[i].pid);
373 } 373 }
374 } 374 }
375 err_printf(m, "Reset count: %u\n", error->reset_count); 375 err_printf(m, "Reset count: %u\n", error->reset_count);
376 err_printf(m, "Suspend count: %u\n", error->suspend_count); 376 err_printf(m, "Suspend count: %u\n", error->suspend_count);
377 err_printf(m, "PCI ID: 0x%04x\n", dev->pdev->device); 377 err_printf(m, "PCI ID: 0x%04x\n", dev->pdev->device);
378 err_printf(m, "IOMMU enabled?: %d\n", error->iommu); 378 err_printf(m, "IOMMU enabled?: %d\n", error->iommu);
379 err_printf(m, "EIR: 0x%08x\n", error->eir); 379 err_printf(m, "EIR: 0x%08x\n", error->eir);
380 err_printf(m, "IER: 0x%08x\n", error->ier); 380 err_printf(m, "IER: 0x%08x\n", error->ier);
381 if (INTEL_INFO(dev)->gen >= 8) { 381 if (INTEL_INFO(dev)->gen >= 8) {
382 for (i = 0; i < 4; i++) 382 for (i = 0; i < 4; i++)
383 err_printf(m, "GTIER gt %d: 0x%08x\n", i, 383 err_printf(m, "GTIER gt %d: 0x%08x\n", i,
384 error->gtier[i]); 384 error->gtier[i]);
385 } else if (HAS_PCH_SPLIT(dev) || IS_VALLEYVIEW(dev)) 385 } else if (HAS_PCH_SPLIT(dev) || IS_VALLEYVIEW(dev))
386 err_printf(m, "GTIER: 0x%08x\n", error->gtier[0]); 386 err_printf(m, "GTIER: 0x%08x\n", error->gtier[0]);
387 err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er); 387 err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
388 err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake); 388 err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
389 err_printf(m, "DERRMR: 0x%08x\n", error->derrmr); 389 err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
390 err_printf(m, "CCID: 0x%08x\n", error->ccid); 390 err_printf(m, "CCID: 0x%08x\n", error->ccid);
391 err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings); 391 err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
392 392
393 for (i = 0; i < dev_priv->num_fence_regs; i++) 393 for (i = 0; i < dev_priv->num_fence_regs; i++)
394 err_printf(m, " fence[%d] = %08"PRIx64"\n", i, error->fence[i]); 394 err_printf(m, " fence[%d] = %08"PRIx64"\n", i, error->fence[i]);
395 395
396 for (i = 0; i < ARRAY_SIZE(error->extra_instdone); i++) 396 for (i = 0; i < ARRAY_SIZE(error->extra_instdone); i++)
397 err_printf(m, " INSTDONE_%d: 0x%08x\n", i, 397 err_printf(m, " INSTDONE_%d: 0x%08x\n", i,
398 error->extra_instdone[i]); 398 error->extra_instdone[i]);
399 399
400 if (INTEL_INFO(dev)->gen >= 6) { 400 if (INTEL_INFO(dev)->gen >= 6) {
401 err_printf(m, "ERROR: 0x%08x\n", error->error); 401 err_printf(m, "ERROR: 0x%08x\n", error->error);
402 402
403 if (INTEL_INFO(dev)->gen >= 8) 403 if (INTEL_INFO(dev)->gen >= 8)
404 err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n", 404 err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n",
405 error->fault_data1, error->fault_data0); 405 error->fault_data1, error->fault_data0);
406 406
407 err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg); 407 err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
408 } 408 }
409 409
410 if (INTEL_INFO(dev)->gen == 7) 410 if (INTEL_INFO(dev)->gen == 7)
411 err_printf(m, "ERR_INT: 0x%08x\n", error->err_int); 411 err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
412 412
413 for (i = 0; i < ARRAY_SIZE(error->ring); i++) 413 for (i = 0; i < ARRAY_SIZE(error->ring); i++)
414 i915_ring_error_state(m, dev, error, i); 414 i915_ring_error_state(m, dev, error, i);
415 415
416 for (i = 0; i < error->vm_count; i++) { 416 for (i = 0; i < error->vm_count; i++) {
417 err_printf(m, "vm[%d]\n", i); 417 err_printf(m, "vm[%d]\n", i);
418 418
419 print_error_buffers(m, "Active", 419 print_error_buffers(m, "Active",
420 error->active_bo[i], 420 error->active_bo[i],
421 error->active_bo_count[i]); 421 error->active_bo_count[i]);
422 422
423 print_error_buffers(m, "Pinned", 423 print_error_buffers(m, "Pinned",
424 error->pinned_bo[i], 424 error->pinned_bo[i],
425 error->pinned_bo_count[i]); 425 error->pinned_bo_count[i]);
426 } 426 }
427 427
428 for (i = 0; i < ARRAY_SIZE(error->ring); i++) { 428 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
429 obj = error->ring[i].batchbuffer; 429 obj = error->ring[i].batchbuffer;
430 if (obj) { 430 if (obj) {
431 err_puts(m, dev_priv->ring[i].name); 431 err_puts(m, dev_priv->ring[i].name);
432 if (error->ring[i].pid != -1) 432 if (error->ring[i].pid != -1)
433 err_printf(m, " (submitted by %s [%d])", 433 err_printf(m, " (submitted by %s [%d])",
434 error->ring[i].comm, 434 error->ring[i].comm,
435 error->ring[i].pid); 435 error->ring[i].pid);
436 err_printf(m, " --- gtt_offset = 0x%08x %08x\n", 436 err_printf(m, " --- gtt_offset = 0x%08x %08x\n",
437 upper_32_bits(obj->gtt_offset), 437 upper_32_bits(obj->gtt_offset),
438 lower_32_bits(obj->gtt_offset)); 438 lower_32_bits(obj->gtt_offset));
439 print_error_obj(m, obj); 439 print_error_obj(m, obj);
440 } 440 }
441 441
442 obj = error->ring[i].wa_batchbuffer; 442 obj = error->ring[i].wa_batchbuffer;
443 if (obj) { 443 if (obj) {
444 err_printf(m, "%s (w/a) --- gtt_offset = 0x%08x\n", 444 err_printf(m, "%s (w/a) --- gtt_offset = 0x%08x\n",
445 dev_priv->ring[i].name, 445 dev_priv->ring[i].name,
446 lower_32_bits(obj->gtt_offset)); 446 lower_32_bits(obj->gtt_offset));
447 print_error_obj(m, obj); 447 print_error_obj(m, obj);
448 } 448 }
449 449
450 if (error->ring[i].num_requests) { 450 if (error->ring[i].num_requests) {
451 err_printf(m, "%s --- %d requests\n", 451 err_printf(m, "%s --- %d requests\n",
452 dev_priv->ring[i].name, 452 dev_priv->ring[i].name,
453 error->ring[i].num_requests); 453 error->ring[i].num_requests);
454 for (j = 0; j < error->ring[i].num_requests; j++) { 454 for (j = 0; j < error->ring[i].num_requests; j++) {
455 err_printf(m, " seqno 0x%08x, emitted %ld, tail 0x%08x\n", 455 err_printf(m, " seqno 0x%08x, emitted %ld, tail 0x%08x\n",
456 error->ring[i].requests[j].seqno, 456 error->ring[i].requests[j].seqno,
457 error->ring[i].requests[j].jiffies, 457 error->ring[i].requests[j].jiffies,
458 error->ring[i].requests[j].tail); 458 error->ring[i].requests[j].tail);
459 } 459 }
460 } 460 }
461 461
462 if ((obj = error->ring[i].ringbuffer)) { 462 if ((obj = error->ring[i].ringbuffer)) {
463 err_printf(m, "%s --- ringbuffer = 0x%08x\n", 463 err_printf(m, "%s --- ringbuffer = 0x%08x\n",
464 dev_priv->ring[i].name, 464 dev_priv->ring[i].name,
465 lower_32_bits(obj->gtt_offset)); 465 lower_32_bits(obj->gtt_offset));
466 print_error_obj(m, obj); 466 print_error_obj(m, obj);
467 } 467 }
468 468
469 if ((obj = error->ring[i].hws_page)) { 469 if ((obj = error->ring[i].hws_page)) {
470 u64 hws_offset = obj->gtt_offset; 470 u64 hws_offset = obj->gtt_offset;
471 u32 *hws_page = &obj->pages[0][0]; 471 u32 *hws_page = &obj->pages[0][0];
472 472
473 if (i915.enable_execlists) { 473 if (i915.enable_execlists) {
474 hws_offset += LRC_PPHWSP_PN * PAGE_SIZE; 474 hws_offset += LRC_PPHWSP_PN * PAGE_SIZE;
475 hws_page = &obj->pages[LRC_PPHWSP_PN][0]; 475 hws_page = &obj->pages[LRC_PPHWSP_PN][0];
476 } 476 }
477 err_printf(m, "%s --- HW Status = 0x%08llx\n", 477 err_printf(m, "%s --- HW Status = 0x%08llx\n",
478 dev_priv->ring[i].name, hws_offset); 478 dev_priv->ring[i].name, hws_offset);
479 offset = 0; 479 offset = 0;
480 for (elt = 0; elt < PAGE_SIZE/16; elt += 4) { 480 for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
481 err_printf(m, "[%04x] %08x %08x %08x %08x\n", 481 err_printf(m, "[%04x] %08x %08x %08x %08x\n",
482 offset, 482 offset,
483 hws_page[elt], 483 hws_page[elt],
484 hws_page[elt+1], 484 hws_page[elt+1],
485 hws_page[elt+2], 485 hws_page[elt+2],
486 hws_page[elt+3]); 486 hws_page[elt+3]);
487 offset += 16; 487 offset += 16;
488 } 488 }
489 } 489 }
490 490
491 if ((obj = error->ring[i].ctx)) { 491 if ((obj = error->ring[i].ctx)) {
492 err_printf(m, "%s --- HW Context = 0x%08x\n", 492 err_printf(m, "%s --- HW Context = 0x%08x\n",
493 dev_priv->ring[i].name, 493 dev_priv->ring[i].name,
494 lower_32_bits(obj->gtt_offset)); 494 lower_32_bits(obj->gtt_offset));
495 print_error_obj(m, obj); 495 print_error_obj(m, obj);
496 } 496 }
497 } 497 }
498 498
499 if ((obj = error->semaphore_obj)) { 499 if ((obj = error->semaphore_obj)) {
500 err_printf(m, "Semaphore page = 0x%08x\n", 500 err_printf(m, "Semaphore page = 0x%08x\n",
501 lower_32_bits(obj->gtt_offset)); 501 lower_32_bits(obj->gtt_offset));
502 for (elt = 0; elt < PAGE_SIZE/16; elt += 4) { 502 for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
503 err_printf(m, "[%04x] %08x %08x %08x %08x\n", 503 err_printf(m, "[%04x] %08x %08x %08x %08x\n",
504 elt * 4, 504 elt * 4,
505 obj->pages[0][elt], 505 obj->pages[0][elt],
506 obj->pages[0][elt+1], 506 obj->pages[0][elt+1],
507 obj->pages[0][elt+2], 507 obj->pages[0][elt+2],
508 obj->pages[0][elt+3]); 508 obj->pages[0][elt+3]);
509 } 509 }
510 } 510 }
511 511
512 if (error->overlay) 512 if (error->overlay)
513 intel_overlay_print_error_state(m, error->overlay); 513 intel_overlay_print_error_state(m, error->overlay);
514 514
515 if (error->display) 515 if (error->display)
516 intel_display_print_error_state(m, dev, error->display); 516 intel_display_print_error_state(m, dev, error->display);
517 517
518out: 518out:
519 if (m->bytes == 0 && m->err) 519 if (m->bytes == 0 && m->err)
520 return m->err; 520 return m->err;
521 521
522 return 0; 522 return 0;
523} 523}
524 524
525int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf, 525int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
526 struct drm_i915_private *i915, 526 struct drm_i915_private *i915,
527 size_t count, loff_t pos) 527 size_t count, loff_t pos)
528{ 528{
529 memset(ebuf, 0, sizeof(*ebuf)); 529 memset(ebuf, 0, sizeof(*ebuf));
530 ebuf->i915 = i915; 530 ebuf->i915 = i915;
531 531
532 /* We need to have enough room to store any i915_error_state printf 532 /* We need to have enough room to store any i915_error_state printf
533 * so that we can move it to start position. 533 * so that we can move it to start position.
534 */ 534 */
535 ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE; 535 ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
536 ebuf->buf = kmalloc(ebuf->size, 536 ebuf->buf = kmalloc(ebuf->size,
537 GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN); 537 GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
538 538
539 if (ebuf->buf == NULL) { 539 if (ebuf->buf == NULL) {
540 ebuf->size = PAGE_SIZE; 540 ebuf->size = PAGE_SIZE;
541 ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY); 541 ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
542 } 542 }
543 543
544 if (ebuf->buf == NULL) { 544 if (ebuf->buf == NULL) {
545 ebuf->size = 128; 545 ebuf->size = 128;
546 ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY); 546 ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
547 } 547 }
548 548
549 if (ebuf->buf == NULL) 549 if (ebuf->buf == NULL)
550 return -ENOMEM; 550 return -ENOMEM;
551 551
552 ebuf->start = pos; 552 ebuf->start = pos;
553 553
554 return 0; 554 return 0;
555} 555}
556 556
557static void i915_error_object_free(struct drm_i915_error_object *obj) 557static void i915_error_object_free(struct drm_i915_error_object *obj)
558{ 558{
559 int page; 559 int page;
560 560
561 if (obj == NULL) 561 if (obj == NULL)
562 return; 562 return;
563 563
564 for (page = 0; page < obj->page_count; page++) 564 for (page = 0; page < obj->page_count; page++)
565 kfree(obj->pages[page]); 565 kfree(obj->pages[page]);
566 566
567 kfree(obj); 567 kfree(obj);
568} 568}
569 569
570static void i915_error_state_free(struct kref *error_ref) 570static void i915_error_state_free(struct kref *error_ref)
571{ 571{
572 struct drm_i915_error_state *error = container_of(error_ref, 572 struct drm_i915_error_state *error = container_of(error_ref,
573 typeof(*error), ref); 573 typeof(*error), ref);
574 int i; 574 int i;
575 575
576 for (i = 0; i < ARRAY_SIZE(error->ring); i++) { 576 for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
577 i915_error_object_free(error->ring[i].batchbuffer); 577 i915_error_object_free(error->ring[i].batchbuffer);
578 i915_error_object_free(error->ring[i].wa_batchbuffer); 578 i915_error_object_free(error->ring[i].wa_batchbuffer);
579 i915_error_object_free(error->ring[i].ringbuffer); 579 i915_error_object_free(error->ring[i].ringbuffer);
580 i915_error_object_free(error->ring[i].hws_page); 580 i915_error_object_free(error->ring[i].hws_page);
581 i915_error_object_free(error->ring[i].ctx); 581 i915_error_object_free(error->ring[i].ctx);
582 kfree(error->ring[i].requests); 582 kfree(error->ring[i].requests);
583 } 583 }
584 584
585 i915_error_object_free(error->semaphore_obj); 585 i915_error_object_free(error->semaphore_obj);
586 586
587 for (i = 0; i < error->vm_count; i++) 587 for (i = 0; i < error->vm_count; i++)
588 kfree(error->active_bo[i]); 588 kfree(error->active_bo[i]);
589 589
590 kfree(error->active_bo); 590 kfree(error->active_bo);
591 kfree(error->active_bo_count); 591 kfree(error->active_bo_count);
592 kfree(error->pinned_bo); 592 kfree(error->pinned_bo);
593 kfree(error->pinned_bo_count); 593 kfree(error->pinned_bo_count);
594 kfree(error->overlay); 594 kfree(error->overlay);
595 kfree(error->display); 595 kfree(error->display);
596 kfree(error); 596 kfree(error);
597} 597}
598 598
599#ifdef __NetBSD__ 599#ifdef __NetBSD__
600# define __aperture_iomem 600# define __aperture_iomem
601# define __iomem __aperture_iomem 601# define __iomem __aperture_iomem
602#endif 602#endif
603 603
604static struct drm_i915_error_object * 604static struct drm_i915_error_object *
605i915_error_object_create(struct drm_i915_private *dev_priv, 605i915_error_object_create(struct drm_i915_private *dev_priv,
606 struct drm_i915_gem_object *src, 606 struct drm_i915_gem_object *src,
607 struct i915_address_space *vm) 607 struct i915_address_space *vm)
608{ 608{
609 struct drm_i915_error_object *dst; 609 struct drm_i915_error_object *dst;
610 struct i915_vma *vma = NULL; 610 struct i915_vma *vma = NULL;
611 int num_pages; 611 int num_pages;
612 bool use_ggtt; 612 bool use_ggtt;
613 int i = 0; 613 int i = 0;
614 u64 reloc_offset; 614 u64 reloc_offset;
615 615
616 if (src == NULL || src->pages == NULL) 616 if (src == NULL || src->pages == NULL)
617 return NULL; 617 return NULL;
618 618
619 num_pages = src->base.size >> PAGE_SHIFT; 619 num_pages = src->base.size >> PAGE_SHIFT;
620 620
621 dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC); 621 dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
622 if (dst == NULL) 622 if (dst == NULL)
623 return NULL; 623 return NULL;
624 624
625 if (i915_gem_obj_bound(src, vm)) 625 if (i915_gem_obj_bound(src, vm))
626 dst->gtt_offset = i915_gem_obj_offset(src, vm); 626 dst->gtt_offset = i915_gem_obj_offset(src, vm);
627 else 627 else
628 dst->gtt_offset = -1; 628 dst->gtt_offset = -1;
629 629
630 reloc_offset = dst->gtt_offset; 630 reloc_offset = dst->gtt_offset;
631 if (i915_is_ggtt(vm)) 631 if (i915_is_ggtt(vm))
632 vma = i915_gem_obj_to_ggtt(src); 632 vma = i915_gem_obj_to_ggtt(src);
633 use_ggtt = (src->cache_level == I915_CACHE_NONE && 633 use_ggtt = (src->cache_level == I915_CACHE_NONE &&
634 vma && (vma->bound & GLOBAL_BIND) && 634 vma && (vma->bound & GLOBAL_BIND) &&
635 reloc_offset + num_pages * PAGE_SIZE <= dev_priv->gtt.mappable_end); 635 reloc_offset + num_pages * PAGE_SIZE <= dev_priv->gtt.mappable_end);
636 636
637 /* Cannot access stolen address directly, try to use the aperture */ 637 /* Cannot access stolen address directly, try to use the aperture */
638 if (src->stolen) { 638 if (src->stolen) {
639 use_ggtt = true; 639 use_ggtt = true;
640 640
641 if (!(vma && vma->bound & GLOBAL_BIND)) 641 if (!(vma && vma->bound & GLOBAL_BIND))
642 goto unwind; 642 goto unwind;
643 643
644 reloc_offset = i915_gem_obj_ggtt_offset(src); 644 reloc_offset = i915_gem_obj_ggtt_offset(src);
645 if (reloc_offset + num_pages * PAGE_SIZE > dev_priv->gtt.mappable_end) 645 if (reloc_offset + num_pages * PAGE_SIZE > dev_priv->gtt.mappable_end)
646 goto unwind; 646 goto unwind;
647 } 647 }
648 648
649 /* Cannot access snooped pages through the aperture */ 649 /* Cannot access snooped pages through the aperture */
650 if (use_ggtt && src->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv->dev)) 650 if (use_ggtt && src->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv->dev))
651 goto unwind; 651 goto unwind;
652 652
653 dst->page_count = num_pages; 653 dst->page_count = num_pages;
654 while (num_pages--) { 654 while (num_pages--) {
655 unsigned long flags; 655 unsigned long flags;
656 void *d; 656 void *d;
657 657
658 d = kmalloc(PAGE_SIZE, GFP_ATOMIC); 658 d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
659 if (d == NULL) 659 if (d == NULL)
660 goto unwind; 660 goto unwind;
661 661
662 local_irq_save(flags); 662 local_irq_save(flags);
663 if (use_ggtt) { 663 if (use_ggtt) {
664 void __iomem *s; 664 void __iomem *s;
665 665
666 /* Simply ignore tiling or any overlapping fence. 666 /* Simply ignore tiling or any overlapping fence.
667 * It's part of the error state, and this hopefully 667 * It's part of the error state, and this hopefully
668 * captures what the GPU read. 668 * captures what the GPU read.
669 */ 669 */
670 670
671 s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable, 671 s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
672 reloc_offset); 672 reloc_offset);
673 memcpy_fromio(d, s, PAGE_SIZE); 673 memcpy_fromio(d, s, PAGE_SIZE);
674#ifdef __NetBSD__ 674#ifdef __NetBSD__
675 io_mapping_unmap_atomic(dev_priv->gtt.mappable, s); 675 io_mapping_unmap_atomic(dev_priv->gtt.mappable, s);
676#else 676#else
677 io_mapping_unmap_atomic(s); 677 io_mapping_unmap_atomic(s);
678#endif 678#endif
679 } else { 679 } else {
680 680
681 if (cpu_intr_p() || cpu_softintr_p() || 681 if (cpu_intr_p() || cpu_softintr_p() ||
682 (curlwp->l_pflag & LP_INTR) != 0) { 682 (curlwp->l_pflag & LP_INTR) != 0) {
683 /* 683 /*
684 * We can't take locks during interrupts 684 * We can't take locks during interrupts
685 * and finding the page from uvm requires 685 * and finding the page from uvm requires
686 * taking a lock. Checking for an interrupt 686 * taking a lock. Checking for an interrupt
687 * context is bogus, but this is the least 687 * context is bogus, but this is the least
688 * intrusive change. Zero the result, doesn't 688 * intrusive change. Zero the result, doesn't
689 * matter much, because this is only used 689 * matter much, because this is only used
690 * for diagnostics. 690 * for diagnostics.
691 */ 691 */
692 memset(d, 0, PAGE_SIZE); 692 memset(d, 0, PAGE_SIZE);
693 } else { 693 } else {
694 struct page *page; 694 struct page *page;
695 void *s; 695 void *s;
696 696
697 page = i915_gem_object_get_page(src, i); 697 page = i915_gem_object_get_page(src, i);
698 698
699 drm_clflush_pages(&page, 1); 699 drm_clflush_pages(&page, 1);
700 700
701 s = kmap_atomic(page); 701 s = kmap_atomic(page);
702 memcpy(d, s, PAGE_SIZE); 702 memcpy(d, s, PAGE_SIZE);
703 kunmap_atomic(s); 703 kunmap_atomic(s);
704 704
705 drm_clflush_pages(&page, 1); 705 drm_clflush_pages(&page, 1);
706 } 706 }
707 } 707 }
708 local_irq_restore(flags); 708 local_irq_restore(flags);
709 709
710 dst->pages[i++] = d; 710 dst->pages[i++] = d;
711 reloc_offset += PAGE_SIZE; 711 reloc_offset += PAGE_SIZE;
712 } 712 }
713 713
714 return dst; 714 return dst;
715 715
716unwind: 716unwind:
717 while (i--) 717 while (i--)
718 kfree(dst->pages[i]); 718 kfree(dst->pages[i]);
719 kfree(dst); 719 kfree(dst);
720 return NULL; 720 return NULL;
721} 721}
722#define i915_error_ggtt_object_create(dev_priv, src) \ 722#define i915_error_ggtt_object_create(dev_priv, src) \
723 i915_error_object_create((dev_priv), (src), &(dev_priv)->gtt.base) 723 i915_error_object_create((dev_priv), (src), &(dev_priv)->gtt.base)
724 724
725#ifdef __NetBSD__ 725#ifdef __NetBSD__
726# undef __iomem 726# undef __iomem
727# undef __aperture_iomem 727# undef __aperture_iomem
728#endif 728#endif
729 729
730static void capture_bo(struct drm_i915_error_buffer *err, 730static void capture_bo(struct drm_i915_error_buffer *err,
731 struct i915_vma *vma) 731 struct i915_vma *vma)
732{ 732{
733 struct drm_i915_gem_object *obj = vma->obj; 733 struct drm_i915_gem_object *obj = vma->obj;
734 int i; 734 int i;
735 735
736 err->size = obj->base.size; 736 err->size = obj->base.size;
737 err->name = obj->base.name; 737 err->name = obj->base.name;
738 for (i = 0; i < I915_NUM_RINGS; i++) 738 for (i = 0; i < I915_NUM_RINGS; i++)
739 err->rseqno[i] = i915_gem_request_get_seqno(obj->last_read_req[i]); 739 err->rseqno[i] = i915_gem_request_get_seqno(obj->last_read_req[i]);
740 err->wseqno = i915_gem_request_get_seqno(obj->last_write_req); 740 err->wseqno = i915_gem_request_get_seqno(obj->last_write_req);
741 err->gtt_offset = vma->node.start; 741 err->gtt_offset = vma->node.start;
742 err->read_domains = obj->base.read_domains; 742 err->read_domains = obj->base.read_domains;
743 err->write_domain = obj->base.write_domain; 743 err->write_domain = obj->base.write_domain;
744 err->fence_reg = obj->fence_reg; 744 err->fence_reg = obj->fence_reg;
745 err->pinned = 0; 745 err->pinned = 0;
746 if (i915_gem_obj_is_pinned(obj)) 746 if (i915_gem_obj_is_pinned(obj))
747 err->pinned = 1; 747 err->pinned = 1;
748 err->tiling = obj->tiling_mode; 748 err->tiling = obj->tiling_mode;
749 err->dirty = obj->dirty; 749 err->dirty = obj->dirty;
750 err->purgeable = obj->madv != I915_MADV_WILLNEED; 750 err->purgeable = obj->madv != I915_MADV_WILLNEED;
751 err->userptr = obj->userptr.mm != NULL; 751 err->userptr = obj->userptr.mm != NULL;
752 err->ring = obj->last_write_req ? 752 err->ring = obj->last_write_req ?
753 i915_gem_request_get_ring(obj->last_write_req)->id : -1; 753 i915_gem_request_get_ring(obj->last_write_req)->id : -1;
754 err->cache_level = obj->cache_level; 754 err->cache_level = obj->cache_level;
755} 755}
756 756
757static u32 capture_active_bo(struct drm_i915_error_buffer *err, 757static u32 capture_active_bo(struct drm_i915_error_buffer *err,
758 int count, struct list_head *head) 758 int count, struct list_head *head)
759{ 759{
760 struct i915_vma *vma; 760 struct i915_vma *vma;
761 int i = 0; 761 int i = 0;
762 762
763 list_for_each_entry(vma, head, mm_list) { 763 list_for_each_entry(vma, head, mm_list) {
764 capture_bo(err++, vma); 764 capture_bo(err++, vma);
765 if (++i == count) 765 if (++i == count)
766 break; 766 break;
767 } 767 }
768 768
769 return i; 769 return i;
770} 770}
771 771
772static u32 capture_pinned_bo(struct drm_i915_error_buffer *err, 772static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
773 int count, struct list_head *head, 773 int count, struct list_head *head,
774 struct i915_address_space *vm) 774 struct i915_address_space *vm)
775{ 775{
776 struct drm_i915_gem_object *obj; 776 struct drm_i915_gem_object *obj;
777 struct drm_i915_error_buffer * const first = err; 777 struct drm_i915_error_buffer * const first = err;
778 struct drm_i915_error_buffer * const last = err + count; 778 struct drm_i915_error_buffer * const last = err + count;
779 779
780 list_for_each_entry(obj, head, global_list) { 780 list_for_each_entry(obj, head, global_list) {
781 struct i915_vma *vma; 781 struct i915_vma *vma;
782 782
783 if (err == last) 783 if (err == last)
784 break; 784 break;
785 785
786 list_for_each_entry(vma, &obj->vma_list, vma_link) 786 list_for_each_entry(vma, &obj->vma_list, vma_link)
787 if (vma->vm == vm && vma->pin_count > 0) 787 if (vma->vm == vm && vma->pin_count > 0)
788 capture_bo(err++, vma); 788 capture_bo(err++, vma);
789 } 789 }
790 790
791 return err - first; 791 return err - first;
792} 792}
793 793
794/* Generate a semi-unique error code. The code is not meant to have meaning, The 794/* Generate a semi-unique error code. The code is not meant to have meaning, The
795 * code's only purpose is to try to prevent false duplicated bug reports by 795 * code's only purpose is to try to prevent false duplicated bug reports by
796 * grossly estimating a GPU error state. 796 * grossly estimating a GPU error state.
797 * 797 *
798 * TODO Ideally, hashing the batchbuffer would be a very nice way to determine 798 * TODO Ideally, hashing the batchbuffer would be a very nice way to determine
799 * the hang if we could strip the GTT offset information from it. 799 * the hang if we could strip the GTT offset information from it.
800 * 800 *
801 * It's only a small step better than a random number in its current form. 801 * It's only a small step better than a random number in its current form.
802 */ 802 */
803static uint32_t i915_error_generate_code(struct drm_i915_private *dev_priv, 803static uint32_t i915_error_generate_code(struct drm_i915_private *dev_priv,
804 struct drm_i915_error_state *error, 804 struct drm_i915_error_state *error,
805 int *ring_id) 805 int *ring_id)
806{ 806{
807 uint32_t error_code = 0; 807 uint32_t error_code = 0;
808 int i; 808 int i;
809 809
810 /* IPEHR would be an ideal way to detect errors, as it's the gross 810 /* IPEHR would be an ideal way to detect errors, as it's the gross
811 * measure of "the command that hung." However, has some very common 811 * measure of "the command that hung." However, has some very common
812 * synchronization commands which almost always appear in the case 812 * synchronization commands which almost always appear in the case
813 * strictly a client bug. Use instdone to differentiate those some. 813 * strictly a client bug. Use instdone to differentiate those some.
814 */ 814 */
815 for (i = 0; i < I915_NUM_RINGS; i++) { 815 for (i = 0; i < I915_NUM_RINGS; i++) {
816 if (error->ring[i].hangcheck_action == HANGCHECK_HUNG) { 816 if (error->ring[i].hangcheck_action == HANGCHECK_HUNG) {
817 if (ring_id) 817 if (ring_id)
818 *ring_id = i; 818 *ring_id = i;
819 819
820 return error->ring[i].ipehr ^ error->ring[i].instdone; 820 return error->ring[i].ipehr ^ error->ring[i].instdone;
821 } 821 }
822 } 822 }
823 823
824 return error_code; 824 return error_code;
825} 825}
826 826
827static void i915_gem_record_fences(struct drm_device *dev, 827static void i915_gem_record_fences(struct drm_device *dev,
828 struct drm_i915_error_state *error) 828 struct drm_i915_error_state *error)
829{ 829{
830 struct drm_i915_private *dev_priv = dev->dev_private; 830 struct drm_i915_private *dev_priv = dev->dev_private;
831 int i; 831 int i;
832 832
833 if (IS_GEN3(dev) || IS_GEN2(dev)) { 833 if (IS_GEN3(dev) || IS_GEN2(dev)) {
834 for (i = 0; i < dev_priv->num_fence_regs; i++) 834 for (i = 0; i < dev_priv->num_fence_regs; i++)
835 error->fence[i] = I915_READ(FENCE_REG(i)); 835 error->fence[i] = I915_READ(FENCE_REG(i));
836 } else if (IS_GEN5(dev) || IS_GEN4(dev)) { 836 } else if (IS_GEN5(dev) || IS_GEN4(dev)) {
837 for (i = 0; i < dev_priv->num_fence_regs; i++) 837 for (i = 0; i < dev_priv->num_fence_regs; i++)
838 error->fence[i] = I915_READ64(FENCE_REG_965_LO(i)); 838 error->fence[i] = I915_READ64(FENCE_REG_965_LO(i));
839 } else if (INTEL_INFO(dev)->gen >= 6) { 839 } else if (INTEL_INFO(dev)->gen >= 6) {
840 for (i = 0; i < dev_priv->num_fence_regs; i++) 840 for (i = 0; i < dev_priv->num_fence_regs; i++)
841 error->fence[i] = I915_READ64(FENCE_REG_GEN6_LO(i)); 841 error->fence[i] = I915_READ64(FENCE_REG_GEN6_LO(i));
842 } 842 }
843} 843}
844 844
845 845
846static void gen8_record_semaphore_state(struct drm_i915_private *dev_priv, 846static void gen8_record_semaphore_state(struct drm_i915_private *dev_priv,
847 struct drm_i915_error_state *error, 847 struct drm_i915_error_state *error,
848 struct intel_engine_cs *ring, 848 struct intel_engine_cs *ring,
849 struct drm_i915_error_ring *ering) 849 struct drm_i915_error_ring *ering)
850{ 850{
851 struct intel_engine_cs *to; 851 struct intel_engine_cs *to;
852 int i; 852 int i;
853 853
854 if (!i915_semaphore_is_enabled(dev_priv->dev)) 854 if (!i915_semaphore_is_enabled(dev_priv->dev))
855 return; 855 return;
856 856
857 if (!error->semaphore_obj) 857 if (!error->semaphore_obj)
858 error->semaphore_obj = 858 error->semaphore_obj =
859 i915_error_ggtt_object_create(dev_priv, 859 i915_error_ggtt_object_create(dev_priv,
860 dev_priv->semaphore_obj); 860 dev_priv->semaphore_obj);
861 861
862 for_each_ring(to, dev_priv, i) { 862 for_each_ring(to, dev_priv, i) {
863 int idx; 863 int idx;
864 u16 signal_offset; 864 u16 signal_offset;
865 u32 *tmp; 865 u32 *tmp;
866 866
867 if (ring == to) 867 if (ring == to)
868 continue; 868 continue;
869 869
870 signal_offset = (GEN8_SIGNAL_OFFSET(ring, i) & (PAGE_SIZE - 1)) 870 signal_offset = (GEN8_SIGNAL_OFFSET(ring, i) & (PAGE_SIZE - 1))
871 / 4; 871 / 4;
872 tmp = error->semaphore_obj->pages[0]; 872 tmp = error->semaphore_obj->pages[0];
873 idx = intel_ring_sync_index(ring, to); 873 idx = intel_ring_sync_index(ring, to);
874 874
875 ering->semaphore_mboxes[idx] = tmp[signal_offset]; 875 ering->semaphore_mboxes[idx] = tmp[signal_offset];
876 ering->semaphore_seqno[idx] = ring->semaphore.sync_seqno[idx]; 876 ering->semaphore_seqno[idx] = ring->semaphore.sync_seqno[idx];
877 } 877 }
878} 878}
879 879
880static void gen6_record_semaphore_state(struct drm_i915_private *dev_priv, 880static void gen6_record_semaphore_state(struct drm_i915_private *dev_priv,
881 struct intel_engine_cs *ring, 881 struct intel_engine_cs *ring,
882 struct drm_i915_error_ring *ering) 882 struct drm_i915_error_ring *ering)
883{ 883{
884 ering->semaphore_mboxes[0] = I915_READ(RING_SYNC_0(ring->mmio_base)); 884 ering->semaphore_mboxes[0] = I915_READ(RING_SYNC_0(ring->mmio_base));
885 ering->semaphore_mboxes[1] = I915_READ(RING_SYNC_1(ring->mmio_base)); 885 ering->semaphore_mboxes[1] = I915_READ(RING_SYNC_1(ring->mmio_base));
886 ering->semaphore_seqno[0] = ring->semaphore.sync_seqno[0]; 886 ering->semaphore_seqno[0] = ring->semaphore.sync_seqno[0];
887 ering->semaphore_seqno[1] = ring->semaphore.sync_seqno[1]; 887 ering->semaphore_seqno[1] = ring->semaphore.sync_seqno[1];
888 888
889 if (HAS_VEBOX(dev_priv->dev)) { 889 if (HAS_VEBOX(dev_priv->dev)) {
890 ering->semaphore_mboxes[2] = 890 ering->semaphore_mboxes[2] =
891 I915_READ(RING_SYNC_2(ring->mmio_base)); 891 I915_READ(RING_SYNC_2(ring->mmio_base));
892 ering->semaphore_seqno[2] = ring->semaphore.sync_seqno[2]; 892 ering->semaphore_seqno[2] = ring->semaphore.sync_seqno[2];
893 } 893 }
894} 894}
895 895
896static void i915_record_ring_state(struct drm_device *dev, 896static void i915_record_ring_state(struct drm_device *dev,
897 struct drm_i915_error_state *error, 897 struct drm_i915_error_state *error,
898 struct intel_engine_cs *ring, 898 struct intel_engine_cs *ring,
899 struct drm_i915_error_ring *ering) 899 struct drm_i915_error_ring *ering)
900{ 900{
901 struct drm_i915_private *dev_priv = dev->dev_private; 901 struct drm_i915_private *dev_priv = dev->dev_private;
902 902
903 if (INTEL_INFO(dev)->gen >= 6) { 903 if (INTEL_INFO(dev)->gen >= 6) {
904 ering->rc_psmi = I915_READ(ring->mmio_base + 0x50); 904 ering->rc_psmi = I915_READ(ring->mmio_base + 0x50);
905 ering->fault_reg = I915_READ(RING_FAULT_REG(ring)); 905 ering->fault_reg = I915_READ(RING_FAULT_REG(ring));
906 if (INTEL_INFO(dev)->gen >= 8) 906 if (INTEL_INFO(dev)->gen >= 8)
907 gen8_record_semaphore_state(dev_priv, error, ring, ering); 907 gen8_record_semaphore_state(dev_priv, error, ring, ering);
908 else 908 else
909 gen6_record_semaphore_state(dev_priv, ring, ering); 909 gen6_record_semaphore_state(dev_priv, ring, ering);
910 } 910 }
911 911
912 if (INTEL_INFO(dev)->gen >= 4) { 912 if (INTEL_INFO(dev)->gen >= 4) {
913 ering->faddr = I915_READ(RING_DMA_FADD(ring->mmio_base)); 913 ering->faddr = I915_READ(RING_DMA_FADD(ring->mmio_base));
914 ering->ipeir = I915_READ(RING_IPEIR(ring->mmio_base)); 914 ering->ipeir = I915_READ(RING_IPEIR(ring->mmio_base));
915 ering->ipehr = I915_READ(RING_IPEHR(ring->mmio_base)); 915 ering->ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
916 ering->instdone = I915_READ(RING_INSTDONE(ring->mmio_base)); 916 ering->instdone = I915_READ(RING_INSTDONE(ring->mmio_base));
917 ering->instps = I915_READ(RING_INSTPS(ring->mmio_base)); 917 ering->instps = I915_READ(RING_INSTPS(ring->mmio_base));
918 ering->bbaddr = I915_READ(RING_BBADDR(ring->mmio_base)); 918 ering->bbaddr = I915_READ(RING_BBADDR(ring->mmio_base));
919 if (INTEL_INFO(dev)->gen >= 8) { 919 if (INTEL_INFO(dev)->gen >= 8) {
920 ering->faddr |= (u64) I915_READ(RING_DMA_FADD_UDW(ring->mmio_base)) << 32; 920 ering->faddr |= (u64) I915_READ(RING_DMA_FADD_UDW(ring->mmio_base)) << 32;
921 ering->bbaddr |= (u64) I915_READ(RING_BBADDR_UDW(ring->mmio_base)) << 32; 921 ering->bbaddr |= (u64) I915_READ(RING_BBADDR_UDW(ring->mmio_base)) << 32;
922 } 922 }
923 ering->bbstate = I915_READ(RING_BBSTATE(ring->mmio_base)); 923 ering->bbstate = I915_READ(RING_BBSTATE(ring->mmio_base));
924 } else { 924 } else {
925 ering->faddr = I915_READ(DMA_FADD_I8XX); 925 ering->faddr = I915_READ(DMA_FADD_I8XX);
926 ering->ipeir = I915_READ(IPEIR); 926 ering->ipeir = I915_READ(IPEIR);
927 ering->ipehr = I915_READ(IPEHR); 927 ering->ipehr = I915_READ(IPEHR);
928 ering->instdone = I915_READ(GEN2_INSTDONE); 928 ering->instdone = I915_READ(GEN2_INSTDONE);
929 } 929 }
930 930
931#ifdef __NetBSD__ 931#ifdef __NetBSD__
932 ering->waiting = DRM_SPIN_WAITERS_P(&ring->irq_queue, 932 ering->waiting = DRM_SPIN_WAITERS_P(&ring->irq_queue,
933 &dev_priv->irq_lock); 933 &dev_priv->irq_lock);
934#else 934#else
935 ering->waiting = waitqueue_active(&ring->irq_queue); 935 ering->waiting = waitqueue_active(&ring->irq_queue);
936#endif 936#endif
937 ering->instpm = I915_READ(RING_INSTPM(ring->mmio_base)); 937 ering->instpm = I915_READ(RING_INSTPM(ring->mmio_base));
938 ering->seqno = ring->get_seqno(ring, false); 938 ering->seqno = ring->get_seqno(ring, false);
939 ering->acthd = intel_ring_get_active_head(ring); 939 ering->acthd = intel_ring_get_active_head(ring);
940 ering->start = I915_READ_START(ring); 940 ering->start = I915_READ_START(ring);
941 ering->head = I915_READ_HEAD(ring); 941 ering->head = I915_READ_HEAD(ring);
942 ering->tail = I915_READ_TAIL(ring); 942 ering->tail = I915_READ_TAIL(ring);
943 ering->ctl = I915_READ_CTL(ring); 943 ering->ctl = I915_READ_CTL(ring);
944 944
945 if (I915_NEED_GFX_HWS(dev)) { 945 if (I915_NEED_GFX_HWS(dev)) {
946 int mmio; 946 int mmio;
947 947
948 if (IS_GEN7(dev)) { 948 if (IS_GEN7(dev)) {
949 switch (ring->id) { 949 switch (ring->id) {
950 default: 950 default:
951 case RCS: 951 case RCS:
952 mmio = RENDER_HWS_PGA_GEN7; 952 mmio = RENDER_HWS_PGA_GEN7;
953 break; 953 break;
954 case BCS: 954 case BCS:
955 mmio = BLT_HWS_PGA_GEN7; 955 mmio = BLT_HWS_PGA_GEN7;
956 break; 956 break;
957 case VCS: 957 case VCS:
958 mmio = BSD_HWS_PGA_GEN7; 958 mmio = BSD_HWS_PGA_GEN7;
959 break; 959 break;
960 case VECS: 960 case VECS:
961 mmio = VEBOX_HWS_PGA_GEN7; 961 mmio = VEBOX_HWS_PGA_GEN7;
962 break; 962 break;
963 } 963 }
964 } else if (IS_GEN6(ring->dev)) { 964 } else if (IS_GEN6(ring->dev)) {
965 mmio = RING_HWS_PGA_GEN6(ring->mmio_base); 965 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
966 } else { 966 } else {
967 /* XXX: gen8 returns to sanity */ 967 /* XXX: gen8 returns to sanity */
968 mmio = RING_HWS_PGA(ring->mmio_base); 968 mmio = RING_HWS_PGA(ring->mmio_base);
969 } 969 }
970 970
971 ering->hws = I915_READ(mmio); 971 ering->hws = I915_READ(mmio);
972 } 972 }
973 973
974 ering->hangcheck_score = ring->hangcheck.score; 974 ering->hangcheck_score = ring->hangcheck.score;
975 ering->hangcheck_action = ring->hangcheck.action; 975 ering->hangcheck_action = ring->hangcheck.action;
976 976
977 if (USES_PPGTT(dev)) { 977 if (USES_PPGTT(dev)) {
978 int i; 978 int i;
979 979
980 ering->vm_info.gfx_mode = I915_READ(RING_MODE_GEN7(ring)); 980 ering->vm_info.gfx_mode = I915_READ(RING_MODE_GEN7(ring));
981 981
982 if (IS_GEN6(dev)) 982 if (IS_GEN6(dev))
983 ering->vm_info.pp_dir_base = 983 ering->vm_info.pp_dir_base =
984 I915_READ(RING_PP_DIR_BASE_READ(ring)); 984 I915_READ(RING_PP_DIR_BASE_READ(ring));
985 else if (IS_GEN7(dev)) 985 else if (IS_GEN7(dev))
986 ering->vm_info.pp_dir_base = 986 ering->vm_info.pp_dir_base =
987 I915_READ(RING_PP_DIR_BASE(ring)); 987 I915_READ(RING_PP_DIR_BASE(ring));
988 else if (INTEL_INFO(dev)->gen >= 8) 988 else if (INTEL_INFO(dev)->gen >= 8)
989 for (i = 0; i < 4; i++) { 989 for (i = 0; i < 4; i++) {
990 ering->vm_info.pdp[i] = 990 ering->vm_info.pdp[i] =
991 I915_READ(GEN8_RING_PDP_UDW(ring, i)); 991 I915_READ(GEN8_RING_PDP_UDW(ring, i));
992 ering->vm_info.pdp[i] <<= 32; 992 ering->vm_info.pdp[i] <<= 32;
993 ering->vm_info.pdp[i] |= 993 ering->vm_info.pdp[i] |=
994 I915_READ(GEN8_RING_PDP_LDW(ring, i)); 994 I915_READ(GEN8_RING_PDP_LDW(ring, i));
995 } 995 }
996 } 996 }
997} 997}
998 998
999 999
1000static void i915_gem_record_active_context(struct intel_engine_cs *ring, 1000static void i915_gem_record_active_context(struct intel_engine_cs *ring,
1001 struct drm_i915_error_state *error, 1001 struct drm_i915_error_state *error,
1002 struct drm_i915_error_ring *ering) 1002 struct drm_i915_error_ring *ering)
1003{ 1003{
1004 struct drm_i915_private *dev_priv = ring->dev->dev_private; 1004 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1005 struct drm_i915_gem_object *obj; 1005 struct drm_i915_gem_object *obj;
1006 1006
1007 /* Currently render ring is the only HW context user */ 1007 /* Currently render ring is the only HW context user */
1008 if (ring->id != RCS || !error->ccid) 1008 if (ring->id != RCS || !error->ccid)
1009 return; 1009 return;
1010 1010
1011 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { 1011 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1012 if (!i915_gem_obj_ggtt_bound(obj)) 1012 if (!i915_gem_obj_ggtt_bound(obj))
1013 continue; 1013 continue;
1014 1014
1015 if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) { 1015 if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
1016 ering->ctx = i915_error_ggtt_object_create(dev_priv, obj); 1016 ering->ctx = i915_error_ggtt_object_create(dev_priv, obj);
1017 break; 1017 break;
1018 } 1018 }
1019 } 1019 }
1020} 1020}
1021 1021
1022static void i915_gem_record_rings(struct drm_device *dev, 1022static void i915_gem_record_rings(struct drm_device *dev,
1023 struct drm_i915_error_state *error) 1023 struct drm_i915_error_state *error)
1024{ 1024{
1025 struct drm_i915_private *dev_priv = dev->dev_private; 1025 struct drm_i915_private *dev_priv = dev->dev_private;
1026 struct drm_i915_gem_request *request; 1026 struct drm_i915_gem_request *request;
1027 int i, count; 1027 int i, count;
1028 1028
1029 for (i = 0; i < I915_NUM_RINGS; i++) { 1029 for (i = 0; i < I915_NUM_RINGS; i++) {
1030 struct intel_engine_cs *ring = &dev_priv->ring[i]; 1030 struct intel_engine_cs *ring = &dev_priv->ring[i];
1031 struct intel_ringbuffer *rbuf; 1031 struct intel_ringbuffer *rbuf;
1032 1032
1033 error->ring[i].pid = -1; 1033 error->ring[i].pid = -1;
1034 1034
1035 if (ring->dev == NULL) 1035 if (ring->dev == NULL)
1036 continue; 1036 continue;
1037 1037
1038 error->ring[i].valid = true; 1038 error->ring[i].valid = true;
1039 1039
1040 i915_record_ring_state(dev, error, ring, &error->ring[i]); 1040 i915_record_ring_state(dev, error, ring, &error->ring[i]);
1041 1041
1042 request = i915_gem_find_active_request(ring); 1042 request = i915_gem_find_active_request(ring);
1043 if (request) { 1043 if (request) {
1044 struct i915_address_space *vm; 1044 struct i915_address_space *vm;
1045 1045
1046 vm = request->ctx && request->ctx->ppgtt ? 1046 vm = request->ctx && request->ctx->ppgtt ?
1047 &request->ctx->ppgtt->base : 1047 &request->ctx->ppgtt->base :
1048 &dev_priv->gtt.base; 1048 &dev_priv->gtt.base;
1049 1049
1050 /* We need to copy these to an anonymous buffer 1050 /* We need to copy these to an anonymous buffer
1051 * as the simplest method to avoid being overwritten 1051 * as the simplest method to avoid being overwritten
1052 * by userspace. 1052 * by userspace.
1053 */ 1053 */
1054 error->ring[i].batchbuffer = 1054 error->ring[i].batchbuffer =
1055 i915_error_object_create(dev_priv, 1055 i915_error_object_create(dev_priv,
1056 request->batch_obj, 1056 request->batch_obj,
1057 vm); 1057 vm);
1058 1058
1059 if (HAS_BROKEN_CS_TLB(dev_priv->dev)) 1059 if (HAS_BROKEN_CS_TLB(dev_priv->dev))
1060 error->ring[i].wa_batchbuffer = 1060 error->ring[i].wa_batchbuffer =
1061 i915_error_ggtt_object_create(dev_priv, 1061 i915_error_ggtt_object_create(dev_priv,
1062 ring->scratch.obj); 1062 ring->scratch.obj);
1063 1063
1064#ifndef __NetBSD__ /* XXX not a clue */ 1064#ifndef __NetBSD__ /* XXX not a clue */
1065 if (request->pid) { 1065 if (request->pid) {
1066 struct task_struct *task; 1066 struct task_struct *task;
1067 1067
1068 rcu_read_lock(); 1068 rcu_read_lock();
1069 task = pid_task(request->pid, PIDTYPE_PID); 1069 task = pid_task(request->pid, PIDTYPE_PID);
1070 if (task) { 1070 if (task) {
1071 strcpy(error->ring[i].comm, task->comm); 1071 strcpy(error->ring[i].comm, task->comm);
1072 error->ring[i].pid = task->pid; 1072 error->ring[i].pid = task->pid;
1073 } 1073 }
1074 rcu_read_unlock(); 1074 rcu_read_unlock();
1075 } 1075 }
1076#endif 1076#endif
1077 } 1077 }
1078 1078
1079 if (i915.enable_execlists) { 1079 if (i915.enable_execlists) {
1080 /* TODO: This is only a small fix to keep basic error 1080 /* TODO: This is only a small fix to keep basic error
1081 * capture working, but we need to add more information 1081 * capture working, but we need to add more information
1082 * for it to be useful (e.g. dump the context being 1082 * for it to be useful (e.g. dump the context being
1083 * executed). 1083 * executed).
1084 */ 1084 */
1085 if (request) 1085 if (request)
1086 rbuf = request->ctx->engine[ring->id].ringbuf; 1086 rbuf = request->ctx->engine[ring->id].ringbuf;
1087 else 1087 else
1088 rbuf = ring->default_context->engine[ring->id].ringbuf; 1088 rbuf = ring->default_context->engine[ring->id].ringbuf;
1089 } else 1089 } else
1090 rbuf = ring->buffer; 1090 rbuf = ring->buffer;
1091 1091
1092 error->ring[i].cpu_ring_head = rbuf->head; 1092 error->ring[i].cpu_ring_head = rbuf->head;
1093 error->ring[i].cpu_ring_tail = rbuf->tail; 1093 error->ring[i].cpu_ring_tail = rbuf->tail;
1094 1094
1095 error->ring[i].ringbuffer = 1095 error->ring[i].ringbuffer =
1096 i915_error_ggtt_object_create(dev_priv, rbuf->obj); 1096 i915_error_ggtt_object_create(dev_priv, rbuf->obj);
1097 1097
1098 error->ring[i].hws_page = 1098 error->ring[i].hws_page =
1099 i915_error_ggtt_object_create(dev_priv, ring->status_page.obj); 1099 i915_error_ggtt_object_create(dev_priv, ring->status_page.obj);
1100 1100
1101 i915_gem_record_active_context(ring, error, &error->ring[i]); 1101 i915_gem_record_active_context(ring, error, &error->ring[i]);
1102 1102
1103 count = 0; 1103 count = 0;
1104 list_for_each_entry(request, &ring->request_list, list) 1104 list_for_each_entry(request, &ring->request_list, list)
1105 count++; 1105 count++;
1106 1106
1107 error->ring[i].num_requests = count; 1107 error->ring[i].num_requests = count;
1108 error->ring[i].requests = 1108 error->ring[i].requests =
1109 kcalloc(count, sizeof(*error->ring[i].requests), 1109 kcalloc(count, sizeof(*error->ring[i].requests),
1110 GFP_ATOMIC); 1110 GFP_ATOMIC);
1111 if (error->ring[i].requests == NULL) { 1111 if (error->ring[i].requests == NULL) {
1112 error->ring[i].num_requests = 0; 1112 error->ring[i].num_requests = 0;
1113 continue; 1113 continue;
1114 } 1114 }
1115 1115
1116 count = 0; 1116 count = 0;
1117 list_for_each_entry(request, &ring->request_list, list) { 1117 list_for_each_entry(request, &ring->request_list, list) {
1118 struct drm_i915_error_request *erq; 1118 struct drm_i915_error_request *erq;
1119 1119
1120 erq = &error->ring[i].requests[count++]; 1120 erq = &error->ring[i].requests[count++];
1121 erq->seqno = request->seqno; 1121 erq->seqno = request->seqno;
1122 erq->jiffies = request->emitted_jiffies; 1122 erq->jiffies = request->emitted_jiffies;
1123 erq->tail = request->postfix; 1123 erq->tail = request->postfix;
1124 } 1124 }
1125 } 1125 }
1126} 1126}
1127 1127
1128/* FIXME: Since pin count/bound list is global, we duplicate what we capture per 1128/* FIXME: Since pin count/bound list is global, we duplicate what we capture per
1129 * VM. 1129 * VM.
1130 */ 1130 */
1131static void i915_gem_capture_vm(struct drm_i915_private *dev_priv, 1131static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
1132 struct drm_i915_error_state *error, 1132 struct drm_i915_error_state *error,
1133 struct i915_address_space *vm, 1133 struct i915_address_space *vm,
1134 const int ndx) 1134 const int ndx)
1135{ 1135{
1136 struct drm_i915_error_buffer *active_bo = NULL, *pinned_bo = NULL; 1136 struct drm_i915_error_buffer *active_bo = NULL, *pinned_bo = NULL;
1137 struct drm_i915_gem_object *obj; 1137 struct drm_i915_gem_object *obj;
1138 struct i915_vma *vma; 1138 struct i915_vma *vma;
1139 int i; 1139 int i;
1140 1140
1141 i = 0; 1141 i = 0;
1142 list_for_each_entry(vma, &vm->active_list, mm_list) 1142 list_for_each_entry(vma, &vm->active_list, mm_list)
1143 i++; 1143 i++;
1144 error->active_bo_count[ndx] = i; 1144 error->active_bo_count[ndx] = i;
1145 1145
1146 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { 1146 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1147 list_for_each_entry(vma, &obj->vma_list, vma_link) 1147 list_for_each_entry(vma, &obj->vma_list, vma_link)
1148 if (vma->vm == vm && vma->pin_count > 0) 1148 if (vma->vm == vm && vma->pin_count > 0)
1149 i++; 1149 i++;
1150 } 1150 }
1151 error->pinned_bo_count[ndx] = i - error->active_bo_count[ndx]; 1151 error->pinned_bo_count[ndx] = i - error->active_bo_count[ndx];
1152 1152
1153 if (i) { 1153 if (i) {
1154 active_bo = kcalloc(i, sizeof(*active_bo), GFP_ATOMIC); 1154 active_bo = kcalloc(i, sizeof(*active_bo), GFP_ATOMIC);
1155 if (active_bo) 1155 if (active_bo)
1156 pinned_bo = active_bo + error->active_bo_count[ndx]; 1156 pinned_bo = active_bo + error->active_bo_count[ndx];
1157 } 1157 }
1158 1158
1159 if (active_bo) 1159 if (active_bo)
1160 error->active_bo_count[ndx] = 1160 error->active_bo_count[ndx] =
1161 capture_active_bo(active_bo, 1161 capture_active_bo(active_bo,
1162 error->active_bo_count[ndx], 1162 error->active_bo_count[ndx],
1163 &vm->active_list); 1163 &vm->active_list);
1164 1164
1165 if (pinned_bo) 1165 if (pinned_bo)
1166 error->pinned_bo_count[ndx] = 1166 error->pinned_bo_count[ndx] =
1167 capture_pinned_bo(pinned_bo, 1167 capture_pinned_bo(pinned_bo,
1168 error->pinned_bo_count[ndx], 1168 error->pinned_bo_count[ndx],
1169 &dev_priv->mm.bound_list, vm); 1169 &dev_priv->mm.bound_list, vm);
1170 error->active_bo[ndx] = active_bo; 1170 error->active_bo[ndx] = active_bo;
1171 error->pinned_bo[ndx] = pinned_bo; 1171 error->pinned_bo[ndx] = pinned_bo;
1172} 1172}
1173 1173
1174static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv, 1174static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
1175 struct drm_i915_error_state *error) 1175 struct drm_i915_error_state *error)
1176{ 1176{
1177 struct i915_address_space *vm; 1177 struct i915_address_space *vm;
1178 int cnt = 0, i = 0; 1178 int cnt = 0, i = 0;
1179 1179
1180 list_for_each_entry(vm, &dev_priv->vm_list, global_link) 1180 list_for_each_entry(vm, &dev_priv->vm_list, global_link)
1181 cnt++; 1181 cnt++;
1182 1182
1183 error->active_bo = kcalloc(cnt, sizeof(*error->active_bo), GFP_ATOMIC); 1183 error->active_bo = kcalloc(cnt, sizeof(*error->active_bo), GFP_ATOMIC);
1184 error->pinned_bo = kcalloc(cnt, sizeof(*error->pinned_bo), GFP_ATOMIC); 1184 error->pinned_bo = kcalloc(cnt, sizeof(*error->pinned_bo), GFP_ATOMIC);
1185 error->active_bo_count = kcalloc(cnt, sizeof(*error->active_bo_count), 1185 error->active_bo_count = kcalloc(cnt, sizeof(*error->active_bo_count),
1186 GFP_ATOMIC); 1186 GFP_ATOMIC);
1187 error->pinned_bo_count = kcalloc(cnt, sizeof(*error->pinned_bo_count), 1187 error->pinned_bo_count = kcalloc(cnt, sizeof(*error->pinned_bo_count),
1188 GFP_ATOMIC); 1188 GFP_ATOMIC);
1189 1189
1190 if (error->active_bo == NULL || 1190 if (error->active_bo == NULL ||
1191 error->pinned_bo == NULL || 1191 error->pinned_bo == NULL ||
1192 error->active_bo_count == NULL || 1192 error->active_bo_count == NULL ||
1193 error->pinned_bo_count == NULL) { 1193 error->pinned_bo_count == NULL) {
1194 kfree(error->active_bo); 1194 kfree(error->active_bo);
1195 kfree(error->active_bo_count); 1195 kfree(error->active_bo_count);
1196 kfree(error->pinned_bo); 1196 kfree(error->pinned_bo);
1197 kfree(error->pinned_bo_count); 1197 kfree(error->pinned_bo_count);
1198 1198
1199 error->active_bo = NULL; 1199 error->active_bo = NULL;
1200 error->active_bo_count = NULL; 1200 error->active_bo_count = NULL;
1201 error->pinned_bo = NULL; 1201 error->pinned_bo = NULL;
1202 error->pinned_bo_count = NULL; 1202 error->pinned_bo_count = NULL;
1203 } else { 1203 } else {
1204 list_for_each_entry(vm, &dev_priv->vm_list, global_link) 1204 list_for_each_entry(vm, &dev_priv->vm_list, global_link)
1205 i915_gem_capture_vm(dev_priv, error, vm, i++); 1205 i915_gem_capture_vm(dev_priv, error, vm, i++);
1206 1206
1207 error->vm_count = cnt; 1207 error->vm_count = cnt;
1208 } 1208 }
1209} 1209}
1210 1210
1211/* Capture all registers which don't fit into another category. */ 1211/* Capture all registers which don't fit into another category. */
1212static void i915_capture_reg_state(struct drm_i915_private *dev_priv, 1212static void i915_capture_reg_state(struct drm_i915_private *dev_priv,
1213 struct drm_i915_error_state *error) 1213 struct drm_i915_error_state *error)
1214{ 1214{
1215 struct drm_device *dev = dev_priv->dev; 1215 struct drm_device *dev = dev_priv->dev;
1216 int i; 1216 int i;
1217 1217
1218 /* General organization 1218 /* General organization
1219 * 1. Registers specific to a single generation 1219 * 1. Registers specific to a single generation
1220 * 2. Registers which belong to multiple generations 1220 * 2. Registers which belong to multiple generations
1221 * 3. Feature specific registers. 1221 * 3. Feature specific registers.
1222 * 4. Everything else 1222 * 4. Everything else
1223 * Please try to follow the order. 1223 * Please try to follow the order.
1224 */ 1224 */
1225 1225
1226 /* 1: Registers specific to a single generation */ 1226 /* 1: Registers specific to a single generation */
1227 if (IS_VALLEYVIEW(dev)) { 1227 if (IS_VALLEYVIEW(dev)) {
1228 error->gtier[0] = I915_READ(GTIER); 1228 error->gtier[0] = I915_READ(GTIER);
1229 error->ier = I915_READ(VLV_IER); 1229 error->ier = I915_READ(VLV_IER);
1230 error->forcewake = I915_READ(FORCEWAKE_VLV); 1230 error->forcewake = I915_READ(FORCEWAKE_VLV);
1231 } 1231 }
1232 1232
1233 if (IS_GEN7(dev)) 1233 if (IS_GEN7(dev))
1234 error->err_int = I915_READ(GEN7_ERR_INT); 1234 error->err_int = I915_READ(GEN7_ERR_INT);
1235 1235
1236 if (INTEL_INFO(dev)->gen >= 8) { 1236 if (INTEL_INFO(dev)->gen >= 8) {
1237 error->fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0); 1237 error->fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
1238 error->fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1); 1238 error->fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
1239 } 1239 }
1240 1240
1241 if (IS_GEN6(dev)) { 1241 if (IS_GEN6(dev)) {
1242 error->forcewake = I915_READ(FORCEWAKE); 1242 error->forcewake = I915_READ(FORCEWAKE);
1243 error->gab_ctl = I915_READ(GAB_CTL); 1243 error->gab_ctl = I915_READ(GAB_CTL);
1244 error->gfx_mode = I915_READ(GFX_MODE); 1244 error->gfx_mode = I915_READ(GFX_MODE);
1245 } 1245 }
1246 1246
1247 /* 2: Registers which belong to multiple generations */ 1247 /* 2: Registers which belong to multiple generations */
1248 if (INTEL_INFO(dev)->gen >= 7) 1248 if (INTEL_INFO(dev)->gen >= 7)
1249 error->forcewake = I915_READ(FORCEWAKE_MT); 1249 error->forcewake = I915_READ(FORCEWAKE_MT);
1250 1250
1251 if (INTEL_INFO(dev)->gen >= 6) { 1251 if (INTEL_INFO(dev)->gen >= 6) {
1252 error->derrmr = I915_READ(DERRMR); 1252 error->derrmr = I915_READ(DERRMR);
1253 error->error = I915_READ(ERROR_GEN6); 1253 error->error = I915_READ(ERROR_GEN6);
1254 error->done_reg = I915_READ(DONE_REG); 1254 error->done_reg = I915_READ(DONE_REG);
1255 } 1255 }
1256 1256
1257 /* 3: Feature specific registers */ 1257 /* 3: Feature specific registers */
1258 if (IS_GEN6(dev) || IS_GEN7(dev)) { 1258 if (IS_GEN6(dev) || IS_GEN7(dev)) {
1259 error->gam_ecochk = I915_READ(GAM_ECOCHK); 1259 error->gam_ecochk = I915_READ(GAM_ECOCHK);
1260 error->gac_eco = I915_READ(GAC_ECO_BITS); 1260 error->gac_eco = I915_READ(GAC_ECO_BITS);
1261 } 1261 }
1262 1262
1263 /* 4: Everything else */ 1263 /* 4: Everything else */
1264 if (HAS_HW_CONTEXTS(dev)) 1264 if (HAS_HW_CONTEXTS(dev))
1265 error->ccid = I915_READ(CCID); 1265 error->ccid = I915_READ(CCID);
1266 1266
1267 if (INTEL_INFO(dev)->gen >= 8) { 1267 if (INTEL_INFO(dev)->gen >= 8) {
1268 error->ier = I915_READ(GEN8_DE_MISC_IER); 1268 error->ier = I915_READ(GEN8_DE_MISC_IER);
1269 for (i = 0; i < 4; i++) 1269 for (i = 0; i < 4; i++)
1270 error->gtier[i] = I915_READ(GEN8_GT_IER(i)); 1270 error->gtier[i] = I915_READ(GEN8_GT_IER(i));
1271 } else if (HAS_PCH_SPLIT(dev)) { 1271 } else if (HAS_PCH_SPLIT(dev)) {
1272 error->ier = I915_READ(DEIER); 1272 error->ier = I915_READ(DEIER);
1273 error->gtier[0] = I915_READ(GTIER); 1273 error->gtier[0] = I915_READ(GTIER);
1274 } else if (IS_GEN2(dev)) { 1274 } else if (IS_GEN2(dev)) {
1275 error->ier = I915_READ16(IER); 1275 error->ier = I915_READ16(IER);
1276 } else if (!IS_VALLEYVIEW(dev)) { 1276 } else if (!IS_VALLEYVIEW(dev)) {
1277 error->ier = I915_READ(IER); 1277 error->ier = I915_READ(IER);
1278 } 1278 }
1279 error->eir = I915_READ(EIR); 1279 error->eir = I915_READ(EIR);
1280 error->pgtbl_er = I915_READ(PGTBL_ER); 1280 error->pgtbl_er = I915_READ(PGTBL_ER);
1281 1281
1282 i915_get_extra_instdone(dev, error->extra_instdone); 1282 i915_get_extra_instdone(dev, error->extra_instdone);
1283} 1283}
1284 1284
1285static void i915_error_capture_msg(struct drm_device *dev, 1285static void i915_error_capture_msg(struct drm_device *dev,
1286 struct drm_i915_error_state *error, 1286 struct drm_i915_error_state *error,
1287 bool wedged, 1287 bool wedged,
1288 const char *error_msg) 1288 const char *error_msg)
1289{ 1289{
1290 struct drm_i915_private *dev_priv = dev->dev_private; 1290 struct drm_i915_private *dev_priv = dev->dev_private;
1291 u32 ecode; 1291 u32 ecode;
1292 int ring_id = -1, len; 1292 int ring_id = -1, len;
1293 1293
1294 ecode = i915_error_generate_code(dev_priv, error, &ring_id); 1294 ecode = i915_error_generate_code(dev_priv, error, &ring_id);
1295 1295
1296 len = scnprintf(error->error_msg, sizeof(error->error_msg), 1296 len = scnprintf(error->error_msg, sizeof(error->error_msg),
1297 "GPU HANG: ecode %d:%d:0x%08x", 1297 "GPU HANG: ecode %d:%d:0x%08x",
1298 INTEL_INFO(dev)->gen, ring_id, ecode); 1298 INTEL_INFO(dev)->gen, ring_id, ecode);
1299 1299
1300 if (ring_id != -1 && error->ring[ring_id].pid != -1) 1300 if (ring_id != -1 && error->ring[ring_id].pid != -1)
1301 len += scnprintf(error->error_msg + len, 1301 len += scnprintf(error->error_msg + len,
1302 sizeof(error->error_msg) - len, 1302 sizeof(error->error_msg) - len,
1303 ", in %s [%d]", 1303 ", in %s [%d]",
1304 error->ring[ring_id].comm, 1304 error->ring[ring_id].comm,
1305 error->ring[ring_id].pid); 1305 error->ring[ring_id].pid);
1306 1306
1307 scnprintf(error->error_msg + len, sizeof(error->error_msg) - len, 1307 scnprintf(error->error_msg + len, sizeof(error->error_msg) - len,
1308 ", reason: %s, action: %s", 1308 ", reason: %s, action: %s",
1309 error_msg, 1309 error_msg,
1310 wedged ? "reset" : "continue"); 1310 wedged ? "reset" : "continue");
1311} 1311}
1312 1312
1313static void i915_capture_gen_state(struct drm_i915_private *dev_priv, 1313static void i915_capture_gen_state(struct drm_i915_private *dev_priv,
1314 struct drm_i915_error_state *error) 1314 struct drm_i915_error_state *error)
1315{ 1315{
1316 error->iommu = -1; 1316 error->iommu = -1;
1317#ifdef CONFIG_INTEL_IOMMU 1317#ifdef CONFIG_INTEL_IOMMU
1318 error->iommu = intel_iommu_gfx_mapped; 1318 error->iommu = intel_iommu_gfx_mapped;
1319#endif 1319#endif
1320 error->reset_count = i915_reset_count(&dev_priv->gpu_error); 1320 error->reset_count = i915_reset_count(&dev_priv->gpu_error);
1321 error->suspend_count = dev_priv->suspend_count; 1321 error->suspend_count = dev_priv->suspend_count;
1322} 1322}
1323 1323
1324/** 1324/**
1325 * i915_capture_error_state - capture an error record for later analysis 1325 * i915_capture_error_state - capture an error record for later analysis
1326 * @dev: drm device 1326 * @dev: drm device
1327 * 1327 *
1328 * Should be called when an error is detected (either a hang or an error 1328 * Should be called when an error is detected (either a hang or an error
1329 * interrupt) to capture error state from the time of the error. Fills 1329 * interrupt) to capture error state from the time of the error. Fills
1330 * out a structure which becomes available in debugfs for user level tools 1330 * out a structure which becomes available in debugfs for user level tools
1331 * to pick up. 1331 * to pick up.
1332 */ 1332 */
1333void i915_capture_error_state(struct drm_device *dev, bool wedged, 1333void i915_capture_error_state(struct drm_device *dev, bool wedged,
1334 const char *error_msg) 1334 const char *error_msg)
1335{ 1335{
1336 static bool warned; 1336 static bool warned;
1337 struct drm_i915_private *dev_priv = dev->dev_private; 1337 struct drm_i915_private *dev_priv = dev->dev_private;
1338 struct drm_i915_error_state *error; 1338 struct drm_i915_error_state *error;
1339 unsigned long flags; 1339 unsigned long flags;
1340 1340
1341 /* Account for pipe specific data like PIPE*STAT */ 1341 /* Account for pipe specific data like PIPE*STAT */
1342 error = kzalloc(sizeof(*error), GFP_ATOMIC); 1342 error = kzalloc(sizeof(*error), GFP_ATOMIC);
1343 if (!error) { 1343 if (!error) {
1344 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n"); 1344 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1345 return; 1345 return;
1346 } 1346 }
1347 1347
1348 kref_init(&error->ref); 1348 kref_init(&error->ref);
1349 1349
1350 i915_capture_gen_state(dev_priv, error); 1350 i915_capture_gen_state(dev_priv, error);
1351 i915_capture_reg_state(dev_priv, error); 1351 i915_capture_reg_state(dev_priv, error);
1352 i915_gem_capture_buffers(dev_priv, error); 1352 i915_gem_capture_buffers(dev_priv, error);
1353 i915_gem_record_fences(dev, error); 1353 i915_gem_record_fences(dev, error);
1354 i915_gem_record_rings(dev, error); 1354 i915_gem_record_rings(dev, error);
1355 1355
1356 do_gettimeofday(&error->time); 1356 do_gettimeofday(&error->time);
1357 1357

cvs diff -r1.2 -r1.3 src/sys/external/bsd/drm2/include/linux/hashtable.h (switch to unified diff)

--- src/sys/external/bsd/drm2/include/linux/hashtable.h 2018/08/27 06:08:09 1.2
+++ src/sys/external/bsd/drm2/include/linux/hashtable.h 2018/08/27 06:08:25 1.3
@@ -1,65 +1,61 @@ @@ -1,65 +1,61 @@
1/* $OpenBSD: drm_linux.h,v 1.89 2018/06/25 22:29:16 kettenis Exp $ */ 1/* $OpenBSD$ */
2/* 2/*
3 * Copyright (c) 2013, 2014, 2015 Mark Kettenis 3 * Copyright (c) 2013, 2014, 2015 Mark Kettenis
4 * Copyright (c) 2017 Martin Pieuchot 4 * Copyright (c) 2017 Martin Pieuchot
5 * 5 *
6 * Permission to use, copy, modify, and distribute this software for any 6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies. 8 * copyright notice and this permission notice appear in all copies.
9 * 9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19#ifndef _LINUX_HASHTABLE_H_ 19#ifndef _LINUX_HASHTABLE_H_
20#define _LINUX_HASHTABLE_H_ 20#define _LINUX_HASHTABLE_H_
21 21
22#include <linux/list.h> 22#include <linux/list.h>
23 23
24#define DECLARE_HASHTABLE(name, bits) struct list_head name[1 << (bits)] 24#define DECLARE_HASHTABLE(name, bits) struct list_head name[1 << (bits)]
25 25
26static inline void 26static inline void
27__hash_init(struct list_head *table, u_int size) 27__hash_init(struct list_head *table, u_int size)
28{ 28{
29 u_int i; 29 u_int i;
30 30
31 for (i = 0; i < size; i++) 31 for (i = 0; i < size; i++)
32 INIT_LIST_HEAD(&table[i]); 32 INIT_LIST_HEAD(&table[i]);
33} 33}
34 34
35static inline bool 35static inline bool
36__hash_empty(struct list_head *table, u_int size) 36__hash_empty(struct list_head *table, u_int size)
37{ 37{
38 u_int i; 38 u_int i;
39 39
40 for (i = 0; i < size; i++) { 40 for (i = 0; i < size; i++) {
41 if (!list_empty(&table[i])) 41 if (!list_empty(&table[i]))
42 return false; 42 return false;
43 } 43 }
44 44
45 return true; 45 return true;
46} 46}
47 47
48#define __hash(table, key) &table[key % (nitems(table) - 1)] 48#define __hash(table, key) &table[key % (nitems(table) - 1)]
49 49
50#define hash_init(table) __hash_init(table, nitems(table)) 50#define hash_init(table) __hash_init(table, nitems(table))
51#define hash_add(table, node, key) \ 51#define hash_add(table, node, key) \
52 hlist_add_head(node, __hash(table, key)) 52 hlist_add_head(node, __hash(table, key))
53#define hash_del(node) hlist_del_init(node) 53#define hash_del(node) hlist_del_init(node)
54#define hash_empty(table) __hash_empty(table, nitems(table)) 54#define hash_empty(table) __hash_empty(table, nitems(table))
55#define hash_for_each_possible(table, obj, member, key) \ 55#define hash_for_each_possible(table, obj, member, key) \
56 hlist_for_each_entry(obj, __hash(table, key), member) 56 hlist_for_each_entry(obj, __hash(table, key), member)
57#define hash_for_each_safe(table, i, tmp, obj, member) \ 57#define hash_for_each_safe(table, i, tmp, obj, member) \
58 for (i = 0; i < nitems(table); i++) \ 58 for (i = 0; i < nitems(table); i++) \
59 list_for_each_entry_safe(obj, tmp, &table[i], member) 59 list_for_each_entry_safe(obj, tmp, &table[i], member)
60 60
61#define DECLARE_HASHTABLE(__a,__b) \ 
62 uint64_t __a /* XXX */ 
63 
64 
65#endif /*_LINUX_HASHTABLE_H_*/ 61#endif /*_LINUX_HASHTABLE_H_*/