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 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 = PriorityEncoder(selectMask) 153 val selectedIdxReg = RegNext(selectedIdxWire - moveMask(selectedIdxWire)) 154 selectedIdxRegOH := UIntToOH(selectedIdxReg) 155 XSDebug( 156 p"selMaskWire:${Binary(selectMask.asUInt())} selected:$selectedIdxWire" + 157 p" moveMask:${Binary(moveMask)} selectedIdxReg:$selectedIdxReg\n" 158 ) 159 160 161 // read regfile 162 val selectedUop = uopQueue(idxQueue(selectedIdxWire)) 163 164 exuCfg match { 165 case Exu.ldExeUnitCfg => 166 io.readIntRf(0).addr := selectedUop.psrc1 // base 167 XSDebug(p"src1 read addr: ${io.readIntRf(0).addr}\n") 168 case Exu.stExeUnitCfg => 169 io.readIntRf(0).addr := selectedUop.psrc1 // base 170 io.readIntRf(1).addr := selectedUop.psrc2 // store data (int) 171 io.readFpRf(0).addr := selectedUop.psrc2 // store data (fp) 172 XSDebug( 173 p"src1 read addr: ${io.readIntRf(0).addr} src2 read addr: ${io.readIntRf(1).addr}\n" 174 ) 175 case _ => 176 require(requirement = false, "Error: IssueQueue only support ldu and stu!") 177 } 178 179 // (fake) deq to Load/Store unit 180 io.deq.valid := (stateQueue(selectedIdxReg)===s_valid) && readyVec(idxQueue(selectedIdxReg)) 181 io.deq.bits.uop := uopQueue(idxQueue(selectedIdxReg)) 182 183 val src1Bypass = doBypass(io.deq.bits.uop.psrc1, io.deq.bits.uop.ctrl.src1Type) 184 io.deq.bits.src1 := Mux(src1Bypass._1, src1Bypass._2, io.readIntRf(0).data) 185 if(exuCfg == Exu.stExeUnitCfg){ 186 val src2Bypass = doBypass(io.deq.bits.uop.psrc2, io.deq.bits.uop.ctrl.src2Type) 187 io.deq.bits.src2 := Mux(src2Bypass._1, 188 src2Bypass._2, 189 Mux(SrcType.isReg(io.deq.bits.uop.ctrl.src2Type), 190 io.readIntRf(1).data, 191 io.readFpRf(0).data 192 ) 193 ) 194 } else { 195 io.deq.bits.src2 := DontCare 196 } 197 io.deq.bits.src3 := DontCare 198 199 when(io.deq.fire()){ 200 stateQueue(selectedIdxReg - moveMask(selectedIdxReg)) := s_wait 201 assert(stateQueue(selectedIdxReg) === s_valid, "Dequeue a invalid entry to lsu!") 202 } 203 204// assert(!(tailPtr===0.U && tlbHit), "Error: queue is empty but tlbHit is true!") 205 206 val tailAfterRealDeq = tailPtr - moveMask(tailPtr.tail(1)) 207 val isFull = tailAfterRealDeq.head(1).asBool() // tailPtr===qsize.U 208 209 // enq 210 io.enq.ready := !isFull && !io.redirect.valid 211 when(io.enq.fire()){ 212 stateQueue(tailAfterRealDeq.tail(1)) := s_valid 213 val uopQIdx = idxQueue(tailPtr.tail(1)) 214 val new_uop = wakeUp(io.enq.bits) 215 uopQueue(uopQIdx) := new_uop 216 } 217 218 tailPtr := tailAfterRealDeq + io.enq.fire() 219 220 XSDebug( 221 realDeqValid, 222 p"realDeqIdx:$realDeqIdx\n" 223 ) 224 225 XSDebug("State Dump: ") 226 stateQueue.reverse.foreach(s =>{ 227 XSDebug(false, s===s_invalid, "-") 228 XSDebug(false, s===s_valid, "v") 229 XSDebug(false, s===s_wait, "w") 230 XSDebug(false, s===s_replay, "p") 231 }) 232 XSDebug(false, true.B, "\n") 233 234 XSDebug("State Dump: ") 235 idxQueue.reverse.foreach(id =>{ 236 XSDebug(false, true.B, p"$id") 237 }) 238 XSDebug(false, true.B, "\n") 239 240 XSDebug("State Dump: ") 241 for(i <- readyVec.indices.reverse){ 242 val r = readyVec(idxQueue(i)) 243 XSDebug(false, r, p"r") 244 XSDebug(false, !r, p"-") 245 } 246 XSDebug(false, true.B, "\n") 247 248// assert(!(tlbMiss && realDeqValid), "Error: realDeqValid should be false when replay valid!") 249 for(i <- 0 until qsize){ 250 val uopQIdx = idxQueue(i) 251 val uop = uopQueue(uopQIdx) 252 val cnt = cntQueue(uopQIdx) 253 val nextIdx = i.U - moveMask(i) 254 //TODO: support replay 255 val roqIdxMatch = uop.roqIdx === io.tlbFeedback.bits.roqIdx 256 val notEmpty = stateQueue(i)=/=s_invalid 257 val replayThis = (stateQueue(i)===s_wait) && tlbMiss && roqIdxMatch 258 val tlbHitThis = notEmpty && tlbHit && roqIdxMatch 259 val flushThis = notEmpty && uop.needFlush(io.redirect) 260 261 when(replayThis){ 262 stateQueue(nextIdx) := s_replay 263 cnt := (replayDelay-1).U 264 } 265 when(stateQueue(i)===s_replay){ 266 when(cnt === 0.U){ 267 stateQueue(nextIdx) := s_valid 268 }.otherwise({ 269 cnt := cnt - 1.U 270 }) 271 } 272 when(flushThis || tlbHitThis){ 273 stateQueue(nextIdx) := s_invalid 274 } 275 } 276 277 278 // assign outputs 279 // TODO currently set to zero 280 io.numExist := 0.U//Mux(isFull, (qsize-1).U, tailPtr) 281 282 // Debug sigs 283 XSInfo( 284 io.enq.fire(), 285 p"enq fire: pc:${Hexadecimal(io.enq.bits.cf.pc)} roqIdx:${io.enq.bits.roqIdx} " + 286 p"src1: ${io.enq.bits.psrc1} src2:${io.enq.bits.psrc2} pdst:${io.enq.bits.pdest}\n" 287 ) 288 XSInfo( 289 io.deq.fire(), 290 p"deq fire: pc:${Hexadecimal(io.deq.bits.uop.cf.pc)} roqIdx:${io.deq.bits.uop.roqIdx} " + 291 p"src1: ${io.deq.bits.uop.psrc1} data: ${Hexadecimal(io.deq.bits.src1)} " + 292 p"src2: ${io.deq.bits.uop.psrc2} data: ${Hexadecimal(io.deq.bits.src2)} " + 293 p"imm : ${Hexadecimal(io.deq.bits.uop.ctrl.imm)}\npdest: ${io.deq.bits.uop.pdest}\n" 294 ) 295 XSDebug(p"tailPtr:$tailPtr tailAfterDeq:$tailAfterRealDeq tlbHit:$tlbHit\n") 296} 297