3 based on Anton Blanchard microwatt mmu.vhdl
6 from enum
import Enum
, unique
7 from nmigen
import (C
, Module
, Signal
, Elaboratable
, Mux
, Cat
, Repl
, Signal
)
8 from nmigen
.cli
import main
9 from nmigen
.cli
import rtlil
10 from nmutil
.iocontrol
import RecordObject
11 from nmutil
.byterev
import byte_reverse
13 from soc
.experiment
.mem_types
import (LoadStore1ToMmuType
,
20 # -- Supports 4-level trees as in arch 3.0B, but not the
21 # -- two-step translation
22 # -- for guests under a hypervisor (i.e. there is no gRA -> hRA translation).
26 IDLE
= 0 # zero is default on reset for r.state
38 class RegStage(RecordObject
):
39 def __init__(self
, name
=None):
40 super().__init
__(name
=name
)
41 # latched request from loadstore1
46 self
.addr
= Signal(64)
47 self
.inval_all
= Signal()
49 self
.prtbl
= Signal(64)
52 self
.state
= Signal(State
) # resets to IDLE
55 self
.pgtbl0
= Signal(64)
56 self
.pt0_valid
= Signal()
57 self
.pgtbl3
= Signal(64)
58 self
.pt3_valid
= Signal()
59 self
.shift
= Signal(6)
60 self
.mask_size
= Signal(5)
61 self
.pgbase
= Signal(56)
63 self
.invalid
= Signal()
64 self
.badtree
= Signal()
65 self
.segerror
= Signal()
66 self
.perm_err
= Signal()
67 self
.rc_error
= Signal()
70 class MMU(Elaboratable
):
73 Supports 4-level trees as in arch 3.0B, but not the
74 two-step translation for guests under a hypervisor
75 (i.e. there is no gRA -> hRA translation).
78 self
.l_in
= LoadStore1ToMmuType()
79 self
.l_out
= MmuToLoadStore1Type()
80 self
.d_out
= MmuToDcacheType()
81 self
.d_in
= DcacheToMmuType()
82 self
.i_out
= MmuToIcacheType()
84 def radix_tree_idle(self
, m
, l_in
, r
, v
):
88 with m
.If(~l_in
.addr
[63]):
89 comb
+= pgtbl
.eq(r
.pgtbl0
)
90 comb
+= pt_valid
.eq(r
.pt0_valid
)
92 comb
+= pgtbl
.eq(r
.pt3_valid
)
93 comb
+= pt_valid
.eq(r
.pt3_valid
)
95 # rts == radix tree size, number of address bits
98 comb
+= rts
.eq(Cat(pgtbl
[5:8], pgtbl
[61:63]))
100 # mbits == number of address bits to index top
103 comb
+= mbits
.eq(pgtbl
[0:5])
105 # set v.shift to rts so that we can use finalmask
106 # for the segment check
107 comb
+= v
.shift
.eq(rts
)
108 comb
+= v
.mask_size
.eq(mbits
[0:5])
109 comb
+= v
.pgbase
.eq(Cat(C(0, 8), pgtbl
[8:56]))
111 with m
.If(l_in
.valid
):
112 comb
+= v
.addr
.eq(l_in
.addr
)
113 comb
+= v
.iside
.eq(l_in
.iside
)
114 comb
+= v
.store
.eq(~
(l_in
.load | l_in
.iside
))
116 with m
.If(l_in
.tlbie
):
117 # Invalidate all iTLB/dTLB entries for
118 # tlbie with RB[IS] != 0 or RB[AP] != 0,
120 comb
+= v
.inval_all
.eq(l_in
.slbia
127 # The RIC field of the tlbie instruction
128 # comes across on the sprn bus as bits 2--3.
129 # RIC=2 flushes process table caches.
130 with m
.If(l_in
.sprn
[3]):
131 comb
+= v
.pt0_valid
.eq(0)
132 comb
+= v
.pt3_valid
.eq(0)
133 comb
+= v
.state
.eq(State
.DO_TLBIE
)
135 comb
+= v
.valid
.eq(1)
136 with m
.If(~pt_valid
):
137 # need to fetch process table entry
138 # set v.shift so we can use finalmask
139 # for generating the process table
141 comb
+= v
.shift
.eq(r
.prtbl
[0:5])
142 comb
+= v
.state
.eq(State
.PROC_TBL_READ
)
145 # Use RPDS = 0 to disable radix tree walks
146 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
147 comb
+= v
.invalid
.eq(1)
149 comb
+= v
.state
.eq(State
.SEGMENT_CHECK
)
151 with m
.If(l_in
.mtspr
):
152 # Move to PID needs to invalidate L1 TLBs
153 # and cached pgtbl0 value. Move to PRTBL
154 # does that plus invalidating the cached
155 # pgtbl3 value as well.
156 with m
.If(~l_in
.sprn
[9]):
157 comb
+= v
.pid
.eq(l_in
.rs
[0:32])
159 comb
+= v
.prtbl
.eq(l_in
.rs
)
160 comb
+= v
.pt3_valid
.eq(0)
162 comb
+= v
.pt0_valid
.eq(0)
163 comb
+= v
.inval_all
.eq(1)
164 comb
+= v
.state
.eq(State
.DO_TLBIE
)
166 def proc_tbl_wait(self
, m
, v
, r
, data
):
168 with m
.If(r
.addr
[63]):
169 comb
+= v
.pgtbl3
.eq(data
)
170 comb
+= v
.pt3_valid
.eq(1)
172 comb
+= v
.pgtbl0
.eq(data
)
173 comb
+= v
.pt0_valid
.eq(1)
174 # rts == radix tree size, # address bits being translated
176 comb
+= rts
.eq(Cat(data
[5:8], data
[61:63]))
178 # mbits == # address bits to index top level of tree
180 comb
+= mbits
.eq(data
[0:5])
181 # set v.shift to rts so that we can use
182 # finalmask for the segment check
183 comb
+= v
.shift
.eq(rts
)
184 comb
+= v
.mask_size
.eq(mbits
[0:5])
185 comb
+= v
.pgbase
.eq(Cat(C(0, 8), data
[8:56]))
188 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
189 comb
+= v
.invalid
.eq(1)
190 comb
+= v
.state
.eq(State
.SEGMENT_CHECK
)
192 def radix_read_wait(self
, m
, v
, r
, d_in
, data
):
194 comb
+= v
.pde
.eq(data
)
198 # check permissions and RC bits
200 comb
+= perm_ok
.eq(0)
201 with m
.If(r
.priv | ~data
[3]):
208 # no IAMR, so no KUEP support
209 # for now deny execute
210 # permission if cache inhibited
211 comb
+= perm_ok
.eq(data
[0] & ~data
[5])
214 comb
+= rc_ok
.eq(data
[8] & (data
[7] |
(~r
.store
)))
215 with m
.If(perm_ok
& rc_ok
):
216 comb
+= v
.state
.eq(State
.RADIX_LOAD_TLB
)
218 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
219 comb
+= v
.perm_err
.eq(~perm_ok
)
220 # permission error takes precedence
222 comb
+= v
.rc_error
.eq(perm_ok
)
225 comb
+= mbits
.eq(data
[0:5])
226 with m
.If((mbits
< 5) |
(mbits
> 16) |
(mbits
> r
.shift
)):
227 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
228 comb
+= v
.badtree
.eq(1)
230 comb
+= v
.shift
.eq(v
.shift
- mbits
)
231 comb
+= v
.mask_size
.eq(mbits
[0:5])
232 comb
+= v
.pgbase
.eq(Cat(C(0, 8), data
[8:56]))
233 comb
+= v
.state
.eq(State
.RADIX_LOOKUP
)
235 def segment_check(self
, m
, v
, r
, data
, finalmask
):
239 comb
+= mbits
.eq(r
.mask_size
)
240 comb
+= v
.shift
.eq(r
.shift
+ (31 - 12) - mbits
)
241 comb
+= nonzero
.eq((r
.addr
[31:62] & ~finalmask
[0:31]).bool())
242 with m
.If((r
.addr
[63] ^ r
.addr
[62]) | nonzero
):
243 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
244 comb
+= v
.segerror
.eq(1)
245 with m
.Elif((mbits
< 5) |
(mbits
> 16) |
246 (mbits
> (r
.shift
+ (31-12)))):
247 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
248 comb
+= v
.badtree
.eq(1)
250 comb
+= v
.state
.eq(State
.RADIX_LOOKUP
)
252 def elaborate(self
, platform
):
260 finalmask
= Signal(44)
263 rin
= RegStage("r_in")
271 # Multiplex internal SPR values back to loadstore1,
272 # selected by l_in.sprn.
273 with m
.If(l_in
.sprn
[9]):
274 comb
+= l_out
.sprval
.eq(r
.prtbl
)
276 comb
+= l_out
.sprval
.eq(r
.pid
)
278 with m
.If(rin
.valid
):
280 #sync += Display(f"MMU got tlb miss for {rin.addr}")
282 with m
.If(l_out
.done
):
284 # sync += Display("MMU completing op without error")
286 with m
.If(l_out
.err
):
288 # sync += Display(f"MMU completing op with err invalid"
289 # "{l_out.invalid} badtree={l_out.badtree}")
291 with m
.If(rin
.state
== State
.RADIX_LOOKUP
):
293 # sync += Display (f"radix lookup shift={rin.shift}"
294 # "msize={rin.mask_size}")
296 with m
.If(r
.state
== State
.RADIX_LOOKUP
):
298 # sync += Display(f"send load addr={d_out.addr}"
299 # "addrsh={addrsh} mask={mask}")
310 prtable_addr
= Signal(64)
311 pgtable_addr
= Signal(64)
313 tlb_data
= Signal(64)
317 comb
+= v
.valid
.eq(0)
321 comb
+= v
.invalid
.eq(0)
322 comb
+= v
.badtree
.eq(0)
323 comb
+= v
.segerror
.eq(0)
324 comb
+= v
.perm_err
.eq(0)
325 comb
+= v
.rc_error
.eq(0)
326 comb
+= tlb_load
.eq(0)
327 comb
+= itlb_load
.eq(0)
328 comb
+= tlbie_req
.eq(0)
329 comb
+= v
.inval_all
.eq(0)
330 comb
+= prtbl_rd
.eq(0)
332 # Radix tree data structures in memory are
333 # big-endian, so we need to byte-swap them
334 data
= byte_reverse(m
, "data", d_in
.data
, 8)
336 # generate mask for extracting address fields for PTE addr generation
337 comb
+= mask
.eq(Cat(C(0x1f,5), ((1<<r
.mask_size
)-1)))
339 # generate mask for extracting address bits to go in
340 # TLB entry in order to support pages > 4kB
341 comb
+= finalmask
.eq(((1<<r
.shift
)-1))
343 with m
.Switch(r
.state
):
344 with m
.Case(State
.IDLE
):
345 self
.radix_tree_idle(m
, l_in
, r
, v
)
347 with m
.Case(State
.DO_TLBIE
):
349 comb
+= tlbie_req
.eq(1)
350 comb
+= v
.state
.eq(State
.TLB_WAIT
)
352 with m
.Case(State
.TLB_WAIT
):
353 with m
.If(d_in
.done
):
354 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
356 with m
.Case(State
.PROC_TBL_READ
):
358 comb
+= prtbl_rd
.eq(1)
359 comb
+= v
.state
.eq(State
.PROC_TBL_WAIT
)
361 with m
.Case(State
.PROC_TBL_WAIT
):
362 with m
.If(d_in
.done
):
363 self
.proc_tbl_wait(m
, v
, r
, data
)
366 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
367 comb
+= v
.badtree
.eq(1)
369 with m
.Case(State
.SEGMENT_CHECK
):
370 self
.segment_check(m
, v
, r
, data
, finalmask
)
372 with m
.Case(State
.RADIX_LOOKUP
):
374 comb
+= v
.state
.eq(State
.RADIX_READ_WAIT
)
376 with m
.Case(State
.RADIX_READ_WAIT
):
377 with m
.If(d_in
.done
):
378 self
.radix_read_wait(m
, v
, r
, d_in
, data
)
380 # non-present PTE, generate a DSI
381 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
382 comb
+= v
.invalid
.eq(1)
385 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
386 comb
+= v
.badtree
.eq(1)
388 with m
.Case(State
.RADIX_LOAD_TLB
):
389 comb
+= tlb_load
.eq(1)
392 comb
+= v
.state
.eq(State
.TLB_WAIT
)
394 comb
+= itlb_load
.eq(1)
395 comb
+= v
.state
.eq(State
.IDLE
)
397 with m
.Case(State
.RADIX_FINISH
):
398 comb
+= v
.state
.eq(State
.IDLE
)
400 with m
.If((v
.state
== State
.RADIX_FINISH
) |
401 ((v
.state
== State
.RADIX_LOAD_TLB
) & r
.iside
)):
402 comb
+= v
.err
.eq(v
.invalid | v
.badtree | v
.segerror
403 | v
.perm_err | v
.rc_error
)
404 comb
+= v
.done
.eq(~v
.err
)
406 with m
.If(~r
.addr
[63]):
407 comb
+= effpid
.eq(r
.pid
)
409 comb
+= prtable_addr
.eq(Cat(
412 (r
.prtbl
[12:36] & ~finalmask
[0:24]) |
413 (effpid
[8:32] & finalmask
[0:24]),
417 comb
+= pgtable_addr
.eq(Cat(
419 (r
.pgbase
[3:19] & ~mask
) |
426 (r
.pde
[12:56] & ~finalmask
) |
427 (r
.addr
[12:56] & finalmask
),
434 with m
.If(tlbie_req
):
435 comb
+= addr
.eq(r
.addr
)
436 with m
.Elif(tlb_load
):
437 comb
+= addr
.eq(Cat(C(0, 12), r
.addr
[12:64]))
438 comb
+= tlb_data
.eq(pte
)
439 with m
.Elif(prtbl_rd
):
440 comb
+= addr
.eq(prtable_addr
)
442 comb
+= addr
.eq(pgtable_addr
)
444 comb
+= l_out
.done
.eq(r
.done
)
445 comb
+= l_out
.err
.eq(r
.err
)
446 comb
+= l_out
.invalid
.eq(r
.invalid
)
447 comb
+= l_out
.badtree
.eq(r
.badtree
)
448 comb
+= l_out
.segerr
.eq(r
.segerror
)
449 comb
+= l_out
.perm_error
.eq(r
.perm_err
)
450 comb
+= l_out
.rc_error
.eq(r
.rc_error
)
452 comb
+= d_out
.valid
.eq(dcreq
)
453 comb
+= d_out
.tlbie
.eq(tlbie_req
)
454 comb
+= d_out
.doall
.eq(r
.inval_all
)
455 comb
+= d_out
.tlbld
.eq(tlb_load
)
456 comb
+= d_out
.addr
.eq(addr
)
457 comb
+= d_out
.pte
.eq(tlb_data
)
459 comb
+= i_out
.tlbld
.eq(itlb_load
)
460 comb
+= i_out
.tlbie
.eq(tlbie_req
)
461 comb
+= i_out
.doall
.eq(r
.inval_all
)
462 comb
+= i_out
.addr
.eq(addr
)
463 comb
+= i_out
.pte
.eq(tlb_data
)
470 yield wp
.data_i
.eq(2)
477 data
= yield rp
.data_o
486 yield wp
.data_i
.eq(6)
488 data
= yield rp
.data_o
495 data
= yield rp
.data_o
499 data
= yield rp
.data_o
504 vl
= rtlil
.convert(dut
, ports
=[])#dut.ports())
505 with
open("test_mmu.il", "w") as f
:
508 run_simulation(dut
, mmu_sim(), vcd_name
='test_mmu.vcd')
510 if __name__
== '__main__':