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