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 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 enablePreAlloc = true, 86 moduleName = "LoadQueueRAR freelist" 87 )) 88 freeList.io := DontCare 89 90 // Real-allocation: load_s2 91 // PAddr write needs 2 cycles, release signal should delay 1 cycle so that 92 // load enqueue can catch release. 93 val release1Cycle = io.release 94 val release2Cycle = RegNext(io.release) 95 val release2Cycle_dup_lsu = RegNext(io.release) 96 97 // LoadQueueRAR enqueue condition: 98 // There are still not completed load instructions before the current load instruction. 99 // (e.g. "not completed" means that load instruction get the data or exception). 100 val canEnqueue = io.query.map(_.req.valid) 101 val cancelEnqueue = io.query.map(_.req.bits.uop.robIdx.needFlush(io.redirect)) 102 val hasNotWritebackedLoad = io.query.map(_.req.bits.uop.lqIdx).map(lqIdx => isAfter(lqIdx, io.ldWbPtr)) 103 val needEnqueue = canEnqueue.zip(hasNotWritebackedLoad).zip(cancelEnqueue).map { case ((v, r), c) => v && r && !c } 104 105 // Allocate logic 106 val acceptedVec = Wire(Vec(LoadPipelineWidth, Bool())) 107 val enqIndexVec = Wire(Vec(LoadPipelineWidth, UInt())) 108 109 for ((enq, w) <- io.query.map(_.req).zipWithIndex) { 110 acceptedVec(w) := false.B 111 paddrModule.io.wen(w) := false.B 112 freeList.io.doAllocate(w) := false.B 113 114 freeList.io.allocateReq(w) := true.B 115 116 // Allocate ready 117 val offset = PopCount(needEnqueue.take(w)) 118 val canAccept = freeList.io.canAllocate(offset) 119 val enqIndex = freeList.io.allocateSlot(offset) 120 enq.ready := Mux(needEnqueue(w), canAccept, true.B) 121 122 enqIndexVec(w) := enqIndex 123 when (needEnqueue(w) && enq.ready) { 124 acceptedVec(w) := true.B 125 126 val debug_robIdx = enq.bits.uop.robIdx.asUInt 127 XSError(allocated(enqIndex), p"LoadQueueRAR: You can not write an valid entry! check: ldu $w, robIdx $debug_robIdx") 128 129 freeList.io.doAllocate(w) := true.B 130 131 // Allocate new entry 132 allocated(enqIndex) := true.B 133 134 // Write paddr 135 paddrModule.io.wen(w) := true.B 136 paddrModule.io.waddr(w) := enqIndex 137 paddrModule.io.wdata(w) := enq.bits.paddr 138 139 // Fill info 140 uop(enqIndex) := enq.bits.uop 141 released(enqIndex) := 142 enq.bits.data_valid && 143 (release2Cycle.valid && 144 enq.bits.paddr(PAddrBits-1, DCacheLineOffset) === release2Cycle.bits.paddr(PAddrBits-1, DCacheLineOffset) || 145 release1Cycle.valid && 146 enq.bits.paddr(PAddrBits-1, DCacheLineOffset) === release1Cycle.bits.paddr(PAddrBits-1, DCacheLineOffset)) 147 } 148 } 149 150 // LoadQueueRAR deallocate 151 val freeMaskVec = Wire(Vec(LoadQueueRARSize, Bool())) 152 153 // init 154 freeMaskVec.map(e => e := false.B) 155 156 // when the loads that "older than" current load were writebacked, 157 // current load will be released. 158 for (i <- 0 until LoadQueueRARSize) { 159 val deqNotBlock = !isBefore(io.ldWbPtr, uop(i).lqIdx) 160 val needFlush = uop(i).robIdx.needFlush(io.redirect) 161 162 when (allocated(i) && (deqNotBlock || needFlush)) { 163 allocated(i) := false.B 164 freeMaskVec(i) := true.B 165 } 166 } 167 168 // if need replay revoke entry 169 val lastCanAccept = RegNext(acceptedVec) 170 val lastAllocIndex = RegNext(enqIndexVec) 171 172 for ((revoke, w) <- io.query.map(_.revoke).zipWithIndex) { 173 val revokeValid = revoke && lastCanAccept(w) 174 val revokeIndex = lastAllocIndex(w) 175 176 when (allocated(revokeIndex) && revokeValid) { 177 allocated(revokeIndex) := false.B 178 freeMaskVec(revokeIndex) := true.B 179 } 180 } 181 182 freeList.io.free := freeMaskVec.asUInt 183 184 // LoadQueueRAR Query 185 // Load-to-Load violation check condition: 186 // 1. Physical address match by CAM port. 187 // 2. release is set. 188 // 3. Younger than current load instruction. 189 val ldLdViolation = Wire(Vec(LoadPipelineWidth, Bool())) 190 val allocatedUInt = RegNext(allocated.asUInt) 191 for ((query, w) <- io.query.zipWithIndex) { 192 ldLdViolation(w) := false.B 193 paddrModule.io.releaseViolationMdata(w) := query.req.bits.paddr 194 195 query.resp.valid := RegNext(query.req.valid) 196 // Generate real violation mask 197 val robIdxMask = VecInit(uop.map(_.robIdx).map(isAfter(_, query.req.bits.uop.robIdx))) 198 val matchMask = allocatedUInt & 199 RegNext(paddrModule.io.releaseViolationMmask(w).asUInt) & 200 RegNext(robIdxMask.asUInt) 201 // Load-to-Load violation check result 202 val ldLdViolationMask = WireInit(matchMask & RegNext(released.asUInt)) 203 ldLdViolationMask.suggestName("ldLdViolationMask_" + w) 204 query.resp.bits.rep_frm_fetch := ldLdViolationMask.orR 205 } 206 207 208 // When io.release.valid (release1cycle.valid), it uses the last ld-ld paddr cam port to 209 // update release flag in 1 cycle 210 val releaseVioMask = Reg(Vec(LoadQueueRARSize, Bool())) 211 when (release1Cycle.valid) { 212 paddrModule.io.releaseMdata.takeRight(1)(0) := release1Cycle.bits.paddr 213 } 214 215 (0 until LoadQueueRARSize).map(i => { 216 when (RegNext(paddrModule.io.releaseMmask.takeRight(1)(0)(i) && allocated(i) && release1Cycle.valid)) { 217 // Note: if a load has missed in dcache and is waiting for refill in load queue, 218 // its released flag still needs to be set as true if addr matches. 219 released(i) := true.B 220 } 221 }) 222 223 io.lqFull := freeList.io.empty 224 225 // perf cnt 226 val canEnqCount = PopCount(io.query.map(_.req.fire)) 227 val validCount = freeList.io.validCount 228 val allowEnqueue = validCount <= (LoadQueueRARSize - LoadPipelineWidth).U 229 val ldLdViolationCount = PopCount(io.query.map(_.resp).map(resp => resp.valid && resp.bits.rep_frm_fetch)) 230 231 QueuePerf(LoadQueueRARSize, validCount, !allowEnqueue) 232 XSPerfAccumulate("enq", canEnqCount) 233 XSPerfAccumulate("ld_ld_violation", ldLdViolationCount) 234 val perfEvents: Seq[(String, UInt)] = Seq( 235 ("enq", canEnqCount), 236 ("ld_ld_violation", ldLdViolationCount) 237 ) 238 generatePerfEvent() 239 // End 240}