Zephyr API Documentation 4.4.0-rc2
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 = {}, \
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
192
198typedef int (*hwspinlock_api_trylock)(const struct device *dev, uint32_t id);
199
205typedef void (*hwspinlock_api_lock)(const struct device *dev, uint32_t id);
206
212typedef void (*hwspinlock_api_unlock)(const struct device *dev, uint32_t id);
213
219typedef uint32_t (*hwspinlock_api_get_max_id)(const struct device *dev);
220
242
245
264static inline int hw_spin_trylock(const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id,
265 k_spinlock_key_t *key)
266{
267 const struct hwspinlock_driver_api *api = (const struct hwspinlock_driver_api *)dev->api;
268 int ret;
269
270 if (api->trylock == NULL) {
271 return -ENOSYS;
272 }
273
274 ret = k_spin_trylock(&ctx->lock, key);
275 if (ret) {
276 return ret;
277 }
278
279 ret = api->trylock(dev, id);
280 if (ret) {
281 /* HW trylock failed: release local lock before returning. */
282 k_spin_unlock(&ctx->lock, *key);
283 return ret;
284 }
285
286 return 0;
287}
288
313static inline k_spinlock_key_t hw_spin_lock(const struct device *dev, hwspinlock_ctx_t *ctx,
314 uint32_t id)
315{
316 const struct hwspinlock_driver_api *api = (const struct hwspinlock_driver_api *)dev->api;
318
319 __ASSERT(api->lock != NULL, "hwspinlock lock callback must be implemented");
320
321 k = k_spin_lock(&ctx->lock);
322 api->lock(dev, id);
323
324 return k;
325}
326
338static inline void hw_spin_unlock(const struct device *dev, hwspinlock_ctx_t *ctx, uint32_t id,
340{
341 const struct hwspinlock_driver_api *api = (const struct hwspinlock_driver_api *)dev->api;
342
343 __ASSERT(api->unlock != NULL, "hwspinlock unlock callback must be implemented");
344
345 api->unlock(dev, id);
346 k_spin_unlock(&ctx->lock, key);
347}
348
359static inline uint32_t hw_spinlock_get_max_id(const struct device *dev)
360{
361 const struct hwspinlock_driver_api *api = (const struct hwspinlock_driver_api *)dev->api;
362
363 __ASSERT(api->get_max_id != NULL, "hwspinlock get_max_id callback must be implemented");
364
365 return api->get_max_id(dev);
366}
367
379static inline int hw_spin_trylock_dt(struct hwspinlock_dt_spec *spec, k_spinlock_key_t *key)
380{
381 return hw_spin_trylock(spec->dev, &spec->ctx, spec->id, key);
382}
383
395{
396 return hw_spin_lock(spec->dev, &spec->ctx, spec->id);
397}
398
409static inline void hw_spin_unlock_dt(struct hwspinlock_dt_spec *spec, k_spinlock_key_t key)
410{
411 hw_spin_unlock(spec->dev, &spec->ctx, spec->id, key);
412}
413
425{
426 return hw_spinlock_get_max_id(spec->dev);
427}
428
429#ifdef __cplusplus
430}
431#endif
432
434
435#endif /* ZEPHYR_INCLUDE_DRIVERS_HWSPINLOCK_H_ */
Devicetree main header.
void(* hwspinlock_api_lock)(const struct device *dev, uint32_t id)
Callback API to lock an HW spinlock.
Definition hwspinlock.h:205
uint32_t(* hwspinlock_api_get_max_id)(const struct device *dev)
Callback API to get the maximum HW spinlock ID.
Definition hwspinlock.h:219
int(* hwspinlock_api_trylock)(const struct device *dev, uint32_t id)
Callback API to try to lock an HW spinlock.
Definition hwspinlock.h:198
void(* hwspinlock_api_unlock)(const struct device *dev, uint32_t id)
Callback API to unlock an HW spinlock.
Definition hwspinlock.h:212
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:424
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:409
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:264
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:394
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:338
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:313
static uint32_t hw_spinlock_get_max_id(const struct device *dev)
Get HW spinlock max ID.
Definition hwspinlock.h:359
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:379
static ALWAYS_INLINE int k_spin_trylock(struct k_spinlock *l, k_spinlock_key_t *k)
Attempt to lock a spinlock.
Definition spinlock.h:229
static ALWAYS_INLINE void k_spin_unlock(struct k_spinlock *l, k_spinlock_key_t key)
Unlock a spin lock.
Definition spinlock.h:299
static ALWAYS_INLINE k_spinlock_key_t k_spin_lock(struct k_spinlock *l)
Lock a spinlock.
Definition spinlock.h:181
struct z_spinlock_key k_spinlock_key_t
Spinlock key type.
Definition spinlock.h:126
#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:513
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:519
HW spinlock controller runtime context.
Definition hwspinlock.h:37
struct k_spinlock lock
Definition hwspinlock.h:45
<span class="mlabel">Driver Operations</span> Hardware Spinlock driver operations
Definition hwspinlock.h:224
hwspinlock_api_unlock unlock
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition hwspinlock.h:236
hwspinlock_api_lock lock
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition hwspinlock.h:232
hwspinlock_api_get_max_id get_max_id
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition hwspinlock.h:240
hwspinlock_api_trylock trylock
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition hwspinlock.h:228
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.