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 gpaddr = UInt(GPAddrBits.W) 44 val isForVSnonLeafPTE = Bool() 45 val exception = UInt(ExceptionType.width.W) 46 val pmp_mmio = Bool() 47 val itlb_pbmt = UInt(Pbmt.width.W) 48 val exceptionFromBackend = Bool() 49} 50 51class ICacheMainPipeBundle(implicit p: Parameters) extends ICacheBundle { 52 val req = Flipped(Decoupled(new FtqToICacheRequestBundle)) 53 val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp)) 54 val topdownIcacheMiss = Output(Bool()) 55 val topdownItlbMiss = Output(Bool()) 56} 57 58class ICacheMetaReqBundle(implicit p: Parameters) extends ICacheBundle { 59 val toIMeta = DecoupledIO(new ICacheReadBundle) 60 val fromIMeta = Input(new ICacheMetaRespBundle) 61} 62 63class ICacheDataReqBundle(implicit p: Parameters) extends ICacheBundle { 64 val toIData = Vec(partWayNum, DecoupledIO(new ICacheReadBundle)) 65 val fromIData = Input(new ICacheDataRespBundle) 66} 67 68class ICacheMSHRBundle(implicit p: Parameters) extends ICacheBundle { 69 val req = Decoupled(new ICacheMissReq) 70 val resp = Flipped(ValidIO(new ICacheMissResp)) 71} 72 73class ICachePMPBundle(implicit p: Parameters) extends ICacheBundle { 74 val req = Valid(new PMPReqBundle()) 75 val resp = Input(new PMPRespBundle()) 76} 77 78class ICachePerfInfo(implicit p: Parameters) extends ICacheBundle { 79 val only_0_hit = Bool() 80 val only_0_miss = Bool() 81 val hit_0_hit_1 = Bool() 82 val hit_0_miss_1 = Bool() 83 val miss_0_hit_1 = Bool() 84 val miss_0_miss_1 = Bool() 85 val hit_0_except_1 = Bool() 86 val miss_0_except_1 = Bool() 87 val except_0 = Bool() 88 val bank_hit = Vec(2, Bool()) 89 val hit = Bool() 90} 91 92class ICacheMainPipeInterface(implicit p: Parameters) extends ICacheBundle { 93 val hartId = Input(UInt(hartIdLen.W)) 94 95 /*** internal interface ***/ 96 val dataArray = new ICacheDataReqBundle 97 98 /** prefetch io */ 99 val touch = Vec(PortNumber, ValidIO(new ReplacerTouch)) 100 val wayLookupRead = Flipped(DecoupledIO(new WayLookupInfo)) 101 102 val mshr = new ICacheMSHRBundle 103 val errors = Output(Vec(PortNumber, ValidIO(new L1CacheErrorInfo))) 104 105 /*** outside interface ***/ 106 // val fetch = Vec(PortNumber, new ICacheMainPipeBundle) 107 /* when ftq.valid is high in T + 1 cycle 108 * the ftq component must be valid in T cycle 109 */ 110 val fetch = new ICacheMainPipeBundle 111 val pmp = Vec(PortNumber, new ICachePMPBundle) 112 val respStall = Input(Bool()) 113 114 val csr_parity_enable = Input(Bool()) 115 val flush = Input(Bool()) 116 117 val perfInfo = Output(new ICachePerfInfo) 118} 119 120class ICacheDB(implicit p: Parameters) extends ICacheBundle { 121 val blk_vaddr = UInt((VAddrBits - blockOffBits).W) 122 val blk_paddr = UInt((PAddrBits - blockOffBits).W) 123 val hit = Bool() 124} 125 126class ICacheMainPipe(implicit p: Parameters) extends ICacheModule { 127 val io = IO(new ICacheMainPipeInterface) 128 129 /** Input/Output port */ 130 val (fromFtq, toIFU) = (io.fetch.req, io.fetch.resp) 131 val (toData, fromData) = (io.dataArray.toIData, io.dataArray.fromIData) 132 val (toMSHR, fromMSHR) = (io.mshr.req, io.mshr.resp) 133 val (toPMP, fromPMP) = (io.pmp.map(_.req), io.pmp.map(_.resp)) 134 val fromWayLookup = io.wayLookupRead 135 136 // Statistics on the frequency distribution of FTQ fire interval 137 val cntFtqFireInterval = RegInit(0.U(32.W)) 138 cntFtqFireInterval := Mux(fromFtq.fire, 1.U, cntFtqFireInterval + 1.U) 139 XSPerfHistogram("ftq2icache_fire", cntFtqFireInterval, fromFtq.fire, 1, 300, 1, right_strict = true) 140 141 /** pipeline control signal */ 142 val s1_ready, s2_ready = Wire(Bool()) 143 val s0_fire, s1_fire, s2_fire = Wire(Bool()) 144 val s0_flush, s1_flush, s2_flush = Wire(Bool()) 145 146 /** 147 ****************************************************************************** 148 * ICache Stage 0 149 * - send req to data SRAM 150 * - get waymask and tlb info from wayLookup 151 ****************************************************************************** 152 */ 153 154 /** s0 control */ 155 // 0,1,2,3 -> dataArray(data); 4 -> mainPipe 156 // Ftq RegNext Register 157 val fromFtqReq = fromFtq.bits.pcMemRead 158 val s0_valid = fromFtq.valid 159 val s0_req_valid_all = (0 until partWayNum + 1).map(i => fromFtq.bits.readValid(i)) 160 val s0_req_vaddr_all = 161 (0 until partWayNum + 1).map(i => VecInit(Seq(fromFtqReq(i).startAddr, fromFtqReq(i).nextlineStart))) 162 val s0_req_vSetIdx_all = (0 until partWayNum + 1).map(i => VecInit(s0_req_vaddr_all(i).map(get_idx))) 163 val s0_req_offset_all = (0 until partWayNum + 1).map(i => s0_req_vaddr_all(i)(0)(log2Ceil(blockBytes) - 1, 0)) 164 val s0_doubleline_all = (0 until partWayNum + 1).map(i => fromFtq.bits.readValid(i) && fromFtqReq(i).crossCacheline) 165 166 val s0_req_vaddr = s0_req_vaddr_all.last 167 val s0_req_vSetIdx = s0_req_vSetIdx_all.last 168 val s0_doubleline = s0_doubleline_all.last 169 170 val s0_ftq_exception = VecInit((0 until PortNumber).map(i => ExceptionType.fromFtq(fromFtq.bits))) 171 val s0_excp_fromBackend = fromFtq.bits.backendIaf || fromFtq.bits.backendIpf || fromFtq.bits.backendIgpf 172 173 /** 174 ****************************************************************************** 175 * get waymask and tlb info from wayLookup 176 ****************************************************************************** 177 */ 178 fromWayLookup.ready := s0_fire 179 val s0_waymasks = VecInit(fromWayLookup.bits.waymask.map(_.asTypeOf(Vec(nWays, Bool())))) 180 val s0_req_ptags = fromWayLookup.bits.ptag 181 val s0_req_gpaddr = fromWayLookup.bits.gpaddr 182 val s0_req_isForVSnonLeafPTE = fromWayLookup.bits.isForVSnonLeafPTE 183 val s0_itlb_exception = fromWayLookup.bits.itlb_exception 184 val s0_itlb_pbmt = fromWayLookup.bits.itlb_pbmt 185 val s0_meta_codes = fromWayLookup.bits.meta_codes 186 val s0_hits = VecInit(fromWayLookup.bits.waymask.map(_.orR)) 187 188 when(s0_fire) { 189 assert( 190 (0 until PortNumber).map(i => s0_req_vSetIdx(i) === fromWayLookup.bits.vSetIdx(i)).reduce(_ && _), 191 "vSetIdxs from ftq and wayLookup are different! vaddr0=0x%x ftq: vidx0=0x%x vidx1=0x%x wayLookup: vidx0=0x%x vidx1=0x%x", 192 s0_req_vaddr(0), 193 s0_req_vSetIdx(0), 194 s0_req_vSetIdx(1), 195 fromWayLookup.bits.vSetIdx(0), 196 fromWayLookup.bits.vSetIdx(1) 197 ) 198 } 199 200 val s0_exception_out = ExceptionType.merge( 201 s0_ftq_exception, // backend-requested exception has the highest priority 202 s0_itlb_exception 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_exception_out, 0.U.asTypeOf(s0_exception_out), s0_fire) 241 val s1_excp_fromBackend = RegEnable(s0_excp_fromBackend, 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 // && s1_itlb_exception === ExceptionType.none 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_excp_fromBackend = RegEnable(s1_excp_fromBackend, 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 s2_exception.take(i + 1).map(_ === ExceptionType.none).reduce(_ && _) && 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.exceptionFromBackend := s2_excp_fromBackend 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) && (s2_exception(1) =/= ExceptionType.none) && s2_doubleline 547 io.perfInfo.miss_0_except_1 := !s2_hits(0) && (s2_exception(1) =/= ExceptionType.none) && 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 := s2_exception(0) =/= ExceptionType.none 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 = toIFU(i).bits.exception =/= ExceptionType.none || 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