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.{DCacheWordIOWithVaddr, MemoryOpConstants} 25import xiangshan.cache.mmu.{TlbCmd, TlbRequestIO} 26import difftest._ 27import xiangshan.backend.fu.PMPRespBundle 28 29class AtomicsUnit(implicit p: Parameters) extends XSModule with MemoryOpConstants{ 30 val io = IO(new Bundle() { 31 val in = Flipped(Decoupled(new ExuInput)) 32 val storeDataIn = Flipped(Valid(new StoreDataBundle)) // src2 from rs 33 val out = Decoupled(new ExuOutput) 34 val dcache = new DCacheWordIOWithVaddr 35 val dtlb = new TlbRequestIO 36 val pmpResp = Flipped(new PMPRespBundle()) 37 val rsIdx = Input(UInt(log2Up(IssQueSize).W)) 38 val flush_sbuffer = new SbufferFlushBundle 39 val feedbackSlow = ValidIO(new RSFeedback) 40 val redirect = Flipped(ValidIO(new Redirect)) 41 val exceptionAddr = ValidIO(UInt(VAddrBits.W)) 42 }) 43 44 //------------------------------------------------------- 45 // Atomics Memory Accsess FSM 46 //------------------------------------------------------- 47 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) 48 val state = RegInit(s_invalid) 49 val data_valid = RegInit(false.B) 50 val in = Reg(new ExuInput()) 51 val exceptionVec = RegInit(0.U.asTypeOf(ExceptionVec())) 52 val atom_override_xtval = RegInit(false.B) 53 // paddr after translation 54 val paddr = Reg(UInt()) 55 val is_mmio = Reg(Bool()) 56 // dcache response data 57 val resp_data = Reg(UInt()) 58 val resp_data_wire = WireInit(0.U) 59 val is_lrsc_valid = Reg(Bool()) 60 61 // Difftest signals 62 val paddr_reg = Reg(UInt(64.W)) 63 val data_reg = Reg(UInt(64.W)) 64 val mask_reg = Reg(UInt(8.W)) 65 val fuop_reg = Reg(UInt(8.W)) 66 67 io.exceptionAddr.valid := atom_override_xtval 68 io.exceptionAddr.bits := in.src(0) 69 70 // assign default value to output signals 71 io.in.ready := false.B 72 io.out.valid := false.B 73 io.out.bits := DontCare 74 75 io.dcache.req.valid := false.B 76 io.dcache.req.bits := DontCare 77 io.dcache.resp.ready := false.B 78 79 io.dtlb.req.valid := false.B 80 io.dtlb.req.bits := DontCare 81 io.dtlb.resp.ready := false.B 82 83 io.flush_sbuffer.valid := false.B 84 85 XSDebug("state: %d\n", state) 86 87 when (state === s_invalid) { 88 io.in.ready := true.B 89 when (io.in.fire()) { 90 in := io.in.bits 91 in.src(1) := in.src(1) // leave src2 unchanged 92 state := s_tlb 93 } 94 } 95 96 when (io.storeDataIn.fire()) { 97 in.src(1) := io.storeDataIn.bits.data 98 data_valid := true.B 99 } 100 101 assert(!(io.storeDataIn.fire() && data_valid), "atomic unit re-receive data") 102 103 // Send TLB feedback to store issue queue 104 // we send feedback right after we receives request 105 // also, we always treat amo as tlb hit 106 // since we will continue polling tlb all by ourself 107 io.feedbackSlow.valid := RegNext(RegNext(io.in.valid)) 108 io.feedbackSlow.bits.hit := true.B 109 io.feedbackSlow.bits.rsIdx := RegEnable(io.rsIdx, io.in.valid) 110 io.feedbackSlow.bits.flushState := DontCare 111 io.feedbackSlow.bits.sourceType := DontCare 112 io.feedbackSlow.bits.dataInvalidSqIdx := DontCare 113 114 // tlb translation, manipulating signals && deal with exception 115 when (state === s_tlb) { 116 // send req to dtlb 117 // keep firing until tlb hit 118 io.dtlb.req.valid := true.B 119 io.dtlb.req.bits.vaddr := in.src(0) 120 io.dtlb.req.bits.robIdx := in.uop.robIdx 121 io.dtlb.resp.ready := true.B 122 val is_lr = in.uop.ctrl.fuOpType === LSUOpType.lr_w || in.uop.ctrl.fuOpType === LSUOpType.lr_d 123 io.dtlb.req.bits.cmd := Mux(is_lr, TlbCmd.atom_read, TlbCmd.atom_write) 124 io.dtlb.req.bits.debug.pc := in.uop.cf.pc 125 io.dtlb.req.bits.debug.isFirstIssue := false.B 126 127 when(io.dtlb.resp.fire && !io.dtlb.resp.bits.miss){ 128 // exception handling 129 val addrAligned = LookupTree(in.uop.ctrl.fuOpType(1,0), List( 130 "b00".U -> true.B, //b 131 "b01".U -> (in.src(0)(0) === 0.U), //h 132 "b10".U -> (in.src(0)(1,0) === 0.U), //w 133 "b11".U -> (in.src(0)(2,0) === 0.U) //d 134 )) 135 exceptionVec(storeAddrMisaligned) := !addrAligned 136 exceptionVec(storePageFault) := io.dtlb.resp.bits.excp.pf.st 137 exceptionVec(loadPageFault) := io.dtlb.resp.bits.excp.pf.ld 138 exceptionVec(storeAccessFault) := io.dtlb.resp.bits.excp.af.st 139 exceptionVec(loadAccessFault) := io.dtlb.resp.bits.excp.af.ld 140 val exception = !addrAligned || 141 io.dtlb.resp.bits.excp.pf.st || 142 io.dtlb.resp.bits.excp.pf.ld || 143 io.dtlb.resp.bits.excp.af.st || 144 io.dtlb.resp.bits.excp.af.ld 145 when (exception) { 146 // check for exceptions 147 // if there are exceptions, no need to execute it 148 state := s_finish 149 atom_override_xtval := true.B 150 } .otherwise { 151 paddr := io.dtlb.resp.bits.paddr 152 state := s_pm 153 } 154 } 155 } 156 157 when (state === s_pm) { 158 is_mmio := io.pmpResp.mmio 159 val exception = io.pmpResp.st 160 when (exception) { 161 state := s_finish 162 atom_override_xtval := true.B 163 }.otherwise { 164 state := s_flush_sbuffer_req 165 } 166 } 167 168 when (state === s_flush_sbuffer_req) { 169 io.flush_sbuffer.valid := true.B 170 state := s_flush_sbuffer_resp 171 } 172 173 when (state === s_flush_sbuffer_resp) { 174 when (io.flush_sbuffer.empty) { 175 state := s_cache_req 176 } 177 } 178 179 when (state === s_cache_req) { 180 io.dcache.req.valid := true.B 181 io.dcache.req.bits.cmd := LookupTree(in.uop.ctrl.fuOpType, List( 182 LSUOpType.lr_w -> M_XLR, 183 LSUOpType.sc_w -> M_XSC, 184 LSUOpType.amoswap_w -> M_XA_SWAP, 185 LSUOpType.amoadd_w -> M_XA_ADD, 186 LSUOpType.amoxor_w -> M_XA_XOR, 187 LSUOpType.amoand_w -> M_XA_AND, 188 LSUOpType.amoor_w -> M_XA_OR, 189 LSUOpType.amomin_w -> M_XA_MIN, 190 LSUOpType.amomax_w -> M_XA_MAX, 191 LSUOpType.amominu_w -> M_XA_MINU, 192 LSUOpType.amomaxu_w -> M_XA_MAXU, 193 194 LSUOpType.lr_d -> M_XLR, 195 LSUOpType.sc_d -> M_XSC, 196 LSUOpType.amoswap_d -> M_XA_SWAP, 197 LSUOpType.amoadd_d -> M_XA_ADD, 198 LSUOpType.amoxor_d -> M_XA_XOR, 199 LSUOpType.amoand_d -> M_XA_AND, 200 LSUOpType.amoor_d -> M_XA_OR, 201 LSUOpType.amomin_d -> M_XA_MIN, 202 LSUOpType.amomax_d -> M_XA_MAX, 203 LSUOpType.amominu_d -> M_XA_MINU, 204 LSUOpType.amomaxu_d -> M_XA_MAXU 205 )) 206 207 io.dcache.req.bits.addr := paddr 208 io.dcache.req.bits.vaddr := in.src(0) // vaddr 209 io.dcache.req.bits.data := genWdata(in.src(1), in.uop.ctrl.fuOpType(1,0)) 210 // TODO: atomics do need mask: fix mask 211 io.dcache.req.bits.mask := genWmask(paddr, in.uop.ctrl.fuOpType(1,0)) 212 io.dcache.req.bits.id := DontCare 213 214 when(io.dcache.req.fire()){ 215 state := s_cache_resp 216 paddr_reg := io.dcache.req.bits.addr 217 data_reg := io.dcache.req.bits.data 218 mask_reg := io.dcache.req.bits.mask 219 fuop_reg := in.uop.ctrl.fuOpType 220 } 221 } 222 223 when (state === s_cache_resp) { 224 io.dcache.resp.ready := data_valid 225 when(io.dcache.resp.fire()) { 226 is_lrsc_valid := io.dcache.resp.bits.id 227 val rdata = io.dcache.resp.bits.data 228 val rdataSel = LookupTree(paddr(2, 0), List( 229 "b000".U -> rdata(63, 0), 230 "b001".U -> rdata(63, 8), 231 "b010".U -> rdata(63, 16), 232 "b011".U -> rdata(63, 24), 233 "b100".U -> rdata(63, 32), 234 "b101".U -> rdata(63, 40), 235 "b110".U -> rdata(63, 48), 236 "b111".U -> rdata(63, 56) 237 )) 238 239 resp_data_wire := LookupTree(in.uop.ctrl.fuOpType, List( 240 LSUOpType.lr_w -> SignExt(rdataSel(31, 0), XLEN), 241 LSUOpType.sc_w -> rdata, 242 LSUOpType.amoswap_w -> SignExt(rdataSel(31, 0), XLEN), 243 LSUOpType.amoadd_w -> SignExt(rdataSel(31, 0), XLEN), 244 LSUOpType.amoxor_w -> SignExt(rdataSel(31, 0), XLEN), 245 LSUOpType.amoand_w -> SignExt(rdataSel(31, 0), XLEN), 246 LSUOpType.amoor_w -> SignExt(rdataSel(31, 0), XLEN), 247 LSUOpType.amomin_w -> SignExt(rdataSel(31, 0), XLEN), 248 LSUOpType.amomax_w -> SignExt(rdataSel(31, 0), XLEN), 249 LSUOpType.amominu_w -> SignExt(rdataSel(31, 0), XLEN), 250 LSUOpType.amomaxu_w -> SignExt(rdataSel(31, 0), XLEN), 251 252 LSUOpType.lr_d -> SignExt(rdataSel(63, 0), XLEN), 253 LSUOpType.sc_d -> rdata, 254 LSUOpType.amoswap_d -> SignExt(rdataSel(63, 0), XLEN), 255 LSUOpType.amoadd_d -> SignExt(rdataSel(63, 0), XLEN), 256 LSUOpType.amoxor_d -> SignExt(rdataSel(63, 0), XLEN), 257 LSUOpType.amoand_d -> SignExt(rdataSel(63, 0), XLEN), 258 LSUOpType.amoor_d -> SignExt(rdataSel(63, 0), XLEN), 259 LSUOpType.amomin_d -> SignExt(rdataSel(63, 0), XLEN), 260 LSUOpType.amomax_d -> SignExt(rdataSel(63, 0), XLEN), 261 LSUOpType.amominu_d -> SignExt(rdataSel(63, 0), XLEN), 262 LSUOpType.amomaxu_d -> SignExt(rdataSel(63, 0), XLEN) 263 )) 264 265 resp_data := resp_data_wire 266 state := s_finish 267 } 268 } 269 270 when (state === s_finish) { 271 io.out.valid := true.B 272 io.out.bits.uop := in.uop 273 io.out.bits.uop.cf.exceptionVec := exceptionVec 274 io.out.bits.uop.diffTestDebugLrScValid := is_lrsc_valid 275 io.out.bits.data := resp_data 276 io.out.bits.redirectValid := false.B 277 io.out.bits.redirect := DontCare 278 io.out.bits.debug.isMMIO := is_mmio 279 io.out.bits.debug.paddr := paddr 280 when (io.out.fire()) { 281 XSDebug("atomics writeback: pc %x data %x\n", io.out.bits.uop.cf.pc, io.dcache.resp.bits.data) 282 state := s_invalid 283 } 284 data_valid := false.B 285 } 286 287 when (io.redirect.valid) { 288 atom_override_xtval := false.B 289 } 290 291 if (!env.FPGAPlatform) { 292 val difftest = Module(new DifftestAtomicEvent) 293 difftest.io.clock := clock 294 difftest.io.coreid := hardId.U 295 difftest.io.atomicResp := io.dcache.resp.fire() 296 difftest.io.atomicAddr := paddr_reg 297 difftest.io.atomicData := data_reg 298 difftest.io.atomicMask := mask_reg 299 difftest.io.atomicFuop := fuop_reg 300 difftest.io.atomicOut := resp_data_wire 301 } 302} 303