Allow the formal engine to perform a same-cycle result in the ALU
[soc.git] / src / soc / fu / mmu / fsm.py
index c57c4a5845916d02b2eb741e18721e8becc13169..24be3f5402710bed1fb3a016e672ef12cc13671b 100644 (file)
+"""
+Based on microwatt mmu.vhdl
+
+* https://bugs.libre-soc.org/show_bug.cgi?id=491
+* https://bugs.libre-soc.org/show_bug.cgi?id=450
+"""
+
 from nmigen import Elaboratable, Module, Signal, Shape, unsigned, Cat, Mux
+from nmigen import Record, Memory
 from nmigen import Const
 from soc.fu.mmu.pipe_data import MMUInputData, MMUOutputData, MMUPipeSpec
 from nmutil.singlepipe import ControlBase
 from nmutil.util import rising_edge
 
 from soc.experiment.mmu import MMU
-from soc.experiment.dcache import DCache
-
-from soc.decoder.power_fields import DecodeFields
-from soc.decoder.power_fieldsn import SignalBitRange
-from soc.decoder.power_decoder2 import decode_spr_num
-from soc.decoder.power_enums import MicrOp, SPR, XER_bits
-
-from soc.experiment.pimem import PortInterface
-from soc.experiment.pimem import PortInterfaceBase
-
-from soc.experiment.mem_types import LoadStore1ToDCacheType, LoadStore1ToMMUType
-from soc.experiment.mem_types import DCacheToLoadStore1Type, MMUToLoadStore1Type
-
-# for testing purposes
-from soc.experiment.testmem import TestMemory
-
-# glue logic for microwatt mmu and dcache
-class LoadStore1(PortInterfaceBase):
-    def __init__(self, regwid=64, addrwid=4):
-        super().__init__(regwid, addrwid)
-        self.d_in  = LoadStore1ToDCacheType()
-        self.d_out = DCacheToLoadStore1Type()
-        self.l_in  = LoadStore1ToMMUType()
-        self.l_out = MMUToLoadStore1Type()
-        # for debugging with gtkwave only
-        self.debug1 = Signal()
-        self.debug2 = Signal()
-
-    def set_wr_addr(self, m, addr, mask):
-        #m.d.comb += self.d_in.valid.eq(1)
-        #m.d.comb += self.l_in.valid.eq(1)
-        #m.d.comb += self.d_in.load.eq(0)
-        #m.d.comb += self.l_in.load.eq(0)
-        m.d.comb += self.d_in.addr.eq(addr)
-        m.d.comb += self.l_in.addr.eq(addr)
-        # TODO set mask
-        return None
-
-    def set_rd_addr(self, m, addr, mask):
-        m.d.comb += self.d_in.valid.eq(1)
-        m.d.comb += self.l_in.valid.eq(1)
-        m.d.comb += self.d_in.load.eq(1)
-        m.d.comb += self.l_in.load.eq(1)
-        m.d.comb += self.d_in.addr.eq(addr)
-        m.d.comb += self.l_in.addr.eq(addr)
-        m.d.comb += self.debug1.eq(1)
-        # m.d.comb += self.debug2.eq(1)
-        # connect testmem first
-        return None #FIXME return value
-
-    def set_wr_data(self, m, data, wen):
-        m.d.comb += self.d_in.data.eq(data)
-        # TODO set wen
-        st_ok = Const(1, 1)
-        return st_ok
-
-    def get_rd_data(self, m):
-        ld_ok = Const(1, 1)
-        data = self.d_out.data
-        return data, ld_ok
-
-    def elaborate(self, platform):
-        m = super().elaborate(platform)
-
-        d_out = self.d_out
-        l_out = self.l_out
 
-        exc = self.pi.exception_o
+from openpower.consts import MSR
+from openpower.decoder.power_fields import DecodeFields
+from openpower.decoder.power_fieldsn import SignalBitRange
+from openpower.decoder.power_decoder2 import decode_spr_num
+from openpower.decoder.power_enums import MicrOp
 
