xics: Add simple ICS
[microwatt.git] / xics.vhdl
1 --
2 -- This is a simple XICS compliant interrupt controller. This is a
3 -- Presenter (ICP) and Source (ICS) in two small units directly
4 -- connected to each other with no routing layer.
5 --
6 -- The sources have a configurable IRQ priority set a set of ICS
7 -- registers in the source units.
8 --
9 -- The source ids start at 16 for int_level_in(0) and go up from
10 -- there (ie int_level_in(1) is source id 17). XXX Make a generic
11 --
12 -- The presentation layer will pick an interupt that is more
13 -- favourable than the current CPPR and present it via the XISR and
14 -- send an interrpt to the processor (via e_out). This may not be the
15 -- highest priority interrupt currently presented (which is allowed
16 -- via XICS)
17 --
18
19 library ieee;
20 use ieee.std_logic_1164.all;
21 use ieee.numeric_std.all;
22
23 library work;
24 use work.common.all;
25 use work.wishbone_types.all;
26
27 entity xics_icp is
28 port (
29 clk : in std_logic;
30 rst : in std_logic;
31
32 wb_in : in wb_io_master_out;
33 wb_out : out wb_io_slave_out;
34
35 ics_in : in ics_to_icp_t;
36 core_irq_out : out std_ulogic
37 );
38 end xics_icp;
39
40 architecture behaviour of xics_icp is
41 type reg_internal_t is record
42 xisr : std_ulogic_vector(23 downto 0);
43 cppr : std_ulogic_vector(7 downto 0);
44 mfrr : std_ulogic_vector(7 downto 0);
45 irq : std_ulogic;
46 wb_rd_data : std_ulogic_vector(31 downto 0);
47 wb_ack : std_ulogic;
48 end record;
49 constant reg_internal_init : reg_internal_t :=
50 (wb_ack => '0',
51 mfrr => x"ff", -- mask everything on reset
52 irq => '0',
53 others => (others => '0'));
54
55 signal r, r_next : reg_internal_t;
56
57 -- hardwire the hardware IRQ priority
58 constant HW_PRIORITY : std_ulogic_vector(7 downto 0) := x"80";
59
60 -- 8 bit offsets for each presentation
61 constant XIRR_POLL : std_ulogic_vector(7 downto 0) := x"00";
62 constant XIRR : std_ulogic_vector(7 downto 0) := x"04";
63 constant RESV0 : std_ulogic_vector(7 downto 0) := x"08";
64 constant MFRR : std_ulogic_vector(7 downto 0) := x"0c";
65
66 begin
67
68 regs : process(clk)
69 begin
70 if rising_edge(clk) then
71 r <= r_next;
72
73 -- We delay core_irq_out by a cycle to help with timing
74 core_irq_out <= r.irq;
75 end if;
76 end process;
77
78 wb_out.dat <= r.wb_rd_data;
79 wb_out.ack <= r.wb_ack;
80 wb_out.stall <= '0'; -- never stall wishbone
81
82 comb : process(all)
83 variable v : reg_internal_t;
84 variable xirr_accept_rd : std_ulogic;
85
86 function bswap(v : in std_ulogic_vector(31 downto 0)) return std_ulogic_vector is
87 variable r : std_ulogic_vector(31 downto 0);
88 begin
89 r( 7 downto 0) := v(31 downto 24);
90 r(15 downto 8) := v(23 downto 16);
91 r(23 downto 16) := v(15 downto 8);
92 r(31 downto 24) := v( 7 downto 0);
93 return r;
94 end function;
95
96 variable be_in : std_ulogic_vector(31 downto 0);
97 variable be_out : std_ulogic_vector(31 downto 0);
98
99 variable pending_priority : std_ulogic_vector(7 downto 0);
100 begin
101 v := r;
102
103 v.wb_ack := '0';
104
105 xirr_accept_rd := '0';
106
107 be_in := bswap(wb_in.dat);
108 be_out := (others => '0');
109
110 if wb_in.cyc = '1' and wb_in.stb = '1' then
111 v.wb_ack := '1'; -- always ack
112 if wb_in.we = '1' then -- write
113 -- writes to both XIRR are the same
114 case wb_in.adr(7 downto 0) is
115 when XIRR_POLL =>
116 report "ICP XIRR_POLL write";
117 v.cppr := be_in(31 downto 24);
118 when XIRR =>
119 v.cppr := be_in(31 downto 24);
120 if wb_in.sel = x"f" then -- 4 byte
121 report "ICP XIRR write word (EOI) :" & to_hstring(be_in);
122 elsif wb_in.sel = x"1" then -- 1 byte
123 report "ICP XIRR write byte (CPPR):" & to_hstring(be_in(31 downto 24));
124 else
125 report "ICP XIRR UNSUPPORTED write ! sel=" & to_hstring(wb_in.sel);
126 end if;
127 when MFRR =>
128 v.mfrr := be_in(31 downto 24);
129 if wb_in.sel = x"f" then -- 4 bytes
130 report "ICP MFRR write word:" & to_hstring(be_in);
131 elsif wb_in.sel = x"1" then -- 1 byte
132 report "ICP MFRR write byte:" & to_hstring(be_in(31 downto 24));
133 else
134 report "ICP MFRR UNSUPPORTED write ! sel=" & to_hstring(wb_in.sel);
135 end if;
136 when others =>
137 end case;
138
139 else -- read
140
141 case wb_in.adr(7 downto 0) is
142 when XIRR_POLL =>
143 report "ICP XIRR_POLL read";
144 be_out := r.cppr & r.xisr;
145 when XIRR =>
146 report "ICP XIRR read";
147 be_out := r.cppr & r.xisr;
148 if wb_in.sel = x"f" then
149 xirr_accept_rd := '1';
150 end if;
151 when MFRR =>
152 report "ICP MFRR read";
153 be_out(31 downto 24) := r.mfrr;
154 when others =>
155 end case;
156 end if;
157 end if;
158
159 pending_priority := x"ff";
160 v.xisr := x"000000";
161 v.irq := '0';
162
163 if ics_in.pri /= x"ff" then
164 v.xisr := x"00001" & ics_in.src;
165 pending_priority := ics_in.pri;
166 end if;
167
168 -- Check MFRR
169 if unsigned(r.mfrr) < unsigned(pending_priority) then --
170 v.xisr := x"000002"; -- special XICS MFRR IRQ source number
171 pending_priority := r.mfrr;
172 end if;
173
174 -- Accept the interrupt
175 if xirr_accept_rd = '1' then
176 report "XICS: ICP ACCEPT" &
177 " cppr:" & to_hstring(r.cppr) &
178 " xisr:" & to_hstring(r.xisr) &
179 " mfrr:" & to_hstring(r.mfrr);
180 v.cppr := pending_priority;
181 end if;
182
183 v.wb_rd_data := bswap(be_out);
184
185 if unsigned(pending_priority) < unsigned(v.cppr) then
186 if r.irq = '0' then
187 report "IRQ set";
188 end if;
189 v.irq := '1';
190 elsif r.irq = '1' then
191 report "IRQ clr";
192 end if;
193
194 if rst = '1' then
195 v := reg_internal_init;
196 end if;
197
198 r_next <= v;
199
200 end process;
201
202 end architecture behaviour;
203
204 library ieee;
205 use ieee.std_logic_1164.all;
206 use ieee.numeric_std.all;
207
208 library work;
209 use work.common.all;
210 use work.wishbone_types.all;
211
212 entity xics_ics is
213 generic (
214 SRC_NUM : positive := 16
215 );
216 port (
217 clk : in std_logic;
218 rst : in std_logic;
219
220 wb_in : in wb_io_master_out;
221 wb_out : out wb_io_slave_out;
222
223 int_level_in : in std_ulogic_vector(SRC_NUM - 1 downto 0);
224 icp_out : out ics_to_icp_t
225 );
226 end xics_ics;
227
228 architecture rtl of xics_ics is
229
230 subtype pri_t is std_ulogic_vector(7 downto 0);
231 type xive_t is record
232 pri : pri_t;
233 end record;
234 type xive_array_t is array(0 to SRC_NUM-1) of xive_t;
235 signal xives : xive_array_t;
236
237 signal wb_valid : std_ulogic;
238 signal reg_idx : integer range 0 to SRC_NUM - 1;
239 signal icp_out_next : ics_to_icp_t;
240 signal int_level_l : std_ulogic_vector(SRC_NUM - 1 downto 0);
241
242 function bswap(v : in std_ulogic_vector(31 downto 0)) return std_ulogic_vector is
243 variable r : std_ulogic_vector(31 downto 0);
244 begin
245 r( 7 downto 0) := v(31 downto 24);
246 r(15 downto 8) := v(23 downto 16);
247 r(23 downto 16) := v(15 downto 8);
248 r(31 downto 24) := v( 7 downto 0);
249 return r;
250 end function;
251
252 -- Register map
253 -- 0 : Config (currently hard wired base irq#)
254 -- 4 : Debug/diagnostics
255 -- 800 : XIVE0
256 -- 804 : XIVE1 ...
257 --
258 -- Config register format:
259 --
260 -- 23.. 0 : Interrupt base (hard wired to 16)
261 --
262 -- XIVE register format:
263 --
264 -- 31 : input bit (reflects interrupt input)
265 -- 30 : reserved
266 -- 29 : P (mirrors input for now)
267 -- 28 : Q (not implemented in this version)
268 -- 30 .. : reserved
269 -- 19 .. 8 : target (not implemented in this version)
270 -- 7 .. 0 : prio/mask
271
272 signal reg_is_xive : std_ulogic;
273 signal reg_is_config : std_ulogic;
274 signal reg_is_debug : std_ulogic;
275
276 begin
277
278 assert SRC_NUM = 16 report "Fixup address decode with log2";
279
280 reg_is_xive <= wb_in.adr(11);
281 reg_is_config <= '1' when wb_in.adr(11 downto 0) = x"000" else '0';
282 reg_is_debug <= '1' when wb_in.adr(11 downto 0) = x"004" else '0';
283
284 -- Register index XX FIXME: figure out bits from SRC_NUM
285 reg_idx <= to_integer(unsigned(wb_in.adr(5 downto 2)));
286
287 -- Latch interrupt inputs for timing
288 int_latch: process(clk)
289 begin
290 if rising_edge(clk) then
291 int_level_l <= int_level_in;
292 end if;
293 end process;
294
295 -- We don't stall. Acks are sent by the read machine one cycle
296 -- after a request, but we can handle one access per cycle.
297 wb_out.stall <= '0';
298 wb_valid <= wb_in.cyc and wb_in.stb;
299
300 -- Big read mux. This could be replaced by a slower state
301 -- machine iterating registers instead if timing gets tight.
302 reg_read: process(clk)
303 variable be_out : std_ulogic_vector(31 downto 0);
304 begin
305 if rising_edge(clk) then
306 be_out := (others => '0');
307
308 if reg_is_xive = '1' then
309 be_out := int_level_l(reg_idx) &
310 '0' &
311 int_level_l(reg_idx) &
312 '0' &
313 x"00000" &
314 xives(reg_idx).pri;
315 elsif reg_is_config = '1' then
316 be_out := std_ulogic_vector(to_unsigned(SRC_NUM, 32));
317 elsif reg_is_debug = '1' then
318 be_out := x"00000" & icp_out_next.src & icp_out_next.pri;
319 end if;
320 wb_out.dat <= bswap(be_out);
321 wb_out.ack <= wb_valid;
322 end if;
323 end process;
324
325 -- Register write machine
326 reg_write: process(clk)
327 variable be_in : std_ulogic_vector(31 downto 0);
328 begin
329 -- Byteswapped input
330 be_in := bswap(wb_in.dat);
331
332 if rising_edge(clk) then
333 if rst = '1' then
334 for i in 0 to SRC_NUM - 1 loop
335 xives(i) <= (pri => x"ff");
336 end loop;
337 elsif wb_valid = '1' and wb_in.we = '1' then
338 if reg_is_xive then
339 -- TODO: When adding support for other bits, make sure to
340 -- properly implement wb_in.sel to allow partial writes.
341 xives(reg_idx).pri <= be_in(7 downto 0);
342 report "ICS irq " & integer'image(reg_idx) & " set to:" & to_hstring(be_in(7 downto 0));
343 end if;
344 end if;
345 end if;
346 end process;
347
348 -- generate interrupt. This is a simple combinational process,
349 -- potentially wasteul in HW for large number of interrupts.
350 --
351 -- could be replaced with iterative state machines and a message
352 -- system between ICSs' (plural) and ICP incl. reject etc...
353 --
354 irq_gen_sync: process(clk)
355 begin
356 if rising_edge(clk) then
357 icp_out <= icp_out_next;
358 end if;
359 end process;
360
361 irq_gen: process(all)
362 variable max_idx : integer range 0 to SRC_NUM-1;
363 variable max_pri : pri_t;
364
365 -- A more favored than b ?
366 function a_mf_b(a: pri_t; b: pri_t) return boolean is
367 variable a_i : integer range 0 to 255;
368 variable b_i : integer range 0 to 255;
369 begin
370 a_i := to_integer(unsigned(a));
371 b_i := to_integer(unsigned(b));
372 return a < b;
373 end function;
374 begin
375 -- XXX FIXME: Use a tree
376 max_pri := x"ff";
377 max_idx := 0;
378 for i in 0 to SRC_NUM - 1 loop
379 if int_level_l(i) = '1' and a_mf_b(xives(i).pri, max_pri) then
380 max_pri := xives(i).pri;
381 max_idx := i;
382 end if;
383 end loop;
384 if max_pri /= x"ff" then
385 report "MFI: " & integer'image(max_idx) & " pri=" & to_hstring(max_pri);
386 end if;
387 icp_out_next.src <= std_ulogic_vector(to_unsigned(max_idx, 4));
388 icp_out_next.pri <= max_pri;
389 end process;
390
391 end architecture rtl;