aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Proshkin <n.proshkin@yadro.com>2023-12-27 14:44:53 +0500
committerMartin Mares <mj@ucw.cz>2024-02-17 23:44:34 +0100
commit7d23054d18402b1891343f090d3cd37d7e83c82f (patch)
tree6433eec74a6b943577562bd2d99603165adc560d
parentd016c32df3d07f180485d7349a0d015deb380ecf (diff)
downloadpciutils-7d23054d18402b1891343f090d3cd37d7e83c82f.tar.gz
libpci: Add separate file for bit manipulation functions
Move several macros from lspci and add some more for operations with bit masks. Reviewed-by: Sergei Miroshnichenko <s.miroshnichenko@yadro.com> Signed-off-by: Nikita Proshkin <n.proshkin@yadro.com>
-rw-r--r--Makefile2
-rw-r--r--lib/bitops.h39
-rw-r--r--lib/pci.h1
-rw-r--r--lspci.h6
4 files changed, 41 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index 228cb56..52538e8 100644
--- a/Makefile
+++ b/Makefile
@@ -62,7 +62,7 @@ LIBNAME=libpci
-include lib/config.mk
-PCIINC=lib/config.h lib/header.h lib/pci.h lib/types.h lib/sysdep.h
+PCIINC=lib/config.h lib/header.h lib/pci.h lib/types.h lib/sysdep.h lib/bitops.h
PCIINC_INS=lib/config.h lib/header.h lib/pci.h lib/types.h
export
diff --git a/lib/bitops.h b/lib/bitops.h
new file mode 100644
index 0000000..029741e
--- /dev/null
+++ b/lib/bitops.h
@@ -0,0 +1,39 @@
+/*
+ * The PCI Utilities -- Decode bits and bit fields
+ *
+ * Copyright (c) 2023 Martin Mares <mj@ucw.cz>
+ * Copyright (c) 2023 KNS Group LLC (YADRO)
+ *
+ * Can be freely distributed and used under the terms of the GNU GPL v2+.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _BITOPS_H
+#define _BITOPS_H
+
+#ifndef _PCI_LIB_H
+#error Import only from pci.h
+#endif
+
+/* Useful macros for decoding of bits and bit fields */
+
+#define FLAG(x, y) ((x & y) ? '+' : '-')
+
+// Generate mask
+
+#define BIT(at) ((u64)1 << (at))
+// Boundaries inclusive
+#define MASK(h, l) ((((u64)1 << ((h) + 1)) - 1) & ~(((u64)1 << (l)) - 1))
+
+// Get/set from register
+
+#define BITS(x, at, width) (((x) >> (at)) & ((1 << (width)) - 1))
+#define GET_REG_MASK(reg, mask) (((reg) & (mask)) / ((mask) & ~((mask) << 1)))
+#define SET_REG_MASK(reg, mask, val) \
+ (((reg) & ~(mask)) | (((val) * ((mask) & ~((mask) << 1))) & (mask)))
+
+#define TABLE(tab, x, buf) \
+ ((x) < sizeof(tab) / sizeof((tab)[0]) ? (tab)[x] : (sprintf((buf), "??%d", (x)), (buf)))
+
+#endif
diff --git a/lib/pci.h b/lib/pci.h
index 03b4c41..d309cbb 100644
--- a/lib/pci.h
+++ b/lib/pci.h
@@ -17,6 +17,7 @@
#include "header.h"
#include "types.h"
+#include "bitops.h"
#define PCI_LIB_VERSION 0x030a00
diff --git a/lspci.h b/lspci.h
index c5a9ec7..4d711a5 100644
--- a/lspci.h
+++ b/lspci.h
@@ -58,12 +58,6 @@ u32 get_conf_long(struct device *d, unsigned int pos);
word get_conf_word(struct device *d, unsigned int pos);
byte get_conf_byte(struct device *d, unsigned int pos);
-/* Useful macros for decoding of bits and bit fields */
-
-#define FLAG(x,y) ((x & y) ? '+' : '-')
-#define BITS(x,at,width) (((x) >> (at)) & ((1 << (width)) - 1))
-#define TABLE(tab,x,buf) ((x) < sizeof(tab)/sizeof((tab)[0]) ? (tab)[x] : (sprintf((buf), "??%d", (x)), (buf)))
-
/* ls-vpd.c */
void cap_vpd(struct device *d);