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.frontend 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage} 22import chisel3.util._ 23import xiangshan._ 24import utils._ 25import chisel3.experimental.chiselName 26 27import scala.math.min 28 29 30trait FTBParams extends HasXSParameter with HasBPUConst { 31 val numEntries = 4096 32 val numWays = 4 33 val numSets = numEntries/numWays // 512 34 val tagSize = 20 35 36 val TAR_STAT_SZ = 2 37 def TAR_FIT = 0.U(TAR_STAT_SZ.W) 38 def TAR_OVF = 1.U(TAR_STAT_SZ.W) 39 def TAR_UDF = 2.U(TAR_STAT_SZ.W) 40 41 def BR_OFFSET_LEN = 12 42 def JMP_OFFSET_LEN = 20 43} 44 45class FTBEntry(implicit p: Parameters) extends XSBundle with FTBParams with BPUUtils { 46 val valid = Bool() 47 48 val brOffset = Vec(numBr, UInt(log2Up(FetchWidth*2).W)) 49 val brLowers = Vec(numBr, UInt(BR_OFFSET_LEN.W)) 50 val brTarStats = Vec(numBr, UInt(TAR_STAT_SZ.W)) 51 val brValids = Vec(numBr, Bool()) 52 53 val jmpOffset = UInt(log2Ceil(PredictWidth).W) 54 val jmpLower = UInt(JMP_OFFSET_LEN.W) 55 val jmpTarStat = UInt(TAR_STAT_SZ.W) 56 val jmpValid = Bool() 57 58 // Partial Fall-Through Address 59 val pftAddr = UInt((log2Up(PredictWidth)+1).W) 60 val carry = Bool() 61 62 val isCall = Bool() 63 val isRet = Bool() 64 val isJalr = Bool() 65 66 val oversize = Bool() 67 68 val last_is_rvc = Bool() 69 70 val always_taken = Vec(numBr, Bool()) 71 72 def getTarget(offsetLen: Int)(pc: UInt, lower: UInt, stat: UInt) = { 73 val higher = pc(VAddrBits-1, offsetLen+1) 74 Cat( 75 Mux(stat === TAR_OVF, higher+1.U, 76 Mux(stat === TAR_UDF, higher-1.U, higher)), 77 lower, 0.U(1.W) 78 ) 79 } 80 val getBrTarget = getTarget(BR_OFFSET_LEN)(_, _, _) 81 82 def getBrTargets(pc: UInt) = { 83 VecInit((brLowers zip brTarStats).map{ 84 case (lower, stat) => getBrTarget(pc, lower, stat) 85 }) 86 } 87 88 def getJmpTarget(pc: UInt) = getTarget(JMP_OFFSET_LEN)(pc, jmpLower, jmpTarStat) 89 90 def getLowerStatByTarget(offsetLen: Int)(pc: UInt, target: UInt) = { 91 val pc_higher = pc(VAddrBits-1, offsetLen+1) 92 val target_higher = target(VAddrBits-1, offsetLen+1) 93 val stat = WireInit(Mux(target_higher > pc_higher, TAR_OVF, 94 Mux(target_higher < pc_higher, TAR_UDF, TAR_FIT))) 95 val lower = WireInit(target(offsetLen, 1)) 96 (lower, stat) 97 } 98 def getBrLowerStatByTarget(pc: UInt, target: UInt) = getLowerStatByTarget(BR_OFFSET_LEN)(pc, target) 99 def getJmpLowerStatByTarget(pc: UInt, target: UInt) = getLowerStatByTarget(JMP_OFFSET_LEN)(pc, target) 100 def setByBrTarget(brIdx: Int, pc: UInt, target: UInt) = { 101 val (lower, stat) = getBrLowerStatByTarget(pc, target) 102 this.brLowers(brIdx) := lower 103 this.brTarStats(brIdx) := stat 104 } 105 def setByJmpTarget(pc: UInt, target: UInt) = { 106 val (lower, stat) = getJmpLowerStatByTarget(pc, target) 107 this.jmpLower := lower 108 this.jmpTarStat := stat 109 } 110 111 def getTargetVec(pc: UInt) = { 112 VecInit(getBrTargets(pc) :+ getJmpTarget(pc)) 113 } 114 115 def getOffsetVec = VecInit(brOffset :+ jmpOffset) 116 def isJal = !isJalr 117 def getFallThrough(pc: UInt) = getFallThroughAddr(pc, carry, pftAddr) 118 def hasBr(offset: UInt) = (brValids zip brOffset).map{ 119 case (v, off) => v && off <= offset 120 }.reduce(_||_) 121 122 def getBrMaskByOffset(offset: UInt) = (brValids zip brOffset).map{ 123 case (v, off) => v && off <= offset 124 } 125 126 def brIsSaved(offset: UInt) = (brValids zip brOffset).map{ 127 case (v, off) => v && off === offset 128 }.reduce(_||_) 129 def display(cond: Bool): Unit = { 130 XSDebug(cond, p"-----------FTB entry----------- \n") 131 XSDebug(cond, p"v=${valid}\n") 132 for(i <- 0 until numBr) { 133 XSDebug(cond, p"[br$i]: v=${brValids(i)}, offset=${brOffset(i)}, lower=${Hexadecimal(brLowers(i))}\n") 134 } 135 XSDebug(cond, p"[jmp]: v=${jmpValid}, offset=${jmpOffset}, lower=${Hexadecimal(jmpLower)}\n") 136 XSDebug(cond, p"pftAddr=${Hexadecimal(pftAddr)}, carry=$carry\n") 137 XSDebug(cond, p"isCall=$isCall, isRet=$isRet, isjalr=$isJalr\n") 138 XSDebug(cond, p"oversize=$oversize, last_is_rvc=$last_is_rvc\n") 139 XSDebug(cond, p"------------------------------- \n") 140 } 141 142} 143 144class FTBEntryWithTag(implicit p: Parameters) extends XSBundle with FTBParams with BPUUtils { 145 val entry = new FTBEntry 146 val tag = UInt(tagSize.W) 147 def display(cond: Bool): Unit = { 148 XSDebug(cond, p"-----------FTB entry----------- \n") 149 XSDebug(cond, p"v=${entry.valid}, tag=${Hexadecimal(tag)}\n") 150 for(i <- 0 until numBr) { 151 XSDebug(cond, p"[br$i]: v=${entry.brValids(i)}, offset=${entry.brOffset(i)}, lower=${Hexadecimal(entry.brLowers(i))}\n") 152 } 153 XSDebug(cond, p"[jmp]: v=${entry.jmpValid}, offset=${entry.jmpOffset}, lower=${Hexadecimal(entry.jmpLower)}\n") 154 XSDebug(cond, p"pftAddr=${Hexadecimal(entry.pftAddr)}, carry=${entry.carry}\n") 155 XSDebug(cond, p"isCall=${entry.isCall}, isRet=${entry.isRet}, isjalr=${entry.isJalr}\n") 156 XSDebug(cond, p"oversize=${entry.oversize}, last_is_rvc=${entry.last_is_rvc}\n") 157 XSDebug(cond, p"------------------------------- \n") 158 } 159} 160 161class FTBMeta(implicit p: Parameters) extends XSBundle with FTBParams { 162 val writeWay = UInt(numWays.W) 163 val hit = Bool() 164 val pred_cycle = UInt(64.W) // TODO: Use Option 165} 166 167object FTBMeta { 168 def apply(writeWay: UInt, hit: Bool, pred_cycle: UInt)(implicit p: Parameters): FTBMeta = { 169 val e = Wire(new FTBMeta) 170 e.writeWay := writeWay 171 e.hit := hit 172 e.pred_cycle := pred_cycle 173 e 174 } 175} 176 177class FTB(implicit p: Parameters) extends BasePredictor with FTBParams with BPUUtils { 178 override val meta_size = WireInit(0.U.asTypeOf(new FTBMeta)).getWidth 179 180 val ftbAddr = new TableAddr(log2Up(numSets), 1) 181 182 class FTBBank(val numSets: Int, val nWays: Int) extends XSModule with BPUUtils { 183 val io = IO(new Bundle { 184 val req_pc = Flipped(DecoupledIO(UInt(VAddrBits.W))) 185 val read_resp = Output(new FTBEntry) 186 187 // when ftb hit, read_hits.valid is true, and read_hits.bits is OH of hit way 188 // when ftb not hit, read_hits.valid is false, and read_hits is OH of allocWay 189 val read_hits = Valid(Vec(numWays, Bool())) 190 191 val update_pc = Input(UInt(VAddrBits.W)) 192 val update_write_data = Flipped(Valid(new FTBEntryWithTag)) 193 val update_write_mask = Input(UInt(numWays.W)) 194 }) 195 196 val ftb = Module(new SRAMTemplate(new FTBEntryWithTag, set = numSets, way = numWays, shouldReset = true, holdRead = true, singlePort = true)) 197 198 ftb.io.r.req.valid := io.req_pc.valid // io.s0_fire 199 ftb.io.r.req.bits.setIdx := ftbAddr.getIdx(io.req_pc.bits) // s0_idx 200 201 io.req_pc.ready := ftb.io.r.req.ready 202 203 val req_tag = RegEnable(ftbAddr.getTag(io.req_pc.bits)(tagSize-1, 0), io.req_pc.valid) 204 205 val read_entries = ftb.io.r.resp.data.map(_.entry) 206 val read_tags = ftb.io.r.resp.data.map(_.tag) 207 208 val total_hits = VecInit((0 until numWays).map(b => read_tags(b) === req_tag && read_entries(b).valid)) 209 val hit = total_hits.reduce(_||_) 210 val hit_way_1h = VecInit(PriorityEncoderOH(total_hits)) 211 212 def allocWay(valids: UInt, meta_tags: UInt, req_tag: UInt) = { 213 val randomAlloc = false 214 if (numWays > 1) { 215 val w = Wire(UInt(log2Up(numWays).W)) 216 val valid = WireInit(valids.andR) 217 val tags = Cat(meta_tags, req_tag) 218 val l = log2Up(numWays) 219 val nChunks = (tags.getWidth + l - 1) / l 220 val chunks = (0 until nChunks).map( i => 221 tags(min((i+1)*l, tags.getWidth)-1, i*l) 222 ) 223 w := Mux(valid, if (randomAlloc) {LFSR64()(log2Up(numWays)-1,0)} else {chunks.reduce(_^_)}, PriorityEncoder(~valids)) 224 w 225 } else { 226 val w = WireInit(0.U) 227 w 228 } 229 } 230 231 val allocWriteWay = allocWay( 232 VecInit(read_entries.map(_.valid)).asUInt, 233 VecInit(read_tags).asUInt, 234 req_tag 235 ) 236 237 io.read_resp := PriorityMux(total_hits, read_entries) // Mux1H 238 io.read_hits.valid := hit 239 io.read_hits.bits := Mux(hit, hit_way_1h, VecInit(UIntToOH(allocWriteWay).asBools())) 240 241 // Update logic 242 val u_valid = io.update_write_data.valid 243 val u_data = io.update_write_data.bits 244 val u_idx = ftbAddr.getIdx(io.update_pc) 245 val u_mask = io.update_write_mask 246 247 ftb.io.w.apply(u_valid, u_data, u_idx, u_mask) 248 } // FTBBank 249 250 val ftbBank = Module(new FTBBank(numSets, numWays)) 251 252 ftbBank.io.req_pc.valid := io.s0_fire 253 ftbBank.io.req_pc.bits := s0_pc 254 255 io.s1_ready := ftbBank.io.req_pc.ready // && !io.redirect.valid 256 257 val ftb_entry = RegEnable(ftbBank.io.read_resp, io.s1_fire) 258 val s1_hit = ftbBank.io.read_hits.valid 259 val s2_hit = RegEnable(s1_hit, io.s1_fire) 260 val writeWay = ftbBank.io.read_hits.bits 261 262 val fallThruAddr = getFallThroughAddr(s2_pc, ftb_entry.carry, ftb_entry.pftAddr) 263 264 // io.out.bits.resp := RegEnable(io.in.bits.resp_in(0), 0.U.asTypeOf(new BranchPredictionResp), io.s1_fire) 265 io.out.resp := io.in.bits.resp_in(0) 266 267 val s1_latch_call_is_rvc = DontCare // TODO: modify when add RAS 268 269 io.out.resp.s2.preds.hit := s2_hit 270 io.out.resp.s2.pc := s2_pc 271 io.out.resp.s2.ftb_entry := ftb_entry 272 io.out.resp.s2.preds.fromFtbEntry(ftb_entry, s2_pc) 273 274 io.out.s3_meta := RegEnable(RegEnable(FTBMeta(writeWay.asUInt(), s1_hit, GTimer()).asUInt(), io.s1_fire), io.s2_fire) 275 276 when(s2_hit) { 277 io.out.resp.s2.ftb_entry.pftAddr := ftb_entry.pftAddr 278 io.out.resp.s2.ftb_entry.carry := ftb_entry.carry 279 }.otherwise { 280 io.out.resp.s2.ftb_entry.pftAddr := s2_pc(instOffsetBits + log2Ceil(PredictWidth), instOffsetBits) ^ (1 << log2Ceil(PredictWidth)).U 281 io.out.resp.s2.ftb_entry.carry := s2_pc(instOffsetBits + log2Ceil(PredictWidth)).asBool 282 io.out.resp.s2.ftb_entry.oversize := false.B 283 } 284 285 // always taken logic 286 when (s2_hit) { 287 for (i <- 0 until numBr) { 288 when (ftb_entry.always_taken(i)) { 289 io.out.resp.s2.preds.taken_mask(i) := true.B 290 } 291 } 292 } 293 294 // Update logic 295 val has_update = RegInit(VecInit(Seq.fill(64)(0.U(VAddrBits.W)))) 296 val has_update_ptr = RegInit(0.U(log2Up(64))) 297 298 val update = RegNext(io.update.bits) 299 300 val u_meta = update.meta.asTypeOf(new FTBMeta) 301 val u_valid = RegNext(io.update.valid && !io.update.bits.old_entry) 302 val u_way_mask = u_meta.writeWay 303 304 val ftb_write = Wire(new FTBEntryWithTag) 305 ftb_write.entry := update.ftb_entry 306 ftb_write.tag := ftbAddr.getTag(update.pc)(tagSize-1, 0) 307 308 ftbBank.io.update_write_data.valid := u_valid 309 ftbBank.io.update_write_data.bits := ftb_write 310 ftbBank.io.update_pc := update.pc 311 ftbBank.io.update_write_mask := u_way_mask 312 313 val r_updated = (0 until 64).map(i => has_update(i) === s1_pc).reduce(_||_) 314 val u_updated = (0 until 64).map(i => has_update(i) === update.pc).reduce(_||_) 315 316 when(u_valid) { 317 when(!u_updated) { has_update(has_update_ptr) := update.pc } 318 319 has_update_ptr := has_update_ptr + !u_updated 320 } 321 322 XSDebug("req_v=%b, req_pc=%x, ready=%b (resp at next cycle)\n", io.s0_fire, s0_pc, ftbBank.io.req_pc.ready) 323 XSDebug("s2_hit=%b, hit_way=%b\n", s2_hit, writeWay.asUInt) 324 XSDebug("s2_taken_mask=%b, s2_real_taken_mask=%b\n", 325 io.in.bits.resp_in(0).s2.preds.taken_mask.asUInt, io.out.resp.s2.real_taken_mask().asUInt) 326 XSDebug("s2_target=%x\n", io.out.resp.s2.target) 327 328 ftb_entry.display(true.B) 329 330 XSDebug(u_valid, "Update from ftq\n") 331 XSDebug(u_valid, "update_pc=%x, tag=%x, update_write_way=%b, pred_cycle=%d\n", 332 update.pc, ftbAddr.getTag(update.pc), u_way_mask, u_meta.pred_cycle) 333 334 335 336 337 338 XSPerfAccumulate("ftb_first_miss", u_valid && !u_updated && !update.preds.hit) 339 XSPerfAccumulate("ftb_updated_miss", u_valid && u_updated && !update.preds.hit) 340 341 XSPerfAccumulate("ftb_read_first_miss", RegNext(io.s0_fire) && !s1_hit && !r_updated) 342 XSPerfAccumulate("ftb_read_updated_miss", RegNext(io.s0_fire) && !s1_hit && r_updated) 343 344 XSPerfAccumulate("ftb_read_hits", RegNext(io.s0_fire) && s1_hit) 345 XSPerfAccumulate("ftb_read_misses", RegNext(io.s0_fire) && !s1_hit) 346 347 XSPerfAccumulate("ftb_commit_hits", u_valid && update.preds.hit) 348 XSPerfAccumulate("ftb_commit_misses", u_valid && !update.preds.hit) 349 350 XSPerfAccumulate("ftb_update_req", io.update.valid) 351 XSPerfAccumulate("ftb_update_ignored", io.update.valid && io.update.bits.old_entry) 352 XSPerfAccumulate("ftb_updated", u_valid) 353} 354