hwspinlock_dm.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-23 GuEe-GUI first version
  9. */
  10. #ifndef __HWSPINLOCK_DM_H__
  11. #define __HWSPINLOCK_DM_H__
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <drivers/misc.h>
  16. #include <ref.h>
  17. /**
  18. * @brief Hardware spinlock instance
  19. *
  20. * Represents a single hardware spinlock managed by a bank. Each lock
  21. * maintains a local software spinlock to protect internal state and a
  22. * usage flag to indicate whether it has been allocated to clients.
  23. */
  24. struct rt_hwspinlock
  25. {
  26. struct rt_hwspinlock_bank *bank;
  27. struct rt_spinlock lock; /**< Local software spinlock to guard structure access */
  28. rt_bool_t used; /**< Indicates whether this lock is currently in use */
  29. void *priv; /**< Driver-specific private data */
  30. };
  31. /**
  32. * @brief Hardware spinlock operations
  33. *
  34. * A hardware spinlock driver must provide these low-level operations.
  35. * trylock() performs a non-blocking acquire. relax() is called inside
  36. * busy-wait loops to reduce power usage or yield the bus.
  37. */
  38. struct rt_hwspinlock_ops
  39. {
  40. /**
  41. * @brief Attempt to acquire the hardware spinlock (non-blocking)
  42. *
  43. * Should immediately return RT_EOK on success or -RT_EBUSY if the lock
  44. * is already taken by another CPU or hardware requester.
  45. */
  46. rt_err_t (*trylock)(struct rt_hwspinlock *hwlock);
  47. /**
  48. * @brief Release the hardware spinlock
  49. */
  50. void (*unlock)(struct rt_hwspinlock *hwlock);
  51. /**
  52. * @brief Relax operation called during spinning
  53. *
  54. * Typically implemented using power-saving instructions (e.g., WFE),
  55. * yielding the CPU, or other hardware-supported wait mechanisms.
  56. */
  57. void (*relax)(struct rt_hwspinlock *hwlock);
  58. };
  59. /**
  60. * @brief Hardware spinlock bank
  61. *
  62. * Represents a group of hardware spinlocks exported by the same device
  63. * or hardware module. A bank is registered globally so that locks can be
  64. * retrieved by index, name, or device tree bindings.
  65. */
  66. struct rt_hwspinlock_bank
  67. {
  68. rt_list_t list;
  69. struct rt_ref ref;
  70. struct rt_device *dev;
  71. const struct rt_hwspinlock_ops *ops;
  72. int base_id; /**< Global base ID used to calculate lock identifiers */
  73. rt_size_t locks_nr; /**< Number of hardware spinlocks in this bank */
  74. /**
  75. * @brief Array of hardware spinlock instances
  76. *
  77. * Implemented as a flexible array member. Memory is allocated using
  78. * hwspinlock_bank_alloc(), ensuring space for all lock structures.
  79. */
  80. struct rt_hwspinlock locks[];
  81. };
  82. /**
  83. * @brief Allocate a hardware spinlock bank with a given number of locks
  84. *
  85. * The allocation includes both the bank structure and the flexible array
  86. * of hardware spinlock objects.
  87. *
  88. * @param obj The object pointer used to store the bank
  89. * @param locks_nr Number of hardware spinlocks to allocate
  90. *
  91. * @return Pointer to the allocated memory or NULL on failure
  92. */
  93. #define hwspinlock_bank_alloc(obj, locks_nr) \
  94. rt_calloc(1, sizeof(typeof(*obj)) + sizeof(struct rt_hwspinlock) * (locks_nr))
  95. /**
  96. * @brief Compute the global hardware spinlock ID
  97. *
  98. * The global ID is calculated as:
  99. * bank->base_id + index
  100. * where the index is derived from the hardware spinlock's position inside
  101. * the bank->locks array.
  102. *
  103. * @param hwlock Pointer to a hardware spinlock
  104. *
  105. * @return The global lock identifier
  106. */
  107. rt_inline int hwspinlock_find_id(struct rt_hwspinlock *hwlock)
  108. {
  109. return hwlock->bank->base_id + (hwlock - &hwlock->bank->locks[0]);
  110. }
  111. #endif /* __HWSPINLOCK_DM_H__ */