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