intr_alloc.c 30 KB

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