diff mbox series

[v2,1/6] drm: add drm_print_bits

Message ID 20190903101248.12879-2-kraxel@redhat.com (mailing list archive)
State New, archived
Headers show
Series drm: add some ttm/vram info to debugfs | expand

Commit Message

Gerd Hoffmann Sept. 3, 2019, 10:12 a.m. UTC
New helper to print named bits of some value (think flags fields).

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

Comments

Daniel Vetter Sept. 3, 2019, 10:54 a.m. UTC | #1
On Tue, Sep 03, 2019 at 12:12:43PM +0200, Gerd Hoffmann wrote:
> New helper to print named bits of some value (think flags fields).
> 
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  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])

I think this should be a WARN_ON, indicates a programming error?

> +			continue;
> +		if (first) {
> +			first = false;
> +			drm_printf_indent(p, indent, "%s=%s",
> +					  label, bits[i]);

Hm, to make this a bit more flexible to use I'd drop the label= printing
...

> +		} else
> +			drm_printf(p, ",%s", bits[i]);
> +	}
> +	if (!first)
> +		drm_printf(p, "\n");

... and also the newline. Then you could also use this for bit-fields
which just a few bits. Also, should we print anything if no bit is set?

If you prefer the label= + \n then pls add that to the kerneldoc, that it
prints this as a line of its own.
-Daniel

> +}
> +EXPORT_SYMBOL(drm_print_bits);
> +
>  void drm_dev_printk(const struct device *dev, const char *level,
>  		    const char *format, ...)
>  {
> -- 
> 2.18.1
>
diff mbox series

Patch

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, ...)
 {