cache.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file common/ARMCMx/cache.h
  15. * @brief Cortex-Mx cache support macros and structures.
  16. *
  17. * @addtogroup COMMON_ARMCMx_CACHE
  18. * @{
  19. */
  20. #ifndef CACHE_H
  21. #define CACHE_H
  22. /*===========================================================================*/
  23. /* Driver constants. */
  24. /*===========================================================================*/
  25. #if defined(__DCACHE_PRESENT) || defined(__DOXYGEN__)
  26. /**
  27. * @brief Data cache line size, zero if there is no data cache.
  28. */
  29. #define CACHE_LINE_SIZE 32U
  30. #else
  31. #define CACHE_LINE_SIZE 0U
  32. #endif
  33. /*===========================================================================*/
  34. /* Driver pre-compile time settings. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Derived constants and error checks. */
  38. /*===========================================================================*/
  39. /*===========================================================================*/
  40. /* Driver data structures and types. */
  41. /*===========================================================================*/
  42. /*===========================================================================*/
  43. /* Driver macros. */
  44. /*===========================================================================*/
  45. #if defined(__DCACHE_PRESENT) || defined(__DOXYGEN__)
  46. #if (__DCACHE_PRESENT != 0) || defined(__DOXYGEN__)
  47. /**
  48. * @brief Aligns the specified size to a multiple of cache line size.
  49. * @note This macros assumes that the size of the type @p t is a power of
  50. * two and not greater than @p CACHE_LINE_SIZE.
  51. *
  52. * @param[in] t type of the buffer element
  53. * @param[in] n number of buffer elements
  54. */
  55. #define CACHE_SIZE_ALIGN(t, n) \
  56. ((((((n) * sizeof (t)) - 1U) | (CACHE_LINE_SIZE - 1U)) + 1U) / sizeof (t))
  57. /**
  58. * @brief Invalidates the data cache lines overlapping a memory buffer.
  59. * @details This function is meant to make sure that data written in
  60. * data cache is invalidated.
  61. * @note On devices without data cache this function does nothing.
  62. * @note The function does not consider the lower 5 bits of addresses,
  63. * the buffers are meant to be aligned to a 32 bytes boundary or
  64. * adjacent data can be invalidated as side effect.
  65. *
  66. * @param[in] saddr start address of the DMA buffer
  67. * @param[in] n size of the DMA buffer in bytes
  68. *
  69. * @api
  70. */
  71. #define cacheBufferInvalidate(saddr, n) { \
  72. uint8_t *start = (uint8_t *)(saddr); \
  73. uint8_t *end = start + (size_t)(n); \
  74. __DSB(); \
  75. while (start < end) { \
  76. SCB->DCIMVAC = (uint32_t)start; \
  77. start += CACHE_LINE_SIZE; \
  78. } \
  79. __DSB(); \
  80. __ISB(); \
  81. }
  82. /**
  83. * @brief Flushes the data cache lines overlapping a DMA buffer.
  84. * @details This function is meant to make sure that data written in
  85. * data cache is flushed to RAM.
  86. * @note On devices without data cache this function does nothing.
  87. * @note The function does not consider the lower 5 bits of addresses,
  88. * the buffers are meant to be aligned to a 32 bytes boundary or
  89. * adjacent data can be flushed as side effect.
  90. *
  91. * @param[in] saddr start address of the DMA buffer
  92. * @param[in] n size of the DMA buffer in bytes
  93. *
  94. * @api
  95. */
  96. #define cacheBufferFlush(saddr, n) { \
  97. uint8_t *start = (uint8_t *)(saddr); \
  98. uint8_t *end = start + (size_t)(n); \
  99. __DSB(); \
  100. while (start < end) { \
  101. SCB->DCCIMVAC = (uint32_t)start; \
  102. start += CACHE_LINE_SIZE; \
  103. } \
  104. __DSB(); \
  105. __ISB(); \
  106. }
  107. #else /* __DCACHE_PRESENT == 0 */
  108. #define cacheBufferInvalidate(addr, size) { \
  109. (void)(addr); \
  110. (void)(size); \
  111. }
  112. #define cacheBufferFlush(addr, size) { \
  113. (void)(addr); \
  114. (void)(size); \
  115. }
  116. #endif
  117. #else /* !defined(__DCACHE_PRESENT) */
  118. #define CACHE_SIZE_ALIGN(t, n) (n)
  119. #define cacheBufferInvalidate(addr, size) { \
  120. (void)(addr); \
  121. (void)(size); \
  122. }
  123. #define cacheBufferFlush(addr, size) { \
  124. (void)(addr); \
  125. (void)(size); \
  126. }
  127. #endif
  128. /*===========================================================================*/
  129. /* External declarations. */
  130. /*===========================================================================*/
  131. #ifdef __cplusplus
  132. extern "C" {
  133. #endif
  134. #ifdef __cplusplus
  135. }
  136. #endif
  137. #endif /* CACHE_H */
  138. /** @} */