xref: /XiangShan/src/main/scala/xiangshan/mem/lsqueue/LoadQueueRAR.scala (revision d2b20d1a96e238e36a849bd253f65ec7b6a5db38)
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***************************************************************************************/
16package xiangshan.mem
17
18import chisel3._
19import chisel3.util._
20import chipsalliance.rocketchip.config._
21import xiangshan._
22import xiangshan.backend.rob.RobPtr
23import xiangshan.cache._
24import utils._
25import utility._
26
27class LoadQueueRAR(implicit p: Parameters) extends XSModule
28  with HasDCacheParameters
29  with HasCircularQueuePtrHelper
30  with HasLoadHelper
31  with HasPerfEvents
32{
33  val io = IO(new Bundle() {
34    val redirect = Flipped(Valid(new Redirect))
35    val query = Vec(LoadPipelineWidth, Flipped(new LoadViolationQueryIO))
36    val release = Flipped(Valid(new Release))
37    val ldWbPtr = Input(new LqPtr)
38    val lqFull = Output(Bool())
39  })
40
41  println("LoadQueueRAR: size: " + LoadQueueRARSize)
42  //  LoadQueueRAR field
43  //  +-------+-------+-------+----------+
44  //  | Valid |  Uop  | PAddr | Released |
45  //  +-------+-------+-------+----------+
46  //
47  //  Field descriptions:
48  //  Allocated   : entry is valid.
49  //  MicroOp     : Micro-op
50  //  PAddr       : physical address.
51  //  Released    : DCache released.
52  //
53  val allocated = RegInit(VecInit(List.fill(LoadQueueRARSize)(false.B))) // The control signals need to explicitly indicate the initial value
54  val uop = Reg(Vec(LoadQueueRARSize, new MicroOp))
55  val paddrModule = Module(new LqPAddrModule(
56    gen = UInt(PAddrBits.W),
57    numEntries = LoadQueueRARSize,
58    numRead = LoadPipelineWidth,
59    numWrite = LoadPipelineWidth,
60    numWBank = LoadQueueNWriteBanks,
61    numWDelay = 2,
62    numCamPort = LoadPipelineWidth
63  ))
64  paddrModule.io := DontCare
65  val released = RegInit(VecInit(List.fill(LoadQueueRARSize)(false.B)))
66
67  // freeliset: store valid entries index.
68  // +---+---+--------------+-----+-----+
69  // | 0 | 1 |      ......  | n-2 | n-1 |
70  // +---+---+--------------+-----+-----+
71  val freeList = Module(new FreeList(
72    size = LoadQueueRARSize,
73    allocWidth = LoadPipelineWidth,
74    freeWidth = 4,
75    moduleName = "LoadQueueRAR freelist"
76  ))
77  freeList.io := DontCare
78
79  // Real-allocation: load_s2
80  // PAddr write needs 2 cycles, release signal should delay 1 cycle so that
81  // load enqueue can catch release.
82  val release1Cycle = io.release
83  val release2Cycle = RegNext(io.release)
84  val release2Cycle_dup_lsu = RegNext(io.release)
85
86  // LoadQueueRAR enqueue condition:
87  // There are still not completed load instructions before the current load instruction.
88  // (e.g. "not completed" means that load instruction get the data or exception).
89  val canEnqueue = io.query.map(_.req.valid)
90  val cancelEnqueue = io.query.map(_.req.bits.uop.robIdx.needFlush(io.redirect))
91  val hasNotWritebackedLoad = io.query.map(_.req.bits.uop.lqIdx).map(lqIdx => isAfter(lqIdx, io.ldWbPtr))
92  val needEnqueue = canEnqueue.zip(hasNotWritebackedLoad).zip(cancelEnqueue).map { case ((v, r), c) => v && r && !c }
93
94  // Allocate logic
95  val enqValidVec = Wire(Vec(LoadPipelineWidth, Bool()))
96  val enqIndexVec = Wire(Vec(LoadPipelineWidth, UInt()))
97  val enqOffset = Wire(Vec(LoadPipelineWidth, UInt()))
98
99  for ((enq, w) <- io.query.map(_.req).zipWithIndex) {
100    paddrModule.io.wen(w) := false.B
101    freeList.io.doAllocate(w) := false.B
102
103    enqOffset(w) := PopCount(needEnqueue.take(w))
104    freeList.io.allocateReq(w) := needEnqueue(w)
105
106    //  Allocate ready
107    enqValidVec(w) := freeList.io.canAllocate(enqOffset(w))
108    enqIndexVec(w) := freeList.io.allocateSlot(enqOffset(w))
109    enq.ready := Mux(needEnqueue(w), enqValidVec(w), true.B)
110
111    val enqIndex = enqIndexVec(w)
112    when (needEnqueue(w) && enq.ready) {
113      val debug_robIdx = enq.bits.uop.robIdx.asUInt
114      XSError(allocated(enqIndex), p"LoadQueueRAR: You can not write an valid entry! check: ldu $w, robIdx $debug_robIdx")
115
116      freeList.io.doAllocate(w) := true.B
117
118      //  Allocate new entry
119      allocated(enqIndex) := true.B
120
121      //  Write paddr
122      paddrModule.io.wen(w) := true.B
123      paddrModule.io.waddr(w) := enqIndex
124      paddrModule.io.wdata(w) := enq.bits.paddr
125
126      //  Fill info
127      uop(enqIndex) := enq.bits.uop
128      released(enqIndex) :=
129        enq.bits.datavalid &&
130        release2Cycle.valid &&
131        enq.bits.paddr(PAddrBits-1, DCacheLineOffset) === release2Cycle.bits.paddr(PAddrBits-1, DCacheLineOffset) ||
132        release1Cycle.valid &&
133        enq.bits.paddr(PAddrBits-1, DCacheLineOffset) === release1Cycle.bits.paddr(PAddrBits-1, DCacheLineOffset)
134    }
135  }
136
137  //  LoadQueueRAR deallocate
138  val freeMaskVec = Wire(Vec(LoadQueueRARSize, Bool()))
139
140  // init
141  freeMaskVec.map(e => e := false.B)
142
143  // when the loads that "older than" current load were writebacked,
144  // current load will be released.
145  for (i <- 0 until LoadQueueRARSize) {
146    val deqNotBlock = !isBefore(io.ldWbPtr, uop(i).lqIdx)
147    val needFlush = uop(i).robIdx.needFlush(io.redirect)
148
149    when (allocated(i) && (deqNotBlock || needFlush)) {
150      allocated(i) := false.B
151      freeMaskVec(i) := true.B
152    }
153  }
154
155  // if need replay release entry
156  val lastCanAccept = RegNext(VecInit(needEnqueue.zip(enqValidVec).map(x => x._1 && x._2)))
157  val lastAllocIndex = RegNext(enqIndexVec)
158
159  for ((release, w) <- io.query.map(_.release).zipWithIndex) {
160    val releaseValid = release && lastCanAccept(w)
161    val releaseIndex = lastAllocIndex(w)
162
163    when (allocated(releaseIndex) && releaseValid) {
164      allocated(releaseIndex) := false.B
165      freeMaskVec(releaseIndex) := true.B
166    }
167  }
168
169  freeList.io.free := freeMaskVec.asUInt
170
171  // LoadQueueRAR Query
172  // Load-to-Load violation check condition:
173  // 1. Physical address match by CAM port.
174  // 2. release is set.
175  // 3. Younger than current load instruction.
176  val ldLdViolation = Wire(Vec(LoadPipelineWidth, Bool()))
177  val allocatedUInt = RegNext(allocated.asUInt)
178  for ((query, w) <- io.query.zipWithIndex) {
179    ldLdViolation(w) := false.B
180    paddrModule.io.releaseViolationMdata(w) := query.req.bits.paddr
181
182    query.resp.valid := RegNext(query.req.valid)
183    // Generate real violation mask
184    val robIdxMask = VecInit(uop.map(_.robIdx).map(isAfter(_, query.req.bits.uop.robIdx)))
185    val matchMask = allocatedUInt &
186                    RegNext(paddrModule.io.releaseViolationMmask(w).asUInt) &
187                    RegNext(robIdxMask.asUInt)
188    //  Load-to-Load violation check result
189    val ldLdViolationMask = WireInit(matchMask & RegNext(released.asUInt))
190    ldLdViolationMask.suggestName("ldLdViolationMask_" + w)
191    query.resp.bits.replayFromFetch := ldLdViolationMask.orR || RegNext(ldLdViolation(w))
192  }
193
194  (0 until LoadPipelineWidth).map(w => {
195    ldLdViolation(w) := (release1Cycle.valid && io.query(w).req.bits.paddr(PAddrBits-1, DCacheLineOffset) === release1Cycle.bits.paddr(PAddrBits-1, DCacheLineOffset)) ||
196                        (release2Cycle.valid && io.query(w).req.bits.paddr(PAddrBits-1, DCacheLineOffset) === release2Cycle.bits.paddr(PAddrBits-1, DCacheLineOffset))
197  })
198
199  // When io.release.valid (release1cycle.valid), it uses the last ld-ld paddr cam port to
200  // update release flag in 1 cycle
201  val releaseVioMask = Reg(Vec(LoadQueueRARSize, Bool()))
202  when (release1Cycle.valid) {
203    paddrModule.io.releaseMdata.takeRight(1)(0) := release1Cycle.bits.paddr
204  }
205
206  (0 until LoadQueueRARSize).map(i => {
207    when (RegNext(paddrModule.io.releaseMmask.takeRight(1)(0)(i) && allocated(i) && release1Cycle.valid)) {
208      // Note: if a load has missed in dcache and is waiting for refill in load queue,
209      // its released flag still needs to be set as true if addr matches.
210      released(i) := true.B
211    }
212  })
213
214  io.lqFull := freeList.io.empty
215
216  // perf cnt
217  val canEnqCount = PopCount(io.query.map(_.req.fire))
218  val validCount = freeList.io.validCount
219  val allowEnqueue = validCount <= (LoadQueueRARSize - LoadPipelineWidth).U
220  val ldLdViolationCount = PopCount(io.query.map(_.resp).map(resp => resp.valid && resp.bits.replayFromFetch))
221
222  QueuePerf(LoadQueueRARSize, validCount, !allowEnqueue)
223  XSPerfAccumulate("enq", canEnqCount)
224  XSPerfAccumulate("ld_ld_violation", ldLdViolationCount)
225  val perfEvents: Seq[(String, UInt)] = Seq(
226    ("enq", canEnqCount),
227    ("ld_ld_violation", ldLdViolationCount)
228  )
229  generatePerfEvent()
230  // End
231}