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 org.chipsalliance.cde.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 val vecFeedback = Vec(VecLoadPipelineWidth, Flipped(ValidIO(new FeedbackToLsqIO))) 38 39 // violation query 40 val query = Vec(LoadPipelineWidth, Flipped(new LoadNukeQueryIO)) 41 42 // release cacheline 43 val release = Flipped(Valid(new Release)) 44 45 // from VirtualLoadQueue 46 val ldWbPtr = Input(new LqPtr) 47 48 // global 49 val lqFull = Output(Bool()) 50 }) 51 52 private val PartialPAddrStride: Int = 6 53 private val PartialPAddrBits: Int = 16 54 private val PartialPAddrLowBits: Int = (PartialPAddrBits - PartialPAddrStride) / 2 // avoid overlap 55 private val PartialPAddrHighBits: Int = PartialPAddrBits - PartialPAddrLowBits 56 private def boundary(x: Int, h: Int) = if (x < h) Some(x) else None 57 private def lowMapping = (0 until PartialPAddrLowBits).map(i => Seq( 58 boundary(PartialPAddrStride + i , PartialPAddrBits), 59 boundary(PartialPAddrBits - i - 1, PartialPAddrBits) 60 ) 61 ) 62 private def highMapping = (0 until PartialPAddrHighBits).map(i => Seq( 63 boundary(i + PartialPAddrStride , PAddrBits), 64 boundary(i + PartialPAddrStride + 11, PAddrBits), 65 boundary(i + PartialPAddrStride + 22, PAddrBits), 66 boundary(i + PartialPAddrStride + 33, PAddrBits) 67 ) 68 ) 69 private def genPartialPAddr(paddr: UInt) = { 70 val ppaddr_low = Wire(Vec(PartialPAddrLowBits, Bool())) 71 ppaddr_low.zip(lowMapping).foreach { 72 case (bit, mapping) => 73 bit := mapping.filter(_.isDefined).map(x => paddr(x.get)).reduce(_^_) 74 } 75 76 val ppaddr_high = Wire(Vec(PartialPAddrHighBits, Bool())) 77 ppaddr_high.zip(highMapping).foreach { 78 case (bit, mapping) => 79 bit := mapping.filter(_.isDefined).map(x => paddr(x.get)).reduce(_^_) 80 } 81 Cat(ppaddr_high.asUInt, ppaddr_low.asUInt) 82 } 83 84 println("LoadQueueRAR: size: " + LoadQueueRARSize) 85 // LoadQueueRAR field 86 // +-------+-------+-------+----------+ 87 // | Valid | Uop | PAddr | Released | 88 // +-------+-------+-------+----------+ 89 // 90 // Field descriptions: 91 // Allocated : entry is valid. 92 // MicroOp : Micro-op 93 // PAddr : physical address. 94 // Released : DCache released. 95 val allocated = RegInit(VecInit(List.fill(LoadQueueRARSize)(false.B))) // The control signals need to explicitly indicate the initial value 96 val uop = Reg(Vec(LoadQueueRARSize, new DynInst)) 97 val paddrModule = Module(new LqPAddrModule( 98 gen = UInt(PartialPAddrBits.W), 99 numEntries = LoadQueueRARSize, 100 numRead = LoadPipelineWidth, 101 numWrite = LoadPipelineWidth, 102 numWBank = LoadQueueNWriteBanks, 103 numWDelay = 2, 104 numCamPort = LoadPipelineWidth 105 )) 106 paddrModule.io := DontCare 107 val released = RegInit(VecInit(List.fill(LoadQueueRARSize)(false.B))) 108 109 // freeliset: store valid entries index. 110 // +---+---+--------------+-----+-----+ 111 // | 0 | 1 | ...... | n-2 | n-1 | 112 // +---+---+--------------+-----+-----+ 113 val freeList = Module(new FreeList( 114 size = LoadQueueRARSize, 115 allocWidth = LoadPipelineWidth, 116 freeWidth = 4, 117 enablePreAlloc = true, 118 moduleName = "LoadQueueRAR freelist" 119 )) 120 freeList.io := DontCare 121 122 // Real-allocation: load_s2 123 // PAddr write needs 2 cycles, release signal should delay 1 cycle so that 124 // load enqueue can catch release. 125 val release1Cycle = io.release 126 // val release2Cycle = RegNext(io.release) 127 // val release2Cycle_dup_lsu = RegNext(io.release) 128 val release2Cycle = RegEnable(io.release, io.release.valid) 129 release2Cycle.valid := RegNext(io.release.valid) 130 //val release2Cycle_dup_lsu = RegEnable(io.release, io.release.valid) 131 132 // LoadQueueRAR enqueue condition: 133 // There are still not completed load instructions before the current load instruction. 134 // (e.g. "not completed" means that load instruction get the data or exception). 135 val canEnqueue = io.query.map(_.req.valid) 136 val cancelEnqueue = io.query.map(_.req.bits.uop.robIdx.needFlush(io.redirect)) 137 val hasNotWritebackedLoad = io.query.map(_.req.bits.uop.lqIdx).map(lqIdx => isAfter(lqIdx, io.ldWbPtr)) 138 val needEnqueue = canEnqueue.zip(hasNotWritebackedLoad).zip(cancelEnqueue).map { case ((v, r), c) => v && r && !c } 139 140 // Allocate logic 141 val acceptedVec = Wire(Vec(LoadPipelineWidth, Bool())) 142 val enqIndexVec = Wire(Vec(LoadPipelineWidth, UInt(log2Up(LoadQueueRARSize).W))) 143 144 for ((enq, w) <- io.query.map(_.req).zipWithIndex) { 145 acceptedVec(w) := false.B 146 paddrModule.io.wen(w) := false.B 147 freeList.io.doAllocate(w) := false.B 148 149 freeList.io.allocateReq(w) := true.B 150 151 // Allocate ready 152 val offset = PopCount(needEnqueue.take(w)) 153 val canAccept = freeList.io.canAllocate(offset) 154 val enqIndex = freeList.io.allocateSlot(offset) 155 enq.ready := Mux(needEnqueue(w), canAccept, true.B) 156 157 enqIndexVec(w) := enqIndex 158 when (needEnqueue(w) && enq.ready) { 159 acceptedVec(w) := true.B 160 161 val debug_robIdx = enq.bits.uop.robIdx.asUInt 162 XSError(allocated(enqIndex), p"LoadQueueRAR: You can not write an valid entry! check: ldu $w, robIdx $debug_robIdx") 163 164 freeList.io.doAllocate(w) := true.B 165 // Allocate new entry 166 allocated(enqIndex) := true.B 167 168 // Write paddr 169 paddrModule.io.wen(w) := true.B 170 paddrModule.io.waddr(w) := enqIndex 171 paddrModule.io.wdata(w) := genPartialPAddr(enq.bits.paddr) 172 173 // Fill info 174 uop(enqIndex) := enq.bits.uop 175 // NC is uncachable and will not be explicitly released. 176 // So NC requests are not allowed to have RAR 177 released(enqIndex) := enq.bits.is_nc || ( 178 enq.bits.data_valid && 179 (release2Cycle.valid && 180 enq.bits.paddr(PAddrBits-1, DCacheLineOffset) === release2Cycle.bits.paddr(PAddrBits-1, DCacheLineOffset) || 181 release1Cycle.valid && 182 enq.bits.paddr(PAddrBits-1, DCacheLineOffset) === release1Cycle.bits.paddr(PAddrBits-1, DCacheLineOffset)) 183 ) 184 } 185 } 186 187 // LoadQueueRAR deallocate 188 val freeMaskVec = Wire(Vec(LoadQueueRARSize, Bool())) 189 190 // init 191 freeMaskVec.map(e => e := false.B) 192 193 // when the loads that "older than" current load were writebacked, 194 // current load will be released. 195 val vecLdCanceltmp = Wire(Vec(LoadQueueRARSize, Vec(VecLoadPipelineWidth, Bool()))) 196 val vecLdCancel = Wire(Vec(LoadQueueRARSize, Bool())) 197 for (i <- 0 until LoadQueueRARSize) { 198 val deqNotBlock = !isBefore(io.ldWbPtr, uop(i).lqIdx) 199 val needFlush = uop(i).robIdx.needFlush(io.redirect) 200 val fbk = io.vecFeedback 201 for (j <- 0 until VecLoadPipelineWidth) { 202 vecLdCanceltmp(i)(j) := allocated(i) && fbk(j).valid && fbk(j).bits.isFlush && uop(i).robIdx === fbk(j).bits.robidx && uop(i).uopIdx === fbk(j).bits.uopidx 203 } 204 vecLdCancel(i) := vecLdCanceltmp(i).reduce(_ || _) 205 206 when (allocated(i) && (deqNotBlock || needFlush || vecLdCancel(i))) { 207 allocated(i) := false.B 208 freeMaskVec(i) := true.B 209 } 210 } 211 212 // if need replay revoke entry 213 val lastCanAccept = GatedRegNext(acceptedVec) 214 val lastAllocIndex = GatedRegNext(enqIndexVec) 215 216 for ((revoke, w) <- io.query.map(_.revoke).zipWithIndex) { 217 val revokeValid = revoke && lastCanAccept(w) 218 val revokeIndex = lastAllocIndex(w) 219 220 when (allocated(revokeIndex) && revokeValid) { 221 allocated(revokeIndex) := false.B 222 freeMaskVec(revokeIndex) := true.B 223 } 224 } 225 226 freeList.io.free := freeMaskVec.asUInt 227 228 // LoadQueueRAR Query 229 // Load-to-Load violation check condition: 230 // 1. Physical address match by CAM port. 231 // 2. release or nc_with_data is set. 232 // 3. Younger than current load instruction. 233 val ldLdViolation = Wire(Vec(LoadPipelineWidth, Bool())) 234 //val allocatedUInt = RegNext(allocated.asUInt) 235 for ((query, w) <- io.query.zipWithIndex) { 236 ldLdViolation(w) := false.B 237 paddrModule.io.releaseViolationMdata(w) := genPartialPAddr(query.req.bits.paddr) 238 239 query.resp.valid := RegNext(query.req.valid) 240 // Generate real violation mask 241 val robIdxMask = VecInit(uop.map(_.robIdx).map(isAfter(_, query.req.bits.uop.robIdx))) 242 val matchMaskReg = Wire(Vec(LoadQueueRARSize, Bool())) 243 for(i <- 0 until LoadQueueRARSize) { 244 matchMaskReg(i) := (allocated(i) & 245 paddrModule.io.releaseViolationMmask(w)(i) & 246 robIdxMask(i) && 247 released(i)) 248 } 249 val matchMask = GatedValidRegNext(matchMaskReg) 250 // Load-to-Load violation check result 251 val ldLdViolationMask = matchMask 252 ldLdViolationMask.suggestName("ldLdViolationMask_" + w) 253 query.resp.bits.rep_frm_fetch := ParallelORR(ldLdViolationMask) 254 } 255 256 257 // When io.release.valid (release1cycle.valid), it uses the last ld-ld paddr cam port to 258 // update release flag in 1 cycle 259 val releaseVioMask = Reg(Vec(LoadQueueRARSize, Bool())) 260 when (release1Cycle.valid) { 261 paddrModule.io.releaseMdata.takeRight(1)(0) := genPartialPAddr(release1Cycle.bits.paddr) 262 } 263 264 (0 until LoadQueueRARSize).map(i => { 265 when (RegNext((paddrModule.io.releaseMmask.takeRight(1)(0)(i)) && allocated(i) && release1Cycle.valid)) { 266 // Note: if a load has missed in dcache and is waiting for refill in load queue, 267 // its released flag still needs to be set as true if addr matches. 268 released(i) := true.B 269 } 270 }) 271 272 io.lqFull := freeList.io.empty 273 274 // perf cnt 275 val canEnqCount = PopCount(io.query.map(_.req.fire)) 276 val validCount = freeList.io.validCount 277 val allowEnqueue = validCount <= (LoadQueueRARSize - LoadPipelineWidth).U 278 val ldLdViolationCount = PopCount(io.query.map(_.resp).map(resp => resp.valid && resp.bits.rep_frm_fetch)) 279 280 QueuePerf(LoadQueueRARSize, validCount, !allowEnqueue) 281 XSPerfAccumulate("enq", canEnqCount) 282 XSPerfAccumulate("ld_ld_violation", ldLdViolationCount) 283 val perfEvents: Seq[(String, UInt)] = Seq( 284 ("enq", canEnqCount), 285 ("ld_ld_violation", ldLdViolationCount) 286 ) 287 generatePerfEvent() 288 // End 289}