From 27ff46bde66bc8a7f6f3c5d4844c946fe8c4a04b Mon Sep 17 00:00:00 2001 From: Yogesh Mohan Marimuthu Date: Wed, 27 Jan 2021 15:06:59 +0530 Subject: [PATCH] ac/rgp: add ac_msgpack.h/c 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 Reviewed-by: Samuel Pitoiset Acked-by: Pierre-Eric Pelloux-Prayer Part-of: --- src/amd/Makefile.sources | 2 + src/amd/common/ac_msgpack.c | 253 ++++++++++++++++++++++++++++++++++++ src/amd/common/ac_msgpack.h | 45 +++++++ src/amd/common/meson.build | 2 + 4 files changed, 302 insertions(+) create mode 100644 src/amd/common/ac_msgpack.c create mode 100644 src/amd/common/ac_msgpack.h diff --git a/src/amd/Makefile.sources b/src/amd/Makefile.sources index c4b74f5c738..a711ffb70a0 100644 --- a/src/amd/Makefile.sources +++ b/src/amd/Makefile.sources @@ -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 index 00000000000..22a3882348d --- /dev/null +++ b/src/amd/common/ac_msgpack.c @@ -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 +#include +#include +#include +#include +#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 index 00000000000..9857dbc6f16 --- /dev/null +++ b/src/amd/common/ac_msgpack.h @@ -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 diff --git a/src/amd/common/meson.build b/src/amd/common/meson.build index ad0df2a97d2..7e1d9d5849e 100644 --- a/src/amd/common/meson.build +++ b/src/amd/common/meson.build @@ -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(