ac/rgp: add ac_msgpack.h/c
authorYogesh Mohan Marimuthu <yogesh.mohanmarimuthu@amd.com>
Wed, 27 Jan 2021 09:36:59 +0000 (15:06 +0530)
committerMarge Bot <eric+marge@anholt.net>
Fri, 19 Feb 2021 14:29:46 +0000 (14:29 +0000)
This patch adds functions to create msgpack formatted data.
For msgpack specification refer to
github.com/msgpack/msgpack/blob/master/spec.md

This patch only adds formats from msgpac specification that
are required for rgp profile data.

v2: for newly added files, change copyright year from 2020 to 2021

Signed-off-by: Yogesh Mohan Marimuthu <yogesh.mohanmarimuthu@amd.com>
Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8609>

src/amd/Makefile.sources
src/amd/common/ac_msgpack.c [new file with mode: 0644]
src/amd/common/ac_msgpack.h [new file with mode: 0644]
src/amd/common/meson.build

index c4b74f5c7385c2323bf86d4afa724b645baeca84..a711ffb70a059d3677a9eba50e2b98fa11dba4c4 100644 (file)
@@ -42,6 +42,8 @@ AMD_COMMON_FILES = \
        common/ac_exp_param.h \
        common/ac_gpu_info.c \
        common/ac_gpu_info.h \
+       common/ac_msgpack.c \
+       common/ac_msgpack.h \
        common/ac_surface.c \
        common/ac_surface.h \
        common/ac_rgp.c \
