Zephyr API Documentation 4.2.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
Hardware Spinlock

Interfaces for hardware spinlocks. More...

Files

file  hwspinlock.h
 Main header file for hardware spinlock driver API.

Data Structures

struct  hwspinlock_context
 HW spinlock controller runtime context. More...
struct  hwspinlock_dt_spec
 Complete hardware spinlock DT information. More...

Macros

#define HWSPINLOCK_CTX_INITIALIZER
 Initializer for a hwspinlock_ctx_t.
#define HWSPINLOCK_DT_SPEC_GET_BY_IDX(node_id, idx)
 Structure initializer for struct hwspinlock_dt_spec from devicetree by index.
#define HWSPINLOCK_DT_SPEC_GET_BY_NAME(node_id, name)
 Structure initializer for struct hwspinlock_dt_spec from devicetree by name.
#define HWSPINLOCK_DT_SPEC_GET(node_id)
 Structure initializer for struct hwspinlock_dt_spec from devicetree.
#define HWSPINLOCK_DT_SPEC_INST_GET_BY_IDX(inst, idx)
 Instance version of HWSPINLOCK_DT_SPEC_GET_BY_IDX()
#define HWSPINLOCK_DT_SPEC_INST_GET_BY_NAME(inst, name)
 Instance version of HWSPINLOCK_DT_SPEC_GET_BY_NAME()
#define HWSPINLOCK_DT_SPEC_INST_GET(inst)
 Instance version of HWSPINLOCK_DT_SPEC_GET()

Typedefs

typedef struct hwspinlock_context hwspinlock_ctx_t
 Opaque type to represent a hwspinlock runtime context.

Functions

static int hw_spin_trylock (const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id, k_spinlock_key_t *key)
 Try to lock HW spinlock.
static k_spinlock_key_t hw_spin_lock (const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id)
 Lock HW spinlock.
static void hw_spin_unlock (const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id, k_spinlock_key_t key)
 Unlock HW spinlock.
static uint32_t hw_spinlock_get_max_id (const struct device *dev)
 Get HW spinlock max ID.
static int hw_spin_trylock_dt (struct hwspinlock_dt_spec *spec, k_spinlock_key_t *key)
 Try to lock HW spinlock from a struct hwspinlock_dt_spec.
static k_spinlock_key_t hw_spin_lock_dt (struct hwspinlock_dt_spec *spec)
 Lock HW spinlock from a struct hwspinlock_dt_spec.
static void hw_spin_unlock_dt (struct hwspinlock_dt_spec *spec, k_spinlock_key_t key)
 Unlock HW spinlock from a struct hwspinlock_dt_spec.
static uint32_t hw_spinlock_get_max_id_dt (struct hwspinlock_dt_spec *spec)
 Get HW spinlock max ID from a struct hwspinlock_dt_spec.

Detailed Description

Interfaces for hardware spinlocks.

Macro Definition Documentation

◆ HWSPINLOCK_CTX_INITIALIZER

#define HWSPINLOCK_CTX_INITIALIZER

#include <zephyr/drivers/hwspinlock.h>

Value:
{ \
.lock = {0}, \
}

Initializer for a hwspinlock_ctx_t.

Note
We must declare each field individually because a struct k_spinlock might have no field depending on Kconfig options, and gcc requires struct without any field to be initialized explicitly, instead of just being able to do {0}.

◆ HWSPINLOCK_DT_SPEC_GET

#define HWSPINLOCK_DT_SPEC_GET ( node_id)

#include <zephyr/drivers/hwspinlock.h>

Value:
#define HWSPINLOCK_DT_SPEC_GET_BY_IDX(node_id, idx)
Structure initializer for struct hwspinlock_dt_spec from devicetree by index.
Definition hwspinlock.h:105

Structure initializer for struct hwspinlock_dt_spec from devicetree.

This is equivalent to HWSPINLOCK_DT_SPEC_GET_BY_IDX(node_id, 0)

Parameters
node_idDevicetree node identifier for the HWSPINLOCK device
See also
HWSPINLOCK_DT_SPEC_GET_BY_IDX()

◆ HWSPINLOCK_DT_SPEC_GET_BY_IDX

#define HWSPINLOCK_DT_SPEC_GET_BY_IDX ( node_id,
idx )

#include <zephyr/drivers/hwspinlock.h>

Value:
{ \
.dev = DEVICE_DT_GET(DT_HWSPINLOCK_CTRL_BY_IDX(node_id, idx)), \
.id = DT_HWSPINLOCK_ID_BY_IDX(node_id, idx), \
}
#define DEVICE_DT_GET(node_id)
Get a device reference from a devicetree node identifier.
Definition device.h:314
#define DT_HWSPINLOCK_ID_BY_IDX(node_id, idx)
Get a hardware spinlock id by index.
Definition hwspinlock.h:149
#define DT_HWSPINLOCK_CTRL_BY_IDX(node_id, idx)
Get the node identifier for the hardware spinlock controller from a hwlocks property by id.
Definition hwspinlock.h:51
#define HWSPINLOCK_CTX_INITIALIZER
Initializer for a hwspinlock_ctx_t.
Definition hwspinlock.h:74

