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***************************************************************************************/ 16 17package xiangshan.cache.mmu 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import xiangshan.backend.rob.RobPtr 25import xiangshan.backend.fu.util.HasCSRConst 26import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 27import freechips.rocketchip.tilelink._ 28import xiangshan.backend.fu.PMPReqBundle 29 30abstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst 31abstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst 32 33 34 35// case class ITLBKey 36// case class LDTLBKey 37// case class STTLBKey 38 39class VaBundle(implicit p: Parameters) extends TlbBundle { 40 val vpn = UInt(vpnLen.W) 41 val off = UInt(offLen.W) 42} 43 44class PtePermBundle(implicit p: Parameters) extends TlbBundle { 45 val d = Bool() 46 val a = Bool() 47 val g = Bool() 48 val u = Bool() 49 val x = Bool() 50 val w = Bool() 51 val r = Bool() 52 53 override def toPrintable: Printable = { 54 p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// + 55 //(if(hasV) (p"v:${v}") else p"") 56 } 57} 58 59class TlbPermBundle(implicit p: Parameters) extends TlbBundle { 60 val pf = Bool() // NOTE: if this is true, just raise pf 61 val af = Bool() // NOTE: if this is true, just raise af 62 // pagetable perm (software defined) 63 val d = Bool() 64 val a = Bool() 65 val g = Bool() 66 val u = Bool() 67 val x = Bool() 68 val w = Bool() 69 val r = Bool() 70 // pma perm (hardwired) 71 val pr = Bool() //readable 72 val pw = Bool() //writeable 73 val pe = Bool() //executable 74 val pa = Bool() //atom op permitted 75 val pi = Bool() //icacheable 76 val pd = Bool() //dcacheable 77 78 override def toPrintable: Printable = { 79 p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}" 80 } 81} 82 83// multi-read && single-write 84// input is data, output is hot-code(not one-hot) 85class CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 86 val io = IO(new Bundle { 87 val r = new Bundle { 88 val req = Input(Vec(readWidth, gen)) 89 val resp = Output(Vec(readWidth, Vec(set, Bool()))) 90 } 91 val w = Input(new Bundle { 92 val valid = Bool() 93 val bits = new Bundle { 94 val index = UInt(log2Up(set).W) 95 val data = gen 96 } 97 }) 98 }) 99 100 val wordType = UInt(gen.getWidth.W) 101 val array = Reg(Vec(set, wordType)) 102 103 io.r.resp.zipWithIndex.map{ case (a,i) => 104 a := array.map(io.r.req(i).asUInt === _) 105 } 106 107 when (io.w.valid) { 108 array(io.w.bits.index) := io.w.bits.data 109 } 110} 111 112class TlbSPMeta(implicit p: Parameters) extends TlbBundle { 113 val tag = UInt(vpnLen.W) // tag is vpn 114 val level = UInt(1.W) // 1 for 2MB, 0 for 1GB 115 val asid = UInt(asidLen.W) 116 117 def hit(vpn: UInt, asid: UInt): Bool = { 118 val a = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2) 119 val b = tag(vpnnLen*2-1, vpnnLen*1) === vpn(vpnnLen*2-1, vpnnLen*1) 120 val asid_hit = this.asid === asid 121 122 XSDebug(Mux(level.asBool, a&b, a), p"Hit superpage: hit:${Mux(level.asBool, a&b, a)} tag:${Hexadecimal(tag)} level:${level} a:${a} b:${b} vpn:${Hexadecimal(vpn)}\n") 123 asid_hit && Mux(level.asBool, a&b, a) 124 } 125 126 def apply(vpn: UInt, asid: UInt, level: UInt) = { 127 this.tag := vpn 128 this.asid := asid 129 this.level := level(0) 130 131 this 132 } 133 134} 135 136class TlbData(superpage: Boolean = false)(implicit p: Parameters) extends TlbBundle { 137 val level = if(superpage) Some(UInt(1.W)) else None // /*2 for 4KB,*/ 1 for 2MB, 0 for 1GB 138 val ppn = UInt(ppnLen.W) 139 val perm = new TlbPermBundle 140 141 def genPPN(vpn: UInt): UInt = { 142 if (superpage) { 143 val insideLevel = level.getOrElse(0.U) 144 Mux(insideLevel.asBool, Cat(ppn(ppn.getWidth-1, vpnnLen*1), vpn(vpnnLen*1-1, 0)), 145 Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0))) 146 } else { 147 ppn 148 } 149 } 150 151 def apply(ppn: UInt, level: UInt, perm: UInt, pf: Bool, af: Bool) = { 152 this.level.map(_ := level(0)) 153 this.ppn := ppn 154 // refill pagetable perm 155 val ptePerm = perm.asTypeOf(new PtePermBundle) 156 this.perm.pf:= pf 157 this.perm.af:= af 158 this.perm.d := ptePerm.d 159 this.perm.a := ptePerm.a 160 this.perm.g := ptePerm.g 161 this.perm.u := ptePerm.u 162 this.perm.x := ptePerm.x 163 this.perm.w := ptePerm.w 164 this.perm.r := ptePerm.r 165 166 // get pma perm 167 val (pmaMode, accessWidth) = AddressSpace.memmapAddrMatch(Cat(ppn, 0.U(12.W))) 168 this.perm.pr := PMAMode.read(pmaMode) 169 this.perm.pw := PMAMode.write(pmaMode) 170 this.perm.pe := PMAMode.execute(pmaMode) 171 this.perm.pa := PMAMode.atomic(pmaMode) 172 this.perm.pi := PMAMode.icache(pmaMode) 173 this.perm.pd := PMAMode.dcache(pmaMode) 174 175 this 176 } 177 178 override def toPrintable: Printable = { 179 val insideLevel = level.getOrElse(0.U) 180 p"level:${insideLevel} ppn:${Hexadecimal(ppn)} perm:${perm}" 181 } 182 183 override def cloneType: this.type = (new TlbData(superpage)).asInstanceOf[this.type] 184} 185 186class TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 187 require(pageNormal || pageSuper) 188 189 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 190 else UInt(vpnLen.W) 191 val asid = UInt(asidLen.W) 192 val level = if (!pageNormal) Some(UInt(1.W)) 193 else if (!pageSuper) None 194 else Some(UInt(2.W)) 195 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 196 else UInt(ppnLen.W) 197 val perm = new TlbPermBundle 198 199 def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false): Bool = { 200 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 201 if (!pageSuper) asid_hit && vpn === tag 202 else if (!pageNormal) asid_hit && MuxLookup(level.get, false.B, Seq( 203 0.U -> (tag(vpnnLen*2-1, vpnnLen) === vpn(vpnLen-1, vpnnLen*2)), 204 1.U -> (tag === vpn(vpnLen-1, vpnnLen)), 205 )) 206 else asid_hit && MuxLookup(level.get, false.B, Seq( 207 0.U -> (tag(vpnLen-1, vpnnLen*2) === vpn(vpnLen-1, vpnnLen*2)), 208 1.U -> (tag(vpnLen-1, vpnnLen) === vpn(vpnLen-1, vpnnLen)), 209 2.U -> (tag === vpn) // if pageNormal is false, this will always be false 210 )) 211 } 212 213 def apply(item: PtwResp, asid: UInt): TlbEntry = { 214 this.tag := {if (pageNormal) item.entry.tag else item.entry.tag(vpnLen-1, vpnnLen)} 215 this.asid := asid 216 val inner_level = item.entry.level.getOrElse(0.U) 217 this.level.map(_ := { if (pageNormal && pageSuper) inner_level 218 else if (pageSuper) inner_level(0) 219 else 0.U}) 220 this.ppn := { if (!pageNormal) item.entry.ppn(ppnLen-1, vpnnLen) 221 else item.entry.ppn } 222 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 223 this.perm.pf := item.pf 224 this.perm.af := item.af 225 this.perm.d := ptePerm.d 226 this.perm.a := ptePerm.a 227 this.perm.g := ptePerm.g 228 this.perm.u := ptePerm.u 229 this.perm.x := ptePerm.x 230 this.perm.w := ptePerm.w 231 this.perm.r := ptePerm.r 232 233 // get pma perm 234 val (pmaMode, accessWidth) = AddressSpace.memmapAddrMatch(Cat(item.entry.ppn, 0.U(12.W))) 235 this.perm.pr := PMAMode.read(pmaMode) 236 this.perm.pw := PMAMode.write(pmaMode) 237 this.perm.pe := PMAMode.execute(pmaMode) 238 this.perm.pa := PMAMode.atomic(pmaMode) 239 this.perm.pi := PMAMode.icache(pmaMode) 240 this.perm.pd := PMAMode.dcache(pmaMode) 241 242 this 243 } 244 245 def genPPN(vpn: UInt) : UInt = { 246 if (!pageSuper) ppn 247 else if (!pageNormal) MuxLookup(level.get, 0.U, Seq( 248 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen*2-1, 0)), 249 1.U -> Cat(ppn, vpn(vpnnLen-1, 0)) 250 )) 251 else MuxLookup(level.get, 0.U, Seq( 252 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 253 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 254 2.U -> ppn 255 )) 256 } 257 258 override def toPrintable: Printable = { 259 val inner_level = level.getOrElse(2.U) 260 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 261 } 262 263 override def cloneType: this.type = (new TlbEntry(pageNormal, pageSuper)).asInstanceOf[this.type] 264} 265 266object TlbCmd { 267 def read = "b00".U 268 def write = "b01".U 269 def exec = "b10".U 270 271 def atom_read = "b100".U // lr 272 def atom_write = "b101".U // sc / amo 273 274 def apply() = UInt(3.W) 275 def isRead(a: UInt) = a(1,0)===read 276 def isWrite(a: UInt) = a(1,0)===write 277 def isExec(a: UInt) = a(1,0)===exec 278 279 def isAtom(a: UInt) = a(2) 280 def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 281} 282 283class TlbStorageIO(nSets: Int, nWays: Int, ports: Int)(implicit p: Parameters) extends MMUIOBaseBundle { 284 val r = new Bundle { 285 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 286 val vpn = Output(UInt(vpnLen.W)) 287 }))) 288 val resp = Vec(ports, ValidIO(new Bundle{ 289 val hit = Output(Bool()) 290 val ppn = Output(UInt(ppnLen.W)) 291 val perm = Output(new TlbPermBundle()) 292 val hitVec = Output(UInt(nWays.W)) 293 })) 294 } 295 val w = Flipped(ValidIO(new Bundle { 296 val wayIdx = Output(UInt(log2Up(nWays).W)) 297 val data = Output(new PtwResp) 298 })) 299 val victim = new Bundle { 300 val out = ValidIO(Output(new Bundle { 301 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 302 })) 303 val in = Flipped(ValidIO(Output(new Bundle { 304 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 305 }))) 306 } 307 308 def r_req_apply(valid: Bool, vpn: UInt, asid: UInt, i: Int): Unit = { 309 this.r.req(i).valid := valid 310 this.r.req(i).bits.vpn := vpn 311 } 312 313 def r_resp_apply(i: Int) = { 314 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.hitVec) 315 } 316 317 def w_apply(valid: Bool, wayIdx: UInt, data: PtwResp): Unit = { 318 this.w.valid := valid 319 this.w.bits.wayIdx := wayIdx 320 this.w.bits.data := data 321 } 322 323 override def cloneType: this.type = new TlbStorageIO(nSets, nWays, ports).asInstanceOf[this.type] 324} 325 326class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 327 val access = Flipped(new Bundle { 328 val sets = Output(Vec(Width, UInt(log2Up(nSets).W))) 329 val touch_ways = Vec(Width, ValidIO(Output(UInt(log2Up(nWays).W)))) 330 }) 331 332 val refillIdx = Output(UInt(log2Up(nWays).W)) 333 val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 334 335 def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 336 for (i <- 0 until Width) { 337 this.access.sets(i) := in(i).access.sets(0) 338 this.access.touch_ways(i) := in(i).access.touch_ways(0) 339 this.chosen_set := get_idx(vpn, nSets) 340 in(i).refillIdx := this.refillIdx 341 } 342 } 343} 344 345class TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 346 TlbBundle { 347 val normalPage = new ReplaceIO(Width, q.normalNSets, q.normalNWays) 348 val superPage = new ReplaceIO(Width, q.superNSets, q.superNWays) 349 350 def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 351 this.normalPage.apply_sep(in.map(_.normalPage), vpn) 352 this.superPage.apply_sep(in.map(_.superPage), vpn) 353 } 354 355 override def cloneType = (new TlbReplaceIO(Width, q)).asInstanceOf[this.type] 356} 357 358class TlbReq(implicit p: Parameters) extends TlbBundle { 359 val vaddr = UInt(VAddrBits.W) 360 val cmd = TlbCmd() 361 val size = UInt(log2Ceil(log2Ceil(XLEN/8)+1).W) 362 val robIdx = new RobPtr 363 val debug = new Bundle { 364 val pc = UInt(XLEN.W) 365 val isFirstIssue = Bool() 366 } 367 368 override def toPrintable: Printable = { 369 p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} pc:0x${Hexadecimal(debug.pc)} robIdx:${robIdx}" 370 } 371} 372 373class TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 374 val ld = Output(Bool()) 375 val st = Output(Bool()) 376 val instr = Output(Bool()) 377} 378 379class TlbResp(implicit p: Parameters) extends TlbBundle { 380 val paddr = UInt(PAddrBits.W) 381 val miss = Bool() 382 val mmio = Bool() 383 val excp = new Bundle { 384 val pf = new TlbExceptionBundle() 385 val af = new TlbExceptionBundle() 386 } 387 val ptwBack = Bool() // when ptw back, wake up replay rs's state 388 389 override def toPrintable: Printable = { 390 p"paddr:0x${Hexadecimal(paddr)} miss:${miss} excp.pf: ld:${excp.pf.ld} st:${excp.pf.st} instr:${excp.pf.instr} ptwBack:${ptwBack}" 391 } 392} 393 394class TlbRequestIO()(implicit p: Parameters) extends TlbBundle { 395 val req = DecoupledIO(new TlbReq) 396 val resp = Flipped(DecoupledIO(new TlbResp)) 397} 398 399class BlockTlbRequestIO()(implicit p: Parameters) extends TlbBundle { 400 val req = DecoupledIO(new TlbReq) 401 val resp = Flipped(DecoupledIO(new TlbResp)) 402} 403 404class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 405 val req = Vec(Width, DecoupledIO(new PtwReq)) 406 val resp = Flipped(DecoupledIO(new PtwResp)) 407 408 override def cloneType: this.type = (new TlbPtwIO(Width)).asInstanceOf[this.type] 409 410 override def toPrintable: Printable = { 411 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 412 } 413} 414 415class MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 416 val sfence = Input(new SfenceBundle) 417 val csr = Input(new TlbCsrBundle) 418} 419 420class TlbIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 421 MMUIOBaseBundle { 422 val requestor = Vec(Width, Flipped(new TlbRequestIO)) 423 val ptw = new TlbPtwIO(Width) 424 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 425 val pmp = Vec(Width, ValidIO(new PMPReqBundle())) 426 427 override def cloneType: this.type = (new TlbIO(Width, q)).asInstanceOf[this.type] 428} 429 430class BTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 431 val req = Vec(Width, DecoupledIO(new PtwReq)) 432 val resp = Flipped(DecoupledIO(new Bundle { 433 val data = new PtwResp 434 val vector = Output(Vec(Width, Bool())) 435 })) 436 437 override def cloneType: this.type = (new BTlbPtwIO(Width)).asInstanceOf[this.type] 438} 439/**************************** Bridge TLB *******************************/ 440 441class BridgeTLBIO(Width: Int)(implicit p: Parameters) extends MMUIOBaseBundle { 442 val requestor = Vec(Width, Flipped(new TlbPtwIO())) 443 val ptw = new BTlbPtwIO(Width) 444 445 override def cloneType: this.type = (new BridgeTLBIO(Width)).asInstanceOf[this.type] 446} 447 448 449/**************************** PTW *************************************/ 450abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 451abstract class PtwModule(outer: PTW) extends LazyModuleImp(outer) 452 with HasXSParameter with HasPtwConst 453 454class PteBundle(implicit p: Parameters) extends PtwBundle{ 455 val reserved = UInt(pteResLen.W) 456 val ppn = UInt(ppnLen.W) 457 val rsw = UInt(2.W) 458 val perm = new Bundle { 459 val d = Bool() 460 val a = Bool() 461 val g = Bool() 462 val u = Bool() 463 val x = Bool() 464 val w = Bool() 465 val r = Bool() 466 val v = Bool() 467 } 468 469 def unaligned(level: UInt) = { 470 isLeaf() && !(level === 2.U || 471 level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 472 level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U) 473 } 474 475 def isPf(level: UInt) = { 476 !perm.v || (!perm.r && perm.w) || unaligned(level) 477 } 478 479 def isLeaf() = { 480 perm.r || perm.x || perm.w 481 } 482 483 def getPerm() = { 484 val pm = Wire(new PtePermBundle) 485 pm.d := perm.d 486 pm.a := perm.a 487 pm.g := perm.g 488 pm.u := perm.u 489 pm.x := perm.x 490 pm.w := perm.w 491 pm.r := perm.r 492 pm 493 } 494 495 override def toPrintable: Printable = { 496 p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 497 } 498} 499 500class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 501 val tag = UInt(tagLen.W) 502 val asid = UInt(asidLen.W) 503 val ppn = UInt(ppnLen.W) 504 val perm = if (hasPerm) Some(new PtePermBundle) else None 505 val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None 506 val prefetch = Bool() 507 508 def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = { 509 require(vpn.getWidth == vpnLen) 510 require(this.asid.getWidth <= asid.getWidth) 511 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 512 if (allType) { 513 require(hasLevel) 514 val hit0 = tag(tagLen - 1, vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2) 515 val hit1 = tag(vpnnLen*2 - 1, vpnnLen) === vpn(vpnnLen*2 - 1, vpnnLen) 516 val hit2 = tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 517 518 asid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 519 } else if (hasLevel) { 520 val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 521 val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 522 523 asid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) 524 } else { 525 asid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 526 } 527 } 528 529 def refill(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool) { 530 require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 531 532 tag := vpn(vpnLen - 1, vpnLen - tagLen) 533 ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 534 perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 535 this.asid := asid 536 this.prefetch := prefetch 537 this.level.map(_ := level) 538 } 539 540 def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool) = { 541 val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 542 e.refill(vpn, asid, pte, level, prefetch) 543 e 544 } 545 546 override def cloneType: this.type = (new PtwEntry(tagLen, hasPerm, hasLevel)).asInstanceOf[this.type] 547 548 override def toPrintable: Printable = { 549 // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 550 p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " + 551 (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 552 (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 553 p"prefetch:${prefetch}" 554 } 555} 556 557class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 558 require(log2Up(num)==log2Down(num)) 559 560 val tag = UInt(tagLen.W) 561 val asid = UInt(asidLen.W) 562 val ppns = Vec(num, UInt(ppnLen.W)) 563 val vs = Vec(num, Bool()) 564 val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 565 val prefetch = Bool() 566 // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 567 568 def tagClip(vpn: UInt) = { 569 require(vpn.getWidth == vpnLen) 570 vpn(vpnLen - 1, vpnLen - tagLen) 571 } 572 573 def sectorIdxClip(vpn: UInt, level: Int) = { 574 getVpnClip(vpn, level)(log2Up(num) - 1, 0) 575 } 576 577 def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false) = { 578 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 579 asid_hit && tag === tagClip(vpn) && vs(sectorIdxClip(vpn, level)) // TODO: optimize this. don't need to compare each with tag 580 } 581 582 def genEntries(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 583 require((data.getWidth / XLEN) == num, 584 s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 585 586 val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm)) 587 ps.tag := tagClip(vpn) 588 ps.asid := asid 589 ps.prefetch := prefetch 590 for (i <- 0 until num) { 591 val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 592 ps.ppns(i) := pte.ppn 593 ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 594 ps.perms.map(_(i) := pte.perm) 595 } 596 ps 597 } 598 599 override def cloneType: this.type = (new PtwEntries(num, tagLen, level, hasPerm)).asInstanceOf[this.type] 600 override def toPrintable: Printable = { 601 // require(num == 4, "if num is not 4, please comment this toPrintable") 602 // NOTE: if num is not 4, please comment this toPrintable 603 val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 604 p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 605 (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 606 } 607} 608 609class PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 610 val entries = new PtwEntries(num, tagLen, level, hasPerm) 611 612 private val encBits = eccCode.width(entries.getWidth) 613 private val eccBits = encBits - entries.getWidth 614 val ecc = UInt(eccBits.W) 615 616 override def cloneType: this.type = new PTWEntriesWithEcc(eccCode, num, tagLen, level, hasPerm).asInstanceOf[this.type] 617} 618 619class PtwReq(implicit p: Parameters) extends PtwBundle { 620 val vpn = UInt(vpnLen.W) 621 622 override def toPrintable: Printable = { 623 p"vpn:0x${Hexadecimal(vpn)}" 624 } 625} 626 627class PtwResp(implicit p: Parameters) extends PtwBundle { 628 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 629 val pf = Bool() 630 val af = Bool() 631 632 633 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 634 this.entry.level.map(_ := level) 635 this.entry.tag := vpn 636 this.entry.perm.map(_ := pte.getPerm()) 637 this.entry.ppn := pte.ppn 638 this.entry.prefetch := DontCare 639 this.entry.asid := asid 640 this.pf := pf 641 this.af := af 642 } 643 644 override def toPrintable: Printable = { 645 p"entry:${entry} pf:${pf} af:${af}" 646 } 647} 648 649class PtwIO(implicit p: Parameters) extends PtwBundle { 650 val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 651 val sfence = Input(new SfenceBundle) 652 val csr = new Bundle { 653 val tlb = Input(new TlbCsrBundle) 654 val distribute_csr = Flipped(new DistributedCSRIO) 655 } 656} 657 658class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 659 val addr = UInt(PAddrBits.W) 660 val id = UInt(bMemID.W) 661} 662 663class L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 664 val source = UInt(bSourceWidth.W) 665}