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