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.mem 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import utility._ 24import xiangshan._ 25import xiangshan.cache.{AtomicWordIO, HasDCacheParameters, MemoryOpConstants} 26import xiangshan.cache.mmu.{TlbCmd, TlbRequestIO} 27import difftest._ 28import xiangshan.ExceptionNO._ 29import xiangshan.backend.fu.PMPRespBundle 30import xiangshan.backend.Bundles.{MemExuInput, MemExuOutput} 31import xiangshan.backend.fu.NewCSR.TriggerUtil 32import xiangshan.backend.fu.util.SdtrigExt 33 34class AtomicsUnit(implicit p: Parameters) extends XSModule 35 with MemoryOpConstants 36 with HasDCacheParameters 37 with SdtrigExt{ 38 val io = IO(new Bundle() { 39 val hartId = Input(UInt(hartIdLen.W)) 40 val in = Flipped(Decoupled(new MemExuInput)) 41 val storeDataIn = Flipped(Valid(new MemExuOutput)) // src2 from rs 42 val out = Decoupled(new MemExuOutput) 43 val dcache = new AtomicWordIO 44 val dtlb = new TlbRequestIO(2) 45 val pmpResp = Flipped(new PMPRespBundle()) 46 val flush_sbuffer = new SbufferFlushBundle 47 val feedbackSlow = ValidIO(new RSFeedback) 48 val redirect = Flipped(ValidIO(new Redirect)) 49 val exceptionInfo = ValidIO(new Bundle { 50 val vaddr = UInt(XLEN.W) 51 val gpaddr = UInt(XLEN.W) 52 val isForVSnonLeafPTE = Bool() 53 }) 54 val csrCtrl = Flipped(new CustomCSRCtrlIO) 55 }) 56 57 //------------------------------------------------------- 58 // Atomics Memory Accsess FSM 59 //------------------------------------------------------- 60 val s_invalid :: s_tlb_and_flush_sbuffer_req :: s_pm :: s_wait_flush_sbuffer_resp :: s_cache_req :: s_cache_resp :: s_cache_resp_latch :: s_finish :: Nil = Enum(8) 61 val state = RegInit(s_invalid) 62 val out_valid = RegInit(false.B) 63 val data_valid = RegInit(false.B) 64 val in = Reg(new MemExuInput()) 65 val exceptionVec = RegInit(0.U.asTypeOf(ExceptionVec())) 66 val trigger = RegInit(TriggerAction.None) 67 val atom_override_xtval = RegInit(false.B) 68 val have_sent_first_tlb_req = RegInit(false.B) 69 val isLr = in.uop.fuOpType === LSUOpType.lr_w || in.uop.fuOpType === LSUOpType.lr_d 70 // paddr after translation 71 val paddr = Reg(UInt()) 72 val gpaddr = Reg(UInt()) 73 val vaddr = in.src(0) 74 val is_mmio = Reg(Bool()) 75 val isForVSnonLeafPTE = Reg(Bool()) 76 77 // dcache response data 78 val resp_data = Reg(UInt()) 79 val resp_data_wire = WireInit(0.U) 80 val is_lrsc_valid = Reg(Bool()) 81 // sbuffer is empty or not 82 val sbuffer_empty = io.flush_sbuffer.empty 83 84 85 // Difftest signals 86 val paddr_reg = Reg(UInt(64.W)) 87 val data_reg = Reg(UInt(64.W)) 88 val mask_reg = Reg(UInt(8.W)) 89 val fuop_reg = Reg(UInt(8.W)) 90 91 io.exceptionInfo.valid := atom_override_xtval 92 io.exceptionInfo.bits.vaddr := in.src(0) 93 io.exceptionInfo.bits.gpaddr := gpaddr 94 io.exceptionInfo.bits.isForVSnonLeafPTE := isForVSnonLeafPTE 95 96 // assign default value to output signals 97 io.in.ready := false.B 98 99 io.dcache.req.valid := false.B 100 io.dcache.req.bits := DontCare 101 102 io.dtlb.req.valid := false.B 103 io.dtlb.req.bits := DontCare 104 io.dtlb.req_kill := false.B 105 io.dtlb.resp.ready := true.B 106 107 io.flush_sbuffer.valid := false.B 108 109 XSDebug("state: %d\n", state) 110 111 when (state === s_invalid) { 112 io.in.ready := true.B 113 when (io.in.fire) { 114 in := io.in.bits 115 in.src(1) := in.src(1) // leave src2 unchanged 116 state := s_tlb_and_flush_sbuffer_req 117 have_sent_first_tlb_req := false.B 118 } 119 } 120 121 when (io.storeDataIn.fire) { 122 in.src(1) := io.storeDataIn.bits.data 123 data_valid := true.B 124 } 125 126 assert(!(io.storeDataIn.fire && data_valid), "atomic unit re-receive data") 127 128 // Send TLB feedback to store issue queue 129 // we send feedback right after we receives request 130 // also, we always treat amo as tlb hit 131 // since we will continue polling tlb all by ourself 132 io.feedbackSlow.valid := GatedValidRegNext(GatedValidRegNext(io.in.valid)) 133 io.feedbackSlow.bits.hit := true.B 134 io.feedbackSlow.bits.robIdx := RegEnable(io.in.bits.uop.robIdx, io.in.valid) 135 io.feedbackSlow.bits.sqIdx := RegEnable(io.in.bits.uop.sqIdx, io.in.valid) 136 io.feedbackSlow.bits.lqIdx := RegEnable(io.in.bits.uop.lqIdx, io.in.valid) 137 io.feedbackSlow.bits.flushState := DontCare 138 io.feedbackSlow.bits.sourceType := DontCare 139 io.feedbackSlow.bits.dataInvalidSqIdx := DontCare 140 141 // atomic trigger 142 val csrCtrl = io.csrCtrl 143 val tdata = Reg(Vec(TriggerNum, new MatchTriggerIO)) 144 val tEnableVec = RegInit(VecInit(Seq.fill(TriggerNum)(false.B))) 145 tEnableVec := csrCtrl.mem_trigger.tEnableVec 146 when(csrCtrl.mem_trigger.tUpdate.valid) { 147 tdata(csrCtrl.mem_trigger.tUpdate.bits.addr) := csrCtrl.mem_trigger.tUpdate.bits.tdata 148 } 149 150 val debugMode = csrCtrl.mem_trigger.debugMode 151 val triggerCanRaiseBpExp = csrCtrl.mem_trigger.triggerCanRaiseBpExp 152 val backendTriggerTimingVec = VecInit(tdata.map(_.timing)) 153 val backendTriggerChainVec = VecInit(tdata.map(_.chain)) 154 val backendTriggerHitVec = WireInit(VecInit(Seq.fill(TriggerNum)(false.B))) 155 val backendTriggerCanFireVec = RegInit(VecInit(Seq.fill(TriggerNum)(false.B))) 156 157 val isNotLr = (in.uop.fuOpType =/= LSUOpType.lr_w) && (in.uop.fuOpType =/= LSUOpType.lr_d) 158 val isNotSc = (in.uop.fuOpType =/= LSUOpType.sc_w) && (in.uop.fuOpType =/= LSUOpType.sc_d) 159 160 // store trigger 161 val store_hit = Wire(Vec(TriggerNum, Bool())) 162 for (j <- 0 until TriggerNum) { 163 store_hit(j) := !tdata(j).select && !debugMode && isNotLr && TriggerCmp( 164 vaddr, 165 tdata(j).tdata2, 166 tdata(j).matchType, 167 tEnableVec(j) && tdata(j).store 168 ) 169 } 170 // load trigger 171 val load_hit = Wire(Vec(TriggerNum, Bool())) 172 for (j <- 0 until TriggerNum) { 173 load_hit(j) := !tdata(j).select && !debugMode && isNotSc && TriggerCmp( 174 vaddr, 175 tdata(j).tdata2, 176 tdata(j).matchType, 177 tEnableVec(j) && tdata(j).load 178 ) 179 } 180 backendTriggerHitVec := store_hit.zip(load_hit).map { case (sh, lh) => sh || lh } 181 // triggerCanFireVec will update at T+1 182 TriggerCheckCanFire(TriggerNum, backendTriggerCanFireVec, backendTriggerHitVec, backendTriggerTimingVec, backendTriggerChainVec) 183 184 val actionVec = VecInit(tdata.map(_.action)) 185 val triggerAction = Wire(TriggerAction()) 186 TriggerUtil.triggerActionGen(triggerAction, backendTriggerCanFireVec, actionVec, triggerCanRaiseBpExp) 187 val triggerDebugMode = TriggerAction.isDmode(triggerAction) 188 val triggerBreakpoint = TriggerAction.isExp(triggerAction) 189 190 // tlb translation, manipulating signals && deal with exception 191 // at the same time, flush sbuffer 192 when (state === s_tlb_and_flush_sbuffer_req) { 193 // send req to dtlb 194 // keep firing until tlb hit 195 io.dtlb.req.valid := true.B 196 io.dtlb.req.bits.vaddr := in.src(0) 197 io.dtlb.req.bits.fullva := in.src(0) 198 io.dtlb.req.bits.checkfullva := true.B 199 io.dtlb.resp.ready := true.B 200 io.dtlb.req.bits.cmd := Mux(isLr, TlbCmd.atom_read, TlbCmd.atom_write) 201 io.dtlb.req.bits.debug.pc := in.uop.pc 202 io.dtlb.req.bits.debug.robIdx := in.uop.robIdx 203 io.dtlb.req.bits.debug.isFirstIssue := false.B 204 io.out.bits.uop.debugInfo.tlbFirstReqTime := GTimer() // FIXME lyq: it will be always assigned 205 206 // send req to sbuffer to flush it if it is not empty 207 io.flush_sbuffer.valid := Mux(sbuffer_empty, false.B, true.B) 208 209 // do not accept tlb resp in the first cycle 210 // this limition is for hw prefetcher 211 // when !have_sent_first_tlb_req, tlb resp may come from hw prefetch 212 have_sent_first_tlb_req := true.B 213 214 when(io.dtlb.resp.fire && have_sent_first_tlb_req){ 215 paddr := io.dtlb.resp.bits.paddr(0) 216 gpaddr := io.dtlb.resp.bits.gpaddr(0) 217 isForVSnonLeafPTE := io.dtlb.resp.bits.isForVSnonLeafPTE 218 // exception handling 219 val addrAligned = LookupTree(in.uop.fuOpType(1,0), List( 220 "b00".U -> true.B, //b 221 "b01".U -> (in.src(0)(0) === 0.U), //h 222 "b10".U -> (in.src(0)(1,0) === 0.U), //w 223 "b11".U -> (in.src(0)(2,0) === 0.U) //d 224 )) 225 exceptionVec(loadAddrMisaligned) := !addrAligned && isLr 226 exceptionVec(storeAddrMisaligned) := !addrAligned && !isLr 227 exceptionVec(storePageFault) := io.dtlb.resp.bits.excp(0).pf.st 228 exceptionVec(loadPageFault) := io.dtlb.resp.bits.excp(0).pf.ld 229 exceptionVec(storeAccessFault) := io.dtlb.resp.bits.excp(0).af.st 230 exceptionVec(loadAccessFault) := io.dtlb.resp.bits.excp(0).af.ld 231 exceptionVec(storeGuestPageFault) := io.dtlb.resp.bits.excp(0).gpf.st 232 exceptionVec(loadGuestPageFault) := io.dtlb.resp.bits.excp(0).gpf.ld 233 234 exceptionVec(breakPoint) := triggerBreakpoint 235 trigger := triggerAction 236 237 when (!io.dtlb.resp.bits.miss) { 238 io.out.bits.uop.debugInfo.tlbRespTime := GTimer() 239 when (!addrAligned || triggerDebugMode || triggerBreakpoint) { 240 // NOTE: when addrAligned or trigger fire, do not need to wait tlb actually 241 // check for miss aligned exceptions, tlb exception are checked next cycle for timing 242 // if there are exceptions, no need to execute it 243 state := s_finish 244 out_valid := true.B 245 atom_override_xtval := true.B 246 } .otherwise { 247 state := s_pm 248 } 249 } 250 } 251 } 252 253 when (state === s_pm) { 254 val pmp = WireInit(io.pmpResp) 255 is_mmio := pmp.mmio 256 257 // NOTE: only handle load/store exception here, if other exception happens, don't send here 258 val exception_va = exceptionVec(storePageFault) || exceptionVec(loadPageFault) || 259 exceptionVec(storeGuestPageFault) || exceptionVec(loadGuestPageFault) || 260 exceptionVec(storeAccessFault) || exceptionVec(loadAccessFault) 261 val exception_pa = pmp.st || pmp.ld || pmp.mmio 262 when (exception_va || exception_pa) { 263 state := s_finish 264 out_valid := true.B 265 atom_override_xtval := true.B 266 }.otherwise { 267 // if sbuffer has been flushed, go to query dcache, otherwise wait for sbuffer. 268 state := Mux(sbuffer_empty, s_cache_req, s_wait_flush_sbuffer_resp); 269 } 270 // update storeAccessFault bit 271 exceptionVec(loadAccessFault) := exceptionVec(loadAccessFault) || (pmp.ld || pmp.mmio) && isLr 272 exceptionVec(storeAccessFault) := exceptionVec(storeAccessFault) || pmp.st || (pmp.ld || pmp.mmio) && !isLr 273 } 274 275 when (state === s_wait_flush_sbuffer_resp) { 276 when (sbuffer_empty) { 277 state := s_cache_req 278 } 279 } 280 281 when (state === s_cache_req) { 282 val pipe_req = io.dcache.req.bits 283 pipe_req := DontCare 284 285 pipe_req.cmd := LookupTree(in.uop.fuOpType, List( 286 LSUOpType.lr_w -> M_XLR, 287 LSUOpType.sc_w -> M_XSC, 288 LSUOpType.amoswap_w -> M_XA_SWAP, 289 LSUOpType.amoadd_w -> M_XA_ADD, 290 LSUOpType.amoxor_w -> M_XA_XOR, 291 LSUOpType.amoand_w -> M_XA_AND, 292 LSUOpType.amoor_w -> M_XA_OR, 293 LSUOpType.amomin_w -> M_XA_MIN, 294 LSUOpType.amomax_w -> M_XA_MAX, 295 LSUOpType.amominu_w -> M_XA_MINU, 296 LSUOpType.amomaxu_w -> M_XA_MAXU, 297 298 LSUOpType.lr_d -> M_XLR, 299 LSUOpType.sc_d -> M_XSC, 300 LSUOpType.amoswap_d -> M_XA_SWAP, 301 LSUOpType.amoadd_d -> M_XA_ADD, 302 LSUOpType.amoxor_d -> M_XA_XOR, 303 LSUOpType.amoand_d -> M_XA_AND, 304 LSUOpType.amoor_d -> M_XA_OR, 305 LSUOpType.amomin_d -> M_XA_MIN, 306 LSUOpType.amomax_d -> M_XA_MAX, 307 LSUOpType.amominu_d -> M_XA_MINU, 308 LSUOpType.amomaxu_d -> M_XA_MAXU 309 )) 310 pipe_req.miss := false.B 311 pipe_req.probe := false.B 312 pipe_req.probe_need_data := false.B 313 pipe_req.source := AMO_SOURCE.U 314 pipe_req.addr := get_block_addr(paddr) 315 pipe_req.vaddr := get_block_addr(in.src(0)) // vaddr 316 pipe_req.word_idx := get_word(paddr) 317 pipe_req.amo_data := genWdata(in.src(1), in.uop.fuOpType(1,0)) 318 pipe_req.amo_mask := genWmask(paddr, in.uop.fuOpType(1,0)) 319 320 io.dcache.req.valid := Mux( 321 io.dcache.req.bits.cmd === M_XLR, 322 !io.dcache.block_lr, // block lr to survive in lr storm 323 data_valid // wait until src(1) is ready 324 ) 325 326 when(io.dcache.req.fire){ 327 state := s_cache_resp 328 paddr_reg := paddr 329 data_reg := io.dcache.req.bits.amo_data 330 mask_reg := io.dcache.req.bits.amo_mask 331 fuop_reg := in.uop.fuOpType 332 } 333 } 334 335 val dcache_resp_data = Reg(UInt()) 336 val dcache_resp_id = Reg(UInt()) 337 val dcache_resp_error = Reg(Bool()) 338 339 when (state === s_cache_resp) { 340 // when not miss 341 // everything is OK, simply send response back to sbuffer 342 // when miss and not replay 343 // wait for missQueue to handling miss and replaying our request 344 // when miss and replay 345 // req missed and fail to enter missQueue, manually replay it later 346 // TODO: add assertions: 347 // 1. add a replay delay counter? 348 // 2. when req gets into MissQueue, it should not miss any more 349 when(io.dcache.resp.fire) { 350 when(io.dcache.resp.bits.miss) { 351 when(io.dcache.resp.bits.replay) { 352 state := s_cache_req 353 } 354 } .otherwise { 355 dcache_resp_data := io.dcache.resp.bits.data 356 dcache_resp_id := io.dcache.resp.bits.id 357 dcache_resp_error := io.dcache.resp.bits.error 358 state := s_cache_resp_latch 359 } 360 } 361 } 362 363 when (state === s_cache_resp_latch) { 364 is_lrsc_valid := dcache_resp_id 365 val rdataSel = LookupTree(paddr(2, 0), List( 366 "b000".U -> dcache_resp_data(63, 0), 367 "b001".U -> dcache_resp_data(63, 8), 368 "b010".U -> dcache_resp_data(63, 16), 369 "b011".U -> dcache_resp_data(63, 24), 370 "b100".U -> dcache_resp_data(63, 32), 371 "b101".U -> dcache_resp_data(63, 40), 372 "b110".U -> dcache_resp_data(63, 48), 373 "b111".U -> dcache_resp_data(63, 56) 374 )) 375 376 resp_data_wire := LookupTree(in.uop.fuOpType, List( 377 LSUOpType.lr_w -> SignExt(rdataSel(31, 0), XLEN), 378 LSUOpType.sc_w -> dcache_resp_data, 379 LSUOpType.amoswap_w -> SignExt(rdataSel(31, 0), XLEN), 380 LSUOpType.amoadd_w -> SignExt(rdataSel(31, 0), XLEN), 381 LSUOpType.amoxor_w -> SignExt(rdataSel(31, 0), XLEN), 382 LSUOpType.amoand_w -> SignExt(rdataSel(31, 0), XLEN), 383 LSUOpType.amoor_w -> SignExt(rdataSel(31, 0), XLEN), 384 LSUOpType.amomin_w -> SignExt(rdataSel(31, 0), XLEN), 385 LSUOpType.amomax_w -> SignExt(rdataSel(31, 0), XLEN), 386 LSUOpType.amominu_w -> SignExt(rdataSel(31, 0), XLEN), 387 LSUOpType.amomaxu_w -> SignExt(rdataSel(31, 0), XLEN), 388 389 LSUOpType.lr_d -> SignExt(rdataSel(63, 0), XLEN), 390 LSUOpType.sc_d -> dcache_resp_data, 391 LSUOpType.amoswap_d -> SignExt(rdataSel(63, 0), XLEN), 392 LSUOpType.amoadd_d -> SignExt(rdataSel(63, 0), XLEN), 393 LSUOpType.amoxor_d -> SignExt(rdataSel(63, 0), XLEN), 394 LSUOpType.amoand_d -> SignExt(rdataSel(63, 0), XLEN), 395 LSUOpType.amoor_d -> SignExt(rdataSel(63, 0), XLEN), 396 LSUOpType.amomin_d -> SignExt(rdataSel(63, 0), XLEN), 397 LSUOpType.amomax_d -> SignExt(rdataSel(63, 0), XLEN), 398 LSUOpType.amominu_d -> SignExt(rdataSel(63, 0), XLEN), 399 LSUOpType.amomaxu_d -> SignExt(rdataSel(63, 0), XLEN) 400 )) 401 402 when (dcache_resp_error && io.csrCtrl.cache_error_enable) { 403 exceptionVec(loadAccessFault) := isLr 404 exceptionVec(storeAccessFault) := !isLr 405 assert(!exceptionVec(loadAccessFault)) 406 assert(!exceptionVec(storeAccessFault)) 407 } 408 409 resp_data := resp_data_wire 410 state := s_finish 411 out_valid := true.B 412 } 413 414 io.out.valid := out_valid 415 XSError((state === s_finish) =/= out_valid, "out_valid reg error\n") 416 io.out.bits := DontCare 417 io.out.bits.uop := in.uop 418 io.out.bits.uop.exceptionVec := exceptionVec 419 io.out.bits.uop.trigger := trigger 420 io.out.bits.data := resp_data 421 io.out.bits.debug.isMMIO := is_mmio 422 io.out.bits.debug.paddr := paddr 423 when (io.out.fire) { 424 XSDebug("atomics writeback: pc %x data %x\n", io.out.bits.uop.pc, io.dcache.resp.bits.data) 425 state := s_invalid 426 out_valid := false.B 427 } 428 429 when (state === s_finish) { 430 data_valid := false.B 431 } 432 433 when (io.redirect.valid) { 434 atom_override_xtval := false.B 435 } 436 437 if (env.EnableDifftest) { 438 val difftest = DifftestModule(new DiffAtomicEvent) 439 difftest.coreid := io.hartId 440 difftest.valid := state === s_cache_resp_latch 441 difftest.addr := paddr_reg 442 difftest.data := data_reg 443 difftest.mask := mask_reg 444 difftest.fuop := fuop_reg 445 difftest.out := resp_data_wire 446 } 447 448 if (env.EnableDifftest || env.AlwaysBasicDiff) { 449 val uop = io.out.bits.uop 450 val difftest = DifftestModule(new DiffLrScEvent) 451 difftest.coreid := io.hartId 452 difftest.valid := io.out.fire && 453 (uop.fuOpType === LSUOpType.sc_d || uop.fuOpType === LSUOpType.sc_w) 454 difftest.success := is_lrsc_valid 455 } 456} 457