[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250318071526.1836194-1-quic_luoj@quicinc.com>
Date: Tue, 18 Mar 2025 15:15:26 +0800
From: Luo Jie <quic_luoj@...cinc.com>
To: <yury.norov@...il.com>, <linux@...musvillemoes.dk>
CC: <linux-kernel@...r.kernel.org>, <andrew@...n.ch>,
<quic_kkumarcs@...cinc.com>, <quic_linchen@...cinc.com>,
<quic_leiwei@...cinc.com>, <quic_suruchia@...cinc.com>,
<quic_pavir@...cinc.com>
Subject: [PATCH] bitfield: add FIELD_MODIFY() helper
Add a helper for replacing the contents of bitfield in memory
with the specified value.
Signed-off-by: Luo Jie <quic_luoj@...cinc.com>
---
The new added macro FIELD_MODIFY() is expected to be used by the
following Ethernet PPE driver as link below.
https://lore.kernel.org/linux-arm-msm/20250209-qcom_ipq_ppe-v3-0-453ea18d3271@quicinc.com/
include/linux/bitfield.h | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 63928f173223..13484e09797f 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -7,8 +7,9 @@
#ifndef _LINUX_BITFIELD_H
#define _LINUX_BITFIELD_H
-#include <linux/build_bug.h>
#include <asm/byteorder.h>
+#include <linux/build_bug.h>
+#include <linux/typecheck.h>
/*
* Bitfield access macros
@@ -156,6 +157,23 @@
(typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
})
+/**
+ * FIELD_MODIFY() - modify a bitfield element
+ * @_mask: shifted mask defining the field's length and position
+ * @_reg_p: pointer to the memory that should be updated
+ * @_val: value to store in the bitfield
+ *
+ * FIELD_MODIFY() modifies the set of bits in @_reg_p specified by @_mask,
+ * by replacing them with the bitfield value passed in as @_val.
+ */
+#define FIELD_MODIFY(_mask, _reg_p, _val) \
+ ({ \
+ typecheck_pointer(_reg_p); \
+ __BF_FIELD_CHECK(_mask, *(_reg_p), _val, "FIELD_MODIFY: "); \
+ *(_reg_p) &= ~(_mask); \
+ *(_reg_p) |= ((_val) << __bf_shf(_mask)) & (_mask); \
+ })
+
extern void __compiletime_error("value doesn't fit into mask")
__field_overflow(void);
extern void __compiletime_error("bad bitfield mask")
--
2.34.1
Powered by blists - more mailing lists