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, PMPConfig} 29import xiangshan.backend.fu.PMPBundle 30 31 32abstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst 33abstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst 34 35class VaBundle(implicit p: Parameters) extends TlbBundle { 36 val vpn = UInt(vpnLen.W) 37 val off = UInt(offLen.W) 38} 39 40class PtePermBundle(implicit p: Parameters) extends TlbBundle { 41 val d = Bool() 42 val a = Bool() 43 val g = Bool() 44 val u = Bool() 45 val x = Bool() 46 val w = Bool() 47 val r = Bool() 48 49 override def toPrintable: Printable = { 50 p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// + 51 //(if(hasV) (p"v:${v}") else p"") 52 } 53} 54 55class TlbPMBundle(implicit p: Parameters) extends TlbBundle { 56 val r = Bool() 57 val w = Bool() 58 val x = Bool() 59 val c = Bool() 60 val atomic = Bool() 61 62 def assign_ap(pm: PMPConfig) = { 63 r := pm.r 64 w := pm.w 65 x := pm.x 66 c := pm.c 67 atomic := pm.atomic 68 } 69} 70 71class TlbPermBundle(implicit p: Parameters) extends TlbBundle { 72 val pf = Bool() // NOTE: if this is true, just raise pf 73 val af = Bool() // NOTE: if this is true, just raise af 74 // pagetable perm (software defined) 75 val d = Bool() 76 val a = Bool() 77 val g = Bool() 78 val u = Bool() 79 val x = Bool() 80 val w = Bool() 81 val r = Bool() 82 83 val pm = new TlbPMBundle 84 85 def apply(item: PtwResp, pm: PMPConfig) = { 86 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 87 this.pf := item.pf 88 this.af := item.af 89 this.d := ptePerm.d 90 this.a := ptePerm.a 91 this.g := ptePerm.g 92 this.u := ptePerm.u 93 this.x := ptePerm.x 94 this.w := ptePerm.w 95 this.r := ptePerm.r 96 97 this.pm.assign_ap(pm) 98 this 99 } 100 override def toPrintable: Printable = { 101 p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " + 102 p"pm:${pm}" 103 } 104} 105 106// multi-read && single-write 107// input is data, output is hot-code(not one-hot) 108class CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 109 val io = IO(new Bundle { 110 val r = new Bundle { 111 val req = Input(Vec(readWidth, gen)) 112 val resp = Output(Vec(readWidth, Vec(set, Bool()))) 113 } 114 val w = Input(new Bundle { 115 val valid = Bool() 116 val bits = new Bundle { 117 val index = UInt(log2Up(set).W) 118 val data = gen 119 } 120 }) 121 }) 122 123 val wordType = UInt(gen.getWidth.W) 124 val array = Reg(Vec(set, wordType)) 125 126 io.r.resp.zipWithIndex.map{ case (a,i) => 127 a := array.map(io.r.req(i).asUInt === _) 128 } 129 130 when (io.w.valid) { 131 array(io.w.bits.index) := io.w.bits.data.asUInt 132 } 133} 134 135class TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 136 require(pageNormal || pageSuper) 137 138 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 139 else UInt(vpnLen.W) 140 val asid = UInt(asidLen.W) 141 val level = if (!pageNormal) Some(UInt(1.W)) 142 else if (!pageSuper) None 143 else Some(UInt(2.W)) 144 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 145 else UInt(ppnLen.W) 146 val perm = new TlbPermBundle 147 148 /** level usage: 149 * !PageSuper: page is only normal, level is None, match all the tag 150 * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 151 * bits0 0: need mid 9bits 152 * 1: no need mid 9bits 153 * PageSuper && PageNormal: page hold all the three type, 154 * bits0 0: need low 9bits 155 * bits1 0: need mid 9bits 156 */ 157 158 def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = { 159 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 160 161 // NOTE: for timing, dont care low set index bits at hit check 162 // do not need store the low bits actually 163 if (!pageSuper) asid_hit && drop_set_equal(vpn, tag, nSets) 164 else if (!pageNormal) { 165 val tag_match_hi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*3-1, vpnnLen*2) 166 val tag_match_mi = tag(vpnnLen-1, 0) === vpn(vpnnLen*2-1, vpnnLen) 167 val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi) 168 asid_hit && tag_match 169 } 170 else { 171 val tmp_level = level.get 172 val tag_match_hi = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2) 173 val tag_match_mi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*2-1, vpnnLen) 174 val tag_match_lo = tag(vpnnLen-1, 0) === vpn(vpnnLen-1, 0) // if pageNormal is false, this will always be false 175 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 176 asid_hit && tag_match 177 } 178 } 179 180 def apply(item: PtwResp, asid: UInt, pm: PMPConfig): TlbEntry = { 181 this.tag := {if (pageNormal) item.entry.tag else item.entry.tag(vpnLen-1, vpnnLen)} 182 this.asid := asid 183 val inner_level = item.entry.level.getOrElse(0.U) 184 this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U, Seq( 185 0.U -> 3.U, 186 1.U -> 1.U, 187 2.U -> 0.U )) 188 else if (pageSuper) ~inner_level(0) 189 else 0.U }) 190 this.ppn := { if (!pageNormal) item.entry.ppn(ppnLen-1, vpnnLen) 191 else item.entry.ppn } 192 this.perm.apply(item, pm) 193 this 194 } 195 196 // 4KB is normal entry, 2MB/1GB is considered as super entry 197 def is_normalentry(): Bool = { 198 if (!pageSuper) { true.B } 199 else if (!pageNormal) { false.B } 200 else { level.get === 0.U } 201 } 202 203 def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 204 val inner_level = level.getOrElse(0.U) 205 val ppn_res = if (!pageSuper) ppn 206 else if (!pageNormal) Cat(ppn(ppnLen-vpnnLen-1, vpnnLen), 207 Mux(inner_level(0), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen-1,0)), 208 vpn(vpnnLen-1, 0)) 209 else Cat(ppn(ppnLen-1, vpnnLen*2), 210 Mux(inner_level(1), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen*2-1, vpnnLen)), 211 Mux(inner_level(0), vpn(vpnnLen-1, 0), ppn(vpnnLen-1, 0))) 212 213 if (saveLevel) Cat(ppn(ppn.getWidth-1, vpnnLen*2), RegEnable(ppn_res(vpnnLen*2-1, 0), valid)) 214 else ppn_res 215 } 216 217 override def toPrintable: Printable = { 218 val inner_level = level.getOrElse(2.U) 219 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 220 } 221 222} 223 224object TlbCmd { 225 def read = "b00".U 226 def write = "b01".U 227 def exec = "b10".U 228 229 def atom_read = "b100".U // lr 230 def atom_write = "b101".U // sc / amo 231 232 def apply() = UInt(3.W) 233 def isRead(a: UInt) = a(1,0)===read 234 def isWrite(a: UInt) = a(1,0)===write 235 def isExec(a: UInt) = a(1,0)===exec 236 237 def isAtom(a: UInt) = a(2) 238 def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 239} 240 241class TlbStorageIO(nSets: Int, nWays: Int, ports: Int)(implicit p: Parameters) extends MMUIOBaseBundle { 242 val r = new Bundle { 243 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 244 val vpn = Output(UInt(vpnLen.W)) 245 }))) 246 val resp = Vec(ports, ValidIO(new Bundle{ 247 val hit = Output(Bool()) 248 val ppn = Output(UInt(ppnLen.W)) 249 val perm = Output(new TlbPermBundle()) 250 })) 251 } 252 val w = Flipped(ValidIO(new Bundle { 253 val wayIdx = Output(UInt(log2Up(nWays).W)) 254 val data = Output(new PtwResp) 255 val data_replenish = Output(new PMPConfig) 256 })) 257 val victim = new Bundle { 258 val out = ValidIO(Output(new Bundle { 259 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 260 })) 261 val in = Flipped(ValidIO(Output(new Bundle { 262 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 263 }))) 264 } 265 val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays)) 266 267 def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = { 268 this.r.req(i).valid := valid 269 this.r.req(i).bits.vpn := vpn 270 } 271 272 def r_resp_apply(i: Int) = { 273 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm) 274 } 275 276 def w_apply(valid: Bool, wayIdx: UInt, data: PtwResp, data_replenish: PMPConfig): Unit = { 277 this.w.valid := valid 278 this.w.bits.wayIdx := wayIdx 279 this.w.bits.data := data 280 this.w.bits.data_replenish := data_replenish 281 } 282 283} 284 285class TlbStorageWrapperIO(ports: Int, q: TLBParameters)(implicit p: Parameters) extends MMUIOBaseBundle { 286 val r = new Bundle { 287 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 288 val vpn = Output(UInt(vpnLen.W)) 289 }))) 290 val resp = Vec(ports, ValidIO(new Bundle{ 291 val hit = Output(Bool()) 292 val ppn = Output(UInt(ppnLen.W)) 293 val perm = Output(new TlbPermBundle()) 294 // below are dirty code for timing optimization 295 val super_hit = Output(Bool()) 296 val super_ppn = Output(UInt(ppnLen.W)) 297 val spm = Output(new TlbPMBundle) 298 })) 299 } 300 val w = Flipped(ValidIO(new Bundle { 301 val data = Output(new PtwResp) 302 val data_replenish = Output(new PMPConfig) 303 })) 304 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null 305 306 def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = { 307 this.r.req(i).valid := valid 308 this.r.req(i).bits.vpn := vpn 309 } 310 311 def r_resp_apply(i: Int) = { 312 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, 313 this.r.resp(i).bits.super_hit, this.r.resp(i).bits.super_ppn, this.r.resp(i).bits.spm) 314 } 315 316 def w_apply(valid: Bool, data: PtwResp, data_replenish: PMPConfig): Unit = { 317 this.w.valid := valid 318 this.w.bits.data := data 319 this.w.bits.data_replenish := data_replenish 320 } 321} 322 323class ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 324 val sets = Output(UInt(log2Up(nSets).W)) 325 val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W))) 326 327} 328 329class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 330 val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays))) 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 ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) { 337 ac_rep := ac_tlb 338 } 339 this.chosen_set := get_set_idx(vpn, nSets) 340 in.map(a => a.refillIdx := this.refillIdx) 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} 355 356class TlbReq(implicit p: Parameters) extends TlbBundle { 357 val vaddr = Output(UInt(VAddrBits.W)) 358 val cmd = Output(TlbCmd()) 359 val size = Output(UInt(log2Ceil(log2Ceil(XLEN/8)+1).W)) 360 val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache 361 val debug = new Bundle { 362 val pc = Output(UInt(XLEN.W)) 363 val robIdx = Output(new RobPtr) 364 val isFirstIssue = Output(Bool()) 365 } 366 367 // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead 368 override def toPrintable: Printable = { 369 p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.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 = Output(UInt(PAddrBits.W)) 381 val miss = Output(Bool()) 382 val fast_miss = Output(Bool()) // without sram part for timing optimization 383 val excp = new Bundle { 384 val pf = new TlbExceptionBundle() 385 val af = new TlbExceptionBundle() 386 } 387 val static_pm = Output(Valid(Bool())) // valid for static, bits for mmio result from normal entries 388 val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state 389 390 override def toPrintable: Printable = { 391 p"paddr:0x${Hexadecimal(paddr)} miss:${miss} excp.pf: ld:${excp.pf.ld} st:${excp.pf.st} instr:${excp.pf.instr} ptwBack:${ptwBack}" 392 } 393} 394 395class TlbRequestIO()(implicit p: Parameters) extends TlbBundle { 396 val req = DecoupledIO(new TlbReq) 397 val resp = Flipped(DecoupledIO(new TlbResp)) 398} 399 400class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 401 val req = Vec(Width, DecoupledIO(new PtwReq)) 402 val resp = Flipped(DecoupledIO(new PtwResp)) 403 404 405 override def toPrintable: Printable = { 406 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 407 } 408} 409 410class MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 411 val sfence = Input(new SfenceBundle) 412 val csr = Input(new TlbCsrBundle) 413 414 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 415 this.sfence <> sfence 416 this.csr <> csr 417 } 418 419 // overwrite satp. write satp will cause flushpipe but csr.priv won't 420 // satp will be dealyed several cycles from writing, but csr.priv won't 421 // so inside mmu, these two signals should be divided 422 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = { 423 this.sfence <> sfence 424 this.csr <> csr 425 this.csr.satp := satp 426 } 427} 428 429class TlbIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 430 MMUIOBaseBundle { 431 val requestor = Vec(Width, Flipped(new TlbRequestIO)) 432 val flushPipe = Vec(Width, Input(Bool())) 433 val ptw = new TlbPtwIO(Width) 434 val ptw_replenish = Input(new PMPConfig()) 435 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 436 val pmp = Vec(Width, ValidIO(new PMPReqBundle())) 437 438} 439 440class VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 441 val req = Vec(Width, DecoupledIO(new PtwReq)) 442 val resp = Flipped(DecoupledIO(new Bundle { 443 val data = new PtwResp 444 val vector = Output(Vec(Width, Bool())) 445 })) 446 447 def connect(normal: TlbPtwIO): Unit = { 448 req <> normal.req 449 resp.ready := normal.resp.ready 450 normal.resp.bits := resp.bits.data 451 normal.resp.valid := resp.valid 452 } 453} 454 455/**************************** L2TLB *************************************/ 456abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 457abstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer) 458 with HasXSParameter with HasPtwConst 459 460class PteBundle(implicit p: Parameters) extends PtwBundle{ 461 val reserved = UInt(pteResLen.W) 462 val ppn = UInt(ppnLen.W) 463 val rsw = UInt(2.W) 464 val perm = new Bundle { 465 val d = Bool() 466 val a = Bool() 467 val g = Bool() 468 val u = Bool() 469 val x = Bool() 470 val w = Bool() 471 val r = Bool() 472 val v = Bool() 473 } 474 475 def unaligned(level: UInt) = { 476 isLeaf() && !(level === 2.U || 477 level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 478 level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U) 479 } 480 481 def isPf(level: UInt) = { 482 !perm.v || (!perm.r && perm.w) || unaligned(level) 483 } 484 485 def isLeaf() = { 486 perm.r || perm.x || perm.w 487 } 488 489 def getPerm() = { 490 val pm = Wire(new PtePermBundle) 491 pm.d := perm.d 492 pm.a := perm.a 493 pm.g := perm.g 494 pm.u := perm.u 495 pm.x := perm.x 496 pm.w := perm.w 497 pm.r := perm.r 498 pm 499 } 500 501 override def toPrintable: Printable = { 502 p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 503 } 504} 505 506class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 507 val tag = UInt(tagLen.W) 508 val asid = UInt(asidLen.W) 509 val ppn = UInt(ppnLen.W) 510 val perm = if (hasPerm) Some(new PtePermBundle) else None 511 val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None 512 val prefetch = Bool() 513 val v = Bool() 514 515 def is_normalentry(): Bool = { 516 if (!hasLevel) true.B 517 else level.get === 2.U 518 } 519 520 def genPPN(vpn: UInt): UInt = { 521 if (!hasLevel) ppn 522 else MuxLookup(level.get, 0.U, Seq( 523 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 524 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 525 2.U -> ppn) 526 ) 527 } 528 529 def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = { 530 require(vpn.getWidth == vpnLen) 531// require(this.asid.getWidth <= asid.getWidth) 532 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 533 if (allType) { 534 require(hasLevel) 535 val hit0 = tag(tagLen - 1, vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2) 536 val hit1 = tag(vpnnLen*2 - 1, vpnnLen) === vpn(vpnnLen*2 - 1, vpnnLen) 537 val hit2 = tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 538 539 asid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 540 } else if (hasLevel) { 541 val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 542 val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 543 544 asid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) 545 } else { 546 asid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 547 } 548 } 549 550 def refill(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) { 551 require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 552 553 tag := vpn(vpnLen - 1, vpnLen - tagLen) 554 ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 555 perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 556 this.asid := asid 557 this.prefetch := prefetch 558 this.v := valid 559 this.level.map(_ := level) 560 } 561 562 def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = { 563 val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 564 e.refill(vpn, asid, pte, level, prefetch, valid) 565 e 566 } 567 568 569 570 override def toPrintable: Printable = { 571 // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 572 p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " + 573 (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 574 (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 575 p"prefetch:${prefetch}" 576 } 577} 578 579class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 580 require(log2Up(num)==log2Down(num)) 581 582 val tag = UInt(tagLen.W) 583 val asid = UInt(asidLen.W) 584 val ppns = Vec(num, UInt(ppnLen.W)) 585 val vs = Vec(num, Bool()) 586 val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 587 val prefetch = Bool() 588 // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 589 590 def tagClip(vpn: UInt) = { 591 require(vpn.getWidth == vpnLen) 592 vpn(vpnLen - 1, vpnLen - tagLen) 593 } 594 595 def sectorIdxClip(vpn: UInt, level: Int) = { 596 getVpnClip(vpn, level)(log2Up(num) - 1, 0) 597 } 598 599 def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false) = { 600 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 601 asid_hit && tag === tagClip(vpn) && vs(sectorIdxClip(vpn, level)) // TODO: optimize this. don't need to compare each with tag 602 } 603 604 def genEntries(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 605 require((data.getWidth / XLEN) == num, 606 s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 607 608 val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm)) 609 ps.tag := tagClip(vpn) 610 ps.asid := asid 611 ps.prefetch := prefetch 612 for (i <- 0 until num) { 613 val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 614 ps.ppns(i) := pte.ppn 615 ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 616 ps.perms.map(_(i) := pte.perm) 617 } 618 ps 619 } 620 621 override def toPrintable: Printable = { 622 // require(num == 4, "if num is not 4, please comment this toPrintable") 623 // NOTE: if num is not 4, please comment this toPrintable 624 val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 625 p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 626 (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 627 } 628} 629 630class PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 631 val entries = new PtwEntries(num, tagLen, level, hasPerm) 632 633 val ecc_block = XLEN 634 val ecc_info = get_ecc_info() 635 val ecc = UInt(ecc_info._1.W) 636 637 def get_ecc_info(): (Int, Int, Int, Int) = { 638 val eccBits_per = eccCode.width(ecc_block) - ecc_block 639 640 val data_length = entries.getWidth 641 val data_align_num = data_length / ecc_block 642 val data_not_align = (data_length % ecc_block) != 0 // ugly code 643 val data_unalign_length = data_length - data_align_num * ecc_block 644 val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length 645 646 val eccBits = eccBits_per * data_align_num + eccBits_unalign 647 (eccBits, eccBits_per, data_align_num, data_unalign_length) 648 } 649 650 def encode() = { 651 val data = entries.asUInt() 652 val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W))) 653 for (i <- 0 until ecc_info._3) { 654 ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block 655 } 656 if (ecc_info._4 != 0) { 657 val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4 658 ecc := Cat(ecc_unaligned, ecc_slices.asUInt()) 659 } else { ecc := ecc_slices.asUInt() } 660 } 661 662 def decode(): Bool = { 663 val data = entries.asUInt() 664 val res = Wire(Vec(ecc_info._3 + 1, Bool())) 665 for (i <- 0 until ecc_info._3) { 666 res(i) := {if (ecc_info._2 != 0) eccCode.decode(Cat(ecc((i+1)*ecc_info._2-1, i*ecc_info._2), data((i+1)*ecc_block-1, i*ecc_block))).error else false.B} 667 } 668 if (ecc_info._2 != 0 && ecc_info._4 != 0) { 669 res(ecc_info._3) := eccCode.decode( 670 Cat(ecc(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error 671 } else { res(ecc_info._3) := false.B } 672 673 Cat(res).orR 674 } 675 676 def gen(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 677 this.entries := entries.genEntries(vpn, asid, data, levelUInt, prefetch) 678 this.encode() 679 } 680} 681 682class PtwReq(implicit p: Parameters) extends PtwBundle { 683 val vpn = UInt(vpnLen.W) 684 685 override def toPrintable: Printable = { 686 p"vpn:0x${Hexadecimal(vpn)}" 687 } 688} 689 690class PtwResp(implicit p: Parameters) extends PtwBundle { 691 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 692 val pf = Bool() 693 val af = Bool() 694 695 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 696 this.entry.level.map(_ := level) 697 this.entry.tag := vpn 698 this.entry.perm.map(_ := pte.getPerm()) 699 this.entry.ppn := pte.ppn 700 this.entry.prefetch := DontCare 701 this.entry.asid := asid 702 this.entry.v := !pf 703 this.pf := pf 704 this.af := af 705 } 706 707 override def toPrintable: Printable = { 708 p"entry:${entry} pf:${pf} af:${af}" 709 } 710} 711 712class L2TLBIO(implicit p: Parameters) extends PtwBundle { 713 val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 714 val sfence = Input(new SfenceBundle) 715 val csr = new Bundle { 716 val tlb = Input(new TlbCsrBundle) 717 val distribute_csr = Flipped(new DistributedCSRIO) 718 } 719} 720 721class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 722 val addr = UInt(PAddrBits.W) 723 val id = UInt(bMemID.W) 724} 725 726class L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 727 val source = UInt(bSourceWidth.W) 728} 729 730 731object ValidHoldBypass{ 732 def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = { 733 val valid = RegInit(false.B) 734 when (infire) { valid := true.B } 735 when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold 736 when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok? 737 valid || infire 738 } 739} 740