intc_esp32.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ZEPHYR_INCLUDE_DRIVERS_ESP_INTR_ALLOC_H__
  7. #define ZEPHYR_INCLUDE_DRIVERS_ESP_INTR_ALLOC_H__
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. /* number of possible interrupts per core */
  11. #define ESP_INTC_INTS_NUM (32)
  12. /*
  13. * Interrupt allocation flags - These flags can be used to specify
  14. * which interrupt qualities the code calling esp_intr_alloc* needs.
  15. *
  16. */
  17. /* Keep the LEVELx values as they are here; they match up with (1<<level) */
  18. #define ESP_INTR_FLAG_LEVEL1 (1<<1) /* Accept a Level 1 int vector, lowest priority */
  19. #define ESP_INTR_FLAG_LEVEL2 (1<<2) /* Accept a Level 2 int vector */
  20. #define ESP_INTR_FLAG_LEVEL3 (1<<3) /* Accept a Level 3 int vector */
  21. #define ESP_INTR_FLAG_LEVEL4 (1<<4) /* Accept a Level 4 int vector */
  22. #define ESP_INTR_FLAG_LEVEL5 (1<<5) /* Accept a Level 5 int vector */
  23. #define ESP_INTR_FLAG_LEVEL6 (1<<6) /* Accept a Level 6 int vector */
  24. #define ESP_INTR_FLAG_NMI (1<<7) /* Accept a Level 7 int vector, highest priority */
  25. #define ESP_INTR_FLAG_SHARED (1<<8) /* Interrupt can be shared between ISRs */
  26. #define ESP_INTR_FLAG_EDGE (1<<9) /* Edge-triggered interrupt */
  27. #define ESP_INTR_FLAG_IRAM (1<<10) /* ISR can be called if cache is disabled */
  28. #define ESP_INTR_FLAG_INTRDISABLED (1<<11) /* Return with this interrupt disabled */
  29. /* Low and medium prio interrupts. These can be handled in C. */
  30. #define ESP_INTR_FLAG_LOWMED (ESP_INTR_FLAG_LEVEL1|ESP_INTR_FLAG_LEVEL2|ESP_INTR_FLAG_LEVEL3)
  31. /* High level interrupts. Need to be handled in assembly. */
  32. #define ESP_INTR_FLAG_HIGH (ESP_INTR_FLAG_LEVEL4|ESP_INTR_FLAG_LEVEL5|ESP_INTR_FLAG_LEVEL6| \
  33. ESP_INTR_FLAG_NMI)
  34. /* Mask for all level flags */
  35. #define ESP_INTR_FLAG_LEVELMASK (ESP_INTR_FLAG_LEVEL1|ESP_INTR_FLAG_LEVEL2|ESP_INTR_FLAG_LEVEL3| \
  36. ESP_INTR_FLAG_LEVEL4|ESP_INTR_FLAG_LEVEL5|ESP_INTR_FLAG_LEVEL6| \
  37. ESP_INTR_FLAG_NMI)
  38. /*
  39. * The esp_intr_alloc* functions can allocate an int for all *_INTR_SOURCE int sources that
  40. * are routed through the interrupt mux. Apart from these sources, each core also has some internal
  41. * sources that do not pass through the interrupt mux. To allocate an interrupt for these sources,
  42. * pass these pseudo-sources to the functions.
  43. */
  44. #define ETS_INTERNAL_TIMER0_INTR_SOURCE -1 /* Xtensa timer 0 interrupt source */
  45. #define ETS_INTERNAL_TIMER1_INTR_SOURCE -2 /* Xtensa timer 1 interrupt source */
  46. #define ETS_INTERNAL_TIMER2_INTR_SOURCE -3 /* Xtensa timer 2 interrupt source */
  47. #define ETS_INTERNAL_SW0_INTR_SOURCE -4 /* Software int source 1 */
  48. #define ETS_INTERNAL_SW1_INTR_SOURCE -5 /* Software int source 2 */
  49. #define ETS_INTERNAL_PROFILING_INTR_SOURCE -6 /* Int source for profiling */
  50. /* Function prototype for interrupt handler function */
  51. typedef void (*intr_handler_t)(void *arg);
  52. struct shared_vector_desc_t {
  53. int disabled : 1;
  54. int source : 8;
  55. volatile uint32_t *statusreg;
  56. uint32_t statusmask;
  57. intr_handler_t isr;
  58. void *arg;
  59. struct shared_vector_desc_t *next;
  60. };
  61. /* Pack using bitfields for better memory use */
  62. struct vector_desc_t {
  63. int flags : 16; /* OR of VECDESC_FLAG_* defines */
  64. unsigned int cpu : 1;
  65. unsigned int intno : 5;
  66. int source : 8; /* Int mux flags, used when not shared */
  67. struct shared_vector_desc_t *shared_vec_info; /* used when VECDESC_FL_SHARED */
  68. struct vector_desc_t *next;
  69. };
  70. /** Interrupt handler associated data structure */
  71. struct intr_handle_data_t {
  72. struct vector_desc_t *vector_desc;
  73. struct shared_vector_desc_t *shared_vector_desc;
  74. };
  75. /**
  76. * @brief Initializes interrupt table to its defaults
  77. */
  78. void esp_intr_initialize(void);
  79. /**
  80. * @brief Mark an interrupt as a shared interrupt
  81. *
  82. * This will mark a certain interrupt on the specified CPU as
  83. * an interrupt that can be used to hook shared interrupt handlers
  84. * to.
  85. *
  86. * @param intno The number of the interrupt (0-31)
  87. * @param cpu CPU on which the interrupt should be marked as shared (0 or 1)
  88. * @param is_in_iram Shared interrupt is for handlers that reside in IRAM and
  89. * the int can be left enabled while the flash cache is disabled.
  90. *
  91. * @return -EINVAL if cpu or intno is invalid
  92. * 0 otherwise
  93. */
  94. int esp_intr_mark_shared(int intno, int cpu, bool is_in_iram);
  95. /**
  96. * @brief Reserve an interrupt to be used outside of this framework
  97. *
  98. * This will mark a certain interrupt on the specified CPU as
  99. * reserved, not to be allocated for any reason.
  100. *
  101. * @param intno The number of the interrupt (0-31)
  102. * @param cpu CPU on which the interrupt should be marked as shared (0 or 1)
  103. *
  104. * @return -EINVAL if cpu or intno is invalid
  105. * 0 otherwise
  106. */
  107. int esp_intr_reserve(int intno, int cpu);
  108. /**
  109. * @brief Allocate an interrupt with the given parameters.
  110. *
  111. * This finds an interrupt that matches the restrictions as given in the flags
  112. * parameter, maps the given interrupt source to it and hooks up the given
  113. * interrupt handler (with optional argument) as well. If needed, it can return
  114. * a handle for the interrupt as well.
  115. *
  116. * The interrupt will always be allocated on the core that runs this function.
  117. *
  118. * If ESP_INTR_FLAG_IRAM flag is used, and handler address is not in IRAM or
  119. * RTC_FAST_MEM, then ESP_ERR_INVALID_ARG is returned.
  120. *
  121. * @param source The interrupt source. One of the *_INTR_SOURCE interrupt mux
  122. * sources, as defined in esp-xtensa-intmux.h, or one of the internal
  123. * ETS_INTERNAL_*_INTR_SOURCE sources as defined in this header.
  124. * @param flags An ORred mask of the ESP_INTR_FLAG_* defines. These restrict the
  125. * choice of interrupts that this routine can choose from. If this value
  126. * is 0, it will default to allocating a non-shared interrupt of level
  127. * 1, 2 or 3. If this is ESP_INTR_FLAG_SHARED, it will allocate a shared
  128. * interrupt of level 1. Setting ESP_INTR_FLAG_INTRDISABLED will return
  129. * from this function with the interrupt disabled.
  130. * @param handler The interrupt handler. Must be NULL when an interrupt of level >3
  131. * is requested, because these types of interrupts aren't C-callable.
  132. * @param arg Optional argument for passed to the interrupt handler
  133. * @param ret_handle Pointer to a struct intr_handle_data_t pointer to store a handle that can
  134. * later be used to request details or free the interrupt. Can be NULL if no handle
  135. * is required.
  136. *
  137. * @return -EINVAL if the combination of arguments is invalid.
  138. * -ENODEV No free interrupt found with the specified flags
  139. * 0 otherwise
  140. */
  141. int esp_intr_alloc(int source,
  142. int flags,
  143. intr_handler_t handler,
  144. void *arg,
  145. struct intr_handle_data_t **ret_handle);
  146. /**
  147. * @brief Allocate an interrupt with the given parameters.
  148. *
  149. *
  150. * This essentially does the same as esp_intr_alloc, but allows specifying a register and mask
  151. * combo. For shared interrupts, the handler is only called if a read from the specified
  152. * register, ANDed with the mask, returns non-zero. By passing an interrupt status register
  153. * address and a fitting mask, this can be used to accelerate interrupt handling in the case
  154. * a shared interrupt is triggered; by checking the interrupt statuses first, the code can
  155. * decide which ISRs can be skipped
  156. *
  157. * @param source The interrupt source. One of the *_INTR_SOURCE interrupt mux
  158. * sources, as defined in esp-xtensa-intmux.h, or one of the internal
  159. * ETS_INTERNAL_*_INTR_SOURCE sources as defined in this header.
  160. * @param flags An ORred mask of the ESP_INTR_FLAG_* defines. These restrict the
  161. * choice of interrupts that this routine can choose from. If this value
  162. * is 0, it will default to allocating a non-shared interrupt of level
  163. * 1, 2 or 3. If this is ESP_INTR_FLAG_SHARED, it will allocate a shared
  164. * interrupt of level 1. Setting ESP_INTR_FLAG_INTRDISABLED will return
  165. * from this function with the interrupt disabled.
  166. * @param intrstatusreg The address of an interrupt status register
  167. * @param intrstatusmask A mask. If a read of address intrstatusreg has any of the bits
  168. * that are 1 in the mask set, the ISR will be called. If not, it will be
  169. * skipped.
  170. * @param handler The interrupt handler. Must be NULL when an interrupt of level >3
  171. * is requested, because these types of interrupts aren't C-callable.
  172. * @param arg Optional argument for passed to the interrupt handler
  173. * @param ret_handle Pointer to a struct intr_handle_data_t pointer to store a handle that can
  174. * later be used to request details or free the interrupt. Can be NULL if no handle
  175. * is required.
  176. *
  177. * @return -EINVAL if the combination of arguments is invalid.
  178. * -ENODEV No free interrupt found with the specified flags
  179. * 0 otherwise
  180. */
  181. int esp_intr_alloc_intrstatus(int source,
  182. int flags,
  183. uint32_t intrstatusreg,
  184. uint32_t intrstatusmask,
  185. intr_handler_t handler,
  186. void *arg,
  187. struct intr_handle_data_t **ret_handle);
  188. /**
  189. * @brief Disable and free an interrupt.
  190. *
  191. * Use an interrupt handle to disable the interrupt and release the resources associated with it.
  192. * If the current core is not the core that registered this interrupt, this routine will be
  193. * assigned to the core that allocated this interrupt, blocking and waiting until the resource
  194. * is successfully released.
  195. *
  196. * @note
  197. * When the handler shares its source with other handlers, the interrupt status bits
  198. * it's responsible for should be managed properly before freeing it. See ``esp_intr_disable``
  199. * for more details. Please do not call this function in ``esp_ipc_call_blocking``.
  200. *
  201. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  202. *
  203. * @return -EINVAL the handle is NULL
  204. * 0 otherwise
  205. */
  206. int esp_intr_free(struct intr_handle_data_t *handle);
  207. /**
  208. * @brief Get CPU number an interrupt is tied to
  209. *
  210. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  211. *
  212. * @return The core number where the interrupt is allocated
  213. */
  214. int esp_intr_get_cpu(struct intr_handle_data_t *handle);
  215. /**
  216. * @brief Get the allocated interrupt for a certain handle
  217. *
  218. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  219. *
  220. * @return The interrupt number
  221. */
  222. int esp_intr_get_intno(struct intr_handle_data_t *handle);
  223. /**
  224. * @brief Disable the interrupt associated with the handle
  225. *
  226. * @note
  227. * 1. For local interrupts (ESP_INTERNAL_* sources), this function has to be called on the
  228. * CPU the interrupt is allocated on. Other interrupts have no such restriction.
  229. * 2. When several handlers sharing a same interrupt source, interrupt status bits, which are
  230. * handled in the handler to be disabled, should be masked before the disabling, or handled
  231. * in other enabled interrupts properly. Miss of interrupt status handling will cause infinite
  232. * interrupt calls and finally system crash.
  233. *
  234. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  235. *
  236. * @return -EINVAL if the combination of arguments is invalid.
  237. * 0 otherwise
  238. */
  239. int esp_intr_disable(struct intr_handle_data_t *handle);
  240. /**
  241. * @brief Enable the interrupt associated with the handle
  242. *
  243. * @note For local interrupts (ESP_INTERNAL_* sources), this function has to be called on the
  244. * CPU the interrupt is allocated on. Other interrupts have no such restriction.
  245. *
  246. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  247. *
  248. * @return -EINVAL if the combination of arguments is invalid.
  249. * 0 otherwise
  250. */
  251. int esp_intr_enable(struct intr_handle_data_t *handle);
  252. /**
  253. * @brief Set the "in IRAM" status of the handler.
  254. *
  255. * @note Does not work on shared interrupts.
  256. *
  257. * @param handle The handle, as obtained by esp_intr_alloc or esp_intr_alloc_intrstatus
  258. * @param is_in_iram Whether the handler associated with this handle resides in IRAM.
  259. * Handlers residing in IRAM can be called when cache is disabled.
  260. *
  261. * @return -EINVAL if the combination of arguments is invalid.
  262. * 0 otherwise
  263. */
  264. int esp_intr_set_in_iram(struct intr_handle_data_t *handle, bool is_in_iram);
  265. /**
  266. * @brief Disable interrupts that aren't specifically marked as running from IRAM
  267. */
  268. void esp_intr_noniram_disable(void);
  269. /**
  270. * @brief Re-enable interrupts disabled by esp_intr_noniram_disable
  271. */
  272. void esp_intr_noniram_enable(void);
  273. #endif