3 based on microwatt loadstore1.vhdl, but conforming to PortInterface.
4 unlike loadstore1.vhdl this does *not* deal with actual Load/Store
5 ops: that job is handled by LDSTCompUnit, which talks to LoadStore1
6 by way of PortInterface. PortInterface is where things need extending,
7 such as adding dcbz support, etc.
9 this module basically handles "pure" load / store operations, and
10 its first job is to ask the D-Cache for the data. if that fails,
11 the second task (if virtual memory is enabled) is to ask the MMU
12 to perform a TLB, then to go *back* to the cache and ask again.
16 * https://bugs.libre-soc.org/show_bug.cgi?id=465
20 from nmigen
import (Elaboratable
, Module
, Signal
, Shape
, unsigned
, Cat
, Mux
,
23 from nmutil
.util
import rising_edge
24 from enum
import Enum
, unique
26 from soc
.experiment
.dcache
import DCache
27 from soc
.experiment
.pimem
import PortInterfaceBase
28 from soc
.experiment
.mem_types
import LoadStore1ToMMUType
29 from soc
.experiment
.mem_types
import MMUToLoadStore1Type
31 from soc
.minerva
.wishbone
import make_wb_layout
32 from soc
.bus
.sram
import SRAM
37 IDLE
= 0 # ready for instruction
38 ACK_WAIT
= 1 # waiting for ack from dcache
39 MMU_LOOKUP
= 2 # waiting for MMU to look up translation
40 TLBIE_WAIT
= 3 # waiting for MMU to finish doing a tlbie
43 # glue logic for microwatt mmu and dcache
44 class LoadStore1(PortInterfaceBase
):
45 def __init__(self
, pspec
):
47 self
.disable_cache
= (hasattr(pspec
, "disable_cache") and
48 pspec
.disable_cache
== True)
49 regwid
= pspec
.reg_wid
50 addrwid
= pspec
.addr_wid
52 super().__init
__(regwid
, addrwid
)
53 self
.dcache
= DCache()
54 # these names are from the perspective of here (LoadStore1)
55 self
.d_out
= self
.dcache
.d_in
# in to dcache is out for LoadStore
56 self
.d_in
= self
.dcache
.d_out
# out from dcache is in for LoadStore
57 self
.m_out
= LoadStore1ToMMUType() # out *to* MMU
58 self
.m_in
= MMUToLoadStore1Type() # in *from* MMU
60 # TODO, convert dcache wb_in/wb_out to "standard" nmigen Wishbone bus
61 self
.dbus
= Record(make_wb_layout(pspec
))
63 # for creating a single clock blip to DCache
64 self
.d_valid
= Signal()
65 self
.d_w_valid
= Signal()
66 self
.d_validblip
= Signal()
68 # DSISR and DAR cached values. note that the MMU FSM is where
69 # these are accessed by OP_MTSPR/OP_MFSPR, on behalf of LoadStore1.
70 # by contrast microwatt has the spr set/get done *in* loadstore1.vhdl
71 self
.dsisr
= Signal(64)
74 # state info for LD/ST
76 # latch most of the input request
80 self
.addr
= Signal(64)
81 self
.store_data
= Signal(64)
82 self
.load_data
= Signal(64)
83 self
.byte_sel
= Signal(8)
84 self
.update
= Signal()
85 #self.xerc : xer_common_t;
86 #self.reserve = Signal()
87 #self.atomic = Signal()
88 #self.atomic_last = Signal()
90 self
.nc
= Signal() # non-cacheable access
91 self
.virt_mode
= Signal()
92 self
.priv_mode
= Signal()
93 self
.state
= Signal(State
)
94 self
.instr_fault
= Signal()
95 self
.align_intr
= Signal()
97 self
.wait_dcache
= Signal()
98 self
.wait_mmu
= Signal()
99 #self.mode_32bit = Signal()
100 self
.wr_sel
= Signal(2)
101 self
.interrupt
= Signal()
102 #self.intr_vec : integer range 0 to 16#fff#;
103 #self.nia = Signal(64)
104 #self.srr1 = Signal(16)
106 def set_wr_addr(self
, m
, addr
, mask
, misalign
, msr_pr
):
107 m
.d
.comb
+= self
.load
.eq(0) # store operation
109 m
.d
.comb
+= self
.d_out
.load
.eq(0)
110 m
.d
.comb
+= self
.byte_sel
.eq(mask
)
111 m
.d
.comb
+= self
.addr
.eq(addr
)
112 m
.d
.comb
+= self
.priv_mode
.eq(~msr_pr
) # not-problem ==> priv
113 m
.d
.comb
+= self
.virt_mode
.eq(msr_pr
) # problem-state ==> virt
114 m
.d
.comb
+= self
.align_intr
.eq(misalign
)
115 # option to disable the cache entirely for write
116 if self
.disable_cache
:
117 m
.d
.comb
+= self
.nc
.eq(1)
120 def set_rd_addr(self
, m
, addr
, mask
, misalign
, msr_pr
):
121 m
.d
.comb
+= self
.d_valid
.eq(1)
122 m
.d
.comb
+= self
.d_out
.valid
.eq(self
.d_validblip
)
123 m
.d
.comb
+= self
.load
.eq(1) # load operation
124 m
.d
.comb
+= self
.d_out
.load
.eq(1)
125 m
.d
.comb
+= self
.byte_sel
.eq(mask
)
126 m
.d
.comb
+= self
.align_intr
.eq(misalign
)
127 m
.d
.comb
+= self
.addr
.eq(addr
)
128 m
.d
.comb
+= self
.priv_mode
.eq(~msr_pr
) # not-problem ==> priv
129 m
.d
.comb
+= self
.virt_mode
.eq(msr_pr
) # problem-state ==> virt
130 # BAD HACK! disable cacheing on LD when address is 0xCxxx_xxxx
131 # this is for peripherals. same thing done in Microwatt loadstore1.vhdl
132 with m
.If(addr
[28:] == Const(0xc, 4)):
133 m
.d
.comb
+= self
.nc
.eq(1)
134 # option to disable the cache entirely for read
135 if self
.disable_cache
:
136 m
.d
.comb
+= self
.nc
.eq(1)
137 return None #FIXME return value
139 def set_wr_data(self
, m
, data
, wen
):
140 # do the "blip" on write data
141 m
.d
.comb
+= self
.d_valid
.eq(1)
142 m
.d
.comb
+= self
.d_out
.valid
.eq(self
.d_validblip
)
143 # put data into comb which is picked up in main elaborate()
144 m
.d
.comb
+= self
.d_w_valid
.eq(1)
145 m
.d
.comb
+= self
.store_data
.eq(data
)
146 #m.d.sync += self.d_out.byte_sel.eq(wen) # this might not be needed
147 st_ok
= self
.done
# TODO indicates write data is valid
150 def get_rd_data(self
, m
):
151 ld_ok
= self
.done
# indicates read data is valid
152 data
= self
.load_data
# actual read data
155 def elaborate(self
, platform
):
156 m
= super().elaborate(platform
)
157 comb
, sync
= m
.d
.comb
, m
.d
.sync
159 # create dcache module
160 m
.submodules
.dcache
= dcache
= self
.dcache
163 d_out
, d_in
, m_in
, dbus
= self
.d_out
, self
.d_in
, self
.m_in
, self
.dbus
165 exception
= exc
.happened
168 # copy of address, but gets over-ridden for OP_FETCH_FAILED
170 m
.d
.comb
+= maddr
.eq(self
.addr
)
172 # create a blip (single pulse) on valid read/write request
173 # this can be over-ridden in the FSM to get dcache to re-run
174 # a request when MMU_LOOKUP completes
175 m
.d
.comb
+= self
.d_validblip
.eq(rising_edge(m
, self
.d_valid
))
178 with m
.Switch(self
.state
):
179 with m
.Case(State
.IDLE
):
180 with m
.If(self
.d_validblip
):
181 sync
+= self
.state
.eq(State
.ACK_WAIT
)
183 # waiting for completion
184 with m
.Case(State
.ACK_WAIT
):
186 with m
.If(d_in
.error
):
187 # cache error is not necessarily "final", it could
188 # be that it was just a TLB miss
189 with m
.If(d_in
.cache_paradox
):
190 comb
+= exception
.eq(1)
191 sync
+= self
.state
.eq(State
.IDLE
)
192 sync
+= self
.dsisr
[63 - 38].eq(~self
.load
)
193 # XXX there is no architected bit for this
194 # (probably should be a machine check in fact)
195 sync
+= self
.dsisr
[63 - 35].eq(d_in
.cache_paradox
)
198 # Look up the translation for TLB miss
199 # and also for permission error and RC error
200 # in case the PTE has been updated.
202 sync
+= self
.state
.eq(State
.MMU_LOOKUP
)
203 with m
.If(d_in
.valid
):
204 m
.d
.comb
+= self
.done
.eq(1)
205 sync
+= self
.state
.eq(State
.IDLE
)
206 with m
.If(self
.load
):
207 m
.d
.comb
+= self
.load_data
.eq(d_in
.data
)
209 # waiting here for the MMU TLB lookup to complete.
210 # either re-try the dcache lookup or throw MMU exception
211 with m
.Case(State
.MMU_LOOKUP
):
212 with m
.If(m_in
.done
):
213 with m
.If(~self
.instr_fault
):
214 # retry the request now that the MMU has
215 # installed a TLB entry
216 m
.d
.comb
+= self
.d_validblip
.eq(1) # re-run dcache req
217 sync
+= self
.state
.eq(State
.ACK_WAIT
)
219 # instruction lookup fault: store address in DAR
220 comb
+= exc
.happened
.eq(1)
221 sync
+= self
.dar
.eq(self
.addr
)
224 # MMU RADIX exception thrown
225 comb
+= exception
.eq(1)
226 sync
+= self
.dsisr
[63 - 33].eq(m_in
.invalid
)
227 sync
+= self
.dsisr
[63 - 36].eq(m_in
.perm_error
)
228 sync
+= self
.dsisr
[63 - 38].eq(self
.load
)
229 sync
+= self
.dsisr
[63 - 44].eq(m_in
.badtree
)
230 sync
+= self
.dsisr
[63 - 45].eq(m_in
.rc_error
)
232 with m
.Case(State
.TLBIE_WAIT
):
235 # alignment error: store address in DAR
236 with m
.If(self
.align_intr
):
237 comb
+= exc
.happened
.eq(1)
238 sync
+= self
.dar
.eq(self
.addr
)
240 # happened, alignment, instr_fault, invalid.
241 # note that all of these flow through - eventually to the TRAP
242 # pipeline, via PowerDecoder2.
243 comb
+= exc
.invalid
.eq(m_in
.invalid
)
244 comb
+= exc
.alignment
.eq(self
.align_intr
)
245 comb
+= exc
.instr_fault
.eq(self
.instr_fault
)
246 # badtree, perm_error, rc_error, segment_fault
247 comb
+= exc
.badtree
.eq(m_in
.badtree
)
248 comb
+= exc
.perm_error
.eq(m_in
.perm_error
)
249 comb
+= exc
.rc_error
.eq(m_in
.rc_error
)
250 comb
+= exc
.segment_fault
.eq(m_in
.segerr
)
252 # TODO, connect dcache wb_in/wb_out to "standard" nmigen Wishbone bus
253 comb
+= dbus
.adr
.eq(dcache
.wb_out
.adr
)
254 comb
+= dbus
.dat_w
.eq(dcache
.wb_out
.dat
)
255 comb
+= dbus
.sel
.eq(dcache
.wb_out
.sel
)
256 comb
+= dbus
.cyc
.eq(dcache
.wb_out
.cyc
)
257 comb
+= dbus
.stb
.eq(dcache
.wb_out
.stb
)
258 comb
+= dbus
.we
.eq(dcache
.wb_out
.we
)
260 comb
+= dcache
.wb_in
.dat
.eq(dbus
.dat_r
)
261 comb
+= dcache
.wb_in
.ack
.eq(dbus
.ack
)
262 if hasattr(dbus
, "stall"):
263 comb
+= dcache
.wb_in
.stall
.eq(dbus
.stall
)
265 # write out d data only when flag set
266 with m
.If(self
.d_w_valid
):
267 m
.d
.sync
+= d_out
.data
.eq(self
.store_data
)
269 m
.d
.sync
+= d_out
.data
.eq(0)
271 # this must move into the FSM, conditionally noticing that
272 # the "blip" comes from self.d_validblip.
273 # task 1: look up in dcache
274 # task 2: if dcache fails, look up in MMU.
275 # do **NOT** confuse the two.
276 m
.d
.comb
+= d_out
.load
.eq(self
.load
)
277 m
.d
.comb
+= d_out
.byte_sel
.eq(self
.byte_sel
)
278 m
.d
.comb
+= d_out
.addr
.eq(self
.addr
)
279 m
.d
.comb
+= d_out
.nc
.eq(self
.nc
)
281 # XXX these should be possible to remove but for some reason
282 # cannot be... yet. TODO, investigate
283 m
.d
.comb
+= self
.done
.eq(d_in
.valid
)
284 m
.d
.comb
+= self
.load_data
.eq(d_in
.data
)
286 ''' TODO: translate to nmigen.
287 -- Update outputs to MMU
288 m_out.valid <= mmureq;
289 m_out.iside <= v.instr_fault;
290 m_out.load <= r.load;
291 # m_out.priv <= r.priv_mode; TODO
292 m_out.tlbie <= v.tlbie;
293 # m_out.mtspr <= mmu_mtspr; # TODO
294 # m_out.sprn <= sprn; # TODO
296 # m_out.slbia <= l_in.insn(7); # TODO: no idea what this is
297 # m_out.rs <= l_in.data; # nope, probably not needed, TODO investigate
303 yield from super().ports()
307 class TestSRAMLoadStore1(LoadStore1
):
308 def __init__(self
, pspec
):
309 super().__init
__(pspec
)
311 # small 32-entry Memory
312 if (hasattr(pspec
, "dmem_test_depth") and
313 isinstance(pspec
.dmem_test_depth
, int)):
314 depth
= pspec
.dmem_test_depth
317 print("TestSRAMBareLoadStoreUnit depth", depth
)
319 self
.mem
= Memory(width
=pspec
.reg_wid
, depth
=depth
)
321 def elaborate(self
, platform
):
322 m
= super().elaborate(platform
)
324 m
.submodules
.sram
= sram
= SRAM(memory
=self
.mem
, granularity
=8,
325 features
={'cti', 'bte', 'err'})
328 # directly connect the wishbone bus of LoadStoreUnitInterface to SRAM
329 # note: SRAM is a target (slave), dbus is initiator (master)
330 fanouts
= ['dat_w', 'sel', 'cyc', 'stb', 'we', 'cti', 'bte']
331 fanins
= ['dat_r', 'ack', 'err']
332 for fanout
in fanouts
:
333 print("fanout", fanout
, getattr(sram
.bus
, fanout
).shape(),
334 getattr(dbus
, fanout
).shape())
335 comb
+= getattr(sram
.bus
, fanout
).eq(getattr(dbus
, fanout
))
336 comb
+= getattr(sram
.bus
, fanout
).eq(getattr(dbus
, fanout
))
338 comb
+= getattr(dbus
, fanin
).eq(getattr(sram
.bus
, fanin
))
340 comb
+= sram
.bus
.adr
.eq(dbus
.adr
)