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} 281 282class TlbStorageIO(nSets: Int, nWays: Int, ports: Int)(implicit p: Parameters) extends MMUIOBaseBundle { 283 val r = new Bundle { 284 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 285 val vpn = Output(UInt(vpnLen.W)) 286 }))) 287 val resp = Vec(ports, ValidIO(new Bundle{ 288 val hit = Output(Bool()) 289 val ppn = Output(UInt(ppnLen.W)) 290 val perm = Output(new TlbPermBundle()) 291 val hitVec = Output(UInt(nWays.W)) 292 })) 293 } 294 val w = Flipped(ValidIO(new Bundle { 295 val wayIdx = Output(UInt(log2Up(nWays).W)) 296 val data = Output(new PtwResp) 297 })) 298 val victim = new Bundle { 299 val out = ValidIO(Output(new Bundle { 300 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 301 })) 302 val in = Flipped(ValidIO(Output(new Bundle { 303 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 304 }))) 305 } 306 307 def r_req_apply(valid: Bool, vpn: UInt, asid: UInt, i: Int): Unit = { 308 this.r.req(i).valid := valid 309 this.r.req(i).bits.vpn := vpn 310 } 311 312 def r_resp_apply(i: Int) = { 313 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.hitVec) 314 } 315 316 def w_apply(valid: Bool, wayIdx: UInt, data: PtwResp): Unit = { 317 this.w.valid := valid 318 this.w.bits.wayIdx := wayIdx 319 this.w.bits.data := data 320 } 321 322 override def cloneType: this.type = new TlbStorageIO(nSets, nWays, ports).asInstanceOf[this.type] 323} 324 325class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 326 val access = Flipped(new Bundle { 327 val sets = Output(Vec(Width, UInt(log2Up(nSets).W))) 328 val touch_ways = Vec(Width, ValidIO(Output(UInt(log2Up(nWays).W)))) 329 }) 330 331 val refillIdx = Output(UInt(log2Up(nWays).W)) 332 val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 333 334 def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 335 for (i <- 0 until Width) { 336 this.access.sets(i) := in(i).access.sets(0) 337 this.access.touch_ways(i) := in(i).access.touch_ways(0) 338 this.chosen_set := get_idx(vpn, nSets) 339 in(i).refillIdx := this.refillIdx 340 } 341 } 342} 343 344class TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 345 TlbBundle { 346 val normalPage = new ReplaceIO(Width, q.normalNSets, q.normalNWays) 347 val superPage = new ReplaceIO(Width, q.superNSets, q.superNWays) 348 349 def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 350 this.normalPage.apply_sep(in.map(_.normalPage), vpn) 351 this.superPage.apply_sep(in.map(_.superPage), vpn) 352 } 353 354 override def cloneType = (new TlbReplaceIO(Width, q)).asInstanceOf[this.type] 355} 356 357class TlbReq(implicit p: Parameters) extends TlbBundle { 358 val vaddr = UInt(VAddrBits.W) 359 val cmd = TlbCmd() 360 val size = UInt(log2Ceil(log2Ceil(XLEN/8)+1).W) 361 val robIdx = new RobPtr 362 val debug = new Bundle { 363 val pc = UInt(XLEN.W) 364 val isFirstIssue = Bool() 365 } 366 367 override def toPrintable: Printable = { 368 p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} pc:0x${Hexadecimal(debug.pc)} robIdx:${robIdx}" 369 } 370} 371 372class TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 373 val ld = Output(Bool()) 374 val st = Output(Bool()) 375 val instr = Output(Bool()) 376} 377 378class TlbResp(implicit p: Parameters) extends TlbBundle { 379 val paddr = UInt(PAddrBits.W) 380 val miss = Bool() 381 val mmio = Bool() 382 val excp = new Bundle { 383 val pf = new TlbExceptionBundle() 384 val af = new TlbExceptionBundle() 385 } 386 val ptwBack = Bool() // when ptw back, wake up replay rs's state 387 388 override def toPrintable: Printable = { 389 p"paddr:0x${Hexadecimal(paddr)} miss:${miss} excp.pf: ld:${excp.pf.ld} st:${excp.pf.st} instr:${excp.pf.instr} ptwBack:${ptwBack}" 390 } 391} 392 393class TlbRequestIO()(implicit p: Parameters) extends TlbBundle { 394 val req = DecoupledIO(new TlbReq) 395 val resp = Flipped(DecoupledIO(new TlbResp)) 396} 397 398class BlockTlbRequestIO()(implicit p: Parameters) extends TlbBundle { 399 val req = DecoupledIO(new TlbReq) 400 val resp = Flipped(DecoupledIO(new TlbResp)) 401} 402 403class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 404 val req = Vec(Width, DecoupledIO(new PtwReq)) 405 val resp = Flipped(DecoupledIO(new PtwResp)) 406 407 override def cloneType: this.type = (new TlbPtwIO(Width)).asInstanceOf[this.type] 408 409 override def toPrintable: Printable = { 410 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 411 } 412} 413 414class MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 415 val sfence = Input(new SfenceBundle) 416 val csr = Input(new TlbCsrBundle) 417} 418 419class TlbIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 420 MMUIOBaseBundle { 421 val requestor = Vec(Width, Flipped(new TlbRequestIO)) 422 val ptw = new TlbPtwIO(Width) 423 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 424 val pmp = Vec(Width, ValidIO(new PMPReqBundle())) 425 426 override def cloneType: this.type = (new TlbIO(Width, q)).asInstanceOf[this.type] 427} 428 429class BTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 430 val req = Vec(Width, DecoupledIO(new PtwReq)) 431 val resp = Flipped(DecoupledIO(new Bundle { 432 val data = new PtwResp 433 val vector = Output(Vec(Width, Bool())) 434 })) 435 436 override def cloneType: this.type = (new BTlbPtwIO(Width)).asInstanceOf[this.type] 437} 438/**************************** Bridge TLB *******************************/ 439 440class BridgeTLBIO(Width: Int)(implicit p: Parameters) extends MMUIOBaseBundle { 441 val requestor = Vec(Width, Flipped(new TlbPtwIO())) 442 val ptw = new BTlbPtwIO(Width) 443 444 override def cloneType: this.type = (new BridgeTLBIO(Width)).asInstanceOf[this.type] 445} 446 447 448/**************************** PTW *************************************/ 449abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 450abstract class PtwModule(outer: PTW) extends LazyModuleImp(outer) 451 with HasXSParameter with HasPtwConst 452 453class PteBundle(implicit p: Parameters) extends PtwBundle{ 454 val reserved = UInt(pteResLen.W) 455 val ppn = UInt(ppnLen.W) 456 val rsw = UInt(2.W) 457 val perm = new Bundle { 458 val d = Bool() 459 val a = Bool() 460 val g = Bool() 461 val u = Bool() 462 val x = Bool() 463 val w = Bool() 464 val r = Bool() 465 val v = Bool() 466 } 467 468 def unaligned(level: UInt) = { 469 isLeaf() && !(level === 2.U || 470 level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 471 level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U) 472 } 473 474 def isPf(level: UInt) = { 475 !perm.v || (!perm.r && perm.w) || unaligned(level) 476 } 477 478 def isLeaf() = { 479 perm.r || perm.x || perm.w 480 } 481 482 def getPerm() = { 483 val pm = Wire(new PtePermBundle) 484 pm.d := perm.d 485 pm.a := perm.a 486 pm.g := perm.g 487 pm.u := perm.u 488 pm.x := perm.x 489 pm.w := perm.w 490 pm.r := perm.r 491 pm 492 } 493 494 override def toPrintable: Printable = { 495 p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 496 } 497} 498 499class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 500 val tag = UInt(tagLen.W) 501 val asid = UInt(asidLen.W) 502 val ppn = UInt(ppnLen.W) 503 val perm = if (hasPerm) Some(new PtePermBundle) else None 504 val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None 505 val prefetch = Bool() 506 507 def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = { 508 require(vpn.getWidth == vpnLen) 509 require(this.asid.getWidth <= asid.getWidth) 510 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 511 if (allType) { 512 require(hasLevel) 513 val hit0 = tag(tagLen - 1, vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2) 514 val hit1 = tag(vpnnLen*2 - 1, vpnnLen) === vpn(vpnnLen*2 - 1, vpnnLen) 515 val hit2 = tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 516 517 asid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 518 } else if (hasLevel) { 519 val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 520 val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 521 522 asid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) 523 } else { 524 asid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 525 } 526 } 527 528 def refill(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool) { 529 require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 530 531 tag := vpn(vpnLen - 1, vpnLen - tagLen) 532 ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 533 perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 534 this.asid := asid 535 this.prefetch := prefetch 536 this.level.map(_ := level) 537 } 538 539 def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool) = { 540 val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 541 e.refill(vpn, asid, pte, level, prefetch) 542 e 543 } 544 545 override def cloneType: this.type = (new PtwEntry(tagLen, hasPerm, hasLevel)).asInstanceOf[this.type] 546 547 override def toPrintable: Printable = { 548 // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 549 p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " + 550 (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 551 (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 552 p"prefetch:${prefetch}" 553 } 554} 555 556class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 557 require(log2Up(num)==log2Down(num)) 558 559 val tag = UInt(tagLen.W) 560 val asid = UInt(asidLen.W) 561 val ppns = Vec(num, UInt(ppnLen.W)) 562 val vs = Vec(num, Bool()) 563 val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 564 val prefetch = Bool() 565 // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 566 567 def tagClip(vpn: UInt) = { 568 require(vpn.getWidth == vpnLen) 569 vpn(vpnLen - 1, vpnLen - tagLen) 570 } 571 572 def sectorIdxClip(vpn: UInt, level: Int) = { 573 getVpnClip(vpn, level)(log2Up(num) - 1, 0) 574 } 575 576 def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false) = { 577 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 578 asid_hit && tag === tagClip(vpn) && vs(sectorIdxClip(vpn, level)) // TODO: optimize this. don't need to compare each with tag 579 } 580 581 def genEntries(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 582 require((data.getWidth / XLEN) == num, 583 s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 584 585 val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm)) 586 ps.tag := tagClip(vpn) 587 ps.asid := asid 588 ps.prefetch := prefetch 589 for (i <- 0 until num) { 590 val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 591 ps.ppns(i) := pte.ppn 592 ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 593 ps.perms.map(_(i) := pte.perm) 594 } 595 ps 596 } 597 598 override def cloneType: this.type = (new PtwEntries(num, tagLen, level, hasPerm)).asInstanceOf[this.type] 599 override def toPrintable: Printable = { 600 // require(num == 4, "if num is not 4, please comment this toPrintable") 601 // NOTE: if num is not 4, please comment this toPrintable 602 val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 603 p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 604 (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 605 } 606} 607 608class PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 609 val entries = new PtwEntries(num, tagLen, level, hasPerm) 610 611 private val encBits = eccCode.width(entries.getWidth) 612 private val eccBits = encBits - entries.getWidth 613 val ecc = UInt(eccBits.W) 614 615 override def cloneType: this.type = new PTWEntriesWithEcc(eccCode, num, tagLen, level, hasPerm).asInstanceOf[this.type] 616} 617 618class PtwReq(implicit p: Parameters) extends PtwBundle { 619 val vpn = UInt(vpnLen.W) 620 621 override def toPrintable: Printable = { 622 p"vpn:0x${Hexadecimal(vpn)}" 623 } 624} 625 626class PtwResp(implicit p: Parameters) extends PtwBundle { 627 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 628 val pf = Bool() 629 val af = Bool() 630 631 632 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 633 this.entry.level.map(_ := level) 634 this.entry.tag := vpn 635 this.entry.perm.map(_ := pte.getPerm()) 636 this.entry.ppn := pte.ppn 637 this.entry.prefetch := DontCare 638 this.entry.asid := asid 639 this.pf := pf 640 this.af := af 641 } 642 643 override def toPrintable: Printable = { 644 p"entry:${entry} pf:${pf} af:${af}" 645 } 646} 647 648class PtwIO(implicit p: Parameters) extends PtwBundle { 649 val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 650 val sfence = Input(new SfenceBundle) 651 val csr = new Bundle { 652 val tlb = Input(new TlbCsrBundle) 653 val distribute_csr = Flipped(new DistributedCSRIO) 654 } 655} 656 657class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 658 val addr = UInt(PAddrBits.W) 659 val id = UInt(bMemID.W) 660} 661 662class L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 663 val source = UInt(bSourceWidth.W) 664}