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 org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import freechips.rocketchip.diplomacy.IdRange 23import freechips.rocketchip.tilelink.ClientStates._ 24import freechips.rocketchip.tilelink.TLPermissions._ 25import freechips.rocketchip.tilelink._ 26import xiangshan._ 27import xiangshan.cache._ 28import utils._ 29import utility._ 30import difftest._ 31 32 33abstract class ICacheMissUnitModule(implicit p: Parameters) extends XSModule 34 with HasICacheParameters 35 36abstract class ICacheMissUnitBundle(implicit p: Parameters) extends XSBundle 37 with HasICacheParameters 38 39class ICacheMissReq(implicit p: Parameters) extends ICacheBundle 40{ 41 val paddr = UInt(PAddrBits.W) 42 val vaddr = UInt(VAddrBits.W) 43 val waymask = UInt(nWays.W) 44 45 def getVirSetIdx = get_idx(vaddr) 46 def getPhyTag = get_phy_tag(paddr) 47} 48 49 50class ICacheMissResp(implicit p: Parameters) extends ICacheBundle 51{ 52 val data = UInt(blockBits.W) 53 val corrupt = Bool() 54} 55 56class ICacheMissBundle(implicit p: Parameters) extends ICacheBundle{ 57 val req = Vec(2, Flipped(DecoupledIO(new ICacheMissReq))) 58 val resp = Vec(2,ValidIO(new ICacheMissResp)) 59 val flush = Input(Bool()) 60} 61 62 63class ICacheMissEntry(edge: TLEdgeOut, id: Int)(implicit p: Parameters) extends ICacheMissUnitModule 64 with MemoryOpConstants 65{ 66 val io = IO(new Bundle { 67 val id = Input(UInt(log2Ceil(PortNumber).W)) 68 69 val req = Flipped(DecoupledIO(new ICacheMissReq)) 70 val resp = ValidIO(new ICacheMissResp) 71 72 //tilelink channel 73 val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle)) 74 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 75 76 val meta_write = DecoupledIO(new ICacheMetaWriteBundle) 77 val data_write = DecoupledIO(new ICacheDataWriteBundle) 78 79 val ongoing_req = Output(new FilterInfo) 80 val fencei = Input(Bool()) 81 }) 82 83 /** default value for control signals */ 84 io.resp := DontCare 85 io.mem_acquire.bits := DontCare 86 io.mem_grant.ready := true.B 87 io.meta_write.bits := DontCare 88 io.data_write.bits := DontCare 89 90 val s_idle :: s_send_mem_aquire :: s_wait_mem_grant :: s_write_back :: s_wait_resp :: Nil = Enum(5) 91 val state = RegInit(s_idle) 92 /** control logic transformation */ 93 //request register 94 val req = Reg(new ICacheMissReq) 95 val req_idx = req.getVirSetIdx //virtual index 96 val req_tag = req.getPhyTag //physical tag 97 val req_waymask = req.waymask 98 val req_corrupt = RegInit(false.B) 99 100 val (_, _, refill_done, refill_address_inc) = edge.addr_inc(io.mem_grant) 101 102 val needflush_r = RegInit(false.B) 103 when (state === s_idle) { needflush_r := false.B } 104 when (state =/= s_idle && io.fencei) { needflush_r := true.B } 105 val needflush = needflush_r | io.fencei 106 107 //cacheline register 108 val readBeatCnt = Reg(UInt(log2Up(refillCycles).W)) 109 val respDataReg = Reg(Vec(refillCycles, UInt(beatBits.W))) 110 111 //initial 112 io.resp.bits := DontCare 113 io.mem_acquire.bits := DontCare 114 io.mem_grant.ready := true.B 115 io.meta_write.bits := DontCare 116 io.data_write.bits := DontCare 117 118 io.req.ready := (state === s_idle) 119 io.mem_acquire.valid := (state === s_send_mem_aquire) 120 121 io.ongoing_req.valid := (state =/= s_idle) 122 io.ongoing_req.paddr := addrAlign(req.paddr, blockBytes, PAddrBits) 123 124 //state change 125 switch(state) { 126 is(s_idle) { 127 when(io.req.fire) { 128 readBeatCnt := 0.U 129 state := s_send_mem_aquire 130 req := io.req.bits 131 } 132 } 133 134 // memory request 135 is(s_send_mem_aquire) { 136 when(io.mem_acquire.fire) { 137 state := s_wait_mem_grant 138 } 139 } 140 141 is(s_wait_mem_grant) { 142 when(edge.hasData(io.mem_grant.bits)) { 143 when(io.mem_grant.fire) { 144 readBeatCnt := readBeatCnt + 1.U 145 respDataReg(readBeatCnt) := io.mem_grant.bits.data 146 req_corrupt := io.mem_grant.bits.corrupt // TODO: seems has bug 147 when(readBeatCnt === (refillCycles - 1).U) { 148 assert(refill_done, "refill not done!") 149 state := s_write_back 150 } 151 } 152 } 153 } 154 155 is(s_write_back) { 156 state := Mux(io.meta_write.fire && io.data_write.fire || needflush, s_wait_resp, s_write_back) 157 } 158 159 is(s_wait_resp) { 160 io.resp.bits.data := respDataReg.asUInt 161 io.resp.bits.corrupt := req_corrupt 162 when(io.resp.fire) { 163 state := s_idle 164 } 165 } 166 } 167 168 /** refill write and meta write */ 169 170 val getBlock = edge.Get( 171 fromSource = io.id, 172 toAddress = addrAlign(req.paddr, blockBytes, PAddrBits), 173 lgSize = (log2Up(cacheParams.blockBytes)).U 174 )._2 175 176 io.mem_acquire.bits := getBlock // getBlock 177 // req source 178 io.mem_acquire.bits.user.lift(ReqSourceKey).foreach(_ := MemReqSource.CPUInst.id.U) 179 require(nSets <= 256) // icache size should not be more than 128KB 180 181 //resp to ifu 182 io.resp.valid := state === s_wait_resp 183 184 io.meta_write.valid := (state === s_write_back && !needflush) 185 io.meta_write.bits.generate(tag = req_tag, idx = req_idx, waymask = req_waymask, bankIdx = req_idx(0)) 186 187 io.data_write.valid := (state === s_write_back && !needflush) 188 io.data_write.bits.generate(data = respDataReg.asUInt, 189 idx = req_idx, 190 waymask = req_waymask, 191 bankIdx = req_idx(0), 192 paddr = req.paddr) 193 194 XSPerfAccumulate( 195 "entryPenalty" + Integer.toString(id, 10), 196 BoolStopWatch( 197 start = io.req.fire, 198 stop = io.resp.valid, 199 startHighPriority = true) 200 ) 201 XSPerfAccumulate("entryReq" + Integer.toString(id, 10), io.req.fire) 202 203 // Statistics on the latency distribution of MSHR 204 val cntLatency = RegInit(0.U(32.W)) 205 cntLatency := Mux(io.mem_acquire.fire, 1.U, cntLatency + 1.U) 206 // the condition is same as the transition from s_wait_mem_grant to s_write_back 207 val cntEnable = (state === s_wait_mem_grant) && edge.hasData(io.mem_grant.bits) && 208 io.mem_grant.fire && (readBeatCnt === (refillCycles - 1).U) 209 XSPerfHistogram("icache_mshr_latency_" + id.toString(), cntLatency, cntEnable, 0, 300, 10, right_strict = true) 210} 211 212 213class ICacheMissUnit(edge: TLEdgeOut)(implicit p: Parameters) extends ICacheMissUnitModule 214{ 215 val io = IO(new Bundle{ 216 val hartId = Input(UInt(8.W)) 217 val req = Vec(2, Flipped(DecoupledIO(new ICacheMissReq))) 218 val resp = Vec(2, ValidIO(new ICacheMissResp)) 219 220 val mem_acquire = DecoupledIO(new TLBundleA(edge.bundle)) 221 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 222 223 val fdip_acquire = Flipped(DecoupledIO(new TLBundleA(edge.bundle))) 224 val fdip_grant = DecoupledIO(new TLBundleD(edge.bundle)) 225 226 val meta_write = DecoupledIO(new ICacheMetaWriteBundle) 227 val data_write = DecoupledIO(new ICacheDataWriteBundle) 228 229 val ICacheMissUnitInfo = new ICacheMissUnitInfo 230 val fencei = Input(Bool()) 231 }) 232 // assign default values to output signals 233 io.mem_grant.ready := false.B 234 235 val meta_write_arb = Module(new Arbiter(new ICacheMetaWriteBundle, PortNumber)) 236 val refill_arb = Module(new Arbiter(new ICacheDataWriteBundle, PortNumber)) 237 238 io.mem_grant.ready := true.B 239 240 val entries = (0 until PortNumber) map { i => 241 val entry = Module(new ICacheMissEntry(edge, i)) 242 243 entry.io.id := i.U 244 245 // entry req 246 entry.io.req.valid := io.req(i).valid 247 entry.io.req.bits := io.req(i).bits 248 io.req(i).ready := entry.io.req.ready 249 250 // entry resp 251 meta_write_arb.io.in(i) <> entry.io.meta_write 252 refill_arb.io.in(i) <> entry.io.data_write 253 254 entry.io.mem_grant.valid := false.B 255 entry.io.mem_grant.bits := DontCare 256 when (io.mem_grant.bits.source === i.U) { 257 entry.io.mem_grant <> io.mem_grant 258 } 259 260 io.resp(i) <> entry.io.resp 261 io.ICacheMissUnitInfo.mshr(i) <> entry.io.ongoing_req 262 entry.io.fencei := io.fencei 263// XSPerfAccumulate( 264// "entryPenalty" + Integer.toString(i, 10), 265// BoolStopWatch( 266// start = entry.io.req.fire, 267// stop = entry.io.resp.fire, 268// startHighPriority = true) 269// ) 270// XSPerfAccumulate("entryReq" + Integer.toString(i, 10), entry.io.req.fire) 271 272 entry 273 } 274 275 io.fdip_grant.valid := false.B 276 io.fdip_grant.bits := DontCare 277 when (io.mem_grant.bits.source === PortNumber.U) { 278 io.fdip_grant <> io.mem_grant 279 } 280 281 /** 282 ****************************************************************************** 283 * Register 2 cycle meta write info for IPrefetchPipe filter 284 ****************************************************************************** 285 */ 286 val meta_write_buffer = InitQueue(new FilterInfo, size = 2) 287 meta_write_buffer(0).valid := io.meta_write.fire 288 meta_write_buffer(0).paddr := io.data_write.bits.paddr 289 meta_write_buffer(1) := meta_write_buffer(0) 290 (0 until 2).foreach (i => { 291 io.ICacheMissUnitInfo.recentWrite(i) := meta_write_buffer(i) 292 }) 293 294 val tl_a_chanel = entries.map(_.io.mem_acquire) :+ io.fdip_acquire 295 TLArbiter.lowest(edge, io.mem_acquire, tl_a_chanel:_*) 296 297 io.meta_write <> meta_write_arb.io.out 298 io.data_write <> refill_arb.io.out 299 300 if (env.EnableDifftest) { 301 val difftest = DifftestModule(new DiffRefillEvent, dontCare = true) 302 difftest.coreid := io.hartId 303 difftest.index := 0.U 304 difftest.valid := refill_arb.io.out.valid 305 difftest.addr := refill_arb.io.out.bits.paddr 306 difftest.data := refill_arb.io.out.bits.data.asTypeOf(difftest.data) 307 difftest.idtfr := DontCare 308 } 309 310 (0 until nWays).map{ w => 311 XSPerfAccumulate("line_0_refill_way_" + Integer.toString(w, 10), entries(0).io.meta_write.valid && OHToUInt(entries(0).io.meta_write.bits.waymask) === w.U) 312 XSPerfAccumulate("line_1_refill_way_" + Integer.toString(w, 10), entries(1).io.meta_write.valid && OHToUInt(entries(1).io.meta_write.bits.waymask) === w.U) 313 } 314 315} 316