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 utility._ 24import xiangshan.ExceptionNO._ 25import xiangshan._ 26import xiangshan.backend.fu.PMPRespBundle 27import xiangshan.backend.rob.DebugLsInfoBundle 28import xiangshan.cache.mmu.{TlbCmd, TlbReq, TlbRequestIO, TlbResp} 29 30class StoreUnit(implicit p: Parameters) extends XSModule { 31 val io = IO(new Bundle() { 32 val redirect = Flipped(ValidIO(new Redirect)) 33 val stin = Flipped(Decoupled(new ExuInput)) 34 val issue = Valid(new ExuInput) 35 val tlb = new TlbRequestIO() 36 val pmp = Flipped(new PMPRespBundle()) 37 val rsIdx = Input(UInt(log2Up(IssQueSize).W)) 38 val isFirstIssue = Input(Bool()) 39 val lsq = ValidIO(new LsPipelineBundle) 40 val lsq_replenish = Output(new LsPipelineBundle()) 41 val feedback_slow = ValidIO(new RSFeedback) 42 val stld_nuke_query = Valid(new StoreNukeQueryIO) 43 val stout = DecoupledIO(new ExuOutput) // writeback store 44 // store mask, send to sq in store_s0 45 val st_mask_out = Valid(new StoreMaskBundle) 46 val debug_ls = Output(new DebugLsInfoBundle) 47 }) 48 49 val s1_ready, s2_ready, s3_ready = WireInit(false.B) 50 51 // Pipeline 52 // -------------------------------------------------------------------------------- 53 // stage 0 54 // -------------------------------------------------------------------------------- 55 // generate addr, use addr to query DCache and DTLB 56 val s0_valid = io.stin.valid 57 val s0_in = io.stin.bits 58 val s0_isFirstIssue = io.isFirstIssue 59 val s0_rsIdx = io.rsIdx 60 val s0_out = Wire(new LsPipelineBundle) 61 val s0_kill = s0_in.uop.robIdx.needFlush(io.redirect) 62 val s0_can_go = s1_ready 63 val s0_fire = s0_valid && !s0_kill && s0_can_go 64 65 // generate addr 66 // val saddr = s0_in.bits.src(0) + SignExt(s0_in.bits.uop.ctrl.imm(11,0), VAddrBits) 67 val imm12 = WireInit(s0_in.uop.ctrl.imm(11,0)) 68 val saddr_lo = s0_in.src(0)(11,0) + Cat(0.U(1.W), imm12) 69 val saddr_hi = Mux(saddr_lo(12), 70 Mux(imm12(11), s0_in.src(0)(VAddrBits-1, 12), s0_in.src(0)(VAddrBits-1, 12)+1.U), 71 Mux(imm12(11), s0_in.src(0)(VAddrBits-1, 12)+SignExt(1.U, VAddrBits-12), s0_in.src(0)(VAddrBits-1, 12)), 72 ) 73 val s0_saddr = Cat(saddr_hi, saddr_lo(11,0)) 74 75 io.tlb.req.valid := s0_valid 76 io.tlb.req.bits.vaddr := s0_saddr 77 io.tlb.req.bits.cmd := TlbCmd.write 78 io.tlb.req.bits.size := LSUOpType.size(s0_in.uop.ctrl.fuOpType) 79 io.tlb.req.bits.kill := DontCare 80 io.tlb.req.bits.memidx.is_ld := false.B 81 io.tlb.req.bits.memidx.is_st := true.B 82 io.tlb.req.bits.memidx.idx := s0_in.uop.sqIdx.value 83 io.tlb.req.bits.debug.robIdx := s0_in.uop.robIdx 84 io.tlb.req.bits.no_translate := false.B 85 io.tlb.req.bits.debug.pc := s0_in.uop.cf.pc 86 io.tlb.req.bits.debug.isFirstIssue := s0_isFirstIssue 87 io.tlb.req_kill := false.B 88 89 s0_out := DontCare 90 s0_out.vaddr := s0_saddr 91 // Now data use its own io 92 // s1_out.data := genWdata(s1_in.src(1), s1_in.uop.ctrl.fuOpType(1,0)) 93 s0_out.data := s0_in.src(1) // FIXME: remove data from pipeline 94 s0_out.uop := s0_in.uop 95 s0_out.miss := DontCare 96 s0_out.rsIdx := s0_rsIdx 97 s0_out.mask := genVWmask(s0_saddr, s0_in.uop.ctrl.fuOpType(1,0)) 98 s0_out.isFirstIssue := s0_isFirstIssue 99 s0_out.isHWPrefetch := false.B // TODO 100 s0_out.wlineflag := s0_in.uop.ctrl.fuOpType === LSUOpType.cbo_zero 101 when(s0_valid && s0_isFirstIssue) { 102 s0_out.uop.debugInfo.tlbFirstReqTime := GTimer() 103 } 104 105 // exception check 106 val s0_addr_aligned = LookupTree(s0_in.uop.ctrl.fuOpType(1,0), List( 107 "b00".U -> true.B, //b 108 "b01".U -> (s0_out.vaddr(0) === 0.U), //h 109 "b10".U -> (s0_out.vaddr(1,0) === 0.U), //w 110 "b11".U -> (s0_out.vaddr(2,0) === 0.U) //d 111 )) 112 s0_out.uop.cf.exceptionVec(storeAddrMisaligned) := !s0_addr_aligned 113 114 io.st_mask_out.valid := s0_valid 115 io.st_mask_out.bits.mask := s0_out.mask 116 io.st_mask_out.bits.sqIdx := s0_out.uop.sqIdx 117 118 io.stin.ready := s1_ready 119 120 // Pipeline 121 // -------------------------------------------------------------------------------- 122 // stage 1 123 // -------------------------------------------------------------------------------- 124 // TLB resp (send paddr to dcache) 125 val s1_valid = RegInit(false.B) 126 val s1_in = RegEnable(s0_out, s0_fire) 127 val s1_out = Wire(new LsPipelineBundle) 128 val s1_kill = Wire(Bool()) 129 val s1_can_go = s2_ready 130 val s1_fire = s1_valid && !s1_kill && s1_can_go 131 132 // mmio cbo decoder 133 val s1_mmio_cbo = s1_in.uop.ctrl.fuOpType === LSUOpType.cbo_clean || 134 s1_in.uop.ctrl.fuOpType === LSUOpType.cbo_flush || 135 s1_in.uop.ctrl.fuOpType === LSUOpType.cbo_inval 136 val s1_paddr = io.tlb.resp.bits.paddr(0) 137 val s1_tlb_miss = io.tlb.resp.bits.miss 138 val s1_mmio = s1_mmio_cbo 139 val s1_exception = ExceptionNO.selectByFu(s1_out.uop.cf.exceptionVec, staCfg).asUInt.orR 140 s1_kill := s1_in.uop.robIdx.needFlush(io.redirect) || s1_tlb_miss 141 142 s1_ready := !s1_valid || s1_kill || s2_ready 143 io.tlb.resp.ready := true.B // TODO: why dtlbResp needs a ready? 144 when (s0_fire) { s1_valid := true.B } 145 .elsewhen (s1_fire) { s1_valid := false.B } 146 .elsewhen (s1_kill) { s1_valid := false.B } 147 148 // st-ld violation dectect request. 149 io.stld_nuke_query.valid := s1_valid && !s1_tlb_miss 150 io.stld_nuke_query.bits.robIdx := s1_in.uop.robIdx 151 io.stld_nuke_query.bits.paddr := s1_paddr 152 io.stld_nuke_query.bits.mask := s1_in.mask 153 154 // issue 155 io.issue.valid := s1_valid && !s1_tlb_miss 156 io.issue.bits := RegEnable(s0_in, s0_valid) 157 158 159 // Send TLB feedback to store issue queue 160 // Store feedback is generated in store_s1, sent to RS in store_s2 161 val s1_feedback = Wire(Valid(new RSFeedback)) 162 s1_feedback.valid := s1_valid & !s1_in.isHWPrefetch 163 s1_feedback.bits.hit := !s1_tlb_miss 164 s1_feedback.bits.flushState := io.tlb.resp.bits.ptwBack 165 s1_feedback.bits.rsIdx := s1_out.rsIdx 166 s1_feedback.bits.sourceType := RSFeedbackType.tlbMiss 167 s1_feedback.bits.dataInvalidSqIdx := DontCare 168 XSDebug(s1_feedback.valid, 169 "S1 Store: tlbHit: %d robIdx: %d\n", 170 s1_feedback.bits.hit, 171 s1_feedback.bits.rsIdx 172 ) 173 174 io.feedback_slow := s1_feedback 175 176 // get paddr from dtlb, check if rollback is needed 177 // writeback store inst to lsq 178 s1_out := s1_in 179 s1_out.paddr := s1_paddr 180 s1_out.miss := false.B 181 s1_out.mmio := s1_mmio 182 s1_out.atomic := s1_mmio 183 s1_out.uop.cf.exceptionVec(storePageFault) := io.tlb.resp.bits.excp(0).pf.st 184 s1_out.uop.cf.exceptionVec(storeAccessFault) := io.tlb.resp.bits.excp(0).af.st 185 186 io.lsq.valid := s1_valid 187 io.lsq.bits := s1_out 188 io.lsq.bits.miss := s1_tlb_miss 189 190 // write below io.out.bits assign sentence to prevent overwriting values 191 val s1_tlb_memidx = io.tlb.resp.bits.memidx 192 when(s1_tlb_memidx.is_st && io.tlb.resp.valid && !s1_tlb_miss && s1_tlb_memidx.idx === s1_out.uop.sqIdx.value) { 193 // printf("Store idx = %d\n", s1_tlb_memidx.idx) 194 s1_out.uop.debugInfo.tlbRespTime := GTimer() 195 } 196 197 // Pipeline 198 // -------------------------------------------------------------------------------- 199 // stage 2 200 // -------------------------------------------------------------------------------- 201 // mmio check 202 val s2_valid = RegInit(false.B) 203 val s2_in = RegEnable(s1_out, s1_fire) 204 val s2_out = Wire(new LsPipelineBundle) 205 val s2_kill = Wire(Bool()) 206 val s2_can_go = s3_ready 207 val s2_fire = s2_valid && !s2_kill && s2_can_go 208 209 s2_ready := !s2_valid || s2_kill || s3_ready 210 when (s1_fire) { s2_valid := true.B } 211 .elsewhen (s2_fire) { s2_valid := false.B } 212 .elsewhen (s2_kill) { s2_valid := false.B } 213 214 val s2_pmp = WireInit(io.pmp) 215 val s2_static_pm = RegNext(io.tlb.resp.bits.static_pm) 216 when (s2_static_pm.valid) { 217 s2_pmp.ld := false.B 218 s2_pmp.st := false.B 219 s2_pmp.instr := false.B 220 s2_pmp.mmio := s2_static_pm.bits 221 } 222 223 val s2_exception = ExceptionNO.selectByFu(s2_out.uop.cf.exceptionVec, staCfg).asUInt.orR 224 val s2_mmio = s2_in.mmio || s2_pmp.mmio 225 s2_kill := (s2_mmio && !s2_exception) || s2_in.uop.robIdx.needFlush(io.redirect) 226 227 s2_out := s2_in 228 s2_out.mmio := s2_mmio && !s2_exception 229 s2_out.atomic := s2_in.atomic || s2_pmp.atomic 230 s2_out.uop.cf.exceptionVec(storeAccessFault) := s2_in.uop.cf.exceptionVec(storeAccessFault) || s2_pmp.st 231 232 // feedback tlb miss to RS in store_s2 233 io.feedback_slow.valid := RegNext(s1_feedback.valid && !s1_out.uop.robIdx.needFlush(io.redirect)) 234 io.feedback_slow.bits := RegNext(s1_feedback.bits) 235 236 // mmio and exception 237 io.lsq_replenish := s2_out 238 239 // Pipeline 240 // -------------------------------------------------------------------------------- 241 // stage 3 242 // -------------------------------------------------------------------------------- 243 // store write back 244 val s3_valid = RegInit(false.B) 245 val s3_in = RegEnable(s2_out, s2_fire) 246 val s3_out = Wire(new ExuOutput) 247 val s3_kill = s3_in.uop.robIdx.needFlush(io.redirect) 248 val s3_can_go = s3_ready 249 val s3_fire = s3_valid && !s3_kill && s3_can_go 250 251 when (s2_fire) { s3_valid := (!s2_mmio || s2_exception) && !s2_out.isHWPrefetch } 252 .elsewhen (s3_fire) { s3_valid := false.B } 253 .elsewhen (s3_kill) { s3_valid := false.B } 254 255 // wb: writeback 256 val SelectGroupSize = RollbackGroupSize 257 val lgSelectGroupSize = log2Ceil(SelectGroupSize) 258 val TotalSelectCycles = scala.math.ceil(log2Ceil(LoadQueueRAWSize).toFloat / lgSelectGroupSize).toInt + 1 259 260 s3_out := DontCare 261 s3_out.uop := s3_in.uop 262 s3_out.data := DontCare 263 s3_out.redirectValid := false.B 264 s3_out.redirect := DontCare 265 s3_out.debug.isMMIO := s3_in.mmio 266 s3_out.debug.paddr := s3_in.paddr 267 s3_out.debug.vaddr := s3_in.vaddr 268 s3_out.debug.isPerfCnt := false.B 269 s3_out.fflags := DontCare 270 271 // Pipeline 272 // -------------------------------------------------------------------------------- 273 // stage x 274 // -------------------------------------------------------------------------------- 275 // delay TotalSelectCycles - 2 cycle(s) 276 val TotalDelayCycles = TotalSelectCycles - 2 277 val sx_valid = Wire(Vec(TotalDelayCycles + 1, Bool())) 278 val sx_ready = Wire(Vec(TotalDelayCycles + 1, Bool())) 279 val sx_in = Wire(Vec(TotalDelayCycles + 1, new ExuOutput)) 280 281 // backward ready signal 282 s3_ready := sx_ready.head 283 for (i <- 0 until TotalDelayCycles + 1) { 284 if (i == 0) { 285 sx_valid(i) := s3_valid 286 sx_in(i) := s3_out 287 sx_ready(i) := !s3_valid(i) || sx_in(i).uop.robIdx.needFlush(io.redirect) || (if (TotalDelayCycles == 0) io.stout.ready else sx_ready(i+1)) 288 } else { 289 val cur_kill = sx_in(i).uop.robIdx.needFlush(io.redirect) 290 val cur_can_go = (if (i == TotalDelayCycles) io.stout.ready else sx_ready(i+1)) 291 val cur_fire = sx_valid(i) && !cur_kill && cur_can_go 292 val prev_fire = sx_valid(i-1) && !sx_in(i-1).uop.robIdx.needFlush(io.redirect) && sx_ready(i) 293 294 sx_ready(i) := !sx_valid(i) || cur_kill || (if (i == TotalDelayCycles) io.stout.ready else sx_ready(i+1)) 295 val sx_valid_can_go = prev_fire || cur_fire || cur_kill 296 sx_valid(i) := RegEnable(Mux(prev_fire, true.B, false.B), sx_valid_can_go) 297 sx_in(i) := RegEnable(sx_in(i-1), prev_fire) 298 } 299 } 300 val sx_last_valid = sx_valid.takeRight(1).head 301 val sx_last_ready = sx_ready.takeRight(1).head 302 val sx_last_in = sx_in.takeRight(1).head 303 sx_last_ready := !sx_last_valid || sx_last_in.uop.robIdx.needFlush(io.redirect) || io.stout.ready 304 305 io.stout.valid := sx_last_valid && !sx_last_in.uop.robIdx.needFlush(io.redirect) 306 io.stout.bits := sx_last_in 307 308 io.debug_ls := DontCare 309 io.debug_ls.s1.isTlbFirstMiss := io.tlb.resp.valid && io.tlb.resp.bits.miss && io.tlb.resp.bits.debug.isFirstIssue 310 io.debug_ls.s1_robIdx := s1_in.uop.robIdx.value 311 312 private def printPipeLine(pipeline: LsPipelineBundle, cond: Bool, name: String): Unit = { 313 XSDebug(cond, 314 p"$name" + p" pc ${Hexadecimal(pipeline.uop.cf.pc)} " + 315 p"addr ${Hexadecimal(pipeline.vaddr)} -> ${Hexadecimal(pipeline.paddr)} " + 316 p"op ${Binary(pipeline.uop.ctrl.fuOpType)} " + 317 p"data ${Hexadecimal(pipeline.data)} " + 318 p"mask ${Hexadecimal(pipeline.mask)}\n" 319 ) 320 } 321 322 printPipeLine(s0_out, s0_valid, "S0") 323 printPipeLine(s1_out, s1_valid, "S1") 324 325 // perf cnt 326 XSPerfAccumulate("s0_in_valid", s0_valid) 327 XSPerfAccumulate("s0_in_fire", s0_fire) 328 XSPerfAccumulate("s0_in_fire_first_issue", s0_fire && s0_isFirstIssue) 329 XSPerfAccumulate("s0_addr_spec_success", s0_fire && s0_saddr(VAddrBits-1, 12) === s0_in.src(0)(VAddrBits-1, 12)) 330 XSPerfAccumulate("s0_addr_spec_failed", s0_fire && s0_saddr(VAddrBits-1, 12) =/= s0_in.src(0)(VAddrBits-1, 12)) 331 XSPerfAccumulate("s0_addr_spec_success_once", s0_fire && s0_saddr(VAddrBits-1, 12) === s0_in.src(0)(VAddrBits-1, 12) && s0_isFirstIssue) 332 XSPerfAccumulate("s0_addr_spec_failed_once", s0_fire && s0_saddr(VAddrBits-1, 12) =/= s0_in.src(0)(VAddrBits-1, 12) && s0_isFirstIssue) 333 334 XSPerfAccumulate("s1_in_valid", s1_valid) 335 XSPerfAccumulate("s1_in_fire", s1_fire) 336 XSPerfAccumulate("s1_in_fire_first_issue", s1_fire && s1_in.isFirstIssue) 337 XSPerfAccumulate("s1_tlb_miss", s1_fire && s1_tlb_miss) 338 XSPerfAccumulate("s1_tlb_miss_first_issue", s1_fire && s1_tlb_miss && s1_in.isFirstIssue) 339 // end 340}