Zephyr API Documentation 4.2.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
hwspinlock.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Sequans Communications
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_HWSPINLOCK_H_
14#define ZEPHYR_INCLUDE_DRIVERS_HWSPINLOCK_H_
15
22
23#include <zephyr/types.h>
24#include <zephyr/sys/util.h>
25#include <zephyr/sys/__assert.h>
26#include <zephyr/device.h>
27#include <zephyr/spinlock.h>
28#include <zephyr/devicetree.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
47
54
66
74#define HWSPINLOCK_CTX_INITIALIZER \
75 { \
76 .lock = {0}, \
77 }
78
105#define HWSPINLOCK_DT_SPEC_GET_BY_IDX(node_id, idx) \
106 { \
107 .dev = DEVICE_DT_GET(DT_HWSPINLOCK_CTRL_BY_IDX(node_id, idx)), \
108 .id = DT_HWSPINLOCK_ID_BY_IDX(node_id, idx), \
109 .ctx = HWSPINLOCK_CTX_INITIALIZER, \
110 }
111
139#define HWSPINLOCK_DT_SPEC_GET_BY_NAME(node_id, name) \
140 { \
141 .dev = DEVICE_DT_GET(DT_HWSPINLOCK_CTRL_BY_NAME(node_id, name)), \
142 .id = DT_HWSPINLOCK_ID_BY_NAME(node_id, name), \
143 .ctx = HWSPINLOCK_CTX_INITIALIZER, \
144 }
145
154#define HWSPINLOCK_DT_SPEC_GET(node_id) \
155 HWSPINLOCK_DT_SPEC_GET_BY_IDX(node_id, 0)
156
165#define HWSPINLOCK_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
166 HWSPINLOCK_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
167
176#define HWSPINLOCK_DT_SPEC_INST_GET_BY_NAME(inst, name) \
177 HWSPINLOCK_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
178
185#define HWSPINLOCK_DT_SPEC_INST_GET(inst) \
186 HWSPINLOCK_DT_SPEC_GET(DT_DRV_INST(inst))
187
189
197typedef int (*hwspinlock_api_trylock)(const struct device *dev, uint32_t id);
198
206typedef void (*hwspinlock_api_lock)(const struct device *dev, uint32_t id);
207
215typedef void (*hwspinlock_api_unlock)(const struct device *dev, uint32_t id);
216
224typedef uint32_t (*hwspinlock_api_get_max_id)(const struct device *dev);
225
226__subsystem struct hwspinlock_driver_api {
227 hwspinlock_api_trylock trylock;
228 hwspinlock_api_lock lock;
229 hwspinlock_api_unlock unlock;
230 hwspinlock_api_get_max_id get_max_id;
231};
232
236
255static inline int hw_spin_trylock(const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id,
256 k_spinlock_key_t *key)
257{
258 const struct hwspinlock_driver_api *api = (const struct hwspinlock_driver_api *)dev->api;
259 int ret;
260
261 if (api->trylock == NULL) {
262 return -ENOSYS;
263 }
264
265 ret = k_spin_trylock(&ctx->lock, key);
266 if (ret) {
267 return ret;
268 }
269 return api->trylock(dev, id);
270}
271
296static inline k_spinlock_key_t hw_spin_lock(const struct device *dev, hwspinlock_ctx_t *ctx,
297 uint32_t id)
298{
299 const struct hwspinlock_driver_api *api = (const struct hwspinlock_driver_api *)dev->api;
301
302 __ASSERT(api->lock != NULL, "hwspinlock lock callback must be implemented");
303
304 k = k_spin_lock(&ctx->lock);
305 api->lock(dev, id);
306
307 return k;
308}
309
321static inline void hw_spin_unlock(const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id,
323{
324 const struct hwspinlock_driver_api *api = (const struct hwspinlock_driver_api *)dev->api;
325
326 __ASSERT(api->unlock != NULL, "hwspinlock unlock callback must be implemented");
327
328 api->unlock(dev, id);
329 k_spin_unlock(&ctx->lock, key);
330}
331
342static inline uint32_t hw_spinlock_get_max_id(const struct device *dev)
343{
344 const struct hwspinlock_driver_api *api = (const struct hwspinlock_driver_api *)dev->api;
345
346 __ASSERT(api->get_max_id != NULL, "hwspinlock get_max_id callback must be implemented");
347
348 return api->get_max_id(dev);
349}
350
362static inline int hw_spin_trylock_dt(struct hwspinlock_dt_spec *spec, k_spinlock_key_t *key)
363{
364 return hw_spin_trylock(spec->dev, &spec->ctx, spec->id, key);
365}
366
378{
379 return hw_spin_lock(spec->dev, &spec->ctx, spec->id);
380}
381
392static inline void hw_spin_unlock_dt(struct hwspinlock_dt_spec *spec, k_spinlock_key_t key)
393{
394 hw_spin_unlock(spec->dev, &spec->ctx, spec->id, key);
395}
396
408{
409 return hw_spinlock_get_max_id(spec->dev);
410}
411
412#ifdef __cplusplus
413}
414#endif
415
417
418#endif /* ZEPHYR_INCLUDE_DRIVERS_HWSPINLOCK_H_ */
Devicetree main header.
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.
Definition hwspinlock.h:407
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.
Definition hwspinlock.h:392
struct hwspinlock_context hwspinlock_ctx_t
Opaque type to represent a hwspinlock runtime context.
Definition hwspinlock.h:53
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.
Definition hwspinlock.h:255
static k_spinlock_key_t hw_spin_lock_dt(struct hwspinlock_dt_spec *spec)
Lock HW spinlock from a struct hwspinlock_dt_spec.
Definition hwspinlock.h:377
static void hw_spin_unlock(const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id, k_spinlock_key_t key)
Unlock HW spinlock.
Definition hwspinlock.h:321
static k_spinlock_key_t hw_spin_lock(const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id)
Lock HW spinlock.
Definition hwspinlock.h:296
static uint32_t hw_spinlock_get_max_id(const struct device *dev)
Get HW spinlock max ID.
Definition hwspinlock.h:342
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.
Definition hwspinlock.h:362
static ALWAYS_INLINE int k_spin_trylock(struct k_spinlock *l, k_spinlock_key_t *k)
Attempt to lock a spinlock.
Definition spinlock.h:233
static ALWAYS_INLINE void k_spin_unlock(struct k_spinlock *l, k_spinlock_key_t key)
Unlock a spin lock.
Definition spinlock.h:303
static ALWAYS_INLINE k_spinlock_key_t k_spin_lock(struct k_spinlock *l)
Lock a spinlock.
Definition spinlock.h:185
struct z_spinlock_key k_spinlock_key_t
Spinlock key type.
Definition spinlock.h:130
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
Public interface for spinlocks.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
HW spinlock controller runtime context.
Definition hwspinlock.h:37
struct k_spinlock lock
Definition hwspinlock.h:45
Complete hardware spinlock DT information.
Definition hwspinlock.h:58
hwspinlock_ctx_t ctx
Runtime context.
Definition hwspinlock.h:64
const struct device * dev
HW spinlock device.
Definition hwspinlock.h:60
uint32_t id
HW spinlock id.
Definition hwspinlock.h:62
Kernel Spin Lock.
Definition spinlock.h:45
Misc utilities.