From mboxrd@z Thu Jan 1 00:00:00 1970 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id EDDFEC433F5 for ; Thu, 14 Apr 2022 06:46:53 +0000 (UTC) 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=vQ76Q9lyHEPxPXUojJf+5iT7vwzLXEfX+SofP582QIg=; b=rPDh7kHGku+IVC ah8Ixj3a3Uj1dQi10lRSeS7v4WpZyalWbCm3Oip1RZ/0nllhgys79c6Ox3UrCctWTgCQP/+AfF+ch P3b7k9EykyXEeLSZWea9UxYB9S/uZa98Xkuqq7BjGC3S4SBPatkOO0Lnua/2E2h6tIKMz65VR8EMk 3XWHwPcNgdlw0+LXvZHA2ocNNUMtNVup3pvibB3QXx1cVMLT6yyFAXto40MI3T1KZeSmYtSd63m5V oTr3cB6nYR+LVsm8CfVazRS/veIR3BleC7CyX0cW03H0V6m+KmlMpc+QpaWZmYt81wGk8uMg0N72w UydiKRF441v6wpqOIIJA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1netEV-0049Zp-14; Thu, 14 Apr 2022 06:45:35 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1netDz-0049SJ-LY for linux-arm-kernel@lists.infradead.org; Thu, 14 Apr 2022 06:45:06 +0000 Received: by mail-wm1-x329.google.com with SMTP id ay11-20020a05600c1e0b00b0038eb92fa965so5338303wmb.4 for ; Wed, 13 Apr 2022 23:45:02 -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=+nBEW0jm0yLeT5vwXFjCiio0kk5nWsE2+oQuJTfnj4Y=; b=cn92g6F0+ystIBpgu05t9FFS9Bn1wdKSiGDbxMb8bnfpGDTZTHcz3xi21GaMJa/1sf kKPxzcy7B3wZNNZW/TX9oHv+6OGuVOAQQZiwYKbcNbtgO4mXlq/YQBJDsmFYIkPGhDwd YrW+afFAXn5lh0jz5EFsYMTDsQJWrCDLQgOQCMxdAuaNEGxJsD+lj7/6Wc8nP9Q2zJ3j 0jN5KDK/PrleJTaAI6fRNPt8xfjNzS+h+lqLrpkrF1gfDR5P7FY1k+/JKFy6jOq6qAXt E4Nz1hJnYTROOuGJuHeKoMBPBFz1tevAgDWthN7+XKrVZQ0GlV38z4PKVH1gjoRpnTTP FtnA== 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=+nBEW0jm0yLeT5vwXFjCiio0kk5nWsE2+oQuJTfnj4Y=; b=gsW8mDm1YS08I7gvHrTtOW5IYVuNgUDwHp/XGSaXYVHKwKFGMEFlX/LK57XWAq6IFf vKMI07H80GI9+LRo82DhG37x3Oi7czO+MDtNxvB9tLp5BAkdIf8kYXzcPZTB8/y7nZ8V NhO397r7XklyIwqb3auGIq73Fah3HWqr5ao9tEOU7ihsiSWkRZM2so3pl8fu+zMQ8svE Ig+47YDu6MB2b2d5oMn8HXOi5olM7ng+HgZUTP8H6h5utUDYasQr8gKC4EKr4R9KBbc/ lbQcaj9CNCxUfYuETFiBS7aGSeNY03//3CD9HGa/W5WjMfit1nA6DclecB/HdxHNpEL/ smzQ== X-Gm-Message-State: AOAM531+RmeJUAj2SjehdesLB6PANHEj2sUVqNh0pB0yaFND4TG4gFMJ 3o/bz2xGy90RqiIUuFzxF+3gfIwqDyesCA== X-Google-Smtp-Source: ABdhPJztG5PYwqWC4ApFpJsUbJrHV29La307G72obov/B82LPG0te6IF5AxvT2MW3AxycE9N/FGlvA== X-Received: by 2002:a05:600c:3586:b0:38c:a92f:1744 with SMTP id p6-20020a05600c358600b0038ca92f1744mr2174259wmq.126.1649918701410; Wed, 13 Apr 2022 23:45:01 -0700 (PDT) Received: from linaro.org ([2a00:23c5:6809:2201:e4bb:355f:cdcf:ea80]) by smtp.gmail.com with ESMTPSA id c18-20020a7bc852000000b0038ebf5f191esm5395954wml.45.2022.04.13.23.45.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 23:45:01 -0700 (PDT) From: Mike Leach To: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org, mathieu.poirier@linaro.org Cc: suzuki.poulose@arm.com, leo.yan@linaro.org, Mike Leach Subject: [PATCH v3 3/5] coresight: configfs: Modify config files to allow userspace use Date: Thu, 14 Apr 2022 07:44:55 +0100 Message-Id: <20220414064457.12052-4-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220414064457.12052-1-mike.leach@linaro.org> References: <20220414064457.12052-1-mike.leach@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220413_234503_793322_0A70456A X-CRM114-Status: GOOD ( 26.00 ) 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 | 95 +++++++++++++------ .../hwtracing/coresight/coresight-config.h | 12 +++ 2 files changed, 80 insertions(+), 27 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-config-file.c b/drivers/hwtracing/coresight/coresight-config-file.c index 3fd001938324..5b8f635ac50e 100644 --- a/drivers/hwtracing/coresight/coresight-config-file.c +++ b/drivers/hwtracing/coresight/coresight-config-file.c @@ -6,8 +6,57 @@ #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 +129,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 +139,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 +156,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 +188,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 +214,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 +229,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 +308,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 +347,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 +362,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 +440,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.h b/drivers/hwtracing/coresight/coresight-config.h index 9cd3c26ce023..fce74e2fbd0c 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 */ @@ -100,6 +107,10 @@ struct cscfg_fs_load_descs { struct cscfg_feature_desc **feat_descs; }; + +/* remainder of header is used by the kernel drivers only */ +#ifdef __KERNEL__ + /** * Device feature descriptor - combination of registers and parameters to * program a device to implement a specific complex function. @@ -272,4 +283,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 */ -- 2.17.1 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel