1package xiangshan.backend.issue 2 3import chisel3.{util, _} 4import chisel3.util._ 5import utils.{ParallelMux, ParallelOR, PriorityEncoderWithFlag, PriorityMuxWithFlag, XSDebug, XSInfo} 6import xiangshan._ 7import xiangshan.backend.exu.{Exu, ExuConfig} 8import xiangshan.backend.regfile.RfReadPort 9 10class IssueQueue 11( 12 val exuCfg: ExuConfig, 13 val wakeupCnt: Int, 14 val bypassCnt: Int = 0 15) extends XSModule with HasIQConst { 16 val io = IO(new Bundle() { 17 val redirect = Flipped(ValidIO(new Redirect)) 18 val enq = Flipped(DecoupledIO(new MicroOp)) 19 val readIntRf = Vec(exuCfg.intSrcCnt, Flipped(new RfReadPort)) 20 val readFpRf = Vec(exuCfg.fpSrcCnt, Flipped(new RfReadPort)) 21 val deq = DecoupledIO(new ExuInput) 22 val wakeUpPorts = Vec(wakeupCnt, Flipped(ValidIO(new ExuOutput))) 23 val bypassUops = Vec(bypassCnt, Flipped(ValidIO(new MicroOp))) 24 val bypassData = Vec(bypassCnt, Flipped(ValidIO(new ExuOutput))) 25 val numExist = Output(UInt(iqIdxWidth.W)) 26 // tlb hit, inst can deq 27 val tlbFeedback = Flipped(ValidIO(new TlbFeedback)) 28 }) 29 30 def qsize: Int = IssQueSize 31 def idxWidth = log2Up(qsize) 32 def replayDelay = 16 33 34 require(isPow2(qsize)) 35 36 val tlbHit = io.tlbFeedback.valid && io.tlbFeedback.bits.hit 37 val tlbMiss = io.tlbFeedback.valid && !io.tlbFeedback.bits.hit 38 39 XSDebug(io.tlbFeedback.valid, 40 "tlb feedback: hit: %d roqIdx: %d\n", 41 io.tlbFeedback.bits.hit, 42 io.tlbFeedback.bits.roqIdx 43 ) 44 /* 45 invalid --[enq]--> valid --[deq]--> wait --[tlbHit]--> invalid 46 wait --[replay]--> replay --[cnt]--> valid 47 */ 48 val s_invalid :: s_valid :: s_wait :: s_replay :: Nil = Enum(4) 49 50 val idxQueue = RegInit(VecInit((0 until qsize).map(_.U(idxWidth.W)))) 51 val stateQueue = RegInit(VecInit(Seq.fill(qsize)(s_invalid))) 52 53 val readyVec = Wire(Vec(qsize, Bool())) 54 val uopQueue = Reg(Vec(qsize, new MicroOp)) 55 val cntQueue = Reg(Vec(qsize, UInt(log2Up(replayDelay).W))) 56 57 val tailPtr = RegInit(0.U((idxWidth+1).W)) 58 59 // real deq 60 61 /* 62 example: realDeqIdx = 2 | realDeqIdx=0 63 moveMask = 11111100 | moveMask=11111111 64 */ 65 66 val (firstBubble, findBubble) = PriorityEncoderWithFlag(stateQueue.map(_ === s_invalid)) 67 val realDeqIdx = firstBubble 68 val realDeqValid = (firstBubble < tailPtr) && findBubble 69 val moveMask = { 70 (Fill(qsize, 1.U(1.W)) << realDeqIdx)(qsize-1, 0) 71 } & Fill(qsize, realDeqValid) 72 73 for(i <- 0 until qsize-1){ 74 when(moveMask(i)){ 75 idxQueue(i) := idxQueue(i+1) 76 stateQueue(i) := stateQueue(i+1) 77 } 78 } 79 when(realDeqValid){ 80 idxQueue.last := idxQueue(realDeqIdx) 81 stateQueue.last := s_invalid 82 } 83 84 85 // wake up 86 def getSrcSeq(uop: MicroOp): Seq[UInt] = Seq(uop.psrc1, uop.psrc2, uop.psrc3) 87 def getSrcTypeSeq(uop: MicroOp): Seq[UInt] = Seq( 88 uop.ctrl.src1Type, uop.ctrl.src2Type, uop.ctrl.src3Type 89 ) 90 def getSrcStateSeq(uop: MicroOp): Seq[UInt] = Seq( 91 uop.src1State, uop.src2State, uop.src3State 92 ) 93 94 def writeBackHit(src: UInt, srcType: UInt, wbUop: (Bool, MicroOp)): Bool = { 95 val (v, uop) = wbUop 96 val isSameType = 97 (SrcType.isReg(srcType) && uop.ctrl.rfWen) || (SrcType.isFp(srcType) && uop.ctrl.fpWen) 98 99 v && isSameType && (src===uop.pdest) 100 } 101 102 def doBypass(src: UInt, srcType: UInt): (Bool, UInt) = { 103 val hitVec = io.bypassData.map(p => (p.valid, p.bits.uop)). 104 map(wbUop => writeBackHit(src, srcType, wbUop)) 105 val data = ParallelMux(hitVec.zip(io.bypassData.map(_.bits.data))) 106 (ParallelOR(hitVec).asBool(), data) 107 } 108 109 def wakeUp(uop: MicroOp): MicroOp = { 110 def getNewSrcState(i: Int): UInt = { 111 val src = getSrcSeq(uop)(i) 112 val srcType = getSrcTypeSeq(uop)(i) 113 val srcState = getSrcStateSeq(uop)(i) 114 val hitVec = ( 115 io.wakeUpPorts.map(w => (w.valid, w.bits.uop)) ++ 116 io.bypassUops.map(p => (p.valid, p.bits)) 117 ).map(wbUop => writeBackHit(src, srcType, wbUop)) 118 val hit = ParallelOR(hitVec).asBool() 119 Mux(hit, SrcState.rdy, srcState) 120 } 121 val new_uop = WireInit(uop) 122 new_uop.src1State := getNewSrcState(0) 123 if(exuCfg==Exu.stExeUnitCfg) new_uop.src2State := getNewSrcState(1) 124 new_uop 125 } 126 127 def uopIsRdy(uop: MicroOp): Bool = { 128 def srcIsRdy(srcType: UInt, srcState: UInt): Bool = { 129 SrcType.isPcImm(srcType) || srcState===SrcState.rdy 130 } 131 exuCfg match { 132 case Exu.ldExeUnitCfg => 133 srcIsRdy(uop.ctrl.src1Type, uop.src1State) 134 case Exu.stExeUnitCfg => 135 srcIsRdy(uop.ctrl.src1Type, uop.src1State) && srcIsRdy(uop.ctrl.src2Type, uop.src2State) 136 } 137 } 138 139 for(i <- 0 until qsize){ 140 val newUop = wakeUp(uopQueue(i)) 141 uopQueue(i) := newUop 142 readyVec(i) := uopIsRdy(newUop) 143 } 144 145 // select 146 val selectedIdxRegOH = Wire(UInt(qsize.W)) 147 val selectMask = WireInit(VecInit( 148 (0 until qsize).map(i => 149 (stateQueue(i)===s_valid) && readyVec(idxQueue(i)) && !(selectedIdxRegOH(i) && io.deq.fire()) 150 ) 151 )) 152 val (selectedIdxWire, sel) = PriorityMuxWithFlag( 153 selectMask.zipWithIndex.map(x => (x._1, x._2.U)).reverse 154 ) 155 val selReg = RegNext(sel) 156 val selectedIdxReg = RegNext(selectedIdxWire - moveMask(selectedIdxWire)) 157 selectedIdxRegOH := UIntToOH(selectedIdxReg) 158 XSDebug( 159 p"selMaskWire:${Binary(selectMask.asUInt())} selected:$selectedIdxWire" + 160 p" moveMask:${Binary(moveMask)} selectedIdxReg:$selectedIdxReg\n" 161 ) 162 163 164 // read regfile 165 val selectedUop = uopQueue(idxQueue(selectedIdxWire)) 166 167 exuCfg match { 168 case Exu.ldExeUnitCfg => 169 io.readIntRf(0).addr := selectedUop.psrc1 // base 170 XSDebug(p"src1 read addr: ${io.readIntRf(0).addr}\n") 171 case Exu.stExeUnitCfg => 172 io.readIntRf(0).addr := selectedUop.psrc1 // base 173 io.readIntRf(1).addr := selectedUop.psrc2 // store data (int) 174 io.readFpRf(0).addr := selectedUop.psrc2 // store data (fp) 175 XSDebug( 176 p"src1 read addr: ${io.readIntRf(0).addr} src2 read addr: ${io.readIntRf(1).addr}\n" 177 ) 178 case _ => 179 require(requirement = false, "Error: IssueQueue only support ldu and stu!") 180 } 181 182 // (fake) deq to Load/Store unit 183 io.deq.valid := (stateQueue(selectedIdxReg)===s_valid) && readyVec(idxQueue(selectedIdxReg)) && selReg 184 io.deq.bits.uop := uopQueue(idxQueue(selectedIdxReg)) 185 186 val src1Bypass = doBypass(io.deq.bits.uop.psrc1, io.deq.bits.uop.ctrl.src1Type) 187 io.deq.bits.src1 := Mux(src1Bypass._1, src1Bypass._2, io.readIntRf(0).data) 188 if(exuCfg == Exu.stExeUnitCfg){ 189 val src2Bypass = doBypass(io.deq.bits.uop.psrc2, io.deq.bits.uop.ctrl.src2Type) 190 io.deq.bits.src2 := Mux(src2Bypass._1, 191 src2Bypass._2, 192 Mux(SrcType.isReg(io.deq.bits.uop.ctrl.src2Type), 193 io.readIntRf(1).data, 194 io.readFpRf(0).data 195 ) 196 ) 197 } else { 198 io.deq.bits.src2 := DontCare 199 } 200 io.deq.bits.src3 := DontCare 201 202 when(io.deq.fire()){ 203 stateQueue(selectedIdxReg - moveMask(selectedIdxReg)) := s_wait 204 assert(stateQueue(selectedIdxReg) === s_valid, "Dequeue a invalid entry to lsu!") 205 } 206 207// assert(!(tailPtr===0.U && tlbHit), "Error: queue is empty but tlbHit is true!") 208 209 val tailAfterRealDeq = tailPtr - moveMask(tailPtr.tail(1)) 210 val isFull = tailAfterRealDeq.head(1).asBool() // tailPtr===qsize.U 211 212 // enq 213 io.enq.ready := !isFull && !io.redirect.valid 214 when(io.enq.fire()){ 215 stateQueue(tailAfterRealDeq.tail(1)) := s_valid 216 val uopQIdx = idxQueue(tailPtr.tail(1)) 217 val new_uop = wakeUp(io.enq.bits) 218 uopQueue(uopQIdx) := new_uop 219 } 220 221 tailPtr := tailAfterRealDeq + io.enq.fire() 222 223 XSDebug( 224 realDeqValid, 225 p"realDeqIdx:$realDeqIdx\n" 226 ) 227 228 XSDebug("State Dump: ") 229 stateQueue.reverse.foreach(s =>{ 230 XSDebug(false, s===s_invalid, "-") 231 XSDebug(false, s===s_valid, "v") 232 XSDebug(false, s===s_wait, "w") 233 XSDebug(false, s===s_replay, "p") 234 }) 235 XSDebug(false, true.B, "\n") 236 237 XSDebug("State Dump: ") 238 idxQueue.reverse.foreach(id =>{ 239 XSDebug(false, true.B, p"$id") 240 }) 241 XSDebug(false, true.B, "\n") 242 243 XSDebug("State Dump: ") 244 for(i <- readyVec.indices.reverse){ 245 val r = readyVec(idxQueue(i)) 246 XSDebug(false, r, p"r") 247 XSDebug(false, !r, p"-") 248 } 249 XSDebug(false, true.B, "\n") 250 251// assert(!(tlbMiss && realDeqValid), "Error: realDeqValid should be false when replay valid!") 252 for(i <- 0 until qsize){ 253 val uopQIdx = idxQueue(i) 254 val uop = uopQueue(uopQIdx) 255 val cnt = cntQueue(uopQIdx) 256 val nextIdx = i.U - moveMask(i) 257 //TODO: support replay 258 val roqIdxMatch = uop.roqIdx === io.tlbFeedback.bits.roqIdx 259 val notEmpty = stateQueue(i)=/=s_invalid 260 val replayThis = (stateQueue(i)===s_wait) && tlbMiss && roqIdxMatch 261 val tlbHitThis = notEmpty && tlbHit && roqIdxMatch 262 val flushThis = notEmpty && uop.needFlush(io.redirect) 263 264 when(replayThis){ 265 stateQueue(nextIdx) := s_replay 266 cnt := (replayDelay-1).U 267 } 268 when(stateQueue(i)===s_replay){ 269 when(cnt === 0.U){ 270 stateQueue(nextIdx) := s_valid 271 }.otherwise({ 272 cnt := cnt - 1.U 273 }) 274 } 275 when(flushThis || tlbHitThis){ 276 stateQueue(nextIdx) := s_invalid 277 } 278 } 279 280 281 // assign outputs 282 // TODO currently set to zero 283 io.numExist := 0.U//Mux(isFull, (qsize-1).U, tailPtr) 284 285 // Debug sigs 286 XSInfo( 287 io.enq.fire(), 288 p"enq fire: pc:${Hexadecimal(io.enq.bits.cf.pc)} roqIdx:${io.enq.bits.roqIdx} " + 289 p"src1: ${io.enq.bits.psrc1} src2:${io.enq.bits.psrc2} pdst:${io.enq.bits.pdest}\n" 290 ) 291 XSInfo( 292 io.deq.fire(), 293 p"deq fire: pc:${Hexadecimal(io.deq.bits.uop.cf.pc)} roqIdx:${io.deq.bits.uop.roqIdx} " + 294 p"src1: ${io.deq.bits.uop.psrc1} data: ${Hexadecimal(io.deq.bits.src1)} " + 295 p"src2: ${io.deq.bits.uop.psrc2} data: ${Hexadecimal(io.deq.bits.src2)} " + 296 p"imm : ${Hexadecimal(io.deq.bits.uop.ctrl.imm)}\npdest: ${io.deq.bits.uop.pdest}\n" 297 ) 298 XSDebug(p"tailPtr:$tailPtr tailAfterDeq:$tailAfterRealDeq tlbHit:$tlbHit\n") 299} 300