xref: /XiangShan/src/main/scala/xiangshan/mem/lsqueue/LSQWrapper.scala (revision 2199a01c65d5a7bf503c4b40771336a50a6f1122)
1package xiangshan.mem
2
3import chisel3._
4import chisel3.util._
5import utils._
6import xiangshan._
7import xiangshan.cache._
8import xiangshan.cache.{DCacheWordIO, DCacheLineIO, TlbRequestIO, MemoryOpConstants}
9import xiangshan.backend.LSUOpType
10import xiangshan.mem._
11import xiangshan.backend.roq.RoqPtr
12
13class ExceptionAddrIO extends XSBundle {
14  val lsIdx = Input(new LSIdx)
15  val isStore = Input(Bool())
16  val vaddr = Output(UInt(VAddrBits.W))
17}
18
19class FwdEntry extends XSBundle {
20  val mask = Vec(8, Bool())
21  val data = Vec(8, UInt(8.W))
22}
23
24// inflight miss block reqs
25class InflightBlockInfo extends XSBundle {
26  val block_addr = UInt(PAddrBits.W)
27  val valid = Bool()
28}
29
30class LsqEnqIO extends XSBundle {
31  val canAccept = Output(Bool())
32  val needAlloc = Vec(RenameWidth, Input(Bool()))
33  val req = Vec(RenameWidth, Flipped(ValidIO(new MicroOp)))
34  val resp = Vec(RenameWidth, Output(new LSIdx))
35}
36
37// Load / Store Queue Wrapper for XiangShan Out of Order LSU
38class LsqWrappper extends XSModule with HasDCacheParameters {
39  val io = IO(new Bundle() {
40    val enq = new LsqEnqIO
41    val brqRedirect = Input(Valid(new Redirect))
42    val loadIn = Vec(LoadPipelineWidth, Flipped(Valid(new LsPipelineBundle)))
43    val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle)))
44    val sbuffer = Vec(StorePipelineWidth, Decoupled(new DCacheWordReq))
45    val ldout = Vec(2, DecoupledIO(new ExuOutput)) // writeback int load
46    val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store
47    val forward = Vec(LoadPipelineWidth, Flipped(new LoadForwardQueryIO))
48    val commits = Flipped(new RoqCommitIO)
49    val rollback = Output(Valid(new Redirect))
50    val dcache = Flipped(ValidIO(new Refill))
51    val uncache = new DCacheWordIO
52    val roqDeqPtr = Input(new RoqPtr)
53    val exceptionAddr = new ExceptionAddrIO
54    val sqempty = Output(Bool())
55  })
56
57  val loadQueue = Module(new LoadQueue)
58  val storeQueue = Module(new StoreQueue)
59
60  // io.enq logic
61  // LSQ: send out canAccept when both load queue and store queue are ready
62  // Dispatch: send instructions to LSQ only when they are ready
63  io.enq.canAccept := loadQueue.io.enq.canAccept && storeQueue.io.enq.canAccept
64  loadQueue.io.enq.sqCanAccept := storeQueue.io.enq.canAccept
65  storeQueue.io.enq.lqCanAccept := loadQueue.io.enq.canAccept
66  for (i <- 0 until RenameWidth) {
67    val isStore = CommitType.lsInstIsStore(io.enq.req(i).bits.ctrl.commitType)
68
69    loadQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i) && !isStore
70    loadQueue.io.enq.req(i).valid  := !isStore && io.enq.req(i).valid
71    loadQueue.io.enq.req(i).bits  := io.enq.req(i).bits
72
73    storeQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i) && isStore
74    storeQueue.io.enq.req(i).valid :=  isStore && io.enq.req(i).valid
75    storeQueue.io.enq.req(i).bits := io.enq.req(i).bits
76
77    io.enq.resp(i).lqIdx := loadQueue.io.enq.resp(i)
78    io.enq.resp(i).sqIdx := storeQueue.io.enq.resp(i)
79  }
80
81  // load queue wiring
82  loadQueue.io.brqRedirect <> io.brqRedirect
83  loadQueue.io.loadIn <> io.loadIn
84  loadQueue.io.storeIn <> io.storeIn
85  loadQueue.io.ldout <> io.ldout
86  loadQueue.io.commits <> io.commits
87  loadQueue.io.rollback <> io.rollback
88  loadQueue.io.dcache <> io.dcache
89  loadQueue.io.roqDeqPtr <> io.roqDeqPtr
90  loadQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx
91  loadQueue.io.exceptionAddr.isStore := DontCare
92
93  // store queue wiring
94  // storeQueue.io <> DontCare
95  storeQueue.io.brqRedirect <> io.brqRedirect
96  storeQueue.io.storeIn <> io.storeIn
97  storeQueue.io.sbuffer <> io.sbuffer
98  storeQueue.io.mmioStout <> io.mmioStout
99  storeQueue.io.commits <> io.commits
100  storeQueue.io.roqDeqPtr <> io.roqDeqPtr
101  storeQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx
102  storeQueue.io.exceptionAddr.isStore := DontCare
103
104  loadQueue.io.load_s1 <> io.forward
105  storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE
106
107  storeQueue.io.sqempty <> io.sqempty
108
109  io.exceptionAddr.vaddr := Mux(io.exceptionAddr.isStore, storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr)
110
111  // naive uncache arbiter
112  val s_idle :: s_load :: s_store :: Nil = Enum(3)
113  val uncacheState = RegInit(s_idle)
114
115  switch(uncacheState){
116    is(s_idle){
117      when(io.uncache.req.fire()){
118        uncacheState := Mux(loadQueue.io.uncache.req.valid, s_load, s_store)
119      }
120    }
121    is(s_load){
122      when(io.uncache.resp.fire()){
123        uncacheState := s_idle
124      }
125    }
126    is(s_store){
127      when(io.uncache.resp.fire()){
128        uncacheState := s_idle
129      }
130    }
131  }
132
133  loadQueue.io.uncache := DontCare
134  storeQueue.io.uncache := DontCare
135  loadQueue.io.uncache.resp.valid := false.B
136  storeQueue.io.uncache.resp.valid := false.B
137  when(loadQueue.io.uncache.req.valid){
138    io.uncache.req <> loadQueue.io.uncache.req
139  }.otherwise{
140    io.uncache.req <> storeQueue.io.uncache.req
141  }
142  when(uncacheState === s_load){
143    io.uncache.resp <> loadQueue.io.uncache.resp
144  }.otherwise{
145    io.uncache.resp <> storeQueue.io.uncache.resp
146  }
147
148  assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid))
149  assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid))
150  assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && uncacheState === s_idle))
151
152}
153