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 utils._ 25import utility._ 26import xiangshan.backend.rob.RobPtr 27import xiangshan.backend.fu.util.HasCSRConst 28import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 29import freechips.rocketchip.tilelink._ 30import xiangshan.backend.fu.{PMPReqBundle, PMPConfig} 31import xiangshan.backend.fu.PMPBundle 32 33 34abstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst 35abstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst 36 37 38class PtePermBundle(implicit p: Parameters) extends TlbBundle { 39 val d = Bool() 40 val a = Bool() 41 val g = Bool() 42 val u = Bool() 43 val x = Bool() 44 val w = Bool() 45 val r = Bool() 46 47 override def toPrintable: Printable = { 48 p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// + 49 //(if(hasV) (p"v:${v}") else p"") 50 } 51} 52 53class TlbPMBundle(implicit p: Parameters) extends TlbBundle { 54 val r = Bool() 55 val w = Bool() 56 val x = Bool() 57 val c = Bool() 58 val atomic = Bool() 59 60 def assign_ap(pm: PMPConfig) = { 61 r := pm.r 62 w := pm.w 63 x := pm.x 64 c := pm.c 65 atomic := pm.atomic 66 } 67} 68 69class TlbPermBundle(implicit p: Parameters) extends TlbBundle { 70 val pf = Bool() // NOTE: if this is true, just raise pf 71 val af = Bool() // NOTE: if this is true, just raise af 72 // pagetable perm (software defined) 73 val d = Bool() 74 val a = Bool() 75 val g = Bool() 76 val u = Bool() 77 val x = Bool() 78 val w = Bool() 79 val r = Bool() 80 81 def apply(item: PtwSectorResp) = { 82 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 83 this.pf := item.pf 84 this.af := item.af 85 this.d := ptePerm.d 86 this.a := ptePerm.a 87 this.g := ptePerm.g 88 this.u := ptePerm.u 89 this.x := ptePerm.x 90 this.w := ptePerm.w 91 this.r := ptePerm.r 92 93 this 94 } 95 96 def applyS2(item: HptwResp) = { 97 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 98 this.pf := item.gpf 99 this.af := item.gaf 100 this.d := ptePerm.d 101 this.a := ptePerm.a 102 this.g := ptePerm.g 103 this.u := ptePerm.u 104 this.x := ptePerm.x 105 this.w := ptePerm.w 106 this.r := ptePerm.r 107 108 this 109 } 110 111 override def toPrintable: Printable = { 112 p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " 113 } 114} 115 116class TlbSectorPermBundle(implicit p: Parameters) extends TlbBundle { 117 val pf = Bool() // NOTE: if this is true, just raise pf 118 val af = Bool() // NOTE: if this is true, just raise af 119 // pagetable perm (software defined) 120 val d = Bool() 121 val a = Bool() 122 val g = Bool() 123 val u = Bool() 124 val x = Bool() 125 val w = Bool() 126 val r = Bool() 127 128 def apply(item: PtwSectorResp) = { 129 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 130 this.pf := item.pf 131 this.af := item.af 132 this.d := ptePerm.d 133 this.a := ptePerm.a 134 this.g := ptePerm.g 135 this.u := ptePerm.u 136 this.x := ptePerm.x 137 this.w := ptePerm.w 138 this.r := ptePerm.r 139 140 this 141 } 142 override def toPrintable: Printable = { 143 p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " 144 } 145} 146 147// multi-read && single-write 148// input is data, output is hot-code(not one-hot) 149class CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 150 val io = IO(new Bundle { 151 val r = new Bundle { 152 val req = Input(Vec(readWidth, gen)) 153 val resp = Output(Vec(readWidth, Vec(set, Bool()))) 154 } 155 val w = Input(new Bundle { 156 val valid = Bool() 157 val bits = new Bundle { 158 val index = UInt(log2Up(set).W) 159 val data = gen 160 } 161 }) 162 }) 163 164 val wordType = UInt(gen.getWidth.W) 165 val array = Reg(Vec(set, wordType)) 166 167 io.r.resp.zipWithIndex.map{ case (a,i) => 168 a := array.map(io.r.req(i).asUInt === _) 169 } 170 171 when (io.w.valid) { 172 array(io.w.bits.index) := io.w.bits.data.asUInt 173 } 174} 175 176class TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 177 require(pageNormal || pageSuper) 178 179 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 180 else UInt(vpnLen.W) 181 val asid = UInt(asidLen.W) 182 val level = if (!pageNormal) Some(UInt(1.W)) 183 else if (!pageSuper) None 184 else Some(UInt(2.W)) 185 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 186 else UInt(ppnLen.W) 187 val perm = new TlbPermBundle 188 189 val g_perm = new TlbPermBundle 190 val vmid = UInt(vmidLen.W) 191 val s2xlate = UInt(2.W) 192 193 194 /** level usage: 195 * !PageSuper: page is only normal, level is None, match all the tag 196 * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 197 * bits0 0: need mid 9bits 198 * 1: no need mid 9bits 199 * PageSuper && PageNormal: page hold all the three type, 200 * bits0 0: need low 9bits 201 * bits1 0: need mid 9bits 202 */ 203 204 205 def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, vmid: UInt, hasS2xlate: Bool, onlyS2: Bool): Bool = { 206 val asid_hit = Mux(hasS2xlate && onlyS2, true.B, if (ignoreAsid) true.B else (this.asid === asid)) 207 val vmid_hit = Mux(hasS2xlate, this.vmid === vmid, true.B) 208 209 // NOTE: for timing, dont care low set index bits at hit check 210 // do not need store the low bits actually 211 if (!pageSuper) asid_hit && drop_set_equal(vpn, tag, nSets) && vmid_hit 212 else if (!pageNormal) { 213 val tag_match_hi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*3-1, vpnnLen*2) 214 val tag_match_mi = tag(vpnnLen-1, 0) === vpn(vpnnLen*2-1, vpnnLen) 215 val tag_match = tag_match_hi && (level.get.asBool || tag_match_mi) 216 asid_hit && tag_match && vmid_hit 217 } 218 else { 219 val tmp_level = level.get 220 val tag_match_hi = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2) 221 val tag_match_mi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*2-1, vpnnLen) 222 val tag_match_lo = tag(vpnnLen-1, 0) === vpn(vpnnLen-1, 0) // if pageNormal is false, this will always be false 223 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 224 asid_hit && tag_match && vmid_hit 225 } 226 } 227 228 def apply(item: PtwRespS2, pm: PMPConfig): TlbEntry = { 229 this.asid := item.s1.entry.asid 230 val inner_level = item.s1.entry.level.getOrElse(0.U) max item.s2.entry.level.getOrElse(0.U) 231 this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U)(Seq( 232 0.U -> 3.U, 233 1.U -> 1.U, 234 2.U -> 0.U )) 235 else if (pageSuper) ~inner_level(0) 236 else 0.U }) 237 val s1tag = {if (pageNormal) Cat(item.s1.entry.tag, OHToUInt(item.s1.pteidx)) else item.s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)} 238 val s2tag = {if (pageNormal) item.s2.entry.tag else item.s2.entry.tag(vpnLen - 1, vpnnLen)} 239 this.tag := Mux(item.s2xlate === onlyStage2, s2tag, s1tag) 240 241 val s1ppn = { 242 if (!pageNormal) item.s1.entry.ppn(sectorgvpnLen - 1, vpnnLen - sectortlbwidth) 243 else Cat(item.s1.entry.ppn, item.s1.ppn_low(OHToUInt(item.s1.pteidx))) 244 } 245 val s2ppn = { 246 if (!pageNormal) item.s2.entry.ppn(ppnLen - 1, vpnnLen) 247 else item.s2.entry.ppn 248 } 249 this.ppn := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn, s2ppn) 250 this.perm.apply(item.s1) 251 this.vmid := item.s1.entry.vmid.getOrElse(0.U) 252 this.g_perm.applyS2(item.s2) 253 this.s2xlate := item.s2xlate 254 this 255 } 256 257 // 4KB is normal entry, 2MB/1GB is considered as super entry 258 def is_normalentry(): Bool = { 259 if (!pageSuper) { true.B } 260 else if (!pageNormal) { false.B } 261 else { level.get === 0.U } 262 } 263 264 265 def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 266 val inner_level = level.getOrElse(0.U) 267 val ppn_res = if (!pageSuper) ppn 268 else if (!pageNormal) Cat(ppn(ppnLen-vpnnLen-1, vpnnLen), 269 Mux(inner_level(0), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen-1,0)), 270 vpn(vpnnLen-1, 0)) 271 else Cat(ppn(ppnLen-1, vpnnLen*2), 272 Mux(inner_level(1), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen*2-1, vpnnLen)), 273 Mux(inner_level(0), vpn(vpnnLen-1, 0), ppn(vpnnLen-1, 0))) 274 275 if (saveLevel) Cat(ppn(ppn.getWidth-1, vpnnLen*2), RegEnable(ppn_res(vpnnLen*2-1, 0), valid)) 276 else ppn_res 277 } 278 279 override def toPrintable: Printable = { 280 val inner_level = level.getOrElse(2.U) 281 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 282 } 283 284} 285 286class TlbSectorEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 287 require(pageNormal || pageSuper) 288 289 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 290 else UInt(sectorvpnLen.W) 291 val asid = UInt(asidLen.W) 292 val level = if (!pageNormal) Some(UInt(1.W)) 293 else if (!pageSuper) None 294 else Some(UInt(2.W)) 295 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 296 else UInt(sectorppnLen.W) //only used when disable s2xlate 297 val perm = new TlbSectorPermBundle 298 val valididx = Vec(tlbcontiguous, Bool()) 299 val pteidx = Vec(tlbcontiguous, Bool()) 300 val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 301 302 val g_perm = new TlbPermBundle 303 val vmid = UInt(vmidLen.W) 304 val s2xlate = UInt(2.W) 305 306 307 /** level usage: 308 * !PageSuper: page is only normal, level is None, match all the tag 309 * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 310 * bits0 0: need mid 9bits 311 * 1: no need mid 9bits 312 * PageSuper && PageNormal: page hold all the three type, 313 * bits0 0: need low 9bits 314 * bits1 0: need mid 9bits 315 */ 316 317 def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, vmid: UInt, hasS2xlate: Bool, onlyS2: Bool = false.B, onlyS1: Bool = false.B): Bool = { 318 val asid_hit = Mux(hasS2xlate && onlyS2, true.B, if (ignoreAsid) true.B else (this.asid === asid)) 319 val addr_low_hit = valididx(vpn(2, 0)) 320 val vmid_hit = Mux(hasS2xlate, this.vmid === vmid, true.B) 321 val isPageSuper = !(level.getOrElse(0.U) === 0.U) 322 val pteidx_hit = Mux(hasS2xlate && !isPageSuper && !onlyS1, pteidx(vpn(2, 0)), true.B) 323 // NOTE: for timing, dont care low set index bits at hit check 324 // do not need store the low bits actually 325 if (!pageSuper) asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) && addr_low_hit && vmid_hit && pteidx_hit 326 else if (!pageNormal) { 327 val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 328 val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen) 329 val tag_match = tag_match_hi && (level.get.asBool || tag_match_mi) 330 asid_hit && tag_match && addr_low_hit && vmid_hit && pteidx_hit 331 } 332 else { 333 val tmp_level = level.get 334 val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 335 val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 336 val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false 337 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 338 asid_hit && tag_match && addr_low_hit && vmid_hit && pteidx_hit 339 } 340 } 341 342 def wbhit(data: PtwRespS2, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, s2xlate: UInt): Bool = { 343 val s1vpn = data.s1.entry.tag 344 val s2vpn = data.s2.entry.tag(vpnLen - 1, sectortlbwidth) 345 val wb_vpn = Mux(s2xlate === onlyStage2, s2vpn, s1vpn) 346 val vpn = Cat(wb_vpn, 0.U(sectortlbwidth.W)) 347 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 348 val vpn_hit = Wire(Bool()) 349 val index_hit = Wire(Vec(tlbcontiguous, Bool())) 350 val wb_valididx = Wire(Vec(tlbcontiguous, Bool())) 351 val hasS2xlate = this.s2xlate =/= noS2xlate 352 val onlyS1 = this.s2xlate === onlyStage1 353 val onlyS2 = this.s2xlate === onlyStage2 354 val pteidx_hit = MuxCase(true.B, Seq( 355 onlyS2 -> (VecInit(UIntToOH(data.s2.entry.tag(sectortlbwidth - 1, 0))).asUInt === pteidx.asUInt), 356 hasS2xlate -> (pteidx.asUInt === data.s1.pteidx.asUInt) 357 )) 358 wb_valididx := Mux(s2xlate === onlyStage2, VecInit(UIntToOH(data.s2.entry.tag(sectortlbwidth - 1, 0)).asBools), data.s1.valididx) 359 val s2xlate_hit = s2xlate === this.s2xlate 360 // NOTE: for timing, dont care low set index bits at hit check 361 // do not need store the low bits actually 362 if (!pageSuper) { 363 vpn_hit := asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) 364 } 365 else if (!pageNormal) { 366 val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 367 val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen) 368 val tag_match = tag_match_hi && (level.get.asBool || tag_match_mi) 369 vpn_hit := asid_hit && tag_match 370 } 371 else { 372 val tmp_level = level.get 373 val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 374 val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 375 val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false 376 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 377 vpn_hit := asid_hit && tag_match 378 } 379 380 for (i <- 0 until tlbcontiguous) { 381 index_hit(i) := wb_valididx(i) && valididx(i) 382 } 383 384 // For example, tlb req to page cache with vpn 0x10 385 // At this time, 0x13 has not been paged, so page cache only resp 0x10 386 // When 0x13 refill to page cache, previous item will be flushed 387 // Now 0x10 and 0x13 are both valid in page cache 388 // However, when 0x13 refill to tlb, will trigger multi hit 389 // So will only trigger multi-hit when PopCount(data.valididx) = 1 390 vpn_hit && index_hit.reduce(_ || _) && PopCount(wb_valididx) === 1.U && s2xlate_hit && pteidx_hit 391 } 392 393 def apply(item: PtwRespS2): TlbSectorEntry = { 394 this.asid := item.s1.entry.asid 395 val inner_level = MuxLookup(item.s2xlate, 2.U)(Seq( 396 onlyStage1 -> item.s1.entry.level.getOrElse(0.U), 397 onlyStage2 -> item.s2.entry.level.getOrElse(0.U), 398 allStage -> (item.s1.entry.level.getOrElse(0.U) max item.s2.entry.level.getOrElse(0.U)), 399 noS2xlate -> item.s1.entry.level.getOrElse(0.U) 400 )) 401 this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U)(Seq( 402 0.U -> 3.U, 403 1.U -> 1.U, 404 2.U -> 0.U )) 405 else if (pageSuper) ~inner_level(0) 406 else 0.U }) 407 this.perm.apply(item.s1) 408 409 val s1tag = {if (pageNormal) item.s1.entry.tag else item.s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)} 410 val s2tag = {if (pageNormal) item.s2.entry.tag(vpnLen - 1, sectortlbwidth) else item.s2.entry.tag(vpnLen - 1, vpnnLen)} 411 // if stage1 page is larger than stage2 page, need to merge s1tag and s2tag. 412 val s1tagFix = { 413 if (pageNormal){ 414 MuxCase(s1tag, Seq( 415 (item.s1.entry.level.getOrElse(0.U) === 0.U && item.s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 2 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)), 416 (item.s1.entry.level.getOrElse(0.U) === 0.U && item.s2.entry.level.getOrElse(0.U) === 2.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)), 417 (item.s1.entry.level.getOrElse(0.U) === 1.U && item.s2.entry.level.getOrElse(0.U) === 2.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth), item.s2.entry.tag(vpnnLen - 1, sectortlbwidth)) 418 )) 419 } else { 420 MuxCase(s1tag, Seq( 421 (item.s1.entry.level.getOrElse(0.U) === 0.U && item.s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 2 - 1, vpnnLen)) 422 )) 423 }} 424 this.tag := Mux(item.s2xlate === onlyStage2, s2tag, Mux(item.s2xlate === allStage, s1tagFix, s1tag)) 425 val s2page_pageSuper = item.s2.entry.level.getOrElse(0.U) =/= 2.U 426 this.pteidx := Mux(item.s2xlate === onlyStage2, VecInit(UIntToOH(item.s2.entry.tag(sectortlbwidth - 1, 0)).asBools), item.s1.pteidx) 427 val s2_valid = Mux(s2page_pageSuper, VecInit(Seq.fill(tlbcontiguous)(true.B)), VecInit(UIntToOH(item.s2.entry.tag(sectortlbwidth - 1, 0)).asBools)) 428 this.valididx := Mux(item.s2xlate === onlyStage2, s2_valid, item.s1.valididx) 429 // if stage2 page is larger than stage1 page, need to merge s2tag and s2ppn to get a new s2ppn. 430 val s1ppn = { 431 if (!pageNormal) item.s1.entry.ppn(sectorgvpnLen - 1, vpnnLen - sectortlbwidth) else item.s1.entry.ppn 432 } 433 val s1ppn_low = item.s1.ppn_low 434 val s2ppn = { 435 if (!pageNormal) 436 MuxLookup(item.s2.entry.level.getOrElse(0.U), item.s2.entry.ppn(ppnLen - 1, vpnnLen))(Seq( 437 0.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 2), item.s2.entry.tag(vpnnLen * 2 - 1, vpnnLen)), 438 )) 439 else 440 MuxLookup(item.s2.entry.level.getOrElse(0.U), item.s2.entry.ppn(ppnLen - 1, sectortlbwidth))(Seq( 441 0.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 2), item.s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)), 442 1.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen), item.s2.entry.tag(vpnnLen - 1, sectortlbwidth)) 443 )) 444 } 445 val s2ppn_tmp = { 446 MuxLookup(item.s2.entry.level.getOrElse(0.U), item.s2.entry.ppn(ppnLen - 1, 0))(Seq( 447 0.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 2), item.s2.entry.tag(vpnnLen * 2 - 1, 0)), 448 1.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen), item.s2.entry.tag(vpnnLen - 1, 0)) 449 )) 450 } 451 val s2ppn_low = VecInit(Seq.fill(tlbcontiguous)(s2ppn_tmp(sectortlbwidth - 1, 0))) 452 this.ppn := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn, s2ppn) 453 this.ppn_low := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn_low, s2ppn_low) 454 this.vmid := item.s1.entry.vmid.getOrElse(0.U) 455 this.g_perm.applyS2(item.s2) 456 this.s2xlate := item.s2xlate 457 this 458 } 459 460 // 4KB is normal entry, 2MB/1GB is considered as super entry 461 def is_normalentry(): Bool = { 462 if (!pageSuper) { true.B } 463 else if (!pageNormal) { false.B } 464 else { level.get === 0.U } 465 } 466 467 468 def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 469 val inner_level = level.getOrElse(0.U) 470 val ppn_res = if (!pageSuper) Cat(ppn, ppn_low(vpn(sectortlbwidth - 1, 0))) 471 else if (!pageNormal) Cat(ppn(ppnLen - vpnnLen - 1, vpnnLen), 472 Mux(inner_level(0), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen - 1,0)), 473 vpn(vpnnLen - 1, 0)) 474 else Cat(ppn(sectorppnLen - 1, vpnnLen * 2 - sectortlbwidth), 475 Mux(inner_level(1), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)), 476 Mux(inner_level(0), vpn(vpnnLen - 1, 0), Cat(ppn(vpnnLen - sectortlbwidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))))) 477 478 if (saveLevel) { 479 if (ppn.getWidth == ppnLen - vpnnLen) { 480 Cat(ppn(ppn.getWidth - 1, vpnnLen * 2), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid)) 481 } else { 482 require(ppn.getWidth == sectorppnLen) 483 Cat(ppn(ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid)) 484 } 485 } 486 else ppn_res 487 } 488 489 def hasS2xlate(): Bool = { 490 this.s2xlate =/= noS2xlate 491 } 492 493 override def toPrintable: Printable = { 494 val inner_level = level.getOrElse(2.U) 495 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 496 } 497 498} 499 500object TlbCmd { 501 def read = "b00".U 502 def write = "b01".U 503 def exec = "b10".U 504 505 def atom_read = "b100".U // lr 506 def atom_write = "b101".U // sc / amo 507 508 def apply() = UInt(3.W) 509 def isRead(a: UInt) = a(1,0)===read 510 def isWrite(a: UInt) = a(1,0)===write 511 def isExec(a: UInt) = a(1,0)===exec 512 513 def isAtom(a: UInt) = a(2) 514 def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 515} 516 517class TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 518 val r = new Bundle { 519 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 520 val vpn = Output(UInt(vpnLen.W)) 521 val s2xlate = Output(UInt(2.W)) 522 }))) 523 val resp = Vec(ports, ValidIO(new Bundle{ 524 val hit = Output(Bool()) 525 val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 526 val perm = Vec(nDups, Output(new TlbSectorPermBundle())) 527 val g_perm = Vec(nDups, Output(new TlbPermBundle())) 528 val s2xlate = Vec(nDups, Output(UInt(2.W))) 529 })) 530 } 531 val w = Flipped(ValidIO(new Bundle { 532 val wayIdx = Output(UInt(log2Up(nWays).W)) 533 val data = Output(new PtwRespS2) 534 })) 535 val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays)) 536 537 def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate:UInt): Unit = { 538 this.r.req(i).valid := valid 539 this.r.req(i).bits.vpn := vpn 540 this.r.req(i).bits.s2xlate := s2xlate 541 542 } 543 544 def r_resp_apply(i: Int) = { 545 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.g_perm) 546 } 547 548 def w_apply(valid: Bool, wayIdx: UInt, data: PtwRespS2): Unit = { 549 this.w.valid := valid 550 this.w.bits.wayIdx := wayIdx 551 this.w.bits.data := data 552 } 553 554} 555 556class TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 557 val r = new Bundle { 558 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 559 val vpn = Output(UInt(vpnLen.W)) 560 val s2xlate = Output(UInt(2.W)) 561 }))) 562 val resp = Vec(ports, ValidIO(new Bundle{ 563 val hit = Output(Bool()) 564 val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 565 val perm = Vec(nDups, Output(new TlbPermBundle())) 566 val g_perm = Vec(nDups, Output(new TlbPermBundle())) 567 val s2xlate = Vec(nDups, Output(UInt(2.W))) 568 })) 569 } 570 val w = Flipped(ValidIO(new Bundle { 571 val data = Output(new PtwRespS2) 572 })) 573 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null 574 575 def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate: UInt): Unit = { 576 this.r.req(i).valid := valid 577 this.r.req(i).bits.vpn := vpn 578 this.r.req(i).bits.s2xlate := s2xlate 579 } 580 581 def r_resp_apply(i: Int) = { 582 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.g_perm, this.r.resp(i).bits.s2xlate) 583 } 584 585 def w_apply(valid: Bool, data: PtwRespS2): Unit = { 586 this.w.valid := valid 587 this.w.bits.data := data 588 } 589} 590 591class ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 592 val sets = Output(UInt(log2Up(nSets).W)) 593 val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W))) 594} 595 596class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 597 val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays))) 598 599 val refillIdx = Output(UInt(log2Up(nWays).W)) 600 val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 601 602 def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 603 for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) { 604 ac_rep := ac_tlb 605 } 606 this.chosen_set := get_set_idx(vpn, nSets) 607 in.map(a => a.refillIdx := this.refillIdx) 608 } 609} 610 611class TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 612 TlbBundle { 613 val page = new ReplaceIO(Width, q.NSets, q.NWays) 614 615 def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 616 this.page.apply_sep(in.map(_.page), vpn) 617 } 618 619} 620 621class MemBlockidxBundle(implicit p: Parameters) extends TlbBundle { 622 val is_ld = Bool() 623 val is_st = Bool() 624 val idx = UInt(log2Ceil(VirtualLoadQueueMaxStoreQueueSize).W) 625} 626 627class TlbReq(implicit p: Parameters) extends TlbBundle { 628 val vaddr = Output(UInt(VAddrBits.W)) 629 val cmd = Output(TlbCmd()) 630 val hyperinst = Output(Bool()) 631 val hlvx = Output(Bool()) 632 val size = Output(UInt(log2Ceil(log2Ceil(VLEN/8)+1).W)) 633 val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache 634 val memidx = Output(new MemBlockidxBundle) 635 // do not translate, but still do pmp/pma check 636 val no_translate = Output(Bool()) 637 val debug = new Bundle { 638 val pc = Output(UInt(XLEN.W)) 639 val robIdx = Output(new RobPtr) 640 val isFirstIssue = Output(Bool()) 641 } 642 643 // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead 644 override def toPrintable: Printable = { 645 p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}" 646 } 647} 648 649class TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 650 val ld = Output(Bool()) 651 val st = Output(Bool()) 652 val instr = Output(Bool()) 653} 654 655class TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 656 val paddr = Vec(nDups, Output(UInt(PAddrBits.W))) 657 val gpaddr = Vec(nDups, Output(UInt(GPAddrBits.W))) 658 val miss = Output(Bool()) 659 val excp = Vec(nDups, new Bundle { 660 val gpf = new TlbExceptionBundle() 661 val pf = new TlbExceptionBundle() 662 val af = new TlbExceptionBundle() 663 }) 664 val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state 665 val memidx = Output(new MemBlockidxBundle) 666 667 val debug = new Bundle { 668 val robIdx = Output(new RobPtr) 669 val isFirstIssue = Output(Bool()) 670 } 671 override def toPrintable: Printable = { 672 p"paddr:0x${Hexadecimal(paddr(0))} miss:${miss} excp.pf: ld:${excp(0).pf.ld} st:${excp(0).pf.st} instr:${excp(0).pf.instr} ptwBack:${ptwBack}" 673 } 674} 675 676class TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 677 val req = DecoupledIO(new TlbReq) 678 val req_kill = Output(Bool()) 679 val resp = Flipped(DecoupledIO(new TlbResp(nRespDups))) 680} 681 682class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 683 val req = Vec(Width, DecoupledIO(new PtwReq)) 684 val resp = Flipped(DecoupledIO(new PtwRespS2)) 685 686 687 override def toPrintable: Printable = { 688 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 689 } 690} 691 692class TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 693 val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx)) 694 val resp = Flipped(DecoupledIO(new PtwRespS2withMemIdx())) 695 696 697 override def toPrintable: Printable = { 698 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 699 } 700} 701 702class TlbHintReq(implicit p: Parameters) extends TlbBundle { 703 val id = Output(UInt(log2Up(loadfiltersize).W)) 704 val full = Output(Bool()) 705} 706 707class TLBHintResp(implicit p: Parameters) extends TlbBundle { 708 val id = Output(UInt(log2Up(loadfiltersize).W)) 709 // When there are multiple matching entries for PTW resp in filter 710 // e.g. vaddr 0, 0x80000000. vaddr 1, 0x80010000 711 // these two vaddrs are not in a same 4K Page, so will send to ptw twice 712 // However, when ptw resp, if they are in a 1G or 2M huge page 713 // The two entries will both hit, and both need to replay 714 val replay_all = Output(Bool()) 715} 716 717class TlbHintIO(implicit p: Parameters) extends TlbBundle { 718 val req = Vec(backendParams.LdExuCnt, new TlbHintReq) 719 val resp = ValidIO(new TLBHintResp) 720} 721 722class MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 723 val sfence = Input(new SfenceBundle) 724 val csr = Input(new TlbCsrBundle) 725 726 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 727 this.sfence <> sfence 728 this.csr <> csr 729 } 730 731 // overwrite satp. write satp will cause flushpipe but csr.priv won't 732 // satp will be dealyed several cycles from writing, but csr.priv won't 733 // so inside mmu, these two signals should be divided 734 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = { 735 this.sfence <> sfence 736 this.csr <> csr 737 this.csr.satp := satp 738 } 739} 740 741class TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle { 742 val valid = Bool() 743 val memidx = new MemBlockidxBundle 744} 745 746class TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends 747 MMUIOBaseBundle { 748 val hartId = Input(UInt(hartIdLen.W)) 749 val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups))) 750 val flushPipe = Vec(Width, Input(Bool())) 751 val redirect = Flipped(ValidIO(new Redirect)) // flush the signal need_gpa in tlb 752 val ptw = new TlbPtwIOwithMemIdx(Width) 753 val refill_to_mem = Output(new TlbRefilltoMemIO()) 754 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 755 val pmp = Vec(Width, ValidIO(new PMPReqBundle(q.lgMaxSize))) 756 val tlbreplay = Vec(Width, Output(Bool())) 757} 758 759class VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 760 val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx())) 761 val resp = Flipped(DecoupledIO(new Bundle { 762 val data = new PtwRespS2withMemIdx 763 val vector = Output(Vec(Width, Bool())) 764 val getGpa = Output(Vec(Width, Bool())) 765 })) 766 767 def connect(normal: TlbPtwIOwithMemIdx): Unit = { 768 req <> normal.req 769 resp.ready := normal.resp.ready 770 normal.resp.bits := resp.bits.data 771 normal.resp.valid := resp.valid 772 } 773} 774 775/**************************** L2TLB *************************************/ 776abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 777abstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer) 778 with HasXSParameter with HasPtwConst 779 780class PteBundle(implicit p: Parameters) extends PtwBundle{ 781 val reserved = UInt(pteResLen.W) 782 val ppn_high = UInt(ppnHignLen.W) 783 val ppn = UInt(ppnLen.W) 784 val rsw = UInt(2.W) 785 val perm = new Bundle { 786 val d = Bool() 787 val a = Bool() 788 val g = Bool() 789 val u = Bool() 790 val x = Bool() 791 val w = Bool() 792 val r = Bool() 793 val v = Bool() 794 } 795 796 def unaligned(level: UInt) = { 797 isLeaf() && !(level === 2.U || 798 level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 799 level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U) 800 } 801 802 def isPf(level: UInt) = { 803 !perm.v || (!perm.r && perm.w) || unaligned(level) 804 } 805 806 // paddr of Xiangshan is 36 bits but ppn of sv39 is 44 bits 807 // access fault will be raised when ppn >> ppnLen is not zero 808 def isAf() = { 809 !(ppn_high === 0.U) 810 } 811 812 def isStage1Af() = { 813 !((Cat(ppn_high, ppn) >> gvpnLen) === 0.U) 814 } 815 816 def isLeaf() = { 817 perm.r || perm.x || perm.w 818 } 819 820 def getPerm() = { 821 val pm = Wire(new PtePermBundle) 822 pm.d := perm.d 823 pm.a := perm.a 824 pm.g := perm.g 825 pm.u := perm.u 826 pm.x := perm.x 827 pm.w := perm.w 828 pm.r := perm.r 829 pm 830 } 831 def getPPN() = { 832 Cat(ppn_high, ppn) 833 } 834 override def toPrintable: Printable = { 835 p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 836 } 837} 838 839class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 840 val tag = UInt(tagLen.W) 841 val asid = UInt(asidLen.W) 842 val vmid = if (HasHExtension) Some(UInt(vmidLen.W)) else None 843 val ppn = UInt(ppnLen.W) 844 val perm = if (hasPerm) Some(new PtePermBundle) else None 845 val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None 846 val prefetch = Bool() 847 val v = Bool() 848 849 def is_normalentry(): Bool = { 850 if (!hasLevel) true.B 851 else level.get === 2.U 852 } 853 854 def genPPN(vpn: UInt): UInt = { 855 if (!hasLevel) ppn 856 else MuxLookup(level.get, 0.U)(Seq( 857 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 858 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 859 2.U -> ppn) 860 ) 861 } 862 863 //s2xlate control whether compare vmid or not 864 def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool) = { 865 require(vpn.getWidth == vpnLen) 866// require(this.asid.getWidth <= asid.getWidth) 867 val asid_value = Mux(s2xlate, vasid, asid) 868 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid_value) 869 val vmid_hit = Mux(s2xlate, (this.vmid.getOrElse(0.U) === vmid), true.B) 870 if (allType) { 871 require(hasLevel) 872 val hit0 = tag(tagLen - 1, vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2) 873 val hit1 = tag(vpnnLen*2 - 1, vpnnLen) === vpn(vpnnLen*2 - 1, vpnnLen) 874 val hit2 = tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 875 876 asid_hit && vmid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 877 } else if (hasLevel) { 878 val hit0 = tag(tagLen - 1, tagLen - vpnnLen - extendVpnnBits) === vpn(vpnLen - 1, vpnLen - vpnnLen - extendVpnnBits) 879 val hit1 = tag(tagLen - vpnnLen - extendVpnnBits - 1, tagLen - vpnnLen * 2 - extendVpnnBits) === vpn(vpnLen - vpnnLen - extendVpnnBits - 1, vpnLen - vpnnLen * 2 - extendVpnnBits) 880 881 asid_hit && vmid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) 882 } else { 883 asid_hit && vmid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 884 } 885 } 886 887 def refill(vpn: UInt, asid: UInt, vmid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B): Unit = { 888 require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 889 890 tag := vpn(vpnLen - 1, vpnLen - tagLen) 891 ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 892 perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 893 this.asid := asid 894 this.vmid.map(_ := vmid) 895 this.prefetch := prefetch 896 this.v := valid 897 this.level.map(_ := level) 898 } 899 900 def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = { 901 val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 902 e.refill(vpn, asid, pte, level, prefetch, valid) 903 e 904 } 905 906 907 908 override def toPrintable: Printable = { 909 // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 910 p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " + 911 (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 912 (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 913 p"prefetch:${prefetch}" 914 } 915} 916 917class PtwSectorEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwEntry(tagLen, hasPerm, hasLevel) { 918 override val ppn = UInt(sectorgvpnLen.W) 919} 920 921class PtwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwSectorEntry(tagLen, hasPerm, hasLevel) { 922 val ppn_low = UInt(sectortlbwidth.W) 923 val af = Bool() 924 val pf = Bool() 925} 926 927class HptwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwMergeEntry(tagLen, hasPerm, hasLevel) 928 929class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean, hasReservedBitforMbist: Boolean)(implicit p: Parameters) extends PtwBundle { 930 require(log2Up(num)==log2Down(num)) 931 // NOTE: hasPerm means that is leaf or not. 932 933 val tag = UInt(tagLen.W) 934 val asid = UInt(asidLen.W) 935 val vmid = Some(UInt(vmidLen.W)) 936 val ppns = Vec(num, UInt(gvpnLen.W)) 937 val vs = Vec(num, Bool()) 938 val af = Vec(num, Bool()) 939 val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 940 val prefetch = Bool() 941 val reservedbit = if(hasReservedBitforMbist) Some(Bool()) else None 942 // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 943 // NOTE: vs is used for different usage: 944 // for l3, which store the leaf(leaves), vs is page fault or not. 945 // for l2, which shoule not store leaf, vs is valid or not, that will anticipate in hit check 946 // Because, l2 should not store leaf(no perm), it doesn't store perm. 947 // If l2 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful. 948 // TODO: divide vs into validVec and pfVec 949 // for l2: may valid but pf, so no need for page walk, return random pte with pf. 950 951 def tagClip(vpn: UInt) = { 952 require(vpn.getWidth == vpnLen) 953 vpn(vpnLen - 1, vpnLen - tagLen) 954 } 955 956 def sectorIdxClip(vpn: UInt, level: Int) = { 957 getVpnClip(vpn, level)(log2Up(num) - 1, 0) 958 } 959 960 def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid:UInt, ignoreAsid: Boolean = false, s2xlate: Bool) = { 961 val asid_value = Mux(s2xlate, vasid, asid) 962 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid_value) 963 val vmid_hit = Mux(s2xlate, this.vmid.getOrElse(0.U) === vmid, true.B) 964 asid_hit && vmid_hit && tag === tagClip(vpn) && !af(sectorIdxClip(vpn, level)) && (if (hasPerm) true.B else vs(sectorIdxClip(vpn, level))) 965 } 966 967 def genEntries(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 968 require((data.getWidth / XLEN) == num, 969 s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 970 971 val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm, hasReservedBitforMbist)) 972 ps.tag := tagClip(vpn) 973 ps.asid := asid 974 ps.vmid.map(_ := vmid) 975 ps.prefetch := prefetch 976 for (i <- 0 until num) { 977 val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 978 ps.ppns(i) := pte.ppn 979 ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 980 ps.af(i) := pte.isAf() 981 ps.perms.map(_(i) := pte.perm) 982 } 983 ps.reservedbit.map(_ := true.B) 984 ps 985 } 986 987 override def toPrintable: Printable = { 988 // require(num == 4, "if num is not 4, please comment this toPrintable") 989 // NOTE: if num is not 4, please comment this toPrintable 990 val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 991 p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 992 (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 993 } 994} 995 996class PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean, hasReservedBitforMbist: Boolean = false)(implicit p: Parameters) extends PtwBundle { 997 val entries = new PtwEntries(num, tagLen, level, hasPerm, hasReservedBitforMbist) 998 999 val ecc_block = XLEN 1000 val ecc_info = get_ecc_info() 1001 val ecc = if (l2tlbParams.enablePTWECC) Some(UInt(ecc_info._1.W)) else None 1002 1003 def get_ecc_info(): (Int, Int, Int, Int) = { 1004 val eccBits_per = eccCode.width(ecc_block) - ecc_block 1005 1006 val data_length = entries.getWidth 1007 val data_align_num = data_length / ecc_block 1008 val data_not_align = (data_length % ecc_block) != 0 // ugly code 1009 val data_unalign_length = data_length - data_align_num * ecc_block 1010 val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length 1011 1012 val eccBits = eccBits_per * data_align_num + eccBits_unalign 1013 (eccBits, eccBits_per, data_align_num, data_unalign_length) 1014 } 1015 1016 def encode() = { 1017 val data = entries.asUInt 1018 val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W))) 1019 for (i <- 0 until ecc_info._3) { 1020 ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block 1021 } 1022 if (ecc_info._4 != 0) { 1023 val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4 1024 ecc.map(_ := Cat(ecc_unaligned, ecc_slices.asUInt)) 1025 } else { ecc.map(_ := ecc_slices.asUInt)} 1026 } 1027 1028 def decode(): Bool = { 1029 val data = entries.asUInt 1030 val res = Wire(Vec(ecc_info._3 + 1, Bool())) 1031 for (i <- 0 until ecc_info._3) { 1032 res(i) := {if (ecc_info._2 != 0) eccCode.decode(Cat(ecc.get((i+1)*ecc_info._2-1, i*ecc_info._2), data((i+1)*ecc_block-1, i*ecc_block))).error else false.B} 1033 } 1034 if (ecc_info._2 != 0 && ecc_info._4 != 0) { 1035 res(ecc_info._3) := eccCode.decode( 1036 Cat(ecc.get(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error 1037 } else { res(ecc_info._3) := false.B } 1038 1039 Cat(res).orR 1040 } 1041 1042 def gen(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 1043 this.entries := entries.genEntries(vpn, asid, vmid, data, levelUInt, prefetch) 1044 this.encode() 1045 } 1046} 1047 1048class PtwReq(implicit p: Parameters) extends PtwBundle { 1049 val vpn = UInt(vpnLen.W) //vpn or gvpn 1050 val s2xlate = UInt(2.W) 1051 def hasS2xlate(): Bool = { 1052 this.s2xlate =/= noS2xlate 1053 } 1054 def isOnlyStage2: Bool = { 1055 this.s2xlate === onlyStage2 1056 } 1057 override def toPrintable: Printable = { 1058 p"vpn:0x${Hexadecimal(vpn)}" 1059 } 1060} 1061 1062class PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq { 1063 val memidx = new MemBlockidxBundle 1064 val getGpa = Bool() // this req is to get gpa when having guest page fault 1065} 1066 1067class PtwResp(implicit p: Parameters) extends PtwBundle { 1068 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 1069 val pf = Bool() 1070 val af = Bool() 1071 1072 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 1073 this.entry.level.map(_ := level) 1074 this.entry.tag := vpn 1075 this.entry.perm.map(_ := pte.getPerm()) 1076 this.entry.ppn := pte.ppn 1077 this.entry.prefetch := DontCare 1078 this.entry.asid := asid 1079 this.entry.v := !pf 1080 this.pf := pf 1081 this.af := af 1082 } 1083 1084 override def toPrintable: Printable = { 1085 p"entry:${entry} pf:${pf} af:${af}" 1086 } 1087} 1088 1089class HptwResp(implicit p: Parameters) extends PtwBundle { 1090 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 1091 val gpf = Bool() 1092 val gaf = Bool() 1093 1094 def apply(gpf: Bool, gaf: Bool, level: UInt, pte: PteBundle, vpn: UInt, vmid: UInt) = { 1095 val resp_pte = Mux(gaf, 0.U.asTypeOf(pte), pte) 1096 this.entry.level.map(_ := level) 1097 this.entry.tag := vpn 1098 this.entry.perm.map(_ := resp_pte.getPerm()) 1099 this.entry.ppn := resp_pte.ppn 1100 this.entry.prefetch := DontCare 1101 this.entry.asid := DontCare 1102 this.entry.vmid.map(_ := vmid) 1103 this.entry.v := !gpf 1104 this.gpf := gpf 1105 this.gaf := gaf 1106 } 1107 1108 def genPPNS2(vpn: UInt): UInt = { 1109 MuxLookup(entry.level.get, 0.U)(Seq( 1110 0.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)), 1111 1.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen), vpn(vpnnLen - 1, 0)), 1112 2.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, 0)) 1113 )) 1114 } 1115 1116 def hit(gvpn: UInt, vmid: UInt): Bool = { 1117 val vmid_hit = this.entry.vmid.getOrElse(0.U) === vmid 1118 val hit0 = entry.tag(vpnLen - 1, vpnnLen * 2) === gvpn(vpnLen - 1, vpnnLen * 2) 1119 val hit1 = entry.tag(vpnnLen * 2 - 1, vpnnLen) === gvpn(vpnnLen * 2 - 1, vpnnLen) 1120 val hit2 = entry.tag(vpnnLen - 1, 0) === gvpn(vpnnLen - 1, 0) 1121 vmid_hit && Mux(entry.level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(entry.level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 1122 } 1123} 1124 1125class PtwResptomerge (implicit p: Parameters) extends PtwBundle { 1126 val entry = UInt(blockBits.W) 1127 val vpn = UInt(vpnLen.W) 1128 val level = UInt(log2Up(Level).W) 1129 val pf = Bool() 1130 val af = Bool() 1131 val asid = UInt(asidLen.W) 1132 1133 def apply(pf: Bool, af: Bool, level: UInt, pte: UInt, vpn: UInt, asid: UInt) = { 1134 this.entry := pte 1135 this.pf := pf 1136 this.af := af 1137 this.level := level 1138 this.vpn := vpn 1139 this.asid := asid 1140 } 1141 1142 override def toPrintable: Printable = { 1143 p"entry:${entry} pf:${pf} af:${af}" 1144 } 1145} 1146 1147class PtwRespwithMemIdx(implicit p: Parameters) extends PtwResp { 1148 val memidx = new MemBlockidxBundle 1149} 1150 1151class PtwSectorRespwithMemIdx(implicit p: Parameters) extends PtwSectorResp { 1152 val memidx = new MemBlockidxBundle 1153} 1154 1155class PtwSectorResp(implicit p: Parameters) extends PtwBundle { 1156 val entry = new PtwSectorEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true) 1157 val addr_low = UInt(sectortlbwidth.W) 1158 val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 1159 val valididx = Vec(tlbcontiguous, Bool()) 1160 val pteidx = Vec(tlbcontiguous, Bool()) 1161 val pf = Bool() 1162 val af = Bool() 1163 1164 1165 def genPPN(vpn: UInt): UInt = { 1166 MuxLookup(entry.level.get, 0.U)(Seq( 1167 0.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen*2-1, 0)), 1168 1.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen - sectortlbwidth), vpn(vpnnLen-1, 0)), 1169 2.U -> Cat(entry.ppn(entry.ppn.getWidth-1, 0), ppn_low(vpn(sectortlbwidth - 1, 0)))) 1170 ) 1171 } 1172 1173 def hit(vpn: UInt, asid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool): Bool = { 1174 require(vpn.getWidth == vpnLen) 1175 // require(this.asid.getWidth <= asid.getWidth) 1176 val asid_hit = if (ignoreAsid) true.B else (this.entry.asid === asid) 1177 val vmid_hit = Mux(s2xlate, this.entry.vmid.getOrElse(0.U) === vmid, true.B) 1178 if (allType) { 1179 val hit0 = entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * 2) 1180 val hit1 = entry.tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 1181 val hit2 = entry.tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) 1182 val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 1183 1184 asid_hit && vmid_hit && Mux(entry.level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(entry.level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) && addr_low_hit 1185 } else { 1186 val hit0 = entry.tag(sectorvpnLen - 1, sectorvpnLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 1187 val hit1 = entry.tag(sectorvpnLen - vpnnLen - 1, sectorvpnLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 1188 val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 1189 1190 asid_hit && vmid_hit && Mux(entry.level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) && addr_low_hit 1191 } 1192 } 1193} 1194 1195class PtwMergeResp(implicit p: Parameters) extends PtwBundle { 1196 val entry = Vec(tlbcontiguous, new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 1197 val pteidx = Vec(tlbcontiguous, Bool()) 1198 val not_super = Bool() 1199 1200 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt, vmid:UInt, addr_low : UInt, not_super : Boolean = true) = { 1201 assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 1202 val resp_pte = pte 1203 val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 1204 ptw_resp.ppn := resp_pte.getPPN()(gvpnLen - 1, sectortlbwidth) 1205 ptw_resp.ppn_low := resp_pte.getPPN()(sectortlbwidth - 1, 0) 1206 ptw_resp.level.map(_ := level) 1207 ptw_resp.perm.map(_ := resp_pte.getPerm()) 1208 ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 1209 ptw_resp.pf := pf 1210 ptw_resp.af := af 1211 ptw_resp.v := !pf 1212 ptw_resp.prefetch := DontCare 1213 ptw_resp.asid := asid 1214 ptw_resp.vmid.map(_ := vmid) 1215 this.pteidx := UIntToOH(addr_low).asBools 1216 this.not_super := not_super.B 1217 1218 1219 for (i <- 0 until tlbcontiguous) { 1220 this.entry(i) := ptw_resp 1221 } 1222 } 1223 1224 def genPPN(): UInt = { 1225 val idx = OHToUInt(pteidx) 1226 val tag = Cat(entry(idx).tag, idx(sectortlbwidth - 1, 0)) 1227 MuxLookup(entry(idx).level.get, 0.U)(Seq( 1228 0.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), tag(vpnnLen * 2 - 1, 0)), 1229 1.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen - sectortlbwidth), tag(vpnnLen - 1, 0)), 1230 2.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, 0), entry(idx).ppn_low)) 1231 ) 1232 } 1233} 1234 1235class PtwRespS2(implicit p: Parameters) extends PtwBundle { 1236 val s2xlate = UInt(2.W) 1237 val s1 = new PtwSectorResp() 1238 val s2 = new HptwResp() 1239 1240 def hasS2xlate: Bool = { 1241 this.s2xlate =/= noS2xlate 1242 } 1243 1244 def isOnlyStage2: Bool = { 1245 this.s2xlate === onlyStage2 1246 } 1247 1248 def getVpn(vpn: UInt): UInt = { 1249 val level = s1.entry.level.getOrElse(0.U) max s2.entry.level.getOrElse(0.U) 1250 val s1tag = Cat(s1.entry.tag, OHToUInt(s1.pteidx)) 1251 val s1tagFix = MuxCase(s1.entry.tag, Seq( 1252 (s1.entry.level.getOrElse(0.U) === 0.U && s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), s2.entry.tag(vpnnLen * 2 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)), 1253 (s1.entry.level.getOrElse(0.U) === 0.U && s2.entry.level.getOrElse(0.U) === 2.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)), 1254 (s1.entry.level.getOrElse(0.U) === 1.U && s2.entry.level.getOrElse(0.U) === 2.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth), s2.entry.tag(vpnnLen - 1, sectortlbwidth)) 1255 )) 1256 val s1_vpn = MuxLookup(level, s1tag)(Seq( 1257 0.U -> Cat(s1tagFix(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen * 2 - 1, 0)), 1258 1.U -> Cat(s1tagFix(sectorvpnLen - 1, vpnnLen - sectortlbwidth), vpn(vpnnLen - 1, 0))) 1259 ) 1260 val s2_vpn = s2.entry.tag 1261 Mux(s2xlate === onlyStage2, s2_vpn, Mux(s2xlate === allStage, s1_vpn, s1tag)) 1262 } 1263 1264 def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false): Bool = { 1265 val noS2_hit = s1.hit(vpn, Mux(this.hasS2xlate, vasid, asid), vmid, allType, ignoreAsid, this.hasS2xlate) 1266 val onlyS2_hit = s2.hit(vpn, vmid) 1267 // allstage and onlys1 hit 1268 val s1vpn = Cat(s1.entry.tag, s1.addr_low) 1269 val level = s1.entry.level.getOrElse(0.U) max s2.entry.level.getOrElse(0.U) 1270 val hit0 = vpn(vpnnLen * 3 - 1, vpnnLen * 2) === s1vpn(vpnnLen * 3 - 1, vpnnLen * 2) 1271 val hit1 = vpn(vpnnLen * 2 - 1, vpnnLen) === s1vpn(vpnnLen * 2 - 1, vpnnLen) 1272 val hit2 = vpn(vpnnLen - 1, 0) === s1vpn(vpnnLen - 1, 0) 1273 val vpn_hit = Mux(level === 2.U, hit2 && hit1 && hit0, Mux(level === 1.U, hit1 && hit0, hit0)) 1274 val vmid_hit = Mux(this.s2xlate === allStage, s2.entry.vmid.getOrElse(0.U) === vmid, true.B) 1275 val vasid_hit = if (ignoreAsid) true.B else (s1.entry.asid === vasid) 1276 val all_onlyS1_hit = vpn_hit && vmid_hit && vasid_hit 1277 Mux(this.s2xlate === noS2xlate, noS2_hit, 1278 Mux(this.s2xlate === onlyStage2, onlyS2_hit, all_onlyS1_hit)) 1279 } 1280} 1281 1282class PtwRespS2withMemIdx(implicit p: Parameters) extends PtwRespS2 { 1283 val memidx = new MemBlockidxBundle() 1284 val getGpa = Bool() // this req is to get gpa when having guest page fault 1285} 1286 1287class L2TLBIO(implicit p: Parameters) extends PtwBundle { 1288 val hartId = Input(UInt(hartIdLen.W)) 1289 val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 1290 val sfence = Input(new SfenceBundle) 1291 val csr = new Bundle { 1292 val tlb = Input(new TlbCsrBundle) 1293 val distribute_csr = Flipped(new DistributedCSRIO) 1294 } 1295} 1296 1297class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 1298 val addr = UInt(PAddrBits.W) 1299 val id = UInt(bMemID.W) 1300 val hptw_bypassed = Bool() 1301} 1302 1303class L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 1304 val source = UInt(bSourceWidth.W) 1305} 1306 1307class L2TlbWithHptwIdBundle(implicit p: Parameters) extends PtwBundle { 1308 val req_info = new L2TlbInnerBundle 1309 val isHptwReq = Bool() 1310 val isLLptw = Bool() 1311 val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W) 1312} 1313 1314object ValidHoldBypass{ 1315 def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = { 1316 val valid = RegInit(false.B) 1317 when (infire) { valid := true.B } 1318 when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold 1319 when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok? 1320 valid || infire 1321 } 1322} 1323 1324class L1TlbDB(implicit p: Parameters) extends TlbBundle { 1325 val vpn = UInt(vpnLen.W) 1326} 1327 1328class PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 1329 val vpn = UInt(vpnLen.W) 1330 val source = UInt(bSourceWidth.W) 1331 val bypassed = Bool() 1332 val is_first = Bool() 1333 val prefetched = Bool() 1334 val prefetch = Bool() 1335 val l2Hit = Bool() 1336 val l1Hit = Bool() 1337 val hit = Bool() 1338} 1339 1340class PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 1341 val vpn = UInt(vpnLen.W) 1342 val source = UInt(bSourceWidth.W) 1343} 1344 1345class L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle { 1346 val vpn = UInt(vpnLen.W) 1347} 1348 1349class L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle { 1350 val vpn = UInt(vpnLen.W) 1351} 1352