xref: /XiangShan/src/main/scala/xiangshan/backend/issue/IssueQueue.scala (revision 4643465d0dfb5261ac6a3e17e508fee733f43c9d)
1package xiangshan.backend.issue
2
3import chisel3.{util, _}
4import chisel3.util._
5import utils.{ParallelMux, ParallelOR, 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 tlbHit = Input(Bool())
28    val replay = Flipped(ValidIO(UInt(RoqIdxWidth.W)))
29  })
30
31  def qsize: Int = IssQueSize
32  def idxWidth = log2Up(qsize)
33  def replayDelay = 16
34
35  require(isPow2(qsize))
36
37  /*
38      invalid --[enq]--> valid --[deq]--> wait --[tlbHit]--> invalid
39                                          wait --[replay]--> replay --[cnt]--> valid
40   */
41  val s_invalid :: s_valid :: s_wait :: s_replay :: Nil = Enum(4)
42
43  val idxQueue = RegInit(VecInit((0 until qsize).map(_.U(idxWidth.W))))
44  val stateQueue = RegInit(VecInit(Seq.fill(qsize)(s_invalid)))
45
46  val readyVec = Wire(Vec(qsize, Bool()))
47  val uopQueue = Reg(Vec(qsize, new MicroOp))
48  val cntQueue = Reg(Vec(qsize, UInt(log2Up(replayDelay).W)))
49
50  val tailPtr = RegInit(0.U((idxWidth+1).W))
51
52  // real deq
53
54  /*
55    example: realDeqIdx = 2       |  realDeqIdx=0
56             moveMask = 11111100  |  moveMask=11111111
57 */
58  assert(!(io.tlbHit && io.replay.valid), "Error: tlbHit and replay are both true!")
59
60  val firstWait = PriorityEncoder(stateQueue.map(_ === s_wait))
61  val firstBubble = PriorityEncoder(stateQueue.map(_ === s_invalid))
62  val realDeqIdx = Mux(io.tlbHit, firstWait, firstBubble)
63  val realDeqValid = io.tlbHit || ((firstBubble < tailPtr.tail(1)) && !io.replay.valid)
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 = RegEnable(
149    enable = io.deq.ready,
150    next = selectedIdxWire - moveMask(selectedIdxWire)
151  )
152//  selectedIdxReg := selectedIdxWire - moveMask(selectedIdxWire)
153  selectedIdxRegOH := UIntToOH(selectedIdxReg)
154  XSDebug(
155    p"selMaskWire:${Binary(selectMask.asUInt())} selected:$selectedIdxWire" +
156      p" moveMask:${Binary(moveMask)} selectedIdxReg:$selectedIdxReg\n"
157  )
158
159
160  // read regfile
161  val selectedUop = uopQueue(idxQueue(Mux(io.deq.ready, selectedIdxWire, selectedIdxReg)))
162
163  exuCfg match {
164    case Exu.ldExeUnitCfg =>
165      io.readIntRf(0).addr := selectedUop.psrc1 // base
166      XSDebug(p"src1 read addr: ${io.readIntRf(0).addr}\n")
167    case Exu.stExeUnitCfg =>
168      io.readIntRf(0).addr := selectedUop.psrc1 // base
169      io.readIntRf(1).addr := selectedUop.psrc2 // store data (int)
170      io.readFpRf(0).addr := selectedUop.psrc2  // store data (fp)
171      XSDebug(
172        p"src1 read addr: ${io.readIntRf(0).addr} src2 read addr: ${io.readIntRf(1).addr}\n"
173      )
174    case _ =>
175      require(requirement = false, "Error: IssueQueue only support ldu and stu!")
176  }
177
178  // (fake) deq to Load/Store unit
179  io.deq.valid := stateQueue(selectedIdxReg)===s_valid
180  io.deq.bits.uop := uopQueue(idxQueue(selectedIdxReg))
181
182  val src1Bypass = doBypass(io.deq.bits.uop.psrc1, io.deq.bits.uop.ctrl.src1Type)
183  io.deq.bits.src1 := Mux(src1Bypass._1, src1Bypass._2, io.readIntRf(0).data)
184  if(exuCfg == Exu.stExeUnitCfg){
185    val src2Bypass = doBypass(io.deq.bits.uop.psrc2, io.deq.bits.uop.ctrl.src2Type)
186    io.deq.bits.src2 := Mux(src2Bypass._1,
187      src2Bypass._2,
188      Mux(SrcType.isReg(io.deq.bits.uop.ctrl.src2Type),
189        io.readIntRf(1).data,
190        io.readFpRf(0).data
191      )
192    )
193  } else {
194    io.deq.bits.src2 := DontCare
195  }
196  io.deq.bits.src3 := DontCare
197
198  when(io.deq.fire()){
199    stateQueue(selectedIdxReg - moveMask(selectedIdxReg)) := s_wait
200    assert(stateQueue(selectedIdxReg) === s_valid, "Dequeue a invalid entry to lsu!")
201  }
202
203  assert(!(tailPtr===0.U && io.tlbHit), "Error: queue is empty but tlbHit is true!")
204
205  val tailAfterRealDeq = tailPtr - moveMask(tailPtr.tail(1))
206  val isFull = tailAfterRealDeq.head(1).asBool() // tailPtr===qsize.U
207
208  // enq
209  io.enq.ready := !isFull && !io.replay.valid && !io.redirect.valid
210  when(io.enq.fire()){
211    stateQueue(tailAfterRealDeq.tail(1)) := s_valid
212    val uopQIdx = idxQueue(tailAfterRealDeq.tail(1))
213    val new_uop = wakeUp(io.enq.bits)
214    uopQueue(uopQIdx) := new_uop
215  }
216
217  tailPtr := tailAfterRealDeq + io.enq.fire()
218
219  XSDebug(
220    realDeqValid,
221    p"firstWait:$firstWait firstBubble:$firstBubble realDeqIdx:$realDeqIdx\n"
222  )
223
224  XSDebug("State Dump: ")
225  stateQueue.reverse.foreach(s =>{
226    XSDebug(false, s===s_invalid, "-")
227    XSDebug(false, s===s_valid, "v")
228    XSDebug(false, s===s_wait, "w")
229    XSDebug(false, s===s_replay, "r")
230  })
231  XSDebug(false, true.B, "\n")
232
233  XSDebug("State Dump: ")
234  idxQueue.reverse.foreach(id =>{
235    XSDebug(false, true.B, p"$id")
236  })
237  XSDebug(false, true.B, "\n")
238
239  XSDebug("State Dump: ")
240  readyVec.reverse.foreach(r =>{
241    XSDebug(false, r, p"r")
242    XSDebug(false, !r, p"-")
243  })
244  XSDebug(false, true.B, "\n")
245
246  assert(!(io.replay.valid && realDeqValid), "Error: realDeqValid should be false when replay valid!")
247  for(i <- 0 until qsize){
248    val uopQIdx = idxQueue(i)
249    val cnt = cntQueue(uopQIdx)
250    val nextIdx = i.U - moveMask(i)
251    when(
252      (io.replay.valid && stateQueue(i)===s_wait) &&
253        uopQueue(uopQIdx).isAfter(io.replay.bits)
254    ){
255      // 'i' is enough because 'realDeqValid' must be false here
256      stateQueue(i) := 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        if(i == 0) {
263          assert(!moveMask(0), "Error: Attemp to delete a 's_replay' entry!")
264        }
265      }.otherwise({
266        cnt := cnt - 1.U
267      })
268    }
269    when(uopQueue(uopQIdx).needFlush(io.redirect)){
270      stateQueue(nextIdx) := s_invalid
271    }
272  }
273
274
275  // assign outputs
276  io.numExist := Mux(isFull, (qsize-1).U, tailPtr)
277
278  // Debug sigs
279  XSInfo(
280    io.enq.fire(),
281    p"enq fire: pc:${Hexadecimal(io.enq.bits.cf.pc)} roqIdx:${io.enq.bits.roqIdx} " +
282      p"src1: ${io.enq.bits.psrc1} src2:${io.enq.bits.psrc2} pdst:${io.enq.bits.pdest}\n"
283  )
284  XSInfo(
285    io.deq.fire(),
286    p"deq fire: pc:${Hexadecimal(io.deq.bits.uop.cf.pc)} roqIdx:${io.deq.bits.uop.roqIdx} " +
287      p"src1: ${io.deq.bits.uop.psrc1} data: ${Hexadecimal(io.deq.bits.src1)} " +
288      p"src2: ${io.deq.bits.uop.psrc2} data: ${Hexadecimal(io.deq.bits.src2)} " +
289      p"imm : ${Hexadecimal(io.deq.bits.uop.ctrl.imm)}\npdest: ${io.deq.bits.uop.pdest}\n"
290  )
291  XSDebug(p"tailPtr:$tailPtr tailAfterDeq:$tailAfterRealDeq tlbHit:${io.tlbHit}\n")
292}
293