anv: add new gem/drm helpers
[mesa.git] / src / intel / isl / isl_priv.h
1 /*
2 * Copyright 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef ISL_PRIV_H
25 #define ISL_PRIV_H
26
27 #include <assert.h>
28 #include <stddef.h>
29 #include <strings.h>
30
31 #include "dev/gen_device_info.h"
32 #include "util/macros.h"
33
34 #include "isl.h"
35
36 #define isl_finishme(format, ...) \
37 do { \
38 static bool reported = false; \
39 if (!reported) { \
40 __isl_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
41 reported = true; \
42 } \
43 } while (0)
44
45 void PRINTFLIKE(3, 4) UNUSED
46 __isl_finishme(const char *file, int line, const char *fmt, ...);
47
48 #define MIN(a, b) ((a) < (b) ? (a) : (b))
49 #define MAX(a, b) ((a) > (b) ? (a) : (b))
50
51 typedef void *(*isl_mem_copy_fn)(void *dest, const void *src, size_t n);
52
53 static inline bool
54 isl_is_pow2(uintmax_t n)
55 {
56 return !(n & (n - 1));
57 }
58
59 /**
60 * Alignment must be a power of 2.
61 */
62 static inline bool
63 isl_is_aligned(uintmax_t n, uintmax_t a)
64 {
65 assert(isl_is_pow2(a));
66 return (n & (a - 1)) == 0;
67 }
68
69 /**
70 * Alignment must be a power of 2.
71 */
72 static inline uintmax_t
73 isl_align(uintmax_t n, uintmax_t a)
74 {
75 assert(a != 0 && isl_is_pow2(a));
76 return (n + a - 1) & ~(a - 1);
77 }
78
79 static inline uintmax_t
80 isl_align_npot(uintmax_t n, uintmax_t a)
81 {
82 assert(a > 0);
83 return ((n + a - 1) / a) * a;
84 }
85
86 static inline uintmax_t
87 isl_assert_div(uintmax_t n, uintmax_t a)
88 {
89 assert(n % a == 0);
90 return n / a;
91 }
92
93 /**
94 * Alignment must be a power of 2.
95 */
96 static inline uintmax_t
97 isl_align_div(uintmax_t n, uintmax_t a)
98 {
99 return isl_align(n, a) / a;
100 }
101
102 static inline uintmax_t
103 isl_align_div_npot(uintmax_t n, uintmax_t a)
104 {
105 return isl_align_npot(n, a) / a;
106 }
107
108 /**
109 * Log base 2, rounding towards zero.
110 */
111 static inline uint32_t
112 isl_log2u(uint32_t n)
113 {
114 assert(n != 0);
115 return 31 - __builtin_clz(n);
116 }
117
118 static inline uint32_t
119 isl_round_up_to_power_of_two(uint32_t value)
120 {
121 if (value <= 1)
122 return value;
123
124 return 1 << (32 - __builtin_clz(value - 1));
125 }
126
127 static inline uint32_t
128 isl_minify(uint32_t n, uint32_t levels)
129 {
130 if (unlikely(n == 0))
131 return 0;
132 else
133 return MAX(n >> levels, 1);
134 }
135
136 static inline struct isl_extent3d
137 isl_extent3d_sa_to_el(enum isl_format fmt, struct isl_extent3d extent_sa)
138 {
139 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
140
141 assert(extent_sa.w % fmtl->bw == 0);
142 assert(extent_sa.h % fmtl->bh == 0);
143 assert(extent_sa.d % fmtl->bd == 0);
144
145 return (struct isl_extent3d) {
146 .w = extent_sa.w / fmtl->bw,
147 .h = extent_sa.h / fmtl->bh,
148 .d = extent_sa.d / fmtl->bd,
149 };
150 }
151
152 static inline struct isl_extent3d
153 isl_extent3d_el_to_sa(enum isl_format fmt, struct isl_extent3d extent_el)
154 {
155 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
156
157 return (struct isl_extent3d) {
158 .w = extent_el.w * fmtl->bw,
159 .h = extent_el.h * fmtl->bh,
160 .d = extent_el.d * fmtl->bd,
161 };
162 }
163
164 void
165 _isl_memcpy_linear_to_tiled(uint32_t xt1, uint32_t xt2,
166 uint32_t yt1, uint32_t yt2,
167 char *dst, const char *src,
168 uint32_t dst_pitch, int32_t src_pitch,
169 bool has_swizzling,
170 enum isl_tiling tiling,
171 isl_memcpy_type copy_type);
172
173 void
174 _isl_memcpy_tiled_to_linear(uint32_t xt1, uint32_t xt2,
175 uint32_t yt1, uint32_t yt2,
176 char *dst, const char *src,
177 int32_t dst_pitch, uint32_t src_pitch,
178 bool has_swizzling,
179 enum isl_tiling tiling,
180 isl_memcpy_type copy_type);
181
182 void
183 _isl_memcpy_linear_to_tiled_sse41(uint32_t xt1, uint32_t xt2,
184 uint32_t yt1, uint32_t yt2,
185 char *dst, const char *src,
186 uint32_t dst_pitch, int32_t src_pitch,
187 bool has_swizzling,
188 enum isl_tiling tiling,
189 isl_memcpy_type copy_type);
190
191 void
192 _isl_memcpy_tiled_to_linear_sse41(uint32_t xt1, uint32_t xt2,
193 uint32_t yt1, uint32_t yt2,
194 char *dst, const char *src,
195 int32_t dst_pitch, uint32_t src_pitch,
196 bool has_swizzling,
197 enum isl_tiling tiling,
198 isl_memcpy_type copy_type);
199
200 /* This is useful for adding the isl_prefix to genX functions */
201 #define __PASTE2(x, y) x ## y
202 #define __PASTE(x, y) __PASTE2(x, y)
203 #define isl_genX(x) __PASTE(isl_, genX(x))
204
205 #ifdef genX
206 # include "isl_genX_priv.h"
207 #else
208 # define genX(x) gen4_##x
209 # include "isl_genX_priv.h"
210 # undef genX
211 # define genX(x) gen5_##x
212 # include "isl_genX_priv.h"
213 # undef genX
214 # define genX(x) gen6_##x
215 # include "isl_genX_priv.h"
216 # undef genX
217 # define genX(x) gen7_##x
218 # include "isl_genX_priv.h"
219 # undef genX
220 # define genX(x) gen75_##x
221 # include "isl_genX_priv.h"
222 # undef genX
223 # define genX(x) gen8_##x
224 # include "isl_genX_priv.h"
225 # undef genX
226 # define genX(x) gen9_##x
227 # include "isl_genX_priv.h"
228 # undef genX
229 # define genX(x) gen10_##x
230 # include "isl_genX_priv.h"
231 # undef genX
232 # define genX(x) gen11_##x
233 # include "isl_genX_priv.h"
234 # undef genX
235 # define genX(x) gen12_##x
236 # include "isl_genX_priv.h"
237 # undef genX
238 #endif
239
240 #endif /* ISL_PRIV_H */