dcache: Output separate done-without-error and error-done signals
[microwatt.git] / mmu.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4
5 library work;
6 use work.common.all;
7
8 -- Radix MMU
9 -- Supports 4-level trees as in arch 3.0B, but not the two-step translation for
10 -- guests under a hypervisor (i.e. there is no gRA -> hRA translation).
11
12 entity mmu is
13 port (
14 clk : in std_ulogic;
15 rst : in std_ulogic;
16
17 l_in : in Loadstore1ToMmuType;
18 l_out : out MmuToLoadstore1Type;
19
20 d_out : out MmuToDcacheType;
21 d_in : in DcacheToMmuType;
22
23 i_out : out MmuToIcacheType
24 );
25 end mmu;
26
27 architecture behave of mmu is
28
29 type state_t is (IDLE,
30 DO_TLBIE,
31 TLB_WAIT,
32 PROC_TBL_READ,
33 PROC_TBL_WAIT,
34 SEGMENT_CHECK,
35 RADIX_LOOKUP,
36 RADIX_READ_WAIT,
37 RADIX_LOAD_TLB,
38 RADIX_FINISH
39 );
40
41 type reg_stage_t is record
42 -- latched request from loadstore1
43 valid : std_ulogic;
44 iside : std_ulogic;
45 store : std_ulogic;
46 priv : std_ulogic;
47 addr : std_ulogic_vector(63 downto 0);
48 inval_all : std_ulogic;
49 -- config SPRs
50 prtbl : std_ulogic_vector(63 downto 0);
51 pid : std_ulogic_vector(31 downto 0);
52 -- internal state
53 state : state_t;
54 done : std_ulogic;
55 pgtbl0 : std_ulogic_vector(63 downto 0);
56 pt0_valid : std_ulogic;
57 pgtbl3 : std_ulogic_vector(63 downto 0);
58 pt3_valid : std_ulogic;
59 shift : unsigned(5 downto 0);
60 mask_size : unsigned(4 downto 0);
61 pgbase : std_ulogic_vector(55 downto 0);
62 pde : std_ulogic_vector(63 downto 0);
63 invalid : std_ulogic;
64 badtree : std_ulogic;
65 segerror : std_ulogic;
66 perm_err : std_ulogic;
67 rc_error : std_ulogic;
68 end record;
69
70 signal r, rin : reg_stage_t;
71
72 signal addrsh : std_ulogic_vector(15 downto 0);
73 signal mask : std_ulogic_vector(15 downto 0);
74 signal finalmask : std_ulogic_vector(43 downto 0);
75
76 begin
77 -- Multiplex internal SPR values back to loadstore1, selected
78 -- by l_in.sprn.
79 l_out.sprval <= r.prtbl when l_in.sprn(9) = '1' else x"00000000" & r.pid;
80
81 mmu_0: process(clk)
82 begin
83 if rising_edge(clk) then
84 if rst = '1' then
85 r.state <= IDLE;
86 r.valid <= '0';
87 r.pt0_valid <= '0';
88 r.pt3_valid <= '0';
89 r.prtbl <= (others => '0');
90 else
91 if rin.valid = '1' then
92 report "MMU got tlb miss for " & to_hstring(rin.addr);
93 end if;
94 if l_out.done = '1' then
95 report "MMU completing op with invalid=" & std_ulogic'image(l_out.invalid) &
96 " badtree=" & std_ulogic'image(l_out.badtree);
97 end if;
98 if rin.state = RADIX_LOOKUP then
99 report "radix lookup shift=" & integer'image(to_integer(rin.shift)) &
100 " msize=" & integer'image(to_integer(rin.mask_size));
101 end if;
102 if r.state = RADIX_LOOKUP then
103 report "send load addr=" & to_hstring(d_out.addr) &
104 " addrsh=" & to_hstring(addrsh) & " mask=" & to_hstring(mask);
105 end if;
106 r <= rin;
107 end if;
108 end if;
109 end process;
110
111 -- Shift address bits 61--12 right by 0--47 bits and
112 -- supply the least significant 16 bits of the result.
113 addrshifter: process(all)
114 variable sh1 : std_ulogic_vector(30 downto 0);
115 variable sh2 : std_ulogic_vector(18 downto 0);
116 variable result : std_ulogic_vector(15 downto 0);
117 begin
118 case r.shift(5 downto 4) is
119 when "00" =>
120 sh1 := r.addr(42 downto 12);
121 when "01" =>
122 sh1 := r.addr(58 downto 28);
123 when others =>
124 sh1 := "0000000000000" & r.addr(61 downto 44);
125 end case;
126 case r.shift(3 downto 2) is
127 when "00" =>
128 sh2 := sh1(18 downto 0);
129 when "01" =>
130 sh2 := sh1(22 downto 4);
131 when "10" =>
132 sh2 := sh1(26 downto 8);
133 when others =>
134 sh2 := sh1(30 downto 12);
135 end case;
136 case r.shift(1 downto 0) is
137 when "00" =>
138 result := sh2(15 downto 0);
139 when "01" =>
140 result := sh2(16 downto 1);
141 when "10" =>
142 result := sh2(17 downto 2);
143 when others =>
144 result := sh2(18 downto 3);
145 end case;
146 addrsh <= result;
147 end process;
148
149 -- generate mask for extracting address fields for PTE address generation
150 addrmaskgen: process(all)
151 variable m : std_ulogic_vector(15 downto 0);
152 begin
153 -- mask_count has to be >= 5
154 m := x"001f";
155 for i in 5 to 15 loop
156 if i < to_integer(r.mask_size) then
157 m(i) := '1';
158 end if;
159 end loop;
160 mask <= m;
161 end process;
162
163 -- generate mask for extracting address bits to go in TLB entry
164 -- in order to support pages > 4kB
165 finalmaskgen: process(all)
166 variable m : std_ulogic_vector(43 downto 0);
167 begin
168 m := (others => '0');
169 for i in 0 to 43 loop
170 if i < to_integer(r.shift) then
171 m(i) := '1';
172 end if;
173 end loop;
174 finalmask <= m;
175 end process;
176
177 mmu_1: process(all)
178 variable v : reg_stage_t;
179 variable dcreq : std_ulogic;
180 variable tlb_load : std_ulogic;
181 variable itlb_load : std_ulogic;
182 variable tlbie_req : std_ulogic;
183 variable prtbl_rd : std_ulogic;
184 variable pt_valid : std_ulogic;
185 variable effpid : std_ulogic_vector(31 downto 0);
186 variable prtable_addr : std_ulogic_vector(63 downto 0);
187 variable rts : unsigned(5 downto 0);
188 variable mbits : unsigned(5 downto 0);
189 variable pgtable_addr : std_ulogic_vector(63 downto 0);
190 variable pte : std_ulogic_vector(63 downto 0);
191 variable tlb_data : std_ulogic_vector(63 downto 0);
192 variable nonzero : std_ulogic;
193 variable pgtbl : std_ulogic_vector(63 downto 0);
194 variable perm_ok : std_ulogic;
195 variable rc_ok : std_ulogic;
196 variable addr : std_ulogic_vector(63 downto 0);
197 variable data : std_ulogic_vector(63 downto 0);
198 begin
199 v := r;
200 v.valid := '0';
201 dcreq := '0';
202 v.done := '0';
203 v.invalid := '0';
204 v.badtree := '0';
205 v.segerror := '0';
206 v.perm_err := '0';
207 v.rc_error := '0';
208 tlb_load := '0';
209 itlb_load := '0';
210 tlbie_req := '0';
211 v.inval_all := '0';
212 prtbl_rd := '0';
213
214 -- Radix tree data structures in memory are big-endian,
215 -- so we need to byte-swap them
216 for i in 0 to 7 loop
217 data(i * 8 + 7 downto i * 8) := d_in.data((7 - i) * 8 + 7 downto (7 - i) * 8);
218 end loop;
219
220 case r.state is
221 when IDLE =>
222 if l_in.addr(63) = '0' then
223 pgtbl := r.pgtbl0;
224 pt_valid := r.pt0_valid;
225 else
226 pgtbl := r.pgtbl3;
227 pt_valid := r.pt3_valid;
228 end if;
229 -- rts == radix tree size, # address bits being translated
230 rts := unsigned('0' & pgtbl(62 downto 61) & pgtbl(7 downto 5));
231 -- mbits == # address bits to index top level of tree
232 mbits := unsigned('0' & pgtbl(4 downto 0));
233 -- set v.shift to rts so that we can use finalmask for the segment check
234 v.shift := rts;
235 v.mask_size := mbits(4 downto 0);
236 v.pgbase := pgtbl(55 downto 8) & x"00";
237
238 if l_in.valid = '1' then
239 v.addr := l_in.addr;
240 v.iside := l_in.iside;
241 v.store := not (l_in.load or l_in.iside);
242 v.priv := l_in.priv;
243 if l_in.tlbie = '1' then
244 -- Invalidate all iTLB/dTLB entries for tlbie with
245 -- RB[IS] != 0 or RB[AP] != 0, or for slbia
246 v.inval_all := l_in.slbia or l_in.addr(11) or l_in.addr(10) or
247 l_in.addr(7) or l_in.addr(6) or l_in.addr(5);
248 -- The RIC field of the tlbie instruction comes across on the
249 -- sprn bus as bits 2--3. RIC=2 flushes process table caches.
250 if l_in.sprn(3) = '1' then
251 v.pt0_valid := '0';
252 v.pt3_valid := '0';
253 end if;
254 v.state := DO_TLBIE;
255 else
256 v.valid := '1';
257 if pt_valid = '0' then
258 -- need to fetch process table entry
259 -- set v.shift so we can use finalmask for generating
260 -- the process table entry address
261 v.shift := unsigned('0' & r.prtbl(4 downto 0));
262 v.state := PROC_TBL_READ;
263 elsif mbits = 0 then
264 -- Use RPDS = 0 to disable radix tree walks
265 v.state := RADIX_FINISH;
266 v.invalid := '1';
267 else
268 v.state := SEGMENT_CHECK;
269 end if;
270 end if;
271 end if;
272 if l_in.mtspr = '1' then
273 -- Move to PID needs to invalidate L1 TLBs and cached
274 -- pgtbl0 value. Move to PRTBL does that plus
275 -- invalidating the cached pgtbl3 value as well.
276 if l_in.sprn(9) = '0' then
277 v.pid := l_in.rs(31 downto 0);
278 else
279 v.prtbl := l_in.rs;
280 v.pt3_valid := '0';
281 end if;
282 v.pt0_valid := '0';
283 v.inval_all := '1';
284 v.state := DO_TLBIE;
285 end if;
286
287 when DO_TLBIE =>
288 dcreq := '1';
289 tlbie_req := '1';
290 v.state := TLB_WAIT;
291
292 when TLB_WAIT =>
293 if d_in.done = '1' then
294 v.state := RADIX_FINISH;
295 end if;
296
297 when PROC_TBL_READ =>
298 dcreq := '1';
299 prtbl_rd := '1';
300 v.state := PROC_TBL_WAIT;
301
302 when PROC_TBL_WAIT =>
303 if d_in.done = '1' then
304 if r.addr(63) = '1' then
305 v.pgtbl3 := data;
306 v.pt3_valid := '1';
307 else
308 v.pgtbl0 := data;
309 v.pt0_valid := '1';
310 end if;
311 -- rts == radix tree size, # address bits being translated
312 rts := unsigned('0' & data(62 downto 61) & data(7 downto 5));
313 -- mbits == # address bits to index top level of tree
314 mbits := unsigned('0' & data(4 downto 0));
315 -- set v.shift to rts so that we can use finalmask for the segment check
316 v.shift := rts;
317 v.mask_size := mbits(4 downto 0);
318 v.pgbase := data(55 downto 8) & x"00";
319 if mbits = 0 then
320 v.state := RADIX_FINISH;
321 v.invalid := '1';
322 else
323 v.state := SEGMENT_CHECK;
324 end if;
325 end if;
326 if d_in.err = '1' then
327 v.state := RADIX_FINISH;
328 v.badtree := '1';
329 end if;
330
331 when SEGMENT_CHECK =>
332 mbits := '0' & r.mask_size;
333 v.shift := r.shift + (31 - 12) - mbits;
334 nonzero := or(r.addr(61 downto 31) and not finalmask(30 downto 0));
335 if r.addr(63) /= r.addr(62) or nonzero = '1' then
336 v.state := RADIX_FINISH;
337 v.segerror := '1';
338 elsif mbits < 5 or mbits > 16 or mbits > (r.shift + (31 - 12)) then
339 v.state := RADIX_FINISH;
340 v.badtree := '1';
341 else
342 v.state := RADIX_LOOKUP;
343 end if;
344
345 when RADIX_LOOKUP =>
346 dcreq := '1';
347 v.state := RADIX_READ_WAIT;
348
349 when RADIX_READ_WAIT =>
350 if d_in.done = '1' then
351 v.pde := data;
352 -- test valid bit
353 if data(63) = '1' then
354 -- test leaf bit
355 if data(62) = '1' then
356 -- check permissions and RC bits
357 perm_ok := '0';
358 if r.priv = '1' or data(3) = '0' then
359 if r.iside = '0' then
360 perm_ok := data(1) or (data(2) and not r.store);
361 else
362 -- no IAMR, so no KUEP support for now
363 -- deny execute permission if cache inhibited
364 perm_ok := data(0) and not data(5);
365 end if;
366 end if;
367 rc_ok := data(8) and (data(7) or not r.store);
368 if perm_ok = '1' and rc_ok = '1' then
369 v.state := RADIX_LOAD_TLB;
370 else
371 v.state := RADIX_FINISH;
372 v.perm_err := not perm_ok;
373 -- permission error takes precedence over RC error
374 v.rc_error := perm_ok;
375 end if;
376 else
377 mbits := unsigned('0' & data(4 downto 0));
378 if mbits < 5 or mbits > 16 or mbits > r.shift then
379 v.state := RADIX_FINISH;
380 v.badtree := '1';
381 else
382 v.shift := v.shift - mbits;
383 v.mask_size := mbits(4 downto 0);
384 v.pgbase := data(55 downto 8) & x"00";
385 v.state := RADIX_LOOKUP;
386 end if;
387 end if;
388 else
389 -- non-present PTE, generate a DSI
390 v.state := RADIX_FINISH;
391 v.invalid := '1';
392 end if;
393 end if;
394 if d_in.err = '1' then
395 v.state := RADIX_FINISH;
396 v.badtree := '1';
397 end if;
398
399 when RADIX_LOAD_TLB =>
400 tlb_load := '1';
401 if r.iside = '0' then
402 dcreq := '1';
403 v.state := TLB_WAIT;
404 else
405 itlb_load := '1';
406 v.state := IDLE;
407 end if;
408
409 when RADIX_FINISH =>
410 v.state := IDLE;
411
412 end case;
413
414 if v.state = RADIX_FINISH or (v.state = RADIX_LOAD_TLB and r.iside = '1') then
415 v.done := '1';
416 end if;
417
418 if r.addr(63) = '1' then
419 effpid := x"00000000";
420 else
421 effpid := r.pid;
422 end if;
423 prtable_addr := x"00" & r.prtbl(55 downto 36) &
424 ((r.prtbl(35 downto 12) and not finalmask(23 downto 0)) or
425 (effpid(31 downto 8) and finalmask(23 downto 0))) &
426 effpid(7 downto 0) & "0000";
427
428 pgtable_addr := x"00" & r.pgbase(55 downto 19) &
429 ((r.pgbase(18 downto 3) and not mask) or (addrsh and mask)) &
430 "000";
431 pte := x"00" &
432 ((r.pde(55 downto 12) and not finalmask) or (r.addr(55 downto 12) and finalmask))
433 & r.pde(11 downto 0);
434
435 -- update registers
436 rin <= v;
437
438 -- drive outputs
439 if tlbie_req = '1' then
440 addr := r.addr;
441 tlb_data := (others => '0');
442 elsif tlb_load = '1' then
443 addr := r.addr(63 downto 12) & x"000";
444 tlb_data := pte;
445 elsif prtbl_rd = '1' then
446 addr := prtable_addr;
447 tlb_data := (others => '0');
448 else
449 addr := pgtable_addr;
450 tlb_data := (others => '0');
451 end if;
452
453 l_out.done <= r.done;
454 l_out.invalid <= r.invalid;
455 l_out.badtree <= r.badtree;
456 l_out.segerr <= r.segerror;
457 l_out.perm_error <= r.perm_err;
458 l_out.rc_error <= r.rc_error;
459
460 d_out.valid <= dcreq;
461 d_out.tlbie <= tlbie_req;
462 d_out.doall <= r.inval_all;
463 d_out.tlbld <= tlb_load;
464 d_out.addr <= addr;
465 d_out.pte <= tlb_data;
466
467 i_out.tlbld <= itlb_load;
468 i_out.tlbie <= tlbie_req;
469 i_out.doall <= r.inval_all;
470 i_out.addr <= addr;
471 i_out.pte <= tlb_data;
472
473 end process;
474 end;