intr_alloc.c 30 KB

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