[PATCH v3 1/7] drm: add drm_print_bits

From: Gerd Hoffmann
Date: Wed Sep 04 2019 - 01:47:52 EST


New helper to print named bits of some value (think flags fields).

Signed-off-by: Gerd Hoffmann <kraxel@xxxxxxxxxx>
---
include/drm/drm_print.h | 3 +++
drivers/gpu/drm/drm_print.c | 33 +++++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+)

diff --git a/include/drm/drm_print.h b/include/drm/drm_print.h
index 112165d3195d..12d4916254b4 100644
--- a/include/drm/drm_print.h
+++ b/include/drm/drm_print.h
@@ -89,6 +89,9 @@ __printf(2, 3)
void drm_printf(struct drm_printer *p, const char *f, ...);
void drm_puts(struct drm_printer *p, const char *str);
void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset);
+void drm_print_bits(struct drm_printer *p,
+ unsigned long value, const char *bits[],
+ unsigned int from, unsigned int to);

__printf(2, 0)
/**
diff --git a/drivers/gpu/drm/drm_print.c b/drivers/gpu/drm/drm_print.c
index ad302d71eeee..dfa27367ebb8 100644
--- a/drivers/gpu/drm/drm_print.c
+++ b/drivers/gpu/drm/drm_print.c
@@ -185,6 +185,39 @@ void drm_printf(struct drm_printer *p, const char *f, ...)
}
EXPORT_SYMBOL(drm_printf);

+/**
+ * drm_print_bits - print bits to a &drm_printer stream
+ *
+ * Print bits (in flag fields for example) in human readable form.
+ * The first name in the @bits array is for the bit indexed by @from.
+ *
+ * @p: the &drm_printer
+ * @value: field value.
+ * @bits: Array with bit names.
+ * @from: start of bit range to print (inclusive).
+ * @to: end of bit range to print (exclusive).
+ */
+void drm_print_bits(struct drm_printer *p,
+ unsigned long value, const char *bits[],
+ unsigned int from, unsigned int to)
+{
+ bool first = true;
+ unsigned int i;
+
+ for (i = from; i < to; i++) {
+ if (!(value & (1 << i)))
+ continue;
+ if (WARN_ON_ONCE(!bits[i-from]))
+ continue;
+ drm_printf(p, "%s%s", first ? "" : ",",
+ bits[i-from]);
+ first = false;
+ }
+ if (first)
+ drm_printf(p, "(none)");
+}
+EXPORT_SYMBOL(drm_print_bits);
+
void drm_dev_printk(const struct device *dev, const char *level,
const char *format, ...)
{
--
2.18.1