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 chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import xiangshan._ 24import xiangshan.cache.{AtomicWordIO, MemoryOpConstants} 25import xiangshan.cache.mmu.{TlbCmd, TlbRequestIO} 26import difftest._ 27import xiangshan.ExceptionNO._ 28import xiangshan.backend.fu.PMPRespBundle 29 30class AtomicsUnit(implicit p: Parameters) extends XSModule with MemoryOpConstants{ 31 val io = IO(new Bundle() { 32 val hartId = Input(UInt(8.W)) 33 val in = Flipped(Decoupled(new ExuInput)) 34 val storeDataIn = Flipped(Valid(new ExuOutput)) // src2 from rs 35 val out = Decoupled(new ExuOutput) 36 val dcache = new AtomicWordIO 37 val dtlb = new TlbRequestIO 38 val pmpResp = Flipped(new PMPRespBundle()) 39 val rsIdx = Input(UInt(log2Up(IssQueSize).W)) 40 val flush_sbuffer = new SbufferFlushBundle 41 val feedbackSlow = ValidIO(new RSFeedback) 42 val redirect = Flipped(ValidIO(new Redirect)) 43 val exceptionAddr = ValidIO(UInt(VAddrBits.W)) 44 val csrCtrl = Flipped(new CustomCSRCtrlIO) 45 }) 46 47 //------------------------------------------------------- 48 // Atomics Memory Accsess FSM 49 //------------------------------------------------------- 50 val s_invalid :: s_tlb :: s_pm :: s_flush_sbuffer_req :: s_flush_sbuffer_resp :: s_cache_req :: s_cache_resp :: s_finish :: Nil = Enum(8) 51 val state = RegInit(s_invalid) 52 val out_valid = RegInit(false.B) 53 val data_valid = RegInit(false.B) 54 val in = Reg(new ExuInput()) 55 val exceptionVec = RegInit(0.U.asTypeOf(ExceptionVec())) 56 val atom_override_xtval = RegInit(false.B) 57 val isLr = in.uop.ctrl.fuOpType === LSUOpType.lr_w || in.uop.ctrl.fuOpType === LSUOpType.lr_d 58 // paddr after translation 59 val paddr = Reg(UInt()) 60 val vaddr = in.src(0) 61 val is_mmio = Reg(Bool()) 62 // pmp check 63 val static_pm = Reg(Valid(Bool())) // valid for static, bits for mmio 64 // dcache response data 65 val resp_data = Reg(UInt()) 66 val resp_data_wire = WireInit(0.U) 67 val is_lrsc_valid = Reg(Bool()) 68 69 70 // Difftest signals 71 val paddr_reg = Reg(UInt(64.W)) 72 val data_reg = Reg(UInt(64.W)) 73 val mask_reg = Reg(UInt(8.W)) 74 val fuop_reg = Reg(UInt(8.W)) 75 76 io.exceptionAddr.valid := atom_override_xtval 77 io.exceptionAddr.bits := in.src(0) 78 79 // assign default value to output signals 80 io.in.ready := false.B 81 82 io.dcache.req.valid := false.B 83 io.dcache.req.bits := DontCare 84 io.dcache.resp.ready := false.B 85 86 io.dtlb.req.valid := false.B 87 io.dtlb.req.bits := DontCare 88 io.dtlb.req_kill := false.B 89 io.dtlb.resp.ready := true.B 90 91 io.flush_sbuffer.valid := false.B 92 93 XSDebug("state: %d\n", state) 94 95 when (state === s_invalid) { 96 io.in.ready := true.B 97 when (io.in.fire) { 98 in := io.in.bits 99 in.src(1) := in.src(1) // leave src2 unchanged 100 state := s_tlb 101 } 102 } 103 104 when (io.storeDataIn.fire) { 105 in.src(1) := io.storeDataIn.bits.data 106 data_valid := true.B 107 } 108 109 assert(!(io.storeDataIn.fire && data_valid), "atomic unit re-receive data") 110 111 // Send TLB feedback to store issue queue 112 // we send feedback right after we receives request 113 // also, we always treat amo as tlb hit 114 // since we will continue polling tlb all by ourself 115 io.feedbackSlow.valid := RegNext(RegNext(io.in.valid)) 116 io.feedbackSlow.bits.hit := true.B 117 io.feedbackSlow.bits.rsIdx := RegEnable(io.rsIdx, io.in.valid) 118 io.feedbackSlow.bits.flushState := DontCare 119 io.feedbackSlow.bits.sourceType := DontCare 120 io.feedbackSlow.bits.dataInvalidSqIdx := DontCare 121 122 // tlb translation, manipulating signals && deal with exception 123 when (state === s_tlb) { 124 // send req to dtlb 125 // keep firing until tlb hit 126 io.dtlb.req.valid := true.B 127 io.dtlb.req.bits.vaddr := in.src(0) 128 val is_lr = in.uop.ctrl.fuOpType === LSUOpType.lr_w || in.uop.ctrl.fuOpType === LSUOpType.lr_d 129 io.dtlb.req.bits.cmd := Mux(is_lr, TlbCmd.atom_read, TlbCmd.atom_write) 130 io.dtlb.req.bits.debug.robIdx := in.uop.robIdx 131 io.dtlb.req.bits.debug.pc := in.uop.cf.pc 132 io.dtlb.req.bits.debug.isFirstIssue := false.B 133 134 when(io.dtlb.resp.fire){ 135 paddr := io.dtlb.resp.bits.paddr 136 // exception handling 137 val addrAligned = LookupTree(in.uop.ctrl.fuOpType(1,0), List( 138 "b00".U -> true.B, //b 139 "b01".U -> (in.src(0)(0) === 0.U), //h 140 "b10".U -> (in.src(0)(1,0) === 0.U), //w 141 "b11".U -> (in.src(0)(2,0) === 0.U) //d 142 )) 143 exceptionVec(storeAddrMisaligned) := !addrAligned 144 exceptionVec(storePageFault) := io.dtlb.resp.bits.excp.pf.st 145 exceptionVec(loadPageFault) := io.dtlb.resp.bits.excp.pf.ld 146 exceptionVec(storeAccessFault) := io.dtlb.resp.bits.excp.af.st 147 exceptionVec(loadAccessFault) := io.dtlb.resp.bits.excp.af.ld 148 static_pm := io.dtlb.resp.bits.static_pm 149 150 when (!io.dtlb.resp.bits.miss) { 151 when (!addrAligned) { 152 // NOTE: when addrAligned, do not need to wait tlb actually 153 // check for miss aligned exceptions, tlb exception are checked next cycle for timing 154 // if there are exceptions, no need to execute it 155 state := s_finish 156 out_valid := true.B 157 atom_override_xtval := true.B 158 } .otherwise { 159 state := s_pm 160 } 161 } 162 } 163 } 164 165 when (state === s_pm) { 166 val pmp = WireInit(io.pmpResp) 167 when (static_pm.valid) { 168 pmp.ld := false.B 169 pmp.st := false.B 170 pmp.instr := false.B 171 pmp.mmio := static_pm.bits 172 } 173 is_mmio := pmp.mmio 174 // NOTE: only handle load/store exception here, if other exception happens, don't send here 175 val exception_va = exceptionVec(storePageFault) || exceptionVec(loadPageFault) || 176 exceptionVec(storeAccessFault) || exceptionVec(loadAccessFault) 177 val exception_pa = pmp.st 178 when (exception_va || exception_pa) { 179 state := s_finish 180 out_valid := true.B 181 atom_override_xtval := true.B 182 }.otherwise { 183 state := s_flush_sbuffer_req 184 } 185 } 186 187 when (state === s_flush_sbuffer_req) { 188 io.flush_sbuffer.valid := true.B 189 state := s_flush_sbuffer_resp 190 } 191 192 when (state === s_flush_sbuffer_resp) { 193 when (io.flush_sbuffer.empty) { 194 state := s_cache_req 195 } 196 } 197 198 when (state === s_cache_req) { 199 io.dcache.req.valid := true.B 200 io.dcache.req.bits.cmd := LookupTree(in.uop.ctrl.fuOpType, List( 201 LSUOpType.lr_w -> M_XLR, 202 LSUOpType.sc_w -> M_XSC, 203 LSUOpType.amoswap_w -> M_XA_SWAP, 204 LSUOpType.amoadd_w -> M_XA_ADD, 205 LSUOpType.amoxor_w -> M_XA_XOR, 206 LSUOpType.amoand_w -> M_XA_AND, 207 LSUOpType.amoor_w -> M_XA_OR, 208 LSUOpType.amomin_w -> M_XA_MIN, 209 LSUOpType.amomax_w -> M_XA_MAX, 210 LSUOpType.amominu_w -> M_XA_MINU, 211 LSUOpType.amomaxu_w -> M_XA_MAXU, 212 213 LSUOpType.lr_d -> M_XLR, 214 LSUOpType.sc_d -> M_XSC, 215 LSUOpType.amoswap_d -> M_XA_SWAP, 216 LSUOpType.amoadd_d -> M_XA_ADD, 217 LSUOpType.amoxor_d -> M_XA_XOR, 218 LSUOpType.amoand_d -> M_XA_AND, 219 LSUOpType.amoor_d -> M_XA_OR, 220 LSUOpType.amomin_d -> M_XA_MIN, 221 LSUOpType.amomax_d -> M_XA_MAX, 222 LSUOpType.amominu_d -> M_XA_MINU, 223 LSUOpType.amomaxu_d -> M_XA_MAXU 224 )) 225 226 io.dcache.req.bits.addr := paddr 227 io.dcache.req.bits.vaddr := in.src(0) // vaddr 228 io.dcache.req.bits.data := genWdata(in.src(1), in.uop.ctrl.fuOpType(1,0)) 229 // TODO: atomics do need mask: fix mask 230 io.dcache.req.bits.mask := genWmask(paddr, in.uop.ctrl.fuOpType(1,0)) 231 io.dcache.req.bits.id := DontCare 232 233 when(io.dcache.req.fire){ 234 state := s_cache_resp 235 paddr_reg := io.dcache.req.bits.addr 236 data_reg := io.dcache.req.bits.data 237 mask_reg := io.dcache.req.bits.mask 238 fuop_reg := in.uop.ctrl.fuOpType 239 } 240 } 241 242 when (state === s_cache_resp) { 243 io.dcache.resp.ready := data_valid 244 when(io.dcache.resp.fire) { 245 is_lrsc_valid := io.dcache.resp.bits.id 246 val rdata = io.dcache.resp.bits.data 247 val rdataSel = LookupTree(paddr(2, 0), List( 248 "b000".U -> rdata(63, 0), 249 "b001".U -> rdata(63, 8), 250 "b010".U -> rdata(63, 16), 251 "b011".U -> rdata(63, 24), 252 "b100".U -> rdata(63, 32), 253 "b101".U -> rdata(63, 40), 254 "b110".U -> rdata(63, 48), 255 "b111".U -> rdata(63, 56) 256 )) 257 258 resp_data_wire := LookupTree(in.uop.ctrl.fuOpType, List( 259 LSUOpType.lr_w -> SignExt(rdataSel(31, 0), XLEN), 260 LSUOpType.sc_w -> rdata, 261 LSUOpType.amoswap_w -> SignExt(rdataSel(31, 0), XLEN), 262 LSUOpType.amoadd_w -> SignExt(rdataSel(31, 0), XLEN), 263 LSUOpType.amoxor_w -> SignExt(rdataSel(31, 0), XLEN), 264 LSUOpType.amoand_w -> SignExt(rdataSel(31, 0), XLEN), 265 LSUOpType.amoor_w -> SignExt(rdataSel(31, 0), XLEN), 266 LSUOpType.amomin_w -> SignExt(rdataSel(31, 0), XLEN), 267 LSUOpType.amomax_w -> SignExt(rdataSel(31, 0), XLEN), 268 LSUOpType.amominu_w -> SignExt(rdataSel(31, 0), XLEN), 269 LSUOpType.amomaxu_w -> SignExt(rdataSel(31, 0), XLEN), 270 271 LSUOpType.lr_d -> SignExt(rdataSel(63, 0), XLEN), 272 LSUOpType.sc_d -> rdata, 273 LSUOpType.amoswap_d -> SignExt(rdataSel(63, 0), XLEN), 274 LSUOpType.amoadd_d -> SignExt(rdataSel(63, 0), XLEN), 275 LSUOpType.amoxor_d -> SignExt(rdataSel(63, 0), XLEN), 276 LSUOpType.amoand_d -> SignExt(rdataSel(63, 0), XLEN), 277 LSUOpType.amoor_d -> SignExt(rdataSel(63, 0), XLEN), 278 LSUOpType.amomin_d -> SignExt(rdataSel(63, 0), XLEN), 279 LSUOpType.amomax_d -> SignExt(rdataSel(63, 0), XLEN), 280 LSUOpType.amominu_d -> SignExt(rdataSel(63, 0), XLEN), 281 LSUOpType.amomaxu_d -> SignExt(rdataSel(63, 0), XLEN) 282 )) 283 284 when (io.dcache.resp.bits.error && io.csrCtrl.cache_error_enable) { 285 exceptionVec(loadAccessFault) := isLr 286 exceptionVec(storeAccessFault) := !isLr 287 assert(!exceptionVec(loadAccessFault)) 288 assert(!exceptionVec(storeAccessFault)) 289 } 290 291 resp_data := resp_data_wire 292 state := s_finish 293 out_valid := true.B 294 } 295 } 296 297 io.out.valid := out_valid 298 XSError((state === s_finish) =/= out_valid, "out_valid reg error\n") 299 io.out.bits := DontCare 300 io.out.bits.uop := in.uop 301 io.out.bits.uop.cf.exceptionVec := exceptionVec 302 io.out.bits.data := resp_data 303 io.out.bits.redirectValid := false.B 304 io.out.bits.debug.isMMIO := is_mmio 305 io.out.bits.debug.paddr := paddr 306 when (io.out.fire) { 307 XSDebug("atomics writeback: pc %x data %x\n", io.out.bits.uop.cf.pc, io.dcache.resp.bits.data) 308 state := s_invalid 309 out_valid := false.B 310 } 311 312 when (state === s_finish) { 313 data_valid := false.B 314 } 315 316 when (io.redirect.valid) { 317 atom_override_xtval := false.B 318 } 319 320 // atomic trigger 321 val csrCtrl = io.csrCtrl 322 val tdata = Reg(Vec(6, new MatchTriggerIO)) 323 val tEnable = RegInit(VecInit(Seq.fill(6)(false.B))) 324 val en = csrCtrl.trigger_enable 325 tEnable := VecInit(en(2), en (3), en(7), en(4), en(5), en(9)) 326 when(csrCtrl.mem_trigger.t.valid) { 327 tdata(csrCtrl.mem_trigger.t.bits.addr) := csrCtrl.mem_trigger.t.bits.tdata 328 } 329 val lTriggerMapping = Map(0 -> 2, 1 -> 3, 2 -> 5) 330 val sTriggerMapping = Map(0 -> 0, 1 -> 1, 2 -> 4) 331 332 val backendTriggerHitReg = Reg(Vec(6, Bool())) 333 backendTriggerHitReg := VecInit(Seq.fill(6)(false.B)) 334 335 when(state === s_cache_req){ 336 // store trigger 337 val store_hit = Wire(Vec(3, Bool())) 338 for (j <- 0 until 3) { 339 store_hit(j) := !tdata(sTriggerMapping(j)).select && TriggerCmp( 340 vaddr, 341 tdata(sTriggerMapping(j)).tdata2, 342 tdata(sTriggerMapping(j)).matchType, 343 tEnable(sTriggerMapping(j)) 344 ) 345 backendTriggerHitReg(sTriggerMapping(j)) := store_hit(j) 346 } 347 348 when(tdata(0).chain) { 349 backendTriggerHitReg(0) := store_hit(0) && store_hit(1) 350 backendTriggerHitReg(1) := store_hit(0) && store_hit(1) 351 } 352 353 when(!in.uop.cf.trigger.backendEn(0)) { 354 backendTriggerHitReg(4) := false.B 355 } 356 357 // load trigger 358 val load_hit = Wire(Vec(3, Bool())) 359 for (j <- 0 until 3) { 360 361 val addrHit = TriggerCmp( 362 vaddr, 363 tdata(lTriggerMapping(j)).tdata2, 364 tdata(lTriggerMapping(j)).matchType, 365 tEnable(lTriggerMapping(j)) 366 ) 367 load_hit(j) := addrHit && !tdata(lTriggerMapping(j)).select 368 backendTriggerHitReg(lTriggerMapping(j)) := load_hit(j) 369 } 370 when(tdata(2).chain) { 371 backendTriggerHitReg(2) := load_hit(0) && load_hit(1) 372 backendTriggerHitReg(3) := load_hit(0) && load_hit(1) 373 } 374 when(!in.uop.cf.trigger.backendEn(1)) { 375 backendTriggerHitReg(5) := false.B 376 } 377 } 378 379 // addr trigger do cmp at s_cache_req 380 // trigger result is used at s_finish 381 // thus we can delay it safely 382 io.out.bits.uop.cf.trigger.backendHit := VecInit(Seq.fill(6)(false.B)) 383 when(isLr){ 384 // enable load trigger 385 io.out.bits.uop.cf.trigger.backendHit(2) := backendTriggerHitReg(2) 386 io.out.bits.uop.cf.trigger.backendHit(3) := backendTriggerHitReg(3) 387 io.out.bits.uop.cf.trigger.backendHit(5) := backendTriggerHitReg(5) 388 }.otherwise{ 389 // enable store trigger 390 io.out.bits.uop.cf.trigger.backendHit(0) := backendTriggerHitReg(0) 391 io.out.bits.uop.cf.trigger.backendHit(1) := backendTriggerHitReg(1) 392 io.out.bits.uop.cf.trigger.backendHit(4) := backendTriggerHitReg(4) 393 } 394 395 if (env.EnableDifftest) { 396 val difftest = Module(new DifftestAtomicEvent) 397 difftest.io.clock := clock 398 difftest.io.coreid := io.hartId 399 difftest.io.atomicResp := io.dcache.resp.fire 400 difftest.io.atomicAddr := paddr_reg 401 difftest.io.atomicData := data_reg 402 difftest.io.atomicMask := mask_reg 403 difftest.io.atomicFuop := fuop_reg 404 difftest.io.atomicOut := resp_data_wire 405 } 406 407 if (env.EnableDifftest || env.AlwaysBasicDiff) { 408 val uop = io.out.bits.uop 409 val difftest = Module(new DifftestLrScEvent) 410 difftest.io.clock := clock 411 difftest.io.coreid := io.hartId 412 difftest.io.valid := io.out.fire && 413 (uop.ctrl.fuOpType === LSUOpType.sc_d || uop.ctrl.fuOpType === LSUOpType.sc_w) 414 difftest.io.success := is_lrsc_valid 415 } 416} 417