1 /*
2  * Copyright (c) 2024 Meta Platforms
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/devicetree.h>
8 #include <zephyr/irq.h>
9 #include <zephyr/irq_multilevel.h>
10 #include <zephyr/ztest.h>
11 
ZTEST(interrupt_feature,test_multi_level_api)12 ZTEST(interrupt_feature, test_multi_level_api)
13 {
14 	/* Zephyr multilevel-encoded IRQ */
15 	const uint32_t irqn_l3 = DT_IRQN(DT_NODELABEL(test_l3_irq));
16 	const uint32_t irqn_l2 = DT_IRQN(DT_NODELABEL(test_l2_irq));
17 	const uint32_t irqn_l1 = DT_IRQN(DT_NODELABEL(test_l1_irq));
18 	/* Raw IRQ specified in the devicetree */
19 	const uint32_t raw_l3 = DT_IRQ(DT_NODELABEL(test_l3_irq), irq);
20 	const uint32_t raw_l2 = DT_IRQ(DT_NODELABEL(test_l2_irq), irq);
21 	const uint32_t raw_l1 = DT_IRQ(DT_NODELABEL(test_l1_irq), irq);
22 
23 	/**
24 	 *  - irq_get_level()
25 	 */
26 	zassert_equal(3, irq_get_level(irqn_l3));
27 	zassert_equal(2, irq_get_level(irqn_l2));
28 	zassert_equal(1, irq_get_level(irqn_l1));
29 
30 	/**
31 	 *  - irq_from_level_2()
32 	 *  - irq_to_level_2()
33 	 *  - irq_parent_level_2()
34 	 */
35 	zassert_equal(irq_from_level_2(irqn_l3), raw_l2);
36 	zassert_equal(irq_from_level_2(irqn_l2), raw_l2);
37 
38 	zassert_equal(irq_to_level_2(raw_l2) & irqn_l2, irq_to_level_2(raw_l2));
39 
40 	zassert_equal(irq_parent_level_2(irqn_l2), raw_l1);
41 
42 	/**
43 	 *  - irq_from_level_3()
44 	 *  - irq_to_level_3()
45 	 *  - irq_parent_level_3()
46 	 */
47 	zassert_equal(irq_from_level_3(irqn_l3), raw_l3);
48 
49 	zassert_equal(irq_to_level_3(raw_l3) & irqn_l3, irq_to_level_3(raw_l3));
50 
51 	zassert_equal(irq_parent_level_3(irqn_l3), raw_l2);
52 
53 	/**
54 	 *  - irq_from_level()
55 	 *  - irq_to_level()
56 	 *  - irq_parent_level()
57 	 */
58 	zassert_equal(irq_from_level(irqn_l3, 2), raw_l2);
59 	zassert_equal(irq_from_level(irqn_l2, 2), raw_l2);
60 	zassert_equal(irq_from_level(irqn_l3, 3), raw_l3);
61 
62 	zassert_equal(irq_to_level(raw_l2, 2) & irqn_l2, irq_to_level(raw_l2, 2));
63 	zassert_equal(irq_to_level(raw_l3, 3) & irqn_l3, irq_to_level(raw_l3, 3));
64 
65 	zassert_equal(irq_parent_level(irqn_l2, 2), raw_l1);
66 	zassert_equal(irq_parent_level(irqn_l3, 3), raw_l2);
67 
68 	/**
69 	 *  - irq_get_intc_irq()
70 	 */
71 	zassert_equal(irq_get_intc_irq(irqn_l3), irqn_l2);
72 	zassert_equal(irq_get_intc_irq(irqn_l2), irqn_l1);
73 	zassert_equal(irq_get_intc_irq(irqn_l1), irqn_l1);
74 
75 	const uint32_t irqn_l3_inc = DT_IRQN(DT_NODELABEL(test_l3_irq_inc));
76 	const uint32_t irqn_l2_inc = DT_IRQN(DT_NODELABEL(test_l2_irq_inc));
77 	const uint32_t irqn_l1_inc = DT_IRQN(DT_NODELABEL(test_l1_irq_inc));
78 
79 	/**
80 	 * - irq_increment()
81 	 */
82 	zassert_equal(irq_increment(irqn_l1, 1), irqn_l1_inc);
83 	zassert_equal(irq_increment(irqn_l2, 2), irqn_l2_inc);
84 	zassert_equal(irq_increment(irqn_l3, 3), irqn_l3_inc);
85 }
86 
87 ZTEST_SUITE(gen_isr_table_multilevel, NULL, NULL, NULL, NULL, NULL);
88