intr_alloc.c 32 KB

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