[PATCH v2 1/6] drm: add drm_print_bits

From: Gerd Hoffmann
Date: Tue Sep 03 2019 - 06:13:18 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 | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+)

diff --git a/include/drm/drm_print.h b/include/drm/drm_print.h
index a5d6f2f3e430..8658c1da1c7d 100644
--- a/include/drm/drm_print.h
+++ b/include/drm/drm_print.h
@@ -88,6 +88,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 int indent,
+ const char *label, unsigned int value,
+ const char *bits[], unsigned int nbits);

__printf(2, 0)
/**
diff --git a/drivers/gpu/drm/drm_print.c b/drivers/gpu/drm/drm_print.c
index a17c8a14dba4..7f7aba920f51 100644
--- a/drivers/gpu/drm/drm_print.c
+++ b/drivers/gpu/drm/drm_print.c
@@ -179,6 +179,42 @@ 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.
+ *
+ * @p: the &drm_printer
+ * @indent: Tab indentation level (max 5)
+ * @label: field label.
+ * @value: field value.
+ * @bits: Array with bit names.
+ * @nbits: bit name array size.
+ */
+void drm_print_bits(struct drm_printer *p, unsigned int indent,
+ const char *label, unsigned int value,
+ const char *bits[], unsigned int nbits)
+{
+ bool first = true;
+ unsigned int i;
+
+ for (i = 0; i < nbits; i++) {
+ if (!(value & (1 << i)))
+ continue;
+ if (!bits[i])
+ continue;
+ if (first) {
+ first = false;
+ drm_printf_indent(p, indent, "%s=%s",
+ label, bits[i]);
+ } else
+ drm_printf(p, ",%s", bits[i]);
+ }
+ if (!first)
+ drm_printf(p, "\n");
+}
+EXPORT_SYMBOL(drm_print_bits);
+
void drm_dev_printk(const struct device *dev, const char *level,
const char *format, ...)
{
--
2.18.1