diff --git a/src/amd/common/ac_msgpack.c b/src/amd/common/ac_msgpack.c
new file mode 100644 (file)
index 0000000..22a3882
--- /dev/null
@@ -0,0 +1,253 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+/**
+ * \file ac_msgpack.c
+ *
+ * This file provides functions to create msgpack formatted data.
+ * for msgpack specification refer to
+ * github.com/msgpack/msgpack/blob/master/spec.md
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include "util/u_math.h"
+#include "ac_msgpack.h"
+
+#define MSGPACK_MEM_START_SIZE 0x1000
+#define MSGPACK_MEM_INC_SIZE 0x1000
+
+#define MSGPACK_FIXMAP_OP 0x80
+#define MSGPACK_MAP16_OP 0xde
+#define MSGPACK_MAP32_OP 0xdf
+
+#define MSGPACK_FIXARRAY_OP 0x90
+#define MSGPACK_ARRAY16_OP 0xdc
+#define MSGPACK_ARRAY32_OP 0xdd
+
+#define MSGPACK_FIXSTR_OP 0xa0
+#define MSGPACK_STR8_OP 0xd9
+#define MSGPACK_STR16_OP 0xda
+#define MSGPACK_STR32_OP 0xdb
+
+#define MSGPACK_UINT8_OP 0xcc
+#define MSGPACK_UINT16_OP 0xcd
+#define MSGPACK_UINT32_OP 0xce
+#define MSGPACK_UINT64_OP 0xcf
+
+#define MSGPACK_NIL_OP 0xc0
+
+#define MSGPACK_INT8_OP 0xd0
+#define MSGPACK_INT16_OP 0xd1
+#define MSGPACK_INT32_OP 0xd2
+#define MSGPACK_INT64_OP 0xd3
+
+
+void ac_msgpack_init(struct ac_msgpack *msgpack)
+{
+   msgpack->mem = malloc(MSGPACK_MEM_START_SIZE);
+   msgpack->mem_size = MSGPACK_MEM_START_SIZE;
+   msgpack->offset = 0;
+}
+
+void ac_msgpack_destroy(struct ac_msgpack *msgpack)
+{
+   free(msgpack->mem);
+}
+
+int ac_msgpack_resize_if_required(struct ac_msgpack *msgpack,
+                                  uint32_t data_size)
+{
+   if ((msgpack->offset + data_size) > msgpack->mem_size) {
+      uint32_t new_mem_size;
+
+      new_mem_size = msgpack->mem_size +
+                     MAX2(MSGPACK_MEM_INC_SIZE, data_size);
+      msgpack->mem = realloc(msgpack->mem, new_mem_size);
+      if (msgpack->mem == NULL)
+         return false;
+
+      msgpack->mem_size = new_mem_size;
+   }
+
+   return true;
+}
+
+void ac_msgpack_add_fixmap_op(struct ac_msgpack *msgpack, uint32_t n)
+{
+   if (n <= 0xf ) {
+      if (!ac_msgpack_resize_if_required(msgpack, 1))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_FIXMAP_OP | n;
+      msgpack->offset = msgpack->offset + 1;
+   } else if (n <= 0xffff) {
+      if (!ac_msgpack_resize_if_required(msgpack, 3))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_MAP16_OP;
+      *((uint16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap16(n);
+      msgpack->offset = msgpack->offset + 3;
+   } else {
+      if (!ac_msgpack_resize_if_required(msgpack, 5))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_MAP32_OP;
+      *((unsigned int*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(n);
+      msgpack->offset = msgpack->offset + 5;
+   }
+}
+
+void ac_msgpack_add_fixarray_op(struct ac_msgpack *msgpack, uint32_t n)
+{
+   if (n <= 0xf ) {
+      if (!ac_msgpack_resize_if_required(msgpack, 1))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_FIXARRAY_OP | n;
+      msgpack->offset = msgpack->offset + 1;
+   } else if (n <= 0xffff) {
+      if (!ac_msgpack_resize_if_required(msgpack, 3))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_ARRAY16_OP;
+      *((uint16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap16(n);
+      msgpack->offset = msgpack->offset + 3;
+   } else {
+      if (!ac_msgpack_resize_if_required(msgpack, 5))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_ARRAY32_OP;
+      *((uint32_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(n);
+      msgpack->offset = msgpack->offset + 5;
+   }
+}
+
+void ac_msgpack_add_fixstr(struct ac_msgpack *msgpack, char *str)
+{
+   uint32_t n;
+
+   n = strlen(str);
+
+   if (n <= 0x1f) {
+      if (!ac_msgpack_resize_if_required(msgpack, 1 + n))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_FIXSTR_OP | n;
+      msgpack->offset = msgpack->offset + 1;
+   } else if (n <= 0xff) {
+      if (!ac_msgpack_resize_if_required(msgpack, 2 + n))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_STR8_OP;
+      msgpack->mem[msgpack->offset + 1] = n;
+      msgpack->offset = msgpack->offset + 2;
+   } else if (n <= 0xffff) {
+      if (!ac_msgpack_resize_if_required(msgpack, 3 + n))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_STR16_OP;
+      *((uint16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap16(n);
+      msgpack->offset = msgpack->offset + 3;
+   } else {
+      if (!ac_msgpack_resize_if_required(msgpack, 5 + n))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_STR32_OP;
+      *((uint32_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(n);
+      msgpack->offset = msgpack->offset + 5;
+   }
+
+   memcpy (&msgpack->mem[msgpack->offset], str, n);
+   msgpack->offset = msgpack->offset + n;
+}
+
+void ac_msgpack_add_uint(struct ac_msgpack *msgpack, uint64_t val)
+{
+   if (val <= 0x7f) {
+      if (!ac_msgpack_resize_if_required(msgpack, 1))
+         return;
+      msgpack->mem[msgpack->offset] = val;
+      msgpack->offset = msgpack->offset + 1;
+   } else if (val <= 0xff) {
+      if (!ac_msgpack_resize_if_required(msgpack, 2))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_UINT8_OP;
+      msgpack->mem[msgpack->offset + 1] = val;
+      msgpack->offset = msgpack->offset + 2;
+   } else if (val <= 0xffff) {
+      if (!ac_msgpack_resize_if_required(msgpack, 3))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_UINT16_OP;
+      *((uint16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap16(val);
+      msgpack->offset = msgpack->offset + 3;
+   } else if (val <= 0xffffffff) {
+      if (!ac_msgpack_resize_if_required(msgpack, 5))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_UINT32_OP;
+      *((uint32_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(val);
+      msgpack->offset = msgpack->offset + 5;
+   } else {
+      if (!ac_msgpack_resize_if_required(msgpack, 9))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_UINT64_OP;
+      *((uint64_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap64(val);
+      msgpack->offset = msgpack->offset + 9;
+   }
+}
+
+void ac_msgpack_add_int(struct ac_msgpack *msgpack, int64_t val)
+{
+   if ((val >= -0x7f) && (val <= 0x7f)) {
+      if ((val >= -31) && (val < 0)) {
+         if (!ac_msgpack_resize_if_required(msgpack, 1))
+            return;
+         msgpack->mem[msgpack->offset] = val | MSGPACK_NIL_OP;
+         msgpack->offset = msgpack->offset + 1;
+      } else if ((val >= 0) && (val <= 127)) {
+         if (!ac_msgpack_resize_if_required(msgpack, 1))
+            return;
+         msgpack->mem[msgpack->offset] = val;
+         msgpack->offset = msgpack->offset + 1;
+      } else {
+         if (!ac_msgpack_resize_if_required(msgpack, 2))
+            return;
+         msgpack->mem[msgpack->offset] = MSGPACK_INT8_OP;
+         msgpack->mem[msgpack->offset + 1] = val;
+         msgpack->offset = msgpack->offset + 2;
+      }
+   } else if ((val >= -0x7fff) && (val <= 0x7fff)) {
+      if (!ac_msgpack_resize_if_required(msgpack, 3))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_INT16_OP;
+      *((int16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(val);
+      msgpack->offset = msgpack->offset + 3;
+   } else if ((val >= -0x7fffffff) && (val <= 0x7fffffff)) {
+      if (!ac_msgpack_resize_if_required(msgpack, 5))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_INT32_OP;
+      *((int32_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(val);
+      msgpack->offset = msgpack->offset + 5;
+   } else {
+      if (!ac_msgpack_resize_if_required(msgpack, 9))
+         return;
+      msgpack->mem[msgpack->offset] = MSGPACK_INT64_OP;
+      *((int64_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap64(val);
+      msgpack->offset = msgpack->offset + 9;
+   }
+}
diff --git a/src/amd/common/ac_msgpack.h b/src/amd/common/ac_msgpack.h
new file mode 100644 (file)
index 0000000..9857dbc
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2021 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef AC_MSGPACK_H
+#define AC_MSGPACK_H
+
+struct ac_msgpack {
+   uint8_t *mem;
+   uint32_t mem_size;
+   uint32_t offset;
+};
+
+void ac_msgpack_init(struct ac_msgpack *msgpack);
+void ac_msgpack_destroy(struct ac_msgpack *msgpack);
+int ac_msgpack_resize_if_required(struct ac_msgpack *msgpack,
+                                  uint32_t data_size);
+void ac_msgpack_add_fixmap_op(struct ac_msgpack *msgpack, uint32_t n);
+void ac_msgpack_add_fixarray_op(struct ac_msgpack *msgpack, uint32_t n);
+void ac_msgpack_add_fixstr(struct ac_msgpack *msgpack, char *str);
+void ac_msgpack_add_uint(struct ac_msgpack *msgpack, uint64_t val);
+void ac_msgpack_add_int(struct ac_msgpack *msgpack, int64_t val);
+
+#endif
index ad0df2a97d20a480942a6bdfba36b56b37384439..7e1d9d5849e4c06722245c57ce83f9589732634b 100644 (file)
@@ -84,6 +84,8 @@ amd_common_files = files(
   'ac_sqtt.h',
   'ac_rgp.c',
   'ac_rgp.h',
+  'ac_msgpack.c',
+  'ac_msgpack.h',
 )
 
 libamd_common = static_library(