From 5598b9356cb6edb05cece95231eb1db3426bf59e Mon Sep 17 00:00:00 2001 From: Michal Wajdeczko Date: Wed, 8 Jul 2026 00:08:08 +0200 Subject: [PATCH] drm/xe/guc: Add KLV parsing helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We have already introduced a helper to encode larger objects. Now add helper to parse the KLVs buffer. We will use it shortly. Signed-off-by: Michal Wajdeczko Reviewed-by: MichaƂ Winiarski Link: https://patch.msgid.link/20260707220816.677-7-michal.wajdeczko@intel.com --- drivers/gpu/drm/xe/xe_guc_klv_helpers.c | 38 +++++++++++++++++++++++++ drivers/gpu/drm/xe/xe_guc_klv_helpers.h | 3 ++ 2 files changed, 41 insertions(+) diff --git a/drivers/gpu/drm/xe/xe_guc_klv_helpers.c b/drivers/gpu/drm/xe/xe_guc_klv_helpers.c index 54d977252431..7ad0b6f88f4c 100644 --- a/drivers/gpu/drm/xe/xe_guc_klv_helpers.c +++ b/drivers/gpu/drm/xe/xe_guc_klv_helpers.c @@ -319,3 +319,41 @@ u32 *xe_guc_klv_encode_object(u32 *klvs, u32 avail, u16 key, const void *obj, *klvs = PREP_GUC_KLV(key, end - (klvs + GUC_KLV_LEN_MIN)); return end; } + +/** + * xe_guc_klv_parser() - Parse and decode stream of KLVs. + * @klvs: the buffer with KLVs + * @num_dwords: number of dwords (u32) available in the buffer + * @obj: opaque pointer to be used by the @decoder function + * @decoder: pointer to the decoder function + * + * Return: The sum of all results returned by the decoder or + * an -errno on decoder or buffer failure. + */ +int xe_guc_klv_parser(const u32 *klvs, u32 num_dwords, void *obj, + int (*decoder)(void *obj, u16 key, u16 len, const u32 *value)) +{ + int total = 0; + int ret; + + while (num_dwords >= GUC_KLV_LEN_MIN) { + u16 key = FIELD_GET(GUC_KLV_0_KEY, klvs[0]); + u16 len = FIELD_GET(GUC_KLV_0_LEN, klvs[0]); + + klvs += GUC_KLV_LEN_MIN; + num_dwords -= GUC_KLV_LEN_MIN; + + if (num_dwords < len) + return -ENODATA; + + ret = decoder(obj, key, len, klvs); + if (ret < 0) + return ret; + total += ret; + + klvs += len; + num_dwords -= len; + } + + return total; +} diff --git a/drivers/gpu/drm/xe/xe_guc_klv_helpers.h b/drivers/gpu/drm/xe/xe_guc_klv_helpers.h index 855805f4463d..cf3b29adc9d6 100644 --- a/drivers/gpu/drm/xe/xe_guc_klv_helpers.h +++ b/drivers/gpu/drm/xe/xe_guc_klv_helpers.h @@ -23,6 +23,9 @@ u32 *xe_guc_klv_encode_string(u32 *klvs, u32 avail, u16 key, const char *s); u32 *xe_guc_klv_encode_object(u32 *klvs, u32 avail, u16 key, const void *obj, u32 *(*encoder)(u32 *klvs, u32 avail, const void *obj)); +int xe_guc_klv_parser(const u32 *klvs, u32 num_dwords, void *obj, + int (*decoder)(void *obj, u16 key, u16 len, const u32 *value)); + /** * PREP_GUC_KLV - Prepare KLV header value based on provided key and len. * @key: KLV key