intr_alloc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include <string.h>
  11. #include <esp_types.h>
  12. #include <limits.h>
  13. #include <assert.h>
  14. #include "sdkconfig.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "esp_err.h"
  18. #include "esp_log.h"
  19. #include "esp_intr_alloc.h"
  20. #include "esp_attr.h"
  21. #include "hal/cpu_hal.h"
  22. #include "hal/interrupt_controller_hal.h"
  23. #if !CONFIG_FREERTOS_UNICORE
  24. #include "esp_ipc.h"
  25. #endif
  26. static const char* TAG = "intr_alloc";
  27. #define ETS_INTERNAL_TIMER0_INTR_NO 6
  28. #define ETS_INTERNAL_TIMER1_INTR_NO 15
  29. #define ETS_INTERNAL_TIMER2_INTR_NO 16
  30. #define ETS_INTERNAL_SW0_INTR_NO 7
  31. #define ETS_INTERNAL_SW1_INTR_NO 29
  32. #define ETS_INTERNAL_PROFILING_INTR_NO 11
  33. /*
  34. Define this to debug the choices made when allocating the interrupt. This leads to much debugging
  35. output within a critical region, which can lead to weird effects like e.g. the interrupt watchdog
  36. being triggered, that is why it is separate from the normal LOG* scheme.
  37. */
  38. // #define DEBUG_INT_ALLOC_DECISIONS
  39. #ifdef DEBUG_INT_ALLOC_DECISIONS
  40. # define ALCHLOG(...) ESP_EARLY_LOGD(TAG, __VA_ARGS__)
  41. #else
  42. # define ALCHLOG(...) do {} while (0)
  43. #endif
  44. typedef struct shared_vector_desc_t shared_vector_desc_t;
  45. typedef struct vector_desc_t vector_desc_t;
  46. struct shared_vector_desc_t {
  47. int disabled: 1;
  48. int source: 8;
  49. volatile uint32_t *statusreg;
  50. uint32_t statusmask;
  51. intr_handler_t isr;
  52. void *arg;
  53. shared_vector_desc_t *next;
  54. };
  55. #define VECDESC_FL_RESERVED (1<<0)
  56. #define VECDESC_FL_INIRAM (1<<1)
  57. #define VECDESC_FL_SHARED (1<<2)
  58. #define VECDESC_FL_NONSHARED (1<<3)
  59. //Pack using bitfields for better memory use
  60. struct vector_desc_t {
  61. int flags: 16; //OR of VECDESC_FLAG_* defines
  62. unsigned int cpu: 1;
  63. unsigned int intno: 5;
  64. int source: 8; //Interrupt mux flags, used when not shared
  65. shared_vector_desc_t *shared_vec_info; //used when VECDESC_FL_SHARED
  66. vector_desc_t *next;
  67. };
  68. struct intr_handle_data_t {
  69. vector_desc_t *vector_desc;
  70. shared_vector_desc_t *shared_vector_desc;
  71. };
  72. typedef struct non_shared_isr_arg_t non_shared_isr_arg_t;
  73. struct non_shared_isr_arg_t {
  74. intr_handler_t isr;
  75. void *isr_arg;
  76. int source;
  77. };
  78. //Linked list of vector descriptions, sorted by cpu.intno value
  79. static vector_desc_t *vector_desc_head = NULL;
  80. //This bitmask has an 1 if the int should be disabled when the flash is disabled.
  81. static uint32_t non_iram_int_mask[SOC_CPU_CORES_NUM];
  82. //This bitmask has 1 in it if the int was disabled using esp_intr_noniram_disable.
  83. static uint32_t non_iram_int_disabled[SOC_CPU_CORES_NUM];
  84. static bool non_iram_int_disabled_flag[SOC_CPU_CORES_NUM];
  85. static portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
  86. //Inserts an item into vector_desc list so that the list is sorted
  87. //with an incrementing cpu.intno value.
  88. static void insert_vector_desc(vector_desc_t *to_insert)
  89. {
  90. vector_desc_t *vd=vector_desc_head;
  91. vector_desc_t *prev=NULL;
  92. while(vd!=NULL) {
  93. if (vd->cpu > to_insert->cpu) break;
  94. if (vd->cpu == to_insert->cpu && vd->intno >= to_insert->intno) break;
  95. prev=vd;
  96. vd=vd->next;
  97. }
  98. if ((vector_desc_head==NULL) || (prev==NULL)) {
  99. //First item
  100. to_insert->next = vd;
  101. vector_desc_head=to_insert;
  102. } else {
  103. prev->next=to_insert;
  104. to_insert->next=vd;
  105. }
  106. }
  107. //Returns a vector_desc entry for an intno/cpu, or NULL if none exists.
  108. static vector_desc_t *find_desc_for_int(int intno, int cpu)
  109. {
  110. vector_desc_t *vd=vector_desc_head;
  111. while(vd!=NULL) {
  112. if (vd->cpu==cpu && vd->intno==intno) break;
  113. vd=vd->next;
  114. }
  115. return vd;
  116. }
  117. //Returns a vector_desc entry for an intno/cpu.
  118. //Either returns a preexisting one or allocates a new one and inserts
  119. //it into the list. Returns NULL on malloc fail.
  120. static vector_desc_t *get_desc_for_int(int intno, int cpu)
  121. {
  122. vector_desc_t *vd=find_desc_for_int(intno, cpu);
  123. if (vd==NULL) {
  124. vector_desc_t *newvd=heap_caps_malloc(sizeof(vector_desc_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  125. if (newvd==NULL) return NULL;
  126. memset(newvd, 0, sizeof(vector_desc_t));
  127. newvd->intno=intno;
  128. newvd->cpu=cpu;
  129. insert_vector_desc(newvd);
  130. return newvd;
  131. } else {
  132. return vd;
  133. }
  134. }
  135. //Returns a vector_desc entry for an source, the cpu parameter is used to tell GPIO_INT and GPIO_NMI from different CPUs
  136. static vector_desc_t * find_desc_for_source(int source, int cpu)
  137. {
  138. vector_desc_t *vd=vector_desc_head;
  139. while(vd!=NULL) {
  140. if ( !(vd->flags & VECDESC_FL_SHARED) ) {
  141. if ( vd->source == source && cpu == vd->cpu ) break;
  142. } else if ( vd->cpu == cpu ) {
  143. // check only shared vds for the correct cpu, otherwise skip
  144. bool found = false;
  145. shared_vector_desc_t *svd = vd->shared_vec_info;
  146. assert(svd != NULL );
  147. while(svd) {
  148. if ( svd->source == source ) {
  149. found = true;
  150. break;
  151. }
  152. svd = svd->next;
  153. }
  154. if ( found ) break;
  155. }
  156. vd=vd->next;
  157. }
  158. return vd;
  159. }
  160. esp_err_t esp_intr_mark_shared(int intno, int cpu, bool is_int_ram)
  161. {
  162. if (intno>31) return ESP_ERR_INVALID_ARG;
  163. if (cpu>=SOC_CPU_CORES_NUM) return ESP_ERR_INVALID_ARG;
  164. portENTER_CRITICAL(&spinlock);
  165. vector_desc_t *vd=get_desc_for_int(intno, cpu);
  166. if (vd==NULL) {
  167. portEXIT_CRITICAL(&spinlock);
  168. return ESP_ERR_NO_MEM;
  169. }
  170. vd->flags=VECDESC_FL_SHARED;
  171. if (is_int_ram) vd->flags|=VECDESC_FL_INIRAM;
  172. portEXIT_CRITICAL(&spinlock);
  173. return ESP_OK;
  174. }
  175. esp_err_t esp_intr_reserve(int intno, int cpu)
  176. {
  177. if (intno>31) return ESP_ERR_INVALID_ARG;
  178. if (cpu>=SOC_CPU_CORES_NUM) return ESP_ERR_INVALID_ARG;
  179. portENTER_CRITICAL(&spinlock);
  180. vector_desc_t *vd=get_desc_for_int(intno, cpu);
  181. if (vd==NULL) {
  182. portEXIT_CRITICAL(&spinlock);
  183. return ESP_ERR_NO_MEM;
  184. }
  185. vd->flags=VECDESC_FL_RESERVED;
  186. portEXIT_CRITICAL(&spinlock);
  187. return ESP_OK;
  188. }
  189. static bool is_vect_desc_usable(vector_desc_t *vd, int flags, int cpu, int force)
  190. {
  191. //Check if interrupt is not reserved by design
  192. int x = vd->intno;
  193. if (interrupt_controller_hal_get_cpu_desc_flags(x, cpu)==INTDESC_RESVD) {
  194. ALCHLOG("....Unusable: reserved");
  195. return false;
  196. }
  197. if (interrupt_controller_hal_get_cpu_desc_flags(x, cpu)==INTDESC_SPECIAL && force==-1) {
  198. ALCHLOG("....Unusable: special-purpose int");
  199. return false;
  200. }
  201. #ifndef SOC_CPU_HAS_FLEXIBLE_INTC
  202. //Check if the interrupt level is acceptable
  203. if (!(flags&(1<<interrupt_controller_hal_get_level(x)))) {
  204. ALCHLOG("....Unusable: incompatible level");
  205. return false;
  206. }
  207. //check if edge/level type matches what we want
  208. if (((flags&ESP_INTR_FLAG_EDGE) && (interrupt_controller_hal_get_type(x)==INTTP_LEVEL)) ||
  209. (((!(flags&ESP_INTR_FLAG_EDGE)) && (interrupt_controller_hal_get_type(x)==INTTP_EDGE)))) {
  210. ALCHLOG("....Unusable: incompatible trigger type");
  211. return false;
  212. }
  213. #endif
  214. //check if interrupt is reserved at runtime
  215. if (vd->flags&VECDESC_FL_RESERVED) {
  216. ALCHLOG("....Unusable: reserved at runtime.");
  217. return false;
  218. }
  219. //Ints can't be both shared and non-shared.
  220. assert(!((vd->flags&VECDESC_FL_SHARED)&&(vd->flags&VECDESC_FL_NONSHARED)));
  221. //check if interrupt already is in use by a non-shared interrupt
  222. if (vd->flags&VECDESC_FL_NONSHARED) {
  223. ALCHLOG("....Unusable: already in (non-shared) use.");
  224. return false;
  225. }
  226. // check shared interrupt flags
  227. if (vd->flags&VECDESC_FL_SHARED ) {
  228. if (flags&ESP_INTR_FLAG_SHARED) {
  229. bool in_iram_flag=((flags&ESP_INTR_FLAG_IRAM)!=0);
  230. bool desc_in_iram_flag=((vd->flags&VECDESC_FL_INIRAM)!=0);
  231. //Bail out if int is shared, but iram property doesn't match what we want.
  232. if ((vd->flags&VECDESC_FL_SHARED) && (desc_in_iram_flag!=in_iram_flag)) {
  233. ALCHLOG("....Unusable: shared but iram prop doesn't match");
  234. return false;
  235. }
  236. } else {
  237. //We need an unshared IRQ; can't use shared ones; bail out if this is shared.
  238. ALCHLOG("...Unusable: int is shared, we need non-shared.");
  239. return false;
  240. }
  241. } else if (interrupt_controller_hal_has_handler(x, cpu)) {
  242. //Check if interrupt already is allocated by interrupt_controller_hal_set_int_handler
  243. ALCHLOG("....Unusable: already allocated");
  244. return false;
  245. }
  246. return true;
  247. }
  248. //Locate a free interrupt compatible with the flags given.
  249. //The 'force' argument can be -1, or 0-31 to force checking a certain interrupt.
  250. //When a CPU is forced, the INTDESC_SPECIAL marked interrupts are also accepted.
  251. static int get_available_int(int flags, int cpu, int force, int source)
  252. {
  253. int x;
  254. int best=-1;
  255. int bestLevel=9;
  256. int bestSharedCt=INT_MAX;
  257. //Default vector desc, for vectors not in the linked list
  258. vector_desc_t empty_vect_desc;
  259. memset(&empty_vect_desc, 0, sizeof(vector_desc_t));
  260. //Level defaults to any low/med interrupt
  261. if (!(flags&ESP_INTR_FLAG_LEVELMASK)) flags|=ESP_INTR_FLAG_LOWMED;
  262. ALCHLOG("get_available_int: try to find existing. Cpu: %d, Source: %d", cpu, source);
  263. vector_desc_t *vd = find_desc_for_source(source, cpu);
  264. if ( vd ) {
  265. // if existing vd found, don't need to search any more.
  266. ALCHLOG("get_avalible_int: existing vd found. intno: %d", vd->intno);
  267. if ( force != -1 && force != vd->intno ) {
  268. ALCHLOG("get_avalible_int: intr forced but not matach existing. existing intno: %d, force: %d", vd->intno, force);
  269. } else if ( !is_vect_desc_usable(vd, flags, cpu, force) ) {
  270. ALCHLOG("get_avalible_int: existing vd invalid.");
  271. } else {
  272. best = vd->intno;
  273. }
  274. return best;
  275. }
  276. if (force!=-1) {
  277. ALCHLOG("get_available_int: try to find force. Cpu: %d, Source: %d, Force: %d", cpu, source, force);
  278. //if force assigned, don't need to search any more.
  279. vd = find_desc_for_int(force, cpu);
  280. if (vd == NULL ) {
  281. //if existing vd not found, just check the default state for the intr.
  282. empty_vect_desc.intno = force;
  283. vd = &empty_vect_desc;
  284. }
  285. if ( is_vect_desc_usable(vd, flags, cpu, force) ) {
  286. best = vd->intno;
  287. } else {
  288. ALCHLOG("get_avalible_int: forced vd invalid.");
  289. }
  290. return best;
  291. }
  292. ALCHLOG("get_free_int: start looking. Current cpu: %d", cpu);
  293. //No allocated handlers as well as forced intr, iterate over the 32 possible interrupts
  294. for (x=0; x<32; x++) {
  295. //Grab the vector_desc for this vector.
  296. vd=find_desc_for_int(x, cpu);
  297. if (vd==NULL) {
  298. empty_vect_desc.intno = x;
  299. vd=&empty_vect_desc;
  300. }
  301. ALCHLOG("Int %d reserved %d level %d %s hasIsr %d",
  302. x, interrupt_controller_hal_get_cpu_desc_flags(x,cpu)==INTDESC_RESVD, interrupt_controller_hal_get_level(x),
  303. interrupt_controller_hal_get_type(x)==INTTP_LEVEL?"LEVEL":"EDGE", interrupt_controller_hal_has_handler(x, cpu));
  304. if ( !is_vect_desc_usable(vd, flags, cpu, force) ) continue;
  305. if (flags&ESP_INTR_FLAG_SHARED) {
  306. //We're allocating a shared int.
  307. //See if int already is used as a shared interrupt.
  308. if (vd->flags&VECDESC_FL_SHARED) {
  309. //We can use this already-marked-as-shared interrupt. Count the already attached isrs in order to see
  310. //how useful it is.
  311. int no=0;
  312. shared_vector_desc_t *svdesc=vd->shared_vec_info;
  313. while (svdesc!=NULL) {
  314. no++;
  315. svdesc=svdesc->next;
  316. }
  317. if (no<bestSharedCt || bestLevel>interrupt_controller_hal_get_level(x)) {
  318. //Seems like this shared vector is both okay and has the least amount of ISRs already attached to it.
  319. best=x;
  320. bestSharedCt=no;
  321. bestLevel=interrupt_controller_hal_get_level(x);
  322. ALCHLOG("...int %d more usable as a shared int: has %d existing vectors", x, no);
  323. } else {
  324. ALCHLOG("...worse than int %d", best);
  325. }
  326. } else {
  327. if (best==-1) {
  328. //We haven't found a feasible shared interrupt yet. This one is still free and usable, even if
  329. //not marked as shared.
  330. //Remember it in case we don't find any other shared interrupt that qualifies.
  331. if (bestLevel>interrupt_controller_hal_get_level(x)) {
  332. best=x;
  333. bestLevel=interrupt_controller_hal_get_level(x);
  334. ALCHLOG("...int %d usable as a new shared int", x);
  335. }
  336. } else {
  337. ALCHLOG("...already have a shared int");
  338. }
  339. }
  340. } else {
  341. //Seems this interrupt is feasible. Select it and break out of the loop; no need to search further.
  342. if (bestLevel>interrupt_controller_hal_get_level(x)) {
  343. best=x;
  344. bestLevel=interrupt_controller_hal_get_level(x);
  345. } else {
  346. ALCHLOG("...worse than int %d", best);
  347. }
  348. }
  349. }
  350. ALCHLOG("get_available_int: using int %d", best);
  351. //Okay, by now we have looked at all potential interrupts and hopefully have selected the best one in best.
  352. return best;
  353. }
  354. //Common shared isr handler. Chain-call all ISRs.
  355. static void IRAM_ATTR shared_intr_isr(void *arg)
  356. {
  357. vector_desc_t *vd=(vector_desc_t*)arg;
  358. shared_vector_desc_t *sh_vec=vd->shared_vec_info;
  359. portENTER_CRITICAL_ISR(&spinlock);
  360. while(sh_vec) {
  361. if (!sh_vec->disabled) {
  362. if ((sh_vec->statusreg == NULL) || (*sh_vec->statusreg & sh_vec->statusmask)) {
  363. traceISR_ENTER(sh_vec->source+ETS_INTERNAL_INTR_SOURCE_OFF);
  364. sh_vec->isr(sh_vec->arg);
  365. // check if we will return to scheduler or to interrupted task after ISR
  366. if (!os_task_switch_is_pended(cpu_hal_get_core_id())) {
  367. traceISR_EXIT();
  368. }
  369. }
  370. }
  371. sh_vec=sh_vec->next;
  372. }
  373. portEXIT_CRITICAL_ISR(&spinlock);
  374. }
  375. #if CONFIG_APPTRACE_SV_ENABLE
  376. //Common non-shared isr handler wrapper.
  377. static void IRAM_ATTR non_shared_intr_isr(void *arg)
  378. {
  379. non_shared_isr_arg_t *ns_isr_arg=(non_shared_isr_arg_t*)arg;
  380. portENTER_CRITICAL_ISR(&spinlock);
  381. traceISR_ENTER(ns_isr_arg->source+ETS_INTERNAL_INTR_SOURCE_OFF);
  382. // FIXME: can we call ISR and check os_task_switch_is_pended() after releasing spinlock?
  383. // when CONFIG_APPTRACE_SV_ENABLE = 0 ISRs for non-shared IRQs are called without spinlock
  384. ns_isr_arg->isr(ns_isr_arg->isr_arg);
  385. // check if we will return to scheduler or to interrupted task after ISR
  386. if (!os_task_switch_is_pended(cpu_hal_get_core_id())) {
  387. traceISR_EXIT();
  388. }
  389. portEXIT_CRITICAL_ISR(&spinlock);
  390. }
  391. #endif
  392. //We use ESP_EARLY_LOG* here because this can be called before the scheduler is running.
  393. esp_err_t esp_intr_alloc_intrstatus(int source, int flags, uint32_t intrstatusreg, uint32_t intrstatusmask, intr_handler_t handler,
  394. void *arg, intr_handle_t *ret_handle)
  395. {
  396. intr_handle_data_t *ret=NULL;
  397. int force=-1;
  398. ESP_EARLY_LOGV(TAG, "esp_intr_alloc_intrstatus (cpu %u): checking args", cpu_hal_get_core_id());
  399. //Shared interrupts should be level-triggered.
  400. if ((flags&ESP_INTR_FLAG_SHARED) && (flags&ESP_INTR_FLAG_EDGE)) return ESP_ERR_INVALID_ARG;
  401. //You can't set an handler / arg for a non-C-callable interrupt.
  402. if ((flags&ESP_INTR_FLAG_HIGH) && (handler)) return ESP_ERR_INVALID_ARG;
  403. //Shared ints should have handler and non-processor-local source
  404. if ((flags&ESP_INTR_FLAG_SHARED) && (!handler || source<0)) return ESP_ERR_INVALID_ARG;
  405. //Statusreg should have a mask
  406. if (intrstatusreg && !intrstatusmask) return ESP_ERR_INVALID_ARG;
  407. //If the ISR is marked to be IRAM-resident, the handler must not be in the cached region
  408. //ToDo: if we are to allow placing interrupt handlers into the 0x400c0000—0x400c2000 region,
  409. //we need to make sure the interrupt is connected to the CPU0.
  410. //CPU1 does not have access to the RTC fast memory through this region.
  411. if ((flags & ESP_INTR_FLAG_IRAM) && handler && !esp_ptr_in_iram(handler) && !esp_ptr_in_rtc_iram_fast(handler)) {
  412. return ESP_ERR_INVALID_ARG;
  413. }
  414. //Default to prio 1 for shared interrupts. Default to prio 1, 2 or 3 for non-shared interrupts.
  415. if ((flags&ESP_INTR_FLAG_LEVELMASK)==0) {
  416. if (flags&ESP_INTR_FLAG_SHARED) {
  417. flags|=ESP_INTR_FLAG_LEVEL1;
  418. } else {
  419. flags|=ESP_INTR_FLAG_LOWMED;
  420. }
  421. }
  422. ESP_EARLY_LOGV(TAG, "esp_intr_alloc_intrstatus (cpu %u): Args okay. Resulting flags 0x%X", cpu_hal_get_core_id(), flags);
  423. //Check 'special' interrupt sources. These are tied to one specific interrupt, so we
  424. //have to force get_free_int to only look at that.
  425. if (source==ETS_INTERNAL_TIMER0_INTR_SOURCE) force=ETS_INTERNAL_TIMER0_INTR_NO;
  426. if (source==ETS_INTERNAL_TIMER1_INTR_SOURCE) force=ETS_INTERNAL_TIMER1_INTR_NO;
  427. if (source==ETS_INTERNAL_TIMER2_INTR_SOURCE) force=ETS_INTERNAL_TIMER2_INTR_NO;
  428. if (source==ETS_INTERNAL_SW0_INTR_SOURCE) force=ETS_INTERNAL_SW0_INTR_NO;
  429. if (source==ETS_INTERNAL_SW1_INTR_SOURCE) force=ETS_INTERNAL_SW1_INTR_NO;
  430. if (source==ETS_INTERNAL_PROFILING_INTR_SOURCE) force=ETS_INTERNAL_PROFILING_INTR_NO;
  431. //Allocate a return handle. If we end up not needing it, we'll free it later on.
  432. ret=heap_caps_malloc(sizeof(intr_handle_data_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  433. if (ret==NULL) return ESP_ERR_NO_MEM;
  434. portENTER_CRITICAL(&spinlock);
  435. uint32_t cpu = cpu_hal_get_core_id();
  436. //See if we can find an interrupt that matches the flags.
  437. int intr=get_available_int(flags, cpu, force, source);
  438. if (intr==-1) {
  439. //None found. Bail out.
  440. portEXIT_CRITICAL(&spinlock);
  441. free(ret);
  442. return ESP_ERR_NOT_FOUND;
  443. }
  444. //Get an int vector desc for int.
  445. vector_desc_t *vd=get_desc_for_int(intr, cpu);
  446. if (vd==NULL) {
  447. portEXIT_CRITICAL(&spinlock);
  448. free(ret);
  449. return ESP_ERR_NO_MEM;
  450. }
  451. //Allocate that int!
  452. if (flags&ESP_INTR_FLAG_SHARED) {
  453. //Populate vector entry and add to linked list.
  454. shared_vector_desc_t *sh_vec=malloc(sizeof(shared_vector_desc_t));
  455. if (sh_vec==NULL) {
  456. portEXIT_CRITICAL(&spinlock);
  457. free(ret);
  458. return ESP_ERR_NO_MEM;
  459. }
  460. memset(sh_vec, 0, sizeof(shared_vector_desc_t));
  461. sh_vec->statusreg=(uint32_t*)intrstatusreg;
  462. sh_vec->statusmask=intrstatusmask;
  463. sh_vec->isr=handler;
  464. sh_vec->arg=arg;
  465. sh_vec->next=vd->shared_vec_info;
  466. sh_vec->source=source;
  467. sh_vec->disabled=0;
  468. vd->shared_vec_info=sh_vec;
  469. vd->flags|=VECDESC_FL_SHARED;
  470. //(Re-)set shared isr handler to new value.
  471. interrupt_controller_hal_set_int_handler(intr, shared_intr_isr, vd);
  472. } else {
  473. //Mark as unusable for other interrupt sources. This is ours now!
  474. vd->flags=VECDESC_FL_NONSHARED;
  475. if (handler) {
  476. #if CONFIG_APPTRACE_SV_ENABLE
  477. non_shared_isr_arg_t *ns_isr_arg=malloc(sizeof(non_shared_isr_arg_t));
  478. if (!ns_isr_arg) {
  479. portEXIT_CRITICAL(&spinlock);
  480. free(ret);
  481. return ESP_ERR_NO_MEM;
  482. }
  483. ns_isr_arg->isr=handler;
  484. ns_isr_arg->isr_arg=arg;
  485. ns_isr_arg->source=source;
  486. interrupt_controller_hal_set_int_handler(intr, non_shared_intr_isr, ns_isr_arg);
  487. #else
  488. interrupt_controller_hal_set_int_handler(intr, handler, arg);
  489. #endif
  490. }
  491. if (flags & ESP_INTR_FLAG_EDGE) {
  492. interrupt_controller_hal_edge_int_acknowledge(intr);
  493. }
  494. vd->source=source;
  495. }
  496. if (flags&ESP_INTR_FLAG_IRAM) {
  497. vd->flags|=VECDESC_FL_INIRAM;
  498. non_iram_int_mask[cpu]&=~(1<<intr);
  499. } else {
  500. vd->flags&=~VECDESC_FL_INIRAM;
  501. non_iram_int_mask[cpu]|=(1<<intr);
  502. }
  503. if (source>=0) {
  504. intr_matrix_set(cpu, source, intr);
  505. }
  506. //Fill return handle data.
  507. ret->vector_desc=vd;
  508. ret->shared_vector_desc=vd->shared_vec_info;
  509. //Enable int at CPU-level;
  510. ESP_INTR_ENABLE(intr);
  511. //If interrupt has to be started disabled, do that now; ints won't be enabled for real until the end
  512. //of the critical section.
  513. if (flags&ESP_INTR_FLAG_INTRDISABLED) {
  514. esp_intr_disable(ret);
  515. }
  516. #ifdef SOC_CPU_HAS_FLEXIBLE_INTC
  517. //Extract the level from the interrupt passed flags
  518. int level = esp_intr_flags_to_level(flags);
  519. interrupt_controller_hal_set_int_level(intr, level);
  520. if (flags & ESP_INTR_FLAG_EDGE) {
  521. interrupt_controller_hal_set_int_type(intr, INTTP_EDGE);
  522. } else {
  523. interrupt_controller_hal_set_int_type(intr, INTTP_LEVEL);
  524. }
  525. #endif
  526. portEXIT_CRITICAL(&spinlock);
  527. //Fill return handle if needed, otherwise free handle.
  528. if (ret_handle!=NULL) {
  529. *ret_handle=ret;
  530. } else {
  531. free(ret);
  532. }
  533. ESP_EARLY_LOGD(TAG, "Connected src %d to int %d (cpu %d)", source, intr, cpu);
  534. return ESP_OK;
  535. }
  536. esp_err_t esp_intr_alloc(int source, int flags, intr_handler_t handler, void *arg, intr_handle_t *ret_handle)
  537. {
  538. /*
  539. As an optimization, we can create a table with the possible interrupt status registers and masks for every single
  540. source there is. We can then add code here to look up an applicable value and pass that to the
  541. esp_intr_alloc_intrstatus function.
  542. */
  543. return esp_intr_alloc_intrstatus(source, flags, 0, 0, handler, arg, ret_handle);
  544. }
  545. esp_err_t IRAM_ATTR esp_intr_set_in_iram(intr_handle_t handle, bool is_in_iram)
  546. {
  547. if (!handle) return ESP_ERR_INVALID_ARG;
  548. vector_desc_t *vd = handle->vector_desc;
  549. if (vd->flags & VECDESC_FL_SHARED) {
  550. return ESP_ERR_INVALID_ARG;
  551. }
  552. portENTER_CRITICAL(&spinlock);
  553. uint32_t mask = (1 << vd->intno);
  554. if (is_in_iram) {
  555. vd->flags |= VECDESC_FL_INIRAM;
  556. non_iram_int_mask[vd->cpu] &= ~mask;
  557. } else {
  558. vd->flags &= ~VECDESC_FL_INIRAM;
  559. non_iram_int_mask[vd->cpu] |= mask;
  560. }
  561. portEXIT_CRITICAL(&spinlock);
  562. return ESP_OK;
  563. }
  564. #if !CONFIG_FREERTOS_UNICORE
  565. static void esp_intr_free_cb(void *arg)
  566. {
  567. (void)esp_intr_free((intr_handle_t)arg);
  568. }
  569. #endif /* !CONFIG_FREERTOS_UNICORE */
  570. esp_err_t esp_intr_free(intr_handle_t handle)
  571. {
  572. bool free_shared_vector=false;
  573. if (!handle) return ESP_ERR_INVALID_ARG;
  574. #if !CONFIG_FREERTOS_UNICORE
  575. //Assign this routine to the core where this interrupt is allocated on.
  576. if (handle->vector_desc->cpu!=cpu_hal_get_core_id()) {
  577. esp_err_t ret = esp_ipc_call_blocking(handle->vector_desc->cpu, &esp_intr_free_cb, (void *)handle);
  578. return ret == ESP_OK ? ESP_OK : ESP_FAIL;
  579. }
  580. #endif /* !CONFIG_FREERTOS_UNICORE */
  581. portENTER_CRITICAL(&spinlock);
  582. esp_intr_disable(handle);
  583. if (handle->vector_desc->flags&VECDESC_FL_SHARED) {
  584. //Find and kill the shared int
  585. shared_vector_desc_t *svd=handle->vector_desc->shared_vec_info;
  586. shared_vector_desc_t *prevsvd=NULL;
  587. assert(svd); //should be something in there for a shared int
  588. while (svd!=NULL) {
  589. if (svd==handle->shared_vector_desc) {
  590. //Found it. Now kill it.
  591. if (prevsvd) {
  592. prevsvd->next=svd->next;
  593. } else {
  594. handle->vector_desc->shared_vec_info=svd->next;
  595. }
  596. free(svd);
  597. break;
  598. }
  599. prevsvd=svd;
  600. svd=svd->next;
  601. }
  602. //If nothing left, disable interrupt.
  603. if (handle->vector_desc->shared_vec_info==NULL) free_shared_vector=true;
  604. ESP_EARLY_LOGV(TAG, "esp_intr_free: Deleting shared int: %s. Shared int is %s", svd?"not found or last one":"deleted", free_shared_vector?"empty now.":"still in use");
  605. }
  606. if ((handle->vector_desc->flags&VECDESC_FL_NONSHARED) || free_shared_vector) {
  607. ESP_EARLY_LOGV(TAG, "esp_intr_free: Disabling int, killing handler");
  608. #if CONFIG_APPTRACE_SV_ENABLE
  609. if (!free_shared_vector) {
  610. void *isr_arg = interrupt_controller_hal_get_int_handler_arg(handle->vector_desc->intno);
  611. if (isr_arg) {
  612. free(isr_arg);
  613. }
  614. }
  615. #endif
  616. //Reset to normal handler:
  617. interrupt_controller_hal_set_int_handler(handle->vector_desc->intno, NULL, (void*)((int)handle->vector_desc->intno));
  618. //Theoretically, we could free the vector_desc... not sure if that's worth the few bytes of memory
  619. //we save.(We can also not use the same exit path for empty shared ints anymore if we delete
  620. //the desc.) For now, just mark it as free.
  621. handle->vector_desc->flags&=!(VECDESC_FL_NONSHARED|VECDESC_FL_RESERVED);
  622. //Also kill non_iram mask bit.
  623. non_iram_int_mask[handle->vector_desc->cpu]&=~(1<<(handle->vector_desc->intno));
  624. }
  625. portEXIT_CRITICAL(&spinlock);
  626. free(handle);
  627. return ESP_OK;
  628. }
  629. int esp_intr_get_intno(intr_handle_t handle)
  630. {
  631. return handle->vector_desc->intno;
  632. }
  633. int esp_intr_get_cpu(intr_handle_t handle)
  634. {
  635. return handle->vector_desc->cpu;
  636. }
  637. /*
  638. Interrupt disabling strategy:
  639. If the source is >=0 (meaning a muxed interrupt), we disable it by muxing the interrupt to a non-connected
  640. interrupt. If the source is <0 (meaning an internal, per-cpu interrupt), we disable it using ESP_INTR_DISABLE.
  641. This allows us to, for the muxed CPUs, disable an int from the other core. It also allows disabling shared
  642. interrupts.
  643. */
  644. //Muxing an interrupt source to interrupt 6, 7, 11, 15, 16 or 29 cause the interrupt to effectively be disabled.
  645. #define INT_MUX_DISABLED_INTNO 6
  646. esp_err_t IRAM_ATTR esp_intr_enable(intr_handle_t handle)
  647. {
  648. if (!handle) return ESP_ERR_INVALID_ARG;
  649. portENTER_CRITICAL_SAFE(&spinlock);
  650. int source;
  651. if (handle->shared_vector_desc) {
  652. handle->shared_vector_desc->disabled=0;
  653. source=handle->shared_vector_desc->source;
  654. } else {
  655. source=handle->vector_desc->source;
  656. }
  657. if (source >= 0) {
  658. //Disabled using int matrix; re-connect to enable
  659. intr_matrix_set(handle->vector_desc->cpu, source, handle->vector_desc->intno);
  660. } else {
  661. //Re-enable using cpu int ena reg
  662. if (handle->vector_desc->cpu!=cpu_hal_get_core_id()) return ESP_ERR_INVALID_ARG; //Can only enable these ints on this cpu
  663. ESP_INTR_ENABLE(handle->vector_desc->intno);
  664. }
  665. portEXIT_CRITICAL_SAFE(&spinlock);
  666. return ESP_OK;
  667. }
  668. esp_err_t IRAM_ATTR esp_intr_disable(intr_handle_t handle)
  669. {
  670. if (!handle) return ESP_ERR_INVALID_ARG;
  671. portENTER_CRITICAL_SAFE(&spinlock);
  672. int source;
  673. bool disabled = 1;
  674. if (handle->shared_vector_desc) {
  675. handle->shared_vector_desc->disabled=1;
  676. source=handle->shared_vector_desc->source;
  677. shared_vector_desc_t *svd=handle->vector_desc->shared_vec_info;
  678. assert( svd != NULL );
  679. while( svd ) {
  680. if ( svd->source == source && svd->disabled == 0 ) {
  681. disabled = 0;
  682. break;
  683. }
  684. svd = svd->next;
  685. }
  686. } else {
  687. source=handle->vector_desc->source;
  688. }
  689. if (source >= 0) {
  690. if ( disabled ) {
  691. //Disable using int matrix
  692. intr_matrix_set(handle->vector_desc->cpu, source, INT_MUX_DISABLED_INTNO);
  693. }
  694. } else {
  695. //Disable using per-cpu regs
  696. if (handle->vector_desc->cpu!=cpu_hal_get_core_id()) {
  697. portEXIT_CRITICAL_SAFE(&spinlock);
  698. return ESP_ERR_INVALID_ARG; //Can only enable these ints on this cpu
  699. }
  700. ESP_INTR_DISABLE(handle->vector_desc->intno);
  701. }
  702. portEXIT_CRITICAL_SAFE(&spinlock);
  703. return ESP_OK;
  704. }
  705. void IRAM_ATTR esp_intr_noniram_disable(void)
  706. {
  707. portENTER_CRITICAL_SAFE(&spinlock);
  708. uint32_t oldint;
  709. uint32_t cpu = cpu_hal_get_core_id();
  710. uint32_t non_iram_ints = non_iram_int_mask[cpu];
  711. if (non_iram_int_disabled_flag[cpu]) {
  712. abort();
  713. }
  714. non_iram_int_disabled_flag[cpu] = true;
  715. oldint = interrupt_controller_hal_read_interrupt_mask();
  716. interrupt_controller_hal_disable_interrupts(non_iram_ints);
  717. // Save disabled ints
  718. non_iram_int_disabled[cpu] = oldint & non_iram_ints;
  719. portEXIT_CRITICAL_SAFE(&spinlock);
  720. }
  721. void IRAM_ATTR esp_intr_noniram_enable(void)
  722. {
  723. portENTER_CRITICAL_SAFE(&spinlock);
  724. uint32_t cpu = cpu_hal_get_core_id();
  725. int non_iram_ints = non_iram_int_disabled[cpu];
  726. if (!non_iram_int_disabled_flag[cpu]) {
  727. abort();
  728. }
  729. non_iram_int_disabled_flag[cpu] = false;
  730. interrupt_controller_hal_enable_interrupts(non_iram_ints);
  731. portEXIT_CRITICAL_SAFE(&spinlock);
  732. }
  733. //These functions are provided in ROM, but the ROM-based functions use non-multicore-capable
  734. //virtualized interrupt levels. Thus, we disable them in the ld file and provide working
  735. //equivalents here.
  736. void IRAM_ATTR ets_isr_unmask(uint32_t mask) {
  737. interrupt_controller_hal_enable_interrupts(mask);
  738. }
  739. void IRAM_ATTR ets_isr_mask(uint32_t mask) {
  740. interrupt_controller_hal_disable_interrupts(mask);
  741. }
  742. void esp_intr_enable_source(int inum)
  743. {
  744. interrupt_controller_hal_enable_interrupts(1 << inum);
  745. }
  746. void esp_intr_disable_source(int inum)
  747. {
  748. interrupt_controller_hal_disable_interrupts(1 << inum);
  749. }