aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAntonio Borneo <borneo.antonio@gmail.com>2019-01-28 18:22:21 +0100
committerTomas Vanek <vanekt@fbl.cz>2019-06-06 16:32:57 +0100
commite5177a5ad455ac0c353fd6bb48ff288912168428 (patch)
tree1b2f81a2787434f22574f56b6e6b3cd94d8360a7 /src
parente26910fdad0cdbb930fbdc938e3b186f5dbaccb9 (diff)
downloadriscv-openocd-e5177a5ad455ac0c353fd6bb48ff288912168428.zip
riscv-openocd-e5177a5ad455ac0c353fd6bb48ff288912168428.tar.gz
riscv-openocd-e5177a5ad455ac0c353fd6bb48ff288912168428.tar.bz2
helper: add bitmap helper primitives
Mainly copied/inspired from Linux kernel code in - include/linux/types.h - include/linux/bitmap.h - include/linux/bitops.h Change-Id: I317b542993ab81530c86553f339b79505d0fef0f Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com> Reviewed-on: http://openocd.zylin.com/4894 Tested-by: jenkins Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
Diffstat (limited to 'src')
-rw-r--r--src/helper/Makefile.am1
-rw-r--r--src/helper/bits.h84
2 files changed, 85 insertions, 0 deletions
diff --git a/src/helper/Makefile.am b/src/helper/Makefile.am
index e0f7f49..2b3523f 100644
--- a/src/helper/Makefile.am
+++ b/src/helper/Makefile.am
@@ -16,6 +16,7 @@ noinst_LTLIBRARIES += %D%/libhelper.la
%D%/jep106.c \
%D%/jim-nvp.c \
%D%/binarybuffer.h \
+ %D%/bits.h \
%D%/configuration.h \
%D%/ioutil.h \
%D%/list.h \
diff --git a/src/helper/bits.h b/src/helper/bits.h
new file mode 100644
index 0000000..464584d
--- /dev/null
+++ b/src/helper/bits.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
+ * Author(s): Antonio Borneo <borneo.antonio@gmail.com> for STMicroelectronics
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * The content of this file is mainly copied/inspired from Linux kernel
+ * code in include/linux/types.h include/linux/bitmap.h include/linux/bitops.h
+ */
+
+#ifndef OPENOCD_HELPER_BITS_H
+#define OPENOCD_HELPER_BITS_H
+
+#include <helper/types.h>
+
+#define BIT(nr) (1UL << (nr))
+#define BITS_PER_BYTE 8
+#define BITS_PER_LONG (BITS_PER_BYTE * sizeof(long))
+#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
+#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
+#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
+#define DECLARE_BITMAP(name, bits) unsigned long name[BITS_TO_LONGS(bits)]
+
+/**
+ * bitmap_zero - Clears all the bits in memory
+ * @dst: the address of the bitmap
+ * @nbits: the number of bits to clear
+ */
+static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
+{
+ unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
+ memset(dst, 0, len);
+}
+
+/**
+ * clear_bit - Clear a bit in memory
+ * @nr: the bit to set
+ * @addr: the address to start counting from
+ */
+static inline void clear_bit(unsigned int nr, volatile unsigned long *addr)
+{
+ unsigned long mask = BIT_MASK(nr);
+ unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+ *p &= ~mask;
+}
+
+/**
+ * set_bit - Set a bit in memory
+ * @nr: the bit to set
+ * @addr: the address to start counting from
+ */
+static inline void set_bit(unsigned int nr, volatile unsigned long *addr)
+{
+ unsigned long mask = BIT_MASK(nr);
+ unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
+
+ *p |= mask;
+}
+
+/**
+ * test_bit - Determine whether a bit is set
+ * @nr: bit number to test
+ * @addr: Address to start counting from
+ */
+static inline int test_bit(unsigned int nr, const volatile unsigned long *addr)
+{
+ return 1UL & (addr[BIT_WORD(nr)] >> (nr % BITS_PER_LONG));
+}
+
+#endif /* OPENOCD_HELPER_BITS_H */