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