From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0F412C433EF for ; Thu, 7 Apr 2022 07:25:24 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5A62C83DDD; Thu, 7 Apr 2022 09:24:58 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1649316299; bh=b2gGbDT5oC1wlYaJh1aWpyU/cCIeYKczhGqNYGhI0SY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=QQyy/tMM0Tmnpv0dpC4ZY4EaFJQdqBPDOyhncqWrZj4Q9DgsBeqxLeaS0Gp/nwS1E 4WctuhtiWpiNerrBO8GISEIO8ChTurx3dEDNbp0S7zZbvXbCBcdcNr0b4HhJsYvOgi U0v1l3LZ68rMEkj8XDuBiwLT1J6r0mVYl4Oh3EAj9I0SRVfp3EQ3GEX6Xjthev8Fvs HJpF29V83kMDP/ZNTg6famCFHWjyDiT+N4ynOaB/cRtb4L24G8cVYMvznDMbWHqBCt oTootfC+lDQyB1GlJNhXTYRvkJFNQM8YH1tiZ0oqgl8TlFzl5XrONVMNlS8/dLtaTt ZTyICWEZovE/w== Received: by phobos.denx.de (Postfix, from userid 109) id 907D183DD7; Thu, 7 Apr 2022 09:24:18 +0200 (CEST) Received: from mout-u-204.mailbox.org (mout-u-204.mailbox.org [IPv6:2001:67c:2050:1::465:204]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E835B83D61 for ; Thu, 7 Apr 2022 09:12:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sr@denx.de Received: from smtp2.mailbox.org (unknown [91.198.250.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-204.mailbox.org (Postfix) with ESMTPS id 4KYsyW5rv1z9sZk; Thu, 7 Apr 2022 09:12:19 +0200 (CEST) From: Stefan Roese To: u-boot@lists.denx.de Cc: daniel.schwierzeck@gmail.com, awilliams@marvell.com, cchavva@marvell.com Subject: [PATCH v2 33/52] mips: octeon: Add cvmx-pki-resources.c Date: Thu, 7 Apr 2022 09:11:35 +0200 Message-Id: <20220407071154.51997-34-sr@denx.de> In-Reply-To: <20220407071154.51997-1-sr@denx.de> References: <20220407071154.51997-1-sr@denx.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean From: Aaron Williams Import cvmx-pki-resources.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms. Signed-off-by: Aaron Williams Signed-off-by: Stefan Roese --- arch/mips/mach-octeon/cvmx-pki-resources.c | 285 +++++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 arch/mips/mach-octeon/cvmx-pki-resources.c diff --git a/arch/mips/mach-octeon/cvmx-pki-resources.c b/arch/mips/mach-octeon/cvmx-pki-resources.c new file mode 100644 index 000000000000..ab8417216241 --- /dev/null +++ b/arch/mips/mach-octeon/cvmx-pki-resources.c @@ -0,0 +1,285 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2018-2022 Marvell International Ltd. + * + * PKI Support. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include + +static s32 cvmx_pki_style_refcnt[CVMX_MAX_NODES][CVMX_PKI_NUM_INTERNAL_STYLE]; + +/** + * This function allocates/reserves a style from pool of global styles per node. + * @param node node to allocate style from. + * @param style style to allocate, if -1 it will be allocated + * first available style from style resource. If index is positive + * number and in range, it will try to allocate specified style. + * @return style number on success, + * -1 on alloc failure. + * -2 on resource already reserved. + */ +int cvmx_pki_style_alloc(int node, int style) +{ + int rs; + + if (cvmx_create_global_resource_range(CVMX_GR_TAG_STYLE(node), + CVMX_PKI_NUM_INTERNAL_STYLE)) { + printf("ERROR: Failed to create styles global resource\n"); + return -1; + } + if (style >= 0) { + /* Reserving specific style, use refcnt for sharing */ + rs = cvmx_atomic_fetch_and_add32( + &cvmx_pki_style_refcnt[node][style], 1); + if (rs > 0) + return CVMX_RESOURCE_ALREADY_RESERVED; + + rs = cvmx_reserve_global_resource_range(CVMX_GR_TAG_STYLE(node), + style, style, 1); + if (rs == -1) { + /* This means the style is taken by another app */ + printf("ERROR: style %d is reserved by another app\n", + style); + cvmx_atomic_fetch_and_add32( + &cvmx_pki_style_refcnt[node][style], -1); + return CVMX_RESOURCE_ALLOC_FAILED; + } + } else { + /* Allocate first available style */ + rs = cvmx_allocate_global_resource_range( + CVMX_GR_TAG_STYLE(node), style, 1, 1); + if (rs < 0) { + printf("ERROR: Failed to allocate style, none available\n"); + return CVMX_RESOURCE_ALLOC_FAILED; + } + style = rs; + /* Increment refcnt for newly created style */ + cvmx_atomic_fetch_and_add32(&cvmx_pki_style_refcnt[node][style], + 1); + } + return style; +} + +/** + * This function frees a style from pool of global styles per node. + * @param node node to free style from. + * @param style style to free + * @return 0 on success, -1 on failure or + * if the style is shared a positive count of remaining users for this style. + */ +int cvmx_pki_style_free(int node, int style) +{ + int rs; + + rs = cvmx_atomic_fetch_and_add32(&cvmx_pki_style_refcnt[node][style], + -1); + if (rs > 1) + return rs - 1; + + if (cvmx_free_global_resource_range_with_base(CVMX_GR_TAG_STYLE(node), + style, 1) == -1) { + printf("ERROR Failed to release style %d\n", (int)style); + return -1; + } + return 0; +} + +/** + * This function allocates/reserves a cluster group from per node + cluster group resources. + * @param node node to allocate cluster group from. + @param cl_grp cluster group to allocate/reserve, if -1 , + * allocate any available cluster group. + * @return cluster group number + * -1 on alloc failure. + * -2 on resource already reserved. + */ +int cvmx_pki_cluster_grp_alloc(int node, int cl_grp) +{ + int rs; + + if (node >= CVMX_MAX_NODES) { + printf("ERROR: Invalid node number %d\n", node); + return -1; + } + if (cvmx_create_global_resource_range(CVMX_GR_TAG_CLUSTER_GRP(node), + CVMX_PKI_NUM_CLUSTER_GROUP)) { + printf("ERROR: Failed to create Cluster group global resource\n"); + return -1; + } + if (cl_grp >= 0) { + rs = cvmx_reserve_global_resource_range( + CVMX_GR_TAG_CLUSTER_GRP(node), 0, cl_grp, 1); + if (rs == -1) { + debug("INFO: cl_grp %d is already reserved\n", + (int)cl_grp); + return CVMX_RESOURCE_ALREADY_RESERVED; + } + } else { + rs = cvmx_allocate_global_resource_range( + CVMX_GR_TAG_CLUSTER_GRP(node), 0, 1, 1); + if (rs == -1) { + debug("Warning: Failed to alloc cluster grp\n"); + return CVMX_RESOURCE_ALLOC_FAILED; + } + } + cl_grp = rs; + return cl_grp; +} + +/** + * This function allocates/reserves a pcam entry from node + * @param node node to allocate pcam entry from. + * @param index index of pacm entry (0-191), if -1 , + * allocate any available pcam entry. + * @param bank pcam bank where to allocate/reserve pcan entry from + * @param cluster_mask mask of clusters from which pcam entry is needed. + * @return pcam entry of -1 on failure + */ +int cvmx_pki_pcam_entry_alloc(int node, int index, int bank, u64 cluster_mask) +{ + int rs = 0; + unsigned int cluster; + + for (cluster = 0; cluster < CVMX_PKI_NUM_CLUSTER; cluster++) { + if ((cluster_mask & (1 << cluster)) == 0) + continue; + rs = cvmx_create_global_resource_range( + CVMX_GR_TAG_PCAM(node, cluster, bank), + CVMX_PKI_TOTAL_PCAM_ENTRY); + if (rs != 0) { + printf("ERROR: Failed to create pki pcam global resource\n"); + return -1; + } + if (index >= 0) + rs = cvmx_reserve_global_resource_range( + CVMX_GR_TAG_PCAM(node, cluster, bank), cluster, + index, 1); + else + rs = cvmx_allocate_global_resource_range( + CVMX_GR_TAG_PCAM(node, cluster, bank), cluster, + 1, 1); + if (rs == -1) { + printf("ERROR: PCAM :index %d not available in cluster %d bank %d", + (int)index, (int)cluster, bank); + return -1; + } + } /* for cluster */ + index = rs; + /* implement cluster handle for pass2, for now assume + all clusters will have same base index*/ + return index; +} + +/** + * This function allocates/reserves QPG table entries per node. + * @param node node number. + * @param base_offset base_offset in qpg table. If -1, first available + * qpg base_offset will be allocated. If base_offset is positive + * number and in range, it will try to allocate specified base_offset. + * @param count number of consecutive qpg entries to allocate. They will be consecutive + * from base offset. + * @return qpg table base offset number on success + * -1 on alloc failure. + * -2 on resource already reserved. + */ +int cvmx_pki_qpg_entry_alloc(int node, int base_offset, int count) +{ + int rs; + + if (cvmx_create_global_resource_range(CVMX_GR_TAG_QPG_ENTRY(node), + CVMX_PKI_NUM_QPG_ENTRY)) { + printf("ERROR: Failed to create qpg_entry global resource\n"); + return -1; + } + if (base_offset >= 0) { + rs = cvmx_reserve_global_resource_range( + CVMX_GR_TAG_QPG_ENTRY(node), base_offset, base_offset, + count); + if (rs == -1) { + debug("INFO: qpg entry %d is already reserved\n", + (int)base_offset); + return CVMX_RESOURCE_ALREADY_RESERVED; + } + } else { + rs = cvmx_allocate_global_resource_range( + CVMX_GR_TAG_QPG_ENTRY(node), base_offset, count, 1); + if (rs == -1) { + printf("ERROR: Failed to allocate qpg entry\n"); + return CVMX_RESOURCE_ALLOC_FAILED; + } + } + base_offset = rs; + return base_offset; +} + +/** + * This function frees QPG table entries per node. + * @param node node number. + * @param base_offset base_offset in qpg table. If -1, first available + * qpg base_offset will be allocated. If base_offset is positive + * number and in range, it will try to allocate specified base_offset. + * @param count number of consecutive qpg entries to allocate. They will be consecutive + * from base offset. + * @return qpg table base offset number on success, -1 on failure. + */ +int cvmx_pki_qpg_entry_free(int node, int base_offset, int count) +{ + if (cvmx_free_global_resource_range_with_base( + CVMX_GR_TAG_QPG_ENTRY(node), base_offset, count) == -1) { + printf("ERROR Failed to release qpg offset %d", + (int)base_offset); + return -1; + } + return 0; +} + +int cvmx_pki_mtag_idx_alloc(int node, int idx) +{ + if (cvmx_create_global_resource_range(CVMX_GR_TAG_MTAG_IDX(node), + CVMX_PKI_NUM_MTAG_IDX)) { + printf("ERROR: Failed to create MTAG-IDX global resource\n"); + return -1; + } + if (idx >= 0) { + idx = cvmx_reserve_global_resource_range( + CVMX_GR_TAG_MTAG_IDX(node), idx, idx, 1); + if (idx == -1) { + debug("INFO: MTAG index %d is already reserved\n", + (int)idx); + return CVMX_RESOURCE_ALREADY_RESERVED; + } + } else { + idx = cvmx_allocate_global_resource_range( + CVMX_GR_TAG_MTAG_IDX(node), idx, 1, 1); + if (idx == -1) { + printf("ERROR: Failed to allocate MTAG index\n"); + return CVMX_RESOURCE_ALLOC_FAILED; + } + } + return idx; +} -- 2.35.1