[dm][hwspinlock] support hwspinlock

Hardware spinlock modules provide hardware assistance for
synchronization and mutual exclusion between heterogeneous processors
and those not operating under a single, shared operating system.

Signed-off-by: GuEe-GUI <2991707448@qq.com>
This commit is contained in:
GuEe-GUI authored and R b b666 committed 2025-12-11 11:08:33 +08:00
1 parent d76356b94e
commit dd20176cba
7 files changed
+659

No files matched your search

+1
View File
@@ -24,6 +24,7 @@ rsource "wlan/Kconfig"
rsource "led/Kconfig"
rsource "input/Kconfig"
rsource "mailbox/Kconfig"
rsource "hwspinlock/Kconfig"
rsource "phye/Kconfig"
rsource "ata/Kconfig"
rsource "nvme/Kconfig"
+15
View File
@@ -0,0 +1,15 @@
menuconfig RT_USING_HWSPINLOCK
bool "Using Hardware Spinlock device drivers"
depends on RT_USING_DM
depends on RT_USING_OFW
select RT_USING_ADT
select RT_USING_ADT_REF
default n
help
Hardware spinlock modules provide hardware assistance for
synchronization and mutual exclusion between heterogeneous processors
and those not operating under a single, shared operating system.
if RT_USING_HWSPINLOCK
osource "$(SOC_DM_HWSPINLOCK_DIR)/Kconfig"
endif
+15
View File
@@ -0,0 +1,15 @@
from building import *
group = []
if not GetDepend(['RT_USING_HWSPINLOCK']):
Return('group')
cwd = GetCurrentDir()
CPPPATH = [cwd + '/../include']
src = ['hwspinlock.c']
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-09-23 GuEe-GUI first version
*/
#ifndef __HWSPINLOCK_DM_H__
#define __HWSPINLOCK_DM_H__
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/misc.h>
#include <ref.h>
/**
* @brief Hardware spinlock instance
*
* Represents a single hardware spinlock managed by a bank. Each lock
* maintains a local software spinlock to protect internal state and a
* usage flag to indicate whether it has been allocated to clients.
*/
struct rt_hwspinlock
{
struct rt_hwspinlock_bank *bank;
struct rt_spinlock lock; /**< Local software spinlock to guard structure access */
rt_bool_t used; /**< Indicates whether this lock is currently in use */
void *priv; /**< Driver-specific private data */
};
/**
* @brief Hardware spinlock operations
*
* A hardware spinlock driver must provide these low-level operations.
* trylock() performs a non-blocking acquire. relax() is called inside
* busy-wait loops to reduce power usage or yield the bus.
*/
struct rt_hwspinlock_ops
{
/**
* @brief Attempt to acquire the hardware spinlock (non-blocking)
*
* Should immediately return RT_EOK on success or -RT_EBUSY if the lock
* is already taken by another CPU or hardware requester.
*/
rt_err_t (*trylock)(struct rt_hwspinlock *hwlock);
/**
* @brief Release the hardware spinlock
*/
void (*unlock)(struct rt_hwspinlock *hwlock);
/**
* @brief Relax operation called during spinning
*
* Typically implemented using power-saving instructions (e.g., WFE),
* yielding the CPU, or other hardware-supported wait mechanisms.
*/
void (*relax)(struct rt_hwspinlock *hwlock);
};
/**
* @brief Hardware spinlock bank
*
* Represents a group of hardware spinlocks exported by the same device
* or hardware module. A bank is registered globally so that locks can be
* retrieved by index, name, or device tree bindings.
*/
struct rt_hwspinlock_bank
{
rt_list_t list;
struct rt_ref ref;
struct rt_device *dev;
const struct rt_hwspinlock_ops *ops;
int base_id; /**< Global base ID used to calculate lock identifiers */
rt_size_t locks_nr; /**< Number of hardware spinlocks in this bank */
/**
* @brief Array of hardware spinlock instances
*
* Implemented as a flexible array member. Memory is allocated using
* hwspinlock_bank_alloc(), ensuring space for all lock structures.
*/
struct rt_hwspinlock locks[];
};
/**
* @brief Allocate a hardware spinlock bank with a given number of locks
*
* The allocation includes both the bank structure and the flexible array
* of hardware spinlock objects.
*
* @param obj The object pointer used to store the bank
* @param locks_nr Number of hardware spinlocks to allocate
*
* @return Pointer to the allocated memory or NULL on failure
*/
#define hwspinlock_bank_alloc(obj, locks_nr) \
rt_calloc(1, sizeof(typeof(*obj)) + sizeof(struct rt_hwspinlock) * (locks_nr))
/**
* @brief Compute the global hardware spinlock ID
*
* The global ID is calculated as:
* bank->base_id + index
* where the index is derived from the hardware spinlock's position inside
* the bank->locks array.
*
* @param hwlock Pointer to a hardware spinlock
*
* @return The global lock identifier
*/
rt_inline int hwspinlock_find_id(struct rt_hwspinlock *hwlock)
{
return hwlock->bank->base_id + (hwlock - &hwlock->bank->locks[0]);
}
#endif /* __HWSPINLOCK_DM_H__ */
@@ -0,0 +1,180 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-09-23 GuEe-GUI first version
*/
#ifndef __HWSPINLOCK_H__
#define __HWSPINLOCK_H__
#include <rtdef.h>
struct rt_hwspinlock;
struct rt_hwspinlock_ops;
struct rt_hwspinlock_bank;
/**
* @brief Register a hardware spinlock bank
*
* A bank represents a contiguous set of hardware spinlocks provided by
* a hardware module. This API installs the bank into the system-wide list
* so that each individual lock can be retrieved and used by clients.
*
* @param bank Pointer to the hardware spinlock bank
* @return RT_EOK on success, otherwise an error code
*/
rt_err_t rt_hwspinlock_bank_register(struct rt_hwspinlock_bank *bank);
/**
* @brief Unregister a hardware spinlock bank
*
* Removes the bank from the global registry. All locks under the bank
* become unavailable. Caller must ensure no active users remain.
*
* @param bank Pointer to the hardware spinlock bank
* @return RT_EOK on success, otherwise an error code
*/
rt_err_t rt_hwspinlock_bank_unregister(struct rt_hwspinlock_bank *bank);
/**
* @brief Try to acquire a hardware spinlock (raw interface)
*
* The raw interface allows hardware drivers to retrieve the IRQ level
* before acquiring the lock, enabling IRQ-safe usage scenarios.
*
* @param hwlock Pointer to the hardware spinlock
* @param out_irq_level If non-NULL, returns saved IRQ level
*
* @return RT_EOK if lock is acquired, -RT_EBUSY if lock is held by others
*/
rt_err_t rt_hwspin_trylock_raw(struct rt_hwspinlock *hwlock,
rt_ubase_t *out_irq_level);
/**
* @brief Acquire a hardware spinlock with timeout (raw interface)
*
* Spins or relaxes according to the hardware implementation until the
* lock becomes available or timeout expires. IRQ level handling is optional.
*
* @param hwlock Pointer to the hardware spinlock
* @param timeout_ms Timeout in milliseconds
* @param out_irq_level If non-NULL, returns saved IRQ level
*
* @return RT_EOK on success, -RT_ETIMEOUT if timed out
*/
rt_err_t rt_hwspin_lock_timeout_raw(struct rt_hwspinlock *hwlock,
rt_uint32_t timeout_ms, rt_ubase_t *out_irq_level);
/**
* @brief Release a hardware spinlock (raw interface)
*
* Restores IRQ state if @p out_irq_level is provided.
*
* @param hwlock Pointer to the hardware spinlock
* @param out_irq_level IRQ level saved during lock operation
*/
void rt_hwspin_unlock_raw(struct rt_hwspinlock *hwlock,
rt_ubase_t *out_irq_level);
/**
* @brief Try to acquire a hardware spinlock (no IRQ management)
*/
rt_inline rt_err_t rt_hwspin_trylock(struct rt_hwspinlock *hwlock)
{
return rt_hwspin_trylock_raw(hwlock, RT_NULL);
}
/**
* @brief Try to acquire a hardware spinlock with IRQ saving
*/
rt_inline rt_err_t rt_hwspin_trylock_irqsave(struct rt_hwspinlock *hwlock,
rt_ubase_t *out_irq_level)
{
return rt_hwspin_trylock_raw(hwlock, out_irq_level);
}
/**
* @brief Acquire a hardware spinlock with timeout (no IRQ management)
*/
rt_inline rt_err_t rt_hwspin_lock_timeout(struct rt_hwspinlock *hwlock,
rt_uint32_t timeout_ms)
{
return rt_hwspin_lock_timeout_raw(hwlock, timeout_ms, RT_NULL);
}
/**
* @brief Acquire a hardware spinlock with timeout and IRQ saving
*/
rt_inline rt_err_t rt_hwspin_lock_timeout_irqsave(struct rt_hwspinlock *hwlock,
rt_uint32_t timeout_ms, rt_ubase_t *out_level)
{
return rt_hwspin_lock_timeout_raw(hwlock, timeout_ms, out_level);
}
/**
* @brief Release a hardware spinlock (no IRQ management)
*/
rt_inline void rt_hwspin_unlock(struct rt_hwspinlock *hwlock)
{
rt_hwspin_unlock_raw(hwlock, RT_NULL);
}
/**
* @brief Release a hardware spinlock and restore IRQ level
*/
rt_inline void rt_hwspin_unlock_irqsave(struct rt_hwspinlock *hwlock,
rt_ubase_t *out_level)
{
rt_hwspin_unlock_raw(hwlock, out_level);
}
/**
* @brief Acquire a free hardware spinlock from any registered bank
*
* The returned lock must later be released via rt_hwspinlock_put().
*
* @return Pointer to a hardware spinlock, or NULL if none available
*/
struct rt_hwspinlock *rt_hwspinlock_get(void);
/**
* @brief Get hardware spinlock by index from a specific device
*
* @param dev Device providing hardware spinlocks
* @param index Hardware spinlock index within device/bank
*
* @return Pointer to the hardware spinlock, or NULL if invalid index
*/
struct rt_hwspinlock *rt_hwspinlock_get_by_index(struct rt_device *dev, int index);
/**
* @brief Get hardware spinlock by name from a specific device
*
* Names are assigned by device drivers and mapped to lock indices.
*
* @return Pointer to the hardware spinlock, or NULL if name not found
*/
struct rt_hwspinlock *rt_hwspinlock_get_by_name(struct rt_device *dev, const char *name);
/**
* @brief Release hardware spinlock reference obtained from get()
*
* Does not unlock the spinlock; only decreases usage count.
*/
void rt_hwspinlock_put(struct rt_hwspinlock *hwlock);
/**
* @brief Get hardware spinlock by index from device tree node
*/
struct rt_hwspinlock *rt_ofw_get_hwspinlock_by_index(struct rt_ofw_node *np, int index);
/**
* @brief Get hardware spinlock by name from device tree node
*/
struct rt_hwspinlock *rt_ofw_get_hwspinlock_by_name(struct rt_ofw_node *np, const char *name);
#endif /* __HWSPINLOCK_H__ */
+4
View File
@@ -66,6 +66,10 @@ extern "C" {
#include "drivers/mailbox.h"
#endif /* RT_USING_MBOX */
#ifdef RT_USING_HWSPINLOCK
#include "drivers/hwspinlock.h"
#endif /* RT_USING_HWSPINLOCK */
#ifdef RT_USING_BLK
#include "drivers/blk.h"
#endif /* RT_USING_BLK */