-        #happened, alignment, instr_fault, invalid,
-        m.d.comb += exc.happened.eq(d_out.error | l_out.err)
-        m.d.comb += exc.invalid.eq(l_out.invalid)
+from soc.experiment.mem_types import LoadStore1ToMMUType
+from soc.experiment.mem_types import MMUToLoadStore1Type
 
-        #badtree, perm_error, rc_error, segment_fault
-        m.d.comb += exc.badtree.eq(l_out.badtree)
-        m.d.comb += exc.perm_error.eq(l_out.perm_error)
-        m.d.comb += exc.rc_error.eq(l_out.rc_error)
-        m.d.comb += exc.segment_fault.eq(l_out.segerr)
+from soc.fu.ldst.loadstore import LoadStore1, TestSRAMLoadStore1
+from nmutil.util import Display
 
-        # TODO connect those signals somewhere
-        #print(d_out.valid)         -> no error
-        #print(d_out.store_done)    -> no error
-        #print(d_out.cache_paradox) -> ?
-        #print(l_out.done)          -> no error
 
-        # TODO some exceptions set SPRs
+class FSMMMUStage(ControlBase):
+    """FSM MMU
 
-        return m
+    FSM-based MMU: must call set_ldst_interface and pass in an instance
+    of a LoadStore1.  this to comply with the ConfigMemoryPortInterface API
 
-    def ports(self):
-        yield from super().ports()
-        # TODO: memory ports
-
-class FSMMMUStage(ControlBase):
+    this Function Unit is extremely unusual in that it actually stores a
+    "thing" rather than "processes inputs and produces outputs".  hence
+    why it has to be a FSM.  linking up LD/ST however is going to have
+    to be done back in Issuer (or Core).  sorted: call set_ldst_interface
+    """
     def __init__(self, pspec):
         super().__init__()
         self.pspec = pspec
 
         # set up p/n data
-        self.p.data_i = MMUInputData(pspec)
-        self.n.data_o = MMUOutputData(pspec)
-
-        # incoming PortInterface
-        self.ldst = LoadStore1()       # TODO make this depend on pspec
-        self.pi = self.ldst.pi
-
-        # this Function Unit is extremely unusual in that it actually stores a
-        # "thing" rather than "processes inputs and produces outputs".  hence
-        # why it has to be a FSM.  linking up LD/ST however is going to have
-        # to be done back in Issuer (or Core)
+        self.p.i_data = MMUInputData(pspec)
+        self.n.o_data = MMUOutputData(pspec)
+        self.exc_o = self.n.o_data.exception # AllFunctionUnits needs this
 
         self.mmu = MMU()
