1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #ifndef __I915_REG_DEFS__
7 #define __I915_REG_DEFS__
8 
9 #include <linux/bitfield.h>
10 #include <linux/bits.h>
11 
12 /**
13  * REG_BIT() - Prepare a u32 bit value
14  * @__n: 0-based bit number
15  *
16  * Local wrapper for BIT() to force u32, with compile time checks.
17  *
18  * @return: Value with bit @__n set.
19  */
20 #define REG_BIT(__n)							\
21 	((u32)(BIT(__n) +						\
22 	       BUILD_BUG_ON_ZERO(__is_constexpr(__n) &&		\
23 				 ((__n) < 0 || (__n) > 31))))
24 
25 /**
26  * REG_GENMASK() - Prepare a continuous u32 bitmask
27  * @__high: 0-based high bit
28  * @__low: 0-based low bit
29  *
30  * Local wrapper for GENMASK() to force u32, with compile time checks.
31  *
32  * @return: Continuous bitmask from @__high to @__low, inclusive.
33  */
34 #define REG_GENMASK(__high, __low)					\
35 	((u32)(GENMASK(__high, __low) +					\
36 	       BUILD_BUG_ON_ZERO(__is_constexpr(__high) &&	\
37 				 __is_constexpr(__low) &&		\
38 				 ((__low) < 0 || (__high) > 31 || (__low) > (__high)))))
39 
40 /*
41  * Local integer constant expression version of is_power_of_2().
42  */
43 #define IS_POWER_OF_2(__x)		((__x) && (((__x) & ((__x) - 1)) == 0))
44 
45 /**
46  * REG_FIELD_PREP() - Prepare a u32 bitfield value
47  * @__mask: shifted mask defining the field's length and position
48  * @__val: value to put in the field
49  *
50  * Local copy of FIELD_PREP() to generate an integer constant expression, force
51  * u32 and for consistency with REG_FIELD_GET(), REG_BIT() and REG_GENMASK().
52  *
53  * @return: @__val masked and shifted into the field defined by @__mask.
54  */
55 #define REG_FIELD_PREP(__mask, __val)						\
56 	((u32)((((typeof(__mask))(__val) << __bf_shf(__mask)) & (__mask)) +	\
57 	       BUILD_BUG_ON_ZERO(!__is_constexpr(__mask)) +		\
58 	       BUILD_BUG_ON_ZERO((__mask) == 0 || (__mask) > U32_MAX) +		\
59 	       BUILD_BUG_ON_ZERO(!IS_POWER_OF_2((__mask) + (1ULL << __bf_shf(__mask)))) + \
60 	       BUILD_BUG_ON_ZERO(__builtin_choose_expr(__is_constexpr(__val), (~((__mask) >> __bf_shf(__mask)) & (__val)), 0))))
61 
62 /**
63  * REG_FIELD_GET() - Extract a u32 bitfield value
64  * @__mask: shifted mask defining the field's length and position
65  * @__val: value to extract the bitfield value from
66  *
67  * Local wrapper for FIELD_GET() to force u32 and for consistency with
68  * REG_FIELD_PREP(), REG_BIT() and REG_GENMASK().
69  *
70  * @return: Masked and shifted value of the field defined by @__mask in @__val.
71  */
72 #define REG_FIELD_GET(__mask, __val)	((u32)FIELD_GET(__mask, __val))
73 
74 typedef struct {
75 	u32 reg;
76 } i915_reg_t;
77 
78 #define _MMIO(r) ((const i915_reg_t){ .reg = (r) })
79 
80 #define INVALID_MMIO_REG _MMIO(0)
81 
82 static __always_inline u32 i915_mmio_reg_offset(i915_reg_t reg)
83 {
84 	return reg.reg;
85 }
86 
87 static inline bool i915_mmio_reg_equal(i915_reg_t a, i915_reg_t b)
88 {
89 	return i915_mmio_reg_offset(a) == i915_mmio_reg_offset(b);
90 }
91 
92 static inline bool i915_mmio_reg_valid(i915_reg_t reg)
93 {
94 	return !i915_mmio_reg_equal(reg, INVALID_MMIO_REG);
95 }
96 
97 #define VLV_DISPLAY_BASE		0x180000
98 
99 #define GEN12_SFC_DONE_MAX		4
100 
101 #endif /* __I915_REG_DEFS__ */
102