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 19 20class LsqEntry extends XSBundle { 21 val vaddr = UInt(VAddrBits.W) // TODO: need opt 22 val paddr = UInt(PAddrBits.W) 23 val mask = UInt(8.W) 24 val data = UInt(XLEN.W) 25 val exception = UInt(16.W) // TODO: opt size 26 val fwdMask = Vec(8, Bool()) 27 val fwdData = Vec(8, UInt(8.W)) 28} 29 30class FwdEntry extends XSBundle { 31 val mask = Vec(8, Bool()) 32 val data = Vec(8, UInt(8.W)) 33} 34 35 36class LSQueueData(size: Int, nchannel: Int) extends XSModule with HasDCacheParameters with HasCircularQueuePtrHelper { 37 val io = IO(new Bundle() { 38 val wb = Vec(nchannel, new Bundle() { 39 val wen = Input(Bool()) 40 val index = Input(UInt(log2Up(size).W)) 41 val wdata = Input(new LsqEntry) 42 }) 43 val uncache = new Bundle() { 44 val wen = Input(Bool()) 45 val index = Input(UInt(log2Up(size).W)) 46 val wdata = Input(UInt(XLEN.W)) 47 } 48 val refill = new Bundle() { 49 val wen = Input(Vec(size, Bool())) 50 val data = Input(UInt((cfg.blockBytes * 8).W)) 51 } 52 val needForward = Input(Vec(nchannel, Vec(2, UInt(size.W)))) 53 val forward = Vec(nchannel, Flipped(new LoadForwardQueryIO)) 54 val rdata = Output(Vec(size, new LsqEntry)) 55 56 // val debug = new Bundle() { 57 // val debug_data = Vec(LoadQueueSize, new LsqEntry) 58 // } 59 60 def wbWrite(channel: Int, index: UInt, wdata: LsqEntry): Unit = { 61 require(channel < nchannel && channel >= 0) 62 // need extra "this.wb(channel).wen := true.B" 63 this.wb(channel).index := index 64 this.wb(channel).wdata := wdata 65 } 66 67 def uncacheWrite(index: UInt, wdata: UInt): Unit = { 68 // need extra "this.uncache.wen := true.B" 69 this.uncache.index := index 70 this.uncache.wdata := wdata 71 } 72 73 def forwardQuery(channel: Int, paddr: UInt, needForward1: Data, needForward2: Data): Unit = { 74 this.needForward(channel)(0) := needForward1 75 this.needForward(channel)(1) := needForward2 76 this.forward(channel).paddr := paddr 77 } 78 79 // def refillWrite(ldIdx: Int): Unit = { 80 // } 81 // use "this.refill.wen(ldIdx) := true.B" instead 82 }) 83 84 io := DontCare 85 86 val data = Reg(Vec(size, new LsqEntry)) 87 88 // writeback to lq/sq 89 (0 until 2).map(i => { 90 when(io.wb(i).wen){ 91 data(io.wb(i).index) := io.wb(i).wdata 92 } 93 }) 94 95 when(io.uncache.wen){ 96 data(io.uncache.index).data := io.uncache.wdata 97 } 98 99 // refill missed load 100 def mergeRefillData(refill: UInt, fwd: UInt, fwdMask: UInt): UInt = { 101 val res = Wire(Vec(8, UInt(8.W))) 102 (0 until 8).foreach(i => { 103 res(i) := Mux(fwdMask(i), fwd(8 * (i + 1) - 1, 8 * i), refill(8 * (i + 1) - 1, 8 * i)) 104 }) 105 res.asUInt 106 } 107 108 // split dcache result into words 109 val words = VecInit((0 until blockWords) map { i => io.refill.data(DataBits * (i + 1) - 1, DataBits * i)}) 110 111 112 (0 until size).map(i => { 113 when(io.refill.wen(i) ){ 114 val refillData = words(get_word(data(i).paddr)) 115 data(i).data := mergeRefillData(refillData, data(i).fwdData.asUInt, data(i).fwdMask.asUInt) 116 XSDebug("miss resp: pos %d addr %x data %x + %x(%b)\n", i.U, data(i).paddr, refillData, data(i).fwdData.asUInt, data(i).fwdMask.asUInt) 117 } 118 }) 119 120 // forwarding 121 // Compare ringBufferTail (deqPtr) and forward.sqIdx, we have two cases: 122 // (1) if they have the same flag, we need to check range(tail, sqIdx) 123 // (2) if they have different flags, we need to check range(tail, LoadQueueSize) and range(0, sqIdx) 124 // Forward1: Mux(same_flag, range(tail, sqIdx), range(tail, LoadQueueSize)) 125 // Forward2: Mux(same_flag, 0.U, range(0, sqIdx) ) 126 // i.e. forward1 is the target entries with the same flag bits and forward2 otherwise 127 128 // entry with larger index should have higher priority since it's data is younger 129 130 // FIXME: old fwd logic for assertion, remove when rtl freeze 131 (0 until nchannel).map(i => { 132 133 val forwardMask1 = WireInit(VecInit(Seq.fill(8)(false.B))) 134 val forwardData1 = WireInit(VecInit(Seq.fill(8)(0.U(8.W)))) 135 val forwardMask2 = WireInit(VecInit(Seq.fill(8)(false.B))) 136 val forwardData2 = WireInit(VecInit(Seq.fill(8)(0.U(8.W)))) 137 138 for (j <- 0 until size) { 139 val needCheck = io.forward(i).paddr(PAddrBits - 1, 3) === data(j).paddr(PAddrBits - 1, 3) 140 (0 until XLEN / 8).foreach(k => { 141 when (needCheck && data(j).mask(k)) { 142 when (io.needForward(i)(0)(j)) { 143 forwardMask1(k) := true.B 144 forwardData1(k) := data(j).data(8 * (k + 1) - 1, 8 * k) 145 } 146 when (io.needForward(i)(1)(j)) { 147 forwardMask2(k) := true.B 148 forwardData2(k) := data(j).data(8 * (k + 1) - 1, 8 * k) 149 } 150 XSDebug(io.needForward(i)(0)(j) || io.needForward(i)(1)(j), 151 p"forwarding $k-th byte ${Hexadecimal(data(j).data(8 * (k + 1) - 1, 8 * k))} " + 152 p"from ptr $j\n") 153 } 154 }) 155 } 156 157 // merge forward lookup results 158 // forward2 is younger than forward1 and should have higher priority 159 val oldFwdResult = Wire(new FwdEntry) 160 (0 until XLEN / 8).map(k => { 161 oldFwdResult.mask(k) := RegNext(forwardMask1(k) || forwardMask2(k)) 162 oldFwdResult.data(k) := RegNext(Mux(forwardMask2(k), forwardData2(k), forwardData1(k))) 163 }) 164 165 // parallel fwd logic 166 val paddrMatch = Wire(Vec(size, Bool())) 167 val matchResultVec = Wire(Vec(size * 2, new FwdEntry)) 168 169 def parallelFwd(xs: Seq[Data]): Data = { 170 ParallelOperation(xs, (a: Data, b: Data) => { 171 val l = a.asTypeOf(new FwdEntry) 172 val r = b.asTypeOf(new FwdEntry) 173 val res = Wire(new FwdEntry) 174 (0 until 8).map(p => { 175 res.mask(p) := l.mask(p) || r.mask(p) 176 res.data(p) := Mux(r.mask(p), r.data(p), l.data(p)) 177 }) 178 res 179 }) 180 } 181 182 for (j <- 0 until size) { 183 paddrMatch(j) := io.forward(i).paddr(PAddrBits - 1, 3) === data(j).paddr(PAddrBits - 1, 3) 184 } 185 186 for (j <- 0 until size) { 187 val needCheck0 = RegNext(paddrMatch(j) && io.needForward(i)(0)(j)) 188 val needCheck1 = RegNext(paddrMatch(j) && io.needForward(i)(1)(j)) 189 (0 until XLEN / 8).foreach(k => { 190 matchResultVec(j).mask(k) := needCheck0 && data(j).mask(k) 191 matchResultVec(j).data(k) := data(j).data(8 * (k + 1) - 1, 8 * k) 192 matchResultVec(size + j).mask(k) := needCheck1 && data(j).mask(k) 193 matchResultVec(size + j).data(k) := data(j).data(8 * (k + 1) - 1, 8 * k) 194 }) 195 } 196 197 val parallelFwdResult = parallelFwd(matchResultVec).asTypeOf(new FwdEntry) 198 199 io.forward(i).forwardMask := parallelFwdResult.mask 200 io.forward(i).forwardData := parallelFwdResult.data 201 202 when( 203 oldFwdResult.mask.asUInt =/= parallelFwdResult.mask.asUInt 204 ){ 205 printf("%d: mask error: right: %b false %b\n", GTimer(), oldFwdResult.mask.asUInt, parallelFwdResult.mask.asUInt) 206 } 207 208 for (p <- 0 until 8) { 209 when( 210 oldFwdResult.data(p) =/= parallelFwdResult.data(p) && oldFwdResult.mask(p) 211 ){ 212 printf("%d: data "+p+" error: right: %x false %x\n", GTimer(), oldFwdResult.data(p), parallelFwdResult.data(p)) 213 } 214 } 215 216 }) 217 218 // data read 219 io.rdata := data 220 // io.debug.debug_data := data 221} 222 223// inflight miss block reqs 224class InflightBlockInfo extends XSBundle { 225 val block_addr = UInt(PAddrBits.W) 226 val valid = Bool() 227} 228 229class LsqEnqIO extends XSBundle { 230 val canAccept = Output(Bool()) 231 val needAlloc = Vec(RenameWidth, Input(Bool())) 232 val req = Vec(RenameWidth, Flipped(ValidIO(new MicroOp))) 233 val resp = Vec(RenameWidth, Output(new LSIdx)) 234} 235 236// Load / Store Queue Wrapper for XiangShan Out of Order LSU 237class LsqWrappper extends XSModule with HasDCacheParameters { 238 val io = IO(new Bundle() { 239 val enq = new LsqEnqIO 240 val brqRedirect = Input(Valid(new Redirect)) 241 val loadIn = Vec(LoadPipelineWidth, Flipped(Valid(new LsPipelineBundle))) 242 val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) 243 val sbuffer = Vec(StorePipelineWidth, Decoupled(new DCacheWordReq)) 244 val ldout = Vec(2, DecoupledIO(new ExuOutput)) // writeback int load 245 val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store 246 val forward = Vec(LoadPipelineWidth, Flipped(new LoadForwardQueryIO)) 247 val commits = Flipped(new RoqCommitIO) 248 val rollback = Output(Valid(new Redirect)) 249 val dcache = Flipped(ValidIO(new Refill)) 250 val uncache = new DCacheWordIO 251 val roqDeqPtr = Input(new RoqPtr) 252 val exceptionAddr = new ExceptionAddrIO 253 }) 254 255 val loadQueue = Module(new LoadQueue) 256 val storeQueue = Module(new StoreQueue) 257 258 // io.enq logic 259 // LSQ: send out canAccept when both load queue and store queue are ready 260 // Dispatch: send instructions to LSQ only when they are ready 261 io.enq.canAccept := loadQueue.io.enq.canAccept && storeQueue.io.enq.canAccept 262 loadQueue.io.enq.sqCanAccept := storeQueue.io.enq.canAccept 263 storeQueue.io.enq.lqCanAccept := loadQueue.io.enq.canAccept 264 for (i <- 0 until RenameWidth) { 265 val isStore = CommitType.lsInstIsStore(io.enq.req(i).bits.ctrl.commitType) 266 267 loadQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i) && !isStore 268 loadQueue.io.enq.req(i).valid := !isStore && io.enq.req(i).valid 269 loadQueue.io.enq.req(i).bits := io.enq.req(i).bits 270 271 storeQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i) && isStore 272 storeQueue.io.enq.req(i).valid := isStore && io.enq.req(i).valid 273 storeQueue.io.enq.req(i).bits := io.enq.req(i).bits 274 275 io.enq.resp(i).lqIdx := loadQueue.io.enq.resp(i) 276 io.enq.resp(i).sqIdx := storeQueue.io.enq.resp(i) 277 } 278 279 // load queue wiring 280 loadQueue.io.brqRedirect <> io.brqRedirect 281 loadQueue.io.loadIn <> io.loadIn 282 loadQueue.io.storeIn <> io.storeIn 283 loadQueue.io.ldout <> io.ldout 284 loadQueue.io.commits <> io.commits 285 loadQueue.io.rollback <> io.rollback 286 loadQueue.io.dcache <> io.dcache 287 loadQueue.io.roqDeqPtr <> io.roqDeqPtr 288 loadQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx 289 loadQueue.io.exceptionAddr.isStore := DontCare 290 291 // store queue wiring 292 // storeQueue.io <> DontCare 293 storeQueue.io.brqRedirect <> io.brqRedirect 294 storeQueue.io.storeIn <> io.storeIn 295 storeQueue.io.sbuffer <> io.sbuffer 296 storeQueue.io.mmioStout <> io.mmioStout 297 storeQueue.io.commits <> io.commits 298 storeQueue.io.roqDeqPtr <> io.roqDeqPtr 299 storeQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx 300 storeQueue.io.exceptionAddr.isStore := DontCare 301 302 loadQueue.io.load_s1 <> io.forward 303 storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE 304 305 io.exceptionAddr.vaddr := Mux(io.exceptionAddr.isStore, storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr) 306 307 // naive uncache arbiter 308 val s_idle :: s_load :: s_store :: Nil = Enum(3) 309 val uncacheState = RegInit(s_idle) 310 311 switch(uncacheState){ 312 is(s_idle){ 313 when(io.uncache.req.fire()){ 314 uncacheState := Mux(loadQueue.io.uncache.req.valid, s_load, s_store) 315 } 316 } 317 is(s_load){ 318 when(io.uncache.resp.fire()){ 319 uncacheState := s_idle 320 } 321 } 322 is(s_store){ 323 when(io.uncache.resp.fire()){ 324 uncacheState := s_idle 325 } 326 } 327 } 328 329 loadQueue.io.uncache := DontCare 330 storeQueue.io.uncache := DontCare 331 loadQueue.io.uncache.resp.valid := false.B 332 storeQueue.io.uncache.resp.valid := false.B 333 when(loadQueue.io.uncache.req.valid){ 334 io.uncache.req <> loadQueue.io.uncache.req 335 }.otherwise{ 336 io.uncache.req <> storeQueue.io.uncache.req 337 } 338 when(uncacheState === s_load){ 339 io.uncache.resp <> loadQueue.io.uncache.resp 340 }.otherwise{ 341 io.uncache.resp <> storeQueue.io.uncache.resp 342 } 343 344 assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid)) 345 assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid)) 346 assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && uncacheState === s_idle)) 347 348} 349