1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.backend.rename 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import utility._ 25import xiangshan.backend.Bundles._ 26import xiangshan.backend.datapath.WbConfig.{IntWB, VfWB, NoWB, PregWB} 27import xiangshan.backend.issue.SchdBlockParams 28import xiangshan.backend.datapath.{DataSource} 29 30object RegStatus { 31 val busy = "b11".U 32 val bypass = "b10".U 33 val regFile = "b00".U 34 35 def apply() = UInt(2.W) 36} 37 38class BusyTableReadIO(implicit p: Parameters) extends XSBundle { 39 val req = Input(UInt(PhyRegIdxWidth.W)) 40 val resp = Output(Bool()) 41 val dataSource = Output(DataSource()) 42 val l1ExuOH = Output(ExuOH()) 43} 44 45class BusyTable(numReadPorts: Int, numWritePorts: Int, numPhyPregs: Int, pregWB: PregWB)(implicit p: Parameters, params: SchdBlockParams) extends XSModule with HasPerfEvents { 46 val io = IO(new Bundle() { 47 // set preg state to busy 48 val allocPregs = Vec(RenameWidth, Flipped(ValidIO(UInt(PhyRegIdxWidth.W)))) 49 // set preg state to ready (write back regfile + rob walk) 50 val wbPregs = Vec(numWritePorts, Flipped(ValidIO(UInt(PhyRegIdxWidth.W)))) 51 // fast wakeup 52 val wakeUp: MixedVec[ValidIO[IssueQueueIQWakeUpBundle]] = Flipped(params.genIQWakeUpInValidBundle) 53 // cancelFromDatapath 54 val cancel = Vec(backendParams.numExu, Flipped(ValidIO(new CancelSignal))) 55 // read preg state 56 val read = Vec(numReadPorts, new BusyTableReadIO) 57 }) 58 59 val wakeUpReg = Reg(params.genIQWakeUpInValidBundle) 60 val table = RegInit(VecInit(Seq.fill(numPhyPregs)(0.U(2.W)))) 61 val tableUpdate = Wire(Vec(numPhyPregs, RegStatus())) 62 val wakeUpFilterLS = io.wakeUp.filter(x => 63 Seq("LDU0", "LDU1", "HYU0").map(x.bits.exuIdx != backendParams.getExuIdx(_)).reduce(_ && _) 64 ) // TODO 65 66 def reqVecToMask(rVec: Vec[Valid[UInt]]): UInt = { 67 ParallelOR(rVec.map(v => Mux(v.valid, UIntToOH(v.bits), 0.U))) 68 } 69 70 val wbMask = reqVecToMask(io.wbPregs) 71 val allocMask = reqVecToMask(io.allocPregs) 72 val wakeUpMask = pregWB match { 73 case _: IntWB => wakeUpFilterLS.map(x => Mux(x.valid && x.bits.rfWen && !x.bits.loadDependency.asUInt.orR, UIntToOH(x.bits.pdest), 0.U)).toSeq.fold(0.U)(_ | _) //TODO: dont implement "load -> wakeUp other -> wakeUp BusyTable" now 74 case _: VfWB => wakeUpFilterLS.map(x => Mux(x.valid && (x.bits.fpWen || x.bits.vecWen) && !x.bits.loadDependency.asUInt.orR, UIntToOH(x.bits.pdest), 0.U)).toSeq.fold(0.U)(_ | _) 75 case _: NoWB => throw new IllegalArgumentException("NoWB is not permitted") 76 } 77 val cancelMask = pregWB match { 78 case _: IntWB => io.cancel.map(x => Mux(x.valid && x.bits.rfWen, UIntToOH(x.bits.pdest), 0.U)).fold(0.U)(_ | _) 79 case _: VfWB => io.cancel.map(x => Mux(x.valid && (x.bits.fpWen || x.bits.vecWen), UIntToOH(x.bits.pdest), 0.U)).fold(0.U)(_ | _) 80 case _: NoWB => throw new IllegalArgumentException("NoWB is not permitted") 81 } 82 83 /* 84 we can ensure that the following conditions are mutually exclusive 85 wakeUp and cancel (same pdest) would not arrive at the same cycle 86 for a pdest: 87 rename alloc => wakeUp => cancel => ... => wakeUp => cancel => wakeUp 88 or 89 rename alloc => wbMask //TODO we still need wbMask because wakeUp signal is partial now 90 the bypass state lasts for a maximum of one cycle, cancel(=> busy) or else(=> regFile) 91 */ 92 tableUpdate.zipWithIndex.foreach{ case (update, idx) => 93 when(allocMask(idx)) { 94 update := RegStatus.busy 95 }.elsewhen(cancelMask(idx)) { 96 update := RegStatus.busy 97 }.elsewhen(wakeUpMask(idx)) { 98 update := RegStatus.bypass 99 }.elsewhen((table(idx) === RegStatus.bypass) || wbMask(idx)) { 100 update := RegStatus.regFile 101 }.otherwise { 102 update := table(idx) 103 } 104 } 105 106 io.read.foreach{ case res => 107 res.resp := !table(res.req).andR 108 res.dataSource.value := DataSource.reg 109 val wakeUpExuOHVec = wakeUpReg.toSeq.map{ case x => 110 val v: Bool = pregWB match { 111 case _: IntWB => x.valid && x.bits.rfWen 112 case _: VfWB => x.valid && (x.bits.fpWen || x.bits.vecWen) 113 case _: NoWB => throw new IllegalArgumentException("NoWB is not permitted") 114 } 115 val pdestHit = res.req === x.bits.pdest 116 val isBypass = table(res.req) === DataSource.bypass 117 Mux(v && pdestHit && isBypass, MathUtils.IntToOH(x.bits.exuIdx).U(backendParams.numExu.W), 0.U) 118 } 119 res.l1ExuOH := Mux(table(res.req) === DataSource.bypass, ParallelOR(wakeUpExuOHVec), 0.U) 120 } 121 122 table := tableUpdate 123 wakeUpReg := io.wakeUp 124 125 val oddTable = table.zipWithIndex.filter(_._2 % 2 == 1).map(!_._1.orR) 126 val evenTable = table.zipWithIndex.filter(_._2 % 2 == 0).map(!_._1.orR) 127 val busyCount = RegNext(RegNext(PopCount(oddTable)) + RegNext(PopCount(evenTable))) 128 129 XSPerfAccumulate("busy_count", PopCount(table.map(_.andR))) 130 131 val perfEvents = Seq( 132 ("std_freelist_1_4_valid", busyCount < (numPhyPregs / 4).U ), 133 ("std_freelist_2_4_valid", busyCount > (numPhyPregs / 4).U && busyCount <= (numPhyPregs / 2).U ), 134 ("std_freelist_3_4_valid", busyCount > (numPhyPregs / 2).U && busyCount <= (numPhyPregs * 3 / 4).U), 135 ("std_freelist_4_4_valid", busyCount > (numPhyPregs * 3 / 4).U ) 136 ) 137 generatePerfEvent() 138} 139