3 based on microwatt loadstore1.vhdl
7 * https://bugs.libre-soc.org/show_bug.cgi?id=465
11 from nmigen
import (Elaboratable
, Module
, Signal
, Shape
, unsigned
, Cat
, Mux
,
14 from nmutil
.util
import rising_edge
15 from enum
import Enum
, unique
17 from soc
.experiment
.dcache
import DCache
18 from soc
.experiment
.pimem
import PortInterfaceBase
19 from soc
.experiment
.mem_types
import LoadStore1ToMMUType
20 from soc
.experiment
.mem_types
import MMUToLoadStore1Type
22 from soc
.minerva
.wishbone
import make_wb_layout
23 from soc
.bus
.sram
import SRAM
28 IDLE
= 0 # ready for instruction
29 ACK_WAIT
= 1 # waiting for ack from dcache
30 MMU_LOOKUP
= 2 # waiting for MMU to look up translation
31 TLBIE_WAIT
= 3 # waiting for MMU to finish doing a tlbie
32 FINISH_LFS
= 4 # write back converted SP data for lfs*
33 COMPLETE
= 5 # extra cycle to complete an operation
36 # glue logic for microwatt mmu and dcache
37 class LoadStore1(PortInterfaceBase
):
38 def __init__(self
, pspec
):
40 self
.disable_cache
= (hasattr(pspec
, "disable_cache") and
41 pspec
.disable_cache
== True)
42 regwid
= pspec
.reg_wid
43 addrwid
= pspec
.addr_wid
45 super().__init
__(regwid
, addrwid
)
46 self
.dcache
= DCache()
47 self
.d_in
= self
.dcache
.d_in
48 self
.d_out
= self
.dcache
.d_out
49 self
.l_in
= LoadStore1ToMMUType()
50 self
.l_out
= MMUToLoadStore1Type()
52 self
.mmureq
= Signal()
53 self
.derror
= Signal()
55 # TODO, convert dcache wb_in/wb_out to "standard" nmigen Wishbone bus
56 self
.dbus
= Record(make_wb_layout(pspec
))
58 # for creating a single clock blip to DCache
59 self
.d_valid
= Signal()
60 self
.d_w_valid
= Signal()
61 self
.d_validblip
= Signal()
63 # DSISR and DAR cached values. note that the MMU FSM is where
64 # these are accessed by OP_MTSPR/OP_MFSPR, on behalf of LoadStore1.
65 # by contrast microwatt has the spr set/get done *in* loadstore1.vhdl
66 self
.dsisr
= Signal(64)
69 # state info for LD/ST
71 # latch most of the input request
75 self
.addr
= Signal(64)
76 self
.store_data
= Signal(64)
77 self
.load_data
= Signal(64)
78 self
.byte_sel
= Signal(8)
79 self
.update
= Signal()
80 #self.xerc : xer_common_t;
81 #self.reserve = Signal()
82 #self.atomic = Signal()
83 #self.atomic_last = Signal()
85 self
.nc
= Signal() # non-cacheable access
86 self
.virt_mode
= Signal()
87 self
.priv_mode
= Signal()
88 self
.state
= Signal(State
)
89 self
.instr_fault
= Signal()
90 self
.align_intr
= Signal()
92 self
.wait_dcache
= Signal()
93 self
.wait_mmu
= Signal()
94 #self.mode_32bit = Signal()
95 self
.wr_sel
= Signal(2)
96 self
.interrupt
= Signal()
97 #self.intr_vec : integer range 0 to 16#fff#;
98 #self.nia = Signal(64)
99 #self.srr1 = Signal(16)
101 def set_wr_addr(self
, m
, addr
, mask
, misalign
):
102 m
.d
.comb
+= self
.load
.eq(0) # store operation
104 m
.d
.comb
+= self
.d_in
.load
.eq(0)
105 m
.d
.comb
+= self
.byte_sel
.eq(mask
)
106 m
.d
.comb
+= self
.addr
.eq(addr
)
107 m
.d
.comb
+= self
.align_intr
.eq(misalign
)
108 # option to disable the cache entirely for write
109 if self
.disable_cache
:
110 m
.d
.comb
+= self
.nc
.eq(1)
113 def set_rd_addr(self
, m
, addr
, mask
, misalign
):
114 m
.d
.comb
+= self
.d_valid
.eq(1)
115 m
.d
.comb
+= self
.d_in
.valid
.eq(self
.d_validblip
)
116 m
.d
.comb
+= self
.load
.eq(1) # load operation
117 m
.d
.comb
+= self
.d_in
.load
.eq(1)
118 m
.d
.comb
+= self
.byte_sel
.eq(mask
)
119 m
.d
.comb
+= self
.align_intr
.eq(misalign
)
120 m
.d
.comb
+= self
.addr
.eq(addr
)
121 # BAD HACK! disable cacheing on LD when address is 0xCxxx_xxxx
122 # this is for peripherals. same thing done in Microwatt loadstore1.vhdl
123 with m
.If(addr
[28:] == Const(0xc, 4)):
124 m
.d
.comb
+= self
.nc
.eq(1)
125 # option to disable the cache entirely for read
126 if self
.disable_cache
:
127 m
.d
.comb
+= self
.nc
.eq(1)
128 return None #FIXME return value
130 def set_wr_data(self
, m
, data
, wen
):
131 # do the "blip" on write data
132 m
.d
.comb
+= self
.d_valid
.eq(1)
133 m
.d
.comb
+= self
.d_in
.valid
.eq(self
.d_validblip
)
134 # put data into comb which is picked up in main elaborate()
135 m
.d
.comb
+= self
.d_w_valid
.eq(1)
136 m
.d
.comb
+= self
.store_data
.eq(data
)
137 #m.d.sync += self.d_in.byte_sel.eq(wen) # this might not be needed
138 st_ok
= self
.done
# TODO indicates write data is valid
141 def get_rd_data(self
, m
):
142 ld_ok
= self
.done
# indicates read data is valid
143 data
= self
.load_data
# actual read data
147 if d_in.error = '1' then
148 if d_in.cache_paradox = '1' then
149 -- signal an interrupt straight away
151 dsisr(63 - 38) := not r2.req.load;
152 -- XXX there is no architected bit for this
153 -- (probably should be a machine check in fact)
154 dsisr(63 - 35) := d_in.cache_paradox;
156 -- Look up the translation for TLB miss
157 -- and also for permission error and RC error
158 -- in case the PTE has been updated.
160 v.state := MMU_LOOKUP;
166 def elaborate(self
, platform
):
167 m
= super().elaborate(platform
)
168 comb
, sync
= m
.d
.comb
, m
.d
.sync
170 # create dcache module
171 m
.submodules
.dcache
= dcache
= self
.dcache
174 d_in
, d_out
, l_out
, dbus
= self
.d_in
, self
.d_out
, self
.l_out
, self
.dbus
177 with m
.Switch(self
.state
):
178 with m
.Case(State
.IDLE
):
180 with m
.Case(State
.ACK_WAIT
):
182 with m
.Case(State
.MMU_LOOKUP
):
184 with m
.Case(State
.TLBIE_WAIT
):
186 with m
.Case(State
.FINISH_LFS
):
188 with m
.Case(State
.COMPLETE
):
191 # microwatt: only if State.ACK_WAIZ
192 with m
.If(d_out
.error
):
193 with m
.If(d_out
.cache_paradox
):
194 sync
+= self
.derror
.eq(1)
195 sync
+= self
.dsisr
[63 - 38].eq(~self
.load
)
196 # XXX there is no architected bit for this
197 # (probably should be a machine check in fact)
198 sync
+= self
.dsisr
[63 - 35].eq(d_out
.cache_paradox
)
201 # Look up the translation for TLB miss
202 # and also for permission error and RC error
203 # in case the PTE has been updated.
204 sync
+= self
.mmureq
.eq(1)
205 sync
+= self
.state
.eq(State
.MMU_LOOKUP
)
209 # happened, alignment, instr_fault, invalid.
210 # note that all of these flow through - eventually to the TRAP
211 # pipeline, via PowerDecoder2.
212 comb
+= exc
.happened
.eq(d_out
.error | l_out
.err | self
.align_intr
)
213 comb
+= exc
.invalid
.eq(l_out
.invalid
)
214 comb
+= exc
.alignment
.eq(self
.align_intr
)
216 # badtree, perm_error, rc_error, segment_fault
217 comb
+= exc
.badtree
.eq(l_out
.badtree
)
218 comb
+= exc
.perm_error
.eq(l_out
.perm_error
)
219 comb
+= exc
.rc_error
.eq(l_out
.rc_error
)
220 comb
+= exc
.segment_fault
.eq(l_out
.segerr
)
222 # TODO some exceptions set SPRs
224 # TODO, connect dcache wb_in/wb_out to "standard" nmigen Wishbone bus
225 comb
+= dbus
.adr
.eq(dcache
.wb_out
.adr
)
226 comb
+= dbus
.dat_w
.eq(dcache
.wb_out
.dat
)
227 comb
+= dbus
.sel
.eq(dcache
.wb_out
.sel
)
228 comb
+= dbus
.cyc
.eq(dcache
.wb_out
.cyc
)
229 comb
+= dbus
.stb
.eq(dcache
.wb_out
.stb
)
230 comb
+= dbus
.we
.eq(dcache
.wb_out
.we
)
232 comb
+= dcache
.wb_in
.dat
.eq(dbus
.dat_r
)
233 comb
+= dcache
.wb_in
.ack
.eq(dbus
.ack
)
234 if hasattr(dbus
, "stall"):
235 comb
+= dcache
.wb_in
.stall
.eq(dbus
.stall
)
237 # create a blip (single pulse) on valid read/write request
238 m
.d
.comb
+= self
.d_validblip
.eq(rising_edge(m
, self
.d_valid
))
240 # write out d data only when flag set
241 with m
.If(self
.d_w_valid
):
242 m
.d
.sync
+= d_in
.data
.eq(self
.store_data
)
244 m
.d
.sync
+= d_in
.data
.eq(0)
246 # this must move into the FSM, conditionally noticing that
247 # the "blip" comes from self.d_validblip.
248 # task 1: look up in dcache
249 # task 2: if dcache fails, look up in MMU.
250 # do **NOT** confuse the two.
251 m
.d
.comb
+= d_in
.load
.eq(self
.load
)
252 m
.d
.comb
+= d_in
.byte_sel
.eq(self
.byte_sel
)
253 m
.d
.comb
+= d_in
.addr
.eq(self
.addr
)
254 m
.d
.comb
+= d_in
.nc
.eq(self
.nc
)
255 m
.d
.comb
+= self
.done
.eq(d_out
.valid
)
256 m
.d
.comb
+= self
.load_data
.eq(d_out
.data
)
261 yield from super().ports()
265 class TestSRAMLoadStore1(LoadStore1
):
266 def __init__(self
, pspec
):
267 super().__init
__(pspec
)
269 # small 32-entry Memory
270 if (hasattr(pspec
, "dmem_test_depth") and
271 isinstance(pspec
.dmem_test_depth
, int)):
272 depth
= pspec
.dmem_test_depth
275 print("TestSRAMBareLoadStoreUnit depth", depth
)
277 self
.mem
= Memory(width
=pspec
.reg_wid
, depth
=depth
)
279 def elaborate(self
, platform
):
280 m
= super().elaborate(platform
)
282 m
.submodules
.sram
= sram
= SRAM(memory
=self
.mem
, granularity
=8,
283 features
={'cti', 'bte', 'err'})
286 # directly connect the wishbone bus of LoadStoreUnitInterface to SRAM
287 # note: SRAM is a target (slave), dbus is initiator (master)
288 fanouts
= ['dat_w', 'sel', 'cyc', 'stb', 'we', 'cti', 'bte']
289 fanins
= ['dat_r', 'ack', 'err']
290 for fanout
in fanouts
:
291 print("fanout", fanout
, getattr(sram
.bus
, fanout
).shape(),
292 getattr(dbus
, fanout
).shape())
293 comb
+= getattr(sram
.bus
, fanout
).eq(getattr(dbus
, fanout
))
294 comb
+= getattr(sram
.bus
, fanout
).eq(getattr(dbus
, fanout
))
296 comb
+= getattr(dbus
, fanin
).eq(getattr(sram
.bus
, fanin
))
298 comb
+= sram
.bus
.adr
.eq(dbus
.adr
)