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