1 /*
2  * Copyright (c) 2023 Antmicro
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <zephyr/ztest.h>
9 
10 uint32_t local_irq_to_reg_index(uint32_t local_irq);
11 uint32_t local_irq_to_reg_offset(uint32_t local_irq);
12 
13 ZTEST_SUITE(intc_plic, NULL, NULL, NULL, NULL, NULL);
14 
15 /* Test calculating the register index from a local IRQ number */
ZTEST(intc_plic,test_local_irq_to_reg_index)16 ZTEST(intc_plic, test_local_irq_to_reg_index)
17 {
18 	zassert_equal(0, local_irq_to_reg_index(0x1f));
19 	zassert_equal(1, local_irq_to_reg_index(0x20));
20 	zassert_equal(1, local_irq_to_reg_index(0x3f));
21 	zassert_equal(2, local_irq_to_reg_index(0x40));
22 }
23 
24 /* Test calculating the register offset from a local IRQ number */
ZTEST(intc_plic,test_local_irq_to_reg_offset)25 ZTEST(intc_plic, test_local_irq_to_reg_offset)
26 {
27 	zassert_equal(0, local_irq_to_reg_offset(0x1f));
28 	zassert_equal(4, local_irq_to_reg_offset(0x20));
29 	zassert_equal(4, local_irq_to_reg_offset(0x3f));
30 	zassert_equal(8, local_irq_to_reg_offset(0x40));
31 }
32 
ZTEST(intc_plic,test_hart_context_mapping)33 ZTEST(intc_plic, test_hart_context_mapping)
34 {
35 	extern const uint32_t plic_hart_contexts_0[];
36 
37 	if (!IS_ENABLED(CONFIG_TEST_INTC_PLIC_ALT_MAPPING)) {
38 		/* Based on the default qemu_riscv64 devicetree */
39 		zassert_equal(plic_hart_contexts_0[0], 0);
40 		zassert_equal(plic_hart_contexts_0[1], 2);
41 		zassert_equal(plic_hart_contexts_0[2], 4);
42 		zassert_equal(plic_hart_contexts_0[3], 6);
43 		zassert_equal(plic_hart_contexts_0[4], 8);
44 		zassert_equal(plic_hart_contexts_0[5], 10);
45 		zassert_equal(plic_hart_contexts_0[6], 12);
46 		zassert_equal(plic_hart_contexts_0[7], 14);
47 	} else {
48 		/* Based on the definition in the `alt_mapping.overlay` */
49 		zassert_equal(plic_hart_contexts_0[0], 0);
50 		zassert_equal(plic_hart_contexts_0[1], 1);
51 		zassert_equal(plic_hart_contexts_0[2], 3);
52 		zassert_equal(plic_hart_contexts_0[3], 5);
53 		zassert_equal(plic_hart_contexts_0[4], 7);
54 		zassert_equal(plic_hart_contexts_0[5], 9);
55 		zassert_equal(plic_hart_contexts_0[6], 11);
56 		zassert_equal(plic_hart_contexts_0[7], 13);
57 	}
58 }
59