2 * Copyright © 2007-2019 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
28 ****************************************************************************************************
30 * @brief Contains the helper function and constants.
31 ****************************************************************************************************
34 #ifndef __ADDR_COMMON_H__
35 #define __ADDR_COMMON_H__
37 #include "addrinterface.h"
47 // ADDR_LNX_KERNEL_BUILD is for internal build
48 // Moved from addrinterface.h so __KERNEL__ is not needed any more
49 #if !defined(__APPLE__) || defined(HAVE_TSERVER)
55 ////////////////////////////////////////////////////////////////////////////////////////////////////
56 // Platform specific debug break defines
57 ////////////////////////////////////////////////////////////////////////////////////////////////////
60 #define ADDR_DBG_BREAK() assert(false)
61 #elif defined(__APPLE__)
62 #define ADDR_DBG_BREAK() { IOPanic("");}
64 #define ADDR_DBG_BREAK() { __debugbreak(); }
67 #define ADDR_DBG_BREAK()
69 ////////////////////////////////////////////////////////////////////////////////////////////////////
71 ////////////////////////////////////////////////////////////////////////////////////////////////////
72 // Debug assertions used in AddrLib
73 ////////////////////////////////////////////////////////////////////////////////////////////////////
74 #if defined(_WIN32) && (_MSC_VER >= 1400)
75 #define ADDR_ANALYSIS_ASSUME(expr) __analysis_assume(expr)
77 #define ADDR_ANALYSIS_ASSUME(expr) do { (void)(expr); } while (0)
80 #define ADDR_ASSERT(__e) assert(__e)
81 #define ADDR_ASSERT_ALWAYS() ADDR_DBG_BREAK()
82 #define ADDR_UNHANDLED_CASE() ADDR_ASSERT(!"Unhandled case")
83 #define ADDR_NOT_IMPLEMENTED() ADDR_ASSERT(!"Not implemented");
84 ////////////////////////////////////////////////////////////////////////////////////////////////////
86 ////////////////////////////////////////////////////////////////////////////////////////////////////
87 // Debug print macro from legacy address library
88 ////////////////////////////////////////////////////////////////////////////////////////////////////
91 #define ADDR_PRNT(a) Object::DebugPrint a
93 /// @brief Macro for reporting informational messages
96 /// This macro optionally prints an informational message to stdout.
97 /// The first parameter is a condition -- if it is true, nothing is done.
98 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
99 /// starting with a string. This is passed to printf() or an equivalent
100 /// in order to format the informational message. For example,
101 /// ADDR_INFO(0, ("test %d",3) ); prints out "test 3".
103 #define ADDR_INFO(cond, a) \
104 { if (!(cond)) { ADDR_PRNT(a); } }
106 /// @brief Macro for reporting error warning messages
109 /// This macro optionally prints an error warning message to stdout,
110 /// followed by the file name and line number where the macro was called.
111 /// The first parameter is a condition -- if it is true, nothing is done.
112 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
113 /// starting with a string. This is passed to printf() or an equivalent
114 /// in order to format the informational message. For example,
115 /// ADDR_WARN(0, ("test %d",3) ); prints out "test 3" followed by
116 /// a second line with the file name and line number.
118 #define ADDR_WARN(cond, a) \
121 ADDR_PRNT((" WARNING in file %s, line %d\n", __FILE__, __LINE__)); \
124 /// @brief Macro for reporting fatal error conditions
127 /// This macro optionally stops execution of the current routine
128 /// after printing an error warning message to stdout,
129 /// followed by the file name and line number where the macro was called.
130 /// The first parameter is a condition -- if it is true, nothing is done.
131 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
132 /// starting with a string. This is passed to printf() or an equivalent
133 /// in order to format the informational message. For example,
134 /// ADDR_EXIT(0, ("test %d",3) ); prints out "test 3" followed by
135 /// a second line with the file name and line number, then stops execution.
137 #define ADDR_EXIT(cond, a) \
139 { ADDR_PRNT(a); ADDR_DBG_BREAK();\
144 #define ADDRDPF 1 ? (void)0 : (void)
148 #define ADDR_DBG_BREAK()
150 #define ADDR_INFO(cond, a)
152 #define ADDR_WARN(cond, a)
154 #define ADDR_EXIT(cond, a)
157 ////////////////////////////////////////////////////////////////////////////////////////////////////
159 #if defined(static_assert)
160 #define ADDR_C_ASSERT(__e) static_assert(__e, "")
162 #define ADDR_C_ASSERT(__e) typedef char __ADDR_C_ASSERT__[(__e) ? 1 : -1]
170 ////////////////////////////////////////////////////////////////////////////////////////////////////
172 ////////////////////////////////////////////////////////////////////////////////////////////////////
173 static const UINT_32 MicroTileWidth
= 8; ///< Micro tile width, for 1D and 2D tiling
174 static const UINT_32 MicroTileHeight
= 8; ///< Micro tile height, for 1D and 2D tiling
175 static const UINT_32 ThickTileThickness
= 4; ///< Micro tile thickness, for THICK modes
176 static const UINT_32 XThickTileThickness
= 8; ///< Extra thick tiling thickness
177 static const UINT_32 PowerSaveTileBytes
= 64; ///< Nuber of bytes per tile for power save 64
178 static const UINT_32 CmaskCacheBits
= 1024; ///< Number of bits for CMASK cache
179 static const UINT_32 CmaskElemBits
= 4; ///< Number of bits for CMASK element
180 static const UINT_32 HtileCacheBits
= 16384; ///< Number of bits for HTILE cache 512*32
182 static const UINT_32 MicroTilePixels
= MicroTileWidth
* MicroTileHeight
;
184 static const INT_32 TileIndexInvalid
= TILEINDEX_INVALID
;
185 static const INT_32 TileIndexLinearGeneral
= TILEINDEX_LINEAR_GENERAL
;
186 static const INT_32 TileIndexNoMacroIndex
= -3;
192 ////////////////////////////////////////////////////////////////////////////////////////////////////
194 ////////////////////////////////////////////////////////////////////////////////////////////////////
195 static const UINT_32 MaxSurfaceHeight
= 16384;
199 ////////////////////////////////////////////////////////////////////////////////////////////////////
201 ////////////////////////////////////////////////////////////////////////////////////////////////////
202 #define BITS_PER_BYTE 8
203 #define BITS_TO_BYTES(x) ( ((x) + (BITS_PER_BYTE-1)) / BITS_PER_BYTE )
204 #define BYTES_TO_BITS(x) ( (x) * BITS_PER_BYTE )
206 /// Helper macros to select a single bit from an int (undefined later in section)
207 #define _BIT(v,b) (((v) >> (b) ) & 1)
210 ****************************************************************************************************
211 * @brief Enums to identify AddrLib type
212 ****************************************************************************************************
225 ****************************************************************************************************
229 * Neutral enums that specifies chip family.
231 ****************************************************************************************************
235 ADDR_CHIP_FAMILY_IVLD
, ///< Invalid family
236 ADDR_CHIP_FAMILY_R6XX
,
237 ADDR_CHIP_FAMILY_R7XX
,
238 ADDR_CHIP_FAMILY_R8XX
,
247 ****************************************************************************************************
251 * This structure is used to set configuration flags.
252 ****************************************************************************************************
258 /// These flags are set up internally thru AddrLib::Create() based on ADDR_CREATE_FLAGS
259 UINT_32 optimalBankSwap
: 1; ///< New bank tiling for RV770 only
260 UINT_32 noCubeMipSlicesPad
: 1; ///< Disables faces padding for cubemap mipmaps
261 UINT_32 fillSizeFields
: 1; ///< If clients fill size fields in all input and
263 UINT_32 ignoreTileInfo
: 1; ///< Don't use tile info structure
264 UINT_32 useTileIndex
: 1; ///< Make tileIndex field in input valid
265 UINT_32 useCombinedSwizzle
: 1; ///< Use combined swizzle
266 UINT_32 checkLast2DLevel
: 1; ///< Check the last 2D mip sub level
267 UINT_32 useHtileSliceAlign
: 1; ///< Do htile single slice alignment
268 UINT_32 allowLargeThickTile
: 1; ///< Allow 64*thickness*bytesPerPixel > rowSize
269 UINT_32 disableLinearOpt
: 1; ///< Disallow tile modes to be optimized to linear
270 UINT_32 use32bppFor422Fmt
: 1; ///< View 422 formats as 32 bits per pixel element
271 UINT_32 forceDccAndTcCompat
: 1; ///< Force enable DCC and TC compatibility
272 UINT_32 reserved
: 20; ///< Reserved bits for future use
278 ////////////////////////////////////////////////////////////////////////////////////////////////////
279 // Misc helper functions
280 ////////////////////////////////////////////////////////////////////////////////////////////////////
283 ****************************************************************************************************
287 * Xor the right-side numberOfBits bits of x.
288 ****************************************************************************************************
290 static inline UINT_32
XorReduce(
292 UINT_32 numberOfBits
)
295 UINT_32 result
= x
& 1;
297 for (i
=1; i
<numberOfBits
; i
++)
299 result
^= ((x
>>i
) & 1);
306 ****************************************************************************************************
310 * Check if the size (UINT_32) is pow 2
311 ****************************************************************************************************
313 static inline UINT_32
IsPow2(
314 UINT_32 dim
) ///< [in] dimension of miplevel
316 ADDR_ASSERT(dim
> 0);
317 return !(dim
& (dim
- 1));
321 ****************************************************************************************************
325 * Check if the size (UINT_64) is pow 2
326 ****************************************************************************************************
328 static inline UINT_64
IsPow2(
329 UINT_64 dim
) ///< [in] dimension of miplevel
331 ADDR_ASSERT(dim
> 0);
332 return !(dim
& (dim
- 1));
336 ****************************************************************************************************
340 * Align UINT_32 "x" to "align" alignment, "align" should be power of 2
341 ****************************************************************************************************
343 static inline UINT_32
PowTwoAlign(
348 // Assert that x is a power of two.
350 ADDR_ASSERT(IsPow2(align
));
351 return (x
+ (align
- 1)) & (~(align
- 1));
355 ****************************************************************************************************
359 * Align UINT_64 "x" to "align" alignment, "align" should be power of 2
360 ****************************************************************************************************
362 static inline UINT_64
PowTwoAlign(
367 // Assert that x is a power of two.
369 ADDR_ASSERT(IsPow2(align
));
370 return (x
+ (align
- 1)) & (~(align
- 1));
374 ****************************************************************************************************
378 * Get the min value between two unsigned values
379 ****************************************************************************************************
381 static inline UINT_32
Min(
385 return ((value1
< (value2
)) ? (value1
) : value2
);
389 ****************************************************************************************************
393 * Get the min value between two signed values
394 ****************************************************************************************************
396 static inline INT_32
Min(
400 return ((value1
< (value2
)) ? (value1
) : value2
);
404 ****************************************************************************************************
408 * Get the max value between two unsigned values
409 ****************************************************************************************************
411 static inline UINT_32
Max(
415 return ((value1
> (value2
)) ? (value1
) : value2
);
419 ****************************************************************************************************
423 * Get the max value between two signed values
424 ****************************************************************************************************
426 static inline INT_32
Max(
430 return ((value1
> (value2
)) ? (value1
) : value2
);
434 ****************************************************************************************************
438 * Compute the mipmap's next level dim size
439 ****************************************************************************************************
441 static inline UINT_32
NextPow2(
442 UINT_32 dim
) ///< [in] dimension of miplevel
446 if (dim
> 0x7fffffff)
448 ADDR_ASSERT_ALWAYS();
463 ****************************************************************************************************
467 * Compute log of base 2 no matter the target is power of 2 or not
468 ****************************************************************************************************
470 static inline UINT_32
Log2NonPow2(
471 UINT_32 x
) ///< [in] the value should calculate log based 2
486 ****************************************************************************************************
490 * Compute log of base 2
491 ****************************************************************************************************
493 static inline UINT_32
Log2(
494 UINT_32 x
) ///< [in] the value should calculate log based 2
496 // Assert that x is a power of two.
497 ADDR_ASSERT(IsPow2(x
));
499 return Log2NonPow2(x
);
503 ****************************************************************************************************
507 * Compute log of base 2 quickly (<= 16)
508 ****************************************************************************************************
510 static inline UINT_32
QLog2(
511 UINT_32 x
) ///< [in] the value should calculate log based 2
513 ADDR_ASSERT(x
<= 16);
535 ADDR_ASSERT_ALWAYS();
542 ****************************************************************************************************
546 * NULL pointer safe assignment
547 ****************************************************************************************************
549 static inline VOID
SafeAssign(
550 UINT_32
* pLVal
, ///< [in] Pointer to left val
551 UINT_32 rVal
) ///< [in] Right value
560 ****************************************************************************************************
564 * NULL pointer safe assignment for 64bit values
565 ****************************************************************************************************
567 static inline VOID
SafeAssign(
568 UINT_64
* pLVal
, ///< [in] Pointer to left val
569 UINT_64 rVal
) ///< [in] Right value
578 ****************************************************************************************************
582 * NULL pointer safe assignment for AddrTileMode
583 ****************************************************************************************************
585 static inline VOID
SafeAssign(
586 AddrTileMode
* pLVal
, ///< [in] Pointer to left val
587 AddrTileMode rVal
) ///< [in] Right value
596 ****************************************************************************************************
601 ****************************************************************************************************
603 static inline UINT_32
RoundHalf(
604 UINT_32 x
) ///< [in] input value
609 return (x
>> 1) + (x
& 1);
616 ****************************************************************************************************
620 * Calculate sum of a geometric progression whose ratio is 1/2
621 ****************************************************************************************************
623 static inline UINT_32
SumGeo(
624 UINT_32 base
, ///< [in] First term in the geometric progression
625 UINT_32 num
) ///< [in] Number of terms to be added into sum
627 ADDR_ASSERT(base
> 0);
631 for (; (i
< num
) && (base
> 1); i
++)
634 base
= RoundHalf(base
);
642 ****************************************************************************************************
646 * Extract bit N value (0 or 1) of a UINT32 value.
647 ****************************************************************************************************
649 static inline UINT_32
GetBit(
650 UINT_32 u32
, ///< [in] UINT32 value
651 UINT_32 pos
) ///< [in] bit position from LSB, valid range is [0..31]
653 ADDR_ASSERT(pos
<= 31);
655 return (u32
>> pos
) & 0x1;
659 ****************************************************************************************************
663 * Copy 'bitsNum' bits from src start from srcStartPos into destination from dstStartPos
664 * srcStartPos: 0~31 for UINT_32
665 * bitsNum : 1~32 for UINT_32
666 * srcStartPos: 0~31 for UINT_32
669 * src : b[31] b[30] b[29] ... ... ... ... ... ... ... ... b[end]..b[beg] ... b[1] b[0]
670 * || Bits num || copy length || Bits num ||
671 * dst : b[31] b[30] b[29] ... b[end]..b[beg] ... ... ... ... ... ... ... ... b[1] b[0]
674 ****************************************************************************************************
676 static inline UINT_32
GetBits(
682 ADDR_ASSERT((srcStartPos
< 32) && (dstStartPos
< 32) && (bitsNum
> 0));
683 ADDR_ASSERT((bitsNum
+ dstStartPos
<= 32) && (bitsNum
+ srcStartPos
<= 32));
685 return ((src
>> srcStartPos
) << (32 - bitsNum
)) >> (32 - bitsNum
- dstStartPos
);
689 ****************************************************************************************************
693 * Generate 2D Morton interleave code with num lowest bits in each channel
694 ****************************************************************************************************
696 static inline UINT_32
MortonGen2d(
697 UINT_32 x
, ///< [in] First channel
698 UINT_32 y
, ///< [in] Second channel
699 UINT_32 num
) ///< [in] Number of bits extracted from each channel
703 for (UINT_32 i
= 0; i
< num
; i
++)
705 mort
|= (GetBit(y
, i
) << (2 * i
));
706 mort
|= (GetBit(x
, i
) << (2 * i
+ 1));
713 ****************************************************************************************************
717 * Generate 3D Morton interleave code with num lowest bits in each channel
718 ****************************************************************************************************
720 static inline UINT_32
MortonGen3d(
721 UINT_32 x
, ///< [in] First channel
722 UINT_32 y
, ///< [in] Second channel
723 UINT_32 z
, ///< [in] Third channel
724 UINT_32 num
) ///< [in] Number of bits extracted from each channel
728 for (UINT_32 i
= 0; i
< num
; i
++)
730 mort
|= (GetBit(z
, i
) << (3 * i
));
731 mort
|= (GetBit(y
, i
) << (3 * i
+ 1));
732 mort
|= (GetBit(x
, i
) << (3 * i
+ 2));
739 ****************************************************************************************************
743 * Return reversed lowest num bits of v: v[0]v[1]...v[num-2]v[num-1]
744 ****************************************************************************************************
746 static inline UINT_32
ReverseBitVector(
747 UINT_32 v
, ///< [in] Reverse operation base value
748 UINT_32 num
) ///< [in] Number of bits used in reverse operation
752 for (UINT_32 i
= 0; i
< num
; i
++)
754 reverse
|= (GetBit(v
, num
- 1 - i
) << i
);
761 ****************************************************************************************************
765 * Xor bit vector v[num-1]v[num-2]...v[1]v[0] with v[num]v[num+1]...v[2*num-2]v[2*num-1]
766 ****************************************************************************************************
768 static inline UINT_32
FoldXor2d(
769 UINT_32 v
, ///< [in] Xor operation base value
770 UINT_32 num
) ///< [in] Number of bits used in fold xor operation
772 return (v
& ((1 << num
) - 1)) ^ ReverseBitVector(v
>> num
, num
);
776 ****************************************************************************************************
780 * Return v[0] | v[2] | v[4] | v[6]... | v[2*num - 2]
781 ****************************************************************************************************
783 static inline UINT_32
DeMort(
784 UINT_32 v
, ///< [in] DeMort operation base value
785 UINT_32 num
) ///< [in] Number of bits used in fold DeMort operation
789 for (UINT_32 i
= 0; i
< num
; i
++)
791 d
|= ((v
& (1 << (i
<< 1))) >> i
);
798 ****************************************************************************************************
802 * v[0]...v[num-1] ^ v[3*num-1]v[3*num-3]...v[num+2]v[num] ^ v[3*num-2]...v[num+1]v[num-1]
803 ****************************************************************************************************
805 static inline UINT_32
FoldXor3d(
806 UINT_32 v
, ///< [in] Xor operation base value
807 UINT_32 num
) ///< [in] Number of bits used in fold xor operation
809 UINT_32 t
= v
& ((1 << num
) - 1);
810 t
^= ReverseBitVector(DeMort(v
>> num
, num
), num
);
811 t
^= ReverseBitVector(DeMort(v
>> (num
+ 1), num
), num
);
817 ****************************************************************************************************
821 * Set channel initialization value via a return value
822 ****************************************************************************************************
824 static inline ADDR_CHANNEL_SETTING
InitChannel(
825 UINT_32 valid
, ///< [in] valid setting
826 UINT_32 channel
, ///< [in] channel setting
827 UINT_32 index
) ///< [in] index setting
829 ADDR_CHANNEL_SETTING t
;
838 ****************************************************************************************************
842 * Set channel initialization value via channel pointer
843 ****************************************************************************************************
845 static inline VOID
InitChannel(
846 UINT_32 valid
, ///< [in] valid setting
847 UINT_32 channel
, ///< [in] channel setting
848 UINT_32 index
, ///< [in] index setting
849 ADDR_CHANNEL_SETTING
*pChanSet
) ///< [out] channel setting to be initialized
851 pChanSet
->valid
= valid
;
852 pChanSet
->channel
= channel
;
853 pChanSet
->index
= index
;
857 ****************************************************************************************************
861 * Set channel initialization value via another channel
862 ****************************************************************************************************
864 static inline VOID
InitChannel(
865 ADDR_CHANNEL_SETTING
*pChanDst
, ///< [in] channel setting to be copied from
866 ADDR_CHANNEL_SETTING
*pChanSrc
) ///< [out] channel setting to be initialized
868 pChanDst
->valid
= pChanSrc
->valid
;
869 pChanDst
->channel
= pChanSrc
->channel
;
870 pChanDst
->index
= pChanSrc
->index
;
874 ****************************************************************************************************
875 * GetMaxValidChannelIndex
878 * Get max valid index for a specific channel
879 ****************************************************************************************************
881 static inline UINT_32
GetMaxValidChannelIndex(
882 const ADDR_CHANNEL_SETTING
*pChanSet
, ///< [in] channel setting to be initialized
883 UINT_32 searchCount
,///< [in] number of channel setting to be searched
884 UINT_32 channel
) ///< [in] channel to be searched
888 for (UINT_32 i
= 0; i
< searchCount
; i
++)
890 if (pChanSet
[i
].valid
&& (pChanSet
[i
].channel
== channel
))
892 index
= Max(index
, static_cast<UINT_32
>(pChanSet
[i
].index
));
900 ****************************************************************************************************
904 * Get bit mask which indicates which positions in the equation match the target coord
905 ****************************************************************************************************
907 static inline UINT_32
GetCoordActiveMask(
908 const ADDR_CHANNEL_SETTING
*pChanSet
, ///< [in] channel setting to be initialized
909 UINT_32 searchCount
,///< [in] number of channel setting to be searched
910 UINT_32 channel
, ///< [in] channel to be searched
911 UINT_32 index
) ///< [in] index to be searched
915 for (UINT_32 i
= 0; i
< searchCount
; i
++)
917 if ((pChanSet
[i
].valid
== TRUE
) &&
918 (pChanSet
[i
].channel
== channel
) &&
919 (pChanSet
[i
].index
== index
))
930 #endif // __ADDR_COMMON_H__