diff mbox series

[1/7] hwspinlock: Introduce refcount

Message ID 20240516-hwspinlock-bust-v1-1-47a90a859238@quicinc.com
State New
Headers show
Series Add support for hwspinlock bust | expand

Commit Message

Chris Lew May 16, 2024, 10:58 p.m. UTC
From: Richard Maina <quic_rmaina@quicinc.com>

Currently each device receives a dedicated hwspinlock that is not
accesible to other device drivers. In order to allow multiple consumers
access to the same hwspinlock, introduce a refcount to hwspinlock struct
and implement refcounting infrastructure.

Signed-off-by: Richard Maina <quic_rmaina@quicinc.com>
Signed-off-by: Chris Lew <quic_clew@quicinc.com>
---
 drivers/hwspinlock/hwspinlock_core.c     | 14 +++++++++-----
 drivers/hwspinlock/hwspinlock_internal.h |  2 ++
 2 files changed, 11 insertions(+), 5 deletions(-)

Comments

Bryan O'Donoghue May 17, 2024, 8:58 a.m. UTC | #1
On 17/05/2024 00:58, Chris Lew wrote:
> +	unsigned int refcnt;

Why int and not refcount_t ?

Have you an argument for or against use of one over another ?

---
bod
Chris Lew May 17, 2024, 6:32 p.m. UTC | #2
On 5/17/2024 1:58 AM, Bryan O'Donoghue wrote:
> On 17/05/2024 00:58, Chris Lew wrote:
>> +    unsigned int refcnt;
> 
> Why int and not refcount_t ?
> 
> Have you an argument for or against use of one over another ?
> 

I wanted to avoid the warning if you try to do a refcount_inc on 0. In 
this case, 0 means the the hwlock is unused but the hwlock should 
persist while waiting for another request. It seemed like refcount_t 
expected the associated object to be released once the count hit 0.

Also the count here is serialized by hwspinlock_tree_lock so the need 
for the atomicity provided by refcount_t was unneeded. Using unsigned 
int here seemed simpler.

> ---
> bod
diff mbox series

Patch

diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c
index 0c0a932c00f3..29b33072ff57 100644
--- a/drivers/hwspinlock/hwspinlock_core.c
+++ b/drivers/hwspinlock/hwspinlock_core.c
@@ -428,6 +428,7 @@  static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id)
 	int ret;
 
 	mutex_lock(&hwspinlock_tree_lock);
+	hwlock->refcnt = 0;
 
 	ret = radix_tree_insert(&hwspinlock_tree, id, hwlock);
 	if (ret) {
@@ -671,6 +672,8 @@  static int __hwspin_lock_request(struct hwspinlock *hwlock)
 
 	ret = 0;
 
+	hwlock->refcnt++;
+
 	/* mark hwspinlock as used, should not fail */
 	tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock),
 							HWSPINLOCK_UNUSED);
@@ -829,12 +832,13 @@  int hwspin_lock_free(struct hwspinlock *hwlock)
 	/* notify the underlying device that power is not needed */
 	pm_runtime_put(dev);
 
-	/* mark this hwspinlock as available */
-	tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock),
-							HWSPINLOCK_UNUSED);
+	if (--hwlock->refcnt == 0) {
+		/* mark this hwspinlock as available */
+		tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock), HWSPINLOCK_UNUSED);
 
-	/* sanity check (this shouldn't happen) */
-	WARN_ON(tmp != hwlock);
+		/* sanity check (this shouldn't happen) */
+		WARN_ON(tmp != hwlock);
+	}
 
 	module_put(dev->driver->owner);
 
diff --git a/drivers/hwspinlock/hwspinlock_internal.h b/drivers/hwspinlock/hwspinlock_internal.h
index 29892767bb7a..12f230b40693 100644
--- a/drivers/hwspinlock/hwspinlock_internal.h
+++ b/drivers/hwspinlock/hwspinlock_internal.h
@@ -35,11 +35,13 @@  struct hwspinlock_ops {
  * struct hwspinlock - this struct represents a single hwspinlock instance
  * @bank: the hwspinlock_device structure which owns this lock
  * @lock: initialized and used by hwspinlock core
+ * @refcnt: counter for the number of existing references to the hwspinlock
  * @priv: private data, owned by the underlying platform-specific hwspinlock drv
  */
 struct hwspinlock {
 	struct hwspinlock_device *bank;
 	spinlock_t lock;
+	unsigned int refcnt;
 	void *priv;
 };