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