-        self.dcache = DCache()
-        regwid=64
-        aw = 5
-        # for verification of DCache
-        # TODO: create connection to real memory, backend memory interface
-        self.testmem = TestMemory(regwid, aw, granularity=regwid//8, init=False)
-
-        # make life a bit easier in Core
-        self.pspec.mmu = self.mmu
-        self.pspec.dcache = self.dcache
 
         # debugging output for gtkw
         self.debug0 = Signal(4)
-        self.debug_wb_cyc = Signal()
-        self.debug_wb_stb = Signal()
-        self.debug_wb_we = Signal()
-        self.debug1 = Signal()
-        #self.debug2 = Signal(64)
-        #self.debug3 = Signal(64)
         self.illegal = Signal()
 
         # for SPR field number access
-        i = self.p.data_i
+        i = self.p.i_data
         self.fields = DecodeFields(SignalBitRange, [i.ctx.op.insn])
         self.fields.create_specs()
 
+    def set_ldst_interface(self, ldst):
+        """must be called back in Core, after FUs have been set up.
+        one of those will be the MMU (us!) but the LoadStore1 instance
+        must be set up in ConfigMemoryPortInterface. sigh.
+        """
+        # incoming PortInterface
+        self.ldst = ldst
+        self.dcache = self.ldst.dcache
+        self.icache = self.ldst.icache
+        self.pi = self.ldst.pi
+
     def elaborate(self, platform):
+        assert hasattr(self, "dcache"), "remember to call set_ldst_interface"
         m = super().elaborate(platform)
-        comb = m.d.comb
+        comb, sync = m.d.comb, m.d.sync
+        dcache, icache = self.dcache, self.icache
+        ldst = self.ldst # managed externally: do not add here
 
-        # link mmu and dcache together
-        m.submodules.dcache = dcache = self.dcache
+        # link mmu, dcache and icache together
         m.submodules.mmu = mmu = self.mmu
-        m.submodules.ldst = ldst = self.ldst
-        m.submodules.testmem = testmem = self.testmem
-        m.d.comb += dcache.m_in.eq(mmu.d_out)
-        m.d.comb += mmu.d_in.eq(dcache.m_out)
+        m.d.comb += dcache.m_in.eq(mmu.d_out) # MMUToDCacheType
+        m.d.comb += mmu.d_in.eq(dcache.m_out) # DCacheToMMUType
+        m.d.comb += icache.m_in.eq(mmu.i_out) # MMUToICacheType
+
         l_in, l_out = mmu.l_in, mmu.l_out
         d_in, d_out = dcache.d_in, dcache.d_out
-        wb_out, wb_in = dcache.wb_out, dcache.wb_in
-
-        # link ldst and dcache together
-        comb += l_in.eq(self.ldst.l_in)
-        comb += self.ldst.l_out.eq(l_out)
-        comb += d_in.eq(self.ldst.d_in)
-        comb += self.ldst.d_out.eq(self.dcache.d_out)
-
-        #connect read port
-        rdport = self.testmem.rdport
-        comb += rdport.addr.eq(wb_out.adr)
-        comb += wb_in.dat.eq(rdport.data)
-
-        #connect write port
-        wrport = self.testmem.wrport
-        comb += wrport.addr.eq(wb_out.adr)
-        comb += wrport.data.eq(wb_out.dat) # write st to mem
-        comb += wrport.en.eq(wb_out.cyc & wb_out.we) # enable writes
-
-        # connect DCache wishbone master to debugger
-        comb += self.debug_wb_cyc.eq(wb_out.cyc)
-        comb += self.debug_wb_stb.eq(wb_out.stb)
-        comb += self.debug_wb_we.eq(wb_out.we)
-
-        comb += wb_in.stall.eq(0)
-        # testmem only takes on cycle
-        with m.If( wb_out.cyc ):
-            m.d.sync += wb_in.ack.eq( wb_out.stb )
-
-        data_i, data_o = self.p.data_i, self.n.data_o
-        a_i, b_i, o = data_i.ra, data_i.rb, data_o.o
-        op = data_i.ctx.op
-
-        # TODO: link these SPRs somewhere
-        dsisr = Signal(64)
-        dar = Signal(64)
+
+        # link ldst and MMU together
+        comb += l_in.eq(ldst.m_out)
+        comb += ldst.m_in.eq(l_out)
+
+        i_data, o_data = self.p.i_data, self.n.o_data
+        op = i_data.ctx.op
+        cia_i = op.cia
+        msr_i = op.msr
+        a_i, b_i, spr1_i = i_data.ra, i_data.rb, i_data.spr1
+        o, exc_o, spr1_o = o_data.o, o_data.exception, o_data.spr1
 
         # busy/done signals
-        busy = Signal()
-        done = Signal()
-        m.d.comb += self.n.valid_o.eq(busy & done)
-        m.d.comb += self.p.ready_o.eq(~busy)
+        busy = Signal(name="mmu_fsm_busy")
+        done = Signal(name="mmu_fsm_done")
+        m.d.comb += self.n.o_valid.eq(busy & done)
+        m.d.comb += self.p.o_ready.eq(~busy)
 
         # take copy of X-Form SPR field
         x_fields = self.fields.FormXFX
@@ -211,8 +114,8 @@ class FSMMMUStage(ControlBase):
         m.d.comb += blip.eq(rising_edge(m, valid))
 
         with m.If(~busy):
-            with m.If(self.p.valid_i):
-                m.d.sync += busy.eq(1)
+            with m.If(self.p.i_valid):
+                sync += busy.eq(1)
         with m.Else():
 
             # based on the Micro-Op, we work out which of MMU or DCache
@@ -220,20 +123,41 @@ class FSMMMUStage(ControlBase):
             # enabled ("valid") and we twiddle our thumbs until it
             # responds ("done").
 
-            # FIXME: properly implement MicrOp.OP_MTSPR and MicrOp.OP_MFSPR
+            # WIP: properly implement MicrOp.OP_MTSPR and MicrOp.OP_MFSPR
 
             with m.Switch(op.insn_type):
+
+                ##########
+                # OP_MTSPR
+                ##########
+
                 with m.Case(MicrOp.OP_MTSPR):
+                    comb += Display("MMUTEST: OP_MTSPR: spr=%i", spr)
+                    # despite redirection this FU **MUST** behave exactly
+                    # like the SPR FU.  this **INCLUDES** updating the SPR
+                    # regfile because the CSV file entry for OP_MTSPR
+                    # categorically defines and requires the expectation
+                    # that the CompUnit **WILL** write to the regfile.
+                    comb += spr1_o.data.eq(a_i)
+                    comb += spr1_o.ok.eq(1)
                     # subset SPR: first check a few bits
+                    # XXX NOTE this must now cover **FOUR** values: this
+                    # test might not be adequate.  DSISR, DAR, PGTBL and PID
+                    # must ALL be covered here.
                     with m.If(~spr[9] & ~spr[5]):
                         comb += self.debug0.eq(3)
+                        #if matched update local cached value
+                        #commented out because there is a driver conflict
+                        comb += ldst.sprval_in.eq(a_i)
+                        comb += ldst.mmu_set_spr.eq(1)
                         with m.If(spr[0]):
-                            comb += dsisr.eq(a_i[:32])
+                            comb += ldst.mmu_set_dar.eq(1)
                         with m.Else():
-                            comb += dar.eq(a_i)
+                            comb += ldst.mmu_set_dsisr.eq(1)
                         comb += done.eq(1)
                     # pass it over to the MMU instead
                     with m.Else():
+                        # PGTBL and PID
                         comb += self.debug0.eq(4)
                         # blip the MMU and wait for it to complete
                         comb += valid.eq(1)   # start "pulse"
@@ -243,43 +167,42 @@ class FSMMMUStage(ControlBase):
                         comb += l_in.rs.eq(a_i)    # incoming operand (RS)
                         comb += done.eq(1) # FIXME l_out.done
 
+                ##########
+                # OP_MFSPR
+                ##########
+
                 with m.Case(MicrOp.OP_MFSPR):
-                    # subset SPR: first check a few bits
-                    with m.If(~spr[9] & ~spr[5]):
-                        comb += self.debug0.eq(5)
+                    comb += Display("MMUTEST: OP_MFSPR: spr=%i returns=%i",
+                                    spr, spr1_i)
+                    # partial SPR number decoding perfectly fine
+                    with m.If(spr[9] | spr[5]):
+                        # identified as an MMU OP_MFSPR, contact the MMU.
+                        # interestingly, the read is combinatorial: no need
+                        # to set "valid", just set the SPR number
+                        comb += l_in.sprn.eq(spr)  # which SPR
+                        comb += o.data.eq(l_out.sprval)
+                    with m.Else():
+                        # identified as DSISR or DAR.  again: read the SPR
+                        # directly, combinatorial access
                         with m.If(spr[0]):
-                            comb += o.data.eq(dsisr)
+                            comb += o.data.eq(ldst.dar)
                         with m.Else():
-                            comb += o.data.eq(dar)
-                        #FIXME comb += o.ok.eq(1)
-                        comb += done.eq(1)
-                    # pass it over to the MMU instead
-                    with m.Else():
-                        comb += self.debug0.eq(6)
-                        # blip the MMU and wait for it to complete
-                        comb += valid.eq(1)   # start "pulse"
-                        comb += l_in.valid.eq(blip)   # start
-                        comb += l_in.mtspr.eq(0)   # mfspr!=mtspr
-                        comb += l_in.sprn.eq(spr)  # which SPR
-                        comb += l_in.rs.eq(a_i)    # incoming operand (RS)
-                        comb += o.data.eq(l_out.sprval) # SPR from MMU
-                        comb += o.ok.eq(l_out.done) # only when l_out valid
-                        comb += done.eq(1) # FIXME l_out.done
+                            comb += o.data.eq(ldst.dsisr)
 
-                with m.Case(MicrOp.OP_DCBZ):
-                    # activate dcbz mode (spec: v3.0B p850)
-                    comb += valid.eq(1)   # start "pulse"
-                    comb += d_in.valid.eq(blip)     # start
-                    comb += d_in.dcbz.eq(1)         # dcbz mode
-                    comb += d_in.addr.eq(a_i + b_i) # addr is (RA|0) + RB
-                    comb += done.eq(d_out.store_done)     # TODO
-                    comb += self.debug0.eq(1)
+                    comb += o.ok.eq(1)
+                    comb += done.eq(1)
+
+                ##########
+                # OP_TLBIE
+                ##########
 
                 with m.Case(MicrOp.OP_TLBIE):
+                    comb += Display("MMUTEST: OP_TLBIE: insn_bits=%i", spr)
                     # pass TLBIE request to MMU (spec: v3.0B p1034)
                     # note that the spr is *not* an actual spr number, it's
                     # just that those bits happen to match with field bits
                     # RIC, PRS, R
+                    comb += Display("TLBIE: %i %i", spr, l_out.done)
                     comb += valid.eq(1)   # start "pulse"
                     comb += l_in.valid.eq(blip)   # start
                     comb += l_in.tlbie.eq(1)   # mtspr mode
@@ -287,11 +210,39 @@ class FSMMMUStage(ControlBase):
                     comb += l_in.addr.eq(b_i)  # incoming operand (RB)
                     comb += done.eq(l_out.done) # zzzz
                     comb += self.debug0.eq(2)
+
+                ##########
+                # OP_FETCH_FAILED
+                ##########
+
+                with m.Case(MicrOp.OP_FETCH_FAILED):
+                    comb += Display("MMUTEST: OP_FETCH_FAILED: @%x", cia_i)
+                    # trigger an instruction fetch failed MMU event.
+                    # PowerDecoder2 drops svstate.pc into NIA for us
+                    # really, this should be direct communication with the
+                    # MMU, rather than going through LoadStore1.  but, doing
+                    # so allows for the opportunity to prevent LoadStore1
+                    # from accepting any other LD/ST requests.
+                    comb += valid.eq(1)   # start "pulse"
+                    comb += ldst.instr_fault.eq(blip)
+                    comb += ldst.priv_mode.eq(~msr_i[MSR.PR])
+                    comb += ldst.maddr.eq(cia_i)
+                    # XXX should not access this!
+                    comb += done.eq(ldst.done)
+                    comb += self.debug0.eq(3)
+                    # LDST unit contains exception data, which (messily)
+                    # is copied over, here.  not ideal but it will do for now
+                    comb += exc_o.eq(ldst.pi.exc_o)
+
+                ############
+                # OP_ILLEGAL
+                ############
+
                 with m.Case(MicrOp.OP_ILLEGAL):
                     comb += self.illegal.eq(1)
 
-            with m.If(self.n.ready_i & self.n.valid_o):
-                m.d.sync += busy.eq(0)
+            with m.If(self.n.i_ready & self.n.o_valid):
+                sync += busy.eq(0)
 
         return m