1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.mem 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import xiangshan._ 24import xiangshan.cache._ 25import xiangshan.cache.{DCacheLineIO, DCacheWordIO, MemoryOpConstants} 26import xiangshan.cache.mmu.TlbRequestIO 27import xiangshan.mem._ 28import xiangshan.backend.rob.RobLsqIO 29import xiangshan.backend.fu.HasExceptionNO 30import xiangshan.frontend.FtqPtr 31import xiangshan.backend.fu.fpu.FPU 32 33 34class LqPtr(implicit p: Parameters) extends CircularQueuePtr[LqPtr]( 35 p => p(XSCoreParamsKey).LoadQueueSize 36){ 37 override def cloneType = (new LqPtr).asInstanceOf[this.type] 38} 39 40object LqPtr { 41 def apply(f: Bool, v: UInt)(implicit p: Parameters): LqPtr = { 42 val ptr = Wire(new LqPtr) 43 ptr.flag := f 44 ptr.value := v 45 ptr 46 } 47} 48 49trait HasLoadHelper { this: XSModule => 50 def rdataHelper(uop: MicroOp, rdata: UInt): UInt = { 51 val fpWen = uop.ctrl.fpWen 52 LookupTree(uop.ctrl.fuOpType, List( 53 LSUOpType.lb -> SignExt(rdata(7, 0) , XLEN), 54 LSUOpType.lh -> SignExt(rdata(15, 0), XLEN), 55 /* 56 riscv-spec-20191213: 12.2 NaN Boxing of Narrower Values 57 Any operation that writes a narrower result to an f register must write 58 all 1s to the uppermost FLEN−n bits to yield a legal NaN-boxed value. 59 */ 60 LSUOpType.lw -> Mux(fpWen, FPU.box(rdata, FPU.S), SignExt(rdata(31, 0), XLEN)), 61 LSUOpType.ld -> Mux(fpWen, FPU.box(rdata, FPU.D), SignExt(rdata(63, 0), XLEN)), 62 LSUOpType.lbu -> ZeroExt(rdata(7, 0) , XLEN), 63 LSUOpType.lhu -> ZeroExt(rdata(15, 0), XLEN), 64 LSUOpType.lwu -> ZeroExt(rdata(31, 0), XLEN), 65 )) 66 } 67} 68 69class LqEnqIO(implicit p: Parameters) extends XSBundle { 70 val canAccept = Output(Bool()) 71 val sqCanAccept = Input(Bool()) 72 val needAlloc = Vec(RenameWidth, Input(Bool())) 73 val req = Vec(RenameWidth, Flipped(ValidIO(new MicroOp))) 74 val resp = Vec(RenameWidth, Output(new LqPtr)) 75} 76 77// Load Queue 78class LoadQueue(implicit p: Parameters) extends XSModule 79 with HasDCacheParameters 80 with HasCircularQueuePtrHelper 81 with HasLoadHelper 82 with HasExceptionNO 83{ 84 val io = IO(new Bundle() { 85 val enq = new LqEnqIO 86 val brqRedirect = Flipped(ValidIO(new Redirect)) 87 val loadIn = Vec(LoadPipelineWidth, Flipped(Valid(new LsPipelineBundle))) 88 val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) 89 val loadDataForwarded = Vec(LoadPipelineWidth, Input(Bool())) 90 val needReplayFromRS = Vec(LoadPipelineWidth, Input(Bool())) 91 val ldout = Vec(2, DecoupledIO(new ExuOutput)) // writeback int load 92 val load_s1 = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO)) 93 val rob = Flipped(new RobLsqIO) 94 val rollback = Output(Valid(new Redirect)) // replay now starts from load instead of store 95 val dcache = Flipped(ValidIO(new Refill)) 96 val uncache = new DCacheWordIO 97 val exceptionAddr = new ExceptionAddrIO 98 val lqFull = Output(Bool()) 99 }) 100 101 println("LoadQueue: size:" + LoadQueueSize) 102 103 val uop = Reg(Vec(LoadQueueSize, new MicroOp)) 104 // val data = Reg(Vec(LoadQueueSize, new LsRobEntry)) 105 val dataModule = Module(new LoadQueueData(LoadQueueSize, wbNumRead = LoadPipelineWidth, wbNumWrite = LoadPipelineWidth)) 106 dataModule.io := DontCare 107 val vaddrModule = Module(new SyncDataModuleTemplate(UInt(VAddrBits.W), LoadQueueSize, numRead = 1, numWrite = LoadPipelineWidth)) 108 vaddrModule.io := DontCare 109 val allocated = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) // lq entry has been allocated 110 val datavalid = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) // data is valid 111 val writebacked = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) // inst has been writebacked to CDB 112 val miss = Reg(Vec(LoadQueueSize, Bool())) // load inst missed, waiting for miss queue to accept miss request 113 // val listening = Reg(Vec(LoadQueueSize, Bool())) // waiting for refill result 114 val pending = Reg(Vec(LoadQueueSize, Bool())) // mmio pending: inst is an mmio inst, it will not be executed until it reachs the end of rob 115 val refilling = WireInit(VecInit(List.fill(LoadQueueSize)(false.B))) // inst has been writebacked to CDB 116 117 val debug_mmio = Reg(Vec(LoadQueueSize, Bool())) // mmio: inst is an mmio inst 118 val debug_paddr = Reg(Vec(LoadQueueSize, UInt(PAddrBits.W))) // mmio: inst is an mmio inst 119 120 val enqPtrExt = RegInit(VecInit((0 until RenameWidth).map(_.U.asTypeOf(new LqPtr)))) 121 val deqPtrExt = RegInit(0.U.asTypeOf(new LqPtr)) 122 val deqPtrExtNext = Wire(new LqPtr) 123 val allowEnqueue = RegInit(true.B) 124 125 val enqPtr = enqPtrExt(0).value 126 val deqPtr = deqPtrExt.value 127 128 val deqMask = UIntToMask(deqPtr, LoadQueueSize) 129 val enqMask = UIntToMask(enqPtr, LoadQueueSize) 130 131 val commitCount = RegNext(io.rob.lcommit) 132 133 /** 134 * Enqueue at dispatch 135 * 136 * Currently, LoadQueue only allows enqueue when #emptyEntries > RenameWidth(EnqWidth) 137 */ 138 io.enq.canAccept := allowEnqueue 139 140 for (i <- 0 until RenameWidth) { 141 val offset = if (i == 0) 0.U else PopCount(io.enq.needAlloc.take(i)) 142 val lqIdx = enqPtrExt(offset) 143 val index = lqIdx.value 144 when (io.enq.req(i).valid && io.enq.canAccept && io.enq.sqCanAccept && !io.brqRedirect.valid) { 145 uop(index) := io.enq.req(i).bits 146 allocated(index) := true.B 147 datavalid(index) := false.B 148 writebacked(index) := false.B 149 miss(index) := false.B 150 // listening(index) := false.B 151 pending(index) := false.B 152 } 153 io.enq.resp(i) := lqIdx 154 } 155 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n") 156 157 /** 158 * Writeback load from load units 159 * 160 * Most load instructions writeback to regfile at the same time. 161 * However, 162 * (1) For an mmio instruction with exceptions, it writes back to ROB immediately. 163 * (2) For an mmio instruction without exceptions, it does not write back. 164 * The mmio instruction will be sent to lower level when it reaches ROB's head. 165 * After uncache response, it will write back through arbiter with loadUnit. 166 * (3) For cache misses, it is marked miss and sent to dcache later. 167 * After cache refills, it will write back through arbiter with loadUnit. 168 */ 169 for (i <- 0 until LoadPipelineWidth) { 170 dataModule.io.wb.wen(i) := false.B 171 val loadWbIndex = io.loadIn(i).bits.uop.lqIdx.value 172 when(io.loadIn(i).fire()) { 173 when(io.loadIn(i).bits.miss) { 174 XSInfo(io.loadIn(i).valid, "load miss write to lq idx %d pc 0x%x vaddr %x paddr %x data %x mask %x forwardData %x forwardMask: %x mmio %x\n", 175 io.loadIn(i).bits.uop.lqIdx.asUInt, 176 io.loadIn(i).bits.uop.cf.pc, 177 io.loadIn(i).bits.vaddr, 178 io.loadIn(i).bits.paddr, 179 io.loadIn(i).bits.data, 180 io.loadIn(i).bits.mask, 181 io.loadIn(i).bits.forwardData.asUInt, 182 io.loadIn(i).bits.forwardMask.asUInt, 183 io.loadIn(i).bits.mmio 184 ) 185 }.otherwise { 186 XSInfo(io.loadIn(i).valid, "load hit write to cbd lqidx %d pc 0x%x vaddr %x paddr %x data %x mask %x forwardData %x forwardMask: %x mmio %x\n", 187 io.loadIn(i).bits.uop.lqIdx.asUInt, 188 io.loadIn(i).bits.uop.cf.pc, 189 io.loadIn(i).bits.vaddr, 190 io.loadIn(i).bits.paddr, 191 io.loadIn(i).bits.data, 192 io.loadIn(i).bits.mask, 193 io.loadIn(i).bits.forwardData.asUInt, 194 io.loadIn(i).bits.forwardMask.asUInt, 195 io.loadIn(i).bits.mmio 196 )} 197 datavalid(loadWbIndex) := (!io.loadIn(i).bits.miss || io.loadDataForwarded(i)) && 198 !io.loadIn(i).bits.mmio && // mmio data is not valid until we finished uncache access 199 !io.needReplayFromRS(i) // do not writeback if that inst will be resend from rs 200 writebacked(loadWbIndex) := !io.loadIn(i).bits.miss && !io.loadIn(i).bits.mmio 201 202 val loadWbData = Wire(new LQDataEntry) 203 loadWbData.paddr := io.loadIn(i).bits.paddr 204 loadWbData.mask := io.loadIn(i).bits.mask 205 loadWbData.data := io.loadIn(i).bits.forwardData.asUInt // fwd data 206 loadWbData.fwdMask := io.loadIn(i).bits.forwardMask 207 dataModule.io.wbWrite(i, loadWbIndex, loadWbData) 208 dataModule.io.wb.wen(i) := true.B 209 210 211 debug_mmio(loadWbIndex) := io.loadIn(i).bits.mmio 212 debug_paddr(loadWbIndex) := io.loadIn(i).bits.paddr 213 214 val dcacheMissed = io.loadIn(i).bits.miss && !io.loadIn(i).bits.mmio 215 miss(loadWbIndex) := dcacheMissed && !io.loadDataForwarded(i) && !io.needReplayFromRS(i) 216 pending(loadWbIndex) := io.loadIn(i).bits.mmio 217 uop(loadWbIndex).debugInfo := io.loadIn(i).bits.uop.debugInfo 218 } 219 // vaddrModule write is delayed, as vaddrModule will not be read right after write 220 vaddrModule.io.waddr(i) := RegNext(loadWbIndex) 221 vaddrModule.io.wdata(i) := RegNext(io.loadIn(i).bits.vaddr) 222 vaddrModule.io.wen(i) := RegNext(io.loadIn(i).fire()) 223 } 224 225 when(io.dcache.valid) { 226 XSDebug("miss resp: paddr:0x%x data %x\n", io.dcache.bits.addr, io.dcache.bits.data) 227 } 228 229 // Refill 64 bit in a cycle 230 // Refill data comes back from io.dcache.resp 231 dataModule.io.refill.valid := io.dcache.valid 232 dataModule.io.refill.paddr := io.dcache.bits.addr 233 dataModule.io.refill.data := io.dcache.bits.data 234 235 (0 until LoadQueueSize).map(i => { 236 dataModule.io.refill.refillMask(i) := allocated(i) && miss(i) 237 when(dataModule.io.refill.valid && dataModule.io.refill.refillMask(i) && dataModule.io.refill.matchMask(i)) { 238 datavalid(i) := true.B 239 miss(i) := false.B 240 refilling(i) := true.B 241 } 242 }) 243 244 // Writeback up to 2 missed load insts to CDB 245 // 246 // Pick 2 missed load (data refilled), write them back to cdb 247 // 2 refilled load will be selected from even/odd entry, separately 248 249 // Stage 0 250 // Generate writeback indexes 251 252 def getEvenBits(input: UInt): UInt = { 253 VecInit((0 until LoadQueueSize/2).map(i => {input(2*i)})).asUInt 254 } 255 def getOddBits(input: UInt): UInt = { 256 VecInit((0 until LoadQueueSize/2).map(i => {input(2*i+1)})).asUInt 257 } 258 259 val loadWbSel = Wire(Vec(LoadPipelineWidth, UInt(log2Up(LoadQueueSize).W))) // index selected last cycle 260 val loadWbSelV = Wire(Vec(LoadPipelineWidth, Bool())) // index selected in last cycle is valid 261 262 val loadWbSelVec = VecInit((0 until LoadQueueSize).map(i => { 263 allocated(i) && !writebacked(i) && (datavalid(i) || refilling(i)) 264 })).asUInt() // use uint instead vec to reduce verilog lines 265 val evenDeqMask = getEvenBits(deqMask) 266 val oddDeqMask = getOddBits(deqMask) 267 // generate lastCycleSelect mask 268 val evenSelectMask = Mux(io.ldout(0).fire(), getEvenBits(UIntToOH(loadWbSel(0))), 0.U) 269 val oddSelectMask = Mux(io.ldout(1).fire(), getOddBits(UIntToOH(loadWbSel(1))), 0.U) 270 // generate real select vec 271 val loadEvenSelVec = getEvenBits(loadWbSelVec) & ~evenSelectMask 272 val loadOddSelVec = getOddBits(loadWbSelVec) & ~oddSelectMask 273 274 def toVec(a: UInt): Vec[Bool] = { 275 VecInit(a.asBools) 276 } 277 278 val loadWbSelGen = Wire(Vec(LoadPipelineWidth, UInt(log2Up(LoadQueueSize).W))) 279 val loadWbSelVGen = Wire(Vec(LoadPipelineWidth, Bool())) 280 loadWbSelGen(0) := Cat(getFirstOne(toVec(loadEvenSelVec), evenDeqMask), 0.U(1.W)) 281 loadWbSelVGen(0):= loadEvenSelVec.asUInt.orR 282 loadWbSelGen(1) := Cat(getFirstOne(toVec(loadOddSelVec), oddDeqMask), 1.U(1.W)) 283 loadWbSelVGen(1) := loadOddSelVec.asUInt.orR 284 285 (0 until LoadPipelineWidth).map(i => { 286 loadWbSel(i) := RegNext(loadWbSelGen(i)) 287 loadWbSelV(i) := RegNext(loadWbSelVGen(i), init = false.B) 288 when(io.ldout(i).fire()){ 289 // Mark them as writebacked, so they will not be selected in the next cycle 290 writebacked(loadWbSel(i)) := true.B 291 } 292 }) 293 294 // Stage 1 295 // Use indexes generated in cycle 0 to read data 296 // writeback data to cdb 297 (0 until LoadPipelineWidth).map(i => { 298 // data select 299 dataModule.io.wb.raddr(i) := loadWbSelGen(i) 300 val rdata = dataModule.io.wb.rdata(i).data 301 val seluop = uop(loadWbSel(i)) 302 val func = seluop.ctrl.fuOpType 303 val raddr = dataModule.io.wb.rdata(i).paddr 304 val rdataSel = LookupTree(raddr(2, 0), List( 305 "b000".U -> rdata(63, 0), 306 "b001".U -> rdata(63, 8), 307 "b010".U -> rdata(63, 16), 308 "b011".U -> rdata(63, 24), 309 "b100".U -> rdata(63, 32), 310 "b101".U -> rdata(63, 40), 311 "b110".U -> rdata(63, 48), 312 "b111".U -> rdata(63, 56) 313 )) 314 val rdataPartialLoad = rdataHelper(seluop, rdataSel) 315 316 // writeback missed int/fp load 317 // 318 // Int load writeback will finish (if not blocked) in one cycle 319 io.ldout(i).bits.uop := seluop 320 io.ldout(i).bits.uop.lqIdx := loadWbSel(i).asTypeOf(new LqPtr) 321 io.ldout(i).bits.data := rdataPartialLoad 322 io.ldout(i).bits.redirectValid := false.B 323 io.ldout(i).bits.redirect := DontCare 324 io.ldout(i).bits.debug.isMMIO := debug_mmio(loadWbSel(i)) 325 io.ldout(i).bits.debug.isPerfCnt := false.B 326 io.ldout(i).bits.debug.paddr := debug_paddr(loadWbSel(i)) 327 io.ldout(i).bits.fflags := DontCare 328 io.ldout(i).valid := loadWbSelV(i) 329 330 when(io.ldout(i).fire()) { 331 XSInfo("int load miss write to cbd robidx %d lqidx %d pc 0x%x mmio %x\n", 332 io.ldout(i).bits.uop.robIdx.asUInt, 333 io.ldout(i).bits.uop.lqIdx.asUInt, 334 io.ldout(i).bits.uop.cf.pc, 335 debug_mmio(loadWbSel(i)) 336 ) 337 } 338 339 }) 340 341 /** 342 * Load commits 343 * 344 * When load commited, mark it as !allocated and move deqPtrExt forward. 345 */ 346 (0 until CommitWidth).map(i => { 347 when(commitCount > i.U){ 348 allocated((deqPtrExt+i.U).value) := false.B 349 } 350 }) 351 352 def getFirstOne(mask: Vec[Bool], startMask: UInt) = { 353 val length = mask.length 354 val highBits = (0 until length).map(i => mask(i) & ~startMask(i)) 355 val highBitsUint = Cat(highBits.reverse) 356 PriorityEncoder(Mux(highBitsUint.orR(), highBitsUint, mask.asUInt)) 357 } 358 359 def getOldestInTwo(valid: Seq[Bool], uop: Seq[MicroOp]) = { 360 assert(valid.length == uop.length) 361 assert(valid.length == 2) 362 Mux(valid(0) && valid(1), 363 Mux(isAfter(uop(0).robIdx, uop(1).robIdx), uop(1), uop(0)), 364 Mux(valid(0) && !valid(1), uop(0), uop(1))) 365 } 366 367 def getAfterMask(valid: Seq[Bool], uop: Seq[MicroOp]) = { 368 assert(valid.length == uop.length) 369 val length = valid.length 370 (0 until length).map(i => { 371 (0 until length).map(j => { 372 Mux(valid(i) && valid(j), 373 isAfter(uop(i).robIdx, uop(j).robIdx), 374 Mux(!valid(i), true.B, false.B)) 375 }) 376 }) 377 } 378 379 /** 380 * Memory violation detection 381 * 382 * When store writes back, it searches LoadQueue for younger load instructions 383 * with the same load physical address. They loaded wrong data and need re-execution. 384 * 385 * Cycle 0: Store Writeback 386 * Generate match vector for store address with rangeMask(stPtr, enqPtr). 387 * Besides, load instructions in LoadUnit_S1 and S2 are also checked. 388 * Cycle 1: Redirect Generation 389 * There're three possible types of violations, up to 6 possible redirect requests. 390 * Choose the oldest load (part 1). (4 + 2) -> (1 + 2) 391 * Cycle 2: Redirect Fire 392 * Choose the oldest load (part 2). (3 -> 1) 393 * Prepare redirect request according to the detected violation. 394 * Fire redirect request (if valid) 395 */ 396 397 // stage 0: lq l1 wb l1 wb lq 398 // | | | | | | (paddr match) 399 // stage 1: lq l1 wb l1 wb lq 400 // | | | | | | 401 // | |------------| | 402 // | | | 403 // stage 2: lq l1wb lq 404 // | | | 405 // -------------------- 406 // | 407 // rollback req 408 io.load_s1 := DontCare 409 def detectRollback(i: Int) = { 410 val startIndex = io.storeIn(i).bits.uop.lqIdx.value 411 val lqIdxMask = UIntToMask(startIndex, LoadQueueSize) 412 val xorMask = lqIdxMask ^ enqMask 413 val sameFlag = io.storeIn(i).bits.uop.lqIdx.flag === enqPtrExt(0).flag 414 val toEnqPtrMask = Mux(sameFlag, xorMask, ~xorMask) 415 416 // check if load already in lq needs to be rolledback 417 dataModule.io.violation(i).paddr := io.storeIn(i).bits.paddr 418 dataModule.io.violation(i).mask := io.storeIn(i).bits.mask 419 val addrMaskMatch = RegNext(dataModule.io.violation(i).violationMask) 420 val entryNeedCheck = RegNext(VecInit((0 until LoadQueueSize).map(j => { 421 allocated(j) && toEnqPtrMask(j) && (datavalid(j) || miss(j)) 422 }))) 423 val lqViolationVec = VecInit((0 until LoadQueueSize).map(j => { 424 addrMaskMatch(j) && entryNeedCheck(j) 425 })) 426 val lqViolation = lqViolationVec.asUInt().orR() 427 val lqViolationIndex = getFirstOne(lqViolationVec, RegNext(lqIdxMask)) 428 val lqViolationUop = uop(lqViolationIndex) 429 // lqViolationUop.lqIdx.flag := deqMask(lqViolationIndex) ^ deqPtrExt.flag 430 // lqViolationUop.lqIdx.value := lqViolationIndex 431 XSDebug(lqViolation, p"${Binary(Cat(lqViolationVec))}, $startIndex, $lqViolationIndex\n") 432 433 // when l/s writeback to rob together, check if rollback is needed 434 val wbViolationVec = RegNext(VecInit((0 until LoadPipelineWidth).map(j => { 435 io.loadIn(j).valid && 436 isAfter(io.loadIn(j).bits.uop.robIdx, io.storeIn(i).bits.uop.robIdx) && 437 io.storeIn(i).bits.paddr(PAddrBits - 1, 3) === io.loadIn(j).bits.paddr(PAddrBits - 1, 3) && 438 (io.storeIn(i).bits.mask & io.loadIn(j).bits.mask).orR 439 }))) 440 val wbViolation = wbViolationVec.asUInt().orR() 441 val wbViolationUop = getOldestInTwo(wbViolationVec, RegNext(VecInit(io.loadIn.map(_.bits.uop)))) 442 XSDebug(wbViolation, p"${Binary(Cat(wbViolationVec))}, $wbViolationUop\n") 443 444 // check if rollback is needed for load in l1 445 val l1ViolationVec = RegNext(VecInit((0 until LoadPipelineWidth).map(j => { 446 io.load_s1(j).valid && // L1 valid 447 isAfter(io.load_s1(j).uop.robIdx, io.storeIn(i).bits.uop.robIdx) && 448 io.storeIn(i).bits.paddr(PAddrBits - 1, 3) === io.load_s1(j).paddr(PAddrBits - 1, 3) && 449 (io.storeIn(i).bits.mask & io.load_s1(j).mask).orR 450 }))) 451 val l1Violation = l1ViolationVec.asUInt().orR() 452 val l1ViolationUop = getOldestInTwo(l1ViolationVec, RegNext(VecInit(io.load_s1.map(_.uop)))) 453 XSDebug(l1Violation, p"${Binary(Cat(l1ViolationVec))}, $l1ViolationUop\n") 454 455 XSDebug( 456 l1Violation, 457 "need rollback (l1 load) pc %x robidx %d target %x\n", 458 io.storeIn(i).bits.uop.cf.pc, io.storeIn(i).bits.uop.robIdx.asUInt, l1ViolationUop.robIdx.asUInt 459 ) 460 XSDebug( 461 lqViolation, 462 "need rollback (ld wb before store) pc %x robidx %d target %x\n", 463 io.storeIn(i).bits.uop.cf.pc, io.storeIn(i).bits.uop.robIdx.asUInt, lqViolationUop.robIdx.asUInt 464 ) 465 XSDebug( 466 wbViolation, 467 "need rollback (ld/st wb together) pc %x robidx %d target %x\n", 468 io.storeIn(i).bits.uop.cf.pc, io.storeIn(i).bits.uop.robIdx.asUInt, wbViolationUop.robIdx.asUInt 469 ) 470 471 ((lqViolation, lqViolationUop), (wbViolation, wbViolationUop), (l1Violation, l1ViolationUop)) 472 } 473 474 def rollbackSel(a: Valid[MicroOpRbExt], b: Valid[MicroOpRbExt]): ValidIO[MicroOpRbExt] = { 475 Mux( 476 a.valid, 477 Mux( 478 b.valid, 479 Mux(isAfter(a.bits.uop.robIdx, b.bits.uop.robIdx), b, a), // a,b both valid, sel oldest 480 a // sel a 481 ), 482 b // sel b 483 ) 484 } 485 val lastCycleRedirect = RegNext(io.brqRedirect) 486 val lastlastCycleRedirect = RegNext(lastCycleRedirect) 487 488 // S2: select rollback (part1) and generate rollback request 489 // rollback check 490 // Wb/L1 rollback seq check is done in s2 491 val rollbackWb = Wire(Vec(StorePipelineWidth, Valid(new MicroOpRbExt))) 492 val rollbackL1 = Wire(Vec(StorePipelineWidth, Valid(new MicroOpRbExt))) 493 val rollbackL1Wb = Wire(Vec(StorePipelineWidth*2, Valid(new MicroOpRbExt))) 494 // Lq rollback seq check is done in s3 (next stage), as getting rollbackLq MicroOp is slow 495 val rollbackLq = Wire(Vec(StorePipelineWidth, Valid(new MicroOpRbExt))) 496 // store ftq index for store set update 497 val stFtqIdxS2 = Wire(Vec(StorePipelineWidth, new FtqPtr)) 498 val stFtqOffsetS2 = Wire(Vec(StorePipelineWidth, UInt(log2Up(PredictWidth).W))) 499 for (i <- 0 until StorePipelineWidth) { 500 val detectedRollback = detectRollback(i) 501 rollbackLq(i).valid := detectedRollback._1._1 && RegNext(io.storeIn(i).valid) 502 rollbackLq(i).bits.uop := detectedRollback._1._2 503 rollbackLq(i).bits.flag := i.U 504 rollbackWb(i).valid := detectedRollback._2._1 && RegNext(io.storeIn(i).valid) 505 rollbackWb(i).bits.uop := detectedRollback._2._2 506 rollbackWb(i).bits.flag := i.U 507 rollbackL1(i).valid := detectedRollback._3._1 && RegNext(io.storeIn(i).valid) 508 rollbackL1(i).bits.uop := detectedRollback._3._2 509 rollbackL1(i).bits.flag := i.U 510 rollbackL1Wb(2*i) := rollbackL1(i) 511 rollbackL1Wb(2*i+1) := rollbackWb(i) 512 stFtqIdxS2(i) := RegNext(io.storeIn(i).bits.uop.cf.ftqPtr) 513 stFtqOffsetS2(i) := RegNext(io.storeIn(i).bits.uop.cf.ftqOffset) 514 } 515 516 val rollbackL1WbSelected = ParallelOperation(rollbackL1Wb, rollbackSel) 517 val rollbackL1WbVReg = RegNext(rollbackL1WbSelected.valid) 518 val rollbackL1WbReg = RegEnable(rollbackL1WbSelected.bits, rollbackL1WbSelected.valid) 519 val rollbackLq0VReg = RegNext(rollbackLq(0).valid) 520 val rollbackLq0Reg = RegEnable(rollbackLq(0).bits, rollbackLq(0).valid) 521 val rollbackLq1VReg = RegNext(rollbackLq(1).valid) 522 val rollbackLq1Reg = RegEnable(rollbackLq(1).bits, rollbackLq(1).valid) 523 524 // S3: select rollback (part2), generate rollback request, then fire rollback request 525 // Note that we use robIdx - 1.U to flush the load instruction itself. 526 // Thus, here if last cycle's robIdx equals to this cycle's robIdx, it still triggers the redirect. 527 528 // FIXME: this is ugly 529 val rollbackValidVec = Seq(rollbackL1WbVReg, rollbackLq0VReg, rollbackLq1VReg) 530 val rollbackUopExtVec = Seq(rollbackL1WbReg, rollbackLq0Reg, rollbackLq1Reg) 531 532 // select uop in parallel 533 val mask = getAfterMask(rollbackValidVec, rollbackUopExtVec.map(i => i.uop)) 534 val oneAfterZero = mask(1)(0) 535 val rollbackUopExt = Mux(oneAfterZero && mask(2)(0), 536 rollbackUopExtVec(0), 537 Mux(!oneAfterZero && mask(2)(1), rollbackUopExtVec(1), rollbackUopExtVec(2))) 538 val stFtqIdxS3 = RegNext(stFtqIdxS2) 539 val stFtqOffsetS3 = RegNext(stFtqOffsetS2) 540 val rollbackUop = rollbackUopExt.uop 541 val rollbackStFtqIdx = stFtqIdxS3(rollbackUopExt.flag) 542 val rollbackStFtqOffset = stFtqOffsetS3(rollbackUopExt.flag) 543 544 // check if rollback request is still valid in parallel 545 val rollbackValidVecChecked = Wire(Vec(3, Bool())) 546 for(((v, uop), idx) <- rollbackValidVec.zip(rollbackUopExtVec.map(i => i.uop)).zipWithIndex) { 547 rollbackValidVecChecked(idx) := v && 548 (!lastCycleRedirect.valid || isBefore(uop.robIdx, lastCycleRedirect.bits.robIdx)) && 549 (!lastlastCycleRedirect.valid || isBefore(uop.robIdx, lastlastCycleRedirect.bits.robIdx)) 550 } 551 552 io.rollback.bits.robIdx := rollbackUop.robIdx 553 io.rollback.bits.ftqIdx := rollbackUop.cf.ftqPtr 554 io.rollback.bits.stFtqIdx := rollbackStFtqIdx 555 io.rollback.bits.ftqOffset := rollbackUop.cf.ftqOffset 556 io.rollback.bits.stFtqOffset := rollbackStFtqOffset 557 io.rollback.bits.level := RedirectLevel.flush 558 io.rollback.bits.interrupt := DontCare 559 io.rollback.bits.cfiUpdate := DontCare 560 io.rollback.bits.cfiUpdate.target := rollbackUop.cf.pc 561 io.rollback.bits.debug_runahead_checkpoint_id := rollbackUop.debugInfo.runahead_checkpoint_id 562 // io.rollback.bits.pc := DontCare 563 564 io.rollback.valid := rollbackValidVecChecked.asUInt.orR 565 566 when(io.rollback.valid) { 567 // XSDebug("Mem rollback: pc %x robidx %d\n", io.rollback.bits.cfi, io.rollback.bits.robIdx.asUInt) 568 } 569 570 /** 571 * Memory mapped IO / other uncached operations 572 * 573 * States: 574 * (1) writeback from store units: mark as pending 575 * (2) when they reach ROB's head, they can be sent to uncache channel 576 * (3) response from uncache channel: mark as datavalid 577 * (4) writeback to ROB (and other units): mark as writebacked 578 * (5) ROB commits the instruction: same as normal instructions 579 */ 580 //(2) when they reach ROB's head, they can be sent to uncache channel 581 val lqTailMmioPending = WireInit(pending(deqPtr)) 582 val lqTailAllocated = WireInit(allocated(deqPtr)) 583 val s_idle :: s_req :: s_resp :: s_wait :: Nil = Enum(4) 584 val uncacheState = RegInit(s_idle) 585 switch(uncacheState) { 586 is(s_idle) { 587 when(io.rob.pendingld && lqTailMmioPending && lqTailAllocated) { 588 uncacheState := s_req 589 } 590 } 591 is(s_req) { 592 when(io.uncache.req.fire()) { 593 uncacheState := s_resp 594 } 595 } 596 is(s_resp) { 597 when(io.uncache.resp.fire()) { 598 uncacheState := s_wait 599 } 600 } 601 is(s_wait) { 602 when(io.rob.commit) { 603 uncacheState := s_idle // ready for next mmio 604 } 605 } 606 } 607 io.uncache.req.valid := uncacheState === s_req 608 609 dataModule.io.uncache.raddr := deqPtrExtNext.value 610 611 io.uncache.req.bits.cmd := MemoryOpConstants.M_XRD 612 io.uncache.req.bits.addr := dataModule.io.uncache.rdata.paddr 613 io.uncache.req.bits.data := dataModule.io.uncache.rdata.data 614 io.uncache.req.bits.mask := dataModule.io.uncache.rdata.mask 615 616 io.uncache.req.bits.id := DontCare 617 io.uncache.req.bits.instrtype := DontCare 618 619 io.uncache.resp.ready := true.B 620 621 when (io.uncache.req.fire()) { 622 pending(deqPtr) := false.B 623 624 XSDebug("uncache req: pc %x addr %x data %x op %x mask %x\n", 625 uop(deqPtr).cf.pc, 626 io.uncache.req.bits.addr, 627 io.uncache.req.bits.data, 628 io.uncache.req.bits.cmd, 629 io.uncache.req.bits.mask 630 ) 631 } 632 633 // (3) response from uncache channel: mark as datavalid 634 dataModule.io.uncache.wen := false.B 635 when(io.uncache.resp.fire()){ 636 datavalid(deqPtr) := true.B 637 dataModule.io.uncacheWrite(deqPtr, io.uncache.resp.bits.data(XLEN-1, 0)) 638 dataModule.io.uncache.wen := true.B 639 640 XSDebug("uncache resp: data %x\n", io.dcache.bits.data) 641 } 642 643 // Read vaddr for mem exception 644 // no inst will be commited 1 cycle before tval update 645 vaddrModule.io.raddr(0) := (deqPtrExt + commitCount).value 646 io.exceptionAddr.vaddr := vaddrModule.io.rdata(0) 647 648 // misprediction recovery / exception redirect 649 // invalidate lq term using robIdx 650 val needCancel = Wire(Vec(LoadQueueSize, Bool())) 651 for (i <- 0 until LoadQueueSize) { 652 needCancel(i) := uop(i).robIdx.needFlush(io.brqRedirect) && allocated(i) 653 when (needCancel(i)) { 654 allocated(i) := false.B 655 } 656 } 657 658 /** 659 * update pointers 660 */ 661 val lastCycleCancelCount = PopCount(RegNext(needCancel)) 662 // when io.brqRedirect.valid, we don't allow eneuque even though it may fire. 663 val enqNumber = Mux(io.enq.canAccept && io.enq.sqCanAccept && !io.brqRedirect.valid, PopCount(io.enq.req.map(_.valid)), 0.U) 664 when (lastCycleRedirect.valid) { 665 // we recover the pointers in the next cycle after redirect 666 enqPtrExt := VecInit(enqPtrExt.map(_ - lastCycleCancelCount)) 667 }.otherwise { 668 enqPtrExt := VecInit(enqPtrExt.map(_ + enqNumber)) 669 } 670 671 deqPtrExtNext := deqPtrExt + commitCount 672 deqPtrExt := deqPtrExtNext 673 674 val validCount = distanceBetween(enqPtrExt(0), deqPtrExt) 675 676 allowEnqueue := validCount + enqNumber <= (LoadQueueSize - RenameWidth).U 677 678 /** 679 * misc 680 */ 681 io.rob.storeDataRobWb := DontCare // will be overwriten by store queue's result 682 683 // perf counter 684 QueuePerf(LoadQueueSize, validCount, !allowEnqueue) 685 io.lqFull := !allowEnqueue 686 XSPerfAccumulate("rollback", io.rollback.valid) // rollback redirect generated 687 XSPerfAccumulate("mmioCycle", uncacheState =/= s_idle) // lq is busy dealing with uncache req 688 XSPerfAccumulate("mmioCnt", io.uncache.req.fire()) 689 XSPerfAccumulate("refill", io.dcache.valid) 690 XSPerfAccumulate("writeback_success", PopCount(VecInit(io.ldout.map(i => i.fire())))) 691 XSPerfAccumulate("writeback_blocked", PopCount(VecInit(io.ldout.map(i => i.valid && !i.ready)))) 692 XSPerfAccumulate("utilization_miss", PopCount((0 until LoadQueueSize).map(i => allocated(i) && miss(i)))) 693 694 // debug info 695 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtrExt.flag, deqPtr) 696 697 def PrintFlag(flag: Bool, name: String): Unit = { 698 when(flag) { 699 XSDebug(false, true.B, name) 700 }.otherwise { 701 XSDebug(false, true.B, " ") 702 } 703 } 704 705 for (i <- 0 until LoadQueueSize) { 706 XSDebug(i + " pc %x pa %x ", uop(i).cf.pc, debug_paddr(i)) 707 PrintFlag(allocated(i), "a") 708 PrintFlag(allocated(i) && datavalid(i), "v") 709 PrintFlag(allocated(i) && writebacked(i), "w") 710 PrintFlag(allocated(i) && miss(i), "m") 711 PrintFlag(allocated(i) && pending(i), "p") 712 XSDebug(false, true.B, "\n") 713 } 714 715} 716