For telemetry nodes, show one line per field with its ID, name, access
mode and, for readable fields, its current value or the status reported
by the device. Add -s <field>=<value> to write a field:

  # drm_ras -n <node-id>
  # drm_ras -n <node-id> -s <field>=<value>

Build-tested only, no telemetry device was available.

v3:
 - Align the field columns (Zack).
 - Return the error of a failed read, after listing all the fields (Zack).

Cc: Zack McKevitt <[email protected]>
Cc: Jeff Hugo <[email protected]>
Tested-by: Zack McKevitt <[email protected]>
Reviewed-by: Zack McKevitt <[email protected]>
Signed-off-by: Rodrigo Vivi <[email protected]>
---
 tools/drm_ras.c | 127 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 121 insertions(+), 6 deletions(-)

diff --git a/tools/drm_ras.c b/tools/drm_ras.c
index 0af1ca919..aae92f99a 100644
--- a/tools/drm_ras.c
+++ b/tools/drm_ras.c
@@ -5,6 +5,7 @@
 
 #include <errno.h>
 #include <getopt.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -14,6 +15,7 @@
 
 static const char * const node_types[] = {
        [DRM_RAS_NODE_TYPE_ERROR_COUNTER] = "error-counter",
+       [DRM_RAS_NODE_TYPE_TELEMETRY] = "telemetry",
 };
 
 #define name_of(names, i) \
@@ -38,6 +40,101 @@ static void print_error_counter(const struct 
igt_drm_ras_error_counter *counter,
        printf("%u\t%s\t%u\n", counter->id, counter->name, counter->value);
 }
 
+struct telemetry_fields {
+       struct igt_drm_ras_telemetry_field *field;
+       int count;
+};
+
+static void add_telemetry_field(const struct igt_drm_ras_telemetry_field 
*field, void *data)
+{
+       struct telemetry_fields *fields = data;
+       struct igt_drm_ras_telemetry_field *f;
+
+       f = realloc(fields->field, (fields->count + 1) * sizeof(*f));
+       if (!f) {
+               perror("realloc");
+               exit(EXIT_FAILURE);
+       }
+
+       fields->field = f;
+       fields->field[fields->count++] = *field;
+}
+
+static const char *field_mode(uint64_t mode)
+{
+       bool read = mode & DRM_RAS_TELEMETRY_FIELD_MODE_READ;
+       bool write = mode & DRM_RAS_TELEMETRY_FIELD_MODE_WRITE;
+
+       return read && write ? "rw" : read ? "r" : write ? "w" : "-";
+}
+
+static int show_telemetry(struct igt_drm_ras_nl_context *ctx, uint32_t id)
+{
+       struct telemetry_fields fields = {};
+       int64_t value, status;
+       int i, len, width = 0;
+       int ret;
+
+       ret = igt_drm_ras_nl_list_telemetry_fields(ctx, id, 
add_telemetry_field, &fields);
+       if (ret)
+               goto out;
+
+       for (i = 0; i < fields.count; i++) {
+               len = strlen(fields.field[i].name);
+               if (len > width)
+                       width = len;
+       }
+
+       for (i = 0; i < fields.count; i++) {
+               const struct igt_drm_ras_telemetry_field *f = &fields.field[i];
+               int err;
+
+               printf("%" PRIu64 "\t%-*s  %-2s", f->field, width, f->name, 
field_mode(f->mode));
+
+               if (f->mode & DRM_RAS_TELEMETRY_FIELD_MODE_READ) {
+                       err = igt_drm_ras_nl_telemetry_get(ctx, id, f->field, 
&value, &status);
+                       if (err) {
+                               printf("  %s", strerror(-err));
+                               ret = err;
+                       } else if (status) {
+                               printf("  status %" PRId64, status);
+                       } else {
+                               printf("  %" PRId64, value);
+                       }
+               }
+
+               printf("\n");
+       }
+
+out:
+       free(fields.field);
+
+       return ret;
+}
+
+static int set_telemetry(struct igt_drm_ras_nl_context *ctx, uint32_t id, 
const char *arg)
+{
+       int64_t value, status;
+       uint64_t field;
+       char *end;
+       int ret;
+
+       field = strtoull(arg, &end, 0);
+       if (end == arg || *end != '=')
+               return -EINVAL;
+
+       arg = end + 1;
+       value = strtoll(arg, &end, 0);
+       if (end == arg || *end)
+               return -EINVAL;
+
+       ret = igt_drm_ras_nl_telemetry_set(ctx, id, field, value, &status);
+       if (!ret && status)
+               printf("status %" PRId64 "\n", status);
+
+       return ret;
+}
+
 static int show_node(struct igt_drm_ras_nl_context *ctx, uint32_t id)
 {
        struct igt_drm_ras_node node = { .id = id };
@@ -52,6 +149,8 @@ static int show_node(struct igt_drm_ras_nl_context *ctx, 
uint32_t id)
                return -ENOENT;
        case DRM_RAS_NODE_TYPE_ERROR_COUNTER:
                return igt_drm_ras_nl_list_error_counters(ctx, id, 
print_error_counter, NULL);
+       case DRM_RAS_NODE_TYPE_TELEMETRY:
+               return show_telemetry(ctx, id);
        default:
                return -EOPNOTSUPP;
        }
@@ -59,28 +158,35 @@ static int show_node(struct igt_drm_ras_nl_context *ctx, 
uint32_t id)
 
 static void usage(const char *name)
 {
-       printf("Usage: %s [-n <node-id>]\n"
+       printf("Usage: %s [-n <node-id> [-s <field>=<value>]]\n"
               "List the DRM RAS nodes, or show the content of one node.\n"
-              "  -n, --node <node-id>  show the error counters of a node\n"
-              "  -h, --help            show this help\n", name);
+              "  -n, --node <node-id>         show the error counters or 
telemetry fields\n"
+              "                               of a node\n"
+              "  -s, --set <field>=<value>    write a telemetry field of the 
node\n"
+              "  -h, --help                   show this help\n", name);
 }
 
 int main(int argc, char **argv)
 {
        static const struct option opts[] = {
                { "node", required_argument, NULL, 'n' },
+               { "set", required_argument, NULL, 's' },
                { "help", no_argument, NULL, 'h' },
                { }
        };
        struct igt_drm_ras_nl_context ctx;
+       const char *set = NULL;
        long node = -1;
        int c, ret;
 
-       while ((c = getopt_long(argc, argv, "n:h", opts, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "n:s:h", opts, NULL)) != -1) {
                switch (c) {
                case 'n':
                        node = strtol(optarg, NULL, 0);
                        break;
+               case 's':
+                       set = optarg;
+                       break;
                case 'h':
                        usage(argv[0]);
                        return EXIT_SUCCESS;
@@ -90,11 +196,20 @@ int main(int argc, char **argv)
                }
        }
 
+       if (set && node < 0) {
+               usage(argv[0]);
+               return EXIT_FAILURE;
+       }
+
        if (igt_drm_ras_nl_init(&ctx))
                return EXIT_FAILURE;
 
-       ret = node < 0 ? igt_drm_ras_nl_list_nodes(&ctx, print_node, NULL) :
-                        show_node(&ctx, node);
+       if (set)
+               ret = set_telemetry(&ctx, node, set);
+       else if (node < 0)
+               ret = igt_drm_ras_nl_list_nodes(&ctx, print_node, NULL);
+       else
+               ret = show_node(&ctx, node);
        if (ret)
                fprintf(stderr, "%s\n", strerror(-ret));
        else if (ctx.dump_interrupted)
-- 
2.56.0

Reply via email to