xref: /XiangShan/src/main/scala/xiangshan/mem/pipeline/AtomicsUnit.scala (revision dff7ca56cd20b781b172ce6aa5464295e0ac5e41)
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 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   := 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          atom_override_xtval := true.B
157        } .otherwise {
158          state := s_pm
159        }
160      }
161    }
162  }
163
164  when (state === s_pm) {
165    val pmp = WireInit(io.pmpResp)
166    when (static_pm.valid) {
167      pmp.ld := false.B
168      pmp.st := false.B
169      pmp.instr := false.B
170      pmp.mmio := static_pm.bits
171    }
172    is_mmio := pmp.mmio
173    // NOTE: only handle load/store exception here, if other exception happens, don't send here
174    val exception_va = exceptionVec(storePageFault) || exceptionVec(loadPageFault) ||
175      exceptionVec(storeAccessFault) || exceptionVec(loadAccessFault)
176    val exception_pa = pmp.st
177    when (exception_va || exception_pa) {
178      state := s_finish
179      atom_override_xtval := true.B
180    }.otherwise {
181      state := s_flush_sbuffer_req
182    }
183  }
184
185  when (state === s_flush_sbuffer_req) {
186    io.flush_sbuffer.valid := true.B
187    state := s_flush_sbuffer_resp
188  }
189
190  when (state === s_flush_sbuffer_resp) {
191    when (io.flush_sbuffer.empty) {
192      state := s_cache_req
193    }
194  }
195
196  when (state === s_cache_req) {
197    io.dcache.req.valid := true.B
198    io.dcache.req.bits.cmd := LookupTree(in.uop.ctrl.fuOpType, List(
199      LSUOpType.lr_w      -> M_XLR,
200      LSUOpType.sc_w      -> M_XSC,
201      LSUOpType.amoswap_w -> M_XA_SWAP,
202      LSUOpType.amoadd_w  -> M_XA_ADD,
203      LSUOpType.amoxor_w  -> M_XA_XOR,
204      LSUOpType.amoand_w  -> M_XA_AND,
205      LSUOpType.amoor_w   -> M_XA_OR,
206      LSUOpType.amomin_w  -> M_XA_MIN,
207      LSUOpType.amomax_w  -> M_XA_MAX,
208      LSUOpType.amominu_w -> M_XA_MINU,
209      LSUOpType.amomaxu_w -> M_XA_MAXU,
210
211      LSUOpType.lr_d      -> M_XLR,
212      LSUOpType.sc_d      -> M_XSC,
213      LSUOpType.amoswap_d -> M_XA_SWAP,
214      LSUOpType.amoadd_d  -> M_XA_ADD,
215      LSUOpType.amoxor_d  -> M_XA_XOR,
216      LSUOpType.amoand_d  -> M_XA_AND,
217      LSUOpType.amoor_d   -> M_XA_OR,
218      LSUOpType.amomin_d  -> M_XA_MIN,
219      LSUOpType.amomax_d  -> M_XA_MAX,
220      LSUOpType.amominu_d -> M_XA_MINU,
221      LSUOpType.amomaxu_d -> M_XA_MAXU
222    ))
223
224    io.dcache.req.bits.addr := paddr
225    io.dcache.req.bits.vaddr := in.src(0) // vaddr
226    io.dcache.req.bits.data := genWdata(in.src(1), in.uop.ctrl.fuOpType(1,0))
227    // TODO: atomics do need mask: fix mask
228    io.dcache.req.bits.mask := genWmask(paddr, in.uop.ctrl.fuOpType(1,0))
229    io.dcache.req.bits.id   := DontCare
230
231    when(io.dcache.req.fire()){
232      state := s_cache_resp
233      paddr_reg := io.dcache.req.bits.addr
234      data_reg := io.dcache.req.bits.data
235      mask_reg := io.dcache.req.bits.mask
236      fuop_reg := in.uop.ctrl.fuOpType
237    }
238  }
239
240  when (state === s_cache_resp) {
241    io.dcache.resp.ready := data_valid
242    when(io.dcache.resp.fire()) {
243      is_lrsc_valid := io.dcache.resp.bits.id
244      val rdata = io.dcache.resp.bits.data
245      val rdataSel = LookupTree(paddr(2, 0), List(
246        "b000".U -> rdata(63, 0),
247        "b001".U -> rdata(63, 8),
248        "b010".U -> rdata(63, 16),
249        "b011".U -> rdata(63, 24),
250        "b100".U -> rdata(63, 32),
251        "b101".U -> rdata(63, 40),
252        "b110".U -> rdata(63, 48),
253        "b111".U -> rdata(63, 56)
254      ))
255
256      resp_data_wire := LookupTree(in.uop.ctrl.fuOpType, List(
257        LSUOpType.lr_w      -> SignExt(rdataSel(31, 0), XLEN),
258        LSUOpType.sc_w      -> rdata,
259        LSUOpType.amoswap_w -> SignExt(rdataSel(31, 0), XLEN),
260        LSUOpType.amoadd_w  -> SignExt(rdataSel(31, 0), XLEN),
261        LSUOpType.amoxor_w  -> SignExt(rdataSel(31, 0), XLEN),
262        LSUOpType.amoand_w  -> SignExt(rdataSel(31, 0), XLEN),
263        LSUOpType.amoor_w   -> SignExt(rdataSel(31, 0), XLEN),
264        LSUOpType.amomin_w  -> SignExt(rdataSel(31, 0), XLEN),
265        LSUOpType.amomax_w  -> SignExt(rdataSel(31, 0), XLEN),
266        LSUOpType.amominu_w -> SignExt(rdataSel(31, 0), XLEN),
267        LSUOpType.amomaxu_w -> SignExt(rdataSel(31, 0), XLEN),
268
269        LSUOpType.lr_d      -> SignExt(rdataSel(63, 0), XLEN),
270        LSUOpType.sc_d      -> rdata,
271        LSUOpType.amoswap_d -> SignExt(rdataSel(63, 0), XLEN),
272        LSUOpType.amoadd_d  -> SignExt(rdataSel(63, 0), XLEN),
273        LSUOpType.amoxor_d  -> SignExt(rdataSel(63, 0), XLEN),
274        LSUOpType.amoand_d  -> SignExt(rdataSel(63, 0), XLEN),
275        LSUOpType.amoor_d   -> SignExt(rdataSel(63, 0), XLEN),
276        LSUOpType.amomin_d  -> SignExt(rdataSel(63, 0), XLEN),
277        LSUOpType.amomax_d  -> SignExt(rdataSel(63, 0), XLEN),
278        LSUOpType.amominu_d -> SignExt(rdataSel(63, 0), XLEN),
279        LSUOpType.amomaxu_d -> SignExt(rdataSel(63, 0), XLEN)
280      ))
281
282      when (io.dcache.resp.bits.error && io.csrCtrl.cache_error_enable) {
283        exceptionVec(loadAccessFault)  := isLr
284        exceptionVec(storeAccessFault) := !isLr
285        assert(!exceptionVec(loadAccessFault))
286        assert(!exceptionVec(storeAccessFault))
287      }
288
289      resp_data := resp_data_wire
290      state := s_finish
291    }
292  }
293
294  when (state === s_finish) {
295    io.out.valid := true.B
296    io.out.bits.uop := in.uop
297    io.out.bits.uop.cf.exceptionVec := exceptionVec
298    io.out.bits.data := resp_data
299    io.out.bits.redirectValid := false.B
300    io.out.bits.redirect := DontCare
301    io.out.bits.debug.isMMIO := is_mmio
302    io.out.bits.debug.paddr := paddr
303    when (io.out.fire()) {
304      XSDebug("atomics writeback: pc %x data %x\n", io.out.bits.uop.cf.pc, io.dcache.resp.bits.data)
305      state := s_invalid
306    }
307    data_valid := false.B
308  }
309
310  when (io.redirect.valid) {
311    atom_override_xtval := false.B
312  }
313
314  // atomic trigger
315  val csrCtrl = io.csrCtrl
316  val tdata = Reg(Vec(6, new MatchTriggerIO))
317  val tEnable = RegInit(VecInit(Seq.fill(6)(false.B)))
318  val en = csrCtrl.trigger_enable
319  tEnable := VecInit(en(2), en (3), en(7), en(4), en(5), en(9))
320  when(csrCtrl.mem_trigger.t.valid) {
321    tdata(csrCtrl.mem_trigger.t.bits.addr) := csrCtrl.mem_trigger.t.bits.tdata
322  }
323  val lTriggerMapping = Map(0 -> 2, 1 -> 3, 2 -> 5)
324  val sTriggerMapping = Map(0 -> 0, 1 -> 1, 2 -> 4)
325
326  val backendTriggerHitReg = Reg(Vec(6, Bool()))
327  backendTriggerHitReg := VecInit(Seq.fill(6)(false.B))
328
329  when(state === s_cache_req){
330    // store trigger
331    val store_hit = Wire(Vec(3, Bool()))
332    for (j <- 0 until 3) {
333        store_hit(j) := !tdata(sTriggerMapping(j)).select && TriggerCmp(
334          vaddr,
335          tdata(sTriggerMapping(j)).tdata2,
336          tdata(sTriggerMapping(j)).matchType,
337          tEnable(sTriggerMapping(j))
338        )
339       backendTriggerHitReg(sTriggerMapping(j)) := store_hit(j)
340     }
341
342    when(tdata(0).chain) {
343      backendTriggerHitReg(0) := store_hit(0) && store_hit(1)
344      backendTriggerHitReg(1) := store_hit(0) && store_hit(1)
345    }
346
347    when(!in.uop.cf.trigger.backendEn(0)) {
348      backendTriggerHitReg(4) := false.B
349    }
350
351    // load trigger
352    val load_hit = Wire(Vec(3, Bool()))
353    for (j <- 0 until 3) {
354
355      val addrHit = TriggerCmp(
356        vaddr,
357        tdata(lTriggerMapping(j)).tdata2,
358        tdata(lTriggerMapping(j)).matchType,
359        tEnable(lTriggerMapping(j))
360      )
361      load_hit(j) := addrHit && !tdata(lTriggerMapping(j)).select
362      backendTriggerHitReg(lTriggerMapping(j)) := load_hit(j)
363    }
364    when(tdata(2).chain) {
365      backendTriggerHitReg(2) := load_hit(0) && load_hit(1)
366      backendTriggerHitReg(3) := load_hit(0) && load_hit(1)
367    }
368    when(!in.uop.cf.trigger.backendEn(1)) {
369      backendTriggerHitReg(5) := false.B
370    }
371  }
372
373  // addr trigger do cmp at s_cache_req
374  // trigger result is used at s_finish
375  // thus we can delay it safely
376  io.out.bits.uop.cf.trigger.backendHit := VecInit(Seq.fill(6)(false.B))
377  when(isLr){
378    // enable load trigger
379    io.out.bits.uop.cf.trigger.backendHit(2) := backendTriggerHitReg(2)
380    io.out.bits.uop.cf.trigger.backendHit(3) := backendTriggerHitReg(3)
381    io.out.bits.uop.cf.trigger.backendHit(5) := backendTriggerHitReg(5)
382  }.otherwise{
383    // enable store trigger
384    io.out.bits.uop.cf.trigger.backendHit(0) := backendTriggerHitReg(0)
385    io.out.bits.uop.cf.trigger.backendHit(1) := backendTriggerHitReg(1)
386    io.out.bits.uop.cf.trigger.backendHit(4) := backendTriggerHitReg(4)
387  }
388
389  if (env.EnableDifftest) {
390    val difftest = Module(new DifftestAtomicEvent)
391    difftest.io.clock      := clock
392    difftest.io.coreid     := io.hartId
393    difftest.io.atomicResp := io.dcache.resp.fire()
394    difftest.io.atomicAddr := paddr_reg
395    difftest.io.atomicData := data_reg
396    difftest.io.atomicMask := mask_reg
397    difftest.io.atomicFuop := fuop_reg
398    difftest.io.atomicOut  := resp_data_wire
399  }
400
401  if (env.EnableDifftest || env.AlwaysBasicDiff) {
402    val uop = io.out.bits.uop
403    val difftest = Module(new DifftestLrScEvent)
404    difftest.io.clock := clock
405    difftest.io.coreid := io.hartId
406    difftest.io.valid := io.out.fire &&
407      (uop.ctrl.fuOpType === LSUOpType.sc_d || uop.ctrl.fuOpType === LSUOpType.sc_w)
408    difftest.io.success := is_lrsc_valid
409  }
410}
411