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