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.cache 18 19import chisel3._ 20import chisel3.util._ 21import freechips.rocketchip.tilelink.TLPermissions._ 22import freechips.rocketchip.tilelink.{TLArbiter, TLBundleC, TLBundleD, TLEdgeOut} 23import org.chipsalliance.cde.config.Parameters 24import utils.HasTLDump 25import utility.{XSDebug, XSPerfAccumulate, HasPerfEvents} 26 27 28class WritebackReqCtrl(implicit p: Parameters) extends DCacheBundle { 29 val param = UInt(cWidth.W) 30 val voluntary = Bool() 31 val hasData = Bool() 32 val corrupt = Bool() 33 val dirty = Bool() 34 35 val delay_release = Bool() 36 val miss_id = UInt(log2Up(cfg.nMissEntries).W) 37} 38 39class WritebackReqWodata(implicit p: Parameters) extends WritebackReqCtrl { 40 val addr = UInt(PAddrBits.W) 41 42 def dump(cond: Bool) = { 43 XSDebug(cond, "WritebackReq addr: %x param: %d voluntary: %b hasData: %b\n", 44 addr, param, voluntary, hasData) 45 } 46} 47 48class WritebackReqData(implicit p: Parameters) extends DCacheBundle { 49 val data = UInt((cfg.blockBytes * 8).W) 50} 51 52class WritebackReq(implicit p: Parameters) extends WritebackReqWodata { 53 val data = UInt((cfg.blockBytes * 8).W) 54 55 override def dump(cond: Bool) = { 56 XSDebug(cond, "WritebackReq addr: %x param: %d voluntary: %b hasData: %b data: %x\n", 57 addr, param, voluntary, hasData, data) 58 } 59 60 def toWritebackReqWodata(): WritebackReqWodata = { 61 val out = Wire(new WritebackReqWodata) 62 out.addr := addr 63 out.param := param 64 out.voluntary := voluntary 65 out.hasData := hasData 66 out.corrupt := corrupt 67 out.dirty := dirty 68 out.delay_release := delay_release 69 out.miss_id := miss_id 70 out 71 } 72 73 def toWritebackReqCtrl(): WritebackReqCtrl = { 74 val out = Wire(new WritebackReqCtrl) 75 out.param := param 76 out.voluntary := voluntary 77 out.hasData := hasData 78 out.corrupt := corrupt 79 out.dirty := dirty 80 out.delay_release := delay_release 81 out.miss_id := miss_id 82 out 83 } 84 85 def toWritebackReqData(): WritebackReqData = { 86 val out = Wire(new WritebackReqData) 87 out.data := data 88 out 89 } 90} 91 92// While a Release sleeps and waits for a refill to wake it up, 93// main pipe might update meta & data during this time. 94// So the meta & data to be released need to be updated too. 95class ReleaseUpdate(implicit p: Parameters) extends DCacheBundle { 96 // only consider store here 97 val addr = UInt(PAddrBits.W) 98 val mask = UInt(DCacheBanks.W) 99 val data = UInt((cfg.blockBytes * 8).W) 100} 101 102// To reduce fanout, writeback queue entry data is updated 1 cycle 103// after ReleaseUpdate.fire 104class WBQEntryReleaseUpdate(implicit p: Parameters) extends DCacheBundle { 105 // only consider store here 106 val addr = UInt(PAddrBits.W) 107 val mask_delayed = UInt(DCacheBanks.W) 108 val data_delayed = UInt((cfg.blockBytes * 8).W) 109 val mask_orr = Bool() 110} 111 112// When a probe TtoB req enter dcache main pipe, check if that cacheline 113// is waiting for release. If it is so, change TtoB to TtoN, set dcache 114// coh to N. 115class ProbeToBCheckReq(implicit p: Parameters) extends DCacheBundle { 116 val addr = UInt(PAddrBits.W) // paddr from mainpipe s1 117} 118 119class ProbeToBCheckResp(implicit p: Parameters) extends DCacheBundle { 120 val toN = Bool() // need to set dcache coh to N 121} 122 123class WritebackEntry(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule with HasTLDump 124{ 125 val io = IO(new Bundle { 126 val id = Input(UInt()) 127 128 val req = Flipped(DecoupledIO(new WritebackReqWodata)) 129 val req_data = Input(new WritebackReqData) 130 131 val mem_release = DecoupledIO(new TLBundleC(edge.bundle)) 132 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 133 val primary_valid = Input(Bool()) 134 val primary_ready = Output(Bool()) 135 val primary_ready_dup = Vec(nDupWbReady, Output(Bool())) 136 137 val block_addr = Output(Valid(UInt())) 138 }) 139 140 val s_invalid :: s_release_req :: s_release_resp ::Nil = Enum(3) 141 // ProbeAck: s_invalid -> s_release_req 142 // ProbeAck merge Release: s_invalid -> s_release_req 143 // Release: s_invalid -> s_sleep -> s_release_req -> s_release_resp 144 // Release merge ProbeAck: s_invalid -> s_sleep -> s_release_req 145 // (change Release into ProbeAck when Release is not fired) 146 // or: s_invalid -> s_sleep -> s_release_req -> s_release_resp -> s_release_req 147 // (send a ProbeAck after Release transaction is over) 148 149 val state = RegInit(s_invalid) 150 val state_dup_0 = RegInit(s_invalid) 151 val state_dup_1 = RegInit(s_invalid) 152 val state_dup_for_mp = RegInit(VecInit(Seq.fill(nDupWbReady)(s_invalid))) //TODO: clock gate 153 154 val remain = RegInit(0.U(refillCycles.W)) 155 val remain_dup_0 = RegInit(0.U(refillCycles.W)) 156 val remain_dup_1 = RegInit(0.U(refillCycles.W)) 157 val remain_set = WireInit(0.U(refillCycles.W)) 158 val remain_clr = WireInit(0.U(refillCycles.W)) 159 remain := (remain | remain_set) & ~remain_clr 160 remain_dup_0 := (remain_dup_0 | remain_set) & ~remain_clr 161 remain_dup_1 := (remain_dup_1 | remain_set) & ~remain_clr 162 163 // writeback queue data 164 val data = Reg(UInt((cfg.blockBytes * 8).W)) 165 166 // writeback queue paddr 167 val paddr_dup_0 = Reg(UInt(PAddrBits.W)) 168 val paddr_dup_1 = Reg(UInt(PAddrBits.W)) 169 val paddr_dup_2 = Reg(UInt(PAddrBits.W)) 170 171 // pending data write 172 // !s_data_override means there is an in-progress data write 173 val s_data_override = RegInit(true.B) 174 // !s_data_merge means there is an in-progress data merge 175 //val s_data_merge = RegInit(true.B) 176 177 // there are valid request that can be sent to release bus 178 //val busy = remain.orR && s_data_override && s_data_merge // have remain beats and data write finished 179 val busy = remain.orR && s_data_override // have remain beats and data write finished 180 val req = Reg(new WritebackReqWodata) 181 182 // assign default signals to output signals 183 io.req.ready := false.B 184 io.mem_release.valid := false.B 185 io.mem_release.bits := DontCare 186 io.mem_grant.ready := false.B 187 io.block_addr.valid := state =/= s_invalid 188 io.block_addr.bits := req.addr 189 190 s_data_override := true.B // data_override takes only 1 cycle 191 //s_data_merge := true.B // data_merge takes only 1 cycle 192 193 XSDebug(state =/= s_invalid, "WritebackEntry: %d state: %d block_addr: %x\n", io.id, state, io.block_addr.bits) 194 195 // -------------------------------------------------------------------------------- 196 // s_invalid: receive requests 197 // new req entering 198 io.req.ready := state === s_invalid 199 val alloc = io.req.valid && io.primary_valid && io.primary_ready 200 when (alloc) { 201 assert (remain === 0.U) 202 req := io.req.bits 203 s_data_override := false.B 204 // only update paddr when allocate a new missqueue entry 205 paddr_dup_0 := io.req.bits.addr 206 paddr_dup_1 := io.req.bits.addr 207 paddr_dup_2 := io.req.bits.addr 208 209 remain_set := Mux(io.req.bits.hasData, ~0.U(refillCycles.W), 1.U(refillCycles.W)) 210 state := s_release_req 211 state_dup_0 := s_release_req 212 state_dup_1 := s_release_req 213 state_dup_for_mp.foreach(_ := s_release_req) 214 } 215 216 // -------------------------------------------------------------------------------- 217 // while there beats remaining to be sent, we keep sending 218 // which beat to send in this cycle? 219 val beat = PriorityEncoder(remain_dup_0) 220 221 val beat_data = Wire(Vec(refillCycles, UInt(beatBits.W))) 222 for (i <- 0 until refillCycles) { 223 beat_data(i) := data((i + 1) * beatBits - 1, i * beatBits) 224 } 225 226 val probeResponse = edge.ProbeAck( 227 fromSource = io.id, 228 toAddress = paddr_dup_1, 229 lgSize = log2Ceil(cfg.blockBytes).U, 230 reportPermissions = req.param 231 ) 232 probeResponse.corrupt := req.corrupt 233 234 val probeResponseData = edge.ProbeAck( 235 fromSource = io.id, 236 toAddress = paddr_dup_1, 237 lgSize = log2Ceil(cfg.blockBytes).U, 238 reportPermissions = req.param, 239 data = beat_data(beat), 240 corrupt = req.corrupt 241 ) 242 243 val voluntaryRelease = edge.Release( 244 fromSource = io.id, 245 toAddress = paddr_dup_2, 246 lgSize = log2Ceil(cfg.blockBytes).U, 247 shrinkPermissions = req.param 248 )._2 249 voluntaryRelease.corrupt := req.corrupt 250 251 val voluntaryReleaseData = edge.Release( 252 fromSource = io.id, 253 toAddress = paddr_dup_2, 254 lgSize = log2Ceil(cfg.blockBytes).U, 255 shrinkPermissions = req.param, 256 data = beat_data(beat), 257 corrupt = req.corrupt 258 )._2 259 260 // voluntaryReleaseData.echo.lift(DirtyKey).foreach(_ := req.dirty) 261 when(busy) { 262 assert(!req.dirty || req.hasData) 263 } 264 265 io.mem_release.valid := busy 266 io.mem_release.bits := Mux(req.voluntary, 267 Mux(req.hasData, voluntaryReleaseData, voluntaryRelease), 268 Mux(req.hasData, probeResponseData, probeResponse)) 269 270 271 when (io.mem_release.fire) {remain_clr := PriorityEncoderOH(remain_dup_1)} 272 273 val (_, _, release_done, _) = edge.count(io.mem_release) 274 275 when(state === s_release_req && release_done){ 276 state := Mux(req.voluntary, s_release_resp, s_invalid) 277 when(req.voluntary){ 278 state_dup_for_mp.foreach(_ := s_release_resp) 279 } .otherwise{ 280 state_dup_for_mp.foreach(_ := s_invalid) 281 } 282 } 283 284 io.primary_ready := state === s_invalid 285 io.primary_ready_dup.zip(state_dup_for_mp).foreach { case (rdy, st) => rdy := st === s_invalid } 286 // -------------------------------------------------------------------------------- 287 // receive ReleaseAck for Releases 288 when (state === s_release_resp) { 289 io.mem_grant.ready := true.B 290 when (io.mem_grant.fire) { 291 state := s_invalid 292 state_dup_for_mp.foreach(_ := s_invalid) 293 } 294 } 295 296 // data update logic 297 when(!s_data_override && (req.hasData || RegNext(alloc))) { 298 data := io.req_data.data 299 } 300 301 // assert(!RegNext(!s_data_merge && !s_data_override)) 302 303 // performance counters 304 XSPerfAccumulate("wb_req", io.req.fire) 305 XSPerfAccumulate("wb_release", state === s_release_req && release_done && req.voluntary) 306 XSPerfAccumulate("wb_probe_resp", state === s_release_req && release_done && !req.voluntary) 307 XSPerfAccumulate("penalty_blocked_by_channel_C", io.mem_release.valid && !io.mem_release.ready) 308 XSPerfAccumulate("penalty_waiting_for_channel_D", io.mem_grant.ready && !io.mem_grant.valid && state === s_release_resp) 309} 310 311class WritebackQueue(edge: TLEdgeOut)(implicit p: Parameters) extends DCacheModule with HasTLDump with HasPerfEvents 312{ 313 val io = IO(new Bundle { 314 val req = Flipped(DecoupledIO(new WritebackReq)) 315 val req_ready_dup = Vec(nDupWbReady, Output(Bool())) 316 val mem_release = DecoupledIO(new TLBundleC(edge.bundle)) 317 val mem_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 318 319 //val probe_ttob_check_req = Flipped(ValidIO(new ProbeToBCheckReq)) 320 //val probe_ttob_check_resp = ValidIO(new ProbeToBCheckResp) 321 322 // 5 miss_req to check: 3*LoadPipe + 1*MainPipe + 1*missReqArb_out 323 val miss_req_conflict_check = Vec(LoadPipelineWidth + 2, Flipped(Valid(UInt()))) 324 val block_miss_req = Vec(LoadPipelineWidth + 2, Output(Bool())) 325 }) 326 327 require(cfg.nReleaseEntries > cfg.nMissEntries) 328 329 val primary_ready_vec = Wire(Vec(cfg.nReleaseEntries, Bool())) 330 val alloc = Cat(primary_ready_vec).orR 331 332 val req = io.req 333 val block_conflict = Wire(Bool()) 334 335 req.ready := alloc && !block_conflict 336 337 // assign default values to output signals 338 io.mem_release.valid := false.B 339 io.mem_release.bits := DontCare 340 io.mem_grant.ready := false.B 341 342 // delay data write in writeback req for 1 cycle 343 val req_data = RegEnable(io.req.bits.toWritebackReqData(), io.req.valid) 344 345 require(isPow2(cfg.nMissEntries)) 346 val grant_source = io.mem_grant.bits.source 347 val entries = Seq.fill(cfg.nReleaseEntries)(Module(new WritebackEntry(edge))) 348 entries.zipWithIndex.foreach { 349 case (entry, i) => 350 val former_primary_ready = if(i == 0) 351 false.B 352 else 353 Cat((0 until i).map(j => entries(j).io.primary_ready)).orR 354 val entry_id = (i + releaseIdBase).U 355 356 entry.io.id := entry_id 357 358 // entry req 359 entry.io.req.valid := req.valid && !block_conflict 360 primary_ready_vec(i) := entry.io.primary_ready 361 entry.io.req.bits := req.bits 362 entry.io.req_data := req_data 363 364 entry.io.primary_valid := alloc && 365 !former_primary_ready && 366 entry.io.primary_ready 367 368 entry.io.mem_grant.valid := (entry_id === grant_source) && io.mem_grant.valid 369 entry.io.mem_grant.bits := io.mem_grant.bits 370 //when (i.U === io.mem_grant.bits.source) { 371 // io.mem_grant.ready := entry.io.mem_grant.ready 372 //} 373 } 374 375 io.req_ready_dup.zipWithIndex.foreach { case (rdy, i) => 376 rdy := Cat(entries.map(_.io.primary_ready_dup(i))).orR && !block_conflict 377 } 378 379 io.mem_grant.ready := true.B 380 block_conflict := VecInit(entries.map(e => e.io.block_addr.valid && e.io.block_addr.bits === io.req.bits.addr)).asUInt.orR 381 val miss_req_conflict = io.miss_req_conflict_check.map{ r => 382 VecInit(entries.map(e => e.io.block_addr.valid && e.io.block_addr.bits === r.bits)).asUInt.orR 383 } 384 io.block_miss_req.zipWithIndex.foreach{ case(blk, i) => 385 blk := io.miss_req_conflict_check(i).valid && miss_req_conflict(i) 386 } 387 388 TLArbiter.robin(edge, io.mem_release, entries.map(_.io.mem_release):_*) 389 390 // sanity check 391 // print all input/output requests for debug purpose 392 // print req 393 io.req.bits.dump(io.req.fire) 394 395 io.mem_grant.bits.dump(io.mem_release.fire) 396 397 // XSDebug(io.miss_req.valid, "miss_req: addr: %x\n", io.miss_req.bits) 398 // XSDebug(io.block_miss_req, "block_miss_req\n") 399 400 // performance counters 401 XSPerfAccumulate("wb_req", io.req.fire) 402 403 val perfValidCount = RegNext(PopCount(entries.map(e => e.io.block_addr.valid))) 404 val perfEvents = Seq( 405 ("dcache_wbq_req ", io.req.fire), 406 ("dcache_wbq_1_4_valid", (perfValidCount < (cfg.nReleaseEntries.U/4.U))), 407 ("dcache_wbq_2_4_valid", (perfValidCount > (cfg.nReleaseEntries.U/4.U)) & (perfValidCount <= (cfg.nReleaseEntries.U/2.U))), 408 ("dcache_wbq_3_4_valid", (perfValidCount > (cfg.nReleaseEntries.U/2.U)) & (perfValidCount <= (cfg.nReleaseEntries.U*3.U/4.U))), 409 ("dcache_wbq_4_4_valid", (perfValidCount > (cfg.nReleaseEntries.U*3.U/4.U))), 410 ) 411 generatePerfEvent() 412 413}