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.icache 18 19import chisel3._ 20import chisel3.util._ 21import difftest._ 22import freechips.rocketchip.tilelink.ClientStates 23import org.chipsalliance.cde.config.Parameters 24import utility._ 25import utils._ 26import xiangshan._ 27import xiangshan.backend.fu.PMPReqBundle 28import xiangshan.backend.fu.PMPRespBundle 29import xiangshan.cache.mmu._ 30import xiangshan.frontend.ExceptionType 31import xiangshan.frontend.FtqICacheInfo 32import xiangshan.frontend.FtqToICacheRequestBundle 33 34class ICacheMainPipeReq(implicit p: Parameters) extends ICacheBundle { 35 val vaddr = UInt(VAddrBits.W) 36 def vSetIdx = get_idx(vaddr) 37} 38 39class ICacheMainPipeResp(implicit p: Parameters) extends ICacheBundle { 40 val vaddr = UInt(VAddrBits.W) 41 val data = UInt(blockBits.W) 42 val paddr = UInt(PAddrBits.W) 43 val exception = UInt(ExceptionType.width.W) 44 val pmp_mmio = Bool() 45 val itlb_pbmt = UInt(Pbmt.width.W) 46 val backendException = Bool() 47 /* NOTE: GPAddrBits(=50bit) is not enough for gpaddr here, refer to PR#3795 48 * Sv48*4 only allows 50bit gpaddr, when software violates this requirement 49 * it needs to fill the mtval2 register with the full XLEN(=64bit) gpaddr, 50 * PAddrBitsMax(=56bit currently) is required for the frontend datapath due to the itlb ppn length limitation 51 * (cases 56<x<=64 are handled by the backend datapath) 52 */ 53 val gpaddr = UInt(PAddrBitsMax.W) 54 val isForVSnonLeafPTE = Bool() 55} 56 57class ICacheMainPipeBundle(implicit p: Parameters) extends ICacheBundle { 58 val req = Flipped(Decoupled(new FtqToICacheRequestBundle)) 59 val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp)) 60 val topdownIcacheMiss = Output(Bool()) 61 val topdownItlbMiss = Output(Bool()) 62} 63 64class ICacheMetaReqBundle(implicit p: Parameters) extends ICacheBundle { 65 val toIMeta = DecoupledIO(new ICacheReadBundle) 66 val fromIMeta = Input(new ICacheMetaRespBundle) 67} 68 69class ICacheDataReqBundle(implicit p: Parameters) extends ICacheBundle { 70 val toIData = Vec(partWayNum, DecoupledIO(new ICacheReadBundle)) 71 val fromIData = Input(new ICacheDataRespBundle) 72} 73 74class ICacheMSHRBundle(implicit p: Parameters) extends ICacheBundle { 75 val req = Decoupled(new ICacheMissReq) 76 val resp = Flipped(ValidIO(new ICacheMissResp)) 77} 78 79class ICachePMPBundle(implicit p: Parameters) extends ICacheBundle { 80 val req = Valid(new PMPReqBundle()) 81 val resp = Input(new PMPRespBundle()) 82} 83 84class ICachePerfInfo(implicit p: Parameters) extends ICacheBundle { 85 val only_0_hit = Bool() 86 val only_0_miss = Bool() 87 val hit_0_hit_1 = Bool() 88 val hit_0_miss_1 = Bool() 89 val miss_0_hit_1 = Bool() 90 val miss_0_miss_1 = Bool() 91 val hit_0_except_1 = Bool() 92 val miss_0_except_1 = Bool() 93 val except_0 = Bool() 94 val bank_hit = Vec(2, Bool()) 95 val hit = Bool() 96} 97 98class ICacheMainPipeInterface(implicit p: Parameters) extends ICacheBundle { 99 val hartId = Input(UInt(hartIdLen.W)) 100 101 /*** internal interface ***/ 102 val dataArray = new ICacheDataReqBundle 103 104 /** prefetch io */ 105 val touch = Vec(PortNumber, ValidIO(new ReplacerTouch)) 106 val wayLookupRead = Flipped(DecoupledIO(new WayLookupInfo)) 107 108 val mshr = new ICacheMSHRBundle 109 val errors = Output(Vec(PortNumber, ValidIO(new L1CacheErrorInfo))) 110 111 /*** outside interface ***/ 112 // val fetch = Vec(PortNumber, new ICacheMainPipeBundle) 113 /* when ftq.valid is high in T + 1 cycle 114 * the ftq component must be valid in T cycle 115 */ 116 val fetch = new ICacheMainPipeBundle 117 val pmp = Vec(PortNumber, new ICachePMPBundle) 118 val respStall = Input(Bool()) 119 120 val csr_parity_enable = Input(Bool()) 121 val flush = Input(Bool()) 122 123 val perfInfo = Output(new ICachePerfInfo) 124} 125 126class ICacheDB(implicit p: Parameters) extends ICacheBundle { 127 val blk_vaddr = UInt((VAddrBits - blockOffBits).W) 128 val blk_paddr = UInt((PAddrBits - blockOffBits).W) 129 val hit = Bool() 130} 131 132class ICacheMainPipe(implicit p: Parameters) extends ICacheModule { 133 val io = IO(new ICacheMainPipeInterface) 134 135 /** Input/Output port */ 136 val (fromFtq, toIFU) = (io.fetch.req, io.fetch.resp) 137 val (toData, fromData) = (io.dataArray.toIData, io.dataArray.fromIData) 138 val (toMSHR, fromMSHR) = (io.mshr.req, io.mshr.resp) 139 val (toPMP, fromPMP) = (io.pmp.map(_.req), io.pmp.map(_.resp)) 140 val fromWayLookup = io.wayLookupRead 141 142 // Statistics on the frequency distribution of FTQ fire interval 143 val cntFtqFireInterval = RegInit(0.U(32.W)) 144 cntFtqFireInterval := Mux(fromFtq.fire, 1.U, cntFtqFireInterval + 1.U) 145 XSPerfHistogram("ftq2icache_fire", cntFtqFireInterval, fromFtq.fire, 1, 300, 1, right_strict = true) 146 147 /** pipeline control signal */ 148 val s1_ready, s2_ready = Wire(Bool()) 149 val s0_fire, s1_fire, s2_fire = Wire(Bool()) 150 val s0_flush, s1_flush, s2_flush = Wire(Bool()) 151 152 /** 153 ****************************************************************************** 154 * ICache Stage 0 155 * - send req to data SRAM 156 * - get waymask and tlb info from wayLookup 157 ****************************************************************************** 158 */ 159 160 /** s0 control */ 161 // 0,1,2,3 -> dataArray(data); 4 -> mainPipe 162 // Ftq RegNext Register 163 val fromFtqReq = fromFtq.bits.pcMemRead 164 val s0_valid = fromFtq.valid 165 val s0_req_valid_all = (0 until partWayNum + 1).map(i => fromFtq.bits.readValid(i)) 166 val s0_req_vaddr_all = 167 (0 until partWayNum + 1).map(i => VecInit(Seq(fromFtqReq(i).startAddr, fromFtqReq(i).nextlineStart))) 168 val s0_req_vSetIdx_all = (0 until partWayNum + 1).map(i => VecInit(s0_req_vaddr_all(i).map(get_idx))) 169 val s0_req_offset_all = (0 until partWayNum + 1).map(i => s0_req_vaddr_all(i)(0)(log2Ceil(blockBytes) - 1, 0)) 170 val s0_doubleline_all = (0 until partWayNum + 1).map(i => fromFtq.bits.readValid(i) && fromFtqReq(i).crossCacheline) 171 172 val s0_req_vaddr = s0_req_vaddr_all.last 173 val s0_req_vSetIdx = s0_req_vSetIdx_all.last 174 val s0_doubleline = s0_doubleline_all.last 175 176 val s0_backendException = fromFtq.bits.backendException 177 178 /** 179 ****************************************************************************** 180 * get waymask and tlb info from wayLookup 181 ****************************************************************************** 182 */ 183 fromWayLookup.ready := s0_fire 184 val s0_waymasks = VecInit(fromWayLookup.bits.waymask.map(_.asTypeOf(Vec(nWays, Bool())))) 185 val s0_req_ptags = fromWayLookup.bits.ptag 186 val s0_req_gpaddr = fromWayLookup.bits.gpaddr 187 val s0_req_isForVSnonLeafPTE = fromWayLookup.bits.isForVSnonLeafPTE 188 val s0_itlb_exception = fromWayLookup.bits.itlb_exception 189 val s0_itlb_pbmt = fromWayLookup.bits.itlb_pbmt 190 val s0_meta_codes = fromWayLookup.bits.meta_codes 191 val s0_hits = VecInit(fromWayLookup.bits.waymask.map(_.orR)) 192 193 when(s0_fire) { 194 assert( 195 (0 until PortNumber).map(i => s0_req_vSetIdx(i) === fromWayLookup.bits.vSetIdx(i)).reduce(_ && _), 196 "vSetIdxs from ftq and wayLookup are different! vaddr0=0x%x ftq: vidx0=0x%x vidx1=0x%x wayLookup: vidx0=0x%x vidx1=0x%x", 197 s0_req_vaddr(0), 198 s0_req_vSetIdx(0), 199 s0_req_vSetIdx(1), 200 fromWayLookup.bits.vSetIdx(0), 201 fromWayLookup.bits.vSetIdx(1) 202 ) 203 } 204 205 /** 206 ****************************************************************************** 207 * data SRAM request 208 ****************************************************************************** 209 */ 210 for (i <- 0 until partWayNum) { 211 toData(i).valid := s0_req_valid_all(i) 212 toData(i).bits.isDoubleLine := s0_doubleline_all(i) 213 toData(i).bits.vSetIdx := s0_req_vSetIdx_all(i) 214 toData(i).bits.blkOffset := s0_req_offset_all(i) 215 toData(i).bits.wayMask := s0_waymasks 216 } 217 218 val s0_can_go = toData.last.ready && fromWayLookup.valid && s1_ready 219 s0_flush := io.flush 220 s0_fire := s0_valid && s0_can_go && !s0_flush 221 222 fromFtq.ready := s0_can_go 223 224 /** 225 ****************************************************************************** 226 * ICache Stage 1 227 * - PMP check 228 * - get Data SRAM read responses (latched for pipeline stop) 229 * - monitor missUint response port 230 ****************************************************************************** 231 */ 232 val s1_valid = generatePipeControl(lastFire = s0_fire, thisFire = s1_fire, thisFlush = s1_flush, lastFlush = false.B) 233 234 val s1_req_vaddr = RegEnable(s0_req_vaddr, 0.U.asTypeOf(s0_req_vaddr), s0_fire) 235 val s1_req_ptags = RegEnable(s0_req_ptags, 0.U.asTypeOf(s0_req_ptags), s0_fire) 236 val s1_req_gpaddr = RegEnable(s0_req_gpaddr, 0.U.asTypeOf(s0_req_gpaddr), s0_fire) 237 val s1_req_isForVSnonLeafPTE = RegEnable(s0_req_isForVSnonLeafPTE, 0.U.asTypeOf(s0_req_isForVSnonLeafPTE), s0_fire) 238 val s1_doubleline = RegEnable(s0_doubleline, 0.U.asTypeOf(s0_doubleline), s0_fire) 239 val s1_SRAMhits = RegEnable(s0_hits, 0.U.asTypeOf(s0_hits), s0_fire) 240 val s1_itlb_exception = RegEnable(s0_itlb_exception, 0.U.asTypeOf(s0_itlb_exception), s0_fire) 241 val s1_backendException = RegEnable(s0_backendException, false.B, s0_fire) 242 val s1_itlb_pbmt = RegEnable(s0_itlb_pbmt, 0.U.asTypeOf(s0_itlb_pbmt), s0_fire) 243 val s1_waymasks = RegEnable(s0_waymasks, 0.U.asTypeOf(s0_waymasks), s0_fire) 244 val s1_meta_codes = RegEnable(s0_meta_codes, 0.U.asTypeOf(s0_meta_codes), s0_fire) 245 246 val s1_req_vSetIdx = s1_req_vaddr.map(get_idx) 247 val s1_req_paddr = s1_req_vaddr.zip(s1_req_ptags).map { case (vaddr, ptag) => get_paddr_from_ptag(vaddr, ptag) } 248 val s1_req_offset = s1_req_vaddr(0)(log2Ceil(blockBytes) - 1, 0) 249 250 // do metaArray ECC check 251 val s1_meta_corrupt = VecInit((s1_req_ptags zip s1_meta_codes zip s1_waymasks).map { case ((meta, code), waymask) => 252 val hit_num = PopCount(waymask) 253 // NOTE: if not hit, encodeMetaECC(meta) =/= code can also be true, but we don't care about it 254 (encodeMetaECC(meta) =/= code && hit_num === 1.U) || // hit one way, but parity code does not match, ECC failure 255 hit_num > 1.U // hit multi way, must be a ECC failure 256 }) 257 258 /** 259 ****************************************************************************** 260 * update replacement status register 261 ****************************************************************************** 262 */ 263 (0 until PortNumber).foreach { i => 264 io.touch(i).bits.vSetIdx := s1_req_vSetIdx(i) 265 io.touch(i).bits.way := OHToUInt(s1_waymasks(i)) 266 } 267 io.touch(0).valid := RegNext(s0_fire) && s1_SRAMhits(0) 268 io.touch(1).valid := RegNext(s0_fire) && s1_SRAMhits(1) && s1_doubleline 269 270 /** 271 ****************************************************************************** 272 * PMP check 273 ****************************************************************************** 274 */ 275 toPMP.zipWithIndex.foreach { case (p, i) => 276 // if itlb has exception, paddr can be invalid, therefore pmp check can be skipped 277 p.valid := s1_valid // && !ExceptionType.hasException(s1_itlb_exception(i)) 278 p.bits.addr := s1_req_paddr(i) 279 p.bits.size := 3.U // TODO 280 p.bits.cmd := TlbCmd.exec 281 } 282 val s1_pmp_exception = VecInit(fromPMP.map(ExceptionType.fromPMPResp)) 283 val s1_pmp_mmio = VecInit(fromPMP.map(_.mmio)) 284 285 // also raise af when meta array corrupt is detected, to cancel fetch 286 val s1_meta_exception = VecInit(s1_meta_corrupt.map(ExceptionType.fromECC(io.csr_parity_enable, _))) 287 288 // merge s1 itlb/pmp/meta exceptions, itlb has the highest priority, pmp next, meta lowest 289 val s1_exception_out = ExceptionType.merge( 290 s1_itlb_exception, 291 s1_pmp_exception, 292 s1_meta_exception 293 ) 294 295 // DO NOT merge pmp mmio and itlb pbmt here, we need them to be passed to IFU separately 296 297 /** 298 ****************************************************************************** 299 * select data from MSHR, SRAM 300 ****************************************************************************** 301 */ 302 val s1_MSHR_match = VecInit((0 until PortNumber).map(i => 303 (s1_req_vSetIdx(i) === fromMSHR.bits.vSetIdx) && 304 (s1_req_ptags(i) === getPhyTagFromBlk(fromMSHR.bits.blkPaddr)) && 305 fromMSHR.valid && !fromMSHR.bits.corrupt 306 )) 307 val s1_MSHR_hits = Seq(s1_valid && s1_MSHR_match(0), s1_valid && (s1_MSHR_match(1) && s1_doubleline)) 308 val s1_MSHR_datas = fromMSHR.bits.data.asTypeOf(Vec(ICacheDataBanks, UInt((blockBits / ICacheDataBanks).W))) 309 310 val s1_hits = (0 until PortNumber).map(i => 311 ValidHoldBypass(s1_MSHR_hits(i) || (RegNext(s0_fire) && s1_SRAMhits(i)), s1_fire || s1_flush) 312 ) 313 314 val s1_bankIdxLow = s1_req_offset >> log2Ceil(blockBytes / ICacheDataBanks) 315 val s1_bankMSHRHit = VecInit((0 until ICacheDataBanks).map(i => 316 (i.U >= s1_bankIdxLow) && s1_MSHR_hits(0) || 317 (i.U < s1_bankIdxLow) && s1_MSHR_hits(1) 318 )) 319 val s1_datas = VecInit((0 until ICacheDataBanks).map(i => 320 DataHoldBypass(Mux(s1_bankMSHRHit(i), s1_MSHR_datas(i), fromData.datas(i)), s1_bankMSHRHit(i) || RegNext(s0_fire)) 321 )) 322 val s1_codes = DataHoldBypass(fromData.codes, RegNext(s0_fire)) 323 324 s1_flush := io.flush 325 s1_ready := s2_ready || !s1_valid 326 s1_fire := s1_valid && s2_ready && !s1_flush 327 328 /** 329 ****************************************************************************** 330 * ICache Stage 2 331 * - send request to MSHR if ICache miss 332 * - monitor missUint response port 333 * - response to IFU 334 ****************************************************************************** 335 */ 336 337 val s2_valid = generatePipeControl(lastFire = s1_fire, thisFire = s2_fire, thisFlush = s2_flush, lastFlush = false.B) 338 339 val s2_req_vaddr = RegEnable(s1_req_vaddr, 0.U.asTypeOf(s1_req_vaddr), s1_fire) 340 val s2_req_ptags = RegEnable(s1_req_ptags, 0.U.asTypeOf(s1_req_ptags), s1_fire) 341 val s2_req_gpaddr = RegEnable(s1_req_gpaddr, 0.U.asTypeOf(s1_req_gpaddr), s1_fire) 342 val s2_req_isForVSnonLeafPTE = RegEnable(s1_req_isForVSnonLeafPTE, 0.U.asTypeOf(s1_req_isForVSnonLeafPTE), s1_fire) 343 val s2_doubleline = RegEnable(s1_doubleline, 0.U.asTypeOf(s1_doubleline), s1_fire) 344 val s2_exception = 345 RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_fire) // includes itlb/pmp/meta exception 346 val s2_backendException = RegEnable(s1_backendException, false.B, s1_fire) 347 val s2_pmp_mmio = RegEnable(s1_pmp_mmio, 0.U.asTypeOf(s1_pmp_mmio), s1_fire) 348 val s2_itlb_pbmt = RegEnable(s1_itlb_pbmt, 0.U.asTypeOf(s1_itlb_pbmt), s1_fire) 349 350 val s2_req_vSetIdx = s2_req_vaddr.map(get_idx) 351 val s2_req_offset = s2_req_vaddr(0)(log2Ceil(blockBytes) - 1, 0) 352 val s2_req_paddr = s2_req_vaddr.zip(s2_req_ptags).map { case (vaddr, ptag) => get_paddr_from_ptag(vaddr, ptag) } 353 354 val s2_SRAMhits = RegEnable(s1_SRAMhits, 0.U.asTypeOf(s1_SRAMhits), s1_fire) 355 val s2_codes = RegEnable(s1_codes, 0.U.asTypeOf(s1_codes), s1_fire) 356 val s2_hits = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 357 val s2_datas = RegInit(VecInit(Seq.fill(ICacheDataBanks)(0.U((blockBits / ICacheDataBanks).W)))) 358 359 /** 360 ****************************************************************************** 361 * report data parity error 362 ****************************************************************************** 363 */ 364 // check data error 365 val s2_bankSel = getBankSel(s2_req_offset, s2_valid) 366 val s2_bank_corrupt = (0 until ICacheDataBanks).map(i => encodeDataECC(s2_datas(i)) =/= s2_codes(i)) 367 val s2_data_corrupt = (0 until PortNumber).map(port => 368 (0 until ICacheDataBanks).map(bank => 369 s2_bank_corrupt(bank) && s2_bankSel(port)(bank).asBool 370 ).reduce(_ || _) && s2_SRAMhits(port) 371 ) 372 // meta error is checked in prefetch pipeline 373 val s2_meta_corrupt = RegEnable(s1_meta_corrupt, 0.U.asTypeOf(s1_meta_corrupt), s1_fire) 374 // send errors to top 375 (0 until PortNumber).map { i => 376 io.errors(i).valid := io.csr_parity_enable && RegNext(s1_fire) && (s2_meta_corrupt(i) || s2_data_corrupt(i)) 377 io.errors(i).bits.report_to_beu := io.csr_parity_enable && RegNext(s1_fire) && (s2_meta_corrupt( 378 i 379 ) || s2_data_corrupt(i)) 380 io.errors(i).bits.paddr := s2_req_paddr(i) 381 io.errors(i).bits.source := DontCare 382 io.errors(i).bits.source.tag := s2_meta_corrupt(i) 383 io.errors(i).bits.source.data := s2_data_corrupt(i) 384 io.errors(i).bits.source.l2 := false.B 385 io.errors(i).bits.opType := DontCare 386 io.errors(i).bits.opType.fetch := true.B 387 } 388 389 /** 390 ****************************************************************************** 391 * monitor missUint response port 392 ****************************************************************************** 393 */ 394 val s2_MSHR_match = VecInit((0 until PortNumber).map(i => 395 (s2_req_vSetIdx(i) === fromMSHR.bits.vSetIdx) && 396 (s2_req_ptags(i) === getPhyTagFromBlk(fromMSHR.bits.blkPaddr)) && 397 fromMSHR.valid // we don't care about whether it's corrupt here 398 )) 399 val s2_MSHR_hits = Seq(s2_valid && s2_MSHR_match(0), s2_valid && s2_MSHR_match(1) && s2_doubleline) 400 val s2_MSHR_datas = fromMSHR.bits.data.asTypeOf(Vec(ICacheDataBanks, UInt((blockBits / ICacheDataBanks).W))) 401 402 val s2_bankIdxLow = s2_req_offset >> log2Ceil(blockBytes / ICacheDataBanks) 403 val s2_bankMSHRHit = VecInit((0 until ICacheDataBanks).map(i => 404 ((i.U >= s2_bankIdxLow) && s2_MSHR_hits(0)) || ((i.U < s2_bankIdxLow) && s2_MSHR_hits(1)) 405 )) 406 407 (0 until ICacheDataBanks).foreach { i => 408 when(s1_fire) { 409 s2_datas := s1_datas 410 }.elsewhen(s2_bankMSHRHit(i) && !fromMSHR.bits.corrupt) { 411 // if corrupt, no need to update s2_datas (it's wrong anyway), to save power 412 s2_datas(i) := s2_MSHR_datas(i) 413 } 414 } 415 416 (0 until PortNumber).foreach { i => 417 when(s1_fire) { 418 s2_hits := s1_hits 419 }.elsewhen(s2_MSHR_hits(i)) { 420 // update s2_hits even if it's corrupt, to let s2_fire 421 s2_hits(i) := true.B 422 } 423 } 424 425 val s2_l2_corrupt = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 426 (0 until PortNumber).foreach { i => 427 when(s1_fire) { 428 s2_l2_corrupt(i) := false.B 429 }.elsewhen(s2_MSHR_hits(i)) { 430 s2_l2_corrupt(i) := fromMSHR.bits.corrupt 431 } 432 } 433 434 /** 435 ****************************************************************************** 436 * send request to MSHR if ICache miss 437 ****************************************************************************** 438 */ 439 440 // merge pmp mmio and itlb pbmt 441 val s2_mmio = VecInit((s2_pmp_mmio zip s2_itlb_pbmt).map { case (mmio, pbmt) => 442 mmio || Pbmt.isUncache(pbmt) 443 }) 444 445 /* s2_exception includes itlb pf/gpf/af, pmp af and meta corruption (af), neither of which should be fetched 446 * mmio should not be fetched, it will be fetched by IFU mmio fsm 447 * also, if previous has exception, latter port should also not be fetched 448 */ 449 val s2_miss = VecInit((0 until PortNumber).map { i => 450 !s2_hits(i) && (if (i == 0) true.B else s2_doubleline) && 451 !ExceptionType.hasException(s2_exception.take(i + 1)) && 452 s2_mmio.take(i + 1).map(!_).reduce(_ && _) 453 }) 454 455 val toMSHRArbiter = Module(new Arbiter(new ICacheMissReq, PortNumber)) 456 457 // To avoid sending duplicate requests. 458 val has_send = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 459 (0 until PortNumber).foreach { i => 460 when(s1_fire) { 461 has_send(i) := false.B 462 }.elsewhen(toMSHRArbiter.io.in(i).fire) { 463 has_send(i) := true.B 464 } 465 } 466 467 (0 until PortNumber).map { i => 468 toMSHRArbiter.io.in(i).valid := s2_valid && s2_miss(i) && !has_send(i) && !s2_flush 469 toMSHRArbiter.io.in(i).bits.blkPaddr := getBlkAddr(s2_req_paddr(i)) 470 toMSHRArbiter.io.in(i).bits.vSetIdx := s2_req_vSetIdx(i) 471 } 472 toMSHR <> toMSHRArbiter.io.out 473 474 XSPerfAccumulate("to_missUnit_stall", toMSHR.valid && !toMSHR.ready) 475 476 val s2_fetch_finish = !s2_miss.reduce(_ || _) 477 478 // also raise af if data/l2 corrupt is detected 479 val s2_data_exception = VecInit(s2_data_corrupt.map(ExceptionType.fromECC(io.csr_parity_enable, _))) 480 val s2_l2_exception = VecInit(s2_l2_corrupt.map(ExceptionType.fromECC(true.B, _))) 481 482 // merge s2 exceptions, itlb has the highest priority, meta next, meta/data/l2 lowest (and we dont care about prioritizing between this three) 483 val s2_exception_out = ExceptionType.merge( 484 s2_exception, // includes itlb/pmp/meta exception 485 s2_data_exception, 486 s2_l2_exception 487 ) 488 489 /** 490 ****************************************************************************** 491 * response to IFU 492 ****************************************************************************** 493 */ 494 (0 until PortNumber).foreach { i => 495 if (i == 0) { 496 toIFU(i).valid := s2_fire 497 toIFU(i).bits.exception := s2_exception_out(i) 498 toIFU(i).bits.pmp_mmio := s2_pmp_mmio(i) // pass pmp_mmio instead of merged mmio to IFU 499 toIFU(i).bits.itlb_pbmt := s2_itlb_pbmt(i) 500 toIFU(i).bits.data := s2_datas.asTypeOf(UInt(blockBits.W)) 501 } else { 502 toIFU(i).valid := s2_fire && s2_doubleline 503 toIFU(i).bits.exception := Mux(s2_doubleline, s2_exception_out(i), ExceptionType.none) 504 toIFU(i).bits.pmp_mmio := s2_pmp_mmio(i) && s2_doubleline 505 toIFU(i).bits.itlb_pbmt := Mux(s2_doubleline, s2_itlb_pbmt(i), Pbmt.pma) 506 toIFU(i).bits.data := DontCare 507 } 508 toIFU(i).bits.backendException := s2_backendException 509 toIFU(i).bits.vaddr := s2_req_vaddr(i) 510 toIFU(i).bits.paddr := s2_req_paddr(i) 511 toIFU(i).bits.gpaddr := s2_req_gpaddr // Note: toIFU(1).bits.gpaddr is actually DontCare in current design 512 toIFU(i).bits.isForVSnonLeafPTE := s2_req_isForVSnonLeafPTE 513 } 514 515 s2_flush := io.flush 516 s2_ready := (s2_fetch_finish && !io.respStall) || !s2_valid 517 s2_fire := s2_valid && s2_fetch_finish && !io.respStall && !s2_flush 518 519 /** 520 ****************************************************************************** 521 * report Tilelink corrupt error 522 ****************************************************************************** 523 */ 524 (0 until PortNumber).map { i => 525 when(RegNext(s2_fire && s2_l2_corrupt(i))) { 526 io.errors(i).valid := true.B 527 io.errors(i).bits.report_to_beu := false.B // l2 should have report that to bus error unit, no need to do it again 528 io.errors(i).bits.paddr := RegNext(s2_req_paddr(i)) 529 io.errors(i).bits.source.tag := false.B 530 io.errors(i).bits.source.data := false.B 531 io.errors(i).bits.source.l2 := true.B 532 } 533 } 534 535 /** 536 ****************************************************************************** 537 * performance info. TODO: need to simplify the logic 538 ***********************************************************s******************* 539 */ 540 io.perfInfo.only_0_hit := s2_hits(0) && !s2_doubleline 541 io.perfInfo.only_0_miss := !s2_hits(0) && !s2_doubleline 542 io.perfInfo.hit_0_hit_1 := s2_hits(0) && s2_hits(1) && s2_doubleline 543 io.perfInfo.hit_0_miss_1 := s2_hits(0) && !s2_hits(1) && s2_doubleline 544 io.perfInfo.miss_0_hit_1 := !s2_hits(0) && s2_hits(1) && s2_doubleline 545 io.perfInfo.miss_0_miss_1 := !s2_hits(0) && !s2_hits(1) && s2_doubleline 546 io.perfInfo.hit_0_except_1 := s2_hits(0) && (ExceptionType.hasException(s2_exception(1))) && s2_doubleline 547 io.perfInfo.miss_0_except_1 := !s2_hits(0) && (ExceptionType.hasException(s2_exception(1))) && s2_doubleline 548 io.perfInfo.bank_hit(0) := s2_hits(0) 549 io.perfInfo.bank_hit(1) := s2_hits(1) && s2_doubleline 550 io.perfInfo.except_0 := ExceptionType.hasException(s2_exception(0)) 551 io.perfInfo.hit := s2_hits(0) && (!s2_doubleline || s2_hits(1)) 552 553 /** <PERF> fetch bubble generated by icache miss */ 554 XSPerfAccumulate("icache_bubble_s2_miss", s2_valid && !s2_fetch_finish) 555 XSPerfAccumulate("icache_bubble_s0_wayLookup", s0_valid && !fromWayLookup.ready) 556 557 io.fetch.topdownIcacheMiss := !s2_fetch_finish 558 io.fetch.topdownItlbMiss := s0_valid && !fromWayLookup.ready 559 560 // class ICacheTouchDB(implicit p: Parameters) extends ICacheBundle{ 561 // val blkPaddr = UInt((PAddrBits - blockOffBits).W) 562 // val vSetIdx = UInt(idxBits.W) 563 // val waymask = UInt(log2Ceil(nWays).W) 564 // } 565 566 // val isWriteICacheTouchTable = WireInit(Constantin.createRecord("isWriteICacheTouchTable" + p(XSCoreParamsKey).HartId.toString)) 567 // val ICacheTouchTable = ChiselDB.createTable("ICacheTouchTable" + p(XSCoreParamsKey).HartId.toString, new ICacheTouchDB) 568 569 // val ICacheTouchDumpData = Wire(Vec(PortNumber, new ICacheTouchDB)) 570 // (0 until PortNumber).foreach{ i => 571 // ICacheTouchDumpData(i).blkPaddr := getBlkAddr(s2_req_paddr(i)) 572 // ICacheTouchDumpData(i).vSetIdx := s2_req_vSetIdx(i) 573 // ICacheTouchDumpData(i).waymask := OHToUInt(s2_tag_match_vec(i)) 574 // ICacheTouchTable.log( 575 // data = ICacheTouchDumpData(i), 576 // en = io.touch(i).valid, 577 // site = "req_" + i.toString, 578 // clock = clock, 579 // reset = reset 580 // ) 581 // } 582 583 /** 584 ****************************************************************************** 585 * difftest refill check 586 ****************************************************************************** 587 */ 588 if (env.EnableDifftest) { 589 val discards = (0 until PortNumber).map { i => 590 val discard = ExceptionType.hasException(toIFU(i).bits.exception) || toIFU(i).bits.pmp_mmio || 591 Pbmt.isUncache(toIFU(i).bits.itlb_pbmt) 592 discard 593 } 594 val blkPaddrAll = s2_req_paddr.map(addr => addr(PAddrBits - 1, blockOffBits) << blockOffBits) 595 (0 until ICacheDataBanks).map { i => 596 val diffMainPipeOut = DifftestModule(new DiffRefillEvent, dontCare = true) 597 diffMainPipeOut.coreid := io.hartId 598 diffMainPipeOut.index := (3 + i).U 599 600 val bankSel = getBankSel(s2_req_offset, s2_valid).reduce(_ | _) 601 val lineSel = getLineSel(s2_req_offset) 602 603 diffMainPipeOut.valid := s2_fire && bankSel(i).asBool && Mux(lineSel(i), !discards(1), !discards(0)) 604 diffMainPipeOut.addr := Mux( 605 lineSel(i), 606 blkPaddrAll(1) + (i.U << (log2Ceil(blockBytes / ICacheDataBanks))), 607 blkPaddrAll(0) + (i.U << (log2Ceil(blockBytes / ICacheDataBanks))) 608 ) 609 610 diffMainPipeOut.data := s2_datas(i).asTypeOf(diffMainPipeOut.data) 611 diffMainPipeOut.idtfr := DontCare 612 } 613 } 614} 615