Structure initializer for struct hwspinlock_dt_spec from devicetree by index.

This helper macro expands to a static initializer for a struct hwspinlock_dt_spec by reading the relevant device controller and id number from the devicetree.

Example devicetree fragment:

n: node {
hwlocks = <&hwlock 8>,
<&hwlock 9>;
};

Example usage:

#define DT_NODELABEL(label)
Get a node identifier for a node label.
Definition devicetree.h:196
Complete hardware spinlock DT information.
Definition hwspinlock.h:58
Parameters
node_idDevicetree node identifier for the HWSPINLOCK device
idxIndex of hwlocks element
Returns
static initializer for a struct hwspinlock_dt_spec

◆ HWSPINLOCK_DT_SPEC_GET_BY_NAME

#define HWSPINLOCK_DT_SPEC_GET_BY_NAME ( node_id,
name )

#include <zephyr/drivers/hwspinlock.h>

Value:
{ \
.dev = DEVICE_DT_GET(DT_HWSPINLOCK_CTRL_BY_NAME(node_id, name)), \
.id = DT_HWSPINLOCK_ID_BY_NAME(node_id, name), \
}
#define DT_HWSPINLOCK_ID_BY_NAME(node_id, name)
Get a hardware spinlock id by name.
Definition hwspinlock.h:119
#define DT_HWSPINLOCK_CTRL_BY_NAME(node_id, name)
Get the node identifier for the hardware spinlock controller from a hwlocks property by name.
Definition hwspinlock.h:82

Structure initializer for struct hwspinlock_dt_spec from devicetree by name.

This helper macro expands to a static initializer for a struct hwspinlock_dt_spec by reading the relevant device controller and id number from the devicetree.

Example devicetree fragment:

n: node {
hwlocks = <&hwlock 8>,
<&hwlock 9>;
hwlock-names = "rd", "wr";
};

Example usage:

#define HWSPINLOCK_DT_SPEC_GET_BY_NAME(node_id, name)
Structure initializer for struct hwspinlock_dt_spec from devicetree by name.
Definition hwspinlock.h:139
Parameters
node_idDevicetree node identifier for the HWSPINLOCK device
namelowercase-and-underscores name of the hwlocks element
Returns
static initializer for a struct hwspinlock_dt_spec

◆ HWSPINLOCK_DT_SPEC_INST_GET

#define HWSPINLOCK_DT_SPEC_INST_GET ( inst)

#include <zephyr/drivers/hwspinlock.h>

Value:
#define DT_DRV_INST(inst)
Node identifier for an instance of a DT_DRV_COMPAT compatible.
Definition devicetree.h:4037
#define HWSPINLOCK_DT_SPEC_GET(node_id)
Structure initializer for struct hwspinlock_dt_spec from devicetree.
Definition hwspinlock.h:154

Instance version of HWSPINLOCK_DT_SPEC_GET()

Parameters
instDT_DRV_COMPAT instance number
See also
HWSPINLOCK_DT_SPEC_GET()

◆ HWSPINLOCK_DT_SPEC_INST_GET_BY_IDX

#define HWSPINLOCK_DT_SPEC_INST_GET_BY_IDX ( inst,
idx )

#include <zephyr/drivers/hwspinlock.h>

Value:

Instance version of HWSPINLOCK_DT_SPEC_GET_BY_IDX()

Parameters
instDT_DRV_COMPAT instance number
idxIndex of the hwlocks element
See also
HWSPINLOCK_DT_SPEC_GET_BY_IDX()

◆ HWSPINLOCK_DT_SPEC_INST_GET_BY_NAME

#define HWSPINLOCK_DT_SPEC_INST_GET_BY_NAME ( inst,
name )

#include <zephyr/drivers/hwspinlock.h>

Value:

Instance version of HWSPINLOCK_DT_SPEC_GET_BY_NAME()

Parameters
instDT_DRV_COMPAT instance number
namelowercase-and-underscores name of the hwlocks element
See also
HWSPINLOCK_DT_SPEC_GET_BY_NAME()

Typedef Documentation

◆ hwspinlock_ctx_t

#include <zephyr/drivers/hwspinlock.h>

Opaque type to represent a hwspinlock runtime context.

This type is not meant to be inspected by application code.

Function Documentation

◆ hw_spin_lock()

k_spinlock_key_t hw_spin_lock ( const struct device * dev,
hwspinlock_ctx_t * ctx,
uint32_t id )
inlinestatic

