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