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