Skip to content

Commit 89084d2

Browse files
committed
baremetal aarch64: timer.c get closer to working
1 parent b3874cc commit 89084d2

File tree

5 files changed

+60
-36
lines changed

5 files changed

+60
-36
lines changed

README.adoc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14156,6 +14156,15 @@ TODO: create and study a minimal examples in gem5 where the DMB instruction lead
1415614156

1415714157
TODO get working. Attempt at: link:baremetal/arch/aarch64/timer.c[]
1415814158

14159+
The timer is documented at: <<armarm8-db>> Chapter D10 "The Generic Timer in AArch64 state"
14160+
14161+
The key registers to keep in mind are:
14162+
14163+
* `CNTVCT_EL0`: "Counter-timer Virtual Count register". The increasing current counter value.
14164+
* `CNTFRQ_EL0`: "Counter-timer Frequency register". "Indicates the system counter clock frequency, in Hz."
14165+
* `CNTV_CTL_EL0`: "Counter-timer Virtual Timer Control register"
14166+
* `CNTV_CVAL_EL0`: "Counter-timer Virtual Timer CompareValue register". The interrupt happens when `CNTVCT_EL0` reaches the value in this register.
14167+
1415914168
==== ARM baremetal bibliography
1416014169

1416114170
First, also consider the userland bibliography: <<arm-assembly-bibliography>>.
@@ -14208,7 +14217,7 @@ which further confirms that the exception is correct: v2 has a register range at
1420814217

1420914218
The original source does not mention GICv3 anywhere, only link:https://github.com/takeharukato/sample-tsk-sw/blob/c7bbc9dce6b14660bcce8d20735f8c6ebb09396b/hal/aarch64/gic-pl390.c[pl390], which is a specific GIC model that predates the GICv2 spec I believe.
1421014219

14211-
TODO I hacked `#define GIC_GICC_BASE (GIC_BASE + 0xa0000)` and now continuing attempt.
14220+
TODO if I hack `#define GIC_GICC_BASE (GIC_BASE + 0xa0000)`, then it goes a bit further, but the next loop never ends.
1421214221

1421314222
===== tukl-msd/gem5.bare-metal
1421414223

baremetal/arch/aarch64/timer.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,62 @@
44
#include <lkmc.h>
55
#include <lkmc/gicv3.h>
66

