forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp_irq.c
682 lines (539 loc) · 17.5 KB
/
esp_irq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/****************************************************************************
* arch/risc-v/src/common/espressif/esp_irq.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <debug.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include "riscv_internal.h"
#include "esp_gpio.h"
#include "esp_irq.h"
#include "esp_attr.h"
#include "esp_bit_defs.h"
#include "esp_cpu.h"
#include "esp_rom_sys.h"
#include "riscv/interrupt.h"
#include "soc/soc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define ESP_DEFAULT_INT_THRESHOLD 1
#define IRQ_UNMAPPED 0xff
/* Helper macros for working with cpuint_mapentry_t fields */
#define CPUINT_DISABLE(cpuint) ((cpuint).cpuint_en = 0)
#define CPUINT_ENABLE(cpuint) ((cpuint).cpuint_en = 1)
#define CPUINT_ASSIGN(cpuint,in_irq) do \
{ \
(cpuint).assigned = 1; \
(cpuint).cpuint_en = 1; \
(cpuint).irq = (in_irq); \
} \
while(0)
#define CPUINT_GETIRQ(cpuint) ((cpuint).irq)
#define CPUINT_FREE(cpuint) ((cpuint).val = 0)
#define CPUINT_ISENABLED(cpuint) ((cpuint).cpuint_en == 1)
#define CPUINT_ISASSIGNED(cpuint) ((cpuint).assigned == 1)
#define CPUINT_ISFREE(cpuint) (!CPUINT_ISASSIGNED(cpuint))
/* CPU interrupts can be detached from any interrupt source by setting the
* map register to ETS_INVALID_INUM, which is an invalid CPU interrupt index.
*/
#define NO_CPUINT ETS_INVALID_INUM
/****************************************************************************
* Private Types
****************************************************************************/
/* CPU interrupts to IRQ index mapping */
typedef union cpuint_mapentry_u
{
struct
{
uint16_t assigned:1;
uint16_t cpuint_en:1;
uint16_t irq:10;
uint16_t reserved0:4;
};
uint16_t val;
} cpuint_mapentry_t;
/****************************************************************************
* Private Data
****************************************************************************/
/* Map a CPU interrupt to the IRQ of the attached interrupt source */
static cpuint_mapentry_t g_cpuint_map[ESP_NCPUINTS];
/* Map an IRQ to a CPU interrupt to which the interrupt source is
* attached to.
*/
static volatile uint8_t g_irq_map[NR_IRQS];
/* Bitsets for free, unallocated CPU interrupts available to peripheral
* devices.
*/
static uint32_t g_cpuint_freelist = ESP_CPUINT_PERIPHSET;
/* This bitmask has an 1 if the int should be disabled
* when the flash is disabled.
*/
static uint32_t non_iram_int_mask[CONFIG_ESPRESSIF_NUM_CPUS];
/* This bitmask has 1 in it if the int was disabled
* using esp_intr_noniram_disable.
*/
static uint32_t non_iram_int_disabled[CONFIG_ESPRESSIF_NUM_CPUS];
static bool non_iram_int_disabled_flag[CONFIG_ESPRESSIF_NUM_CPUS];
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: esp_cpuint_alloc
*
* Description:
* Allocate a free CPU interrupt for a peripheral device. This function
* will not ignore all of the pre-allocated CPU interrupts for internal
* devices.
*
* Input Parameters:
* irq - IRQ number.
*
* Returned Value:
* On success, a CPU interrupt number is returned.
* A negated errno is returned on failure.
*
****************************************************************************/
static int esp_cpuint_alloc(int irq)
{
uint32_t bitmask;
uint32_t intset;
int cpuint = ESP_NCPUINTS;
/* Check if there are CPU interrupts with the requested properties
* available.
*/
intset = g_cpuint_freelist;
if (intset != 0)
{
/* Skip over initial unavailable CPU interrupts quickly in groups
* of 8 interrupt.
*/
for (cpuint = 0, bitmask = 0xff;
cpuint < ESP_NCPUINTS && (intset & bitmask) == 0;
cpuint += 8, bitmask <<= 8);
/* Search for an unallocated CPU interrupt number in the remaining
* intset.
*/
for (; cpuint < ESP_NCPUINTS; cpuint++)
{
/* If the bit corresponding to the CPU interrupt is '1', then
* that CPU interrupt is available.
*/
bitmask = BIT(cpuint);
if ((intset & bitmask) != 0)
{
/* Got it! */
g_cpuint_freelist &= ~bitmask;
break;
}
}
}
if (cpuint == ESP_NCPUINTS)
{
/* No unallocated CPU interrupt found */
return -ENOMEM;
}
DEBUGASSERT(CPUINT_ISFREE(g_cpuint_map[cpuint]));
CPUINT_ASSIGN(g_cpuint_map[cpuint], irq);
g_irq_map[irq] = cpuint;
return cpuint;
}
/****************************************************************************
* Name: esp_cpuint_free
*
* Description:
* Free a previously allocated CPU interrupt.
*
* Input Parameters:
* cpuint - CPU interrupt to be freed.
*
* Returned Value:
* None.
*
****************************************************************************/
static void esp_cpuint_free(int cpuint)
{
uint32_t bitmask;
DEBUGASSERT(cpuint >= 0 && cpuint < ESP_NCPUINTS);
DEBUGASSERT(CPUINT_ISASSIGNED(g_cpuint_map[cpuint]));
int irq = CPUINT_GETIRQ(g_cpuint_map[cpuint]);
g_irq_map[irq] = IRQ_UNMAPPED;
CPUINT_FREE(g_cpuint_map[cpuint]);
/* Mark the CPU interrupt as available */
bitmask = BIT(cpuint);
DEBUGASSERT((g_cpuint_freelist & bitmask) == 0);
g_cpuint_freelist |= bitmask;
}
/****************************************************************************
* Name: esp_cpuint_initialize
*
* Description:
* Initialize CPU interrupts.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
static void esp_cpuint_initialize(void)
{
/* Unmap CPU interrupts from every interrupt source */
for (int source = 0; source < ESP_NSOURCES; source++)
{
esp_rom_route_intr_matrix(PRO_CPU_NUM, source, NO_CPUINT);
}
/* Set CPU interrupt threshold level */
esprv_intc_int_set_threshold(ESP_DEFAULT_INT_THRESHOLD);
/* Indicate that no interrupt sources are assigned to CPU interrupts */
memset(g_cpuint_map, 0, sizeof(g_cpuint_map));
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_irqinitialize
*
* Description:
* Complete initialization of the interrupt system and enable normal,
* interrupt processing.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
void up_irqinitialize(void)
{
/* All CPU ints are non-IRAM interrupts at the beginning and should be
* disabled during a SPI flash operation
*/
for (int i = 0; i < CONFIG_SMP_NCPUS; i++)
{
non_iram_int_mask[i] = UINT32_MAX;
}
/* Indicate that no interrupt sources are assigned to CPU interrupts */
for (int i = 0; i < NR_IRQS; i++)
{
g_irq_map[i] = IRQ_UNMAPPED;
}
/* Initialize CPU interrupts */
esp_cpuint_initialize();
/* Initialize GPIO interrupt support */
#ifdef CONFIG_ESPRESSIF_GPIO_IRQ
esp_gpioirqinitialize();
#endif
/* Attach the common interrupt handler */
riscv_exception_attach();
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* And finally, enable interrupts */
up_irq_enable();
#endif
}
/****************************************************************************
* Name: up_enable_irq
*
* Description:
* Enable the interrupt specified by 'irq'.
*
* Input Parameters:
* irq - IRQ number.
*
* Returned Value:
* None.
*
****************************************************************************/
void up_enable_irq(int irq)
{
int cpuint = g_irq_map[irq];
irqinfo("irq=%d | cpuint=%d \n", irq, cpuint);
/* Check if IRQ is initialized */
DEBUGASSERT(cpuint >= 0 && cpuint < ESP_NCPUINTS);
irqstate_t irqstate = enter_critical_section();
CPUINT_ENABLE(g_cpuint_map[cpuint]);
esprv_intc_int_enable(BIT(cpuint));
leave_critical_section(irqstate);
}
/****************************************************************************
* Name: up_disable_irq
*
* Description:
* Disable the interrupt specified by 'irq'.
*
* Input Parameters:
* irq - IRQ number.
*
* Returned Value:
* None.
*
****************************************************************************/
void up_disable_irq(int irq)
{
int cpuint = g_irq_map[irq];
irqinfo("irq=%d | cpuint=%d \n", irq, cpuint);
/* Check if IRQ is initialized */
DEBUGASSERT(cpuint >= 0 && cpuint < ESP_NCPUINTS);
irqstate_t irqstate = enter_critical_section();
CPUINT_DISABLE(g_cpuint_map[cpuint]);
esprv_intc_int_disable(BIT(cpuint));
leave_critical_section(irqstate);
}
/****************************************************************************
* Name: esp_route_intr
*
* Description:
* Assign an interrupt source to a pre-allocated CPU interrupt.
*
* Input Parameters:
* source - Interrupt source (see irq.h) to be assigned to a CPU
* interrupt.
* cpuint - Pre-allocated CPU interrupt to which the interrupt
* source will be assigned.
* priority - Interrupt priority.
* type - Interrupt trigger type.
*
* Returned Value:
* None.
*
****************************************************************************/
void esp_route_intr(int source, int cpuint, irq_priority_t priority,
irq_trigger_t type)
{
/* Ensure the CPU interrupt is disabled */
esprv_intc_int_disable(BIT(cpuint));
/* Set the interrupt priority */
esprv_intc_int_set_priority(cpuint, priority);
/* Set the interrupt trigger type (Edge or Level) */
if (type == ESP_IRQ_TRIGGER_EDGE)
{
esprv_intc_int_set_type(cpuint, INTR_TYPE_EDGE);
}
else
{
esprv_intc_int_set_type(cpuint, INTR_TYPE_LEVEL);
}
/* Route the interrupt source to the provided CPU interrupt */
esp_rom_route_intr_matrix(PRO_CPU_NUM, source, cpuint);
}
/****************************************************************************
* Name: esp_setup_irq
*
* Description:
* Configure an IRQ. It allocates a CPU interrupt of the given
* priority and type and attaches a given interrupt source to it.
*
* Input Parameters:
* source - Interrupt source (see irq.h) to be assigned to
* a CPU interrupt.
* priority - Interrupt priority.
* type - Interrupt trigger type.
*
* Returned Value:
* Allocated CPU interrupt.
*
****************************************************************************/
int esp_setup_irq(int source, irq_priority_t priority, irq_trigger_t type)
{
irqstate_t irqstate;
int irq;
int cpuint;
irqinfo("source = %d\n", source);
DEBUGASSERT(source >= 0 && source < ESP_NSOURCES);
irqstate = enter_critical_section();
/* Setting up an IRQ includes the following steps:
* 1. Allocate a CPU interrupt.
* 2. Map the CPU interrupt to the IRQ to ease searching later.
* 3. Attach the interrupt source to the newly allocated CPU interrupt.
*/
irq = ESP_SOURCE2IRQ(source);
cpuint = esp_cpuint_alloc(irq);
if (cpuint < 0)
{
_alert("Unable to allocate CPU interrupt for source=%d\n",
priority, type);
PANIC();
}
esp_route_intr(source, cpuint, priority, type);
leave_critical_section(irqstate);
return cpuint;
}
/****************************************************************************
* Name: esp_teardown_irq
*
* Description:
* This function undoes the operations done by esp_setup_irq.
* It detaches an interrupt source from a CPU interrupt and frees the
* CPU interrupt.
*
* Input Parameters:
* source - Interrupt source (see irq.h) to be detached from the
* CPU interrupt.
* cpuint - CPU interrupt from which the interrupt source will
* be detached.
*
* Returned Value:
* None.
*
****************************************************************************/
void esp_teardown_irq(int source, int cpuint)
{
irqstate_t irqstate = enter_critical_section();
/* Tearing down an IRQ includes the following steps:
* 1. Free the previously allocated CPU interrupt.
* 2. Unmap the IRQ from the IRQ-to-cpuint map.
* 3. Detach the interrupt source from the CPU interrupt.
*/
esp_cpuint_free(cpuint);
DEBUGASSERT(source >= 0 && source < ESP_NSOURCES);
esp_rom_route_intr_matrix(PRO_CPU_NUM, source, NO_CPUINT);
leave_critical_section(irqstate);
}
/****************************************************************************
* Name: riscv_dispatch_irq
*
* Description:
* Process interrupt and its callback function.
*
* Input Parameters:
* mcause - RISC-V "mcause" register.
* regs - Saved registers reference.
*
* Returned Value:
* None.
*
****************************************************************************/
IRAM_ATTR uintptr_t *riscv_dispatch_irq(uintptr_t mcause, uintptr_t *regs)
{
int irq;
bool is_irq = (RISCV_IRQ_BIT & mcause) != 0;
bool is_edge = false;
if (is_irq)
{
uint8_t cpuint = mcause & RISCV_IRQ_MASK;
DEBUGASSERT(cpuint >= 0 && cpuint < ESP_NCPUINTS);
DEBUGASSERT(CPUINT_ISENABLED(g_cpuint_map[cpuint]));
DEBUGASSERT(CPUINT_ISASSIGNED(g_cpuint_map[cpuint]));
irq = g_cpuint_map[cpuint].irq;
is_edge = esprv_intc_int_get_type(cpuint) == INTR_TYPE_EDGE;
if (is_edge)
{
/* Clear edge interrupts. */
esp_cpu_intr_edge_ack(cpuint);
}
}
else
{
/* It's exception */
irq = mcause;
}
regs = riscv_doirq(irq, regs);
return regs;
}
/****************************************************************************
* Name: up_irq_enable
*
* Description:
* Enable interrupts globally.
*
* Input Parameters:
* None.
*
* Returned Value:
* The interrupt state prior to enabling interrupts.
*
****************************************************************************/
irqstate_t up_irq_enable(void)
{
irqstate_t flags;
/* Read mstatus & set machine interrupt enable (MIE) in mstatus */
flags = READ_AND_SET_CSR(mstatus, MSTATUS_MIE);
return flags;
}
/****************************************************************************
* Name: esp_intr_noniram_disable
*
* Description:
* Disable interrupts that aren't specifically marked as running from IRAM.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
void IRAM_ATTR esp_intr_noniram_disable(void)
{
uint32_t oldint;
irqstate_t irqstate;
uint32_t cpu;
uint32_t non_iram_ints;
irqstate = enter_critical_section();
cpu = esp_cpu_get_core_id();
non_iram_ints = non_iram_int_mask[cpu];
if (non_iram_int_disabled_flag[cpu])
{
abort();
}
non_iram_int_disabled_flag[cpu] = true;
oldint = esp_cpu_intr_get_enabled_mask();
esp_cpu_intr_disable(non_iram_ints);
/* Save disabled ints */
non_iram_int_disabled[cpu] = oldint & non_iram_ints;
leave_critical_section(irqstate);
}
/****************************************************************************
* Name: esp_intr_noniram_enable
*
* Description:
* Enable interrupts that aren't specifically marked as running from IRAM.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
void IRAM_ATTR esp_intr_noniram_enable(void)
{
irqstate_t irqstate;
uint32_t cpu;
int non_iram_ints;
irqstate = enter_critical_section();
cpu = esp_cpu_get_core_id();
non_iram_ints = non_iram_int_disabled[cpu];
if (!non_iram_int_disabled_flag[cpu])
{
abort();
}
non_iram_int_disabled_flag[cpu] = false;
esp_cpu_intr_enable(non_iram_ints);
leave_critical_section(irqstate);
}