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 utility._ 25import xiangshan.backend.rob.RobPtr 26import xiangshan.backend.fu.util.HasCSRConst 27import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 28import freechips.rocketchip.tilelink._ 29import xiangshan.backend.fu.{PMPReqBundle, PMPConfig} 30import xiangshan.backend.fu.PMPBundle 31 32 33abstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst 34abstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst 35 36class VaBundle(implicit p: Parameters) extends TlbBundle { 37 val vpn = UInt(vpnLen.W) 38 val off = UInt(offLen.W) 39} 40 41class PtePermBundle(implicit p: Parameters) extends TlbBundle { 42 val d = Bool() 43 val a = Bool() 44 val g = Bool() 45 val u = Bool() 46 val x = Bool() 47 val w = Bool() 48 val r = Bool() 49 50 override def toPrintable: Printable = { 51 p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// + 52 //(if(hasV) (p"v:${v}") else p"") 53 } 54} 55 56class TlbPMBundle(implicit p: Parameters) extends TlbBundle { 57 val r = Bool() 58 val w = Bool() 59 val x = Bool() 60 val c = Bool() 61 val atomic = Bool() 62 63 def assign_ap(pm: PMPConfig) = { 64 r := pm.r 65 w := pm.w 66 x := pm.x 67 c := pm.c 68 atomic := pm.atomic 69 } 70} 71 72class TlbPermBundle(implicit p: Parameters) extends TlbBundle { 73 val pf = Bool() // NOTE: if this is true, just raise pf 74 val af = Bool() // NOTE: if this is true, just raise af 75 // pagetable perm (software defined) 76 val d = Bool() 77 val a = Bool() 78 val g = Bool() 79 val u = Bool() 80 val x = Bool() 81 val w = Bool() 82 val r = Bool() 83 84 // static pmp & pma check has a minimum grain size of 4K 85 // So sector tlb will use eight static pm entries 86 val pm = Vec(tlbcontiguous, new TlbPMBundle) 87 88 def apply(item: PtwSectorResp, pm: Seq[PMPConfig]) = { 89 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 90 this.pf := item.pf 91 this.af := item.af 92 this.d := ptePerm.d 93 this.a := ptePerm.a 94 this.g := ptePerm.g 95 this.u := ptePerm.u 96 this.x := ptePerm.x 97 this.w := ptePerm.w 98 this.r := ptePerm.r 99 100 for (i <- 0 until tlbcontiguous) { 101 this.pm(i).assign_ap(pm(i)) 102 } 103 this 104 } 105 override def toPrintable: Printable = { 106 p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " + 107 p"pm:${pm}" 108 } 109} 110 111// multi-read && single-write 112// input is data, output is hot-code(not one-hot) 113class CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 114 val io = IO(new Bundle { 115 val r = new Bundle { 116 val req = Input(Vec(readWidth, gen)) 117 val resp = Output(Vec(readWidth, Vec(set, Bool()))) 118 } 119 val w = Input(new Bundle { 120 val valid = Bool() 121 val bits = new Bundle { 122 val index = UInt(log2Up(set).W) 123 val data = gen 124 } 125 }) 126 }) 127 128 val wordType = UInt(gen.getWidth.W) 129 val array = Reg(Vec(set, wordType)) 130 131 io.r.resp.zipWithIndex.map{ case (a,i) => 132 a := array.map(io.r.req(i).asUInt === _) 133 } 134 135 when (io.w.valid) { 136 array(io.w.bits.index) := io.w.bits.data.asUInt 137 } 138} 139 140class TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 141 require(pageNormal || pageSuper) 142 143 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 144 else UInt(sectorvpnLen.W) 145 val asid = UInt(asidLen.W) 146 val level = if (!pageNormal) Some(UInt(1.W)) 147 else if (!pageSuper) None 148 else Some(UInt(2.W)) 149 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 150 else UInt(sectorppnLen.W) 151 val perm = new TlbPermBundle 152 val valididx = Vec(tlbcontiguous, Bool()) 153 val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 154 155 /** level usage: 156 * !PageSuper: page is only normal, level is None, match all the tag 157 * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 158 * bits0 0: need mid 9bits 159 * 1: no need mid 9bits 160 * PageSuper && PageNormal: page hold all the three type, 161 * bits0 0: need low 9bits 162 * bits1 0: need mid 9bits 163 */ 164 165 def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = { 166 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 167 val addr_low_hit = valididx(vpn(2, 0)) 168 169 // NOTE: for timing, dont care low set index bits at hit check 170 // do not need store the low bits actually 171 if (!pageSuper) asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) && addr_low_hit 172 else if (!pageNormal) { 173 val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 174 val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen) 175 val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi) 176 asid_hit && tag_match && addr_low_hit 177 } 178 else { 179 val tmp_level = level.get 180 val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 181 val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 182 val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false 183 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 184 asid_hit && tag_match && addr_low_hit 185 } 186 } 187 188 def wbhit(data: PtwSectorResp, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = { 189 val vpn = Cat(data.entry.tag, 0.U(sectortlbwidth.W)) 190 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 191 val vpn_hit = Wire(Bool()) 192 val index_hit = Wire(Vec(tlbcontiguous, Bool())) 193 194 // NOTE: for timing, dont care low set index bits at hit check 195 // do not need store the low bits actually 196 if (!pageSuper) { 197 vpn_hit := asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) 198 } 199 else if (!pageNormal) { 200 val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 201 val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen) 202 val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi) 203 vpn_hit := asid_hit && tag_match 204 } 205 else { 206 val tmp_level = level.get 207 val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 208 val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 209 val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false 210 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 211 vpn_hit := asid_hit && tag_match 212 } 213 214 for (i <- 0 until tlbcontiguous) { 215 index_hit(i) := data.valididx(i) && valididx(i) 216 } 217 218 // For example, tlb req to page cache with vpn 0x10 219 // At this time, 0x13 has not been paged, so page cache only resp 0x10 220 // When 0x13 refill to page cache, previous item will be flushed 221 // Now 0x10 and 0x13 are both valid in page cache 222 // However, when 0x13 refill to tlb, will trigger multi hit 223 // So will only trigger multi-hit when PopCount(data.valididx) = 1 224 vpn_hit && index_hit.reduce(_ || _) && PopCount(data.valididx) === 1.U 225 } 226 227 def apply(item: PtwSectorResp, asid: UInt, pm: Seq[PMPConfig]): TlbEntry = { 228 this.tag := {if (pageNormal) item.entry.tag else item.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)} 229 this.asid := asid 230 val inner_level = item.entry.level.getOrElse(0.U) 231 this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U, Seq( 232 0.U -> 3.U, 233 1.U -> 1.U, 234 2.U -> 0.U )) 235 else if (pageSuper) ~inner_level(0) 236 else 0.U }) 237 this.ppn := { if (!pageNormal) item.entry.ppn(sectorppnLen - 1, vpnnLen - sectortlbwidth) 238 else item.entry.ppn } 239 this.perm.apply(item, pm) 240 this.ppn_low := item.ppn_low 241 this.valididx := item.valididx 242 this 243 } 244 245 // 4KB is normal entry, 2MB/1GB is considered as super entry 246 def is_normalentry(): Bool = { 247 if (!pageSuper) { true.B } 248 else if (!pageNormal) { false.B } 249 else { level.get === 0.U } 250 } 251 252 def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 253 val inner_level = level.getOrElse(0.U) 254 val ppn_res = if (!pageSuper) Cat(ppn, ppn_low(vpn(sectortlbwidth - 1, 0))) 255 else if (!pageNormal) Cat(ppn(ppnLen - vpnnLen - 1, vpnnLen), 256 Mux(inner_level(0), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen - 1,0)), 257 vpn(vpnnLen - 1, 0)) 258 else Cat(ppn(sectorppnLen - 1, vpnnLen * 2 - sectortlbwidth), 259 Mux(inner_level(1), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)), 260 Mux(inner_level(0), vpn(vpnnLen - 1, 0), Cat(ppn(vpnnLen - sectortlbwidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))))) 261 262 if (saveLevel) { 263 if (ppn.getWidth == ppnLen - vpnnLen) { 264 Cat(ppn(ppn.getWidth - 1, vpnnLen * 2), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid)) 265 } else { 266 require(ppn.getWidth == sectorppnLen) 267 Cat(ppn(ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid)) 268 } 269 } 270 else ppn_res 271 } 272 273 override def toPrintable: Printable = { 274 val inner_level = level.getOrElse(2.U) 275 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 276 } 277 278} 279 280object TlbCmd { 281 def read = "b00".U 282 def write = "b01".U 283 def exec = "b10".U 284 285 def atom_read = "b100".U // lr 286 def atom_write = "b101".U // sc / amo 287 288 def apply() = UInt(3.W) 289 def isRead(a: UInt) = a(1,0)===read 290 def isWrite(a: UInt) = a(1,0)===write 291 def isExec(a: UInt) = a(1,0)===exec 292 293 def isAtom(a: UInt) = a(2) 294 def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 295} 296 297class TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 298 val r = new Bundle { 299 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 300 val vpn = Output(UInt(vpnLen.W)) 301 }))) 302 val resp = Vec(ports, ValidIO(new Bundle{ 303 val hit = Output(Bool()) 304 val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 305 val perm = Vec(nDups, Output(new TlbPermBundle())) 306 })) 307 } 308 val w = Flipped(ValidIO(new Bundle { 309 val wayIdx = Output(UInt(log2Up(nWays).W)) 310 val data = Output(new PtwSectorResp) 311 val data_replenish = Vec(tlbcontiguous, Output(new PMPConfig)) 312 })) 313 val victim = new Bundle { 314 val out = ValidIO(Output(new Bundle { 315 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 316 })) 317 val in = Flipped(ValidIO(Output(new Bundle { 318 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 319 }))) 320 } 321 val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays)) 322 323 def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = { 324 this.r.req(i).valid := valid 325 this.r.req(i).bits.vpn := vpn 326 } 327 328 def r_resp_apply(i: Int) = { 329 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm) 330 } 331 332 def w_apply(valid: Bool, wayIdx: UInt, data: PtwSectorResp, data_replenish: Seq[PMPConfig]): Unit = { 333 this.w.valid := valid 334 this.w.bits.wayIdx := wayIdx 335 this.w.bits.data := data 336 this.w.bits.data_replenish := data_replenish 337 } 338 339} 340 341class TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 342 val r = new Bundle { 343 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 344 val vpn = Output(UInt(vpnLen.W)) 345 }))) 346 val resp = Vec(ports, ValidIO(new Bundle{ 347 val hit = Output(Bool()) 348 val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 349 val perm = Vec(nDups, Output(new TlbPermBundle())) 350 // below are dirty code for timing optimization 351 val super_hit = Output(Bool()) 352 val super_ppn = Output(UInt(ppnLen.W)) 353 val spm = Output(new TlbPMBundle) 354 })) 355 } 356 val w = Flipped(ValidIO(new Bundle { 357 val data = Output(new PtwSectorResp) 358 val data_replenish = Vec(tlbcontiguous, Output(new PMPConfig)) 359 })) 360 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null 361 362 def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = { 363 this.r.req(i).valid := valid 364 this.r.req(i).bits.vpn := vpn 365 } 366 367 def r_resp_apply(i: Int) = { 368 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, 369 this.r.resp(i).bits.super_hit, this.r.resp(i).bits.super_ppn, this.r.resp(i).bits.spm) 370 } 371 372 def w_apply(valid: Bool, data: PtwSectorResp, data_replenish: Seq[PMPConfig]): Unit = { 373 this.w.valid := valid 374 this.w.bits.data := data 375 this.w.bits.data_replenish := data_replenish 376 } 377} 378 379class ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 380 val sets = Output(UInt(log2Up(nSets).W)) 381 val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W))) 382} 383 384class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 385 val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays))) 386 387 val refillIdx = Output(UInt(log2Up(nWays).W)) 388 val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 389 390 def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 391 for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) { 392 ac_rep := ac_tlb 393 } 394 this.chosen_set := get_set_idx(vpn, nSets) 395 in.map(a => a.refillIdx := this.refillIdx) 396 } 397} 398 399class TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 400 TlbBundle { 401 val normalPage = new ReplaceIO(Width, q.normalNSets, q.normalNWays) 402 val superPage = new ReplaceIO(Width, q.superNSets, q.superNWays) 403 404 def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 405 this.normalPage.apply_sep(in.map(_.normalPage), vpn) 406 this.superPage.apply_sep(in.map(_.superPage), vpn) 407 } 408 409} 410 411class MemBlockidxBundle(implicit p: Parameters) extends TlbBundle { 412 val is_ld = Bool() 413 val is_st = Bool() 414 val idx = 415 if (LoadQueueSize >= StoreQueueSize) { 416 val idx = UInt(log2Ceil(LoadQueueSize).W) 417 idx 418 } else { 419 val idx = UInt(log2Ceil(StoreQueueSize).W) 420 idx 421 } 422} 423 424class TlbReq(implicit p: Parameters) extends TlbBundle { 425 val vaddr = Output(UInt(VAddrBits.W)) 426 val cmd = Output(TlbCmd()) 427 val size = Output(UInt(log2Ceil(log2Ceil(XLEN/8)+1).W)) 428 val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache 429 val memidx = Output(new MemBlockidxBundle) 430 // do not translate, but still do pmp/pma check 431 val no_translate = Output(Bool()) 432 val debug = new Bundle { 433 val pc = Output(UInt(XLEN.W)) 434 val robIdx = Output(new RobPtr) 435 val isFirstIssue = Output(Bool()) 436 } 437 438 // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead 439 override def toPrintable: Printable = { 440 p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}" 441 } 442} 443 444class TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 445 val ld = Output(Bool()) 446 val st = Output(Bool()) 447 val instr = Output(Bool()) 448} 449 450class TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 451 val paddr = Vec(nDups, Output(UInt(PAddrBits.W))) 452 val miss = Output(Bool()) 453 val fast_miss = Output(Bool()) // without sram part for timing optimization 454 val excp = Vec(nDups, new Bundle { 455 val pf = new TlbExceptionBundle() 456 val af = new TlbExceptionBundle() 457 }) 458 val static_pm = Output(Valid(Bool())) // valid for static, bits for mmio result from normal entries 459 val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state 460 val memidx = Output(new MemBlockidxBundle) 461 462 val debug = new Bundle { 463 val robIdx = Output(new RobPtr) 464 val isFirstIssue = Output(Bool()) 465 } 466 override def toPrintable: Printable = { 467 p"paddr:0x${Hexadecimal(paddr(0))} miss:${miss} excp.pf: ld:${excp(0).pf.ld} st:${excp(0).pf.st} instr:${excp(0).pf.instr} ptwBack:${ptwBack}" 468 } 469} 470 471class TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 472 val req = DecoupledIO(new TlbReq) 473 val req_kill = Output(Bool()) 474 val resp = Flipped(DecoupledIO(new TlbResp(nRespDups))) 475} 476 477class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 478 val req = Vec(Width, DecoupledIO(new PtwReq)) 479 val resp = Flipped(DecoupledIO(new PtwSectorResp)) 480 481 482 override def toPrintable: Printable = { 483 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 484 } 485} 486 487class TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 488 val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx)) 489 val resp = Flipped(DecoupledIO(new PtwSectorRespwithMemIdx)) 490 491 492 override def toPrintable: Printable = { 493 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 494 } 495} 496 497class MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 498 val sfence = Input(new SfenceBundle) 499 val csr = Input(new TlbCsrBundle) 500 501 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 502 this.sfence <> sfence 503 this.csr <> csr 504 } 505 506 // overwrite satp. write satp will cause flushpipe but csr.priv won't 507 // satp will be dealyed several cycles from writing, but csr.priv won't 508 // so inside mmu, these two signals should be divided 509 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = { 510 this.sfence <> sfence 511 this.csr <> csr 512 this.csr.satp := satp 513 } 514} 515 516class TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle { 517 val valid = Bool() 518 val memidx = new MemBlockidxBundle 519} 520 521class TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends 522 MMUIOBaseBundle { 523 val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups))) 524 val flushPipe = Vec(Width, Input(Bool())) 525 val ptw = new TlbPtwIOwithMemIdx(Width) 526 val refill_to_mem = Output(new TlbRefilltoMemIO()) 527 val ptw_replenish = Vec(tlbcontiguous, Input(new PMPConfig())) 528 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 529 val pmp = Vec(Width, ValidIO(new PMPReqBundle())) 530 531} 532 533class VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 534 val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx())) 535 val resp = Flipped(DecoupledIO(new Bundle { 536 val data = new PtwSectorRespwithMemIdx 537 val vector = Output(Vec(Width, Bool())) 538 })) 539 540 def connect(normal: TlbPtwIOwithMemIdx): Unit = { 541 req <> normal.req 542 resp.ready := normal.resp.ready 543 normal.resp.bits := resp.bits.data 544 normal.resp.valid := resp.valid 545 } 546} 547 548/**************************** L2TLB *************************************/ 549abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 550abstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer) 551 with HasXSParameter with HasPtwConst 552 553class PteBundle(implicit p: Parameters) extends PtwBundle{ 554 val reserved = UInt(pteResLen.W) 555 val ppn_high = UInt(ppnHignLen.W) 556 val ppn = UInt(ppnLen.W) 557 val rsw = UInt(2.W) 558 val perm = new Bundle { 559 val d = Bool() 560 val a = Bool() 561 val g = Bool() 562 val u = Bool() 563 val x = Bool() 564 val w = Bool() 565 val r = Bool() 566 val v = Bool() 567 } 568 569 def unaligned(level: UInt) = { 570 isLeaf() && !(level === 2.U || 571 level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 572 level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U) 573 } 574 575 def isPf(level: UInt) = { 576 !perm.v || (!perm.r && perm.w) || unaligned(level) 577 } 578 579 // paddr of Xiangshan is 36 bits but ppn of sv39 is 44 bits 580 // access fault will be raised when ppn >> ppnLen is not zero 581 def isAf() = { 582 !(ppn_high === 0.U) 583 } 584 585 def isLeaf() = { 586 perm.r || perm.x || perm.w 587 } 588 589 def getPerm() = { 590 val pm = Wire(new PtePermBundle) 591 pm.d := perm.d 592 pm.a := perm.a 593 pm.g := perm.g 594 pm.u := perm.u 595 pm.x := perm.x 596 pm.w := perm.w 597 pm.r := perm.r 598 pm 599 } 600 601 override def toPrintable: Printable = { 602 p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 603 } 604} 605 606class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 607 val tag = UInt(tagLen.W) 608 val asid = UInt(asidLen.W) 609 val ppn = UInt(ppnLen.W) 610 val perm = if (hasPerm) Some(new PtePermBundle) else None 611 val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None 612 val prefetch = Bool() 613 val v = Bool() 614 615 def is_normalentry(): Bool = { 616 if (!hasLevel) true.B 617 else level.get === 2.U 618 } 619 620 def genPPN(vpn: UInt): UInt = { 621 if (!hasLevel) ppn 622 else MuxLookup(level.get, 0.U, Seq( 623 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 624 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 625 2.U -> ppn) 626 ) 627 } 628 629 def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = { 630 require(vpn.getWidth == vpnLen) 631// require(this.asid.getWidth <= asid.getWidth) 632 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 633 if (allType) { 634 require(hasLevel) 635 val hit0 = tag(tagLen - 1, vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2) 636 val hit1 = tag(vpnnLen*2 - 1, vpnnLen) === vpn(vpnnLen*2 - 1, vpnnLen) 637 val hit2 = tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 638 639 asid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 640 } else if (hasLevel) { 641 val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 642 val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 643 644 asid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) 645 } else { 646 asid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 647 } 648 } 649 650 def refill(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) { 651 require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 652 653 tag := vpn(vpnLen - 1, vpnLen - tagLen) 654 ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 655 perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 656 this.asid := asid 657 this.prefetch := prefetch 658 this.v := valid 659 this.level.map(_ := level) 660 } 661 662 def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = { 663 val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 664 e.refill(vpn, asid, pte, level, prefetch, valid) 665 e 666 } 667 668 669 670 override def toPrintable: Printable = { 671 // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 672 p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " + 673 (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 674 (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 675 p"prefetch:${prefetch}" 676 } 677} 678 679class PtwSectorEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwEntry(tagLen, hasPerm, hasLevel) { 680 override val ppn = UInt(sectorppnLen.W) 681} 682 683class PtwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwSectorEntry(tagLen, hasPerm, hasLevel) { 684 val ppn_low = UInt(sectortlbwidth.W) 685 val af = Bool() 686 val pf = Bool() 687} 688 689class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 690 require(log2Up(num)==log2Down(num)) 691 // NOTE: hasPerm means that is leaf or not. 692 693 val tag = UInt(tagLen.W) 694 val asid = UInt(asidLen.W) 695 val ppns = Vec(num, UInt(ppnLen.W)) 696 val vs = Vec(num, Bool()) 697 val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 698 val prefetch = Bool() 699 // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 700 // NOTE: vs is used for different usage: 701 // for l3, which store the leaf(leaves), vs is page fault or not. 702 // for l2, which shoule not store leaf, vs is valid or not, that will anticipate in hit check 703 // Because, l2 should not store leaf(no perm), it doesn't store perm. 704 // If l2 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful. 705 // TODO: divide vs into validVec and pfVec 706 // for l2: may valid but pf, so no need for page walk, return random pte with pf. 707 708 def tagClip(vpn: UInt) = { 709 require(vpn.getWidth == vpnLen) 710 vpn(vpnLen - 1, vpnLen - tagLen) 711 } 712 713 def sectorIdxClip(vpn: UInt, level: Int) = { 714 getVpnClip(vpn, level)(log2Up(num) - 1, 0) 715 } 716 717 def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false) = { 718 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 719 asid_hit && tag === tagClip(vpn) && (if (hasPerm) true.B else vs(sectorIdxClip(vpn, level))) 720 } 721 722 def genEntries(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 723 require((data.getWidth / XLEN) == num, 724 s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 725 726 val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm)) 727 ps.tag := tagClip(vpn) 728 ps.asid := asid 729 ps.prefetch := prefetch 730 for (i <- 0 until num) { 731 val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 732 ps.ppns(i) := pte.ppn 733 ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 734 ps.perms.map(_(i) := pte.perm) 735 } 736 ps 737 } 738 739 override def toPrintable: Printable = { 740 // require(num == 4, "if num is not 4, please comment this toPrintable") 741 // NOTE: if num is not 4, please comment this toPrintable 742 val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 743 p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 744 (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 745 } 746} 747 748class PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 749 val entries = new PtwEntries(num, tagLen, level, hasPerm) 750 751 val ecc_block = XLEN 752 val ecc_info = get_ecc_info() 753 val ecc = UInt(ecc_info._1.W) 754 755 def get_ecc_info(): (Int, Int, Int, Int) = { 756 val eccBits_per = eccCode.width(ecc_block) - ecc_block 757 758 val data_length = entries.getWidth 759 val data_align_num = data_length / ecc_block 760 val data_not_align = (data_length % ecc_block) != 0 // ugly code 761 val data_unalign_length = data_length - data_align_num * ecc_block 762 val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length 763 764 val eccBits = eccBits_per * data_align_num + eccBits_unalign 765 (eccBits, eccBits_per, data_align_num, data_unalign_length) 766 } 767 768 def encode() = { 769 val data = entries.asUInt() 770 val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W))) 771 for (i <- 0 until ecc_info._3) { 772 ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block 773 } 774 if (ecc_info._4 != 0) { 775 val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4 776 ecc := Cat(ecc_unaligned, ecc_slices.asUInt()) 777 } else { ecc := ecc_slices.asUInt() } 778 } 779 780 def decode(): Bool = { 781 val data = entries.asUInt() 782 val res = Wire(Vec(ecc_info._3 + 1, Bool())) 783 for (i <- 0 until ecc_info._3) { 784 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} 785 } 786 if (ecc_info._2 != 0 && ecc_info._4 != 0) { 787 res(ecc_info._3) := eccCode.decode( 788 Cat(ecc(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error 789 } else { res(ecc_info._3) := false.B } 790 791 Cat(res).orR 792 } 793 794 def gen(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 795 this.entries := entries.genEntries(vpn, asid, data, levelUInt, prefetch) 796 this.encode() 797 } 798} 799 800class PtwReq(implicit p: Parameters) extends PtwBundle { 801 val vpn = UInt(vpnLen.W) 802 803 override def toPrintable: Printable = { 804 p"vpn:0x${Hexadecimal(vpn)}" 805 } 806} 807 808class PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq { 809 val memidx = new MemBlockidxBundle 810} 811 812class PtwResp(implicit p: Parameters) extends PtwBundle { 813 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 814 val pf = Bool() 815 val af = Bool() 816 817 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 818 this.entry.level.map(_ := level) 819 this.entry.tag := vpn 820 this.entry.perm.map(_ := pte.getPerm()) 821 this.entry.ppn := pte.ppn 822 this.entry.prefetch := DontCare 823 this.entry.asid := asid 824 this.entry.v := !pf 825 this.pf := pf 826 this.af := af 827 } 828 829 override def toPrintable: Printable = { 830 p"entry:${entry} pf:${pf} af:${af}" 831 } 832} 833 834class PtwResptomerge (implicit p: Parameters) extends PtwBundle { 835 val entry = UInt(blockBits.W) 836 val vpn = UInt(vpnLen.W) 837 val level = UInt(log2Up(Level).W) 838 val pf = Bool() 839 val af = Bool() 840 val asid = UInt(asidLen.W) 841 842 def apply(pf: Bool, af: Bool, level: UInt, pte: UInt, vpn: UInt, asid: UInt) = { 843 this.entry := pte 844 this.pf := pf 845 this.af := af 846 this.level := level 847 this.vpn := vpn 848 this.asid := asid 849 } 850 851 override def toPrintable: Printable = { 852 p"entry:${entry} pf:${pf} af:${af}" 853 } 854} 855 856class PtwRespwithMemIdx(implicit p: Parameters) extends PtwResp { 857 val memidx = new MemBlockidxBundle 858} 859 860class PtwSectorRespwithMemIdx(implicit p: Parameters) extends PtwSectorResp { 861 val memidx = new MemBlockidxBundle 862} 863 864class PtwSectorResp(implicit p: Parameters) extends PtwBundle { 865 val entry = new PtwSectorEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true) 866 val addr_low = UInt(sectortlbwidth.W) 867 val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 868 val valididx = Vec(tlbcontiguous, Bool()) 869 val pf = Bool() 870 val af = Bool() 871 872 def genPPN(vpn: UInt): UInt = { 873 MuxLookup(entry.level.get, 0.U, Seq( 874 0.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen*2-1, 0)), 875 1.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen - sectortlbwidth), vpn(vpnnLen-1, 0)), 876 2.U -> Cat(entry.ppn(entry.ppn.getWidth-1, 0), ppn_low(vpn(sectortlbwidth - 1, 0)))) 877 ) 878 } 879 880 def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = { 881 require(vpn.getWidth == vpnLen) 882 // require(this.asid.getWidth <= asid.getWidth) 883 val asid_hit = if (ignoreAsid) true.B else (this.entry.asid === asid) 884 if (allType) { 885 val hit0 = entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * 2) 886 val hit1 = entry.tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 887 val hit2 = entry.tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) 888 val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 889 890 asid_hit && Mux(entry.level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(entry.level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) && addr_low_hit 891 } else { 892 val hit0 = entry.tag(sectorvpnLen - 1, sectorvpnLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 893 val hit1 = entry.tag(sectorvpnLen - vpnnLen - 1, sectorvpnLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 894 val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 895 896 asid_hit && Mux(entry.level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) && addr_low_hit 897 } 898 } 899} 900 901class PtwMergeResp(implicit p: Parameters) extends PtwBundle { 902 val entry = Vec(tlbcontiguous, new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 903 val pteidx = Vec(tlbcontiguous, Bool()) 904 val not_super = Bool() 905 906 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt, addr_low : UInt, not_super : Boolean = true) = { 907 assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 908 909 val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 910 ptw_resp.ppn := pte.ppn(ppnLen - 1, sectortlbwidth) 911 ptw_resp.ppn_low := pte.ppn(sectortlbwidth - 1, 0) 912 ptw_resp.level.map(_ := level) 913 ptw_resp.perm.map(_ := pte.getPerm()) 914 ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 915 ptw_resp.pf := pf 916 ptw_resp.af := af 917 ptw_resp.v := !pf 918 ptw_resp.prefetch := DontCare 919 ptw_resp.asid := asid 920 this.pteidx := UIntToOH(addr_low).asBools 921 this.not_super := not_super.B 922 for (i <- 0 until tlbcontiguous) { 923 this.entry(i) := ptw_resp 924 } 925 } 926} 927 928class L2TLBIO(implicit p: Parameters) extends PtwBundle { 929 val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 930 val sfence = Input(new SfenceBundle) 931 val csr = new Bundle { 932 val tlb = Input(new TlbCsrBundle) 933 val distribute_csr = Flipped(new DistributedCSRIO) 934 } 935} 936 937class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 938 val addr = UInt(PAddrBits.W) 939 val id = UInt(bMemID.W) 940} 941 942class L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 943 val source = UInt(bSourceWidth.W) 944} 945 946 947object ValidHoldBypass{ 948 def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = { 949 val valid = RegInit(false.B) 950 when (infire) { valid := true.B } 951 when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold 952 when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok? 953 valid || infire 954 } 955} 956 957class L1TlbDB(implicit p: Parameters) extends TlbBundle { 958 val vpn = UInt(vpnLen.W) 959} 960 961class PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 962 val vpn = UInt(vpnLen.W) 963 val source = UInt(bSourceWidth.W) 964 val bypassed = Bool() 965 val is_first = Bool() 966 val prefetched = Bool() 967 val prefetch = Bool() 968 val l2Hit = Bool() 969 val l1Hit = Bool() 970 val hit = Bool() 971} 972 973class PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 974 val vpn = UInt(vpnLen.W) 975 val source = UInt(bSourceWidth.W) 976} 977 978class L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle { 979 val vpn = UInt(vpnLen.W) 980} 981 982class L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle { 983 val vpn = UInt(vpnLen.W) 984} 985