From patchwork Thu May 11 06:53:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Leach X-Patchwork-Id: 92371 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4161641vqo; Wed, 10 May 2023 23:55:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6LxxVp1z89NGZ2OIvLe/q9Ky12ixu7CovnfjCLjbc0F9rsDYHbuV3ilTuvx4k38tzT3XN+ X-Received: by 2002:a05:6a21:99a5:b0:f0:515d:1742 with SMTP id ve37-20020a056a2199a500b000f0515d1742mr26992272pzb.53.1683788121716; Wed, 10 May 2023 23:55:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683788121; cv=none; d=google.com; s=arc-20160816; b=SZ0Jiuq+jVNTwhlV0boYwlXgXuCLxYFJAaEET2v6lWZ29YqrCXLvSz9fKTsbG1NcUb 9cnp6eFZCJzQRyttqX6LD7t142u3+LmfMzUqyuTnPwH6HwuzhPebJDWszrK61hvPNuPb c3LlUQQ+kFoxF/DDSpDL5bVsSysSVivL4PwwVpJOhwwXs6eC5WEaI6pQS8OHmH7NH39a j5ThMb6Ovz0kow1jrMkNq623Rpav4yOtRGYUIZ+6tW9xOSbg+zaG5pV/TP5oHs+bs6rL aXomMZ+84W54hjIdvLUYwMZHOshiVZ1SxiwUtc5FgoxLhm1mZ+Ubdz5oIjQDNfYFRfZj 93ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=FpWMvThlfNIMaMhl/rPa62rSrEhuyn8IMP1y7eIgCPE=; b=A6OLpDB2SIZicgpemAxBMNlTMXNk8WGI8cKk+V9n9frS4VSLSM+igm4KB6D+vQJKBZ sOPV7lhApEB4bsiHk4veS9QBRUnKCAGnXC5jlQzvTun9g0PEH6HcsS0zywk9CGt//sdj SR2pD6idx0/s/DGD86w2Mb6VLtiS3RwnyvAniSxWYsttCNQCvLoXyHVklYBQYwf7J0cD TCAwIXJ9YM4UqjUJAMocqBXvkLQXLr1NuICFRr4PM2dHm9uCWUwixh8JceMnWM+cqhik KHKBzUCvuF4UesUuYPDA/q+WA2IBALMqjwbLCBaLMFT67Rit7aTIpruoGvN24EHXuC1Z QBrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="szB/3Odl"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b5-20020a639305000000b0052ca3209fc9si6181180pge.654.2023.05.10.23.55.09; Wed, 10 May 2023 23:55:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="szB/3Odl"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237195AbjEKGxo (ORCPT + 99 others); Thu, 11 May 2023 02:53:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237174AbjEKGxj (ORCPT ); Thu, 11 May 2023 02:53:39 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E22CD4EC6 for ; Wed, 10 May 2023 23:53:35 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id 5b1f17b1804b1-3f41dceb93bso37403165e9.1 for ; Wed, 10 May 2023 23:53:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683788014; x=1686380014; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=FpWMvThlfNIMaMhl/rPa62rSrEhuyn8IMP1y7eIgCPE=; b=szB/3Odl4sPY8015upRVgMz7tptrckYPyYL3yod3kM6N4U9eYAEOKR24mif050F1db JoMTXCd17qAmFhr7R+9gqMcL/iBQscleyIt0K7w3siAPOro/V82kZgyGfWaaefR9T5H4 KH52Jz5kwBihddvj4UTD3IYlIvKng+VSFnjDqwQZvTTaA8lYU1qrFe+CHBEOJBxJpRip kxjTwdHDOHxvNbQrr1RfNa+LReHC3rHJVv1NNiDrqQuJMowE9yuhQAIYcsCJSMTcl9BV 2SOo9LZQn1zDpq1B7Ov0YXHkfk4vtKoc3oRNfH337MIv6msVfxn+LrXlz1xhaQ/f8zSb eevw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683788014; x=1686380014; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FpWMvThlfNIMaMhl/rPa62rSrEhuyn8IMP1y7eIgCPE=; b=QtGetbMz/ymoDasrm8Z2Um3WDFNke28KO8Rx5PyUeT7tZrmtm2oB7h5V++TYG/WYjF YgVccPz0wzSmnBUF1KGKXvJr88oJOpeCZ7nAqKLHvb+1Z0fWG8fQXoUAGGtjaD1QLgkX AJVJ9cY/aWI1xpzh+AeyZ2lvNpGfZa7D1b+U6DcFgchkZEGMeV5YFqx3Ml6ue6dBYQEN JN1Ef3hqEGyeDDkOhw9Q/E7yGJQ5ITzDfApU7YwfpqKA194vpkyKuoOdb0V4n+LPDTgE qeu8UFxVL0pGTrdwJXDCm2eNbCuC7f2ohmQfMLVSgvLQzd/rTYBPL8OvOEsD9n29x/Qg ULRQ== X-Gm-Message-State: AC+VfDw1SMkfF2Cs3hzRZvMwAVTSRTCGU6ipgoi00IJUDjuIOmqUKNwo uZ/MQodBFsW94Tl1ZFIPf1Mvaw== X-Received: by 2002:a7b:cd11:0:b0:3f4:b6bc:bd93 with SMTP id f17-20020a7bcd11000000b003f4b6bcbd93mr2992004wmj.35.1683788014222; Wed, 10 May 2023 23:53:34 -0700 (PDT) Received: from linaro.org ([2a00:23c5:680a:d01:3110:8b33:cdf9:dea0]) by smtp.gmail.com with ESMTPSA id f8-20020a7bc8c8000000b003f4e8530696sm1038257wml.46.2023.05.10.23.53.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 23:53:33 -0700 (PDT) From: Mike Leach To: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org, linux-kernel@vger.kernel.org Cc: suzuki.poulose@arm.com, acme@kernel.org, james.clark@arm.com, Mike Leach Subject: [PATCH v6 1/6] coresight: config: add config table runtime load functionality Date: Thu, 11 May 2023 07:53:25 +0100 Message-Id: <20230511065330.26392-2-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230511065330.26392-1-mike.leach@linaro.org> References: <20230511065330.26392-1-mike.leach@linaro.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765579813724903107?= X-GMAIL-MSGID: =?utf-8?q?1765579813724903107?= Introduce a CoreSight configuration table format to allow dynamic configuration load at runtime. Define a binary table format and provide a reader for that format that will create and populate the internal configuration and feature structures use by the driver infrastructure. Adds API to access new functionality. Functionality to be used to load configurations from external sources at runtime, such as configfs. Signed-off-by: Mike Leach --- drivers/hwtracing/coresight/Makefile | 3 +- .../coresight/coresight-config-table.c | 427 ++++++++++++++++++ .../coresight/coresight-config-table.h | 146 ++++++ .../hwtracing/coresight/coresight-config.h | 21 + 4 files changed, 596 insertions(+), 1 deletion(-) create mode 100644 drivers/hwtracing/coresight/coresight-config-table.c create mode 100644 drivers/hwtracing/coresight/coresight-config-table.h diff --git a/drivers/hwtracing/coresight/Makefile b/drivers/hwtracing/coresight/Makefile index 33bcc3f7b8ae..b282f123582e 100644 --- a/drivers/hwtracing/coresight/Makefile +++ b/drivers/hwtracing/coresight/Makefile @@ -6,7 +6,8 @@ obj-$(CONFIG_CORESIGHT) += coresight.o coresight-y := coresight-core.o coresight-etm-perf.o coresight-platform.o \ coresight-sysfs.o coresight-syscfg.o coresight-config.o \ coresight-cfg-preload.o coresight-cfg-afdo.o \ - coresight-syscfg-configfs.o coresight-trace-id.o + coresight-syscfg-configfs.o coresight-trace-id.o \ + coresight-config-table.o obj-$(CONFIG_CORESIGHT_LINK_AND_SINK_TMC) += coresight-tmc.o coresight-tmc-y := coresight-tmc-core.o coresight-tmc-etf.o \ coresight-tmc-etr.o diff --git a/drivers/hwtracing/coresight/coresight-config-table.c b/drivers/hwtracing/coresight/coresight-config-table.c new file mode 100644 index 000000000000..0a8f017d76d2 --- /dev/null +++ b/drivers/hwtracing/coresight/coresight-config-table.c @@ -0,0 +1,427 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2020 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#include "coresight-config-table.h" + +#define cscfg_extract_u64(val64) { \ + val64 = *(u64 *)(buffer + used); \ + used += sizeof(u64); \ + } + +#define cscfg_extract_u32(val32) { \ + val32 = *(u32 *)(buffer + used); \ + used += sizeof(u32); \ + } + +#define cscfg_extract_u16(val16) { \ + val16 = *(u16 *)(buffer + used); \ + used += sizeof(u16); \ + } + +#define cscfg_extract_u8(val8) { \ + val8 = *(buffer + used); \ + used++; \ + } + +static int cscfg_table_read_hdr(const u8 *buffer, const int buflen, int *buf_used, + struct cscfg_table_header *hdr) +{ + /* table header always at the start of the buffer */ + int used = 0; + + if (buflen < sizeof(struct cscfg_table_header)) + return -EINVAL; + + cscfg_extract_u32(hdr->magic_version); + if (hdr->magic_version != CSCFG_TABLE_MAGIC_VERSION) + return -EINVAL; + + cscfg_extract_u16(hdr->length); + if (hdr->length > buflen) + return -EINVAL; + + cscfg_extract_u16(hdr->nr_configs); + cscfg_extract_u16(hdr->nr_features); + + *buf_used = used; + return 0; +} + +static int cscfg_table_read_elem_hdr(const u8 *buffer, const int buflen, int *buf_used, + struct cscfg_table_elem_header *elem_hdr) +{ + int used = *buf_used; + + if ((buflen - used) < (sizeof(u16) + sizeof(u8))) + return -EINVAL; + + /* read length and check enough buffer remains for this element */ + elem_hdr->elem_length = *(u16 *)(buffer + used); + if ((buflen - used) < elem_hdr->elem_length) + return -EINVAL; + /* don't use extract fn as we update used _after_ the comparison */ + used += sizeof(u16); + + /* read type and validate */ + cscfg_extract_u8(elem_hdr->elem_type); + if ((elem_hdr->elem_type < CSCFG_TABLE_ELEM_TYPE_FEAT) || + (elem_hdr->elem_type > CSCFG_TABLE_ELEM_TYPE_CFG)) + return -EINVAL; + + *buf_used = used; + return 0; +} + +static int cscfg_table_read_elem_str(const u8 *buffer, const int buflen, int *buf_used, + struct cscfg_table_elem_str *elem_str) +{ + int used = *buf_used; + + if ((buflen - used) < sizeof(u16)) + return -EINVAL; + + cscfg_extract_u16(elem_str->str_len); + + if ((buflen - used) < elem_str->str_len) + return -EINVAL; + + /* check for 0 termination */ + if (buffer[used + (elem_str->str_len - 1)] != 0) + return -EINVAL; + + elem_str->str = kstrdup((char *)(buffer + used), GFP_KERNEL); + used += elem_str->str_len; + + *buf_used = used; + return 0; +} + +static int cscfg_table_alloc_desc_arrays(struct cscfg_table_load_descs *desc_arrays, + int nr_features, int nr_configs) +{ + /* arrays are 0 terminated - nr_configs & nr_features elements */ + desc_arrays->config_descs = kcalloc(nr_configs + 1, sizeof(struct cscfg_config_desc *), + GFP_KERNEL); + if (!desc_arrays->config_descs) + return -ENOMEM; + desc_arrays->feat_descs = kcalloc(nr_features + 1, sizeof(struct cscfg_feature_desc *), + GFP_KERNEL); + if (!desc_arrays->feat_descs) + return -ENOMEM; + return 0; +} + +/* free up the data allocated to a config desc */ +static void cscfg_table_free_config_desc(struct cscfg_config_desc *config_desc) +{ + int i; + + if (!config_desc) + return; + + /* free presets */ + kfree(config_desc->presets); + + /* free feat ref strings */ + if (config_desc->nr_feat_refs) { + /* each string */ + for (i = 0; i < config_desc->nr_feat_refs; i++) + kfree(config_desc->feat_ref_names[i]); + + /* and the char * array */ + kfree(config_desc->feat_ref_names); + } + + /* next the strings */ + kfree(config_desc->name); + kfree(config_desc->description); + + /* finally the struct itself */ + kfree(config_desc); +} + +static int cscfg_table_read_elem_config(const u8 *buffer, const int buflen, int *buf_used, + struct cscfg_table_load_descs *desc_arrays, + const int cfg_index) +{ + struct cscfg_table_elem_header elem_hdr; + struct cscfg_table_elem_str elem_str; + struct cscfg_config_desc *config_desc; + int used = *buf_used, nr_preset_vals, nr_preset_bytes, i; + int err = 0; + u64 *presets; + + /* + * read the header - if not config, then don't update buf_used + * pointer on return + */ + err = cscfg_table_read_elem_hdr(buffer, buflen, &used, &elem_hdr); + if (err) + return err; + if (elem_hdr.elem_type != CSCFG_TABLE_ELEM_TYPE_CFG) + return 0; + + /* we have a config - allocate the descriptor */ + config_desc = kzalloc(sizeof(struct cscfg_config_desc), GFP_KERNEL); + if (!config_desc) + return -ENOMEM; + + /* read the name string */ + err = cscfg_table_read_elem_str(buffer, buflen, &used, &elem_str); + if (err) + return err; + config_desc->name = elem_str.str; + + /* allocate load name if not set */ + if (!desc_arrays->load_name) + desc_arrays->load_name = kstrdup(config_desc->name, GFP_KERNEL); + + /* read the description string */ + err = cscfg_table_read_elem_str(buffer, buflen, &used, &elem_str); + if (err) + return err; + config_desc->description = elem_str.str; + + /* read in some values */ + if ((buflen - used) < sizeof(u64)) + return -EINVAL; + cscfg_extract_u16(config_desc->nr_presets); + cscfg_extract_u32(config_desc->nr_total_params); + cscfg_extract_u16(config_desc->nr_feat_refs); + + /* 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 = kcalloc(nr_preset_vals, sizeof(u64), GFP_KERNEL); + if (!presets) + return -ENOMEM; + + nr_preset_bytes = sizeof(u64) * nr_preset_vals; + if ((buflen - used) < nr_preset_bytes) + return -EINVAL; + + memcpy(presets, (buffer + used), nr_preset_bytes); + config_desc->presets = presets; + used += nr_preset_bytes; + } + + /* read the array of feature names referenced by the config */ + if (config_desc->nr_feat_refs) { + config_desc->feat_ref_names = kcalloc(config_desc->nr_feat_refs, + sizeof(char *), GFP_KERNEL); + if (!config_desc->feat_ref_names) + return -ENOMEM; + + for (i = 0; i < config_desc->nr_feat_refs; i++) { + err = cscfg_table_read_elem_str(buffer, buflen, &used, &elem_str); + if (err) + return err; + config_desc->feat_ref_names[i] = elem_str.str; + } + } + + desc_arrays->config_descs[cfg_index] = config_desc; + *buf_used = used; + return 0; +} + +static int cscfg_table_read_elem_param(const u8 *buffer, const int buflen, int *buf_used, + struct cscfg_parameter_desc *param_desc) +{ + struct cscfg_table_elem_str elem_str; + int err = 0, used = *buf_used; + + /* parameter name */ + err = cscfg_table_read_elem_str(buffer, buflen, &used, &elem_str); + if (err) + return err; + param_desc->name = elem_str.str; + + /* parameter value */ + if ((buflen - used) < sizeof(u64)) + return -EINVAL; + cscfg_extract_u64(param_desc->value); + + *buf_used = used; + return err; +} + +static void cscfg_table_free_feat_desc(struct cscfg_feature_desc *feat_desc) +{ + if (!feat_desc) + return; + + /* free up the register descriptor array */ + kfree(feat_desc->regs_desc); + + /* free up the parameters array */ + kfree(feat_desc->params_desc); + + /* name and description strings */ + kfree(feat_desc->name); + kfree(feat_desc->description); + + /* finally the struct itself */ + kfree(feat_desc); +} + +static int cscfg_table_read_elem_feature(const u8 *buffer, const int buflen, int *buf_used, + struct cscfg_table_load_descs *desc_arrays, + const int feat_idx) +{ + struct cscfg_table_elem_header elem_hdr; + struct cscfg_table_elem_str elem_str; + struct cscfg_feature_desc *feat_desc; + struct cscfg_regval_desc *p_reg_desc; + int used = *buf_used, err, i, nr_regs_bytes; + u32 val32; + + /* allocate the feature descriptor object */ + feat_desc = kzalloc(sizeof(struct cscfg_feature_desc), GFP_KERNEL); + if (!feat_desc) + return -ENOMEM; + + /* read and check the element header */ + err = cscfg_table_read_elem_hdr(buffer, buflen, &used, &elem_hdr); + if (err) + return err; + + if (elem_hdr.elem_type != CSCFG_TABLE_ELEM_TYPE_FEAT) + return -EINVAL; + + /* read the feature name */ + err = cscfg_table_read_elem_str(buffer, buflen, &used, &elem_str); + if (err) + return err; + feat_desc->name = elem_str.str; + + /* allocate load name if not set previously by config */ + if (!desc_arrays->load_name) + desc_arrays->load_name = kstrdup(feat_desc->name, GFP_KERNEL); + + /* read the description string */ + err = cscfg_table_read_elem_str(buffer, buflen, &used, &elem_str); + if (err) + return err; + feat_desc->description = elem_str.str; + + /* + * read in some values + * [u32 value: match_flags] + * [u16 value: nr_regs] - number of registers. + * [u16 value: nr_params] - number of parameters. + */ + cscfg_extract_u32(feat_desc->match_flags); + cscfg_extract_u16(feat_desc->nr_regs); + cscfg_extract_u16(feat_desc->nr_params); + + /* register descriptors - 32 bit + 64 bit value */ + if (feat_desc->nr_regs) { + nr_regs_bytes = ((sizeof(u32) + sizeof(u64)) * feat_desc->nr_regs); + if ((buflen - used) < nr_regs_bytes) + return -EINVAL; + feat_desc->regs_desc = kcalloc(feat_desc->nr_regs, + sizeof(struct cscfg_regval_desc), GFP_KERNEL); + if (!feat_desc->regs_desc) + return -ENOMEM; + + for (i = 0; i < feat_desc->nr_regs; i++) { + cscfg_extract_u32(val32); + p_reg_desc = (struct cscfg_regval_desc *)&feat_desc->regs_desc[i]; + CSCFG_TABLE_U32_TO_REG_DESC_INFO(val32, p_reg_desc); + cscfg_extract_u64(feat_desc->regs_desc[i].val64); + } + } + + /* parameter descriptors - string + 64 bit value */ + if (feat_desc->nr_params) { + feat_desc->params_desc = kcalloc(feat_desc->nr_params, + sizeof(struct cscfg_parameter_desc), GFP_KERNEL); + if (!feat_desc->params_desc) + return -ENOMEM; + for (i = 0; i < feat_desc->nr_params; i++) { + err = cscfg_table_read_elem_param(buffer, buflen, &used, + &feat_desc->params_desc[i]); + if (err) + return err; + } + } + + desc_arrays->feat_descs[feat_idx] = feat_desc; + *buf_used = used; + return 0; +} + +/* + * Read the table from a buffer and create the internal configuration and + * feature descriptors to load into the cscfg system + */ +int cscfg_table_read_buffer(const u8 *buffer, const int buflen, + struct cscfg_table_load_descs *desc_arrays) +{ + struct cscfg_table_header hdr; + int used = 0, err, i; + + /* read in the table header */ + err = cscfg_table_read_hdr(buffer, buflen, &used, &hdr); + if (err) + return err; + + /* allocate the memory for the descriptor pointer arrays */ + err = cscfg_table_alloc_desc_arrays(desc_arrays, hdr.nr_features, hdr.nr_configs); + if (err) + return err; + + /* read elements */ + + /* first elements are configurations */ + for (i = 0; i < hdr.nr_configs; i++) { + err = cscfg_table_read_elem_config(buffer, buflen, &used, desc_arrays, i); + if (err) + return err; + } + + /* now read and populate all the feature descriptors */ + for (i = 0; i < hdr.nr_features; i++) { + err = cscfg_table_read_elem_feature(buffer, buflen, &used, desc_arrays, i); + if (err) + return err; + } + return 0; +} + +/* + * Need to free up the dynamically allocated descriptor arrays on unload + * as the memory used could be significant if many configurations are loaded + * and unloaded while the machine is operational. + * + * This frees up all the memory allocated by config during the load process. + */ +void cscfg_table_free_load_descs(struct cscfg_table_load_descs *desc_arrays) +{ + int i = 0; + + if (!desc_arrays) + return; + + /* free up each of the config descriptors */ + while (desc_arrays->config_descs[i]) { + cscfg_table_free_config_desc(desc_arrays->config_descs[i]); + i++; + } + + /* free up each of the feature descriptors */ + i = 0; + while (desc_arrays->feat_descs[i]) { + cscfg_table_free_feat_desc(desc_arrays->feat_descs[i]); + i++; + } + + /* finally free up the load descs pointer arrays */ + kfree(desc_arrays->config_descs); + kfree(desc_arrays->feat_descs); + kfree(desc_arrays->load_name); +} diff --git a/drivers/hwtracing/coresight/coresight-config-table.h b/drivers/hwtracing/coresight/coresight-config-table.h new file mode 100644 index 000000000000..2cd8fb0630e2 --- /dev/null +++ b/drivers/hwtracing/coresight/coresight-config-table.h @@ -0,0 +1,146 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#ifndef _CORESIGHT_CORESIGHT_CONFIG_TABLE_H +#define _CORESIGHT_CORESIGHT_CONFIG_TABLE_H + +#include + +#include "coresight-config.h" + +/* + * Configurations and features can be dynamically loaded at runtime + * using a table format that is converted on read into the internal + * structures used by the cscfg infrastructure. + * + * Table structure - for loading configuration(s) + feature(s) + * from configfs or other external sources. + * + * [cscfg_table_header] - mandatory + * [CONFIG_ELEM] * [cscfg_table_header.nr_configs] - optional. + * [FEATURE_ELEM] * [cscfg_table_header.nr_features] - optional. + * + * Table valid if it has both config(s) and feature(s), only config(s) + * or only feature(s). + * + * Invalid table if no config or features. + * + * Structure for [CONFIG_ELEM]: + * + * [cscfg_table_elem_header] - header length value to end of feature strings. + * [cscfg_table_elem_str] - name of the configuration + * [cscfg_table_elem_str] - description of configuration + * [u16 value - nr_presets] - number of sets of presets supplied + * [u32 value - nr_total_params] - total of all params in referenced features + * [u16 value - nr_feat_refs] - number of features selected by this configuration + * [u64 values] * (nr_presets * nr_total_params) + * [cscfg_table_elem_str] * nr_feat_refs - names of features selected by configuration. + * + * A configuration must reference at least one feature. + * Referenced features may be in this table, or have been loaded previously. + * + * Structure for a [FEATURE_ELEM] + * + * [cscfg_table_elem_header] - header length is total bytes to end of param structures. + * [cscfg_table_elem_str] - feature name. + * [cscfg_table_elem_str] - feature description. + * [u32 value: match_flags] + * [u16 value: nr_regs] - number of registers. + * [u16 value: nr_params] - number of parameters. + * [cscfg_regval_desc struct] * nr_regs + * [PARAM_ELEM] * nr_params + * + * Structure for [PARAM_ELEM] + * + * [cscfg_table_elem_str] - parameter name. + * [u64 value: param_value] - initial value. + */ + +/* major element types - configurations and features */ + +#define CSCFG_TABLE_ELEM_TYPE_FEAT 0x1 +#define CSCFG_TABLE_ELEM_TYPE_CFG 0x2 + +#define CSCFG_TABLE_MAGIC_VERSION 0xC5CF0001 + +#define CSCFG_TABLE_U32_TO_REG_DESC_INFO(val32, p_desc) \ + { \ + p_desc->type = (val32 >> 24) & 0xFF; \ + p_desc->offset = (val32 >> 12) & 0xFFF; \ + p_desc->hw_info = val32 & 0xFFF; \ + } + +#define CSCFG_TABLE_REG_DESC_INFO_TO_U32(val32, p_desc) \ + { \ + val32 = p_desc->hw_info & 0xFFF; \ + val32 |= ((p_desc->offset & 0xFFF) << 12); \ + val32 |= ((p_desc->type & 0xFF) << 24); \ + } + +/* + * Define a maximum size for any configuration table. + * + * Use a value that will reasonably cover all the usable & programmable + * registers in an ETM, the most complex device we have. + * + * This may also be used for the binary attributes in configfs which need a max + * size, as an internal buffer is declared, and will not be exceeded to prevent + * kernel OOM errors / attacks. + * + */ +#define CSCFG_TABLE_MAXSIZE SZ_16K + +/* limit string sizes - used for descriptions and names. */ +#define CSCFG_TABLE_STR_MAXSIZE SZ_1K + +/** + * Table header. + * + * @magic_version: magic number / version for table format. + * @length : total length of all data in the table. + * @nr_configs : total number of configs in the table. + * @nr_features : total number of features in the table. + */ +struct cscfg_table_header { + u32 magic_version; + u16 length; + u16 nr_configs; + u16 nr_features; +}; + +/** + * element header + * + * @elem_length: total length of this element + * @elem_type : type of this element - one of CSCFG_TABLE_ELEM_TYPE.. defines. + */ +struct cscfg_table_elem_header { + u16 elem_length; + u8 elem_type; +}; + +/** + * string table element. + * + * @str_len: length of string buffer including 0 terminator + * @str : string buffer - 0 terminated. + */ +struct cscfg_table_elem_str { + u16 str_len; + char *str; +}; + +/* + * Read a configuration programming table from the buffer and create the + * structures needed to load into the cscfg system + */ +int cscfg_table_read_buffer(const u8 *buffer, const int buflen, + struct cscfg_table_load_descs *desc_arrays); + +/* on unload we need to free up memory allocated on read */ +void cscfg_table_free_load_descs(struct cscfg_table_load_descs *desc_arrays); + +#endif /* _CORESIGHT_CORESIGHT_CONFIG_TABLE_H */ diff --git a/drivers/hwtracing/coresight/coresight-config.h b/drivers/hwtracing/coresight/coresight-config.h index 6ba013975741..ea3aaf0d129b 100644 --- a/drivers/hwtracing/coresight/coresight-config.h +++ b/drivers/hwtracing/coresight/coresight-config.h @@ -85,6 +85,27 @@ struct cscfg_regval_desc { }; }; +/** + * Dynamically loaded descriptor arrays. + * + * For builtin or module loaded configurations / features these are + * statically defined at compile time. + + * For a dynamic load at runtime, using a config table, (e.g. load from + * configfs) we create the arrays dynamically so need a structure to + * manage these. + * + * @config_descs: array of config descriptor pointers. + * @feat_descs: array of feature descriptor pointers. + * @load_name: user readable name which may be used to unload later. + * Will be name of first config if present, or first feature. + */ +struct cscfg_table_load_descs { + struct cscfg_config_desc **config_descs; + struct cscfg_feature_desc **feat_descs; + char *load_name; +}; + /** * Device feature descriptor - combination of registers and parameters to * program a device to implement a specific complex function. From patchwork Thu May 11 06:53:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Leach X-Patchwork-Id: 92370 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4161647vqo; Wed, 10 May 2023 23:55:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6HHCBBHKhMc2hPFcRQYsW/5ifkqHnAVrnj2z47eHm/9hs2Bxc/3S606XLOG5YMhWepKZpy X-Received: by 2002:a05:6a20:729e:b0:100:bae5:acf3 with SMTP id o30-20020a056a20729e00b00100bae5acf3mr15316776pzk.37.1683788122212; Wed, 10 May 2023 23:55:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683788122; cv=none; d=google.com; s=arc-20160816; b=MeoWzuzkQ/UZITcXeZWF/7UPyD+P7KtBKxmQV0FO99KzKe26g4a88NVFQtCSSaSLQ4 CJz0DAHiGdsv7oHEfQxpzSjgqHN2QZ7VcFb7UEEAuvF94HfscupSM5RAt7IGerHMuV1I Qdfb27NV4C8WGmRJa8klfvqcCIdx1ok0y0sKuWjj8Lonjuxv1ZOQY4iW/UFTFO5IUgux 6vW7YbICeuR7mO18ym967RXmrEHre7C0t9aBQaG1IhW+xKxMYW9USQqGEJPfKtScks1l 4KnU/QBD6HQdzcpgRMYPHonzfO74oi1sM6Bcpd+cSktqh8PVBPbd2aFk2DJhvF+sUuHB 05xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=kVN6AgfRQ9JB5xb8lTlVzU3nqEsQK95Vgz2t3dnIbI4=; b=A1A4h5IbGZzV4j0HvM3ZHPVBthG2luaDaHhuth/obkGsoZw8DbRsRQTjgWTvU99PoG tQqhnoJPtARaI2s6PU0hpFRDK0mAU40dKm9DAL44ObqjxufXwo0zweqPFeIzRaBlP6PS YAap2OQ7y1p89cDtGmQOM6GY5/KTBTgkw+piEJTni/n6nbj/PlM1js5nOJx/vphVXVDU utoFev7z3pbWUHAeiLAxwl7TC8nuu+Z8WulrgBaHY8ylYHptZlXPlLYpFZt08zDCCXiw O4D2Dk9vJg+fZdy94nafSM0uOzKkUhJqe1pdDflJa8OOueKT4Jg+A0XKoGBvmFFIWNnQ bRHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GWnAUiuM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g1-20020a63b141000000b00502f4fd0c16si6139771pgp.653.2023.05.10.23.55.09; Wed, 10 May 2023 23:55:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GWnAUiuM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237177AbjEKGxk (ORCPT + 99 others); Thu, 11 May 2023 02:53:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237090AbjEKGxi (ORCPT ); Thu, 11 May 2023 02:53:38 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8F474EEB for ; Wed, 10 May 2023 23:53:36 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id 5b1f17b1804b1-3f4449fa085so14026635e9.0 for ; Wed, 10 May 2023 23:53:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683788015; x=1686380015; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=kVN6AgfRQ9JB5xb8lTlVzU3nqEsQK95Vgz2t3dnIbI4=; b=GWnAUiuMAyQ3vhuczplkyUK9hkjarHhKn92pktjUO2edc1aly6Kve5PT1niMSZ0cyz Rh8uywRvpab3kS7wQsfqHTm1hB/I4vbqq6BfwtfISE4H0lGnpuT64h9RzAYi7tud8/20 GqEAedC+MQx8QYb07fCxJLYlhl7G8rhp9VLuskFLOhgoTfs+hkkC8St47uVHuUPHoTNz wKTG81jjK6CicEkqxoeDG/lVxZmzhAEyOHjUIKfsDOR5HMilJegkIGhkh6Ip9hMHCXGi Lglx3eECZiCig5EJghEQ6OW/CpGGQrpkHiZi3GJ84OcXslJWG0rSe8AOGclJAPh4sXoj vDwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683788015; x=1686380015; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kVN6AgfRQ9JB5xb8lTlVzU3nqEsQK95Vgz2t3dnIbI4=; b=ggEIB5ta6/8yAw0bbEi3dvPS2jp+RI/0C7Z/1SqBBc5i4CKs9hFwZw+aiah1/c+8Ld 7N8p6rkn7x4cx08eZC7xHamKlH4kT0SXii8p+qIuGsyGlWfEV8qaJNy8u5MXzsi9eT9x CiTegrCT7Qw3nyVzvkiLf6ZsF6YyhcsfGj5zKZ2ImrFbKC52eK4Bqx8FdQESreTWbtRW f4Ib+WFGisdDUb/9ZAnCg23Voua9YLFGM9RVqyp2zjwQ9En9aVNU2lsnl73Y50OYm2ol OEJOhxk94dMDEeghZGHAoGocqaCD6WIbhCqVqiRtCr9Exx8Ls6v85kJ2vCylNRh5thZV hcDA== X-Gm-Message-State: AC+VfDw+kzsFeHWGmpeAsFrgSjmhzY4He/OvwW8ZYNkzJh0Zqgt1DSff 8u2kOrKPpEvMGmCf0cC9Dl8aYA== X-Received: by 2002:a1c:7c10:0:b0:3f4:2610:5cc1 with SMTP id x16-20020a1c7c10000000b003f426105cc1mr9329850wmc.1.1683788015372; Wed, 10 May 2023 23:53:35 -0700 (PDT) Received: from linaro.org ([2a00:23c5:680a:d01:3110:8b33:cdf9:dea0]) by smtp.gmail.com with ESMTPSA id f8-20020a7bc8c8000000b003f4e8530696sm1038257wml.46.2023.05.10.23.53.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 23:53:34 -0700 (PDT) From: Mike Leach To: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org, linux-kernel@vger.kernel.org Cc: suzuki.poulose@arm.com, acme@kernel.org, james.clark@arm.com, Mike Leach Subject: [PATCH v6 2/6] coresight: configfs: Update memory allocation / free for configfs elements Date: Thu, 11 May 2023 07:53:26 +0100 Message-Id: <20230511065330.26392-3-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230511065330.26392-1-mike.leach@linaro.org> References: <20230511065330.26392-1-mike.leach@linaro.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765579814466192927?= X-GMAIL-MSGID: =?utf-8?q?1765579814466192927?= Previously, the objects backing the configfs directories and files were created using devm managed memory on the coresight device. Now we are adding configfs load/unload, configurations can be loaded many times over the lifetime of the device, so it is more appropriate to use normally allocated and freed memory. Signed-off-by: Mike Leach --- .../coresight/coresight-syscfg-configfs.c | 119 +++++++++++++----- 1 file changed, 88 insertions(+), 31 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-syscfg-configfs.c b/drivers/hwtracing/coresight/coresight-syscfg-configfs.c index 433ede94dd63..6e8c8db52d39 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg-configfs.c +++ b/drivers/hwtracing/coresight/coresight-syscfg-configfs.c @@ -14,7 +14,7 @@ static inline struct config_item_type *cscfg_create_ci_type(void) { struct config_item_type *ci_type; - ci_type = devm_kzalloc(cscfg_device(), sizeof(*ci_type), GFP_KERNEL); + ci_type = kzalloc(sizeof(*ci_type), GFP_KERNEL); if (ci_type) ci_type->ct_owner = THIS_MODULE; @@ -175,6 +175,19 @@ static struct config_item_type cscfg_config_preset_type = { .ct_attrs = cscfg_config_preset_attrs, }; + +/* walk list of presets and free the previously allocated memory */ +static void cscfg_destroy_preset_groups(struct config_group *cfg_view_group) +{ + struct cscfg_fs_preset *cfg_fs_preset; + struct config_group *p_group; + + list_for_each_entry(p_group, &cfg_view_group->default_groups, default_groups) { + cfg_fs_preset = container_of(p_group, struct cscfg_fs_preset, group); + kfree(cfg_fs_preset); + } +} + static int cscfg_add_preset_groups(struct cscfg_fs_config *cfg_view) { int preset_num; @@ -186,11 +199,12 @@ static int cscfg_add_preset_groups(struct cscfg_fs_config *cfg_view) return 0; for (preset_num = 1; preset_num <= config_desc->nr_presets; preset_num++) { - cfg_fs_preset = devm_kzalloc(cscfg_device(), - sizeof(struct cscfg_fs_preset), GFP_KERNEL); + cfg_fs_preset = kzalloc(sizeof(struct cscfg_fs_preset), GFP_KERNEL); - if (!cfg_fs_preset) + if (!cfg_fs_preset) { + cscfg_destroy_preset_groups(&cfg_view->group); return -ENOMEM; + } snprintf(name, CONFIGFS_ITEM_NAME_LEN, "preset%d", preset_num); cfg_fs_preset->preset_num = preset_num; @@ -204,14 +218,10 @@ static int cscfg_add_preset_groups(struct cscfg_fs_config *cfg_view) static struct config_group *cscfg_create_config_group(struct cscfg_config_desc *config_desc) { - struct cscfg_fs_config *cfg_view; - struct device *dev = cscfg_device(); + struct cscfg_fs_config *cfg_view = NULL; int err; - if (!dev) - return ERR_PTR(-EINVAL); - - cfg_view = devm_kzalloc(dev, sizeof(struct cscfg_fs_config), GFP_KERNEL); + cfg_view = kzalloc(sizeof(struct cscfg_fs_config), GFP_KERNEL); if (!cfg_view) return ERR_PTR(-ENOMEM); @@ -220,12 +230,21 @@ static struct config_group *cscfg_create_config_group(struct cscfg_config_desc * /* add in a preset dir for each preset */ err = cscfg_add_preset_groups(cfg_view); - if (err) + if (err) { + kfree(cfg_view); return ERR_PTR(err); - + } return &cfg_view->group; } +static void cscfg_destroy_config_group(struct config_group *group) +{ + struct cscfg_fs_config *cfg_view = container_of(group, struct cscfg_fs_config, group); + + cscfg_destroy_preset_groups(&cfg_view->group); + kfree(cfg_view); +} + /* attributes for features view */ static ssize_t cscfg_feat_description_show(struct config_item *item, char *page) @@ -314,6 +333,17 @@ static struct config_item_type cscfg_param_view_type = { .ct_attrs = cscfg_param_view_attrs, }; +/* walk the list of default groups - which were set as param items and remove */ +static void cscfg_destroy_params_group_items(struct config_group *params_group) +{ + struct cscfg_fs_param *param_item; + struct config_group *p_group; + + list_for_each_entry(p_group, ¶ms_group->default_groups, default_groups) { + param_item = container_of(p_group, struct cscfg_fs_param, group); + kfree(param_item); + } +} /* * configfs has far less functionality provided to add attributes dynamically than sysfs, * and the show and store fns pass the enclosing config_item so the actual attribute cannot @@ -322,15 +352,16 @@ static struct config_item_type cscfg_param_view_type = { static int cscfg_create_params_group_items(struct cscfg_feature_desc *feat_desc, struct config_group *params_group) { - struct device *dev = cscfg_device(); struct cscfg_fs_param *param_item; int i; /* parameter items - as groups with default_value attribute */ for (i = 0; i < feat_desc->nr_params; i++) { - param_item = devm_kzalloc(dev, sizeof(struct cscfg_fs_param), GFP_KERNEL); - if (!param_item) + param_item = kzalloc(sizeof(struct cscfg_fs_param), GFP_KERNEL); + if (!param_item) { + cscfg_destroy_params_group_items(params_group); return -ENOMEM; + } param_item->feat_desc = feat_desc; param_item->param_idx = i; config_group_init_type_name(¶m_item->group, @@ -343,27 +374,22 @@ static int cscfg_create_params_group_items(struct cscfg_feature_desc *feat_desc, static struct config_group *cscfg_create_feature_group(struct cscfg_feature_desc *feat_desc) { - struct cscfg_fs_feature *feat_view; - struct config_item_type *params_group_type; + struct cscfg_fs_feature *feat_view = NULL; + struct config_item_type *params_group_type = NULL; struct config_group *params_group = NULL; - struct device *dev = cscfg_device(); - int item_err; - - if (!dev) - return ERR_PTR(-EINVAL); + int err = -ENOMEM; - feat_view = devm_kzalloc(dev, sizeof(struct cscfg_fs_feature), GFP_KERNEL); + feat_view = kzalloc(sizeof(struct cscfg_fs_feature), GFP_KERNEL); if (!feat_view) return ERR_PTR(-ENOMEM); if (feat_desc->nr_params) { - params_group = devm_kzalloc(dev, sizeof(struct config_group), GFP_KERNEL); + params_group = kzalloc(sizeof(struct config_group), GFP_KERNEL); if (!params_group) - return ERR_PTR(-ENOMEM); - + goto exit_err_free_mem; params_group_type = cscfg_create_ci_type(); if (!params_group_type) - return ERR_PTR(-ENOMEM); + goto exit_err_free_mem; } feat_view->feat_desc = feat_desc; @@ -373,11 +399,36 @@ static struct config_group *cscfg_create_feature_group(struct cscfg_feature_desc if (params_group) { config_group_init_type_name(params_group, "params", params_group_type); configfs_add_default_group(params_group, &feat_view->group); - item_err = cscfg_create_params_group_items(feat_desc, params_group); - if (item_err) - return ERR_PTR(item_err); + err = cscfg_create_params_group_items(feat_desc, params_group); + if (err) + goto exit_err_free_mem; } return &feat_view->group; + +exit_err_free_mem: + kfree(feat_view); + kfree(params_group_type); + kfree(params_group); + return ERR_PTR(err); +} + +static void cscfg_destroy_feature_group(struct config_group *feat_group) +{ + struct cscfg_fs_feature *feat_view; + struct config_group *params_group = NULL; + + feat_view = container_of(feat_group, struct cscfg_fs_feature, group); + + /* params group is the first item on the default group list */ + if (!list_empty(&feat_group->default_groups)) { + params_group = list_first_entry(&feat_group->default_groups, + struct config_group, default_groups); + cscfg_destroy_params_group_items(params_group); + /* free the item type, then the group */ + kfree(params_group->cg_item.ci_type); + kfree(params_group); + } + kfree(feat_view); } static struct config_item_type cscfg_configs_type = { @@ -403,6 +454,8 @@ int cscfg_configfs_add_config(struct cscfg_config_desc *config_desc) err = configfs_register_group(&cscfg_configs_grp, new_group); if (!err) config_desc->fs_group = new_group; + else + cscfg_destroy_config_group(new_group); return err; } @@ -410,6 +463,7 @@ void cscfg_configfs_del_config(struct cscfg_config_desc *config_desc) { if (config_desc->fs_group) { configfs_unregister_group(config_desc->fs_group); + cscfg_destroy_config_group(config_desc->fs_group); config_desc->fs_group = NULL; } } @@ -434,9 +488,11 @@ int cscfg_configfs_add_feature(struct cscfg_feature_desc *feat_desc) new_group = cscfg_create_feature_group(feat_desc); if (IS_ERR(new_group)) return PTR_ERR(new_group); - err = configfs_register_group(&cscfg_features_grp, new_group); + err = configfs_register_group(&cscfg_features_grp, new_group); if (!err) feat_desc->fs_group = new_group; + else + cscfg_destroy_feature_group(new_group); return err; } @@ -444,6 +500,7 @@ void cscfg_configfs_del_feature(struct cscfg_feature_desc *feat_desc) { if (feat_desc->fs_group) { configfs_unregister_group(feat_desc->fs_group); + cscfg_destroy_feature_group(feat_desc->fs_group); feat_desc->fs_group = NULL; } } From patchwork Thu May 11 06:53:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Leach X-Patchwork-Id: 92372 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4161686vqo; Wed, 10 May 2023 23:55:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Zm9pGcvH90mSJO3aT+E6zafGPChjiNT4d02d0QLeXVEkffL4SdGS3wOBjmTCgqAgUVtA3 X-Received: by 2002:a17:90b:945:b0:24c:df8:8efa with SMTP id dw5-20020a17090b094500b0024c0df88efamr20229489pjb.39.1683788127320; Wed, 10 May 2023 23:55:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683788127; cv=none; d=google.com; s=arc-20160816; b=mnaPNhzbhncMb3Af0gsj5zxC0yptgITQODQN9rpuoY47nO8nJly8DvOKPGsJT5z7eV r7ZxIkJC9UYzkCn+aEPlXMsnlE+AHpw0h3Zj/KiiCTJvsIlDoyG9Yb6G0ctyCdEHPkR6 UThZvH5UJ8QFUwFyMXBAuYD2ZuhLCxuE6DlA9Wfv2FQmDEhRytPebrE8eKIQOc8abu47 zF+he3Yyr0ysW2T1E1ZGdeNOqU1phhi0f5IzYqKvxZo3q8/hV73r9Sfji0WanAxcix/j z16Pl2YAU4J4lj1kdmXfRMIZSTcCGnlEW0vT64vomjR3W8BuXB7e/bf6VQMSb2YKxpcf ft5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=eNo/unRaEGMgy+WDudBvlLmaOxK4JQaFVhpw20oCbZQ=; b=W40u8ey6hRAqoOa/DhjutaO1jovi5jO3pRukPHLXDt0eBlFue3rsmi947iSmRFbdfG wHuyuJs6THtFCszKXO1PbaALeN9lbGdvCwpLchrcqP6zZThXdyd0Z3loi01lD5YLvRL/ g/28PwwpqVQK/DgIsudfLLYhtDfag9l1pmQOSzIYvFBtYmv1+uQ8h91H6RqHaHaHOAmi q5zNsZ0UoIaBnKcpk6fKmHNQHxmaTLgBEf6tE5yuTLJLTslgKGi6YKZ/9A08dcc3Rrkx rOvdGjIijTX/6YWSLJZeKO8MUpAY8c0NxFGoSO3B5h2r7sgyFhRAsvwbRaZWqk9sgMwe o3mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JklId06A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nl16-20020a17090b385000b002502b3fa445si14244253pjb.14.2023.05.10.23.55.15; Wed, 10 May 2023 23:55:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JklId06A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237182AbjEKGxw (ORCPT + 99 others); Thu, 11 May 2023 02:53:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237179AbjEKGxl (ORCPT ); Thu, 11 May 2023 02:53:41 -0400 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 357ED4EF7 for ; Wed, 10 May 2023 23:53:38 -0700 (PDT) Received: by mail-wm1-x344.google.com with SMTP id 5b1f17b1804b1-3f315712406so271843115e9.0 for ; Wed, 10 May 2023 23:53:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683788016; x=1686380016; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=eNo/unRaEGMgy+WDudBvlLmaOxK4JQaFVhpw20oCbZQ=; b=JklId06AD1CoKYJaaeJ9VrVLkezkYR1Zl3lLFJjXdloTBzPrAIyh5BwEEQVXkNzh0a txWSnlG7wC8L4HHJXrx6Yn09XY7shZv9EqC3JXxzlbD/f/8PBvMaScqYfA+X3hTvmx5h pkpQTokJvf/5eF8lmYfjC8wTO0bMdLDSXIpeT1HS1TIgZz3DGiFYF51ONhKJKg2MGI2a FFzWmJg8vhdt87lDNrfxhafnt5ql4snedbHvefcGaKOh+Ako8QjyZeY/wPSR21djJt+H tW/c6Ns4unxJG+c5q92osyHIe/qYTqMhgKO+ljt9oz2mSExwW/GGUdJjRJbK6khUl+qy Xuhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683788016; x=1686380016; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eNo/unRaEGMgy+WDudBvlLmaOxK4JQaFVhpw20oCbZQ=; b=jPOO3iYR4iNS9HhCGpj+oSjBkzSutWZYd27ljXu025+NOUB/UpS2B2jzYtUYBlnz7C O3Bte57zKkgBjg2UDx7WP0GoHkrW4S6+q7qS0NongoLpUP486V1EYoTLGV6wLgN6sxUc bJnKmhhkSlMdySMuulNKtauf7AdNl3oZm6/Dryzb+fvsoTpOcbRAglig4fba72mlmydW 2HakBouvlL9WphhNqD5b0fsIO0kycb2XJzpmmuLgqr7WJDLbng5SPk7026oKhP6/UcKw fMtLBvMj8d9Gww82Z/W5hREjcQsWgPUZY9dRb05CrHdrqd5dTL2XkPQG/9UU1LUweUJO 17bA== X-Gm-Message-State: AC+VfDzRVCcGyTlRyURzec4ufo1NH7/SfJDJuXKMfGNCRTzUhLifZZ40 WhDDreTb9+QRsaz5QFc3JGOFTA== X-Received: by 2002:a05:600c:3792:b0:3f1:6ead:e396 with SMTP id o18-20020a05600c379200b003f16eade396mr12888919wmr.17.1683788016457; Wed, 10 May 2023 23:53:36 -0700 (PDT) Received: from linaro.org ([2a00:23c5:680a:d01:3110:8b33:cdf9:dea0]) by smtp.gmail.com with ESMTPSA id f8-20020a7bc8c8000000b003f4e8530696sm1038257wml.46.2023.05.10.23.53.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 23:53:35 -0700 (PDT) From: Mike Leach To: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org, linux-kernel@vger.kernel.org Cc: suzuki.poulose@arm.com, acme@kernel.org, james.clark@arm.com, Mike Leach Subject: [PATCH v6 3/6] coresight: configfs: Add attributes to load config tables at runtime Date: Thu, 11 May 2023 07:53:27 +0100 Message-Id: <20230511065330.26392-4-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230511065330.26392-1-mike.leach@linaro.org> References: <20230511065330.26392-1-mike.leach@linaro.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765579819692887890?= X-GMAIL-MSGID: =?utf-8?q?1765579819692887890?= The existing configfs structure for Coresight is extended to allow load and unload of Coresight configurations at runtime. This provides more flexibility than configurations defined at compile time in drivers or loadable kernel modules, and allows use in situations where adding additional kernel modules is undesireable or not permitted. This also removes any dependence on specific compile time kernel versions, improving portability and re-use. Configurations are validated against the hardware present on the device. The configurations are loaded onto all relevant devices in the Coresight sub-system atomically. Configurations will not be available for use until the whole system is updated, and the load will be un-wound if any part of the validation fails. These will then appear in configfs, in the existing 'configurations' and 'features' directories, as well as in the perf 'events' area ready for use. Runtime configuration tables are loaded via the 'load' attribute. This is a binary attribute - which the configfs infrastructure provides as a size limited buffer - limited to the defined maximum size of a Coresight configuration table. This provides a reliable load mechanism as tables are variable in size, dependent on the operations required. (note: this follows the example set by the ACPI table load which also uses binary attributes) The system reads the incoming data, which is validated as being formatted correctly by the table reader code. This will then create configuration(s) and/or feature(s) objects which are then loaded into the system, using the existing code - which validates correctness for use with the system hardware. These will then appear in configfs, in the 'configurations' and 'features' directories, ready for use. Configurations are unloaded using the 'unload_last' attribute. As configurations can be dependent on previously loaded items, the system enforces a strict unload ordering in reverse order of loading. A mutex is used to prevent load and unload operations from happening simultaneously. Further, a flag enabling configfs load/unload is provided, along with API calls to allow this functionality to be controlled during start-up and shut-down, and when configurations are loaded via loadable modules. This ensures that load/unload operations are strictly serialised. Signed-off-by: Mike Leach --- .../coresight/coresight-syscfg-configfs.c | 394 +++++++++++++++++- .../coresight/coresight-syscfg-configfs.h | 5 + .../hwtracing/coresight/coresight-syscfg.c | 101 ++++- .../hwtracing/coresight/coresight-syscfg.h | 6 +- 4 files changed, 492 insertions(+), 14 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-syscfg-configfs.c b/drivers/hwtracing/coresight/coresight-syscfg-configfs.c index 6e8c8db52d39..e0eb53afbb74 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg-configfs.c +++ b/drivers/hwtracing/coresight/coresight-syscfg-configfs.c @@ -5,10 +5,96 @@ */ #include +#include +#include #include "coresight-config.h" +#include "coresight-config-table.h" #include "coresight-syscfg-configfs.h" +/* prevent race in load / unload operations */ +static DEFINE_MUTEX(cfs_mutex); + +/* + * need to enable / disable load via configfs when + * initialising / shutting down the subsystem, or + * loading / unloading module. + */ +static bool cscfg_fs_load_enabled; + +/* + * Lockdep issues occur if deleting the config directory as part + * of the unload write operation. Therefore we schedule the main + * part of the unload to be completed as a work item + */ +struct cscfg_load_owner_info *unload_owner_info; + +/* complete the unload operation */ +static void cscfg_complete_unload(struct work_struct *work) +{ + mutex_lock(&cfs_mutex); + + if (!cscfg_fs_load_enabled || !unload_owner_info) { + pr_warn("cscfg: skipping unload completion\n"); + goto exit_unlock; + } + + if (!cscfg_unload_config_sets(unload_owner_info)) + cscfg_configfs_free_owner_info(unload_owner_info); + else + pr_err("cscfg: configfs configuration unload error\n"); + unload_owner_info = NULL; + +exit_unlock: + mutex_unlock(&cfs_mutex); + kfree(work); +} + +static int cscfg_schedule_unload(struct cscfg_load_owner_info *owner_info, const char *name) +{ + struct work_struct *work; + int err; + + work = kzalloc(sizeof(struct work_struct), GFP_KERNEL); + if (!work) + return -ENOMEM; + + /* set cscfg state as starting an unload operation */ + err = cscfg_set_unload_start(); + if (err) { + pr_err("Config unload %s: failed to set unload start flag\n", name); + kfree(work); + return err; + } + + INIT_WORK(work, cscfg_complete_unload); + unload_owner_info = owner_info; + schedule_work(work); + return 0; +} + +void cscfg_configfs_enable_fs_load(void) +{ + mutex_lock(&cfs_mutex); + cscfg_fs_load_enabled = true; + mutex_unlock(&cfs_mutex); +} + +void cscfg_configfs_disable_fs_load(void) +{ + mutex_lock(&cfs_mutex); + cscfg_fs_load_enabled = false; + mutex_unlock(&cfs_mutex); +} + +void cscfg_configfs_at_exit(void) +{ + mutex_lock(&cfs_mutex); + cscfg_fs_load_enabled = false; + unload_owner_info = NULL; + mutex_unlock(&cfs_mutex); +} + /* create a default ci_type. */ static inline struct config_item_type *cscfg_create_ci_type(void) { @@ -431,14 +517,306 @@ static void cscfg_destroy_feature_group(struct config_group *feat_group) kfree(feat_view); } -static struct config_item_type cscfg_configs_type = { +/* Attrib in configfs that allow load and unload of configuration binary tables */ + +/* free memory associated with a configfs loaded configuration & descriptors */ +void cscfg_configfs_free_owner_info(struct cscfg_load_owner_info *owner_info) +{ + struct cscfg_table_load_descs *load_descs = 0; + + if (!owner_info) + return; + + load_descs = (struct cscfg_table_load_descs *)(owner_info->owner_handle); + + if (load_descs) { + /* free the data allocated on table load, pointed to by load_descs */ + cscfg_table_free_load_descs(load_descs); + kfree(load_descs); + } + + kfree(owner_info); +} + +/* return load name if configfs owned element */ +const char *cscfg_configfs_get_load_name(struct cscfg_load_owner_info *owner_info) +{ + const char *name = "unknown"; + struct cscfg_table_load_descs *load_descs; + + if (!owner_info) + return name; + + load_descs = (struct cscfg_table_load_descs *)(owner_info->owner_handle); + if (owner_info->type == CSCFG_OWNER_CONFIGFS) + return load_descs->load_name; + + return name; +} + +/* load configuration table from buffer */ +static ssize_t cscfg_cfg_load_write(struct config_item *item, const void *buffer, size_t size) +{ + struct cscfg_table_load_descs *load_descs = 0; + struct cscfg_load_owner_info *owner_info = 0; + int err = 0; + + /* ensure we cannot simultaneously load and unload */ + if (!mutex_trylock(&cfs_mutex)) + return -EBUSY; + + /* check configfs load / unload ops are permitted */ + if (!cscfg_fs_load_enabled || unload_owner_info) { + err = -EBUSY; + goto exit_unlock; + } + + if (size > CSCFG_TABLE_MAXSIZE) { + pr_err("cscfg: Load error - Input file too large.\n"); + err = -EINVAL; + goto exit_unlock; + } + + load_descs = kzalloc(sizeof(struct cscfg_table_load_descs), GFP_KERNEL); + if (!load_descs) { + err = -ENOMEM; + goto exit_unlock; + } + + owner_info = kzalloc(sizeof(struct cscfg_load_owner_info), GFP_KERNEL); + if (!owner_info) { + kfree(load_descs); + err = -ENOMEM; + goto exit_unlock; + } + + owner_info->owner_handle = load_descs; + owner_info->type = CSCFG_OWNER_CONFIGFS; + + /* convert table into internal data structures */ + err = cscfg_table_read_buffer(buffer, size, load_descs); + if (err) { + pr_err("cscfg: Load error - Failed to read input buffer.\n"); + goto exit_memfree; + } + + err = cscfg_load_config_sets(load_descs->config_descs, load_descs->feat_descs, owner_info); + if (err) { + pr_err("cscfg: Load error - Failed to load configuaration table.\n"); + goto exit_memfree; + } + + mutex_unlock(&cfs_mutex); + return size; + +exit_memfree: + /* frees up owner_info and load_descs */ + cscfg_configfs_free_owner_info(owner_info); + +exit_unlock: + mutex_unlock(&cfs_mutex); + return err; +} +CONFIGFS_BIN_ATTR_WO(cscfg_cfg_, load, NULL, CSCFG_TABLE_MAXSIZE); + +static ssize_t cscfg_cfg_unload_store(struct config_item *item, const char *page, size_t count) +{ + struct cscfg_load_owner_info *owner_info = 0; + const char *load_name; + char *unload_name; + int err = -EINVAL; + + /* ensure we cannot simultaneously load and unload */ + if (!mutex_trylock(&cfs_mutex)) + return -EBUSY; + + /* check configfs load / unload ops are permitted & no ongoing unload */ + if (!cscfg_fs_load_enabled || unload_owner_info) { + err = -EBUSY; + goto exit_unlock; + } + + /* find the last loaded owner info block */ + owner_info = cscfg_find_last_loaded_cfg_owner(); + if (!owner_info) { + pr_err("cscfg: Unload error: Failed to find any loaded configuration\n"); + goto exit_unlock; + } + + if (owner_info->type != CSCFG_OWNER_CONFIGFS) { + pr_err("cscfg: Last loaded configuration not configfs loaded item\n"); + goto exit_unlock; + } + + /* get the load name for this item */ + load_name = cscfg_configfs_get_load_name(owner_info); + + /* input will have a \n - need to get rid */ + unload_name = kstrdup(page, GFP_KERNEL); + if (!unload_name) { + err = -ENOMEM; + goto exit_unlock; + } + unload_name[strlen(unload_name) - 1] = 0; + + /* must match input name to unload */ + if (!strcmp(load_name, unload_name)) { + + /* actual unload is scheduled as a work item */ + err = cscfg_schedule_unload(owner_info, load_name); + if (!err) + err = count; + } else { + pr_err("cscfg: unload name: %s; does not match last loaded: %s;\n", + unload_name, load_name); + } + kfree(unload_name); + +exit_unlock: + mutex_unlock(&cfs_mutex); + return err; +} +CONFIGFS_ATTR_WO(cscfg_cfg_, unload); + +/* create a string representing a loaded config based on owner info */ +static ssize_t cscfg_cfg_get_cfg_info_str_owner(struct cscfg_load_owner_info *owner_info, + char *buffer, ssize_t size) +{ + struct cscfg_table_load_descs *load_descs; + ssize_t size_used = 0; + int i; + static const char *load_type[] = { + "Built in driver", + "Loadable module", + "configfs dynamic load", + }; + + /* limited info for none dynamic loaded stuff */ + if (owner_info->type != CSCFG_OWNER_CONFIGFS) { + size_used = scnprintf(buffer, size, + "load name: [Not Set]\nload type: %s\n", + load_type[owner_info->type]); + goto buffer_done; + } + + /* configfs dynamic type will have all the info */ + load_descs = (struct cscfg_table_load_descs *)owner_info->owner_handle; + + /* first is the load name and type - need for unload request */ + size_used = scnprintf(buffer, size, "load name: %s\nload type: %s\n", + load_descs->load_name, + load_type[owner_info->type]); + + /* list of configurations loaded by this owner element */ + size_used += scnprintf(buffer + size_used, size - size_used, + "(configurations: "); + if (!(size_used < size)) + goto buffer_done; + + if (!load_descs->config_descs[0]) { + size_used += scnprintf(buffer + size_used, size - size_used, + " None )\n"); + if (!(size_used < size)) + goto buffer_done; + } else { + i = 0; + while (load_descs->config_descs[i] && (size_used < size)) { + size_used += scnprintf(buffer + size_used, + size - size_used, " %s", + load_descs->config_descs[i]->name); + i++; + } + size_used += + scnprintf(buffer + size_used, size - size_used, " )\n"); + } + if (!(size_used < size)) + goto buffer_done; + + /* list of features loaded by this owner element */ + size_used += scnprintf(buffer + size_used, size - size_used, "(features: "); + if (!(size_used < size)) + goto buffer_done; + + if (!load_descs->feat_descs[0]) { + size_used += + scnprintf(buffer + size_used, size - size_used, " None )\n"); + if (!(size_used < size)) + goto buffer_done; + } else { + i = 0; + while (load_descs->feat_descs[i] && (size_used < size)) { + size_used += scnprintf(buffer + size_used, + size - size_used, " %s", + load_descs->feat_descs[i]->name); + i++; + } + size_used += + scnprintf(buffer + size_used, size - size_used, " )\n"); + } + + /* done or buffer full */ +buffer_done: + return size_used; +} + +static ssize_t cscfg_cfg_show_last_load_show(struct config_item *item, char *page) +{ + struct cscfg_load_owner_info *owner_info = 0; + ssize_t size = 0; + + /* ensure we cannot simultaneously load and unload */ + if (!mutex_trylock(&cfs_mutex)) + return -EBUSY; + + /* check configfs load / unload ops are permitted & no ongoing unload */ + if (!cscfg_fs_load_enabled || unload_owner_info) { + size = -EBUSY; + goto exit_unlock; + } + + /* find the last loaded owner info block */ + owner_info = cscfg_find_last_loaded_cfg_owner(); + if (!owner_info) { + size = scnprintf(page, PAGE_SIZE, + "Failed to find any loaded configuration\n"); + goto exit_unlock; + } + + /* get string desc of last unload configuration from owner info */ + size = cscfg_cfg_get_cfg_info_str_owner(owner_info, page, PAGE_SIZE); + +exit_unlock: + mutex_unlock(&cfs_mutex); + return size; +} +CONFIGFS_ATTR_RO(cscfg_cfg_, show_last_load); + +static struct configfs_bin_attribute *cscfg_config_configfs_bin_attrs[] = { + &cscfg_cfg_attr_load, + NULL, +}; + +static struct configfs_attribute *cscfg_config_configfs_attrs[] = { + &cscfg_cfg_attr_unload, + &cscfg_cfg_attr_show_last_load, + NULL, +}; + +static struct config_item_type cscfg_configs_load_type = { + .ct_owner = THIS_MODULE, + .ct_attrs = cscfg_config_configfs_attrs, + .ct_bin_attrs = cscfg_config_configfs_bin_attrs, +}; + +static struct config_item_type cscfg_configs_grp_type = { .ct_owner = THIS_MODULE, }; +/* group for configurations dir */ static struct config_group cscfg_configs_grp = { .cg_item = { .ci_namebuf = "configurations", - .ci_type = &cscfg_configs_type, + .ci_type = &cscfg_configs_grp_type, }, }; @@ -508,18 +886,20 @@ void cscfg_configfs_del_feature(struct cscfg_feature_desc *feat_desc) int cscfg_configfs_init(struct cscfg_manager *cscfg_mgr) { struct configfs_subsystem *subsys; - struct config_item_type *ci_type; if (!cscfg_mgr) return -EINVAL; - ci_type = cscfg_create_ci_type(); - if (!ci_type) - return -ENOMEM; + /* load and unload by configfs initially disabled */ + cscfg_fs_load_enabled = false; + + /* no current unload operation in progress */ + unload_owner_info = NULL; + /* init subsystem group - with load and unload attributes */ subsys = &cscfg_mgr->cfgfs_subsys; config_item_set_name(&subsys->su_group.cg_item, CSCFG_FS_SUBSYS_NAME); - subsys->su_group.cg_item.ci_type = ci_type; + subsys->su_group.cg_item.ci_type = &cscfg_configs_load_type; config_group_init(&subsys->su_group); mutex_init(&subsys->su_mutex); diff --git a/drivers/hwtracing/coresight/coresight-syscfg-configfs.h b/drivers/hwtracing/coresight/coresight-syscfg-configfs.h index 373d84d43268..a7d1fec967ef 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg-configfs.h +++ b/drivers/hwtracing/coresight/coresight-syscfg-configfs.h @@ -45,5 +45,10 @@ int cscfg_configfs_add_config(struct cscfg_config_desc *config_desc); int cscfg_configfs_add_feature(struct cscfg_feature_desc *feat_desc); void cscfg_configfs_del_config(struct cscfg_config_desc *config_desc); void cscfg_configfs_del_feature(struct cscfg_feature_desc *feat_desc); +const char *cscfg_configfs_get_load_name(struct cscfg_load_owner_info *owner_info); +void cscfg_configfs_free_owner_info(struct cscfg_load_owner_info *owner_info); +void cscfg_configfs_enable_fs_load(void); +void cscfg_configfs_disable_fs_load(void); +void cscfg_configfs_at_exit(void); #endif /* CORESIGHT_SYSCFG_CONFIGFS_H */ diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtracing/coresight/coresight-syscfg.c index 11138a9762b0..500af8152769 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg.c +++ b/drivers/hwtracing/coresight/coresight-syscfg.c @@ -554,6 +554,22 @@ static int cscfg_fs_register_cfgs_feats(struct cscfg_config_desc **config_descs, return 0; } +/* + * check owner info and if module owner, disable / enable + * configfs load ops. + */ +static void cscfg_check_disable_fs_load(struct cscfg_load_owner_info *owner_info) +{ + if (owner_info->type == CSCFG_OWNER_MODULE) + cscfg_configfs_disable_fs_load(); +} + +static void cscfg_check_enable_fs_load(struct cscfg_load_owner_info *owner_info) +{ + if (owner_info->type == CSCFG_OWNER_MODULE) + cscfg_configfs_enable_fs_load(); +} + /** * cscfg_load_config_sets - API function to load feature and config sets. * @@ -578,10 +594,13 @@ int cscfg_load_config_sets(struct cscfg_config_desc **config_descs, { int err = 0; + /* if this load is by module owner, need to disable configfs load/unload */ + cscfg_check_disable_fs_load(owner_info); + mutex_lock(&cscfg_mutex); if (cscfg_mgr->load_state != CSCFG_NONE) { - mutex_unlock(&cscfg_mutex); - return -EBUSY; + err = -EBUSY; + goto exit_unlock; } cscfg_mgr->load_state = CSCFG_LOAD; @@ -616,7 +635,7 @@ int cscfg_load_config_sets(struct cscfg_config_desc **config_descs, /* mark any new configs as available for activation */ cscfg_set_configs_available(config_descs); - goto exit_unlock; + goto exit_clear_state; err_clean_cfs: /* cleanup after error registering with configfs */ @@ -631,9 +650,13 @@ int cscfg_load_config_sets(struct cscfg_config_desc **config_descs, err_clean_load: cscfg_unload_owned_cfgs_feats(owner_info); -exit_unlock: +exit_clear_state: cscfg_mgr->load_state = CSCFG_NONE; + +exit_unlock: mutex_unlock(&cscfg_mutex); + + cscfg_check_enable_fs_load(owner_info); return err; } EXPORT_SYMBOL_GPL(cscfg_load_config_sets); @@ -659,8 +682,12 @@ int cscfg_unload_config_sets(struct cscfg_load_owner_info *owner_info) int err = 0; struct cscfg_load_owner_info *load_list_item = NULL; + /* if this unload is by module owner, need to disable configfs load/unload */ + cscfg_check_disable_fs_load(owner_info); + mutex_lock(&cscfg_mutex); - if (cscfg_mgr->load_state != CSCFG_NONE) { + if ((cscfg_mgr->load_state != CSCFG_NONE) && + (cscfg_mgr->load_state != CSCFG_UNLOAD_START)) { mutex_unlock(&cscfg_mutex); return -EBUSY; } @@ -705,10 +732,44 @@ int cscfg_unload_config_sets(struct cscfg_load_owner_info *owner_info) exit_unlock: cscfg_mgr->load_state = CSCFG_NONE; mutex_unlock(&cscfg_mutex); + + cscfg_check_enable_fs_load(owner_info); return err; } EXPORT_SYMBOL_GPL(cscfg_unload_config_sets); +int cscfg_set_unload_start(void) +{ + int ret = 0; + + mutex_lock(&cscfg_mutex); + if (cscfg_mgr->load_state != CSCFG_NONE) + ret = -EBUSY; + else + cscfg_mgr->load_state = CSCFG_UNLOAD_START; + mutex_unlock(&cscfg_mutex); + + return ret; +} +EXPORT_SYMBOL_GPL(cscfg_set_unload_start); + +/* find the last loaded config owner info */ +struct cscfg_load_owner_info *cscfg_find_last_loaded_cfg_owner(void) +{ + struct cscfg_load_owner_info *owner_info = NULL; + + mutex_lock(&cscfg_mutex); + + if (!list_empty(&cscfg_mgr->load_order_list)) + owner_info = list_last_entry(&cscfg_mgr->load_order_list, + struct cscfg_load_owner_info, item); + + + mutex_unlock(&cscfg_mutex); + return owner_info; + +} + /* Handle coresight device registration and add configs and features to devices */ /* iterate through config lists and load matching configs to device */ @@ -881,7 +942,7 @@ static int _cscfg_activate_config(unsigned long cfg_hash) struct cscfg_config_desc *config_desc; int err = -EINVAL; - if (cscfg_mgr->load_state == CSCFG_UNLOAD) + if (cscfg_mgr->load_state >= CSCFG_UNLOAD) return -EBUSY; list_for_each_entry(config_desc, &cscfg_mgr->config_desc_list, item) { @@ -1206,6 +1267,7 @@ static int cscfg_create_device(void) static void cscfg_unload_cfgs_on_exit(void) { struct cscfg_load_owner_info *owner_info = NULL; + bool free_configfs_owner = false; /* * grab the mutex - even though we are exiting, some configfs files @@ -1240,6 +1302,23 @@ static void cscfg_unload_cfgs_on_exit(void) */ pr_err("cscfg: ERROR: prior module failed to unload configuration\n"); goto list_remove; + + case CSCFG_OWNER_CONFIGFS: + /* + * configfs loaded items may still be present if the user did not + * unload them during the session. These have dynamically allocated + * descriptor tables (unlike the two types above that are statically + * allocated at compile time) + */ + pr_info("cscfg: unloading configfs loaded configuration %s\n", + cscfg_configfs_get_load_name(owner_info)); + + /* + * as this is not being unloaded by configfs, need to flag the + * requirement to free up the descriptors. + */ + free_configfs_owner = true; + break; } /* remove from configfs - outside the scope of the list mutex */ @@ -1253,6 +1332,12 @@ static void cscfg_unload_cfgs_on_exit(void) list_remove: /* remove from load order list */ list_del(&owner_info->item); + + /* configfs owned dynamic loaded config, free memory now */ + if (free_configfs_owner) { + cscfg_configfs_free_owner_info(owner_info); + free_configfs_owner = false; + } } mutex_unlock(&cscfg_mutex); } @@ -1284,6 +1369,9 @@ int __init cscfg_init(void) if (err) goto exit_err; + /* can now allow load / unload from configfs */ + cscfg_configfs_enable_fs_load(); + dev_info(cscfg_device(), "CoreSight Configuration manager initialised"); return 0; @@ -1294,5 +1382,6 @@ int __init cscfg_init(void) void cscfg_exit(void) { + cscfg_configfs_at_exit(); cscfg_clear_device(); } diff --git a/drivers/hwtracing/coresight/coresight-syscfg.h b/drivers/hwtracing/coresight/coresight-syscfg.h index 66e2db890d82..530b32956ab6 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg.h +++ b/drivers/hwtracing/coresight/coresight-syscfg.h @@ -20,7 +20,8 @@ enum cscfg_load_ops { CSCFG_NONE, CSCFG_LOAD, - CSCFG_UNLOAD + CSCFG_UNLOAD, + CSCFG_UNLOAD_START, /* unload started by fs, will be completed later */ }; /** @@ -79,6 +80,7 @@ struct cscfg_registered_csdev { enum cscfg_load_owner_type { CSCFG_OWNER_PRELOAD, CSCFG_OWNER_MODULE, + CSCFG_OWNER_CONFIGFS, }; /** @@ -107,6 +109,7 @@ int cscfg_update_feat_param_val(struct cscfg_feature_desc *feat_desc, int param_idx, u64 value); int cscfg_config_sysfs_activate(struct cscfg_config_desc *cfg_desc, bool activate); void cscfg_config_sysfs_set_preset(int preset); +struct cscfg_load_owner_info *cscfg_find_last_loaded_cfg_owner(void); /* syscfg manager external API */ int cscfg_load_config_sets(struct cscfg_config_desc **cfg_descs, @@ -123,5 +126,6 @@ int cscfg_csdev_enable_active_config(struct coresight_device *csdev, unsigned long cfg_hash, int preset); void cscfg_csdev_disable_active_config(struct coresight_device *csdev); void cscfg_config_sysfs_get_active_cfg(unsigned long *cfg_hash, int *preset); +int cscfg_set_unload_start(void); #endif /* CORESIGHT_SYSCFG_H */ From patchwork Thu May 11 06:53:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Leach X-Patchwork-Id: 92369 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4161636vqo; Wed, 10 May 2023 23:55:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4ggo4e8YFyq334UcQPQok3b3VPuHBOEeJeZPRUQHeqY5RXIGhVdXrjAwS/LxC5dfZvZJWC X-Received: by 2002:a05:6a00:1343:b0:646:c30d:e9c2 with SMTP id k3-20020a056a00134300b00646c30de9c2mr13889559pfu.28.1683788121277; Wed, 10 May 2023 23:55:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683788121; cv=none; d=google.com; s=arc-20160816; b=sZXt83yI6tHty5kxC93F/q4D9NMFYh6wIVXXmbhi4PS7wq0+W6w+3C/00xEwYlIoUT +S1P0/7LkLYYop+EW5bg1Ix4ZnSBbSbdQ3hm4JKg2o5bpbuKXBTW/Ztj644+unFtudkb TuTbxz8lWVH9siJHiH2qAFi/koYb6btfSgHQb9Jtv6t/rFiTz0z89uIYEyIHXwmDcMVT QjRv8Iwi1AZBVKaBDVvYLIGUnf/vNvaLMUyqGt5kNf0YUOHSP57N0ToRMw0vINpHL2Uc KR1dsJ2JRWLYJNR6CZVyCwO5lwPj7BwxIaI838OyJELJEUqyEdZ7/8gl3/i+zLM0UYYg 8v0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=KufGNqo65itbtJV3rnh/P/Cd+7Use3BzqsEUJeyfZoI=; b=OwHsT+a4xkMPRJgXOwcIF1YGMwhdq5IUIfqITxsLcdKEr1iKRbCWAWgK5tWzLyhMmm /zIH3nO5du88ctNagfswb1tB5Aeq7XF7vfmgInzqzE/l5jziuDg9R2nBTpPEz6I8/JAS Nb5cg7ppg64kW4WTxUKKrLSIxcqMufJJQJTolCyD1MCElAQsWNhYkRVKwy25nxljAioM 8dYn6U8uPKsOl/uitBW77KEWh8rPvsFCwiqDfxWFeDPZsShCoOR7+QPtwr9F8eDPJYJJ 6DTU97NaXgyjduTblbpFBjrD68YLJX1eCVz7Lqu9vlYVqKbsTQYJaKYomAPA7sddEj8j HwnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=a3p9JbJf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h26-20020aa796da000000b0063b630df52asi6904721pfq.252.2023.05.10.23.55.09; Wed, 10 May 2023 23:55:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=a3p9JbJf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237209AbjEKGxs (ORCPT + 99 others); Thu, 11 May 2023 02:53:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237182AbjEKGxl (ORCPT ); Thu, 11 May 2023 02:53:41 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B7205B8B for ; Wed, 10 May 2023 23:53:39 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-3f41d087bd3so36655345e9.3 for ; Wed, 10 May 2023 23:53:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683788018; x=1686380018; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=KufGNqo65itbtJV3rnh/P/Cd+7Use3BzqsEUJeyfZoI=; b=a3p9JbJfiX/nQDvzxZeTxtzNbc2/q9Nx6SwGnAJ3wuAa3s4FnoE17U/75F1Lmd8cj/ 10KRCepQI9tUeWi53VDLI+imYqf69hjkuE304JPI8BNC07hIj98KhK1+W7nwcVAGjais 3+IWon6P1viLfYOqFlHFdOOUIjAfDIgnR35f1HBYwIKLXAafSN8BaYfAqMsJtgUNY3Vm 1mrNxcX9IUj5RTK/RA8g2o3XGyJXGuHlc24gIZv7nBjQ/WPKZxsdXDjo0ugg3yxLKbF0 RG5noc6kUngUXpGbWIvsefweT4ZoFFbb8PdfMqdZJHS5lYjVviP6aR6sPilCkVeGiua0 5l8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683788018; x=1686380018; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KufGNqo65itbtJV3rnh/P/Cd+7Use3BzqsEUJeyfZoI=; b=cViKjkF8I4DV25nrT/vnoyFgLfPP46USrMMBwJU4i3kXvM0G8S00G1krC9D02jmLuy TcXsRjRb1Pd0qq8UyjzBuuiNyZ2d9FmM1v33NIxS4gr2MGdUucC4VLIVPXFzXWpIxyAY +yr4/SjuGJ6WXAI3cdvzUY3wnSg/LPBk610oVWPCN5ZsCWDig3XfX5CXPOWFcbYLGRx6 bJOoCZqwGgtFyfftQcr4osgz7s9oREr3i/ESHhhw1DX/k9RzTTQQwIpCmYMtxk4n9gH0 picuOcul9WJ2lU9mVZQME9jEVN3DIoA8LhkZlSxeyfDh0vb17OVhvQyP/Qrap537Qqmk lR9Q== X-Gm-Message-State: AC+VfDxhR5496usWaMMe4FNw61uq8ROVT5haifPAeGwGXvf09GwbYFhx chw4RcBoBhtGK06PRj48snNH3A== X-Received: by 2002:a7b:c8c6:0:b0:3f4:2220:28d5 with SMTP id f6-20020a7bc8c6000000b003f4222028d5mr9949577wml.29.1683788017635; Wed, 10 May 2023 23:53:37 -0700 (PDT) Received: from linaro.org ([2a00:23c5:680a:d01:3110:8b33:cdf9:dea0]) by smtp.gmail.com with ESMTPSA id f8-20020a7bc8c8000000b003f4e8530696sm1038257wml.46.2023.05.10.23.53.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 23:53:37 -0700 (PDT) From: Mike Leach To: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org, linux-kernel@vger.kernel.org Cc: suzuki.poulose@arm.com, acme@kernel.org, james.clark@arm.com, Mike Leach Subject: [PATCH v6 4/6] coresight: config: extract shared structures to common header file Date: Thu, 11 May 2023 07:53:28 +0100 Message-Id: <20230511065330.26392-5-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230511065330.26392-1-mike.leach@linaro.org> References: <20230511065330.26392-1-mike.leach@linaro.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765579813323821544?= X-GMAIL-MSGID: =?utf-8?q?1765579813323821544?= Extract shared user space and kernel common structures from coresight-config.h into common header file coresight-config-desc.h Abstract memory allocation in coresight-config-table.c to allow read table functions to be run in userspace and kernel drivers. Signed-off-by: Mike Leach --- .../coresight/coresight-config-desc.h | 105 ++++++++++++++++ .../coresight/coresight-config-table.c | 66 +++++----- .../hwtracing/coresight/coresight-config.h | 119 ++++-------------- 3 files changed, 165 insertions(+), 125 deletions(-) create mode 100644 drivers/hwtracing/coresight/coresight-config-desc.h diff --git a/drivers/hwtracing/coresight/coresight-config-desc.h b/drivers/hwtracing/coresight/coresight-config-desc.h new file mode 100644 index 000000000000..4a487743d7e2 --- /dev/null +++ b/drivers/hwtracing/coresight/coresight-config-desc.h @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#ifndef _CORESIGHT_CORESIGHT_CONFIG_DESC_H +#define _CORESIGHT_CORESIGHT_CONFIG_DESC_H + +/* Coresight Descriptors common to kernel and userspace applications */ +/* + * Register type flags for register value descriptor: + * describe how the value is interpreted, and handled. + */ +#define CS_CFG_REG_TYPE_STD 0x80 /* reg is standard reg */ +#define CS_CFG_REG_TYPE_RESOURCE 0x40 /* reg is a resource */ +#define CS_CFG_REG_TYPE_VAL_PARAM 0x08 /* reg value uses param */ +#define CS_CFG_REG_TYPE_VAL_MASK 0x04 /* reg value bit masked */ +#define CS_CFG_REG_TYPE_VAL_64BIT 0x02 /* reg value 64 bit */ +#define CS_CFG_REG_TYPE_VAL_SAVE 0x01 /* reg value save on disable */ + +/* + * flags defining what device class a feature will match to when processing a + * system configuration - used by config data and devices. + */ +#define CS_CFG_MATCH_CLASS_SRC_ALL 0x0001 /* match any source */ +#define CS_CFG_MATCH_CLASS_SRC_ETM4 0x0002 /* match any ETMv4 device */ + +/* flags defining device instance matching - used in config match desc data. */ +#define CS_CFG_MATCH_INST_ANY 0x80000000 /* any instance of a class */ + +/* + * Limit number of presets in a configuration + * This is related to the number of bits (4) we use to select the preset on + * the perf command line. Preset 0 is always none selected. + * See PMU_FORMAT_ATTR(preset, "config:0-3") in coresight-etm-perf.c + */ +#define CS_CFG_CONFIG_PRESET_MAX 15 + +/** + * Parameter descriptor for a device feature. + * + * @name: Name of parameter. + * @value: Initial or default value. + */ +struct cscfg_parameter_desc { + const char *name; + u64 value; +}; + +/** + * Representation of register value and a descriptor of register usage. + * + * Used as a descriptor in the feature descriptors. + * Used as a value in when in a feature loading into a csdev. + * + * Supports full 64 bit register value, or 32 bit value with optional mask + * value. + * + * @type: define register usage and interpretation. + * @offset: the address offset for register in the hardware device (per device specification). + * @hw_info: optional hardware device type specific information. (ETM / CTI specific etc) + * @val64: 64 bit value. + * @val32: 32 bit value. + * @mask32: 32 bit mask when using 32 bit value to access device register - if mask type. + * @param_idx: parameter index value into parameter array if param type. + */ +struct cscfg_regval_desc { + struct { + u32 type:8; + u32 offset:12; + u32 hw_info:12; + }; + union { + u64 val64; + struct { + u32 val32; + u32 mask32; + }; + u32 param_idx; + }; +}; + +/** + * Dynamically loaded descriptor arrays. + * + * For builtin or module loaded configurations / features these are + * statically defined at compile time. + + * For a dynamic load at runtime, using a config table, (e.g. load from + * configfs) we create the arrays dynamically so need a structure to + * manage these. + * + * @config_descs: array of config descriptor pointers. + * @feat_descs: array of feature descriptor pointers. + * @load_name: user readable name which may be used to unload later. + * Will be name of first config if present, or first feature. + */ +struct cscfg_table_load_descs { + struct cscfg_config_desc **config_descs; + struct cscfg_feature_desc **feat_descs; + char *load_name; +}; + +#endif /* _CORESIGHT_CORESIGHT_CONFIG_DESC_H */ diff --git a/drivers/hwtracing/coresight/coresight-config-table.c b/drivers/hwtracing/coresight/coresight-config-table.c index 0a8f017d76d2..6216dffc9f9e 100644 --- a/drivers/hwtracing/coresight/coresight-config-table.c +++ b/drivers/hwtracing/coresight/coresight-config-table.c @@ -79,6 +79,7 @@ static int cscfg_table_read_elem_str(const u8 *buffer, const int buflen, int *bu struct cscfg_table_elem_str *elem_str) { int used = *buf_used; + const u8 *str; if ((buflen - used) < sizeof(u16)) return -EINVAL; @@ -88,11 +89,13 @@ static int cscfg_table_read_elem_str(const u8 *buffer, const int buflen, int *bu if ((buflen - used) < elem_str->str_len) return -EINVAL; + str = buffer + used; + /* check for 0 termination */ - if (buffer[used + (elem_str->str_len - 1)] != 0) + if (str[elem_str->str_len - 1] != 0) return -EINVAL; - elem_str->str = kstrdup((char *)(buffer + used), GFP_KERNEL); + elem_str->str = cscfg_strdup((char *)str); used += elem_str->str_len; *buf_used = used; @@ -103,12 +106,13 @@ static int cscfg_table_alloc_desc_arrays(struct cscfg_table_load_descs *desc_arr int nr_features, int nr_configs) { /* arrays are 0 terminated - nr_configs & nr_features elements */ - desc_arrays->config_descs = kcalloc(nr_configs + 1, sizeof(struct cscfg_config_desc *), - GFP_KERNEL); + desc_arrays->config_descs = cscfg_calloc(nr_configs + 1, + sizeof(struct cscfg_config_desc *)); if (!desc_arrays->config_descs) return -ENOMEM; - desc_arrays->feat_descs = kcalloc(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; @@ -123,24 +127,24 @@ static void cscfg_table_free_config_desc(struct cscfg_config_desc *config_desc) return; /* free presets */ - kfree(config_desc->presets); + cscfg_free((void *)config_desc->presets); /* free feat ref strings */ if (config_desc->nr_feat_refs) { /* each string */ for (i = 0; i < config_desc->nr_feat_refs; i++) - kfree(config_desc->feat_ref_names[i]); + cscfg_free((void *)config_desc->feat_ref_names[i]); /* and the char * array */ - kfree(config_desc->feat_ref_names); + cscfg_free((void *)config_desc->feat_ref_names); } /* next the strings */ - kfree(config_desc->name); - kfree(config_desc->description); + cscfg_free((void *)config_desc->name); + cscfg_free((void *)config_desc->description); /* finally the struct itself */ - kfree(config_desc); + cscfg_free((void *)config_desc); } static int cscfg_table_read_elem_config(const u8 *buffer, const int buflen, int *buf_used, @@ -165,7 +169,7 @@ static int cscfg_table_read_elem_config(const u8 *buffer, const int buflen, int return 0; /* we have a config - allocate the descriptor */ - config_desc = kzalloc(sizeof(struct cscfg_config_desc), GFP_KERNEL); + config_desc = cscfg_zalloc(sizeof(struct cscfg_config_desc)); if (!config_desc) return -ENOMEM; @@ -177,7 +181,7 @@ static int cscfg_table_read_elem_config(const u8 *buffer, const int buflen, int /* allocate load name if not set */ if (!desc_arrays->load_name) - desc_arrays->load_name = kstrdup(config_desc->name, GFP_KERNEL); + desc_arrays->load_name = cscfg_strdup(config_desc->name); /* read the description string */ err = cscfg_table_read_elem_str(buffer, buflen, &used, &elem_str); @@ -195,7 +199,7 @@ static int cscfg_table_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 = kcalloc(nr_preset_vals, sizeof(u64), GFP_KERNEL); + presets = cscfg_calloc(nr_preset_vals, sizeof(u64)); if (!presets) return -ENOMEM; @@ -210,8 +214,8 @@ static int cscfg_table_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 = kcalloc(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; @@ -255,17 +259,17 @@ static void cscfg_table_free_feat_desc(struct cscfg_feature_desc *feat_desc) return; /* free up the register descriptor array */ - kfree(feat_desc->regs_desc); + cscfg_free((void *)feat_desc->regs_desc); /* free up the parameters array */ - kfree(feat_desc->params_desc); + cscfg_free((void *)feat_desc->params_desc); /* name and description strings */ - kfree(feat_desc->name); - kfree(feat_desc->description); + cscfg_free((void *)feat_desc->name); + cscfg_free((void *)feat_desc->description); /* finally the struct itself */ - kfree(feat_desc); + cscfg_free((void *)feat_desc); } static int cscfg_table_read_elem_feature(const u8 *buffer, const int buflen, int *buf_used, @@ -280,7 +284,7 @@ static int cscfg_table_read_elem_feature(const u8 *buffer, const int buflen, int u32 val32; /* allocate the feature descriptor object */ - feat_desc = kzalloc(sizeof(struct cscfg_feature_desc), GFP_KERNEL); + feat_desc = cscfg_zalloc(sizeof(struct cscfg_feature_desc)); if (!feat_desc) return -ENOMEM; @@ -300,7 +304,7 @@ static int cscfg_table_read_elem_feature(const u8 *buffer, const int buflen, int /* allocate load name if not set previously by config */ if (!desc_arrays->load_name) - desc_arrays->load_name = kstrdup(feat_desc->name, GFP_KERNEL); + desc_arrays->load_name = cscfg_strdup(feat_desc->name); /* read the description string */ err = cscfg_table_read_elem_str(buffer, buflen, &used, &elem_str); @@ -323,8 +327,8 @@ static int cscfg_table_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 = kcalloc(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; @@ -338,8 +342,8 @@ static int cscfg_table_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 = kcalloc(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++) { @@ -421,7 +425,7 @@ void cscfg_table_free_load_descs(struct cscfg_table_load_descs *desc_arrays) } /* finally free up the load descs pointer arrays */ - kfree(desc_arrays->config_descs); - kfree(desc_arrays->feat_descs); - kfree(desc_arrays->load_name); + cscfg_free(desc_arrays->config_descs); + cscfg_free(desc_arrays->feat_descs); + cscfg_free(desc_arrays->load_name); } diff --git a/drivers/hwtracing/coresight/coresight-config.h b/drivers/hwtracing/coresight/coresight-config.h index ea3aaf0d129b..8b298e9a3776 100644 --- a/drivers/hwtracing/coresight/coresight-config.h +++ b/drivers/hwtracing/coresight/coresight-config.h @@ -10,101 +10,10 @@ #include #include -/* CoreSight Configuration Management - component and system wide configuration */ - -/* - * Register type flags for register value descriptor: - * describe how the value is interpreted, and handled. - */ -#define CS_CFG_REG_TYPE_STD 0x80 /* reg is standard reg */ -#define CS_CFG_REG_TYPE_RESOURCE 0x40 /* reg is a resource */ -#define CS_CFG_REG_TYPE_VAL_PARAM 0x08 /* reg value uses param */ -#define CS_CFG_REG_TYPE_VAL_MASK 0x04 /* reg value bit masked */ -#define CS_CFG_REG_TYPE_VAL_64BIT 0x02 /* reg value 64 bit */ -#define CS_CFG_REG_TYPE_VAL_SAVE 0x01 /* reg value save on disable */ - -/* - * flags defining what device class a feature will match to when processing a - * system configuration - used by config data and devices. - */ -#define CS_CFG_MATCH_CLASS_SRC_ALL 0x0001 /* match any source */ -#define CS_CFG_MATCH_CLASS_SRC_ETM4 0x0002 /* match any ETMv4 device */ - -/* flags defining device instance matching - used in config match desc data. */ -#define CS_CFG_MATCH_INST_ANY 0x80000000 /* any instance of a class */ - -/* - * Limit number of presets in a configuration - * This is related to the number of bits (4) we use to select the preset on - * the perf command line. Preset 0 is always none selected. - * See PMU_FORMAT_ATTR(preset, "config:0-3") in coresight-etm-perf.c - */ -#define CS_CFG_CONFIG_PRESET_MAX 15 - -/** - * Parameter descriptor for a device feature. - * - * @name: Name of parameter. - * @value: Initial or default value. - */ -struct cscfg_parameter_desc { - const char *name; - u64 value; -}; - -/** - * Representation of register value and a descriptor of register usage. - * - * Used as a descriptor in the feature descriptors. - * Used as a value in when in a feature loading into a csdev. - * - * Supports full 64 bit register value, or 32 bit value with optional mask - * value. - * - * @type: define register usage and interpretation. - * @offset: the address offset for register in the hardware device (per device specification). - * @hw_info: optional hardware device type specific information. (ETM / CTI specific etc) - * @val64: 64 bit value. - * @val32: 32 bit value. - * @mask32: 32 bit mask when using 32 bit value to access device register - if mask type. - * @param_idx: parameter index value into parameter array if param type. - */ -struct cscfg_regval_desc { - struct { - u32 type:8; - u32 offset:12; - u32 hw_info:12; - }; - union { - u64 val64; - struct { - u32 val32; - u32 mask32; - }; - u32 param_idx; - }; -}; - -/** - * Dynamically loaded descriptor arrays. - * - * For builtin or module loaded configurations / features these are - * statically defined at compile time. +/* common descriptor definitions */ +#include "coresight-config-desc.h" - * For a dynamic load at runtime, using a config table, (e.g. load from - * configfs) we create the arrays dynamically so need a structure to - * manage these. - * - * @config_descs: array of config descriptor pointers. - * @feat_descs: array of feature descriptor pointers. - * @load_name: user readable name which may be used to unload later. - * Will be name of first config if present, or first feature. - */ -struct cscfg_table_load_descs { - struct cscfg_config_desc **config_descs; - struct cscfg_feature_desc **feat_descs; - char *load_name; -}; +/* CoreSight Configuration Management - component and system wide configuration */ /** * Device feature descriptor - combination of registers and parameters to @@ -280,4 +189,26 @@ 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); + +/* Kernel allocators for descriptors in common config table read code */ +static inline void *cscfg_calloc(size_t num, size_t size) +{ + return kcalloc(num, size, GFP_KERNEL); +} + +static inline char *cscfg_strdup(const char *str) +{ + return kstrdup(str, GFP_KERNEL); +} + +static inline void *cscfg_zalloc(size_t size) +{ + return kzalloc(size, GFP_KERNEL); +} + +static inline void cscfg_free(void *mem) +{ + kfree(mem); +} + #endif /* _CORESIGHT_CORESIGHT_CONFIG_H */ From patchwork Thu May 11 06:53:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Leach X-Patchwork-Id: 92374 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4161968vqo; Wed, 10 May 2023 23:56:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ59Bbi33DokS0H8NxOThgx6JyuOXpFq3ff4kpnPnTgYbGDW2mpjQjl/yWDWl43z1QNplXm/ X-Received: by 2002:a17:902:f54f:b0:1ac:750e:33d6 with SMTP id h15-20020a170902f54f00b001ac750e33d6mr17201138plf.23.1683788172933; Wed, 10 May 2023 23:56:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683788172; cv=none; d=google.com; s=arc-20160816; b=vqVeDBepNddkBBjmCNdhlN6lC6omCV2Lj5HcTB6CXqkH/MoZ21zVdxOGS6d3i1B1Ke VRuLEZmT46kjyBBsQLwqIP8BOrd7boU1nbz+MKPMLBpJOtN9B9JQtkOvdroX3TQWUqAZ lbsGq607tOrzVUALnam+ByLm5UPCt99KMioFR/PpMRz11XJubAUmIYrEjzV3Sbd4tlVp XVsKVO2xQAVrFCAEKAvBKyd3qX79YJuvFe15/3ektRtqHLwOLLlOpcbcFmqafreWmgyr G6MyQHZmMrW8Ct8h6WkiDcS8gqGDmHeOW76cs8N5yjTrUb6uJyIuaQixd669d+9c3ZIU Lv5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=hUvTc2zD4JZvJuDgGtGCVbueY/7mtRbz8rOovwWY6Kg=; b=FNageTq8q+mSXV0RPErUPjaZZNFEGut2gHUbSTpQeDOGduuRtEjErDSubam8Gmu15x gDnxB2vEEpbfge51SIpNSxd4N6nKYLV6p9BUHB4FElBG+0Cr1lDPkIzD6cpFUUhWMazd PpXuGw/ZBwZlR6lZxr0qcJQRyjmzdFJNFeDLnxV2pcTMmEKBlfXx3QR3deQb6vh8zZYb 7mUH4DeSEmLJfGME8su3jGH6AQGrr/x146HCduHK8mOksso88U3Ek9aAz9i3PRMoxo5W fyfx1UDDUwCC94fUMX6IqLirurCfvRVgURW/1QhhdndybUpDxn3ujIOWLF5ka6DRCda2 wFsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sXtu+Y6r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u4-20020a170902e80400b001a67a19331dsi6369812plg.202.2023.05.10.23.56.00; Wed, 10 May 2023 23:56:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sXtu+Y6r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236812AbjEKGyb (ORCPT + 99 others); Thu, 11 May 2023 02:54:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237205AbjEKGxp (ORCPT ); Thu, 11 May 2023 02:53:45 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9F9A61A4 for ; Wed, 10 May 2023 23:53:40 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id 5b1f17b1804b1-3f42d937d61so23309435e9.3 for ; Wed, 10 May 2023 23:53:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683788019; x=1686380019; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=hUvTc2zD4JZvJuDgGtGCVbueY/7mtRbz8rOovwWY6Kg=; b=sXtu+Y6rjzUNZzM9e0xnCYkBmnY1OFi1FFYoZ0A+T66gtrBve58dGeKcxck7kVxXxb LRPxVYb0PuL4vhMR9Tz7FymK2Zh80C3kuddGatoOyVJOfVIIC8+4Qrs2voC3XLNQYm8o f9zTROyf1V5a2Dd5Z4DvLUNe0suqTlu4vN07s7l4rIdVq+TxY37HaVNDzjf944KHU01W 81IYtNP2iGVchxxIrHj5krpYea92Cj2FtKO7N47d0E6mrpvC2PisTmjpPWQuQC9YA1dU aIms5qfKVpFCY2l7X8K+29MS4gFMWUmEBwOOllg/d5+0Qgz2CbzRsDvv8ywbViSTS9/v V2tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683788019; x=1686380019; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hUvTc2zD4JZvJuDgGtGCVbueY/7mtRbz8rOovwWY6Kg=; b=GaG+TT2XXlS9UEwVqnF6Q0Wq108r4hKISJioiKGa/xaQz6W2adKbGupH2THPVEAhmv tUh3IDuYaJcrjsgTI/4xtukLwzqFlh6yYSo3Ebynuz1md0VEdAfgE8YZanbTwmTgMwea xAUKCGB8E+Vdv08bIIJPNwu8oV/20ZKNYG8NdCj0tQvlcMx+45a6RQDA0dINL1iy86hs /afK8sBYbaMdJSgRcL0HHTHR+jm4Fd5JkmGQgtfcSgOQHRwwmSfVoFcYPE+vl88QydBH v3yAqBVt3jU2YbiHAMI8nv8iQohREzMqUVvM8dRGaN9DFBc0Wt3x44mT2x+RrXEujk9T 5eeg== X-Gm-Message-State: AC+VfDy/s00KC/dp4CY3bxXdvODmvf3S68yGlQHR6J+rMyL22thUjlws QJFbt9trRyEmK1wPf1Xtv3XEuQ== X-Received: by 2002:a7b:cb58:0:b0:3ee:b3bf:5f7c with SMTP id v24-20020a7bcb58000000b003eeb3bf5f7cmr14539244wmj.23.1683788018816; Wed, 10 May 2023 23:53:38 -0700 (PDT) Received: from linaro.org ([2a00:23c5:680a:d01:3110:8b33:cdf9:dea0]) by smtp.gmail.com with ESMTPSA id f8-20020a7bc8c8000000b003f4e8530696sm1038257wml.46.2023.05.10.23.53.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 23:53:38 -0700 (PDT) From: Mike Leach To: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org, linux-kernel@vger.kernel.org Cc: suzuki.poulose@arm.com, acme@kernel.org, james.clark@arm.com, Mike Leach Subject: [PATCH v6 5/6] coresight: tools: Add config table file write and reader tools Date: Thu, 11 May 2023 07:53:29 +0100 Message-Id: <20230511065330.26392-6-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230511065330.26392-1-mike.leach@linaro.org> References: <20230511065330.26392-1-mike.leach@linaro.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765579867313894634?= X-GMAIL-MSGID: =?utf-8?q?1765579867313894634?= Add an example config table generator to test loading configurations via a binary attribute in configfs. Provides a table buffer writer function that can be re-used in other userspace programs. Table write format matches that expected by the corresponding reader in the configfs driver code. Generates tables and outputs in form of binary files. Add a config table file reader and printer. Takes in config table files and prints the contents. Uses table reader source from kernel driver. Signed-off-by: Mike Leach --- MAINTAINERS | 1 + .../coresight/coresight-config-table.h | 5 + tools/coresight/Makefile | 56 ++++ tools/coresight/coresight-cfg-bufw.c | 309 ++++++++++++++++++ tools/coresight/coresight-cfg-bufw.h | 26 ++ tools/coresight/coresight-cfg-example1.c | 62 ++++ tools/coresight/coresight-cfg-example2.c | 95 ++++++ tools/coresight/coresight-cfg-examples.h | 25 ++ tools/coresight/coresight-cfg-file-gen.c | 61 ++++ tools/coresight/coresight-cfg-file-read.c | 227 +++++++++++++ tools/coresight/coresight-config-uapi.h | 105 ++++++ 11 files changed, 972 insertions(+) create mode 100644 tools/coresight/Makefile create mode 100644 tools/coresight/coresight-cfg-bufw.c create mode 100644 tools/coresight/coresight-cfg-bufw.h create mode 100644 tools/coresight/coresight-cfg-example1.c create mode 100644 tools/coresight/coresight-cfg-example2.c create mode 100644 tools/coresight/coresight-cfg-examples.h create mode 100644 tools/coresight/coresight-cfg-file-gen.c create mode 100644 tools/coresight/coresight-cfg-file-read.c create mode 100644 tools/coresight/coresight-config-uapi.h diff --git a/MAINTAINERS b/MAINTAINERS index 1dc8bd26b6cf..074989716fbf 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2109,6 +2109,7 @@ F: drivers/hwtracing/coresight/* F: include/dt-bindings/arm/coresight-cti-dt.h F: include/linux/coresight* F: samples/coresight/* +F: tools/coresight/* F: tools/perf/tests/shell/coresight/* F: tools/perf/arch/arm/util/auxtrace.c F: tools/perf/arch/arm/util/cs-etm.c diff --git a/drivers/hwtracing/coresight/coresight-config-table.h b/drivers/hwtracing/coresight/coresight-config-table.h index 2cd8fb0630e2..b9935bff7169 100644 --- a/drivers/hwtracing/coresight/coresight-config-table.h +++ b/drivers/hwtracing/coresight/coresight-config-table.h @@ -9,7 +9,12 @@ #include +#ifdef __KERNEL__ #include "coresight-config.h" +#else +#include "coresight-config-uapi.h" +#endif + /* * Configurations and features can be dynamically loaded at runtime diff --git a/tools/coresight/Makefile b/tools/coresight/Makefile new file mode 100644 index 000000000000..175d2b45bb44 --- /dev/null +++ b/tools/coresight/Makefile @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: GPL-2.0-only +include ../scripts/Makefile.include +include ../scripts/Makefile.arch + +# Makefile to build the coresight configuration table file reader and generator tools + +this-makefile := $(lastword $(MAKEFILE_LIST)) +tools-src := $(realpath $(dir $(this-makefile))) +srctree := $(realpath $(dir $(tools-src)/../../.)) + +# ensure we use all as the default - skip anything in included Makefile +.DEFAULT_GOAL = all +# MAKECMDGOALS isn't set if there's no explicit goal in the +# command line, so set the default. +MAKECMDGOALS ?= $(.DEFAULT_GOAL) + +# cs tools includes +CS_TOOLS_INCLUDE = -I$(srctree)/drivers/hwtracing/coresight -I$(srctree)/tools/include/ \ + -I$(srctree)/tools/include/uapi -I$(srctree)/tools/coresight + +# compile flags +CFLAGS += $(CPPFLAGS) -c -Wall -DLINUX -Wno-switch -Wlogical-op -fPIC $(CS_TOOLS_INCLUDE) + +# object files +coresight-cfg-file-gen-objs := coresight-cfg-file-gen.o coresight-cfg-bufw.o \ + coresight-cfg-example1.o coresight-cfg-example2.o +coresight-cfg-file-read-objs := coresight-cfg-file-read.o coresight-config-table.o + +# debug variant +ifdef DEBUG +CFLAGS += -g -O0 -DDEBUG +else +CFLAGS += -O2 -DNDEBUG +endif + +.PHONY: all +all: coresight-cfg-file-gen coresight-cfg-file-read + +coresight-config-table.o: src_copy + $(CC) $(CFLAGS) coresight-config-table.c -o coresight-config-table.o + +.PHONY: src_copy +src_copy: + @cp $(srctree)/drivers/hwtracing/coresight/coresight-config-table.c $(srctree)/tools/coresight/. + +coresight-cfg-file-gen: $(coresight-cfg-file-gen-objs) + $(CC) $(LDFLAGS) $(coresight-cfg-file-gen-objs) -o coresight-cfg-file-gen + +coresight-cfg-file-read: $(coresight-cfg-file-read-objs) + $(CC) $(LDFLAGS) $(coresight-cfg-file-read-objs) -o coresight-cfg-file-read + +clean: + rm -f coresight-cfg-file-gen coresight-cfg-file-read + rm -f *.o + rm -f coresight-config-table.c + rm -f *.cscfg diff --git a/tools/coresight/coresight-cfg-bufw.c b/tools/coresight/coresight-cfg-bufw.c new file mode 100644 index 000000000000..23ec33781330 --- /dev/null +++ b/tools/coresight/coresight-cfg-bufw.c @@ -0,0 +1,309 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#include + +#include "coresight-cfg-bufw.h" +#include "coresight-config-uapi.h" + +/* + * Set of macros to make writing the buffer code easier. + *. + * Uses naming convention as 'buffer' for the buffer pointer and + * 'used' as the current bytes used by the encosing function. + */ +#define cscfg_write_u64(val64) { \ + *(u64 *)(buffer + used) = val64; \ + used += sizeof(u64); \ + } + +#define cscfg_write_u32(val32) { \ + *(u32 *)(buffer + used) = val32; \ + used += sizeof(u32); \ + } + +#define cscfg_write_u16(val16) { \ + *(u16 *)(buffer + used) = val16; \ + used += sizeof(u16); \ + } + +#define cscfg_write_u8(val8) { \ + *(buffer + used) = val8; \ + used++; \ + } + +#define CHECK_WRET(rval) { \ + if (rval < 0) \ + return rval; \ + used += rval; \ + } + +/* write the header at the start of the buffer */ +static int cscfg_table_write_fhdr(u8 *buffer, const int buflen, + const struct cscfg_table_header *fhdr) +{ + int used = 0; + + cscfg_write_u32(fhdr->magic_version); + cscfg_write_u16(fhdr->length); + cscfg_write_u16(fhdr->nr_configs); + cscfg_write_u16(fhdr->nr_features); + return used; +} + +static int cscfg_table_write_string(u8 *buffer, const int buflen, const char *string) +{ + int len, used = 0; + + len = strlen(string); + if (len > CSCFG_TABLE_STR_MAXSIZE) + return -EINVAL; + + if (buflen < (len + 1 + sizeof(u16))) + return -EINVAL; + + cscfg_write_u16((u16)(len + 1)); + strcpy((char *)(buffer + used), string); + used += (len + 1); + + return used; +} + +static int cscfg_table_write_elem_hdr(u8 *buffer, const int buflen, + struct cscfg_table_elem_header *ehdr) +{ + int used = 0; + + if (buflen < (sizeof(u16) + sizeof(u8))) + return -EINVAL; + + cscfg_write_u16(ehdr->elem_length); + cscfg_write_u8(ehdr->elem_type); + + return used; +} + +static int cscfg_table_write_config(u8 *buffer, const int buflen, + struct cscfg_config_desc *config_desc) +{ + int used = 0, bytes_w, space_req, preset_bytes, i; + struct cscfg_table_elem_header ehdr; + + ehdr.elem_length = 0; + ehdr.elem_type = CSCFG_TABLE_ELEM_TYPE_CFG; + + /* write element header at current buffer location */ + bytes_w = cscfg_table_write_elem_hdr(buffer, buflen, &ehdr); + CHECK_WRET(bytes_w); + + /* write out the configuration name */ + bytes_w = cscfg_table_write_string(buffer + used, buflen - used, + config_desc->name); + CHECK_WRET(bytes_w); + + /* write out the description string */ + bytes_w = cscfg_table_write_string(buffer + used, buflen - used, + config_desc->description); + CHECK_WRET(bytes_w); + + /* + * calculate the space needed for variables + presets + * [u16 value - nr_presets] + * [u32 value - nr_total_params] + * [u16 value - nr_feat_refs] + * [u64 values] * (nr_presets * nr_total_params) + */ + preset_bytes = sizeof(u64) * config_desc->nr_presets * config_desc->nr_total_params; + space_req = (sizeof(u16) * 2) + sizeof(u32) + preset_bytes; + + if ((buflen - used) < space_req) + return -EINVAL; + + cscfg_write_u16((u16)config_desc->nr_presets); + cscfg_write_u32((u32)config_desc->nr_total_params); + cscfg_write_u16((u16)config_desc->nr_feat_refs); + if (preset_bytes) { + memcpy(buffer + used, (u8 *)config_desc->presets, preset_bytes); + used += preset_bytes; + } + + /* now write the feature ref names */ + for (i = 0; i < config_desc->nr_feat_refs; i++) { + bytes_w = cscfg_table_write_string(buffer + used, buflen - used, + config_desc->feat_ref_names[i]); + CHECK_WRET(bytes_w); + } + + /* rewrite the element header with the correct length */ + ehdr.elem_length = used; + bytes_w = cscfg_table_write_elem_hdr(buffer, buflen, &ehdr); + /* no CHECK_WRET as used must not be updated */ + if (bytes_w < 0) + return bytes_w; + + return used; +} + +/* + * write a parameter structure into the buffer in following format: + * [cscfg_table_elem_str] - parameter name. + * [u64 value: param_value] - initial value. + */ +static int cscfg_table_write_param(u8 *buffer, const int buflen, + struct cscfg_parameter_desc *param_desc) +{ + int used = 0, bytes_w; + + bytes_w = cscfg_table_write_string(buffer + used, buflen - used, + param_desc->name); + CHECK_WRET(bytes_w); + + if ((buflen - used) < sizeof(u64)) + return -EINVAL; + + cscfg_write_u64(param_desc->value); + return used; +} + +/* + * Write a feature element from cscfg_feature_desc in following format: + * + * [cscfg_table_elem_header] - header length is total bytes to end of param structures. + * [cscfg_table_elem_str] - feature name. + * [cscfg_table_elem_str] - feature description. + * [u32 value: match_flags] + * [u16 value: nr_regs] - number of registers. + * [u16 value: nr_params] - number of parameters. + * [cscfg_regval_desc struct] * nr_regs + * [PARAM_ELEM] * nr_params + */ +static int cscfg_table_write_feat(u8 *buffer, const int buflen, + struct cscfg_feature_desc *feat_desc) +{ + struct cscfg_table_elem_header ehdr; + struct cscfg_regval_desc *p_reg_desc; + int used = 0, bytes_w, i, space_req; + u32 val32; + + ehdr.elem_length = 0; + ehdr.elem_type = CSCFG_TABLE_ELEM_TYPE_FEAT; + + /* write element header at current buffer location */ + bytes_w = cscfg_table_write_elem_hdr(buffer, buflen, &ehdr); + CHECK_WRET(bytes_w); + + /* write out the name string */ + bytes_w = cscfg_table_write_string(buffer + used, buflen - used, + feat_desc->name); + CHECK_WRET(bytes_w) + + /* write out the description string */ + bytes_w = cscfg_table_write_string(buffer + used, buflen - used, + feat_desc->description); + CHECK_WRET(bytes_w); + + /* check for space for variables and register structures */ + space_req = (sizeof(u16) * 2) + sizeof(u32) + + (sizeof(struct cscfg_regval_desc) * feat_desc->nr_regs); + if ((buflen - used) < space_req) + return -EINVAL; + + /* write the variables */ + cscfg_write_u32((u32)feat_desc->match_flags); + cscfg_write_u16((u16)feat_desc->nr_regs); + cscfg_write_u16((u16)feat_desc->nr_params); + + /*write the registers */ + for (i = 0; i < feat_desc->nr_regs; i++) { + p_reg_desc = (struct cscfg_regval_desc *)&feat_desc->regs_desc[i]; + CSCFG_TABLE_REG_DESC_INFO_TO_U32(val32, p_reg_desc); + cscfg_write_u32(val32); + cscfg_write_u64(feat_desc->regs_desc[i].val64); + } + + /* write any parameters */ + for (i = 0; i < feat_desc->nr_params; i++) { + bytes_w = cscfg_table_write_param(buffer + used, buflen - used, + &feat_desc->params_desc[i]); + CHECK_WRET(bytes_w); + } + + /* + * rewrite the element header at the start of the buffer block + * with the correct length + */ + ehdr.elem_length = used; + bytes_w = cscfg_table_write_elem_hdr(buffer, buflen, &ehdr); + /* no CHECK_WRET as used must not be updated */ + if (bytes_w < 0) + return bytes_w; + + return used; +} + +/* + * write a buffer from the configuration and feature + * descriptors to write into a file for configfs. + * + * Will only write one config, and/or a number of features, + * per the file standard. + */ +int cscfg_table_write_buffer(u8 *buffer, const int buflen, + struct cscfg_config_desc **config_descs, + struct cscfg_feature_desc **feat_descs) +{ + struct cscfg_table_header fhdr; + int used = 0, bytes_w, i; + + /* init the file header */ + fhdr.magic_version = CSCFG_TABLE_MAGIC_VERSION; + fhdr.length = 0; + fhdr.nr_configs = 0; + fhdr.nr_features = 0; + + /* count the configs */ + if (config_descs) { + while (config_descs[fhdr.nr_configs]) + fhdr.nr_configs++; + } + + /* count the features */ + if (feat_descs) { + while (feat_descs[fhdr.nr_features]) + fhdr.nr_features++; + } + + /* need a buffer and at least one config or feature */ + if ((!fhdr.nr_configs && !fhdr.nr_features) || + !buffer || (buflen > CSCFG_TABLE_MAXSIZE)) + return -EINVAL; + + /* write a header at the start to get the length of the header */ + bytes_w = cscfg_table_write_fhdr(buffer, buflen, &fhdr); + CHECK_WRET(bytes_w); + + /* write configs */ + for (i = 0; i < fhdr.nr_configs; i++) { + bytes_w = cscfg_table_write_config(buffer + used, buflen - used, + config_descs[i]); + CHECK_WRET(bytes_w); + } + + /* write any features */ + for (i = 0; i < fhdr.nr_features; i++) { + bytes_w = cscfg_table_write_feat(buffer + used, buflen - used, + feat_descs[i]); + CHECK_WRET(bytes_w); + } + + /* finally re-write the header at the buffer start with the correct length */ + fhdr.length = (u16)used; + bytes_w = cscfg_table_write_fhdr(buffer, buflen, &fhdr); + /* no CHECK_WRET as used must not be updated */ + if (bytes_w < 0) + return bytes_w; + return used; +} diff --git a/tools/coresight/coresight-cfg-bufw.h b/tools/coresight/coresight-cfg-bufw.h new file mode 100644 index 000000000000..2d6ce64220a4 --- /dev/null +++ b/tools/coresight/coresight-cfg-bufw.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#ifndef _CORESIGHT_CFG_BUFW_H +#define _CORESIGHT_CFG_BUFW_H + +#include + +#include "coresight-config-table.h" + +/* + * Function to take coresight configurations and features and + * write them into a supplied memory buffer for serialisation + * into a file. + * + * Resulting file can then be loaded into the coresight + * infrastructure via configfs. + */ +int cscfg_table_write_buffer(u8 *buffer, const int buflen, + struct cscfg_config_desc **config_descs, + struct cscfg_feature_desc **feat_descs); + +#endif /* _CORESIGHT_CFG_BUFW_H */ diff --git a/tools/coresight/coresight-cfg-example1.c b/tools/coresight/coresight-cfg-example1.c new file mode 100644 index 000000000000..c562116ffc94 --- /dev/null +++ b/tools/coresight/coresight-cfg-example1.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ +#include +#include +#include +#include + +#include "coresight-cfg-examples.h" + +/* + * create a configuration only example using the strobing feature + */ + +/* we will provide 4 sets of preset parameter values */ +#define AFDO3_NR_PRESETS 4 +/* the total number of parameters in used features - strobing has 2 */ +#define AFDO3_NR_PARAM_SUM 2 + +static const char *afdo3_ref_names[] = { + "strobing", +}; + +/* + * set of presets leaves strobing window constant while varying period to allow + * experimentation with mark / space ratios for various workloads + */ +static u64 afdo3_presets[AFDO3_NR_PRESETS][AFDO3_NR_PARAM_SUM] = { + { 2000, 100 }, + { 2000, 1000 }, + { 2000, 5000 }, + { 2000, 10000 }, +}; + +struct cscfg_config_desc afdo3 = { + .name = "autofdo3", + .description = "Setup ETMs with strobing for autofdo\n" + "Supplied presets allow experimentation with mark-space ratio for various loads\n", + .nr_feat_refs = ARRAY_SIZE(afdo3_ref_names), + .feat_ref_names = afdo3_ref_names, + .nr_presets = AFDO3_NR_PRESETS, + .nr_total_params = AFDO3_NR_PARAM_SUM, + .presets = &afdo3_presets[0][0], +}; + +static struct cscfg_feature_desc *sample_feats[] = { + NULL +}; + +static struct cscfg_config_desc *sample_cfgs[] = { + &afdo3, + NULL +}; + +struct cscfg_file_eg_info file_info_eg1 = { + .example_name = "example1", + .filename = "example1.cscfg", + .config_descs = sample_cfgs, + .feat_descs = sample_feats, +}; diff --git a/tools/coresight/coresight-cfg-example2.c b/tools/coresight/coresight-cfg-example2.c new file mode 100644 index 000000000000..6312a185bd46 --- /dev/null +++ b/tools/coresight/coresight-cfg-example2.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ +#include +#include +#include +#include + +#include "coresight-cfg-examples.h" + +/* + * create a dual configuration only example using the strobing feature + */ + +/* we will provide 10 sets of preset parameter values */ +#define AFDO_NR_PRESETS 10 +/* the total number of parameters in used features - strobing has 2 */ +#define AFDO_NR_PARAM_SUM 2 + +static const char *afdo_ref_names[] = { + "strobing", +}; + +/* + * sets of presets leaves strobing window constant while varying period to allow + * experimentation with mark / space ratios for various workloads + */ +static u64 afdo_set_a_presets[AFDO_NR_PRESETS][AFDO_NR_PARAM_SUM] = { + { 2000, 100 }, + { 2000, 1000 }, + { 2000, 5000 }, + { 2000, 10000 }, + { 4000, 100 }, + { 4000, 1000 }, + { 4000, 5000 }, + { 4000, 10000 }, + { 6000, 100 }, + { 6000, 1000 }, +}; + + +static u64 afdo_set_b_presets[AFDO_NR_PRESETS][AFDO_NR_PARAM_SUM] = { + { 6000, 5000 }, + { 6000, 10000 }, + { 8000, 100 }, + { 8000, 1000 }, + { 8000, 5000 }, + { 8000, 10000 }, + { 12000, 100 }, + { 12000, 1000 }, + { 12000, 5000 }, + { 12000, 10000 }, +}; +/* two configurations with differing preset tables */ +struct cscfg_config_desc afdo_seta = { + .name = "autofdo_set_a", + .description = "Setup ETMs with strobing for autofdo\n" + "Supplied presets allow experimentation with mark-space ratio for various loads\n", + .nr_feat_refs = ARRAY_SIZE(afdo_ref_names), + .feat_ref_names = afdo_ref_names, + .nr_presets = AFDO_NR_PRESETS, + .nr_total_params = AFDO_NR_PARAM_SUM, + .presets = &afdo_set_a_presets[0][0], +}; + +struct cscfg_config_desc afdo_setb = { + .name = "autofdo_set_b", + .description = "Setup ETMs with strobing for autofdo\n" + "Supplied presets allow experimentation with mark-space ratio for various loads\n", + .nr_feat_refs = ARRAY_SIZE(afdo_ref_names), + .feat_ref_names = afdo_ref_names, + .nr_presets = AFDO_NR_PRESETS, + .nr_total_params = AFDO_NR_PARAM_SUM, + .presets = &afdo_set_b_presets[0][0], +}; + + +static struct cscfg_feature_desc *sample_feats[] = { + NULL +}; + +static struct cscfg_config_desc *sample_cfgs[] = { + &afdo_seta, + &afdo_setb, + NULL +}; + +struct cscfg_file_eg_info file_info_eg2 = { + .example_name = "example2", + .filename = "example2.cscfg", + .config_descs = sample_cfgs, + .feat_descs = sample_feats, +}; diff --git a/tools/coresight/coresight-cfg-examples.h b/tools/coresight/coresight-cfg-examples.h new file mode 100644 index 000000000000..c9719e7b2233 --- /dev/null +++ b/tools/coresight/coresight-cfg-examples.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#ifndef _CORESIGHT_CFG_EXAMPLES_H +#define _CORESIGHT_CFG_EXAMPLES_H + +#include + +#include "coresight-config-uapi.h" + +/* + * structure to pass C configuration structure information to + * configuration table file generator program + */ +struct cscfg_file_eg_info { + const char *example_name; + const char *filename; + struct cscfg_config_desc **config_descs; + struct cscfg_feature_desc **feat_descs; +}; + +#endif /* _CORESIGHT_CFG_EXAMPLES_H */ diff --git a/tools/coresight/coresight-cfg-file-gen.c b/tools/coresight/coresight-cfg-file-gen.c new file mode 100644 index 000000000000..3ee3cdc02c4f --- /dev/null +++ b/tools/coresight/coresight-cfg-file-gen.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#include +#include +#include +#include + +#include "coresight-cfg-examples.h" +#include "coresight-cfg-bufw.h" + +/* references to the configuration and feature example structures */ +extern struct cscfg_file_eg_info file_info_eg1; +extern struct cscfg_file_eg_info file_info_eg2; + +/* array of example table files to generate */ +struct cscfg_file_eg_info *info_ptrs[] = { + &file_info_eg1, + &file_info_eg2, + NULL, +}; + +int main(int argc, char **argv) +{ + struct cscfg_config_desc **config_descs; + struct cscfg_feature_desc **feat_descs; + u8 buffer[CSCFG_TABLE_MAXSIZE]; + int used, idx = 0; + FILE *fp; + const char *filename; + + printf("Coresight Configuration table file Generator\n\n"); + + while (info_ptrs[idx]) { + printf("Generating %s example\n", info_ptrs[idx]->example_name); + config_descs = info_ptrs[idx]->config_descs; + feat_descs = info_ptrs[idx]->feat_descs; + filename = info_ptrs[idx]->filename; + used = cscfg_table_write_buffer(buffer, CSCFG_TABLE_MAXSIZE, + config_descs, feat_descs); + + if (used < 0) { + printf("Error %d writing configuration %s into buffer\n", + used, info_ptrs[idx]->example_name); + return used; + } + + fp = fopen(filename, "wb"); + if (fp == NULL) { + printf("Error opening file %s\n", filename); + return -1; + } + fwrite(buffer, used, sizeof(u8), fp); + fclose(fp); + idx++; + } + return 0; +} diff --git a/tools/coresight/coresight-cfg-file-read.c b/tools/coresight/coresight-cfg-file-read.c new file mode 100644 index 000000000000..89dcca5e776f --- /dev/null +++ b/tools/coresight/coresight-cfg-file-read.c @@ -0,0 +1,227 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2020-2022 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "coresight-config-table.h" +#include "coresight-config-uapi.h" + +/* + * tool to read and print a generated configuration + * re-uses the read code source from the driver. + */ + +static void validate_config_name(const char *name) +{ + int i, len = strlen(name); + + for (i = 0; i < len; i++) { + if (!isalnum(name[i]) && !(name[i] == '_')) { + printf("\n************************************************\n"); + printf("ERROR: Configuration name %s invalid character(s)\n", name); + printf(" : must contain only alphanumeric and _ only\n"); + printf("************************************************\n"); + } + } +} + +static void print_configs(struct cscfg_table_load_descs *load_descs) +{ + struct cscfg_config_desc *config_desc; + int i, j, p, cfg_idx = 0; + + config_desc = load_descs->config_descs[cfg_idx]; + if (!config_desc) { + printf("File contains no configurations.\n\n"); + return; + } + + while (config_desc) { + printf("Configuration %d\nName:- %s\n", cfg_idx + 1, config_desc->name); + validate_config_name(config_desc->name); + printf("Description:-\n%s\n", config_desc->description); + printf("Uses %d features:-\n", config_desc->nr_feat_refs); + for (i = 0; i < config_desc->nr_feat_refs; i++) + printf("Feature-%d: %s\n", i + 1, config_desc->feat_ref_names[i]); + + printf("\nProvides %d sets of preset values, %d presets per set\n", + config_desc->nr_presets, config_desc->nr_total_params); + if (config_desc->nr_presets) { + for (i = 0; i < config_desc->nr_presets; i++) { + printf("set[%d]: ", i); + for (j = 0; j < config_desc->nr_total_params; j++) { + p = (i * config_desc->nr_total_params) + j; + printf("0x%lx, ", config_desc->presets[p]); + } + printf("\n"); + } + } + printf("\n============================================\n"); + cfg_idx++; + config_desc = load_descs->config_descs[cfg_idx]; + } +} + +static void print_reg_type_info(u8 type) +{ + if (type & CS_CFG_REG_TYPE_STD) + printf("std_reg "); + if (type & CS_CFG_REG_TYPE_RESOURCE) + printf("resource "); + if (type & CS_CFG_REG_TYPE_VAL_PARAM) + printf("param_index "); + if (type & CS_CFG_REG_TYPE_VAL_64BIT) + printf("64_bit "); + else + printf("32_bit "); + if (type & CS_CFG_REG_TYPE_VAL_MASK) + printf("masked "); + if (type & CS_CFG_REG_TYPE_VAL_SAVE) + printf("save_on_disable "); + +} + +static void print_regs(int nr, struct cscfg_regval_desc *regs_desc_array) +{ + int i; + struct cscfg_regval_desc *reg_desc; + u8 type; + u16 offset; + u16 info; + + for (i = 0; i < nr; i++) { + reg_desc = ®s_desc_array[i]; + type = (u8)reg_desc->type; + offset = (u16)reg_desc->offset; + info = (u16)reg_desc->hw_info; + + printf("Reg(%d): Type 0x%x: ", i, type); + print_reg_type_info(type); + printf("\nOffset: 0x%03x; HW Info: 0x%03x\n", offset, info); + printf("Value: "); + if (type & CS_CFG_REG_TYPE_VAL_64BIT) + printf("0x%lx\n", reg_desc->val64); + else if (type & CS_CFG_REG_TYPE_VAL_PARAM) { + printf("param(%d) ", reg_desc->param_idx); + if (type & (CS_CFG_REG_TYPE_VAL_MASK)) + printf(" mask: 0x%x", reg_desc->mask32); + printf("\n"); + } else { + printf("0x%x ", reg_desc->val32); + if (type & (CS_CFG_REG_TYPE_VAL_MASK)) + printf(" mask: 0x%x", reg_desc->mask32); + printf("\n"); + } + } +} + +static void print_params(int nr, struct cscfg_parameter_desc *params_desc) +{ + int i; + + for (i = 0; i < nr; i++) + printf("Param(%d) : %s; Init value 0x%lx\n", i, + params_desc[i].name, params_desc[i].value); +} + +static void print_features(struct cscfg_table_load_descs *load_descs) +{ + struct cscfg_feature_desc *feat_desc = 0; + int idx = 0; + + feat_desc = load_descs->feat_descs[idx]; + if (!feat_desc) { + printf("File contains no features\n\n"); + return; + } + + while (feat_desc) { + printf("Feature %d\nName:- %s\n\n", idx + 1, feat_desc->name); + printf("Description:- %s\n", feat_desc->description); + printf("Match flags: 0x%x\n", feat_desc->match_flags); + printf("\nNumber of Paraneters: %d\n", feat_desc->nr_params); + if (feat_desc->nr_params) + print_params(feat_desc->nr_params, feat_desc->params_desc); + printf("\nNumber of Registers: %d\n", feat_desc->nr_regs); + if (feat_desc->nr_regs) + print_regs(feat_desc->nr_regs, feat_desc->regs_desc); + printf("\n============================================\n"); + + /* next feature */ + idx++; + feat_desc = load_descs->feat_descs[idx]; + } +} + +int main(int argc, char **argv) +{ + FILE *fp; + struct cscfg_table_load_descs *load_descs; + int err, fsize; + u8 buffer[CSCFG_TABLE_MAXSIZE]; + + printf("CoreSight Configuration table file reader"); + printf("\n============================================\n\n"); + + + /* need a filename */ + if (argc <= 1) { + printf("Please provide filename on command line\n"); + return -EINVAL; + } + + /* open file and read into the buffer. */ + fp = fopen(argv[1], "rb"); + if (fp == NULL) { + printf("Error opening file %s\n", argv[1]); + return -EINVAL; + } + + fseek(fp, 0, SEEK_END); + fsize = ftell(fp); + rewind(fp); + if (fsize > CSCFG_TABLE_MAXSIZE) { + printf("Error: Input file too large."); + fclose(fp); + return -EINVAL; + } + err = fread(buffer, sizeof(u8), fsize, fp); + fclose(fp); + + if (err < fsize) { + printf("Error reading file %s\n", argv[1]); + return -EINVAL; + } + + /* allocate the descriptor structures to be populated by read operation */ + load_descs = malloc(sizeof(struct cscfg_table_load_descs)); + if (!load_descs) { + printf("Error allocating load descs structure.\n"); + return -ENOMEM; + } + + /* read the buffer and create the configuration and feature structures */ + err = cscfg_table_read_buffer(buffer, fsize, load_descs); + if (err) { + printf("Error reading configuration file\n"); + goto exit_free_mem; + } + + /* print the contents of the structures */ + print_configs(load_descs); + print_features(load_descs); + +exit_free_mem: + cscfg_table_free_load_descs(load_descs); + free(load_descs); + return err; +} diff --git a/tools/coresight/coresight-config-uapi.h b/tools/coresight/coresight-config-uapi.h new file mode 100644 index 000000000000..f3698a5b10ef --- /dev/null +++ b/tools/coresight/coresight-config-uapi.h @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2020 Linaro Limited, All rights reserved. + * Author: Mike Leach + */ + +#ifndef _CORESIGHT_CORESIGHT_CONFIG_UAPI_H +#define _CORESIGHT_CORESIGHT_CONFIG_UAPI_H + +#include +#include + +#include +#include +#include + +#include "coresight-config-desc.h" + +/* + * Userspace versions of the configuration and feature descriptors. + * Used in the tools/coresight programs. + * + * Compatible with structures in coresight-config.h for use in + * coresight-config-file.c common reader source file. + */ + +/** + * Device feature descriptor - combination of registers and parameters to + * program a device to implement a specific complex function. + * + * UAPI version - removed kernel constructs. + * + * @name: feature name. + * @description: brief description of the feature. + * @match_flags: matching information if loading into a device + * @nr_params: number of parameters used. + * @params_desc: array of parameters used. + * @nr_regs: number of registers used. + * @regs_desc: array of registers used. + */ +struct cscfg_feature_desc { + const char *name; + const char *description; + u32 match_flags; + int nr_params; + struct cscfg_parameter_desc *params_desc; + int nr_regs; + struct cscfg_regval_desc *regs_desc; +}; + +/** + * Configuration descriptor - describes selectable system configuration. + * + * A configuration describes device features in use, and may provide preset + * values for the parameters in those features. + * + * A single set of presets is the sum of the parameters declared by + * all the features in use - this value is @nr_total_params. + * + * UAPI version - removed kernel constructs. + * + * @name: name of the configuration - used for selection. + * @description: description of the purpose of the configuration. + * @nr_feat_refs: Number of features used in this configuration. + * @feat_ref_names: references to features used in this configuration. + * @nr_presets: Number of sets of presets supplied by this configuration. + * @nr_total_params: Sum of all parameters declared by used features + * @presets: Array of preset values. + */ +struct cscfg_config_desc { + const char *name; + const char *description; + int nr_feat_refs; + const char **feat_ref_names; + int nr_presets; + int nr_total_params; + const u64 *presets; /* nr_presets * nr_total_params */ +}; + +/* UAPI allocators for descriptors in common config file buffer read code */ +static inline void *cscfg_calloc(size_t num, size_t size) +{ + return calloc(num, size); +} + +static inline char *cscfg_strdup(const char *str) +{ + return strdup(str); +} + +static inline void *cscfg_zalloc(size_t size) +{ + void *ptr = malloc(size); + + if (ptr) + memset(ptr, 0, size); + return ptr; +} + +static inline void cscfg_free(void *mem) +{ + free(mem); +} + +#endif /* _CORESIGHT_CORESIGHT_CONFIG_UAPI_H */ From patchwork Thu May 11 06:53:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Leach X-Patchwork-Id: 92373 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4161796vqo; Wed, 10 May 2023 23:55:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6UPaepbLLh9hwFmKbbP+78uMi3YH5vFXQT4Ug0YoWumi0q4V560ou6o8xLsinewbg60gs+ X-Received: by 2002:a05:6a20:258a:b0:102:7c37:aeef with SMTP id k10-20020a056a20258a00b001027c37aeefmr6630215pzd.26.1683788146114; Wed, 10 May 2023 23:55:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683788146; cv=none; d=google.com; s=arc-20160816; b=v8iel5p1Oi60049qotIXwbzR94UD9oSRD53tYRkwfg6c5XTZNqMbNrmVmboRmOsfF/ 5zKFEyPmZAa6+U2Upvpy/DGcDhEZuevNl+V4ScTPc0omLSGICMWNqBEzeqUqjTNfq65M dCyFBPB/fDuoUeTM5pDSZ/SxckUJYFQHBxt4mznfGTUt4XxQuvE58QXlg0f3m5L+97/d +znM7waqrs+bS3JM++jB8pQPbH4zB9dv6tJFVD/bgB6NcuQJC7A5HLWCdLkPMMLjSfb0 Hx+p5TXw/WYqbt8N60VemqjkjgHeehmM7Vemd8TFEdmWIWmOsMi1offVwhVGoWdzwEAl eAtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=UpvUNADgatGSUNbf1DFHHdQ9AqekXwHBfv8Q4aLvujc=; b=MsuRnpJ4bwiDDvF1YEa9gKMZAswBb6d+lBLo/qVYyvHx1JXsiwafNPIEKFCQdyi7CP /YSalL5edkEc4ox5tIPbpnk75V2yNfXulNDyPHnul2ktg9ob0zaLl5aWYSM+O1BE3Hba RB3lXJIfarkHwp7QcOvSlLfuV6Lpbqg4IEtL92nH4IJzR1oOVqO5O9BSJeJQbdCXjHDJ UgpzbJmi3UUmQcIXcIi6XrwWgp6BxZaDGshudEGCi4Gv+nypthORGAzJi6K+sw+QB7dv XM3TtwTiTaKeScI0DZLO301Gao5Dcn35x1H6hcpaCehREyV6svccZoVz1ENlgsXbWflN POQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mBVlcxXH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j5-20020a637a45000000b005303a26dbf8si5766509pgn.408.2023.05.10.23.55.33; Wed, 10 May 2023 23:55:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mBVlcxXH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230205AbjEKGyD (ORCPT + 99 others); Thu, 11 May 2023 02:54:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237194AbjEKGxo (ORCPT ); Thu, 11 May 2023 02:53:44 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C60E65A0 for ; Wed, 10 May 2023 23:53:41 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id 5b1f17b1804b1-3f315712406so271844295e9.0 for ; Wed, 10 May 2023 23:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683788020; x=1686380020; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=UpvUNADgatGSUNbf1DFHHdQ9AqekXwHBfv8Q4aLvujc=; b=mBVlcxXH+iTZY0pBa0EAwoRxuLOBdwD03ZNhzjPDkkQKBC45w2SoM9+i/MvE5DS9kv eC6cpAvAsaO0nGVnY2MihTdzWfL18inltI32ZSNzoiTkxnNT//b3W17BG2TYV7AHmDHv JGa2gEC3ApeuGJL14csQYlXsPG0sCUweboabeMzoGXd2is7fAPDyK6WiTiJUHtRfOskj jG8laSAEtNvsDjXNpa5ZxrHVp7Pl14Fq5CbnloW3InnuKmgga6yhAavF4wXvibQEwAqQ CxLiWF+NKDey73x9cg148sA8ZtS8V7c/xSW3Y/o4hV+XphdBhQytPOd/c0G+e6MWs31V x7bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683788020; x=1686380020; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UpvUNADgatGSUNbf1DFHHdQ9AqekXwHBfv8Q4aLvujc=; b=f7/gMV9BSPmtUIFIBYsRuolRr1XgzqfIm/df0GOkdGIchlZr/138VrcviFFM8rl7z4 M50X9DwgPjnQ7uu0RW4Xph2vr0eSP4wRnUvPwza2cevQduvJK/SpieTLSliIwFdzG9gd EoKZABcJsDdsS6tUJjrrard68C/hgJ6ZTUAykHWt/fFCdA5/GPz49gYk+LyYPFG/41EN Oi2/473gLufF7nUYZNdEhQ7Nn0+1QavhJRbf8z5DuAP/0yK87FvQrmk4+bSGvVOufm4W M+fnVLP/hI6UQRy22r2THi6wc/mrZV4CwlWgXgQBU9UldOE+pW4/vc1ktukDidf4cYbo Pixg== X-Gm-Message-State: AC+VfDw/GpYv+BaAGguRvSs1IsT0thqVD8azyQls6gLYhv6c0AeMC7yW ul4QUs7nIGS4+EUZEJ19NfpLqQ== X-Received: by 2002:a05:600c:4e44:b0:3f4:9a86:f2ab with SMTP id e4-20020a05600c4e4400b003f49a86f2abmr2939240wmq.17.1683788019954; Wed, 10 May 2023 23:53:39 -0700 (PDT) Received: from linaro.org ([2a00:23c5:680a:d01:3110:8b33:cdf9:dea0]) by smtp.gmail.com with ESMTPSA id f8-20020a7bc8c8000000b003f4e8530696sm1038257wml.46.2023.05.10.23.53.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 23:53:39 -0700 (PDT) From: Mike Leach To: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org, linux-kernel@vger.kernel.org Cc: suzuki.poulose@arm.com, acme@kernel.org, james.clark@arm.com, Mike Leach , Jonathan Corbet , linux-doc@vger.kernel.org Subject: [PATCH v6 6/6] Documentation: coresight: docs for config load via configfs Date: Thu, 11 May 2023 07:53:30 +0100 Message-Id: <20230511065330.26392-7-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230511065330.26392-1-mike.leach@linaro.org> References: <20230511065330.26392-1-mike.leach@linaro.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765579838685520092?= X-GMAIL-MSGID: =?utf-8?q?1765579838685520092?= Add documentation covering the configfs updates that allow configuration tables to be loaded and unloaded via configfs, along with the demonstration programs in tools/coresight. Cc: Jonathan Corbet Cc: linux-doc@vger.kernel.org Signed-off-by: Mike Leach --- .../trace/coresight/coresight-config.rst | 265 ++++++++++++++++-- 1 file changed, 242 insertions(+), 23 deletions(-) diff --git a/Documentation/trace/coresight/coresight-config.rst b/Documentation/trace/coresight/coresight-config.rst index 6d5ffa6f7347..fe50bfaada5e 100644 --- a/Documentation/trace/coresight/coresight-config.rst +++ b/Documentation/trace/coresight/coresight-config.rst @@ -109,20 +109,20 @@ Operation The following steps take place in the operation of a configuration. -1) In this example, the configuration is 'autofdo', which has an - associated feature 'strobing' that works on ETMv4 CoreSight Devices. +1) In this example, the configuration is ``autofdo``, which has an + associated feature ``strobing`` that works on ETMv4 CoreSight Devices. -2) The configuration is enabled. For example 'perf' may select the +2) The configuration is enabled. For example ``perf`` may select the configuration as part of its command line:: perf record -e cs_etm/autofdo/ myapp - which will enable the 'autofdo' configuration. + which will enable the ``autofdo`` configuration. 3) perf starts tracing on the system. As each ETMv4 that perf uses for trace is enabled, the configuration manager will check if the ETMv4 has a feature that relates to the currently active configuration. - In this case 'strobing' is enabled & programmed into the ETMv4. + In this case ``strobing`` is enabled & programmed into the ETMv4. 4) When the ETMv4 is disabled, any registers marked as needing to be saved will be read back. @@ -136,18 +136,18 @@ Viewing Configurations and Features The set of configurations and features that are currently loaded into the system can be viewed using the configfs API. -Mount configfs as normal and the 'cs-syscfg' subsystem will appear:: +Mount configfs as normal and the ``cs-syscfg`` subsystem will appear:: $ ls /config cs-syscfg stp-policy -This has two sub-directories:: +This has two sub-directories, with the load and unload attribute files:: $ cd cs-syscfg/ $ ls - configurations features + configurations features load unload -The system has the configuration 'autofdo' built in. It may be examined as +The system has the configuration ``autofdo`` built in. It may be examined as follows:: $ cd configurations/ @@ -162,7 +162,7 @@ follows:: $ cat feature_refs strobing -Each preset declared has a 'preset' subdirectory declared. The values for +Each preset declared has a ``preset`` subdirectory declared. The values for the preset can be examined:: $ cat preset1/values @@ -170,7 +170,7 @@ the preset can be examined:: $ cat preset2/values strobing.window = 0x1388 strobing.period = 0x4 -The 'enable' and 'preset' files allow the control of a configuration when +The ``enable`` and ``preset`` files allow the control of a configuration when using CoreSight with sysfs. The features referenced by the configuration can be examined in the features @@ -210,18 +210,18 @@ Using Configurations in perf ============================ The configurations loaded into the CoreSight configuration management are -also declared in the perf 'cs_etm' event infrastructure so that they can +also declared in the perf ``cs_etm`` event infrastructure so that they can be selected when running trace under perf:: $ ls /sys/devices/cs_etm cpu0 cpu2 events nr_addr_filters power subsystem uevent cpu1 cpu3 format perf_event_mux_interval_ms sinks type -The key directory here is 'events' - a generic perf directory which allows +The key directory here is ``events`` - a generic perf directory which allows selection on the perf command line. As with the sinks entries, this provides a hash of the configuration name. -The entry in the 'events' directory uses perfs built in syntax generator +The entry in the ``events`` directory uses perfs built in syntax generator to substitute the syntax for the name when evaluating the command:: $ ls events/ @@ -229,7 +229,7 @@ to substitute the syntax for the name when evaluating the command:: $ cat events/autofdo configid=0xa7c3dddd -The 'autofdo' configuration may be selected on the perf command line:: +The ``autofdo`` configuration may be selected on the perf command line:: $ perf record -e cs_etm/autofdo/u --per-thread @@ -246,7 +246,7 @@ Using Configurations in sysfs Coresight can be controlled using sysfs. When this is in use then a configuration can be made active for the devices that are used in the sysfs session. -In a configuration there are 'enable' and 'preset' files. +In a configuration there are ``enable`` and ``preset`` files. To enable a configuration for use with sysfs:: @@ -256,13 +256,13 @@ To enable a configuration for use with sysfs:: This will then use any default parameter values in the features - which can be adjusted as described above. -To use a preset set of parameter values:: +To use a ``preset`` set of parameter values:: $ echo 3 > preset This will select preset3 for the configuration. The valid values for preset are 0 - to deselect presets, and any value of - where a preset sub-directory is present. + where a ``preset`` sub-directory is present. Note that the active sysfs configuration is a global parameter, therefore only a single configuration can be active for sysfs at any one time. @@ -278,9 +278,16 @@ Creating and Loading Custom Configurations ========================================== Custom configurations and / or features can be dynamically loaded into the -system by using a loadable module. +system by using a loadable module, or by loading a configuration table +through in configfs. -An example of a custom configuration is found in ./samples/coresight. +Loaded configurations can use previously loaded features. The system will +ensure that it is not possible to unload a feature that is currently in +use, by enforcing the unload order as the strict reverse of the load order. + + +Using a Loadable Module +----------------------- This creates a new configuration that uses the existing built in strobing feature, but provides a different set of presets. @@ -289,6 +296,218 @@ When the module is loaded, then the configuration appears in the configfs file system and is selectable in the same way as the built in configuration described above. -Configurations can use previously loaded features. The system will ensure -that it is not possible to unload a feature that is currently in use, by -enforcing the unload order as the strict reverse of the load order. +The file ``coresight-cfg-sample.c`` contains the configuration and module +initialisation code needed to create the loadable module. + +This will be built alongside the kernel modules if selected in KConfig. +(select ``CONFIG_SAMPLE_CORESIGHT_SYSCFG``). + +An example of a custom configuration module is found in ``./samples/coresight``. + +Using a Configuration Table File +-------------------------------- + +The ``./tools/coresight`` directory contains example programs to generate and +read and print binary configuration table files. + +Building the tools creates the ``coresight-cfg-file-gen`` program that will +generate a configuration binary ``example1.cscfg`` that can be loaded into the +system using configfs. The configuration declared in the source file +``coresight-cfg-example1.c`` is named ``autofdo3`` - the name that will be used +once loaded. + +The source files ``coresight-cfg-bufw.h`` and ``coresight-cfg-bufw.c`` provide a +standard function to convert a configuration declared in 'C' into the correct +binary buffer format. These files can be re-used to create new custom +configurations. Alternatively, additional examples can be added to the +``coresight-cfg-file-gen`` program:: + + $ ./coresight-cfg-file-gen + Coresight Configuration file Generator + + Generating example1 example + Generating example2 example + +The program ``coresight-cfg-file-read`` can read back and print a configuration +binary. This is built using the file reader from the driver code +(``coresight-config-file.c``), which is copied over into ``./tools/coresight`` at +build time.:: + + ./coresight-cfg-file-read example1.cscfg + CoreSight Configuration file reader + ============================================ + + Configuration 1 + Name:- autofdo3 + Description:- + Setup ETMs with strobing for autofdo + Supplied presets allow experimentation with mark-space ratio for various loads + + Uses 1 features:- + Feature-1: strobing + + Provides 4 sets of preset values, 2 presets per set + set[0]: 0x7d0, 0x64, + set[1]: 0x7d0, 0x3e8, + set[2]: 0x7d0, 0x1388, + set[3]: 0x7d0, 0x2710, + + ============================================ + File contains no features + +There are additional attributes in the cs-syscfg directory - ``load`` and +``unload`` that can be used to load and unload configuration tables. + +To load, 'cat' the table file into the load attribute:: + + $ ls /config/cs-syscfg + configurations features load show_last_load unload + $ cat example1.cscfg > /config/cs-syscfg/load + $ ls /config/cs-syscfg/configurations/ + autofdo autofdo3 + +As described above, in order to respect configuration dependencies, unload +order is scrictly enforced to be the reverse of load order. + +``unload`` can be used to unload the last loaded configuration, but only +if this was loaded using configfs. + +To unload, the *load name* of the configuration is written to ``unload``. +This load name is the name of the first configuration in the table, or if no +configurations, the name of the first feature. + +This name can be read either as described above, or by reading the +``show_last_load`` attribute to obtain the details of the last loaded +configuration:: + + $ cat /config/cs-syscfg/show_last_load + load name: autofdo3 + load type: configfs dynamic load + (configurations: autofdo3 ) + (features: None ) + +Once this is known then unload by:: + + $ echo autofdo3 > /config/cs-syscfg/unload + ls /config/cs-syscfg/configurations/ + autofdo + +Load and unload cannot be done if trace is in progress using a configuration. + +Unload will fail if the last loaded item was not a configfs dynamic loaded file. +When using ``show_last_load`` a non configfs item will show:: + + cat /config/cs-syscfg/show_last_load + load name: [Not Set] + load type: Built in driver + + + +CoreSight Configuration Table Format +------------------------------------ + +The file format is defined in the source file ``coresight-config-table.h`` + +The source reader and generator examples use/produce a table in this format, +as a binary file. + +This arrangement is reproduced below:- + +Overall Table structure +~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + [cscfg_table_header] // Mandatory + [CONFIG_ELEM]* // Optional - multiple, defined by cscfg_table_header.nr_configs + [FEATURE_ELEM]* // Optional - multiple, defined by cscfg_table_header.nr_features + +Table is invalid if both [CONFIG_ELEM] and [FEATURE_ELEM] are omitted. + +A table that contains only [FEATURE_ELEM] may be loaded, and the features used +by subsequently loaded files with [CONFIG_ELEM] elements. + +Element Name Strings +~~~~~~~~~~~~~~~~~~~~ + +Configuration name strings are required to consist of alphanumeric characters and '_' only. Other special characters are not permitted. + +For example ``my_config_2`` is a valid name, while ``this-bad-config#5`` will not work. + +This is in order to comply with the requirements of the perf command line. + +It is recommended that Feature and Parameter names use the same convention to allow for future enhancements to the command line syntax. + +CONFIG_ELEM element +~~~~~~~~~~~~~~~~~~~ + +:: + + [cscfg_table_elem_header] // header length value to end of feature strings. + [cscfg_table_elem_str] // name of the configuration. + // (see element string name requirements) + [cscfg_table_elem_str] // description of configuration. + [u16 value](nr_presets) // number of defined sets presets values. + [u32 value](nr_total_params) // total parameters defined by all used features. + [u16 value](nr_feat_refs) // number of features referenced by the configuration + [u64 values] * (nr_presets * nr_total_params) // the preset values. + [cscfg_table_elem_str] * (nr_feat_refs) // names of features used in the configurations. + +FEATURE_ELEM element +~~~~~~~~~~~~~~~~~~~~ + +:: + + [cscfg_table_elem_header] // header length is total bytes to end of param structures. + [cscfg_table_elem_str] // feature name. + [cscfg_table_elem_str] // feature description. + [u32 value](match_flags) // flags to associate the feature with a device. + [u16 value](nr_regs) // number of registers. + [u16 value](nr_params) // number of parameters. + [cscfg_regval_desc struct] * (nr_regs) // register definitions + [PARAM_ELEM] * (nr_params) // parameters definitions + +PARAM_ELEM element +~~~~~~~~~~~~~~~~~~ + +:: + + [cscfg_table_elem_str] // parameter name. + [u64 value](param_value) // initial value. + +Additional definitions +~~~~~~~~~~~~~~~~~~~~~~ + +The following structures are defined in ``coresight-config-file.h`` + + * ``struct cscfg_table_header`` : This structure contains an initial magic number, the total + length of the table, and the number of configurations and features in the table. + * ``struct cscfg_table_elem_header``: This defines the total length and type of a CONFIG_ELEM + or a FEATURE_ELEM. + * ``struct cscfg_table_elem_str``: This defines a string and its length. + +The magic number in cscfg_table_header is defined as two bitfields:: + + [31:8] Fixed magic number to identify table type. + [7:0] Current table format version. + +The following defines determine the maximum overall table size and maximum individual +string size + + * ``CSCFG_TABLE_MAXSIZE`` : maximum overall table size. + * ``CSCFG_TABLE_STR_MAXSIZE`` : maximum individual string size. + +Load Dependencies +~~~~~~~~~~~~~~~~~ + +Files may be unloaded only in the strict reverse order of loading. This is enforced by the +configuration system. + +This is to ensure that any load dependencies are maintained. + +A configuration table that contains a CONFIG_ELEM that references named features "feat_A" and "feat_B" will load only if either:- + +a) "feat_A" and/or "feat_B" has been loaded previously, or are present as built-in / module loaded features. +b) "feat_A" and/or "feat_B" are declared as FEAT_ELEM in the same table as the CONFIG_ELEM. + +Tables that contain features or configurations with the same names as those already loaded will fail to load.