Add igt_drm_ras_nl_list_telemetry_fields(), igt_drm_ras_nl_telemetry_get()
and igt_drm_ras_nl_telemetry_set() for telemetry nodes. The request
helper is split so that requests can carry more than the node ID.

v3:
 - Move nla_get_sint_any() next to nla_get_uint_any() (Zack).
 - Initialize the reply value and status before the request (Zack).

Cc: Zack McKevitt <[email protected]>
Cc: Jeff Hugo <[email protected]>
Reviewed-by: Zack McKevitt <[email protected]>
Signed-off-by: Rodrigo Vivi <[email protected]>
---
 lib/igt_drm_ras_nl.c | 199 ++++++++++++++++++++++++++++++++++++++++---
 lib/igt_drm_ras_nl.h |  21 +++++
 2 files changed, 207 insertions(+), 13 deletions(-)

diff --git a/lib/igt_drm_ras_nl.c b/lib/igt_drm_ras_nl.c
index 282648f55..177d2bc88 100644
--- a/lib/igt_drm_ras_nl.c
+++ b/lib/igt_drm_ras_nl.c
@@ -661,6 +661,17 @@ int igt_drm_ras_nl_wait_for_error_report_event(struct 
igt_drm_ras_nl_context *ct
        return 0;
 }
 
+static uint64_t nla_get_uint_any(const struct nlattr *attr)
+{
+       return nla_len(attr) == sizeof(uint32_t) ? nla_get_u32(attr) : 
nla_get_u64(attr);
+}
+
+static int64_t nla_get_sint_any(const struct nlattr *attr)
+{
+       return nla_len(attr) == sizeof(int32_t) ? (int32_t)nla_get_u32(attr) :
+                                                 (int64_t)nla_get_u64(attr);
+}
+
 struct request_state {
        struct igt_drm_ras_nl_context *ctx;
        bool done;
@@ -699,24 +710,35 @@ static int request_dump_intr_cb(struct nl_msg *msg, void 
*arg)
        return NL_OK;
 }
 
-/* Send @cmd and feed each reply to @parse until the reply, dump or ACK 
completes */
-static int request(struct igt_drm_ras_nl_context *ctx, uint8_t cmd, int flags,
-                  int node_attr, uint32_t node_id, nl_recvmsg_msg_cb_t parse, 
void *arg)
+static int request_alloc(struct igt_drm_ras_nl_context *ctx, uint8_t cmd, int 
flags,
+                        struct nl_msg **msg)
+{
+       if (!ctx || !ctx->sock || ctx->family_id < 0)
+               return -EINVAL;
+
+       *msg = nlmsg_alloc();
+       if (!*msg)
+               return -ENOMEM;
+
+       if (!genlmsg_put(*msg, NL_AUTO_PORT, NL_AUTO_SEQ, ctx->family_id, 0,
+                        flags | NLM_F_ACK, cmd, DRM_RAS_FAMILY_VERSION)) {
+               nlmsg_free(*msg);
+               return -ENOMEM;
+       }
+
+       return 0;
+}
+
+/* Send @msg and feed each reply to @parse until the reply, dump or ACK 
completes */
+static int request_send(struct igt_drm_ras_nl_context *ctx, struct nl_msg *msg,
+                       nl_recvmsg_msg_cb_t parse, void *arg)
 {
        struct request_state st = { .ctx = ctx };
-       struct nl_msg *msg;
        struct nl_cb *cb;
        int ret;
 
-       if (!ctx || !ctx->sock || ctx->family_id < 0)
-               return -EINVAL;
-
-       msg = nlmsg_alloc();
        cb = nl_cb_alloc(NL_CB_DEFAULT);
-       if (!msg || !cb ||
-           !genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, ctx->family_id, 0,
-                        flags | NLM_F_ACK, cmd, DRM_RAS_FAMILY_VERSION) ||
-           (node_attr && nla_put_u32(msg, node_attr, node_id))) {
+       if (!cb) {
                ret = -ENOMEM;
                goto out;
        }
@@ -734,12 +756,30 @@ static int request(struct igt_drm_ras_nl_context *ctx, 
uint8_t cmd, int flags,
                ret = nl_recvmsgs(ctx->sock, cb);
 
        ret = st.error ?: (ret < 0 ? normalize_nl_error(ret) : 0);
+       nl_cb_put(cb);
 out:
        nlmsg_free(msg);
-       nl_cb_put(cb);
        return ret;
 }
 
+static int request(struct igt_drm_ras_nl_context *ctx, uint8_t cmd, int flags,
+                  int node_attr, uint32_t node_id, nl_recvmsg_msg_cb_t parse, 
void *arg)
+{
+       struct nl_msg *msg;
+       int ret;
+
+       ret = request_alloc(ctx, cmd, flags, &msg);
+       if (ret)
+               return ret;
+
+       if (node_attr && nla_put_u32(msg, node_attr, node_id)) {
+               nlmsg_free(msg);
+               return -ENOMEM;
+       }
+
+       return request_send(ctx, msg, parse, arg);
+}
+
 struct node_list {
        igt_drm_ras_node_fn fn;
        void *data;
@@ -829,3 +869,136 @@ int igt_drm_ras_nl_list_error_counters(struct 
igt_drm_ras_nl_context *ctx, uint3
        return request(ctx, DRM_RAS_CMD_GET_ERROR_COUNTER, NLM_F_DUMP,
                       DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID, node_id, 
error_counter_cb, &list);
 }
+
+struct telemetry_field_list {
+       igt_drm_ras_telemetry_field_fn fn;
+       void *data;
+};
+
+static int telemetry_field_cb(struct nl_msg *msg, void *arg)
+{
+       struct nlattr *tb[DRM_RAS_A_TELEMETRY_ATTRS_MAX + 1];
+       struct telemetry_field_list *list = arg;
+       struct igt_drm_ras_telemetry_field field = {};
+
+       if (genlmsg_parse(nlmsg_hdr(msg), 0, tb, DRM_RAS_A_TELEMETRY_ATTRS_MAX, 
NULL) ||
+           !tb[DRM_RAS_A_TELEMETRY_ATTRS_FIELD])
+               return NL_SKIP;
+
+       field.field = nla_get_uint_any(tb[DRM_RAS_A_TELEMETRY_ATTRS_FIELD]);
+       if (tb[DRM_RAS_A_TELEMETRY_ATTRS_FIELD_NAME])
+               nla_strlcpy(field.name, 
tb[DRM_RAS_A_TELEMETRY_ATTRS_FIELD_NAME],
+                           sizeof(field.name));
+       if (tb[DRM_RAS_A_TELEMETRY_ATTRS_MODE])
+               field.mode = 
nla_get_uint_any(tb[DRM_RAS_A_TELEMETRY_ATTRS_MODE]);
+
+       list->fn(&field, list->data);
+
+       return NL_OK;
+}
+
+/**
+ * igt_drm_ras_nl_list_telemetry_fields - Call @fn for each telemetry field of 
a node
+ * @ctx: initialized DRM RAS netlink context
+ * @node_id: telemetry node ID
+ * @fn: called once per field
+ * @data: passed to @fn
+ *
+ * Returns: 0 on success, negative errno otherwise.
+ */
+int igt_drm_ras_nl_list_telemetry_fields(struct igt_drm_ras_nl_context *ctx, 
uint32_t node_id,
+                                        igt_drm_ras_telemetry_field_fn fn, 
void *data)
+{
+       struct telemetry_field_list list = { fn, data };
+
+       return request(ctx, DRM_RAS_CMD_TELEMETRY_LIST_FIELDS, NLM_F_DUMP,
+                      DRM_RAS_A_TELEMETRY_ATTRS_NODE_ID, node_id, 
telemetry_field_cb, &list);
+}
+
+struct telemetry_reply {
+       int64_t *value;
+       int64_t *status;
+};
+
+static int telemetry_reply_cb(struct nl_msg *msg, void *arg)
+{
+       struct nlattr *tb[DRM_RAS_A_TELEMETRY_ATTRS_MAX + 1];
+       struct telemetry_reply *reply = arg;
+
+       if (genlmsg_parse(nlmsg_hdr(msg), 0, tb, DRM_RAS_A_TELEMETRY_ATTRS_MAX, 
NULL))
+               return NL_SKIP;
+
+       if (reply->value && tb[DRM_RAS_A_TELEMETRY_ATTRS_VALUE])
+               *reply->value = 
nla_get_sint_any(tb[DRM_RAS_A_TELEMETRY_ATTRS_VALUE]);
+       if (reply->status && tb[DRM_RAS_A_TELEMETRY_ATTRS_STATUS])
+               *reply->status = 
nla_get_sint_any(tb[DRM_RAS_A_TELEMETRY_ATTRS_STATUS]);
+
+       return NL_OK;
+}
+
+static int telemetry_request(struct igt_drm_ras_nl_context *ctx, uint8_t cmd, 
uint32_t node_id,
+                            uint64_t field, const int64_t *value, struct 
telemetry_reply *reply)
+{
+       struct nl_msg *msg;
+       int ret;
+
+       if (reply->value)
+               *reply->value = 0;
+       if (reply->status)
+               *reply->status = -1;
+
+       ret = request_alloc(ctx, cmd, 0, &msg);
+       if (ret)
+               return ret;
+
+       if (nla_put_u32(msg, DRM_RAS_A_TELEMETRY_ATTRS_NODE_ID, node_id))
+               goto err;
+
+       if (nla_put_u64(msg, DRM_RAS_A_TELEMETRY_ATTRS_FIELD, field))
+               goto err;
+
+       if (value && nla_put_u64(msg, DRM_RAS_A_TELEMETRY_ATTRS_VALUE, *value))
+               goto err;
+
+       return request_send(ctx, msg, telemetry_reply_cb, reply);
+
+err:
+       nlmsg_free(msg);
+       return -ENOMEM;
+}
+
+/**
+ * igt_drm_ras_nl_telemetry_get - Read a telemetry field
+ * @ctx: initialized DRM RAS netlink context
+ * @node_id: telemetry node ID
+ * @field: telemetry field to read
+ * @value: set to the value read, only meaningful when @status is 0
+ * @status: set to the status reported by the device, -1 if not reported
+ *
+ * Returns: 0 if the read was performed, negative errno otherwise.
+ */
+int igt_drm_ras_nl_telemetry_get(struct igt_drm_ras_nl_context *ctx, uint32_t 
node_id,
+                                uint64_t field, int64_t *value, int64_t 
*status)
+{
+       struct telemetry_reply reply = { value, status };
+
+       return telemetry_request(ctx, DRM_RAS_CMD_TELEMETRY_GET, node_id, 
field, NULL, &reply);
+}
+
+/**
+ * igt_drm_ras_nl_telemetry_set - Write a telemetry field
+ * @ctx: initialized DRM RAS netlink context
+ * @node_id: telemetry node ID
+ * @field: telemetry field to write
+ * @value: value to write
+ * @status: set to the status reported by the device, -1 if not reported
+ *
+ * Returns: 0 if the write was performed, negative errno otherwise.
+ */
+int igt_drm_ras_nl_telemetry_set(struct igt_drm_ras_nl_context *ctx, uint32_t 
node_id,
+                                uint64_t field, int64_t value, int64_t *status)
+{
+       struct telemetry_reply reply = { NULL, status };
+
+       return telemetry_request(ctx, DRM_RAS_CMD_TELEMETRY_SET, node_id, 
field, &value, &reply);
+}
diff --git a/lib/igt_drm_ras_nl.h b/lib/igt_drm_ras_nl.h
index 4980904f8..1b2a8f164 100644
--- a/lib/igt_drm_ras_nl.h
+++ b/lib/igt_drm_ras_nl.h
@@ -95,9 +95,23 @@ struct igt_drm_ras_error_counter {
        uint32_t value;
 };
 
+/**
+ * struct igt_drm_ras_telemetry_field - DRM RAS telemetry field
+ */
+struct igt_drm_ras_telemetry_field {
+       /** @field: Field ID, as defined by the device uAPI */
+       uint64_t field;
+       /** @name: Field name */
+       char name[IGT_DRM_RAS_NAME_LEN];
+       /** @mode: Supported access modes (enum drm_ras_telemetry_field_mode 
bits) */
+       uint64_t mode;
+};
+
 typedef void (*igt_drm_ras_node_fn)(const struct igt_drm_ras_node *node, void 
*data);
 typedef void (*igt_drm_ras_error_counter_fn)(const struct 
igt_drm_ras_error_counter *counter,
                                             void *data);
+typedef void (*igt_drm_ras_telemetry_field_fn)(const struct 
igt_drm_ras_telemetry_field *field,
+                                              void *data);
 
 void igt_drm_ras_nl_cleanup(struct igt_drm_ras_nl_context *ctx);
 int igt_drm_ras_nl_init(struct igt_drm_ras_nl_context *ctx);
@@ -112,4 +126,11 @@ int igt_drm_ras_nl_list_nodes(struct 
igt_drm_ras_nl_context *ctx,
 int igt_drm_ras_nl_list_error_counters(struct igt_drm_ras_nl_context *ctx, 
uint32_t node_id,
                                       igt_drm_ras_error_counter_fn fn, void 
*data);
 
+int igt_drm_ras_nl_list_telemetry_fields(struct igt_drm_ras_nl_context *ctx, 
uint32_t node_id,
+                                        igt_drm_ras_telemetry_field_fn fn, 
void *data);
+int igt_drm_ras_nl_telemetry_get(struct igt_drm_ras_nl_context *ctx, uint32_t 
node_id,
+                                uint64_t field, int64_t *value, int64_t 
*status);
+int igt_drm_ras_nl_telemetry_set(struct igt_drm_ras_nl_context *ctx, uint32_t 
node_id,
+                                uint64_t field, int64_t value, int64_t 
*status);
+
 #endif /* IGT_DRM_RAS_NL_H */
-- 
2.56.0

Reply via email to