Merge remote-tracking branch 'origin/master' into isa/arm64
[lunaix-os.git] / lunaix-os / arch / aarch64 / includes / asm / soc / gic.h
1 #ifndef __LUNAIX_GIC_H
2 #define __LUNAIX_GIC_H
3
4 #include <lunaix/types.h>
5 #include <lunaix/ds/bitmap.h>
6 #include <lunaix/ds/hashtable.h>
7
8 #include <hal/devtree.h>
9
10 #include <asm/aa64_gic.h>
11 #include <asm-generic/isrm.h>
12
13 // nr of cpus, must be 1
14 #define NR_CPU      1
15 #define gic_bmp     PREP_BITMAP(gicreg_t, gic_intr, BMP_ORIENT_LSB)
16
17 #define INITID_SGI_BASE     0
18 #define INITID_SGI_END      15
19
20 #define INITID_PPI_BASE     16
21 #define INITID_PPI_END      31
22
23 #define INITID_SPI_BASE     32
24 #define INITID_SPI_END      1019
25
26 #define INITID_SPEC_BASE    1020
27 #define INITID_SPEC_END     1023
28
29 #define INITID_ePPI_BASE    1056
30 #define INITID_ePPI_END     1119
31
32 #define INITID_eSPI_BASE    4096
33 #define INITID_eSPI_END     5119
34
35 #define INITID_LPI_BASE     8192
36
37 #define INITID_SPI_NR       (INITID_SPEC_BASE - INITID_SPI_BASE)
38
39 enum gic_int_type
40 {
41     GIC_IDL,
42     GIC_LPI,
43     GIC_SPI,
44     GIC_PPI,
45     GIC_SGI,
46     GIC_RSV
47 } compact;
48
49 enum gic_tri_type
50 {
51     GIC_TRIG_EDGE,
52     GIC_TRIG_LEVEL
53 } compact;
54
55 enum gic_grp_type
56 {
57     GIC_G0,
58     GIC_G1S,
59     GIC_G1NS
60 } compact;
61
62 DECLARE_BITMAP(gic_bmp);
63
64 struct gic_int_param
65 {
66     enum gic_int_type class;
67     enum gic_tri_type trigger;
68     enum gic_grp_type group;
69     unsigned int priority;
70     unsigned int rel_intid;
71     int cpu_id;
72     bool as_nmi;
73     bool ext_range;
74 };
75
76 struct gic_intcfg {
77     enum gic_int_type class;
78     enum gic_tri_type trigger;
79     enum gic_grp_type group;
80     bool as_nmi;
81 };
82
83 struct gic_idomain
84 {
85     DECLARE_HASHTABLE(recs, 32);
86     BITMAP(gic_bmp) ivmap;
87     unsigned int base;
88     bool extended;
89 };
90
91 struct gic_interrupt
92 {
93     struct hlist_node node;
94
95     struct gic_idomain* domain;
96     
97     unsigned int intid;
98     struct gic_intcfg config;
99     
100     isr_cb handler;
101     void* payload;
102 };
103
104 struct gic_distributor
105 {
106     BITMAP(gic_bmp) group;
107     BITMAP(gic_bmp) grpmod;
108     BITMAP(gic_bmp) enable;
109     BITMAP(gic_bmp) disable;
110     BITMAP(gic_bmp) icfg;
111     BITMAP(gic_bmp) nmi;
112 };
113
114 struct gic_rd
115 {
116     gicreg_t base[FRAME_LEN];
117     gicreg_t sgi_base[FRAME_LEN];
118 } compact align(4);
119
120 struct gic_cpuif
121 {
122     gicreg_t base[FRAME_LEN];
123 } compact align(4);
124
125 #define gic_reg64(base, index) (*(gicreg64_t*)(&base[index]))
126 #define gic_regptr(base, index) (__ptr(&base[index]))
127
128 struct gic_pe
129 {
130     struct gic_interrupt* active;
131     reg_t iar_val;
132     unsigned int priority;
133
134     struct gic_rd* _rd;
135     struct gic_cpuif* _if;
136
137     struct gic_distributor rdist;
138
139     struct {
140         struct gic_idomain* local_ints;
141         struct gic_idomain* eppi;
142     } idomain;
143
144     struct {
145         unsigned int affinity;
146         unsigned int ppi_nr;
147         bool eppi_ready;
148     };
149 };
150
151 struct gic_its_regs
152 {
153     gicreg_t base[FRAME_LEN];        // control regs
154     gicreg_t trn_space[FRAME_LEN];   // translation space
155 } compact align(4);
156
157 struct gic_its_regs_v41
158 {
159     gicreg_t base[FRAME_LEN];        // control regs
160     gicreg_t trn_space[FRAME_LEN];   // translation space
161     gicreg_t vsgi_space[FRAME_LEN];  // vSGI space (v4.1+)
162 } compact align(4);
163
164 struct gic_its_cmdqeue
165 {
166     gicreg64_t base;
167     gicreg_t   wr_ptr;
168     gicreg_t   rd_ptr;  
169 } compact align(4);
170
171 struct gic_its_cmd
172 {
173     gicreg64_t dw[4];
174 };
175
176 struct gic_its_devmap
177 {
178     struct hlist_node node;
179     unsigned int devid;
180     unsigned int next_evtid;
181 };
182
183 struct gic_its
184 {
185     struct llist_header its;
186
187     struct {
188         unsigned int nr_devid;
189         unsigned int nr_evtid;
190         unsigned int nr_cid;
191         unsigned int sz_itte;
192
193         bool pta;
194         bool ext_cids;
195     };
196
197     union {
198         struct gic_its_regs* reg;
199         struct gic_its_regs_v41* reg_v41;
200         ptr_t  reg_ptr;
201     };
202
203     struct {
204         union {
205             struct gic_its_cmdqeue *cmd_queue;
206             ptr_t  cmd_queue_ptr;
207         };
208         
209         union {
210             struct gic_its_cmd *cmds;
211             ptr_t cmds_ptr;
212         };
213
214         unsigned int max_cmd;
215     };
216
217     union {
218         struct {
219             gicreg_t base[8];
220         } *tables;
221         ptr_t  table_ptr;
222     };
223
224     DECLARE_HASHTABLE(devmaps, 8);
225 };
226
227 typedef unsigned char lpi_entry_t;
228
229 struct arm_gic
230 {
231     unsigned int max_intid;
232     struct gic_pe pes[NR_CPU];
233
234     struct {
235         unsigned int lpi_nr;
236         unsigned int spi_nr;
237         unsigned int espi_nr;
238         bool lpi_ready;
239         bool nmi_ready;
240         bool has_espi;
241         bool msi_via_spi;
242     };
243
244     struct {
245         gicreg_t* dist_base;
246     } mmrs;
247
248     struct llist_header its;
249
250     struct {
251         ptr_t prop_pa;
252         lpi_entry_t* prop_table;
253         
254         ptr_t pend;
255         BITMAP(gic_bmp) pendings;
256     } lpi_tables;
257
258     struct gic_distributor dist;
259     struct gic_distributor dist_e;
260
261     struct {
262         struct gic_idomain* lpi;
263         struct gic_idomain* spi;
264         struct gic_idomain* espi;
265     } idomain;
266 };
267
268 void
269 gic_create_from_dt(struct arm_gic* gic);
270
271 unsigned int;
272 gic_dtprop_interpret(struct gic_int_param* param, 
273                      struct dt_prop_val* val, int width);
274
275 struct gic_its*
276 gic_its_create(struct arm_gic* gic, ptr_t regs);
277
278 void
279 gic_configure_its(struct arm_gic* gic);
280
281 struct gic_interrupt*
282 gic_install_int(struct gic_int_param* param, isr_cb handler, bool alloc);
283
284 unsigned int
285 gic_its_map_lpi(struct gic_its* its, 
286                 unsigned int devid, unsigned int lpid);
287
288 #endif /* __LUNAIX_GIC_H */