Merge remote-tracking branch 'origin/master' into isa/arm64
[lunaix-os.git] / lunaix-os / arch / generic / includes / asm-generic / isrm.h
1 /**
2  * @file irqm.h
3  * @author Lunaixsky
4  * @brief ISR Manager, managing the interrupt service routine allocations
5  * @version 0.1
6  * @date 2022-10-18
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
11 #ifndef __LUNAIX_ISRM_H
12 #define __LUNAIX_ISRM_H
13
14 #include <lunaix/types.h>
15 #include <lunaix/hart_state.h>
16 #include <lunaix/device.h>
17
18 #include <hal/devtree.h>
19
20 typedef void (*isr_cb)(const struct hart_state*);
21
22 typedef struct {
23     ptr_t msi_addr;
24     reg_t msi_data;
25     int mapped_iv;
26 } msi_vector_t;
27 #define msi_addr(msiv)   ((msiv).msi_addr)
28 #define msi_data(msiv)   ((msiv).msi_data)
29 #define msi_vect(msiv)   ((msiv).mapped_iv)
30 #define check_msiv_invalid(msiv)  (msi_vect(msiv) == -1)
31 #define invalid_msi_vector  ((msi_vector_t) { (ptr_t)-1, (reg_t)-1, -1 });
32
33 typedef void* msienv_t;
34
35 void
36 isrm_init();
37
38 /**
39  * @brief Release a iv resource
40  *
41  * @param iv
42  */
43 void
44 isrm_ivfree(int iv);
45
46 /**
47  * @brief Begin MSI allocation for given device
48  *
49  * @param iv
50  */
51 msienv_t
52 isrm_msi_start(struct device* dev);
53
54 /**
55  * @brief Query number of msi avaliable for the device
56  */
57 int
58 isrm_msi_avaliable(msienv_t msienv);
59
60 /**
61  * @brief Allocate a msi resource within defined msi resource list
62  *        for the device, indexed by `index`
63  */
64 msi_vector_t
65 isrm_msi_alloc(msienv_t msienv, cpu_t cpu, int index, isr_cb handler);
66
67 /**
68  * @brief Set the sideband information will be used for upcoming
69  *        allocations
70  */
71 void
72 isrm_msi_set_sideband(msienv_t msienv, ptr_t sideband);
73
74 /**
75  * @brief Done MSI allocation
76  */
77 void
78 isrm_msi_done(msienv_t msienv);
79
80 static inline must_inline msi_vector_t
81 isrm_msi_alloc_simple(struct device* dev, cpu_t cpu, isr_cb handler)
82 {   
83     msi_vector_t v;
84     msienv_t env;
85
86     env = isrm_msi_start(dev);
87     v = isrm_msi_alloc(env, cpu, 0, handler);
88     isrm_msi_done(env);
89
90     return v;
91 }
92
93 /**
94  * @brief Bind the iv according to given device tree node
95  *
96  * @param node
97  */
98 int
99 isrm_bind_dtn(struct dt_intr_node* node);
100
101 /**
102  * @brief Get the handler associated with the given iv
103  *
104  * @param iv
105  * @return isr_cb
106  */
107 isr_cb
108 isrm_get(int iv);
109
110 ptr_t
111 isrm_get_payload(const struct hart_state*);
112
113 void
114 isrm_set_payload(int iv, ptr_t);
115
116 /**
117  * @brief Notify end of interrupt event
118  *
119  * @param id
120  */
121 void
122 isrm_notify_eoi(cpu_t id, int iv);
123
124 /**
125  * @brief Notify end of scheduling event
126  *
127  * @param id
128  */
129 void
130 isrm_notify_eos(cpu_t id);
131
132 #endif /* __LUNAIX_ISRM_H */