3 # License for original copyright mmu.vhdl by microwatt authors: CC4
4 # License for copyrighted modifications made in mmu.py: LGPLv3+
6 # This derivative work although includes CC4 licensed material is
7 # covered by the LGPLv3+
11 based on Anton Blanchard microwatt mmu.vhdl
14 from enum
import Enum
, unique
15 from nmigen
import (C
, Module
, Signal
, Elaboratable
, Mux
, Cat
, Repl
, Signal
)
16 from nmigen
.cli
import main
17 from nmigen
.cli
import rtlil
18 from nmutil
.iocontrol
import RecordObject
19 from nmutil
.byterev
import byte_reverse
20 from nmutil
.mask
import Mask
, masked
21 from nmutil
.util
import Display
24 from nmigen
.back
.pysim
import Simulator
, Delay
, Settle
26 from nmigen
.sim
.cxxsim
import Simulator
, Delay
, Settle
27 from nmutil
.util
import wrap
29 from soc
.experiment
.mem_types
import (LoadStore1ToMMUType
,
38 IDLE
= 0 # zero is default on reset for r.state
50 class RegStage(RecordObject
):
51 def __init__(self
, name
=None):
52 super().__init
__(name
=name
)
53 # latched request from loadstore1
58 self
.addr
= Signal(64)
59 self
.inval_all
= Signal()
61 self
.prtbl
= Signal(64)
64 self
.state
= Signal(State
) # resets to IDLE
67 self
.pgtbl0
= Signal(64)
68 self
.pt0_valid
= Signal()
69 self
.pgtbl3
= Signal(64)
70 self
.pt3_valid
= Signal()
71 self
.shift
= Signal(6)
72 self
.mask_size
= Signal(5)
73 self
.pgbase
= Signal(56)
75 self
.invalid
= Signal()
76 self
.badtree
= Signal()
77 self
.segerror
= Signal()
78 self
.perm_err
= Signal()
79 self
.rc_error
= Signal()
82 class MMU(Elaboratable
):
85 Supports 4-level trees as in arch 3.0B, but not the
86 two-step translation for guests under a hypervisor
87 (i.e. there is no gRA -> hRA translation).
90 self
.l_in
= LoadStore1ToMMUType()
91 self
.l_out
= MMUToLoadStore1Type()
92 self
.d_out
= MMUToDCacheType()
93 self
.d_in
= DCacheToMMUType()
94 self
.i_out
= MMUToICacheType()
96 def radix_tree_idle(self
, m
, l_in
, r
, v
):
105 with m
.If(~l_in
.addr
[63]):
106 comb
+= pgtbl
.eq(r
.pgtbl0
)
107 comb
+= pt_valid
.eq(r
.pt0_valid
)
109 comb
+= pgtbl
.eq(r
.pgtbl3
)
110 comb
+= pt_valid
.eq(r
.pt3_valid
)
112 # rts == radix tree size, number of address bits
114 comb
+= rts
.eq(Cat(pgtbl
[5:8], pgtbl
[61:63]))
116 # mbits == number of address bits to index top
118 comb
+= mbits
.eq(pgtbl
[0:5])
120 # set v.shift to rts so that we can use finalmask
121 # for the segment check
122 comb
+= v
.shift
.eq(rts
)
123 comb
+= v
.mask_size
.eq(mbits
[0:5])
124 comb
+= v
.pgbase
.eq(Cat(C(0, 8), pgtbl
[8:56]))
126 with m
.If(l_in
.valid
):
127 comb
+= v
.addr
.eq(l_in
.addr
)
128 comb
+= v
.iside
.eq(l_in
.iside
)
129 comb
+= v
.store
.eq(~
(l_in
.load | l_in
.iside
))
130 comb
+= v
.priv
.eq(l_in
.priv
)
132 comb
+= Display("l_in.valid addr %x iside %d store %d "
133 "rts %x mbits %x pt_valid %d",
134 v
.addr
, v
.iside
, v
.store
,
135 rts
, mbits
, pt_valid
)
137 with m
.If(l_in
.tlbie
):
138 # Invalidate all iTLB/dTLB entries for
139 # tlbie with RB[IS] != 0 or RB[AP] != 0,
141 comb
+= v
.inval_all
.eq(l_in
.slbia
148 # The RIC field of the tlbie instruction
149 # comes across on the sprn bus as bits 2--3.
150 # RIC=2 flushes process table caches.
151 with m
.If(l_in
.sprn
[3]):
152 comb
+= v
.pt0_valid
.eq(0)
153 comb
+= v
.pt3_valid
.eq(0)
154 comb
+= v
.state
.eq(State
.DO_TLBIE
)
156 comb
+= v
.valid
.eq(1)
157 with m
.If(~pt_valid
):
158 # need to fetch process table entry
159 # set v.shift so we can use finalmask
160 # for generating the process table
162 comb
+= v
.shift
.eq(r
.prtbl
[0:5])
163 comb
+= v
.state
.eq(State
.PROC_TBL_READ
)
165 with m
.Elif(mbits
== 0):
166 # Use RPDS = 0 to disable radix tree walks
167 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
168 comb
+= v
.invalid
.eq(1)
170 comb
+= v
.state
.eq(State
.SEGMENT_CHECK
)
172 with m
.If(l_in
.mtspr
):
173 # Move to PID needs to invalidate L1 TLBs
174 # and cached pgtbl0 value. Move to PRTBL
175 # does that plus invalidating the cached
176 # pgtbl3 value as well.
177 with m
.If(~l_in
.sprn
[9]):
178 comb
+= v
.pid
.eq(l_in
.rs
[0:32])
180 comb
+= v
.prtbl
.eq(l_in
.rs
)
181 comb
+= v
.pt3_valid
.eq(0)
183 comb
+= v
.pt0_valid
.eq(0)
184 comb
+= v
.inval_all
.eq(1)
185 comb
+= v
.state
.eq(State
.DO_TLBIE
)
187 def proc_tbl_wait(self
, m
, v
, r
, data
):
189 with m
.If(r
.addr
[63]):
190 comb
+= v
.pgtbl3
.eq(data
)
191 comb
+= v
.pt3_valid
.eq(1)
193 comb
+= v
.pgtbl0
.eq(data
)
194 comb
+= v
.pt0_valid
.eq(1)
199 # rts == radix tree size, # address bits being translated
200 comb
+= rts
.eq(Cat(data
[5:8], data
[61:63]))
202 # mbits == # address bits to index top level of tree
203 comb
+= mbits
.eq(data
[0:5])
205 # set v.shift to rts so that we can use finalmask for the segment check
206 comb
+= v
.shift
.eq(rts
)
207 comb
+= v
.mask_size
.eq(mbits
[0:5])
208 comb
+= v
.pgbase
.eq(Cat(C(0, 8), data
[8:56]))
211 comb
+= v
.state
.eq(State
.SEGMENT_CHECK
)
213 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
214 comb
+= v
.invalid
.eq(1)
216 def radix_read_wait(self
, m
, v
, r
, d_in
, data
):
227 with m
.If(d_in
.done
& (r
.state
== State
.RADIX_READ_WAIT
)):
228 comb
+= Display("RDW %016x done %d "
229 "perm %d rc %d mbits %d rshift %d "
230 "valid %d leaf %d badtree %d",
231 data
, d_in
.done
, perm_ok
, rc_ok
,
232 mbits
, r
.shift
, valid
, leaf
, badtree
)
235 comb
+= v
.pde
.eq(data
)
238 comb
+= valid
.eq(data
[63]) # valid=data[63]
239 comb
+= leaf
.eq(data
[62]) # valid=data[63]
241 comb
+= v
.pde
.eq(data
)
245 # check permissions and RC bits
246 with m
.If(r
.priv | ~data
[3]):
248 comb
+= perm_ok
.eq(data
[1] |
(data
[2] & ~r
.store
))
250 # no IAMR, so no KUEP support for now
251 # deny execute permission if cache inhibited
252 comb
+= perm_ok
.eq(data
[0] & ~data
[5])
254 comb
+= rc_ok
.eq(data
[8] & (data
[7] | ~r
.store
))
255 with m
.If(perm_ok
& rc_ok
):
256 comb
+= v
.state
.eq(State
.RADIX_LOAD_TLB
)
258 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
259 comb
+= v
.perm_err
.eq(~perm_ok
)
260 # permission error takes precedence over RC error
261 comb
+= v
.rc_error
.eq(perm_ok
)
265 comb
+= mbits
.eq(data
[0:5])
266 comb
+= badtree
.eq((mbits
< 5) |
270 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
271 comb
+= v
.badtree
.eq(1)
273 comb
+= v
.shift
.eq(v
.shift
- mbits
)
274 comb
+= v
.mask_size
.eq(mbits
[0:5])
275 comb
+= v
.pgbase
.eq(Cat(C(0, 8), data
[8:56]))
276 comb
+= v
.state
.eq(State
.RADIX_LOOKUP
)
279 # non-present PTE, generate a DSI
280 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
281 comb
+= v
.invalid
.eq(1)
283 def segment_check(self
, m
, v
, r
, data
, finalmask
):
288 comb
+= mbits
.eq(r
.mask_size
)
289 comb
+= v
.shift
.eq(r
.shift
+ (31 - 12) - mbits
)
290 comb
+= nonzero
.eq((r
.addr
[31:62] & ~finalmask
[0:31]).bool())
291 with m
.If((r
.addr
[63] ^ r
.addr
[62]) | nonzero
):
292 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
293 comb
+= v
.segerror
.eq(1)
294 with m
.Elif((mbits
< 5) |
(mbits
> 16) |
295 (mbits
> (r
.shift
+ (31-12)))):
296 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
297 comb
+= v
.badtree
.eq(1)
299 comb
+= v
.state
.eq(State
.RADIX_LOOKUP
)
301 def mmu_0(self
, m
, r
, rin
, l_in
, l_out
, d_out
, addrsh
, mask
):
305 # Multiplex internal SPR values back to loadstore1,
306 # selected by l_in.sprn.
307 with m
.If(l_in
.sprn
[9]):
308 comb
+= l_out
.sprval
.eq(r
.prtbl
)
310 comb
+= l_out
.sprval
.eq(r
.pid
)
312 with m
.If(rin
.valid
):
313 sync
+= Display("MMU got tlb miss for %x", rin
.addr
)
315 with m
.If(l_out
.done
):
316 sync
+= Display("MMU completing op without error")
318 with m
.If(l_out
.err
):
319 sync
+= Display("MMU completing op with err invalid"
320 "%d badtree=%d", l_out
.invalid
, l_out
.badtree
)
322 with m
.If(rin
.state
== State
.RADIX_LOOKUP
):
323 sync
+= Display ("radix lookup shift=%d msize=%d",
324 rin
.shift
, rin
.mask_size
)
326 with m
.If(r
.state
== State
.RADIX_LOOKUP
):
327 sync
+= Display(f
"send load addr=%x addrsh=%d mask=%x",
328 d_out
.addr
, addrsh
, mask
)
331 def elaborate(self
, platform
):
339 finalmask
= Signal(44)
341 self
.rin
= rin
= RegStage("r_in")
350 self
.mmu_0(m
, r
, rin
, l_in
, l_out
, d_out
, addrsh
, mask
)
359 prtb_adr
= Signal(64)
360 pgtb_adr
= Signal(64)
362 tlb_data
= Signal(64)
366 comb
+= v
.valid
.eq(0)
370 comb
+= v
.invalid
.eq(0)
371 comb
+= v
.badtree
.eq(0)
372 comb
+= v
.segerror
.eq(0)
373 comb
+= v
.perm_err
.eq(0)
374 comb
+= v
.rc_error
.eq(0)
375 comb
+= tlb_load
.eq(0)
376 comb
+= itlb_load
.eq(0)
377 comb
+= tlbie_req
.eq(0)
378 comb
+= v
.inval_all
.eq(0)
379 comb
+= prtbl_rd
.eq(0)
381 # Radix tree data structures in memory are
382 # big-endian, so we need to byte-swap them
383 data
= byte_reverse(m
, "data", d_in
.data
, 8)
385 # generate mask for extracting address fields for PTE addr generation
386 m
.submodules
.pte_mask
= pte_mask
= Mask(16-5)
387 comb
+= pte_mask
.shift
.eq(r
.mask_size
- 5)
388 comb
+= mask
.eq(Cat(C(0x1f, 5), pte_mask
.mask
))
390 # generate mask for extracting address bits to go in
391 # TLB entry in order to support pages > 4kB
392 m
.submodules
.tlb_mask
= tlb_mask
= Mask(44)
393 comb
+= tlb_mask
.shift
.eq(r
.shift
)
394 comb
+= finalmask
.eq(tlb_mask
.mask
)
396 with m
.If(r
.state
!= State
.IDLE
):
397 sync
+= Display("MMU state %d %016x", r
.state
, data
)
399 with m
.Switch(r
.state
):
400 with m
.Case(State
.IDLE
):
401 self
.radix_tree_idle(m
, l_in
, r
, v
)
403 with m
.Case(State
.DO_TLBIE
):
405 comb
+= tlbie_req
.eq(1)
406 comb
+= v
.state
.eq(State
.TLB_WAIT
)
408 with m
.Case(State
.TLB_WAIT
):
409 with m
.If(d_in
.done
):
410 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
412 with m
.Case(State
.PROC_TBL_READ
):
413 sync
+= Display(" TBL_READ %016x", prtb_adr
)
415 comb
+= prtbl_rd
.eq(1)
416 comb
+= v
.state
.eq(State
.PROC_TBL_WAIT
)
418 with m
.Case(State
.PROC_TBL_WAIT
):
419 with m
.If(d_in
.done
):
420 self
.proc_tbl_wait(m
, v
, r
, data
)
423 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
424 comb
+= v
.badtree
.eq(1)
426 with m
.Case(State
.SEGMENT_CHECK
):
427 self
.segment_check(m
, v
, r
, data
, finalmask
)
429 with m
.Case(State
.RADIX_LOOKUP
):
430 sync
+= Display(" RADIX_LOOKUP")
432 comb
+= v
.state
.eq(State
.RADIX_READ_WAIT
)
434 with m
.Case(State
.RADIX_READ_WAIT
):
435 sync
+= Display(" READ_WAIT")
436 with m
.If(d_in
.done
):
437 self
.radix_read_wait(m
, v
, r
, d_in
, data
)
440 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
441 comb
+= v
.badtree
.eq(1)
443 with m
.Case(State
.RADIX_LOAD_TLB
):
444 comb
+= tlb_load
.eq(1)
447 comb
+= v
.state
.eq(State
.TLB_WAIT
)
449 comb
+= itlb_load
.eq(1)
450 comb
+= v
.state
.eq(State
.IDLE
)
452 with m
.Case(State
.RADIX_FINISH
):
453 sync
+= Display(" RADIX_FINISH")
454 comb
+= v
.state
.eq(State
.IDLE
)
456 with m
.If((v
.state
== State
.RADIX_FINISH
) |
457 ((v
.state
== State
.RADIX_LOAD_TLB
) & r
.iside
)):
458 comb
+= v
.err
.eq(v
.invalid | v
.badtree | v
.segerror
459 | v
.perm_err | v
.rc_error
)
460 comb
+= v
.done
.eq(~v
.err
)
462 with m
.If(~r
.addr
[63]):
463 comb
+= effpid
.eq(r
.pid
)
465 pr24
= Signal(24, reset_less
=True)
466 comb
+= pr24
.eq(masked(r
.prtbl
[12:36], effpid
[8:32], finalmask
))
467 comb
+= prtb_adr
.eq(Cat(C(0, 4), effpid
[0:8], pr24
, r
.prtbl
[36:56]))
469 pg16
= Signal(16, reset_less
=True)
470 comb
+= pg16
.eq(masked(r
.pgbase
[3:19], addrsh
, mask
))
471 comb
+= pgtb_adr
.eq(Cat(C(0, 3), pg16
, r
.pgbase
[19:56]))
473 pd44
= Signal(44, reset_less
=True)
474 comb
+= pd44
.eq(masked(r
.pde
[12:56], r
.addr
[12:56], finalmask
))
475 comb
+= pte
.eq(Cat(r
.pde
[0:12], pd44
))
481 with m
.If(tlbie_req
):
482 comb
+= addr
.eq(r
.addr
)
483 with m
.Elif(tlb_load
):
484 comb
+= addr
.eq(Cat(C(0, 12), r
.addr
[12:64]))
485 comb
+= tlb_data
.eq(pte
)
486 with m
.Elif(prtbl_rd
):
487 comb
+= addr
.eq(prtb_adr
)
489 comb
+= addr
.eq(pgtb_adr
)
491 comb
+= l_out
.done
.eq(r
.done
)
492 comb
+= l_out
.err
.eq(r
.err
)
493 comb
+= l_out
.invalid
.eq(r
.invalid
)
494 comb
+= l_out
.badtree
.eq(r
.badtree
)
495 comb
+= l_out
.segerr
.eq(r
.segerror
)
496 comb
+= l_out
.perm_error
.eq(r
.perm_err
)
497 comb
+= l_out
.rc_error
.eq(r
.rc_error
)
499 comb
+= d_out
.valid
.eq(dcreq
)
500 comb
+= d_out
.tlbie
.eq(tlbie_req
)
501 comb
+= d_out
.doall
.eq(r
.inval_all
)
502 comb
+= d_out
.tlbld
.eq(tlb_load
)
503 comb
+= d_out
.addr
.eq(addr
)
504 comb
+= d_out
.pte
.eq(tlb_data
)
506 comb
+= i_out
.tlbld
.eq(itlb_load
)
507 comb
+= i_out
.tlbie
.eq(tlbie_req
)
508 comb
+= i_out
.doall
.eq(r
.inval_all
)
509 comb
+= i_out
.addr
.eq(addr
)
510 comb
+= i_out
.pte
.eq(tlb_data
)
517 """simulator process for getting memory load requests
523 return int.from_bytes(x
.to_bytes(8, byteorder
='little'),
524 byteorder
='big', signed
=False)
526 mem
= {0x10000: # PARTITION_TABLE_2
527 # PATB_GR=1 PRTB=0x1000 PRTS=0xb
528 b(0x800000000100000b),
530 0x30000: # RADIX_ROOT_PTE
531 # V = 1 L = 0 NLB = 0x400 NLS = 9
532 b(0x8000000000040009),
534 0x1000000: # PROCESS_TABLE_3
535 # RTS1 = 0x2 RPDB = 0x300 RTS2 = 0x5 RPDS = 13
536 b(0x40000000000300ad),
540 while True: # wait for dc_valid
543 dc_valid
= yield (dut
.d_out
.valid
)
547 addr
= yield dut
.d_out
.addr
549 print (" DCACHE LOOKUP FAIL %x" % (addr
))
554 yield dut
.d_in
.data
.eq(data
)
555 print ("dcache get %x data %x" % (addr
, data
))
557 yield dut
.d_in
.done
.eq(1)
559 yield dut
.d_in
.done
.eq(0)
563 yield dut
.rin
.prtbl
.eq(0x1000000) # set process table
566 yield dut
.l_in
.load
.eq(1)
567 yield dut
.l_in
.priv
.eq(1)
568 yield dut
.l_in
.addr
.eq(0x10000)
569 yield dut
.l_in
.valid
.eq(1)
570 while True: # wait for dc_valid / err
571 l_done
= yield (dut
.l_out
.done
)
572 l_err
= yield (dut
.l_out
.err
)
573 l_badtree
= yield (dut
.l_out
.badtree
)
574 l_permerr
= yield (dut
.l_out
.perm_error
)
575 l_rc_err
= yield (dut
.l_out
.rc_error
)
576 l_segerr
= yield (dut
.l_out
.segerr
)
577 l_invalid
= yield (dut
.l_out
.invalid
)
578 if (l_done
or l_err
or l_badtree
or
579 l_permerr
or l_rc_err
or l_segerr
or l_invalid
):
582 addr
= yield dut
.d_out
.addr
583 pte
= yield dut
.d_out
.pte
584 print ("translated done %d err %d badtree %d addr %x pte %x" % \
585 (l_done
, l_err
, l_badtree
, addr
, pte
))
593 vl
= rtlil
.convert(dut
, ports
=[])#dut.ports())
594 with
open("test_mmu.il", "w") as f
:
598 m
.submodules
.mmu
= dut
604 sim
.add_sync_process(wrap(mmu_sim(dut
)))
605 sim
.add_sync_process(wrap(dcache_get(dut
)))
606 with sim
.write_vcd('test_mmu.vcd'):
609 if __name__
== '__main__':