From nobody Tue Oct 7 20:08:41 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34A05218821; Tue, 8 Jul 2025 04:49:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751950196; cv=none; b=DPHd/5I7sITCM8uvWazGNKGr8lZ60Hu6qwOWvH434Doscz6PMj+2enF+SC9mg5nSGD3Ecj0Xl/3xLYkuBQRjlMP+/wOrwjVb5NBWp1JnFvi1hbV7DAWPBlsNikaN3M8gBcavC7MnnowkOvByPLaQSZvT16HwW9dOAjaMXei2rk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751950196; c=relaxed/simple; bh=lBthyPAfKhHtbhMUBAuoaz/8Tw5PYAJ1Odx6iC5j3ns=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=P0Aa3wMPodN63GWhyKv1j+aVTBmQPilbzrWzpj1EsYmU90+ugh5NawB6SGpSQ4h1xXkAxccxgg0inlLMROc1XzjztRzVYg+q46nrz3fQM0axfV2qaOVqGlkGmmFxN3m6GyCAXsL/LU1q6L1jm5asMKwV16dAM5kLBvYvRzf5KCk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mWiuw0dq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mWiuw0dq" Received: by smtp.kernel.org (Postfix) with ESMTPS id BDA02C4CEF0; Tue, 8 Jul 2025 04:49:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751950195; bh=lBthyPAfKhHtbhMUBAuoaz/8Tw5PYAJ1Odx6iC5j3ns=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=mWiuw0dqGTuR6DzY5sOLlvskm9m2j8635xdOkyxPZErFrRZmtXWB2TlNzLz8QecfI Tw4Zlc8L94On6ZTWETY1hnvVmZQG+/wlM1OVk4BBz3MzU7QzHVCo84gvFTiJM8Rkgp BWmzoRsgLFahc6vIwqKaFh8AyGFENDSUZ71/T1F/E19/F9V0FocBFoU+0KxTKigqR+ jnG9MvG2opK/KXpmUF9LGfK+Xe3grU+hqTid26VLuQCgI5WDIeVeh0YKRxLSCMDfO+ 3tNdfuXmINXDCQIH+rm2ZLoXFocESZpv9AcwIoBf9kKeEZIwTac0bEkOu0BnTEouDA lRy8XJwUYLEiw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id AFE94C83F0D; Tue, 8 Jul 2025 04:49:55 +0000 (UTC) From: Mahesh Rao via B4 Relay Date: Tue, 08 Jul 2025 12:49:07 +0800 Subject: [PATCH RESEND v5 2/5] firmware: stratix10-svc: Implement ID pool management for asynchronous operations Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250708-sip_svc_upstream-v5-2-9c4289256d54@altera.com> References: <20250708-sip_svc_upstream-v5-0-9c4289256d54@altera.com> In-Reply-To: <20250708-sip_svc_upstream-v5-0-9c4289256d54@altera.com> To: Dinh Nguyen , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Mahesh Rao Cc: Matthew Gerlach , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1751950193; l=6320; i=mahesh.rao@altera.com; s=20250107; h=from:subject:message-id; bh=n18tKLs7MLh8G8eEquC0bpSpqdEDws81zjcXK5xIQgg=; b=RgKbq92m4gtHYTuVFyPGfyDCUjR+X43gsEi6VVxPT5jkwafMyFM2W0YDyDy91nbY0m+Fpd+Vn VKQZcqFLjJHCQdUb9v7KWob0M1b9upE/Ba8HnPzBb/XbZI5XguzXx4o X-Developer-Key: i=mahesh.rao@altera.com; a=ed25519; pk=tQiFUzoKxHrQLDtWeEeaeTeJTl/UfclUHWZy1fjSiyg= X-Endpoint-Received: by B4 Relay for mahesh.rao@altera.com/20250107 with auth_id=337 X-Original-From: Mahesh Rao Reply-To: mahesh.rao@altera.com From: Mahesh Rao Implement ID pool management API's which will be used for Stratix10 Asynchronous communication with Secure Device Manager. These API's will be used in subsequent patches for ID management in asynchronous operations. Signed-off-by: Mahesh Rao Reviewed-by: Matthew Gerlach --- drivers/firmware/stratix10-svc.c | 194 +++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 194 insertions(+) diff --git a/drivers/firmware/stratix10-svc.c b/drivers/firmware/stratix10-= svc.c index 73c77b8e9f2b44a6729dd349ef0947ed47be681b..a8c57963da993a3d6207dbf8026= 33bc82006ada7 100644 --- a/drivers/firmware/stratix10-svc.c +++ b/drivers/firmware/stratix10-svc.c @@ -170,6 +170,21 @@ struct stratix10_svc_chan { spinlock_t lock; }; =20 +/** + * struct stratix10_sip_id_pool - Structure representing a pool of IDs for + * asynchronous operations. + * @head: The head index of the ID pool. + * @size: The total size of the ID pool. + * @id_mask: Pointer to an array representing the mask of allocated I= Ds. + * @lock: Mutex lock to protect access to the ID pool. + */ +struct stratix10_sip_id_pool { + unsigned long head; + unsigned long size; + unsigned long *id_mask; + struct mutex lock; +}; + static LIST_HEAD(svc_ctrl); static LIST_HEAD(svc_data_mem); /* svc_mem_lock protects access to the svc_data_mem list for @@ -177,6 +192,185 @@ static LIST_HEAD(svc_data_mem); */ static DEFINE_MUTEX(svc_mem_lock); =20 +/** + * stratix10_id_pool_create - Create a new ID pool for Stratix10 + * async operation + * @size: The size of the ID pool to create + * + * This function allocates and initializes a new ID pool structure + * for Stratix10 async operations. It allocates memory for the ID + * pool structure and the associated bitmaps for ID management. + * + * Return: Pointer to the newly created ID pool structure, or NULL + * on failure. + */ +static struct stratix10_sip_id_pool *stratix10_id_pool_create(unsigned lon= g size) +{ + struct stratix10_sip_id_pool *id_pool =3D NULL; + + if (size =3D=3D 0) + return NULL; + + id_pool =3D kzalloc(sizeof(*id_pool), GFP_KERNEL); + if (!id_pool) + return NULL; + + id_pool->size =3D size; + + id_pool->id_mask =3D bitmap_zalloc(size, GFP_KERNEL); + if (!id_pool->id_mask) { + kfree(id_pool); + return NULL; + } + + mutex_init(&id_pool->lock); + + return id_pool; +} + +/** + * stratix10_id_pool_destroy - Destroy an ID pool for Stratix10 async oper= ation + * @id_pool: Pointer to the ID pool structure + * + * This function destroys an ID pool for Stratix10 async operations. It fi= rst + * checks if the ID pool is valid, then frees the associated bitmap and the + * ID pool structure itself. + * + * Return: 0 on success, -EINVAL if the ID pool is invalid. + */ +static int stratix10_id_pool_destroy(struct stratix10_sip_id_pool *id_pool) +{ + if (!id_pool) + return -EINVAL; + + mutex_lock(&id_pool->lock); + + if (id_pool->id_mask) + bitmap_free(id_pool->id_mask); + + mutex_unlock(&id_pool->lock); + mutex_destroy(&id_pool->lock); + + kfree(id_pool); + + return 0; +} + +/** + * stratix10_reserve_id - Reserve an ID in the ID pool + * @id_pool: Pointer to the ID pool structure + * @id: The ID to be reserved + * + * This function reserves an ID in the given ID pool. It first checks if + * the ID pool is valid and if the ID is within the valid range. + * + * Return: + * 0 on success, + * -EINVAL if the ID pool is invalid, the ID is out of range, or the ID is + * already reserved. + */ +static int stratix10_reserve_id(struct stratix10_sip_id_pool *id_pool, uns= igned long id) +{ + if (!id_pool) + return -EINVAL; + + if (id >=3D id_pool->size) + return -EINVAL; + + mutex_lock(&id_pool->lock); + + if (test_bit(id, id_pool->id_mask)) { + mutex_unlock(&id_pool->lock); + return -EINVAL; + } + set_bit(id, id_pool->id_mask); + + mutex_unlock(&id_pool->lock); + return 0; +} + +/** + * stratix10_allocate_id - Allocate an ID from the ID pool + * @id_pool: Pointer to the ID pool structure + * + * This function allocates an ID from the given ID pool. It searches for + * the next available ID in the pool, marks it as allocated, + * and returns it. + * + * Return: + * A non-negative integer representing the allocated ID on success + * -EINVAL if the id_pool is NULL + * -ENOMEM if no IDs are available in the pool + */ +static int stratix10_allocate_id(struct stratix10_sip_id_pool *id_pool) +{ + unsigned long tries =3D 0; + int id; + + if (!id_pool) + return -EINVAL; + + if (id_pool->head >=3D id_pool->size) + return -ENOMEM; + + mutex_lock(&id_pool->lock); + + do { + id_pool->head =3D find_next_zero_bit(id_pool->id_mask, + id_pool->size, id_pool->head); + if (id_pool->head >=3D id_pool->size) { + id_pool->head =3D 0; + tries++; + } + /* cycle through the whole bitmap at least once*/ + } while (tries < 2 && test_bit(id_pool->head, id_pool->id_mask)); + + if (tries >=3D 2) { + mutex_unlock(&id_pool->lock); + return -ENOMEM; + } + + set_bit(id_pool->head, id_pool->id_mask); + id =3D id_pool->head; + id_pool->head =3D (id_pool->head + 1) % id_pool->size; + mutex_unlock(&id_pool->lock); + return id; +} + +/** + * stratix10_deallocate_id - Deallocate an ID in the ID pool + * @id_pool: Pointer to the ID pool structure + * @id: The ID to be deallocated + * + * This function deallocates an ID in the given ID pool. It first + * checks if the ID pool is valid and if the ID is within the valid + * range. + * + * Return: + * 0 on success, + * -EINVAL if the ID pool is invalid, the ID is out of range, or the + * ID is not set. + */ +static int stratix10_deallocate_id(struct stratix10_sip_id_pool *id_pool, = unsigned long id) +{ + int ret =3D -EINVAL; + + if (!id_pool) + return ret; + + if (id >=3D id_pool->size) + return ret; + + mutex_lock(&id_pool->lock); + if (test_bit(id, id_pool->id_mask)) { + clear_bit(id, id_pool->id_mask); + ret =3D 0; + } + mutex_unlock(&id_pool->lock); + + return ret; +} + /** * svc_pa_to_va() - translate physical address to virtual address * @addr: to be translated physical address --=20 2.35.3