xref: /XiangShan/src/main/scala/xiangshan/mem/pipeline/AtomicsUnit.scala (revision 57bb43b5f11c3f1e89ac52f232fe73056b35d9bd)
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.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 DCacheWordIOWithVaddr
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 data_valid = RegInit(false.B)
53  val in = Reg(new ExuInput())
54  val exceptionVec = RegInit(0.U.asTypeOf(ExceptionVec()))
55  val atom_override_xtval = RegInit(false.B)
56  val isLr = in.uop.ctrl.fuOpType === LSUOpType.lr_w || in.uop.ctrl.fuOpType === LSUOpType.lr_d
57  // paddr after translation
58  val paddr = Reg(UInt())
59  val vaddr = in.src(0)
60  val is_mmio = Reg(Bool())
61  // pmp check
62  val static_pm = Reg(Valid(Bool())) // valid for static, bits for mmio
63  // dcache response data
64  val resp_data = Reg(UInt())
65  val resp_data_wire = WireInit(0.U)
66  val is_lrsc_valid = Reg(Bool())
67
68
69  // Difftest signals
70  val paddr_reg = Reg(UInt(64.W))
71  val data_reg = Reg(UInt(64.W))
72  val mask_reg = Reg(UInt(8.W))
73  val fuop_reg = Reg(UInt(8.W))
74
75  io.exceptionAddr.valid := atom_override_xtval
76  io.exceptionAddr.bits  := in.src(0)
77
78  // assign default value to output signals
79  io.in.ready          := false.B
80  io.out.valid         := false.B
81  io.out.bits          := DontCare
82
83  io.dcache.req.valid  := false.B
84  io.dcache.req.bits   := DontCare
85  io.dcache.resp.ready := false.B
86
87  io.dtlb.req.valid    := false.B
88  io.dtlb.req.bits     := DontCare
89  io.dtlb.resp.ready   := false.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    io.dtlb.req.bits.robIdx := in.uop.robIdx
129    io.dtlb.resp.ready      := true.B
130    val is_lr = in.uop.ctrl.fuOpType === LSUOpType.lr_w || in.uop.ctrl.fuOpType === LSUOpType.lr_d
131    io.dtlb.req.bits.cmd    := Mux(is_lr, TlbCmd.atom_read, TlbCmd.atom_write)
132    io.dtlb.req.bits.debug.pc := in.uop.cf.pc
133    io.dtlb.req.bits.debug.isFirstIssue := false.B
134
135    when(io.dtlb.resp.fire){
136      paddr := io.dtlb.resp.bits.paddr
137      // exception handling
138      val addrAligned = LookupTree(in.uop.ctrl.fuOpType(1,0), List(
139        "b00".U   -> true.B,              //b
140        "b01".U   -> (in.src(0)(0) === 0.U),   //h
141        "b10".U   -> (in.src(0)(1,0) === 0.U), //w
142        "b11".U   -> (in.src(0)(2,0) === 0.U)  //d
143      ))
144      exceptionVec(storeAddrMisaligned) := !addrAligned
145      exceptionVec(storePageFault)      := io.dtlb.resp.bits.excp.pf.st
146      exceptionVec(loadPageFault)       := io.dtlb.resp.bits.excp.pf.ld
147      exceptionVec(storeAccessFault)    := io.dtlb.resp.bits.excp.af.st
148      exceptionVec(loadAccessFault)     := io.dtlb.resp.bits.excp.af.ld
149      static_pm := io.dtlb.resp.bits.static_pm
150
151      when (!io.dtlb.resp.bits.miss) {
152        when (!addrAligned) {
153          // NOTE: when addrAligned, do not need to wait tlb actually
154          // check for miss aligned exceptions, tlb exception are checked next cycle for timing
155          // if there are exceptions, no need to execute it
156          state := s_finish
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      atom_override_xtval := true.B
181    }.otherwise {
182      state := s_flush_sbuffer_req
183    }
184  }
185
186  when (state === s_flush_sbuffer_req) {
187    io.flush_sbuffer.valid := true.B
188    state := s_flush_sbuffer_resp
189  }
190
191  when (state === s_flush_sbuffer_resp) {
192    when (io.flush_sbuffer.empty) {
193      state := s_cache_req
194    }
195  }
196
197  when (state === s_cache_req) {
198    io.dcache.req.valid := true.B
199    io.dcache.req.bits.cmd := LookupTree(in.uop.ctrl.fuOpType, List(
200      LSUOpType.lr_w      -> M_XLR,
201      LSUOpType.sc_w      -> M_XSC,
202      LSUOpType.amoswap_w -> M_XA_SWAP,
203      LSUOpType.amoadd_w  -> M_XA_ADD,
204      LSUOpType.amoxor_w  -> M_XA_XOR,
205      LSUOpType.amoand_w  -> M_XA_AND,
206      LSUOpType.amoor_w   -> M_XA_OR,
207      LSUOpType.amomin_w  -> M_XA_MIN,
208      LSUOpType.amomax_w  -> M_XA_MAX,
209      LSUOpType.amominu_w -> M_XA_MINU,
210      LSUOpType.amomaxu_w -> M_XA_MAXU,
211
212      LSUOpType.lr_d      -> M_XLR,
213      LSUOpType.sc_d      -> M_XSC,
214      LSUOpType.amoswap_d -> M_XA_SWAP,
215      LSUOpType.amoadd_d  -> M_XA_ADD,
216      LSUOpType.amoxor_d  -> M_XA_XOR,
217      LSUOpType.amoand_d  -> M_XA_AND,
218      LSUOpType.amoor_d   -> M_XA_OR,
219      LSUOpType.amomin_d  -> M_XA_MIN,
220      LSUOpType.amomax_d  -> M_XA_MAX,
221      LSUOpType.amominu_d -> M_XA_MINU,
222      LSUOpType.amomaxu_d -> M_XA_MAXU
223    ))
224
225    io.dcache.req.bits.addr := paddr
226    io.dcache.req.bits.vaddr := in.src(0) // vaddr
227    io.dcache.req.bits.data := genWdata(in.src(1), in.uop.ctrl.fuOpType(1,0))
228    // TODO: atomics do need mask: fix mask
229    io.dcache.req.bits.mask := genWmask(paddr, in.uop.ctrl.fuOpType(1,0))
230    io.dcache.req.bits.id   := DontCare
231
232    when(io.dcache.req.fire()){
233      state := s_cache_resp
234      paddr_reg := io.dcache.req.bits.addr
235      data_reg := io.dcache.req.bits.data
236      mask_reg := io.dcache.req.bits.mask
237      fuop_reg := in.uop.ctrl.fuOpType
238    }
239  }
240
241  when (state === s_cache_resp) {
242    io.dcache.resp.ready := data_valid
243    when(io.dcache.resp.fire()) {
244      is_lrsc_valid := io.dcache.resp.bits.id
245      val rdata = io.dcache.resp.bits.data
246      val rdataSel = LookupTree(paddr(2, 0), List(
247        "b000".U -> rdata(63, 0),
248        "b001".U -> rdata(63, 8),
249        "b010".U -> rdata(63, 16),
250        "b011".U -> rdata(63, 24),
251        "b100".U -> rdata(63, 32),
252        "b101".U -> rdata(63, 40),
253        "b110".U -> rdata(63, 48),
254        "b111".U -> rdata(63, 56)
255      ))
256
257      resp_data_wire := LookupTree(in.uop.ctrl.fuOpType, List(
258        LSUOpType.lr_w      -> SignExt(rdataSel(31, 0), XLEN),
259        LSUOpType.sc_w      -> rdata,
260        LSUOpType.amoswap_w -> SignExt(rdataSel(31, 0), XLEN),
261        LSUOpType.amoadd_w  -> SignExt(rdataSel(31, 0), XLEN),
262        LSUOpType.amoxor_w  -> SignExt(rdataSel(31, 0), XLEN),
263        LSUOpType.amoand_w  -> SignExt(rdataSel(31, 0), XLEN),
264        LSUOpType.amoor_w   -> SignExt(rdataSel(31, 0), XLEN),
265        LSUOpType.amomin_w  -> SignExt(rdataSel(31, 0), XLEN),
266        LSUOpType.amomax_w  -> SignExt(rdataSel(31, 0), XLEN),
267        LSUOpType.amominu_w -> SignExt(rdataSel(31, 0), XLEN),
268        LSUOpType.amomaxu_w -> SignExt(rdataSel(31, 0), XLEN),
269
270        LSUOpType.lr_d      -> SignExt(rdataSel(63, 0), XLEN),
271        LSUOpType.sc_d      -> rdata,
272        LSUOpType.amoswap_d -> SignExt(rdataSel(63, 0), XLEN),
273        LSUOpType.amoadd_d  -> SignExt(rdataSel(63, 0), XLEN),
274        LSUOpType.amoxor_d  -> SignExt(rdataSel(63, 0), XLEN),
275        LSUOpType.amoand_d  -> SignExt(rdataSel(63, 0), XLEN),
276        LSUOpType.amoor_d   -> SignExt(rdataSel(63, 0), XLEN),
277        LSUOpType.amomin_d  -> SignExt(rdataSel(63, 0), XLEN),
278        LSUOpType.amomax_d  -> SignExt(rdataSel(63, 0), XLEN),
279        LSUOpType.amominu_d -> SignExt(rdataSel(63, 0), XLEN),
280        LSUOpType.amomaxu_d -> SignExt(rdataSel(63, 0), XLEN)
281      ))
282
283      when (io.dcache.resp.bits.error && io.csrCtrl.cache_error_enable) {
284        exceptionVec(loadAccessFault)  := isLr
285        exceptionVec(storeAccessFault) := !isLr
286        assert(!exceptionVec(loadAccessFault))
287        assert(!exceptionVec(storeAccessFault))
288      }
289
290      resp_data := resp_data_wire
291      state := s_finish
292    }
293  }
294
295  when (state === s_finish) {
296    io.out.valid := true.B
297    io.out.bits.uop := in.uop
298    io.out.bits.uop.cf.exceptionVec := exceptionVec
299    io.out.bits.data := resp_data
300    io.out.bits.redirectValid := false.B
301    io.out.bits.redirect := DontCare
302    io.out.bits.debug.isMMIO := is_mmio
303    io.out.bits.debug.paddr := paddr
304    when (io.out.fire()) {
305      XSDebug("atomics writeback: pc %x data %x\n", io.out.bits.uop.cf.pc, io.dcache.resp.bits.data)
306      state := s_invalid
307    }
308    data_valid := false.B
309  }
310
311  when (io.redirect.valid) {
312    atom_override_xtval := false.B
313  }
314
315  // atomic trigger
316  val csrCtrl = io.csrCtrl
317  val tdata = Reg(Vec(6, new MatchTriggerIO))
318  val tEnable = RegInit(VecInit(Seq.fill(6)(false.B)))
319  val en = csrCtrl.trigger_enable
320  tEnable := VecInit(en(2), en (3), en(7), en(4), en(5), en(9))
321  when(csrCtrl.mem_trigger.t.valid) {
322    tdata(csrCtrl.mem_trigger.t.bits.addr) := csrCtrl.mem_trigger.t.bits.tdata
323  }
324  val lTriggerMapping = Map(0 -> 2, 1 -> 3, 2 -> 5)
325  val sTriggerMapping = Map(0 -> 0, 1 -> 1, 2 -> 4)
326
327  val backendTriggerHitReg = Reg(Vec(6, Bool()))
328  backendTriggerHitReg := VecInit(Seq.fill(6)(false.B))
329
330  when(state === s_cache_req){
331    // store trigger
332    val store_hit = Wire(Vec(3, Bool()))
333    for (j <- 0 until 3) {
334        store_hit(j) := !tdata(sTriggerMapping(j)).select && TriggerCmp(
335          vaddr,
336          tdata(sTriggerMapping(j)).tdata2,
337          tdata(sTriggerMapping(j)).matchType,
338          tEnable(sTriggerMapping(j))
339        )
340       backendTriggerHitReg(sTriggerMapping(j)) := store_hit(j)
341     }
342
343    when(tdata(0).chain) {
344      backendTriggerHitReg(0) := store_hit(0) && store_hit(1)
345      backendTriggerHitReg(1) := store_hit(0) && store_hit(1)
346    }
347
348    when(!in.uop.cf.trigger.backendEn(0)) {
349      backendTriggerHitReg(4) := false.B
350    }
351
352    // load trigger
353    val load_hit = Wire(Vec(3, Bool()))
354    for (j <- 0 until 3) {
355
356      val addrHit = TriggerCmp(
357        vaddr,
358        tdata(lTriggerMapping(j)).tdata2,
359        tdata(lTriggerMapping(j)).matchType,
360        tEnable(lTriggerMapping(j))
361      )
362      load_hit(j) := addrHit && !tdata(lTriggerMapping(j)).select
363      backendTriggerHitReg(lTriggerMapping(j)) := load_hit(j)
364    }
365    when(tdata(2).chain) {
366      backendTriggerHitReg(2) := load_hit(0) && load_hit(1)
367      backendTriggerHitReg(3) := load_hit(0) && load_hit(1)
368    }
369    when(!in.uop.cf.trigger.backendEn(1)) {
370      backendTriggerHitReg(5) := false.B
371    }
372  }
373
374  // addr trigger do cmp at s_cache_req
375  // trigger result is used at s_finish
376  // thus we can delay it safely
377  io.out.bits.uop.cf.trigger.backendHit := VecInit(Seq.fill(6)(false.B))
378  when(isLr){
379    // enable load trigger
380    io.out.bits.uop.cf.trigger.backendHit(2) := backendTriggerHitReg(2)
381    io.out.bits.uop.cf.trigger.backendHit(3) := backendTriggerHitReg(3)
382    io.out.bits.uop.cf.trigger.backendHit(5) := backendTriggerHitReg(5)
383  }.otherwise{
384    // enable store trigger
385    io.out.bits.uop.cf.trigger.backendHit(0) := backendTriggerHitReg(0)
386    io.out.bits.uop.cf.trigger.backendHit(1) := backendTriggerHitReg(1)
387    io.out.bits.uop.cf.trigger.backendHit(4) := backendTriggerHitReg(4)
388  }
389
390  if (env.EnableDifftest) {
391    val difftest = Module(new DifftestAtomicEvent)
392    difftest.io.clock      := clock
393    difftest.io.coreid     := io.hartId
394    difftest.io.atomicResp := io.dcache.resp.fire()
395    difftest.io.atomicAddr := paddr_reg
396    difftest.io.atomicData := data_reg
397    difftest.io.atomicMask := mask_reg
398    difftest.io.atomicFuop := fuop_reg
399    difftest.io.atomicOut  := resp_data_wire
400  }
401
402  if (env.EnableDifftest || env.AlwaysBasicDiff) {
403    val uop = io.out.bits.uop
404    val difftest = Module(new DifftestLrScEvent)
405    difftest.io.clock := clock
406    difftest.io.coreid := io.hartId
407    difftest.io.valid := io.out.fire &&
408      (uop.ctrl.fuOpType === LSUOpType.sc_d || uop.ctrl.fuOpType === LSUOpType.sc_w)
409    difftest.io.success := is_lrsc_valid
410  }
411}
412