From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 650E342600; Tue, 19 Sep 2023 11:57:29 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C0AD840EF0; Tue, 19 Sep 2023 11:56:03 +0200 (CEST) Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2110.outbound.protection.outlook.com [40.107.243.110]) by mails.dpdk.org (Postfix) with ESMTP id 8FB8B40ECF for ; Tue, 19 Sep 2023 11:55:57 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VG1wxmyFwJ3HL+IAQtt18CbkGqazXMXI3Qa71Q6L4cWcImJy2FtrLKOzomCPnmjxED/j2BiHElGvELPZ+3wr2EeGHT/h5Si2UE5MzkVrObozpCoFFBYY/Q8WcM3Bt+jvsyVWtEjLH6lHZWEj5zYOmlEkIBZck90N+/N5MGUSHsSGEw436B0VR3IxE3cp9yVh20HqIZOv1C7Nesao06IFM7uAA3LtxM3fdilRYIJuOtk80Idi4RrA+RSwmfYn7vJy5XPt6bUY6MFiLT7bsWtfjBnhp+ZjIJsv6ympF/X62YMJTi5gBC2f7kKjHxO32JVGqz1OwdKzOWn+dd0YWiQEmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=HO/WS3nZ7otbTAkxBeSVSWcslCn2AlhzaWiM5uoDsnU=; b=aeXHb+qR8X//3lrCZ8MFRtSclDOkK0DyM/oPBcRGbTq0b+r4erndfKbwmDIGiHiSU7g1MrMHWUy53zxnCbqRDLxkc7JC1jDR6nNjS14O+BE/u9zYvs8i4bj/KOTPDyXI/7+lUS3CM86U7+ELiedU1AHHEJatKJw5qZWtK8fJq9LLjL92DgRrnURvGxt0X5ned1C9tC0Lib8pP1Y0L5TEuWy+3pSIT3eExJAXYca0siUhVAZDoqBf5FzQ7C9/l/YEUkJ/hLP5hmHqgROpsO4Mj3DJT407Nt3uqS/sXoNO0K7h0mKiMkx1ORRa+kMq4gB6/sh/4svgAt6t234tyeMafg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=corigine.com; dmarc=pass action=none header.from=corigine.com; dkim=pass header.d=corigine.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=corigine.onmicrosoft.com; s=selector2-corigine-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HO/WS3nZ7otbTAkxBeSVSWcslCn2AlhzaWiM5uoDsnU=; b=pAuyjAj17BSz4h8t+R41f7nyGQE9xvYOtcBk4cF+K39H6Ny9+S4jI23p5vnVlNJW4QZtEeDcpWyYXLQ3cFAQN2LeBDgUQBVUDvJh9qgzjPY7Y+Awyb/NkaCLzdlAwUxfvPQM5t+CbIqgtC6KD8Brp8NLbkzhYf7FbgIylMINMUE= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=corigine.com; Received: from SJ0PR13MB5545.namprd13.prod.outlook.com (2603:10b6:a03:424::5) by MW5PR13MB5877.namprd13.prod.outlook.com (2603:10b6:303:1c7::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.28; Tue, 19 Sep 2023 09:55:51 +0000 Received: from SJ0PR13MB5545.namprd13.prod.outlook.com ([fe80::7a1c:2887:348a:84bd]) by SJ0PR13MB5545.namprd13.prod.outlook.com ([fe80::7a1c:2887:348a:84bd%6]) with mapi id 15.20.6792.026; Tue, 19 Sep 2023 09:55:51 +0000 From: Chaoyong He To: dev@dpdk.org Cc: oss-drivers@corigine.com, Chaoyong He Subject: [PATCH v5 19/26] net/nfp: refact the mutex module Date: Tue, 19 Sep 2023 17:54:33 +0800 Message-Id: <20230919095440.45445-20-chaoyong.he@corigine.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230919095440.45445-1-chaoyong.he@corigine.com> References: <20230918024612.1600536-1-chaoyong.he@corigine.com> <20230919095440.45445-1-chaoyong.he@corigine.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: BY5PR04CA0014.namprd04.prod.outlook.com (2603:10b6:a03:1d0::24) To SJ0PR13MB5545.namprd13.prod.outlook.com (2603:10b6:a03:424::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR13MB5545:EE_|MW5PR13MB5877:EE_ X-MS-Office365-Filtering-Correlation-Id: d052ef02-65ee-4eb0-ca0c-08dbb8f69b1d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zCSPQIvRaIe3AqymxrU3sJZO0R7UayMwZaHJx+qF1JluogbOcBoNMxoH98R1HSeY2q3PAH1qGyWtDAyQAq1D1DSpx10iLdR6GQLmpaRbj7p5uWRWWlzx3nyl4kXCxR2mkWUngiQgwApdOc9LXHaPvLOtgo+RHSOg9PAnDDmOSa8skGFHyu9z/z5FcCWycvE05xclV/XD0i1HSEHQD3Dt4VCgj2wiphvXzT/wDkQGZ25Z8yp8BmCA/Wi9uGvKqtEvU96GpCfiENCipHe0H9S8bpMmpDU2kjXiZSsrCon9MWeDRyOwovAawk2z/4ZXl6V/0M5CpB0FQfCbkKkUsNq0vf2mqBEE4CvNxSNj4E7jqgxg2M72Q6AcoqM6Mhqwsp6I+L1Xeu5Tr0OAvpex/Pzb5R0einHFSDNEzyuyeTMuPwCYPT/njIGzSRxSvbwNVCiYRyTyB6Tp9R6Num69fc2nD3/ZZKlKN+YzghiSIxurKZu/FzxzX30VCLdMkqnC1HcH9Xe8ni763Ia49zjCEsc4IhhvJEGkWRyq8MfqWMGLxkAd0oYoFv1nd7dMx3s1eCVF/Taf87MyZa+PtLrLI2ULz0O9w5Ial0U6mkj2EoTIFssd0bVw6/oEYKULk+nAeTk+1LOIoOxo/pEKu7kME12Q0uh2pwVVTjY067E0m72k9bE= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SJ0PR13MB5545.namprd13.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(346002)(136003)(396003)(39840400004)(366004)(376002)(186009)(451199024)(1800799009)(6506007)(6486002)(6666004)(52116002)(86362001)(36756003)(38350700002)(38100700002)(2616005)(1076003)(107886003)(26005)(2906002)(30864003)(6512007)(478600001)(83380400001)(8676002)(5660300002)(4326008)(41300700001)(8936002)(44832011)(66946007)(66556008)(66476007)(6916009)(316002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?5lgAsJmGA6a26JjCp0m3DsvN/GuzU/fSSvkKqLzhbxiP/PX7Pd+zaZifVmY7?= =?us-ascii?Q?RXsiMHvuXjMrIBVNe/3AH9T0QWd6e7QU/LoFihsu7DeKXTCcsAYhZ/UDxYWo?= =?us-ascii?Q?7dhWQaDVPsZIF1pCpiwZ8Ovys/Y9ow3dy8lzSKH74vZVAEaZozuXUnxn6GlR?= =?us-ascii?Q?SYugH4uS5B5oFtZZNxfxRnq02Qg75qnluafDRPDIKuJrB8sd7DcH5bjxCZg8?= =?us-ascii?Q?z2Cuba+FcIO1WawGlrmMFJJc/XGW2svGuSV9L3mohVpbPiQAe4UihP1AFiNQ?= =?us-ascii?Q?r1A2CIQ5QAuBs3GI6uEgIeGNCasA7zfA9nCbOh82MQtkdswNDWRX9ZDB9u5S?= =?us-ascii?Q?zEUgHFsrsPdAhJ7gmh7DUixTauuV22IQe5CzpA/YN2A7YdlxTv05sG8NsLpV?= =?us-ascii?Q?wOjbtAFSqOsga3WgPveVET6DvT7N8YMWgPJBIOQqLdRGbbj+6leybPD36Ag3?= =?us-ascii?Q?hfZv2mVnz+a7cZ3eL+6VCTuUusyGYgTJp5TIefoIui9TnTlyD4k3+gP3HjE1?= =?us-ascii?Q?3OP1SweXuYasIwiGTZ6M7BQlw7Fz/88fl8/dTLrqSj7u/7iZxqqZ/+xdgDr0?= =?us-ascii?Q?aJwEwg9bLS7IpVEsAJJRPY63II2LgISF+M7Km0jtLibpBAHsfBRyVAttPZV2?= =?us-ascii?Q?peqBvRbAk4GWUIKfJLxCzAtzR2iKsknqRlxZPurWhKdmfwM5emUMjeJlVX+P?= =?us-ascii?Q?vyCGTrEw4QC4B7ib9kwSvjFl/EyuJOBTBMafnELf7ih/3NBTmQpuSF6+RV/D?= =?us-ascii?Q?xNp5vQ9GM7FHzKNIOTHtI0Iv8lOZvPj8qhh+eKjDNCvqKVChXe1r3AHxTvm9?= =?us-ascii?Q?UIOS6lNi93Ct+c7SnfM8B4ngpP5YFh9f7IKYpu6mARKvQlk57BWWZyJxXBYg?= =?us-ascii?Q?J39wU4kHeYX1FselBSshLgXOtFnk6LxAGSqRNFeAoXwvsEhIHuJxUn+hdtU8?= =?us-ascii?Q?dH5k9zOvFsZSSLIQOeK7NdXotuqLmvsiA7H9ZCoscxIWx4VjDssVMjorjpyx?= =?us-ascii?Q?l9FI713jgMiaAypBI8BfvNlEVe9okm7Lgc/qjaKxUZCT1oC2CTZtphd89fEN?= =?us-ascii?Q?uNQjlsTT2+6yoSKeehwO11SF0Z9GsfBHxu5gxpoO9r0H869tKsyWOiCuB08d?= =?us-ascii?Q?yRDiWjkLS8nNfrODNcvuAaPhJaPth6Kt8UMRkwqNYLsCB+6pyImwZqhgsyxJ?= =?us-ascii?Q?mW/i3RWVJZsQUmGByFu/JMxEGhvAHPc5a4lcpX8Pl3gjnPt3ErfgtVBtgaxb?= =?us-ascii?Q?RJX/Dpu7tMFULYS/HpzhHA7Ho85AGMwKTxeL6qtD9mNCCWTfx2iC0SlcBfsg?= =?us-ascii?Q?0iHztxoSCakLiRl3HpEWMz/mPIcQpnXmI1Jsx6P2e1JDSPss5+CknevD6d4j?= =?us-ascii?Q?WDAevpiQma96Fmb+TaRjxbb/4FilNnwvKeUve+IXiUMT8WHY/Sejob4l5VKO?= =?us-ascii?Q?M0fgYVSLYOtpgkI1ZjvQqDV7+bVGZZ2c6PVeyoV3PDSUtxU5Zs9Gnml0Ef3W?= =?us-ascii?Q?K7xm1yqgd55cR067F0uJ5vYByI9IDz2Dt7qZg/4Q8Kup1vMdOCfkD9O66z77?= =?us-ascii?Q?l4EmPiJjiS1u8LV+s9ZmDn6/d+SRPHEga0paOItU/bJLiBVuC5l9VBob1JyI?= =?us-ascii?Q?Cg=3D=3D?= X-OriginatorOrg: corigine.com X-MS-Exchange-CrossTenant-Network-Message-Id: d052ef02-65ee-4eb0-ca0c-08dbb8f69b1d X-MS-Exchange-CrossTenant-AuthSource: SJ0PR13MB5545.namprd13.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2023 09:55:50.9963 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: fe128f2c-073b-4c20-818e-7246a585940c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: EdLfpu+o28cVEJZMBy2r0/OTRiVXvSg9fPooQAQsTsFt3LS+CNUjpl9JY2eFlcro1Nh2Wiwmg3mi6BRHiSyufMYvNTPRAWIkaEehp8+lZkk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR13MB5877 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Add a header file to holds the API declarations of this module. Also sync the logic from kernel driver and remove the unneeded header file include statements. Signed-off-by: Chaoyong He --- drivers/net/nfp/nfpcore/nfp_cpp.h | 16 -- drivers/net/nfp/nfpcore/nfp_mutex.c | 205 ++++++++++++++----------- drivers/net/nfp/nfpcore/nfp_mutex.h | 25 +++ drivers/net/nfp/nfpcore/nfp_resource.c | 1 + 4 files changed, 138 insertions(+), 109 deletions(-) create mode 100644 drivers/net/nfp/nfpcore/nfp_mutex.h diff --git a/drivers/net/nfp/nfpcore/nfp_cpp.h b/drivers/net/nfp/nfpcore/nfp_cpp.h index be7ae1d919..42c4df5fdd 100644 --- a/drivers/net/nfp/nfpcore/nfp_cpp.h +++ b/drivers/net/nfp/nfpcore/nfp_cpp.h @@ -8,8 +8,6 @@ #include -struct nfp_cpp_mutex; - /* NFP CPP handle */ struct nfp_cpp { uint32_t model; @@ -426,20 +424,6 @@ int nfp_cpp_readq(struct nfp_cpp *cpp, uint32_t cpp_id, int nfp_cpp_writeq(struct nfp_cpp *cpp, uint32_t cpp_id, uint64_t address, uint64_t value); -int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target, - uint64_t address, uint32_t key_id); - -struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target, - uint64_t address, uint32_t key_id); - -void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex); - -int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex); - -int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex); - -int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex); - uint32_t nfp_cpp_mu_locality_lsb(struct nfp_cpp *cpp); #endif /* __NFP_CPP_H__ */ diff --git a/drivers/net/nfp/nfpcore/nfp_mutex.c b/drivers/net/nfp/nfpcore/nfp_mutex.c index 96ba60697c..3c10c7a090 100644 --- a/drivers/net/nfp/nfpcore/nfp_mutex.c +++ b/drivers/net/nfp/nfpcore/nfp_mutex.c @@ -3,21 +3,13 @@ * All rights reserved. */ -#include -#include +#include "nfp_mutex.h" + #include -#include "nfp_cpp.h" #include "nfp_logs.h" #include "nfp_target.h" -#define MUTEX_LOCKED(interface) ((((uint32_t)(interface)) << 16) | 0x000f) -#define MUTEX_UNLOCK(interface) (0 | 0x0000) - -#define MUTEX_IS_LOCKED(value) (((value) & 0xffff) == 0x000f) -#define MUTEX_IS_UNLOCKED(value) (((value) & 0xffff) == 0x0000) -#define MUTEX_INTERFACE(value) (((value) >> 16) & 0xffff) - /* * If you need more than 65536 recursive locks, please * rethink your code. @@ -34,21 +26,51 @@ struct nfp_cpp_mutex { struct nfp_cpp_mutex *prev, *next; }; +static inline uint32_t +nfp_mutex_locked(uint16_t interface) +{ + return (uint32_t)interface << 16 | 0x000f; +} + +static inline uint32_t +nfp_mutex_unlocked(uint16_t interface) +{ + return (uint32_t)interface << 16 | 0x0000; +} + +static inline uint16_t +nfp_mutex_owner(uint32_t val) +{ + return (val >> 16) & 0xffff; +} + +static inline bool +nfp_mutex_is_locked(uint32_t val) +{ + return (val & 0xffff) == 0x000f; +} + +static inline bool +nfp_mutex_is_unlocked(uint32_t val) +{ + return (val & 0xffff) == 0; +} + static int -nfp_cpp_mutex_validate(uint32_t model, +nfp_cpp_mutex_validate(uint16_t interface, int *target, uint64_t address) { + /* Not permitted on invalid interfaces */ + if (NFP_CPP_INTERFACE_TYPE_of(interface) == NFP_CPP_INTERFACE_TYPE_INVALID) + return -EINVAL; + /* Address must be 64-bit aligned */ if ((address & 7) != 0) return -EINVAL; - if (NFP_CPP_MODEL_IS_6000(model)) { - if (*target != NFP_CPP_TARGET_MU) - return -EINVAL; - } else { + if (*target != NFP_CPP_TARGET_MU) return -EINVAL; - } return 0; } @@ -84,10 +106,10 @@ nfp_cpp_mutex_init(struct nfp_cpp *cpp, uint32_t key) { int err; - uint32_t model = nfp_cpp_model(cpp); uint32_t muw = NFP_CPP_ID(target, 4, 0); /* atomic_write */ + uint16_t interface = nfp_cpp_interface(cpp); - err = nfp_cpp_mutex_validate(model, &target, address); + err = nfp_cpp_mutex_validate(interface, &target, address); if (err < 0) return err; @@ -95,8 +117,7 @@ nfp_cpp_mutex_init(struct nfp_cpp *cpp, if (err < 0) return err; - err = nfp_cpp_writel(cpp, muw, address + 0, - MUTEX_LOCKED(nfp_cpp_interface(cpp))); + err = nfp_cpp_writel(cpp, muw, address, nfp_mutex_locked(interface)); if (err < 0) return err; @@ -133,26 +154,10 @@ nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int err; uint32_t tmp; struct nfp_cpp_mutex *mutex; - uint32_t model = nfp_cpp_model(cpp); uint32_t mur = NFP_CPP_ID(target, 3, 0); /* atomic_read */ + uint16_t interface = nfp_cpp_interface(cpp); - /* Look for cached mutex */ - for (mutex = cpp->mutex_cache; mutex; mutex = mutex->next) { - if (mutex->target == target && mutex->address == address) - break; - } - - if (mutex) { - if (mutex->key == key) { - mutex->usage++; - return mutex; - } - - /* If the key doesn't match... */ - return NULL; - } - - err = nfp_cpp_mutex_validate(model, &target, address); + err = nfp_cpp_mutex_validate(interface, &target, address); if (err < 0) return NULL; @@ -172,16 +177,6 @@ nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, mutex->address = address; mutex->key = key; mutex->depth = 0; - mutex->usage = 1; - - /* Add mutex to the cache */ - if (cpp->mutex_cache) { - cpp->mutex_cache->prev = mutex; - mutex->next = cpp->mutex_cache; - cpp->mutex_cache = mutex; - } else { - cpp->mutex_cache = mutex; - } return mutex; } @@ -195,20 +190,6 @@ nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex) { - mutex->usage--; - if (mutex->usage > 0) - return; - - /* Remove mutex from the cache */ - if (mutex->next) - mutex->next->prev = mutex->prev; - if (mutex->prev) - mutex->prev->next = mutex->next; - - /* If mutex->cpp == NULL, something broke */ - if (mutex->cpp && mutex == mutex->cpp->mutex_cache) - mutex->cpp->mutex_cache = mutex->next; - free(mutex); } @@ -268,32 +249,28 @@ nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex) return 0; } - err = nfp_cpp_readl(mutex->cpp, mur, mutex->address, &value); - if (err < 0) - goto exit; - err = nfp_cpp_readl(mutex->cpp, mur, mutex->address + 4, &key); if (err < 0) - goto exit; + return err; - if (key != mutex->key) { - err = -EPERM; - goto exit; - } + if (key != mutex->key) + return -EPERM; - if (value != MUTEX_LOCKED(interface)) { - err = -EACCES; - goto exit; - } + err = nfp_cpp_readl(mutex->cpp, mur, mutex->address, &value); + if (err < 0) + return err; + + if (value != nfp_mutex_locked(interface)) + return -EACCES; - err = nfp_cpp_writel(cpp, muw, mutex->address, MUTEX_UNLOCK(interface)); + err = nfp_cpp_writel(cpp, muw, mutex->address, + nfp_mutex_unlocked(interface)); if (err < 0) - goto exit; + return err; mutex->depth = 0; -exit: - return err; + return 0; } /** @@ -332,19 +309,17 @@ nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex) /* Verify that the lock marker is not damaged */ err = nfp_cpp_readl(cpp, mur, mutex->address + 4, &key); if (err < 0) - goto exit; + return err; - if (key != mutex->key) { - err = -EPERM; - goto exit; - } + if (key != mutex->key) + return -EPERM; /* * Compare against the unlocked state, and if true, * write the interface id into the top 16 bits, and * mark as locked. */ - value = MUTEX_LOCKED(nfp_cpp_interface(cpp)); + value = nfp_mutex_locked(nfp_cpp_interface(cpp)); /* * We use test_set_imm here, as it implies a read @@ -361,10 +336,10 @@ nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex) */ err = nfp_cpp_readl(cpp, mus, mutex->address, &tmp); if (err < 0) - goto exit; + return err; /* Was it unlocked? */ - if (MUTEX_IS_UNLOCKED(tmp)) { + if (nfp_mutex_is_unlocked(tmp)) { /* * The read value can only be 0x....0000 in the unlocked state. * If there was another contending for this lock, then @@ -376,20 +351,64 @@ nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex) */ err = nfp_cpp_writel(cpp, muw, mutex->address, value); if (err < 0) - goto exit; + return err; mutex->depth = 1; - goto exit; + return 0; } /* Already locked by us? Success! */ if (tmp == value) { mutex->depth = 1; - goto exit; + return 0; } - err = MUTEX_IS_LOCKED(tmp) ? -EBUSY : -EINVAL; + return nfp_mutex_is_locked(tmp) ? -EBUSY : -EINVAL; +} + +/** + * Release lock if held by local system. + * Extreme care is advised, call only when no local lock users can exist. + * + * @param cpp + * NFP CPP handle + * @param target + * NFP CPP target ID (ie NFP_CPP_TARGET_CLS or NFP_CPP_TARGET_MU) + * @param address + * Offset into the address space of the NFP CPP target ID + * + * @return + * - (0) if the lock was OK + * - (1) if locked by us + * - (-errno) on invalid mutex + */ +int +nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, + int target, + uint64_t address) +{ + int err; + uint32_t tmp; + uint16_t interface = nfp_cpp_interface(cpp); + const uint32_t mur = NFP_CPP_ID(target, 3, 0); /* atomic_read */ + const uint32_t muw = NFP_CPP_ID(target, 4, 0); /* atomic_write */ + + err = nfp_cpp_mutex_validate(interface, &target, address); + if (err != 0) + return err; + + /* Check lock */ + err = nfp_cpp_readl(cpp, mur, address, &tmp); + if (err < 0) + return err; + + if (nfp_mutex_is_unlocked(tmp) || nfp_mutex_owner(tmp) != interface) + return 0; + + /* Bust the lock */ + err = nfp_cpp_writel(cpp, muw, address, nfp_mutex_unlocked(interface)); + if (err < 0) + return err; -exit: - return err; + return 1; } diff --git a/drivers/net/nfp/nfpcore/nfp_mutex.h b/drivers/net/nfp/nfpcore/nfp_mutex.h new file mode 100644 index 0000000000..a79490b4d6 --- /dev/null +++ b/drivers/net/nfp/nfpcore/nfp_mutex.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Corigine, Inc. + * All rights reserved. + */ + +#ifndef __NFP_MUTEX_H__ +#define __NFP_MUTEX_H__ + +#include "nfp_cpp.h" + +struct nfp_cpp_mutex; + +int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target, + uint64_t address, uint32_t key_id); + +struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target, + uint64_t address, uint32_t key_id); + +void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex); +int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex); +int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex); +int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex); +int nfp_cpp_mutex_reclaim(struct nfp_cpp *cpp, int target, uint64_t address); + +#endif /* __NFP_MUTEX_H__ */ diff --git a/drivers/net/nfp/nfpcore/nfp_resource.c b/drivers/net/nfp/nfpcore/nfp_resource.c index 539eb69680..d59d2d6c1e 100644 --- a/drivers/net/nfp/nfpcore/nfp_resource.c +++ b/drivers/net/nfp/nfpcore/nfp_resource.c @@ -7,6 +7,7 @@ #include "nfp_crc.h" #include "nfp_logs.h" +#include "nfp_mutex.h" #include "nfp_target.h" #define NFP_RESOURCE_TBL_TARGET NFP_CPP_TARGET_MU -- 2.39.1