History

drm/i915: use REG_FIELD_PREP() to define register bitfield values

Slightly verbose, but does away with hand rolled shifts. Ties the field values with the mask defining the field. Unfortunately we have to make a local copy of FIELD_PREP() to evaluate to a integer constant expression. But with this, we can ensure the mask is non-zero, power of 2, fits u32, and the value fits the mask (when the value is a constant expression). Convert power sequencer registers as an example. v4: - rebase v3: - rename the macro to REG_FIELD_PREP to avoid underscore prefix and to be in line with kernel macros (Chris) - rename power of 2 check macro (Chris) v2: - add build-time checks with BUILD_BUG_ON_ZERO() - rename to just _FIELD() due to regmap.h REG_FIELD() clash

Jani Nikula committed 7 years ago

Commit baa09e71 parent 78b36b11 file · +39 −30Browse files

Unified · Split

drivers/gpu/drm/i915/i915_reg.h+39−30
@@ -62,11 +62,11 @@ · 61 unmodified lines
62 * significant to least significant bit. Indent the register content macros62 * significant to least significant bit. Indent the register content macros
63 * using two extra spaces between ``#define`` and the macro name.63 * using two extra spaces between ``#define`` and the macro name.
64 *64 *
65 * Define bit fields using ``REG_GENMASK(h, l)``. Define bit field contents so65 * Define bit fields using ``REG_GENMASK(h, l)``. Define bit field contents
66 * that they are already shifted in place, and can be directly OR'd. For66 * using ``REG_FIELD_PREP(mask, value)``. This will define the values already
67 * convenience, function-like macros may be used to define bit fields, but do67 * shifted in place, so they can be directly OR'd together. For convenience,
68 * note that the macros may be needed to read as well as write the register68 * function-like macros may be used to define bit fields, but do note that the
69 * contents.69 * macros may be needed to read as well as write the register contents.
70 *70 *
71 * Define bits using ``REG_BIT(N)``. Do **not** add ``_BIT`` suffix to the name.71 * Define bits using ``REG_BIT(N)``. Do **not** add ``_BIT`` suffix to the name.
72 *72 *
@@ -108,9 +108,9 @@ · 35 unmodified lines
108 * #define FOO(pipe) _MMIO_PIPE(pipe, _FOO_A, _FOO_B)108 * #define FOO(pipe) _MMIO_PIPE(pipe, _FOO_A, _FOO_B)
109 * #define FOO_ENABLE REG_BIT(31)109 * #define FOO_ENABLE REG_BIT(31)
110 * #define FOO_MODE_MASK REG_GENMASK(19, 16)110 * #define FOO_MODE_MASK REG_GENMASK(19, 16)
111 * #define FOO_MODE_BAR (0 << 16)111 * #define FOO_MODE_BAR REG_FIELD_PREP(FOO_MODE_MASK, 0)
112 * #define FOO_MODE_BAZ (1 << 16)112 * #define FOO_MODE_BAZ REG_FIELD_PREP(FOO_MODE_MASK, 1)
113 * #define FOO_MODE_QUX_SNB (2 << 16)113 * #define FOO_MODE_QUX_SNB REG_FIELD_PREP(FOO_MODE_MASK, 2)
114 *114 *
115 * #define BAR _MMIO(0xb000)115 * #define BAR _MMIO(0xb000)
116 * #define GEN8_BAR _MMIO(0xb888)116 * #define GEN8_BAR _MMIO(0xb888)
@@ -144,17 +144,27 @@ · 27 unmodified lines
144 __builtin_constant_p(__low) && \144 __builtin_constant_p(__low) && \
145 ((__low) < 0 || (__high) > 31 || (__low) > (__high)))))145 ((__low) < 0 || (__high) > 31 || (__low) > (__high)))))
146 146
147/*
148 * Local integer constant expression version of is_power_of_2().
149 */
150#define IS_POWER_OF_2(__x) ((__x) && (((__x) & ((__x) - 1)) == 0))
151
147/**152/**
148 * REG_FIELD_PREP() - Prepare a u32 bitfield value153 * REG_FIELD_PREP() - Prepare a u32 bitfield value
149 * @__mask: shifted mask defining the field's length and position154 * @__mask: shifted mask defining the field's length and position
150 * @__val: value to put in the field155 * @__val: value to put in the field
151 156
152 * Local wrapper for FIELD_PREP() to force u32 and for consistency with157 * Local copy of FIELD_PREP() to generate an integer constant expression, force
153 * REG_FIELD_GET(), REG_BIT() and REG_GENMASK().158 * u32 and for consistency with REG_FIELD_GET(), REG_BIT() and REG_GENMASK().
154 *159 *
155 * @return: @__val masked and shifted into the field defined by @__mask.160 * @return: @__val masked and shifted into the field defined by @__mask.
156 */161 */
157#define REG_FIELD_PREP(__mask, __val) ((u32)FIELD_PREP(__mask, __val))162#define REG_FIELD_PREP(__mask, __val) \
163 ((u32)((((typeof(__mask))(__val) << __bf_shf(__mask)) & (__mask)) + \
164 BUILD_BUG_ON_ZERO(!__builtin_constant_p(__mask)) + \
165 BUILD_BUG_ON_ZERO((__mask) == 0 || (__mask) > U32_MAX) + \
166 BUILD_BUG_ON_ZERO(!IS_POWER_OF_2((__mask) + (1ULL << __bf_shf(__mask)))) + \
167 BUILD_BUG_ON_ZERO(__builtin_choose_expr(__builtin_constant_p(__val), (~((__mask) >> __bf_shf(__mask)) & (__val)), 0))))
158 168
159/**169/**
160 * REG_FIELD_GET() - Extract a u32 bitfield value170 * REG_FIELD_GET() - Extract a u32 bitfield value
@@ -4764,27 +4774,26 @@ enum { · 4603 unmodified lines
4764 */4774 */
4765#define PP_READY REG_BIT(30)4775#define PP_READY REG_BIT(30)
4766#define PP_SEQUENCE_MASK REG_GENMASK(29, 28)4776#define PP_SEQUENCE_MASK REG_GENMASK(29, 28)
4767#define PP_SEQUENCE_NONE (0 << 28)4777#define PP_SEQUENCE_NONE REG_FIELD_PREP(PP_SEQUENCE_MASK, 0)
4768#define PP_SEQUENCE_POWER_UP (1 << 28)4778#define PP_SEQUENCE_POWER_UP REG_FIELD_PREP(PP_SEQUENCE_MASK, 1)
4769#define PP_SEQUENCE_POWER_DOWN (2 << 28)4779#define PP_SEQUENCE_POWER_DOWN REG_FIELD_PREP(PP_SEQUENCE_MASK, 2)
4770#define PP_CYCLE_DELAY_ACTIVE REG_BIT(27)4780#define PP_CYCLE_DELAY_ACTIVE REG_BIT(27)
4771#define PP_SEQUENCE_STATE_MASK REG_GENMASK(3, 0)4781#define PP_SEQUENCE_STATE_MASK REG_GENMASK(3, 0)
4772#define PP_SEQUENCE_STATE_OFF_IDLE (0x0 << 0)4782#define PP_SEQUENCE_STATE_OFF_IDLE REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0x0)
4773#define PP_SEQUENCE_STATE_OFF_S0_1 (0x1 << 0)4783#define PP_SEQUENCE_STATE_OFF_S0_1 REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0x1)
4774#define PP_SEQUENCE_STATE_OFF_S0_2 (0x2 << 0)4784#define PP_SEQUENCE_STATE_OFF_S0_2 REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0x2)
4775#define PP_SEQUENCE_STATE_OFF_S0_3 (0x3 << 0)4785#define PP_SEQUENCE_STATE_OFF_S0_3 REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0x3)
4776#define PP_SEQUENCE_STATE_ON_IDLE (0x8 << 0)4786#define PP_SEQUENCE_STATE_ON_IDLE REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0x8)
4777#define PP_SEQUENCE_STATE_ON_S1_1 (0x9 << 0)4787#define PP_SEQUENCE_STATE_ON_S1_1 REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0x9)
4778#define PP_SEQUENCE_STATE_ON_S1_2 (0xa << 0)4788#define PP_SEQUENCE_STATE_ON_S1_2 REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0xa)
4779#define PP_SEQUENCE_STATE_ON_S1_3 (0xb << 0)4789#define PP_SEQUENCE_STATE_ON_S1_3 REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0xb)
4780#define PP_SEQUENCE_STATE_RESET (0xf << 0)4790#define PP_SEQUENCE_STATE_RESET REG_FIELD_PREP(PP_SEQUENCE_STATE_MASK, 0xf)
4781 4791
4782#define _PP_CONTROL 0x612044792#define _PP_CONTROL 0x61204
4783#define PP_CONTROL(pps_idx) _MMIO_PPS(pps_idx, _PP_CONTROL)4793#define PP_CONTROL(pps_idx) _MMIO_PPS(pps_idx, _PP_CONTROL)
4784#define PANEL_UNLOCK_MASK REG_GENMASK(31, 16)4794#define PANEL_UNLOCK_MASK REG_GENMASK(31, 16)
4785#define PANEL_UNLOCK_REGS (0xabcd << 16)4795#define PANEL_UNLOCK_REGS REG_FIELD_PREP(PANEL_UNLOCK_MASK, 0xabcd)
4786#define BXT_POWER_CYCLE_DELAY_MASK REG_GENMASK(8, 4)4796#define BXT_POWER_CYCLE_DELAY_MASK REG_GENMASK(8, 4)
4787#define BXT_POWER_CYCLE_DELAY_SHIFT 4
4788#define EDP_FORCE_VDD REG_BIT(3)4797#define EDP_FORCE_VDD REG_BIT(3)
4789#define EDP_BLC_ENABLE REG_BIT(2)4798#define EDP_BLC_ENABLE REG_BIT(2)
4790#define PANEL_POWER_RESET REG_BIT(1)4799#define PANEL_POWER_RESET REG_BIT(1)
@@ -4793,11 +4802,11 @@ enum { · 2 unmodified lines
4793#define _PP_ON_DELAYS 0x612084802#define _PP_ON_DELAYS 0x61208
4794#define PP_ON_DELAYS(pps_idx) _MMIO_PPS(pps_idx, _PP_ON_DELAYS)4803#define PP_ON_DELAYS(pps_idx) _MMIO_PPS(pps_idx, _PP_ON_DELAYS)
4795#define PANEL_PORT_SELECT_MASK REG_GENMASK(31, 30)4804#define PANEL_PORT_SELECT_MASK REG_GENMASK(31, 30)
4796#define PANEL_PORT_SELECT_LVDS (0 << 30)4805#define PANEL_PORT_SELECT_LVDS REG_FIELD_PREP(PANEL_PORT_SELECT_MASK, 0)
4797#define PANEL_PORT_SELECT_DPA (1 << 30)4806#define PANEL_PORT_SELECT_DPA REG_FIELD_PREP(PANEL_PORT_SELECT_MASK, 1)
4798#define PANEL_PORT_SELECT_DPC (2 << 30)4807#define PANEL_PORT_SELECT_DPC REG_FIELD_PREP(PANEL_PORT_SELECT_MASK, 2)
4799#define PANEL_PORT_SELECT_DPD (3 << 30)4808#define PANEL_PORT_SELECT_DPD REG_FIELD_PREP(PANEL_PORT_SELECT_MASK, 3)
4800#define PANEL_PORT_SELECT_VLV(port) ((port) << 30)4809#define PANEL_PORT_SELECT_VLV(port) REG_FIELD_PREP(PANEL_PORT_SELECT_MASK, port)
4801#define PANEL_POWER_UP_DELAY_MASK REG_GENMASK(28, 16)4810#define PANEL_POWER_UP_DELAY_MASK REG_GENMASK(28, 16)
4802#define PANEL_LIGHT_ON_DELAY_MASK REG_GENMASK(12, 0)4811#define PANEL_LIGHT_ON_DELAY_MASK REG_GENMASK(12, 0)
4803 4812