7-
#define CNTV_CTL_ENABLE (1 << 0)
8-
#define CNTV_CTL_IMASK (1 << 1)
9-
#define CNTV_CTL_ISTATUS (1 << 2)
10-
11-
void cntv_ctl_el0_disable(void) {
12-
lkmc_sysreg_cntv_ctl_el0_write(lkmc_sysreg_cntv_ctl_el0_read() & ~CNTV_CTL_ENABLE);
7+
void lkmc_vector_trap_handler(LkmcVectorExceptionFrame *exception __attribute__((unused))) {
8+
printf("CNTVCT_EL0 0x%" PRIx64 "\n", lkmc_sysreg_cntvct_el0_read());
139
}
1410

15-
/* If enabled, when: cntv_ctl > cntv_cval then:
16-
*
17-
* * if CNTV_CTL_IMASK is clear, raise an interrupt
18-
* * set CNTV_CTL_ISTATUS
19-
*/
20-
void cntv_ctl_el0_enable(void) {
11+
#define CNTV_CTL_ENABLE (1 << 0) /* Enables the timer */
12+
#define CNTV_CTL_IMASK (1 << 1) /* Timer interrupt mask bit */
13+
#define CNTV_CTL_ISTATUS (1 << 2) /* The status of the timer interrupt. This bit is read-only */
14+
15+
/* DAIF, Interrupt Mask Bits */
16+
#define DAIF_DBG_BIT (1<<3) /* Debug mask bit */
17+
#define DAIF_ABT_BIT (1<<2) /* Asynchronous abort mask bit */
18+
#define DAIF_IRQ_BIT (1<<1) /* IRQ mask bit */
19+
#define DAIF_FIQ_BIT (1<<0) /* FIQ mask bit */
20+
21+
#define wfi() __asm__ __volatile__ ("wfi" : : : "memory")
22+
23+
void enable_cntv(void) {
2124
lkmc_sysreg_cntv_ctl_el0_write(lkmc_sysreg_cntv_ctl_el0_read() | CNTV_CTL_ENABLE);
2225
}
2326

24-
25-
void lkmc_vector_trap_handler(LkmcVectorExceptionFrame *exception __attribute__((unused))) {
27+
void enable_irq(void) {
28+
__asm__ __volatile__ ("msr DAIFClr, %0" : : "i" (DAIF_IRQ_BIT) : "memory");
2629
}
2730

2831
int main(void) {
2932
/* Initial state. */
30-
printf("cntv_ctl_el0 0x%" PRIx32 "\n", lkmc_sysreg_cntv_ctl_el0_read());
31-
printf("cntfrq_el0 0x%" PRIx64 "\n", lkmc_sysreg_cntfrq_el0_read());
32-
printf("cntv_cval_el0 0x%" PRIx64 "\n", lkmc_sysreg_cntv_cval_el0_read());
33+
printf("CNTV_CTL_EL0 0x%" PRIx32 "\n", lkmc_sysreg_cntv_ctl_el0_read());
34+
printf("CNTFRQ_EL0 0x%" PRIx64 "\n", lkmc_sysreg_cntfrq_el0_read());
35+
printf("CNTV_CVAL_EL0 0x%" PRIx64 "\n", lkmc_sysreg_cntv_cval_el0_read());
3336

3437
/* Get the counter value many times to watch the time pass. */
35-
printf("cntvct_el0 0x%" PRIx64 "\n", lkmc_sysreg_cntvct_el0_read());
36-
printf("cntvct_el0 0x%" PRIx64 "\n", lkmc_sysreg_cntvct_el0_read());
37-
printf("cntvct_el0 0x%" PRIx64 "\n", lkmc_sysreg_cntvct_el0_read());
38+
printf("CNTVCT_EL0 0x%" PRIx64 "\n", lkmc_sysreg_cntvct_el0_read());
39+
printf("CNTVCT_EL0 0x%" PRIx64 "\n", lkmc_sysreg_cntvct_el0_read());
40+
printf("CNTVCT_EL0 0x%" PRIx64 "\n", lkmc_sysreg_cntvct_el0_read());
3841

39-
/*gic_v3_initialize();*/
42+
/**/
43+
gic_v3_initialize();
4044
{
41-
/*uint64_t ticks, current_cnt;*/
42-
/*uint32_t cntfrq;*/
43-
/*cntfrq = raw_read_cntfrq_el0();*/
44-
/*ticks = cntfrq;*/
45+
uint64_t ticks, current_cnt;
46+
uint32_t cntfrq;
47+
cntfrq = lkmc_sysreg_cntfrq_el0_read();
48+
ticks = cntfrq;
49+
current_cnt = lkmc_sysreg_cntvct_el0_read();
50+
lkmc_sysreg_cntv_cval_el0_write(current_cnt + ticks);
51+
enable_cntv();
52+
enable_irq();
53+
}
54+
while (1) {
55+
/*puts("qwer");*/
4556
/*current_cnt = raw_read_cntvct_el0();*/
46-
/*raw_write_cntv_cval_el0(current_cnt + ticks);*/
47-
/*enable_cntv();*/
48-
/*enable_irq();*/
57+
/*val = raw_read_cntv_ctl();*/
58+
/*printf("CNTVCT_EL0 = ");*/
59+
/*uart_puthex(current_cnt);*/
60+
/*uart_puts(", CNTV_CTL_EL0 = ");*/
61+
/*uart_puthex(val);*/
62+
/*wfi();*/
4963
}
5064

5165
#if 0

common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def join(*paths):
699699
# http://lists.nongnu.org/archive/html/qemu-discuss/2018-08/msg00034.html
700700
env['machine2'] = 'highmem=off'
701701
elif env['arch'] == 'aarch64':
702-
env['machine2'] = 'gic_version=3'
702+
env['machine2'] = None
703703
else:
704704
env['machine2'] = None
705705

lkmc/gicv3.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
typedef int32_t irq_no;
88

99
#define GIC_GICD_BASE (GIC_BASE)
10-
#define GIC_GICC_BASE (GIC_BASE + 0xa0000)
10+
#define GIC_GICC_BASE (GIC_BASE + 0x10000)
1111

1212
#define GIC_GICD_INT_PER_REG (32)
1313
#define GIC_GICD_IPRIORITY_PER_REG (4)
@@ -135,9 +135,11 @@ static void init_gicc(void) {
135135
*REG_GIC_GICC_BPR = GICC_BPR_NO_GROUP;
136136

137137
/* Clear all of the active interrupts */
138-
for(pending_irq = ( *REG_GIC_GICC_IAR & GICC_IAR_INTR_IDMASK );
139-
( pending_irq != GICC_IAR_SPURIOUS_INTR );
140-
pending_irq = ( *REG_GIC_GICC_IAR & GICC_IAR_INTR_IDMASK ) )
138+
for (
139+
pending_irq = (*REG_GIC_GICC_IAR & GICC_IAR_INTR_IDMASK);
140+
pending_irq != GICC_IAR_SPURIOUS_INTR;
141+
pending_irq = (*REG_GIC_GICC_IAR & GICC_IAR_INTR_IDMASK)
142+
)
141143
*REG_GIC_GICC_EOIR = *REG_GIC_GICC_IAR;
142144

143145
/* Enable CPU interface */
@@ -215,7 +217,6 @@ static int gicd_probe_pending(irq_no irq) {
215217

216218
is_pending = ( *REG_GIC_GICD_ISPENDR( (irq / GIC_GICD_ISPENDR_PER_REG) ) &
217219
( 1U << ( irq % GIC_GICD_ISPENDR_PER_REG ) ) );
218-
219220
return ( is_pending != 0 );
220221
}
221222

@@ -232,7 +233,6 @@ static void gicd_set_target(irq_no irq, uint32_t p){
232233
uint32_t reg;
233234

234235
shift = (irq % GIC_GICD_ITARGETSR_PER_REG) * GIC_GICD_ITARGETSR_SIZE_PER_REG;
235-
236236
reg = *REG_GIC_GICD_ITARGETSR(irq / GIC_GICD_ITARGETSR_PER_REG);
237237
reg &= ~( ((uint32_t)(0xff)) << shift);
238238
reg |= (p << shift);

path_properties.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def get(path):
303303
),
304304
'return1.S': {'exit_status': 1},
305305
'semihost_exit.S': {'requires_semihosting': True},
306+
'timer.c': {'skip_run_unclassified': True},
306307
},
307308
)
308309
}

0 commit comments

Comments
 (0)