LSQWrapper.scala 7.8 KB
Newer Older
L
Lemover 已提交
1 2
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
Y
Yinan Xu 已提交
3
* Copyright (c) 2020-2021 Peng Cheng Laboratory
L
Lemover 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

Y
Yinan Xu 已提交
17 18
package xiangshan.mem

19
import chipsalliance.rocketchip.config.Parameters
Y
Yinan Xu 已提交
20 21 22 23 24
import chisel3._
import chisel3.util._
import utils._
import xiangshan._
import xiangshan.cache._
25 26
import xiangshan.cache.{DCacheWordIO, DCacheLineIO, MemoryOpConstants}
import xiangshan.cache.mmu.{TlbRequestIO}
Y
Yinan Xu 已提交
27
import xiangshan.mem._
Y
Yinan Xu 已提交
28
import xiangshan.backend.rob.RobLsqIO
Y
Yinan Xu 已提交
29

30
class ExceptionAddrIO(implicit p: Parameters) extends XSBundle {
Y
Yinan Xu 已提交
31 32 33 34 35
  val lsIdx = Input(new LSIdx)
  val isStore = Input(Bool())
  val vaddr = Output(UInt(VAddrBits.W))
}

36
class FwdEntry extends Bundle {
37 38 39
  val validFast = Bool() // validFast is generated the same cycle with query
  val valid = Bool() // valid is generated 1 cycle after query request
  val data = UInt(8.W) // data is generated 1 cycle after query request
W
William Wang 已提交
40 41
}

Y
Yinan Xu 已提交
42
// inflight miss block reqs
43
class InflightBlockInfo(implicit p: Parameters) extends XSBundle {
Y
Yinan Xu 已提交
44 45 46 47
  val block_addr = UInt(PAddrBits.W)
  val valid = Bool()
}

48
class LsqEnqIO(implicit p: Parameters) extends XSBundle {
Y
Yinan Xu 已提交
49
  val canAccept = Output(Bool())
Y
Yinan Xu 已提交
50 51 52
  val needAlloc = Vec(exuParameters.LsExuCnt, Input(UInt(2.W)))
  val req = Vec(exuParameters.LsExuCnt, Flipped(ValidIO(new MicroOp)))
  val resp = Vec(exuParameters.LsExuCnt, Output(new LSIdx))
Y
Yinan Xu 已提交
53 54
}

Y
Yinan Xu 已提交
55
// Load / Store Queue Wrapper for XiangShan Out of Order LSU
56
class LsqWrappper(implicit p: Parameters) extends XSModule with HasDCacheParameters {
Y
Yinan Xu 已提交
57
  val io = IO(new Bundle() {
Y
Yinan Xu 已提交
58
    val enq = new LsqEnqIO
59
    val brqRedirect = Flipped(ValidIO(new Redirect))
Y
Yinan Xu 已提交
60 61
    val loadIn = Vec(LoadPipelineWidth, Flipped(Valid(new LsPipelineBundle)))
    val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle)))
62
    val storeInRe = Vec(StorePipelineWidth, Input(new LsPipelineBundle()))
63
    val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreDataBundle))) // store data, send to sq from rs
64
    val loadDataForwarded = Vec(LoadPipelineWidth, Input(Bool()))
65
    val needReplayFromRS = Vec(LoadPipelineWidth, Input(Bool()))
66
    val sbuffer = Vec(StorePipelineWidth, Decoupled(new DCacheWordReqWithVaddr))
67
    val ldout = Vec(2, DecoupledIO(new ExuOutput)) // writeback int load
68
    val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store
69
    val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO))
W
William Wang 已提交
70
    val loadViolationQuery = Vec(LoadPipelineWidth, Flipped(new LoadViolationQueryIO))
Y
Yinan Xu 已提交
71
    val rob = Flipped(new RobLsqIO)
Y
Yinan Xu 已提交
72
    val rollback = Output(Valid(new Redirect))
73
    val dcache = Flipped(ValidIO(new Refill))
W
William Wang 已提交
74
    val release = Flipped(ValidIO(new Release))
Y
Yinan Xu 已提交
75 76
    val uncache = new DCacheWordIO
    val exceptionAddr = new ExceptionAddrIO
W
William Wang 已提交
77
    val sqempty = Output(Bool())
78
    val issuePtrExt = Output(new SqPtr)
79 80
    val sqFull = Output(Bool())
    val lqFull = Output(Bool())
Y
Yinan Xu 已提交
81 82 83 84 85
  })

  val loadQueue = Module(new LoadQueue)
  val storeQueue = Module(new StoreQueue)

86 87 88 89
  // io.enq logic
  // LSQ: send out canAccept when both load queue and store queue are ready
  // Dispatch: send instructions to LSQ only when they are ready
  io.enq.canAccept := loadQueue.io.enq.canAccept && storeQueue.io.enq.canAccept
90 91
  loadQueue.io.enq.sqCanAccept := storeQueue.io.enq.canAccept
  storeQueue.io.enq.lqCanAccept := loadQueue.io.enq.canAccept