#include <zephyr/drivers/hwspinlock.h>

Lock HW spinlock.

This function is used when locking an HW spinlock. If the spinlock is already locked by another party, waits for it to be released. This is useful when protecting a resource which is shared between multiple clusters.

Note
The goal is not to replace regular spinlocks, but to protect shared resources between clusters. However, because we don't want another thread to take the same HW spinlock twice, the locking mechanism acts as a regular spinlock as well.

Because this uses a regular zephyr spinlock in conjunction with a hwspinlock, the same rules applies. Separate hwspinlock may be nested. It is legal to lock another (unlocked) hwspinlock while holding a lock. However, an attempt to acquire a hwspinlock that the CPU already holds will deadlock.

See also
k_spin_lock
Parameters
devHW spinlock device instance.
ctxHW spinlock runtime context.
idSpinlock identifier.
Returns
A key value that must be passed to hw_spin_unlock() when the lock is released.

◆ hw_spin_lock_dt()

k_spinlock_key_t hw_spin_lock_dt ( struct hwspinlock_dt_spec * spec)
inlinestatic

#include <zephyr/drivers/hwspinlock.h>

Lock HW spinlock from a struct hwspinlock_dt_spec.

This is the dt_spec equivalent of hw_spin_lock()

See also
hw_spin_lock
Parameters
specHWSPINLOCK specification from devicetree
Returns
See return values from hw_spin_lock()

◆ hw_spin_trylock()

int hw_spin_trylock ( const struct device * dev,
hwspinlock_ctx_t * ctx,
uint32_t id,
k_spinlock_key_t * key )
inlinestatic

#include <zephyr/drivers/hwspinlock.h>

Try to lock HW spinlock.

This function is used when trying to lock an HW spinlock. If the spinlock is already locked by another cluster, exits with -EBUSY.

See also
hw_spin_lock
Parameters
devHW spinlock device instance.
ctxHW spinlock runtime context.
idSpinlock identifier.
[out]keyA pointer to the spinlock key.
Return values
0On success.
-ENOSYSIf the operation is not implemented.
-EINVALIf HW spinlock id is invalid.
-EBUSYIf HW spinlock is already locked by someone else.

◆ hw_spin_trylock_dt()

int hw_spin_trylock_dt ( struct hwspinlock_dt_spec * spec,
k_spinlock_key_t * key )
inlinestatic

#include <zephyr/drivers/hwspinlock.h>

Try to lock HW spinlock from a struct hwspinlock_dt_spec.

This is the dt_spec equivalent of hw_spin_trylock()

See also
hw_spin_trylock
Parameters
specHWSPINLOCK specification from devicetree
[out]keyA pointer to the spinlock key.
Returns
See return values from hw_spin_trylock()

◆ hw_spin_unlock()

void hw_spin_unlock ( const struct device * dev,
hwspinlock_ctx_t * ctx,
uint32_t id,
k_spinlock_key_t key )
inlinestatic

#include <zephyr/drivers/hwspinlock.h>

Unlock HW spinlock.

This function to unlock an HW spinlock

Parameters
devHW spinlock device instance.
ctxHW spinlock runtime context.
idSpinlock identifier.
keyThe value returned from hw_spin_lock() when this lock was acquired

◆ hw_spin_unlock_dt()

void hw_spin_unlock_dt ( struct hwspinlock_dt_spec * spec,
k_spinlock_key_t key )
inlinestatic

#include <zephyr/drivers/hwspinlock.h>

Unlock HW spinlock from a struct hwspinlock_dt_spec.

This is the dt_spec equivalent of hw_spin_unlock()

See also
hw_spin_unlock
Parameters
specHWSPINLOCK specification from devicetree
keyThe value returned from hw_spin_lock() when this lock was acquired

◆ hw_spinlock_get_max_id()

uint32_t hw_spinlock_get_max_id ( const struct device * dev)
inlinestatic

#include <zephyr/drivers/hwspinlock.h>

Get HW spinlock max ID.

This function is used to get the HW spinlock maximum ID. It should be called before attempting to lock/unlock a specific HW spinlock.

Parameters
devHW spinlock device instance.
Return values
HWspinlock max ID.

◆ hw_spinlock_get_max_id_dt()

uint32_t hw_spinlock_get_max_id_dt ( struct hwspinlock_dt_spec * spec)
inlinestatic

#include <zephyr/drivers/hwspinlock.h>

Get HW spinlock max ID from a struct hwspinlock_dt_spec.

This is the dt_spec equivalent of hw_spinlock_get_max_id()

See also
hw_spinlock_get_max_id
Parameters
specHWSPINLOCK specification from devicetree
Returns
See return values from hw_spinlock_get_max_id()