From patchwork Mon Nov 1 22:47:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Leach X-Patchwork-Id: 12597511 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F81AC433EF for ; Mon, 1 Nov 2021 22:49:22 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F047960EDF for ; Mon, 1 Nov 2021 22:49:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org F047960EDF Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KDX4fb35enD/YVzG9JMMBLMly3ROXtlWyZdCP29L2qA=; b=bB9XALnLZsfjT/ F78oG83zR+9yYn6YLFaYZbho1D5Eh/zWWu4KteU/d4gQzsyMKYIdPup/axcPEK7k5RB1/EHDeQoNZ yZh3QLTHR0HECZlS8F5oSUNB1bIrq8GNFoOMABc5eLEgi5c4f9UIaWZAa8+iKZSol3c/W4rtMy9rf PNvYnQPRoKufhfbICYfZddTn5hn6wmNBgu84zcEdtL+RodrNQZUkwq61jCwW8eR+eMX8pIhkRMtmj 8x8XiK1L1lUtIRODUqe1ccmwqTplOaY+2JdEq/kb3YCUJOdcfMqLI/qM8mdls5O54t49+FFSNtSCD mlLJfBB93hSzOui0HqjA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mhg5y-00HRaZ-BM; Mon, 01 Nov 2021 22:48:02 +0000 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mhg5Z-00HRVq-4d for linux-arm-kernel@lists.infradead.org; Mon, 01 Nov 2021 22:47:40 +0000 Received: by mail-wr1-x42c.google.com with SMTP id o14so30306742wra.12 for ; Mon, 01 Nov 2021 15:47:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZnJC6HDO4hKLnjoDe3yF52L4cyf5HBXuoo8iYrPGqxk=; b=g1JU4d0ZZgYReuLo1r1rs+vQSBa0naVHcblGIlO9p568/vtbAvwi99VvzORL0gZYrT SXwh2zhFmFmN+2ZR/DYaVYTdbkmylkl5WptBZqOJ1Y3GK7zxI0YsCTB2hJLC7rc3m4ja Bl5Brhs+rBrKH6FznpK17RkoVNzxauVWKVwhhFruU9ptQu2Vlglxq9bH9aoWXYJ+1JG1 mi1ZWji2pBiTU/PfUDTgOP414ysGELacucHpzJO2Xu8tilXzbr5SrwFN3f/f9YmMgZ03 bC+RaFNJ0KhfBNwAvMoEx3q2n0/CmvBe4dRRMm/BoKCky0Ngh1EbagJMb14cbe/0HtJo Q87Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZnJC6HDO4hKLnjoDe3yF52L4cyf5HBXuoo8iYrPGqxk=; b=5cZtvBqaSCnXPX2s/GvE8DS72F6asrpowrmiW/FbZlTPTqvy3kfT1BkfU8xp/rcj+C 6Ilc+Rt5Yx2gRDxUAy66n0wLQGk0nhADzgRzIX4VshfgWASENfZ9ZFlOU1+50zjFKps6 UaiRLoTUI1vnQuxtdLchrNNm6Q9WT5zUQfOcAOAHef1ad+/76Qt99Dnwcr6KV+hDKOS/ 2ypwA7EZFgtIgY1+sCZshUrVJSkZRSc4nLYtA35gJsouLR33v31WVGJnDFdDj8d6bilE UIbcoqmNjSIX5MdRTO0ztLrWCmWxdPFsKZ6tPyZum27JFzIYPMQrDsBFTf8USzv+To6k nFWw== X-Gm-Message-State: AOAM5331GD7/3Y7XsGxNu1y6ht6Tg0DdK9S7DMPJkM0ME4nr4W5kMGXR v6ONq0on10MsE44DRQFSy7y+W369I2+mgA== X-Google-Smtp-Source: ABdhPJyC9R8v6NZJN6N3rZS79CuRZCvccTIstRdhkApOo3igdBxkyAhPXNNjA7SNOyR81XP8lQZ8Hg== X-Received: by 2002:adf:fa8f:: with SMTP id h15mr42245281wrr.323.1635806855660; Mon, 01 Nov 2021 15:47:35 -0700 (PDT) Received: from linaro.org ([2a00:23c5:6809:2201:cd6d:75aa:3402:e4a]) by smtp.gmail.com with ESMTPSA id 10sm858647wme.27.2021.11.01.15.47.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Nov 2021 15:47:35 -0700 (PDT) From: Mike Leach To: coresight@lists.linaro.org Cc: linux-arm-kernel@lists.infradead.org, mathieu.poirier@linaro.org, suzuki.poulose@arm.com, leo.yan@linaro.org, Mike Leach Subject: [PATCH 3/6] coresight: configfs: Modify config files to allow userspace use Date: Mon, 1 Nov 2021 22:47:28 +0000 Message-Id: <20211101224731.27870-4-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211101224731.27870-1-mike.leach@linaro.org> References: <20211101224731.27870-1-mike.leach@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211101_154737_209683_F2344F7A X-CRM114-Status: GOOD ( 28.36 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Update coresight-config.h and the coresight-config-file.c & .h to allow use in userspace programs. Use __KERNEL__ defines to filter out driver only structures and elements so that user space programs can use the descriptor structures. Abstract memory allocation in coresight-config-file.c to allow read file functions to be run in userspace and kernel drivers. Signed-off-by: Mike Leach --- .../coresight/coresight-config-file.c | 96 +++++++++++++------ .../coresight/coresight-config-file.h | 33 +++++++ .../hwtracing/coresight/coresight-config.h | 21 ++++ 3 files changed, 122 insertions(+), 28 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-config-file.c b/drivers/hwtracing/coresight/coresight-config-file.c index 3fd001938324..77a64b54280d 100644 --- a/drivers/hwtracing/coresight/coresight-config-file.c +++ b/drivers/hwtracing/coresight/coresight-config-file.c @@ -4,10 +4,58 @@ * Author: Mike Leach */ -#include "coresight-config.h" #include "coresight-config-file.h" + +/* + * To allow reuse of this source in tools, define memory allocation fns according + * to build environment. + */ + +#ifdef __KERNEL__ #include "coresight-syscfg.h" +static void *cscfg_calloc(size_t num, size_t size) +{ + return devm_kcalloc(cscfg_device(), num, size, GFP_KERNEL); +} + +static char *cscfg_strdup(const char *str) +{ + return devm_kstrdup(cscfg_device(), str, GFP_KERNEL); +} + +static void *cscfg_zalloc(size_t size) +{ + return devm_kzalloc(cscfg_device(), size, GFP_KERNEL); +} + +#else + +#include +#include +#include + +static void *cscfg_calloc(size_t num, size_t size) +{ + return calloc(num, size); +} + +static char *cscfg_strdup(const char *str) +{ + return strdup(str); +} + +static void *cscfg_zalloc(size_t size) +{ + void *ptr = malloc(size); + + if (ptr) + memset(ptr, 0, size); + return ptr; +} + +#endif + #define cscfg_extract_u64(val64) { \ val64 = *(u64 *)(buffer + used); \ used += sizeof(u64); \ @@ -80,6 +128,7 @@ static int cscfg_file_read_elem_str(const u8 *buffer, const int buflen, int *buf struct cscfg_file_elem_str *elem_str) { int used = *buf_used; + const u8 *str; if ((buflen - used) < sizeof(u16)) return -EINVAL; @@ -89,11 +138,13 @@ static int cscfg_file_read_elem_str(const u8 *buffer, const int buflen, int *buf if ((buflen - used) < elem_str->str_len) return -EINVAL; + str = buffer + used; + /* check for 0 termination */ - if (buffer[elem_str->str_len - 1] != 0) + if (str[elem_str->str_len - 1] != 0) return -EINVAL; - elem_str->str = devm_kstrdup(cscfg_device(), (char *)buffer, GFP_KERNEL); + elem_str->str = cscfg_strdup((char *)str); used += elem_str->str_len; *buf_used = used; @@ -104,14 +155,12 @@ static int cscfg_file_alloc_desc_arrays(struct cscfg_fs_load_descs *desc_arrays, int nr_features) { /* arrays are 0 terminated - max of 1 config & nr_features features */ - desc_arrays->config_descs = devm_kcalloc(cscfg_device(), 2, - sizeof(struct cscfg_config_desc *), - GFP_KERNEL); + desc_arrays->config_descs = cscfg_calloc(2, sizeof(struct cscfg_config_desc *)); if (!desc_arrays->config_descs) return -ENOMEM; - desc_arrays->feat_descs = devm_kcalloc(cscfg_device(), nr_features + 1, - sizeof(struct cscfg_feature_desc *), - GFP_KERNEL); + + desc_arrays->feat_descs = cscfg_calloc(nr_features + 1, + sizeof(struct cscfg_feature_desc *)); if (!desc_arrays->feat_descs) return -ENOMEM; return 0; @@ -138,8 +187,7 @@ static int cscfg_file_read_elem_config(const u8 *buffer, const int buflen, int * return 0; /* we have a config - allocate the descriptor */ - config_desc = devm_kzalloc(cscfg_device(), sizeof(struct cscfg_config_desc), - GFP_KERNEL); + config_desc = cscfg_zalloc(sizeof(struct cscfg_config_desc)); if (!config_desc) return -ENOMEM; @@ -165,8 +213,7 @@ static int cscfg_file_read_elem_config(const u8 *buffer, const int buflen, int * /* read the array of 64bit presets if present */ nr_preset_vals = config_desc->nr_total_params * config_desc->nr_presets; if (nr_preset_vals) { - presets = devm_kcalloc(cscfg_device(), nr_preset_vals, - sizeof(u64), GFP_KERNEL); + presets = cscfg_calloc(nr_preset_vals, sizeof(u64)); if (!presets) return -ENOMEM; @@ -181,10 +228,8 @@ static int cscfg_file_read_elem_config(const u8 *buffer, const int buflen, int * /* read the array of feature names referenced by the config */ if (config_desc->nr_feat_refs) { - config_desc->feat_ref_names = devm_kcalloc(cscfg_device(), - config_desc->nr_feat_refs, - sizeof(char *), - GFP_KERNEL); + config_desc->feat_ref_names = cscfg_calloc(config_desc->nr_feat_refs, + sizeof(char *)); if (!config_desc->feat_ref_names) return -ENOMEM; @@ -262,8 +307,7 @@ static int cscfg_file_read_elem_feature(const u8 *buffer, const int buflen, int u32 val32; /* allocate the feature descriptor object */ - feat_desc = devm_kzalloc(cscfg_device(), sizeof(struct cscfg_feature_desc), - GFP_KERNEL); + feat_desc = cscfg_zalloc(sizeof(struct cscfg_feature_desc)); if (!feat_desc) return -ENOMEM; @@ -302,10 +346,8 @@ static int cscfg_file_read_elem_feature(const u8 *buffer, const int buflen, int nr_regs_bytes = ((sizeof(u32) + sizeof(u64)) * feat_desc->nr_regs); if ((buflen - used) < nr_regs_bytes) return -EINVAL; - feat_desc->regs_desc = devm_kcalloc(cscfg_device(), - feat_desc->nr_regs, - sizeof(struct cscfg_regval_desc), - GFP_KERNEL); + feat_desc->regs_desc = cscfg_calloc(feat_desc->nr_regs, + sizeof(struct cscfg_regval_desc)); if (!feat_desc->regs_desc) return -ENOMEM; @@ -319,10 +361,8 @@ static int cscfg_file_read_elem_feature(const u8 *buffer, const int buflen, int /* parameter descriptors - string + 64 bit value */ if (feat_desc->nr_params) { - feat_desc->params_desc = devm_kcalloc(cscfg_device(), - feat_desc->nr_params, - sizeof(struct cscfg_parameter_desc), - GFP_KERNEL); + feat_desc->params_desc = cscfg_calloc(feat_desc->nr_params, + sizeof(struct cscfg_parameter_desc)); if (!feat_desc->params_desc) return -ENOMEM; for (i = 0; i < feat_desc->nr_params; i++) { @@ -399,7 +439,7 @@ int cscfg_file_read_buffer(const u8 *buffer, const int buflen, if (err) return err; } - return used; + return 0; } int cscfg_file_read_buffer_first_name(const u8 *buffer, const int buflen, diff --git a/drivers/hwtracing/coresight/coresight-config-file.h b/drivers/hwtracing/coresight/coresight-config-file.h index 03899f7d94c9..04c365e5109b 100644 --- a/drivers/hwtracing/coresight/coresight-config-file.h +++ b/drivers/hwtracing/coresight/coresight-config-file.h @@ -7,6 +7,39 @@ #ifndef _CORESIGHT_CORESIGHT_CONFIG_FILE_H #define _CORESIGHT_CORESIGHT_CONFIG_FILE_H +#include + +#ifndef __KERNEL__ +/* + * allow the user space programs to include the coresight config headers + * to use the _desc structures, and reuse the read code + */ +#ifndef u8 +typedef __u8 u8; +typedef __u16 u16; +typedef __u32 u32; +typedef __u64 u64; +#endif + +/* ARRAY SIZE is useful too */ +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#endif + +/* define EINVAL for user space */ +#ifndef EINVAL +#define EINVAL 22 +#endif + +#ifndef ENOMEM +#define ENOMEM 12 +#endif + +#endif + +#include "coresight-config.h" + + /* * Structures to represent configuration descriptors in a memory buffer * to serialise to and from files diff --git a/drivers/hwtracing/coresight/coresight-config.h b/drivers/hwtracing/coresight/coresight-config.h index 770986316bc2..2136393df710 100644 --- a/drivers/hwtracing/coresight/coresight-config.h +++ b/drivers/hwtracing/coresight/coresight-config.h @@ -7,7 +7,14 @@ #ifndef _CORESIGHT_CORESIGHT_CONFIG_H #define _CORESIGHT_CORESIGHT_CONFIG_H +/* + * Filter out kernel only portions of the file to allow user space programs + * to use the descriptor definitions. + */ +#ifdef __KERNEL__ #include +#endif + #include /* CoreSight Configuration Management - component and system wide configuration */ @@ -103,14 +110,18 @@ struct cscfg_regval_desc { struct cscfg_feature_desc { const char *name; const char *description; +#ifdef __KERNEL__ struct list_head item; +#endif u32 match_flags; int nr_params; struct cscfg_parameter_desc *params_desc; int nr_regs; struct cscfg_regval_desc *regs_desc; +#ifdef __KERNEL__ void *load_owner; struct config_group *fs_group; +#endif }; /** @@ -138,16 +149,20 @@ struct cscfg_feature_desc { struct cscfg_config_desc { const char *name; const char *description; +#ifdef __KERNEL__ struct list_head item; +#endif int nr_feat_refs; const char **feat_ref_names; int nr_presets; int nr_total_params; const u64 *presets; /* nr_presets * nr_total_params */ +#ifdef __KERNEL__ struct dev_ext_attribute *event_ea; atomic_t active_cnt; void *load_owner; struct config_group *fs_group; +#endif }; /** @@ -162,11 +177,16 @@ struct cscfg_config_desc { * @feat_descs: array of feature descriptor pointers. */ struct cscfg_fs_load_descs { +#ifdef __KERNEL__ void *owner_info; +#endif struct cscfg_config_desc **config_descs; struct cscfg_feature_desc **feat_descs; }; +/* remainder of header is used by the kernel drivers only */ +#ifdef __KERNEL__ + /** * config register instance - part of a loaded feature. * maps register values to csdev driver structures @@ -274,4 +294,5 @@ void cscfg_csdev_disable_config(struct cscfg_config_csdev *config_csdev); /* reset a feature to default values */ void cscfg_reset_feat(struct cscfg_feature_csdev *feat_csdev); +#endif /* __KERNEL__ */ #endif /* _CORESIGHT_CORESIGHT_CONFIG_H */