Y
Yinan Xu 已提交
92 93 94 95 96
  for (i <- io.enq.req.indices) {
    loadQueue.io.enq.needAlloc(i)      := io.enq.needAlloc(i)(0)
    loadQueue.io.enq.req(i).valid      := io.enq.needAlloc(i)(0) && io.enq.req(i).valid
    loadQueue.io.enq.req(i).bits       := io.enq.req(i).bits
    loadQueue.io.enq.req(i).bits.sqIdx := storeQueue.io.enq.resp(i)
Y
Yinan Xu 已提交
97

Y
Yinan Xu 已提交
98 99 100 101 102
    storeQueue.io.enq.needAlloc(i)      := io.enq.needAlloc(i)(1)
    storeQueue.io.enq.req(i).valid      := io.enq.needAlloc(i)(1) && io.enq.req(i).valid
    storeQueue.io.enq.req(i).bits       := io.enq.req(i).bits
    storeQueue.io.enq.req(i).bits       := io.enq.req(i).bits
    storeQueue.io.enq.req(i).bits.lqIdx := loadQueue.io.enq.resp(i)
Y
Yinan Xu 已提交
103

104 105 106 107
    io.enq.resp(i).lqIdx := loadQueue.io.enq.resp(i)
    io.enq.resp(i).sqIdx := storeQueue.io.enq.resp(i)
  }

Y
Yinan Xu 已提交
108 109 110 111
  // load queue wiring
  loadQueue.io.brqRedirect <> io.brqRedirect
  loadQueue.io.loadIn <> io.loadIn
  loadQueue.io.storeIn <> io.storeIn
112
  loadQueue.io.loadDataForwarded <> io.loadDataForwarded
113
  loadQueue.io.needReplayFromRS <> io.needReplayFromRS
Y
Yinan Xu 已提交
114
  loadQueue.io.ldout <> io.ldout
Y
Yinan Xu 已提交
115
  loadQueue.io.rob <> io.rob
Y
Yinan Xu 已提交
116 117
  loadQueue.io.rollback <> io.rollback
  loadQueue.io.dcache <> io.dcache
W
William Wang 已提交
118
  loadQueue.io.release <> io.release
Y
Yinan Xu 已提交
119 120 121 122 123 124 125
  loadQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx
  loadQueue.io.exceptionAddr.isStore := DontCare

  // store queue wiring
  // storeQueue.io <> DontCare
  storeQueue.io.brqRedirect <> io.brqRedirect
  storeQueue.io.storeIn <> io.storeIn
126
  storeQueue.io.storeInRe <> io.storeInRe
127
  storeQueue.io.storeDataIn <> io.storeDataIn
Y
Yinan Xu 已提交
128
  storeQueue.io.sbuffer <> io.sbuffer
129
  storeQueue.io.mmioStout <> io.mmioStout
Y
Yinan Xu 已提交
130
  storeQueue.io.rob <> io.rob
Y
Yinan Xu 已提交
131 132
  storeQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx
  storeQueue.io.exceptionAddr.isStore := DontCare
133
  storeQueue.io.issuePtrExt <> io.issuePtrExt
Y
Yinan Xu 已提交
134

Y
Yinan Xu 已提交
135
  loadQueue.io.load_s1 <> io.forward
Y
Yinan Xu 已提交
136 137
  storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE

W
William Wang 已提交
138 139
  loadQueue.io.loadViolationQuery <> io.loadViolationQuery

W
William Wang 已提交
140 141
  storeQueue.io.sqempty <> io.sqempty

Y
Yinan Xu 已提交
142 143 144 145
  io.exceptionAddr.vaddr := Mux(io.exceptionAddr.isStore, storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr)

  // naive uncache arbiter
  val s_idle :: s_load :: s_store :: Nil = Enum(3)
146
  val pendingstate = RegInit(s_idle)
Y
Yinan Xu 已提交
147

148
  switch(pendingstate){
Y
Yinan Xu 已提交
149 150
    is(s_idle){
      when(io.uncache.req.fire()){
151
        pendingstate := Mux(loadQueue.io.uncache.req.valid, s_load, s_store)
Y
Yinan Xu 已提交
152 153 154 155
      }
    }
    is(s_load){
      when(io.uncache.resp.fire()){
156
        pendingstate := s_idle
Y
Yinan Xu 已提交
157 158 159 160
      }
    }
    is(s_store){
      when(io.uncache.resp.fire()){
161
        pendingstate := s_idle
Y
Yinan Xu 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174
      }
    }
  }

  loadQueue.io.uncache := DontCare
  storeQueue.io.uncache := DontCare
  loadQueue.io.uncache.resp.valid := false.B
  storeQueue.io.uncache.resp.valid := false.B
  when(loadQueue.io.uncache.req.valid){
    io.uncache.req <> loadQueue.io.uncache.req
  }.otherwise{
    io.uncache.req <> storeQueue.io.uncache.req
  }
175
  when(pendingstate === s_load){
Y
Yinan Xu 已提交
176 177 178 179 180 181 182
    io.uncache.resp <> loadQueue.io.uncache.resp
  }.otherwise{
    io.uncache.resp <> storeQueue.io.uncache.resp
  }

  assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid))
  assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid))
183
  assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && pendingstate === s_idle))
Y
Yinan Xu 已提交
184

185 186
  io.lqFull := loadQueue.io.lqFull
  io.sqFull := storeQueue.io.sqFull
187 188 189 190 191 192 193 194 195 196

  val ldq_perf = loadQueue.perfEvents.map(_._1).zip(loadQueue.perfinfo.perfEvents.perf_events)
  val stq_perf = storeQueue.perfEvents.map(_._1).zip(storeQueue.perfinfo.perfEvents.perf_events)
  val perfEvents = ldq_perf ++ stq_perf
  val perf_list = storeQueue.perfinfo.perfEvents.perf_events ++ loadQueue.perfinfo.perfEvents.perf_events
  val perfinfo = IO(new Bundle(){
    val perfEvents = Output(new PerfEventsBundle(perf_list.length))
  })
  perfinfo.perfEvents.perf_events := perf_list
}