2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
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).
17 l_in : in Loadstore1ToMmuType;
18 l_out : out MmuToLoadstore1Type;
20 d_out : out MmuToDcacheType;
21 d_in : in DcacheToMmuType;
23 i_out : out MmuToIcacheType
27 architecture behave of mmu is
29 type state_t is (IDLE,
44 type reg_stage_t is record
45 -- latched request from loadstore1
50 addr : std_ulogic_vector(63 downto 0);
51 inval_all : std_ulogic;
53 ptcr : std_ulogic_vector(63 downto 0);
54 pid : std_ulogic_vector(31 downto 0);
59 prtbl : std_ulogic_vector(63 downto 0);
60 ptb_valid : std_ulogic;
61 pgtbl0 : std_ulogic_vector(63 downto 0);
62 pt0_valid : std_ulogic;
63 pgtbl3 : std_ulogic_vector(63 downto 0);
64 pt3_valid : std_ulogic;
65 shift : unsigned(5 downto 0);
66 mask_size : unsigned(4 downto 0);
67 pgbase : std_ulogic_vector(55 downto 0);
68 pde : std_ulogic_vector(63 downto 0);
71 segerror : std_ulogic;
72 perm_err : std_ulogic;
73 rc_error : std_ulogic;
76 signal r, rin : reg_stage_t;
78 signal addrsh : std_ulogic_vector(15 downto 0);
79 signal mask : std_ulogic_vector(15 downto 0);
80 signal finalmask : std_ulogic_vector(43 downto 0);
83 -- Multiplex internal SPR values back to loadstore1, selected
85 l_out.sprval <= r.ptcr when l_in.sprn(8) = '1' else x"00000000" & r.pid;
89 if rising_edge(clk) then
96 r.ptcr <= (others => '0');
97 r.pid <= (others => '0');
99 if rin.valid = '1' then
100 report "MMU got tlb miss for " & to_hstring(rin.addr);
102 if l_out.done = '1' then
103 report "MMU completing op without error";
105 if l_out.err = '1' then
106 report "MMU completing op with err invalid=" & std_ulogic'image(l_out.invalid) &
107 " badtree=" & std_ulogic'image(l_out.badtree);
109 if rin.state = RADIX_LOOKUP then
110 report "radix lookup shift=" & integer'image(to_integer(rin.shift)) &
111 " msize=" & integer'image(to_integer(rin.mask_size));
113 if r.state = RADIX_LOOKUP then
114 report "send load addr=" & to_hstring(d_out.addr) &
115 " addrsh=" & to_hstring(addrsh) & " mask=" & to_hstring(mask);
122 -- Shift address bits 61--12 right by 0--47 bits and
123 -- supply the least significant 16 bits of the result.
124 addrshifter: process(all)
125 variable sh1 : std_ulogic_vector(30 downto 0);
126 variable sh2 : std_ulogic_vector(18 downto 0);
127 variable result : std_ulogic_vector(15 downto 0);
129 case r.shift(5 downto 4) is
131 sh1 := r.addr(42 downto 12);
133 sh1 := r.addr(58 downto 28);
135 sh1 := "0000000000000" & r.addr(61 downto 44);
137 case r.shift(3 downto 2) is
139 sh2 := sh1(18 downto 0);
141 sh2 := sh1(22 downto 4);
143 sh2 := sh1(26 downto 8);
145 sh2 := sh1(30 downto 12);
147 case r.shift(1 downto 0) is
149 result := sh2(15 downto 0);
151 result := sh2(16 downto 1);
153 result := sh2(17 downto 2);
155 result := sh2(18 downto 3);
160 -- generate mask for extracting address fields for PTE address generation
161 addrmaskgen: process(all)
162 variable m : std_ulogic_vector(15 downto 0);
164 -- mask_count has to be >= 5
166 for i in 5 to 15 loop
167 if i < to_integer(r.mask_size) then
174 -- generate mask for extracting address bits to go in TLB entry
175 -- in order to support pages > 4kB
176 finalmaskgen: process(all)
177 variable m : std_ulogic_vector(43 downto 0);
179 m := (others => '0');
180 for i in 0 to 43 loop
181 if i < to_integer(r.shift) then
189 variable v : reg_stage_t;
190 variable dcreq : std_ulogic;
191 variable tlb_load : std_ulogic;
192 variable itlb_load : std_ulogic;
193 variable tlbie_req : std_ulogic;
194 variable ptbl_rd : std_ulogic;
195 variable prtbl_rd : std_ulogic;
196 variable pt_valid : std_ulogic;
197 variable effpid : std_ulogic_vector(31 downto 0);
198 variable prtable_addr : std_ulogic_vector(63 downto 0);
199 variable rts : unsigned(5 downto 0);
200 variable mbits : unsigned(5 downto 0);
201 variable pgtable_addr : std_ulogic_vector(63 downto 0);
202 variable pte : std_ulogic_vector(63 downto 0);
203 variable tlb_data : std_ulogic_vector(63 downto 0);
204 variable nonzero : std_ulogic;
205 variable pgtbl : std_ulogic_vector(63 downto 0);
206 variable perm_ok : std_ulogic;
207 variable rc_ok : std_ulogic;
208 variable addr : std_ulogic_vector(63 downto 0);
209 variable data : std_ulogic_vector(63 downto 0);
228 -- Radix tree data structures in memory are big-endian,
229 -- so we need to byte-swap them
231 data(i * 8 + 7 downto i * 8) := d_in.data((7 - i) * 8 + 7 downto (7 - i) * 8);
236 if l_in.addr(63) = '0' then
238 pt_valid := r.pt0_valid;
241 pt_valid := r.pt3_valid;
243 -- rts == radix tree size, # address bits being translated
244 rts := unsigned('0' & pgtbl(62 downto 61) & pgtbl(7 downto 5));
245 -- mbits == # address bits to index top level of tree
246 mbits := unsigned('0' & pgtbl(4 downto 0));
247 -- set v.shift to rts so that we can use finalmask for the segment check
249 v.mask_size := mbits(4 downto 0);
250 v.pgbase := pgtbl(55 downto 8) & x"00";
252 if l_in.valid = '1' then
254 v.iside := l_in.iside;
255 v.store := not (l_in.load or l_in.iside);
257 if l_in.tlbie = '1' then
258 -- Invalidate all iTLB/dTLB entries for tlbie with
259 -- RB[IS] != 0 or RB[AP] != 0, or for slbia
260 v.inval_all := l_in.slbia or l_in.addr(11) or l_in.addr(10) or
261 l_in.addr(7) or l_in.addr(6) or l_in.addr(5);
262 -- The RIC field of the tlbie instruction comes across on the
263 -- sprn bus as bits 2--3. RIC=2 flushes process table caches.
264 if l_in.sprn(3) = '1' then
272 if r.ptb_valid = '0' then
273 -- need to fetch process table base from partition table
274 v.state := PART_TBL_READ;
275 elsif pt_valid = '0' then
276 -- need to fetch process table entry
277 -- set v.shift so we can use finalmask for generating
278 -- the process table entry address
279 v.shift := unsigned('0' & r.prtbl(4 downto 0));
280 v.state := PROC_TBL_READ;
282 -- Use RPDS = 0 to disable radix tree walks
283 v.state := RADIX_FINISH;
286 v.state := SEGMENT_CHECK;
290 if l_in.mtspr = '1' then
291 -- Move to PID needs to invalidate L1 TLBs and cached
292 -- pgtbl0 value. Move to PTCR does that plus
293 -- invalidating the cached pgtbl3 and prtbl values as well.
294 if l_in.sprn(8) = '0' then
295 v.pid := l_in.rs(31 downto 0);
312 if d_in.done = '1' then
313 v.state := RADIX_FINISH;
316 when PART_TBL_READ =>
319 v.state := PART_TBL_WAIT;
321 when PART_TBL_WAIT =>
322 if d_in.done = '1' then
325 v.state := PART_TBL_DONE;
328 when PART_TBL_DONE =>
329 v.shift := unsigned('0' & r.prtbl(4 downto 0));
330 v.state := PROC_TBL_READ;
332 when PROC_TBL_READ =>
335 v.state := PROC_TBL_WAIT;
337 when PROC_TBL_WAIT =>
338 if d_in.done = '1' then
339 if r.addr(63) = '1' then
346 -- rts == radix tree size, # address bits being translated
347 rts := unsigned('0' & data(62 downto 61) & data(7 downto 5));
348 -- mbits == # address bits to index top level of tree
349 mbits := unsigned('0' & data(4 downto 0));
350 -- set v.shift to rts so that we can use finalmask for the segment check
352 v.mask_size := mbits(4 downto 0);
353 v.pgbase := data(55 downto 8) & x"00";
355 v.state := RADIX_FINISH;
358 v.state := SEGMENT_CHECK;
361 if d_in.err = '1' then
362 v.state := RADIX_FINISH;
366 when SEGMENT_CHECK =>
367 mbits := '0' & r.mask_size;
368 v.shift := r.shift + (31 - 12) - mbits;
369 nonzero := or(r.addr(61 downto 31) and not finalmask(30 downto 0));
370 if r.addr(63) /= r.addr(62) or nonzero = '1' then
371 v.state := RADIX_FINISH;
373 elsif mbits < 5 or mbits > 16 or mbits > (r.shift + (31 - 12)) then
374 v.state := RADIX_FINISH;
377 v.state := RADIX_LOOKUP;
382 v.state := RADIX_READ_WAIT;
384 when RADIX_READ_WAIT =>
385 if d_in.done = '1' then
388 if data(63) = '1' then
390 if data(62) = '1' then
391 -- check permissions and RC bits
393 if r.priv = '1' or data(3) = '0' then
394 if r.iside = '0' then
395 perm_ok := data(1) or (data(2) and not r.store);
397 -- no IAMR, so no KUEP support for now
398 -- deny execute permission if cache inhibited
399 perm_ok := data(0) and not data(5);
402 rc_ok := data(8) and (data(7) or not r.store);
403 if perm_ok = '1' and rc_ok = '1' then
404 v.state := RADIX_LOAD_TLB;
406 v.state := RADIX_FINISH;
407 v.perm_err := not perm_ok;
408 -- permission error takes precedence over RC error
409 v.rc_error := perm_ok;
412 mbits := unsigned('0' & data(4 downto 0));
413 if mbits < 5 or mbits > 16 or mbits > r.shift then
414 v.state := RADIX_FINISH;
417 v.shift := v.shift - mbits;
418 v.mask_size := mbits(4 downto 0);
419 v.pgbase := data(55 downto 8) & x"00";
420 v.state := RADIX_LOOKUP;
424 -- non-present PTE, generate a DSI
425 v.state := RADIX_FINISH;
429 if d_in.err = '1' then
430 v.state := RADIX_FINISH;
434 when RADIX_LOAD_TLB =>
436 if r.iside = '0' then
449 if v.state = RADIX_FINISH or (v.state = RADIX_LOAD_TLB and r.iside = '1') then
450 v.err := v.invalid or v.badtree or v.segerror or v.perm_err or v.rc_error;
454 if r.addr(63) = '1' then
455 effpid := x"00000000";
459 prtable_addr := x"00" & r.prtbl(55 downto 36) &
460 ((r.prtbl(35 downto 12) and not finalmask(23 downto 0)) or
461 (effpid(31 downto 8) and finalmask(23 downto 0))) &
462 effpid(7 downto 0) & "0000";
464 pgtable_addr := x"00" & r.pgbase(55 downto 19) &
465 ((r.pgbase(18 downto 3) and not mask) or (addrsh and mask)) &
468 ((r.pde(55 downto 12) and not finalmask) or (r.addr(55 downto 12) and finalmask))
469 & r.pde(11 downto 0);
475 if tlbie_req = '1' then
477 tlb_data := (others => '0');
478 elsif tlb_load = '1' then
479 addr := r.addr(63 downto 12) & x"000";
481 elsif ptbl_rd = '1' then
482 addr := x"00" & r.ptcr(55 downto 12) & x"008";
483 tlb_data := (others => '0');
484 elsif prtbl_rd = '1' then
485 addr := prtable_addr;
486 tlb_data := (others => '0');
488 addr := pgtable_addr;
489 tlb_data := (others => '0');
492 l_out.done <= r.done;
494 l_out.invalid <= r.invalid;
495 l_out.badtree <= r.badtree;
496 l_out.segerr <= r.segerror;
497 l_out.perm_error <= r.perm_err;
498 l_out.rc_error <= r.rc_error;
500 d_out.valid <= dcreq;
501 d_out.tlbie <= tlbie_req;
502 d_out.doall <= r.inval_all;
503 d_out.tlbld <= tlb_load;
505 d_out.pte <= tlb_data;
507 i_out.tlbld <= itlb_load;
508 i_out.tlbie <= tlbie_req;
509 i_out.doall <= r.inval_all;
511 i_out.pte <= tlb_data;