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]):
203 comb
+= perm_ok
.eq((data
[1] | data
[2]) & (~r
.store
))
205 # no IAMR, so no KUEP support
206 # for now deny execute
207 # permission if cache inhibited
208 comb
+= perm_ok
.eq(data
[0] & ~data
[5])
211 comb
+= rc_ok
.eq(data
[8] & (data
[7] |
(~r
.store
)))
212 with m
.If(perm_ok
& rc_ok
):
213 comb
+= v
.state
.eq(State
.RADIX_LOAD_TLB
)
215 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
216 comb
+= v
.perm_err
.eq(~perm_ok
)
217 # permission error takes precedence
219 comb
+= v
.rc_error
.eq(perm_ok
)
222 comb
+= mbits
.eq(data
[0:5])
223 with m
.If((mbits
< 5) |
(mbits
> 16) |
(mbits
> r
.shift
)):
224 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
225 comb
+= v
.badtree
.eq(1)
227 comb
+= v
.shift
.eq(v
.shift
- mbits
)
228 comb
+= v
.mask_size
.eq(mbits
[0:5])
229 comb
+= v
.pgbase
.eq(Cat(C(0, 8), data
[8:56]))
230 comb
+= v
.state
.eq(State
.RADIX_LOOKUP
)
232 def segment_check(self
, m
, v
, r
, data
, finalmask
):
236 comb
+= mbits
.eq(r
.mask_size
)
237 comb
+= v
.shift
.eq(r
.shift
+ (31 - 12) - mbits
)
238 comb
+= nonzero
.eq((r
.addr
[31:62] & ~finalmask
[0:31]).bool())
239 with m
.If((r
.addr
[63] ^ r
.addr
[62]) | nonzero
):
240 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
241 comb
+= v
.segerror
.eq(1)
242 with m
.Elif((mbits
< 5) |
(mbits
> 16) |
243 (mbits
> (r
.shift
+ (31-12)))):
244 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
245 comb
+= v
.badtree
.eq(1)
247 comb
+= v
.state
.eq(State
.RADIX_LOOKUP
)
249 def elaborate(self
, platform
):
257 finalmask
= Signal(44)
260 rin
= RegStage("r_in")
268 # Multiplex internal SPR values back to loadstore1,
269 # selected by l_in.sprn.
270 with m
.If(l_in
.sprn
[9]):
271 comb
+= l_out
.sprval
.eq(r
.prtbl
)
273 comb
+= l_out
.sprval
.eq(r
.pid
)
275 with m
.If(rin
.valid
):
277 #sync += Display(f"MMU got tlb miss for {rin.addr}")
279 with m
.If(l_out
.done
):
281 # sync += Display("MMU completing op without error")
283 with m
.If(l_out
.err
):
285 # sync += Display(f"MMU completing op with err invalid"
286 # "{l_out.invalid} badtree={l_out.badtree}")
288 with m
.If(rin
.state
== State
.RADIX_LOOKUP
):
290 # sync += Display (f"radix lookup shift={rin.shift}"
291 # "msize={rin.mask_size}")
293 with m
.If(r
.state
== State
.RADIX_LOOKUP
):
295 # sync += Display(f"send load addr={d_out.addr}"
296 # "addrsh={addrsh} mask={mask}")
307 prtable_addr
= Signal(64)
308 pgtable_addr
= Signal(64)
310 tlb_data
= Signal(64)
314 comb
+= v
.valid
.eq(0)
318 comb
+= v
.invalid
.eq(0)
319 comb
+= v
.badtree
.eq(0)
320 comb
+= v
.segerror
.eq(0)
321 comb
+= v
.perm_err
.eq(0)
322 comb
+= v
.rc_error
.eq(0)
323 comb
+= tlb_load
.eq(0)
324 comb
+= itlb_load
.eq(0)
325 comb
+= tlbie_req
.eq(0)
326 comb
+= v
.inval_all
.eq(0)
327 comb
+= prtbl_rd
.eq(0)
329 # Radix tree data structures in memory are
330 # big-endian, so we need to byte-swap them
331 data
= byte_reverse(m
, "data", d_in
.data
, 8)
333 # generate mask for extracting address fields for PTE addr generation
334 comb
+= mask
.eq(Cat(C(0x1f,5), ((1<<r
.mask_size
)-1)))
336 # generate mask for extracting address bits to go in
337 # TLB entry in order to support pages > 4kB
338 comb
+= finalmask
.eq(((1<<r
.shift
)-1))
340 with m
.Switch(r
.state
):
341 with m
.Case(State
.IDLE
):
342 self
.radix_tree_idle(m
, l_in
, r
, v
)
344 with m
.Case(State
.DO_TLBIE
):
346 comb
+= tlbie_req
.eq(1)
347 comb
+= v
.state
.eq(State
.TLB_WAIT
)
349 with m
.Case(State
.TLB_WAIT
):
350 with m
.If(d_in
.done
):
351 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
353 with m
.Case(State
.PROC_TBL_READ
):
355 comb
+= prtbl_rd
.eq(1)
356 comb
+= v
.state
.eq(State
.PROC_TBL_WAIT
)
358 with m
.Case(State
.PROC_TBL_WAIT
):
359 with m
.If(d_in
.done
):
360 self
.proc_tbl_wait(m
, v
, r
, data
)
363 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
364 comb
+= v
.badtree
.eq(1)
366 with m
.Case(State
.SEGMENT_CHECK
):
367 self
.segment_check(m
, v
, r
, data
, finalmask
)
369 with m
.Case(State
.RADIX_LOOKUP
):
371 comb
+= v
.state
.eq(State
.RADIX_READ_WAIT
)
373 with m
.Case(State
.RADIX_READ_WAIT
):
374 with m
.If(d_in
.done
):
375 self
.radix_read_wait(m
, v
, r
, d_in
, data
)
377 # non-present PTE, generate a DSI
378 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
379 comb
+= v
.invalid
.eq(1)
382 comb
+= v
.state
.eq(State
.RADIX_FINISH
)
383 comb
+= v
.badtree
.eq(1)
385 with m
.Case(State
.RADIX_LOAD_TLB
):
386 comb
+= tlb_load
.eq(1)
389 comb
+= v
.state
.eq(State
.TLB_WAIT
)
391 comb
+= itlb_load
.eq(1)
392 comb
+= v
.state
.eq(State
.IDLE
)
394 with m
.Case(State
.RADIX_FINISH
):
395 comb
+= v
.state
.eq(State
.IDLE
)
397 with m
.If((v
.state
== State
.RADIX_FINISH
) |
398 ((v
.state
== State
.RADIX_LOAD_TLB
) & r
.iside
)):
399 comb
+= v
.err
.eq(v
.invalid | v
.badtree | v
.segerror
400 | v
.perm_err | v
.rc_error
)
401 comb
+= v
.done
.eq(~v
.err
)
403 with m
.If(~r
.addr
[63]):
404 comb
+= effpid
.eq(r
.pid
)
406 comb
+= prtable_addr
.eq(Cat(
409 (r
.prtbl
[12:36] & ~finalmask
[0:24]) |
410 (effpid
[8:32] & finalmask
[0:24]),
414 comb
+= pgtable_addr
.eq(Cat(
416 (r
.pgbase
[3:19] & ~mask
) |
423 (r
.pde
[12:56] & ~finalmask
) |
424 (r
.addr
[12:56] & finalmask
),
431 with m
.If(tlbie_req
):
432 comb
+= addr
.eq(r
.addr
)
433 with m
.Elif(tlb_load
):
434 comb
+= addr
.eq(Cat(C(0, 12), r
.addr
[12:64]))
435 comb
+= tlb_data
.eq(pte
)
436 with m
.Elif(prtbl_rd
):
437 comb
+= addr
.eq(prtable_addr
)
439 comb
+= addr
.eq(pgtable_addr
)
441 comb
+= l_out
.done
.eq(r
.done
)
442 comb
+= l_out
.err
.eq(r
.err
)
443 comb
+= l_out
.invalid
.eq(r
.invalid
)
444 comb
+= l_out
.badtree
.eq(r
.badtree
)
445 comb
+= l_out
.segerr
.eq(r
.segerror
)
446 comb
+= l_out
.perm_error
.eq(r
.perm_err
)
447 comb
+= l_out
.rc_error
.eq(r
.rc_error
)
449 comb
+= d_out
.valid
.eq(dcreq
)
450 comb
+= d_out
.tlbie
.eq(tlbie_req
)
451 comb
+= d_out
.doall
.eq(r
.inval_all
)
452 comb
+= d_out
.tlbld
.eq(tlb_load
)
453 comb
+= d_out
.addr
.eq(addr
)
454 comb
+= d_out
.pte
.eq(tlb_data
)
456 comb
+= i_out
.tlbld
.eq(itlb_load
)
457 comb
+= i_out
.tlbie
.eq(tlbie_req
)
458 comb
+= i_out
.doall
.eq(r
.inval_all
)
459 comb
+= i_out
.addr
.eq(addr
)
460 comb
+= i_out
.pte
.eq(tlb_data
)
467 yield wp
.data_i
.eq(2)
474 data
= yield rp
.data_o
483 yield wp
.data_i
.eq(6)
485 data
= yield rp
.data_o
492 data
= yield rp
.data_o
496 data
= yield rp
.data_o
501 vl
= rtlil
.convert(dut
, ports
=[])#dut.ports())
502 with
open("test_mmu.il", "w") as f
:
505 run_simulation(dut
, mmu_sim(), vcd_name
='test_mmu.vcd')
507 if __name__
== '__main__':