1/*************************************************************************************** 2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 4* Copyright (c) 2020-2021 Peng Cheng Laboratory 5* 6* XiangShan is licensed under Mulan PSL v2. 7* You can use this software according to the terms and conditions of the Mulan PSL v2. 8* You may obtain a copy of Mulan PSL v2 at: 9* http://license.coscl.org.cn/MulanPSL2 10* 11* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 12* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 13* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 14* 15* See the Mulan PSL v2 for more details. 16***************************************************************************************/ 17 18package xiangshan.cache.mmu 19 20import org.chipsalliance.cde.config.Parameters 21import chisel3._ 22import chisel3.util._ 23import xiangshan._ 24import xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 25import utils._ 26import utility._ 27import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 28import freechips.rocketchip.tilelink._ 29 30 31case class TLBParameters 32( 33 name: String = "none", 34 fetchi: Boolean = false, // TODO: remove it 35 fenceDelay: Int = 2, 36 useDmode: Boolean = true, 37 NSets: Int = 1, 38 NWays: Int = 2, 39 Replacer: Option[String] = Some("plru"), 40 Associative: String = "fa", // must be fa 41 outReplace: Boolean = false, 42 partialStaticPMP: Boolean = false, // partial static pmp result stored in entries 43 outsideRecvFlush: Boolean = false, // if outside moudle waiting for tlb recv flush pipe 44 saveLevel: Boolean = false, 45 lgMaxSize: Int = 3 46) 47 48case class L2TLBParameters 49( 50 name: String = "l2tlb", 51 // l3 52 l3Size: Int = 16, 53 l3Associative: String = "fa", 54 l3Replacer: Option[String] = Some("plru"), 55 // l2 56 l2Size: Int = 16, 57 l2Associative: String = "fa", 58 l2Replacer: Option[String] = Some("plru"), 59 // l1 60 l1nSets: Int = 8, 61 l1nWays: Int = 4, 62 l1Replacer: Option[String] = Some("setplru"), 63 // l0 64 l0nSets: Int = 32, 65 l0nWays: Int = 8, 66 l0Replacer: Option[String] = Some("setplru"), 67 // sp 68 spSize: Int = 16, 69 spReplacer: Option[String] = Some("plru"), 70 // filter 71 ifilterSize: Int = 8, 72 dfilterSize: Int = 32, 73 // miss queue, add more entries than 'must require' 74 // 0 for easier bug trigger, please set as big as u can, 8 maybe 75 missqueueExtendSize: Int = 0, 76 // llptw 77 llptwsize: Int = 6, 78 // way size 79 blockBytes: Int = 64, 80 // prefetch 81 enablePrefetch: Boolean = true, 82 // ecc 83 ecc: Option[String] = Some("secded"), 84 // enable ecc 85 enablePTWECC: Boolean = false 86) 87 88trait HasTlbConst extends HasXSParameter { 89 val Level = if (EnableSv48) 3 else 2 90 91 val offLen = 12 92 val ppnLen = PAddrBits - offLen 93 val vpnnLen = 9 94 val extendVpnnBits = if (HasHExtension) 2 else 0 95 val vpnLen = VAddrBits - offLen // when opening H extention, vpnlen broaden two bits 96 val flagLen = 8 97 val pteResLen = XLEN - 44 - 2 - flagLen 98 val ppnHignLen = 44 - ppnLen 99 val gvpnLen = GPAddrBits - offLen 100 101 val tlbcontiguous = 8 102 val sectortlbwidth = log2Up(tlbcontiguous) 103 val sectorppnLen = ppnLen - sectortlbwidth 104 val sectorgvpnLen = gvpnLen - sectortlbwidth 105 val sectorvpnLen = vpnLen - sectortlbwidth 106 107 val loadfiltersize = 16 // 4*3(LduCnt:2 + HyuCnt:1) + 4(prefetch:1) 108 val storefiltersize = if (StorePipelineWidth >= 3) 16 else 8 109 val prefetchfiltersize = 8 110 111 val sramSinglePort = true 112 113 val timeOutThreshold = 10000 114 115 def noS2xlate = "b00".U 116 def allStage = "b11".U 117 def onlyStage1 = "b01".U 118 def onlyStage2 = "b10".U 119 120 def Sv39 = "h8".U 121 def Sv48 = "h9".U 122 123 def get_pn(addr: UInt) = { 124 require(addr.getWidth > offLen) 125 addr(addr.getWidth-1, offLen) 126 } 127 def get_off(addr: UInt) = { 128 require(addr.getWidth > offLen) 129 addr(offLen-1, 0) 130 } 131 132 def get_set_idx(vpn: UInt, nSets: Int): UInt = { 133 require(nSets >= 1) 134 vpn(log2Up(nSets)-1, 0) 135 } 136 137 def drop_set_idx(vpn: UInt, nSets: Int): UInt = { 138 require(nSets >= 1) 139 require(vpn.getWidth > log2Ceil(nSets)) 140 vpn(vpn.getWidth-1, log2Ceil(nSets)) 141 } 142 143 def drop_set_equal(vpn1: UInt, vpn2: UInt, nSets: Int): Bool = { 144 require(nSets >= 1) 145 require(vpn1.getWidth == vpn2.getWidth) 146 if (vpn1.getWidth <= log2Ceil(nSets)) { 147 true.B 148 } else { 149 drop_set_idx(vpn1, nSets) === drop_set_idx(vpn2, nSets) 150 } 151 } 152 153 def replaceWrapper(v: UInt, lruIdx: UInt): UInt = { 154 val width = v.getWidth 155 val emptyIdx = ParallelPriorityMux((0 until width).map( i => (!v(i), i.U(log2Up(width).W)))) 156 val full = Cat(v).andR 157 Mux(full, lruIdx, emptyIdx) 158 } 159 160 def replaceWrapper(v: Seq[Bool], lruIdx: UInt): UInt = { 161 replaceWrapper(VecInit(v).asUInt, lruIdx) 162 } 163 164 import scala.language.implicitConversions 165 166 implicit def hptwresp_to_tlbperm(hptwResp: HptwResp): TlbPermBundle = { 167 val tp = Wire(new TlbPermBundle) 168 val ptePerm = hptwResp.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 169 tp.pf := hptwResp.gpf 170 tp.af := hptwResp.gaf 171 tp.d := ptePerm.d 172 tp.a := ptePerm.a 173 tp.g := ptePerm.g 174 tp.u := ptePerm.u 175 tp.x := ptePerm.x 176 tp.w := ptePerm.w 177 tp.r := ptePerm.r 178 tp 179 } 180 181 implicit def ptwresp_to_tlbperm(ptwResp: PtwSectorResp): TlbPermBundle = { 182 val tp = Wire(new TlbPermBundle) 183 val ptePerm = ptwResp.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 184 tp.pf := ptwResp.pf 185 tp.af := ptwResp.af 186 tp.d := ptePerm.d 187 tp.a := ptePerm.a 188 tp.g := ptePerm.g 189 tp.u := ptePerm.u 190 tp.x := ptePerm.x 191 tp.w := ptePerm.w 192 tp.r := ptePerm.r 193 tp 194 } 195} 196 197trait HasPtwConst extends HasTlbConst with MemoryOpConstants{ 198 val PtwWidth = 2 199 val sourceWidth = { if (l2tlbParams.enablePrefetch) PtwWidth + 1 else PtwWidth} 200 val prefetchID = PtwWidth 201 202 val blockBits = l2tlbParams.blockBytes * 8 203 204 val bPtwWidth = log2Up(PtwWidth) 205 val bSourceWidth = log2Up(sourceWidth) 206 // ptwl3: fully-associated 207 val PtwL3TagLen = if (EnableSv48) vpnnLen + extendVpnnBits else 0 208 // ptwl2: fully-associated 209 val PtwL2TagLen = if (EnableSv48) vpnnLen * 2 + extendVpnnBits else vpnnLen + extendVpnnBits 210 211 /* +-------+----------+-------------+ 212 * | Tag | SetIdx | SectorIdx | 213 * +-------+----------+-------------+ 214 */ 215 // ptwl1: 8-way group-associated 216 val PtwL1SetNum = l2tlbParams.l1nSets 217 val PtwL1SectorSize = blockBits / XLEN 218 val PtwL1IdxLen = log2Up(PtwL1SetNum * PtwL1SectorSize) 219 val PtwL1SectorIdxLen = log2Up(PtwL1SectorSize) 220 val PtwL1SetIdxLen = log2Up(PtwL1SetNum) 221 val PtwL1TagLen = if (EnableSv48) vpnnLen * 3 - PtwL1IdxLen + extendVpnnBits else vpnnLen * 2 - PtwL1IdxLen + extendVpnnBits 222 223 // ptwl0: 16-way group-associated 224 val PtwL0SetNum = l2tlbParams.l0nSets 225 val PtwL0SectorSize = blockBits / XLEN 226 val PtwL0IdxLen = log2Up(PtwL0SetNum * PtwL0SectorSize) 227 val PtwL0SectorIdxLen = log2Up(PtwL0SectorSize) 228 val PtwL0SetIdxLen = log2Up(PtwL0SetNum) 229 val PtwL0TagLen = if (EnableSv48) vpnnLen * 4 - PtwL0IdxLen + extendVpnnBits else vpnnLen * 3 - PtwL0IdxLen + extendVpnnBits 230 231 // super page, including 1GB and 2MB page 232 val SPTagLen = if (EnableSv48) vpnnLen * 3 + extendVpnnBits else vpnnLen * 2 + extendVpnnBits 233 234 // miss queue 235 val MissQueueSize = l2tlbParams.ifilterSize + l2tlbParams.dfilterSize 236 val MemReqWidth = l2tlbParams.llptwsize + 1 + 1 237 val HptwReqId = l2tlbParams.llptwsize + 1 238 val FsmReqID = l2tlbParams.llptwsize 239 val bMemID = log2Up(MemReqWidth) 240 241 def genPtwL1Idx(vpn: UInt) = { 242 (vpn(vpnLen - 1, vpnnLen))(PtwL1IdxLen - 1, 0) 243 } 244 245 def genPtwL1SectorIdx(vpn: UInt) = { 246 genPtwL1Idx(vpn)(PtwL1SectorIdxLen - 1, 0) 247 } 248 249 def genPtwL1SetIdx(vpn: UInt) = { 250 genPtwL1Idx(vpn)(PtwL1SetIdxLen + PtwL1SectorIdxLen - 1, PtwL1SectorIdxLen) 251 } 252 253 def genPtwL0Idx(vpn: UInt) = { 254 vpn(PtwL0IdxLen - 1, 0) 255 } 256 257 def genPtwL0SectorIdx(vpn: UInt) = { 258 genPtwL0Idx(vpn)(PtwL0SectorIdxLen - 1, 0) 259 } 260 261 def dropL0SectorBits(vpn: UInt) = { 262 vpn(vpn.getWidth-1, PtwL0SectorIdxLen) 263 } 264 265 def genPtwL0SetIdx(vpn: UInt) = { 266 genPtwL0Idx(vpn)(PtwL0SetIdxLen + PtwL0SectorIdxLen - 1, PtwL0SectorIdxLen) 267 } 268 269 def MakeAddr(ppn: UInt, off: UInt) = { 270 require(off.getWidth == 9) 271 Cat(ppn, off, 0.U(log2Up(XLEN/8).W))(PAddrBits-1, 0) 272 } 273 274 def MakeGPAddr(ppn: UInt, off: UInt) = { 275 require(off.getWidth == 9 || off.getWidth == 11) 276 (Cat(ppn, 0.U(offLen.W)) + Cat(off, 0.U(log2Up(XLEN / 8).W)))(GPAddrBits - 1, 0) 277 } 278 279 def getVpnn(vpn: UInt, idx: Int): UInt = { 280 vpn(vpnnLen*(idx+1)-1, vpnnLen*idx) 281 } 282 283 def getVpnn(vpn: UInt, idx: UInt): UInt = { 284 MuxLookup(idx, 0.U)(Seq( 285 0.U -> vpn(vpnnLen - 1, 0), 286 1.U -> vpn(vpnnLen * 2 - 1, vpnnLen), 287 2.U -> vpn(vpnnLen * 3 - 1, vpnnLen * 2), 288 3.U -> vpn(vpnnLen * 4 - 1, vpnnLen * 3)) 289 ) 290 } 291 292 def getGVpnn(vpn: UInt, idx: UInt, mode: UInt): UInt = { 293 MuxLookup(idx, 0.U)(Seq( 294 0.U -> vpn(vpnnLen - 1, 0), 295 1.U -> vpn(vpnnLen * 2 - 1, vpnnLen), 296 2.U -> Mux(mode === Sv48, vpn(vpnnLen * 3 - 1, vpnnLen * 2), vpn(vpnnLen * 3 + 1, vpnnLen * 2)), 297 3.U -> vpn(vpnnLen * 4 + 1, vpnnLen * 3)) 298 ) 299 } 300 301 def getVpnClip(vpn: UInt, level: Int) = { 302 // level 2 /* vpnn2 */ 303 // level 1 /* vpnn2 * vpnn1 */ 304 // level 0 /* vpnn2 * vpnn1 * vpnn0*/ 305 vpn(vpnLen - 1, level * vpnnLen) 306 } 307 308 def get_next_line(vpn: UInt) = { 309 Cat(dropL0SectorBits(vpn) + 1.U, 0.U(PtwL0SectorIdxLen.W)) 310 } 311 312 def same_l1entry(vpn1: UInt, vpn2: UInt) = { 313 vpn1(vpnLen-1, vpnnLen) === vpn2(vpnLen-1, vpnnLen) 314 } 315 316 def from_pre(source: UInt) = { 317 (source === prefetchID.U) 318 } 319 320 def sel_data(data: UInt, index: UInt): UInt = { 321 val inner_data = data.asTypeOf(Vec(data.getWidth / XLEN, UInt(XLEN.W))) 322 inner_data(index) 323 } 324 325 // vpn1 and vpn2 is at same cacheline 326 def dup(vpn1: UInt, vpn2: UInt): Bool = { 327 dropL0SectorBits(vpn1) === dropL0SectorBits(vpn2) 328 } 329 330 331 def printVec[T <: Data](x: Seq[T]): Printable = { 332 (0 until x.length).map(i => p"(${i.U})${x(i)} ").reduce(_+_) 333 } 334} 335