xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/TLBStorage.scala (revision 6ed1154e257b89d669ae5b1c150687b660760e8b)
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.experimental.{ExtModule, chiselName}
22import chisel3.util._
23import utils._
24import utility._
25import freechips.rocketchip.formal.PropertyClass
26import xiangshan.backend.fu.util.HasCSRConst
27
28import scala.math.min
29
30class BankedAsyncDataModuleTemplateWithDup[T <: Data](
31  gen: T,
32  numEntries: Int,
33  numRead: Int,
34  numDup: Int,
35  numBanks: Int
36) extends Module {
37  val io = IO(new Bundle {
38    val raddr = Vec(numRead, Input(UInt(log2Ceil(numEntries).W)))
39    val rdata = Vec(numRead, Vec(numDup, Output(gen)))
40    val wen   = Input(Bool())
41    val waddr = Input(UInt(log2Ceil(numEntries).W))
42    val wdata = Input(gen)
43  })
44  require(numBanks > 1)
45  require(numEntries > numBanks)
46
47  val numBankEntries = numEntries / numBanks
48  def bankOffset(address: UInt): UInt = {
49    address(log2Ceil(numBankEntries) - 1, 0)
50  }
51
52  def bankIndex(address: UInt): UInt = {
53    address(log2Ceil(numEntries) - 1, log2Ceil(numBankEntries))
54  }
55
56  val dataBanks = Seq.tabulate(numBanks)(i => {
57    val bankEntries = if (i < numBanks - 1) numBankEntries else (numEntries - (i * numBankEntries))
58    Mem(bankEntries, gen)
59  })
60
61  // async read, but regnext
62  for (i <- 0 until numRead) {
63    val data_read = Reg(Vec(numDup, Vec(numBanks, gen)))
64    val bank_index = Reg(Vec(numDup, UInt(numBanks.W)))
65    for (j <- 0 until numDup) {
66      bank_index(j) := UIntToOH(bankIndex(io.raddr(i)))
67      for (k <- 0 until numBanks) {
68        data_read(j)(k) := Mux(io.wen && (io.waddr === io.raddr(i)),
69          io.wdata, dataBanks(k)(bankOffset(io.raddr(i))))
70      }
71    }
72    // next cycle
73    for (j <- 0 until numDup) {
74      io.rdata(i)(j) := Mux1H(bank_index(j), data_read(j))
75    }
76  }
77
78  // write
79  for (i <- 0 until numBanks) {
80    when (io.wen && (bankIndex(io.waddr) === i.U)) {
81      dataBanks(i)(bankOffset(io.waddr)) := io.wdata
82    }
83  }
84}
85
86@chiselName
87class TLBFA(
88  parentName: String,
89  ports: Int,
90  nSets: Int,
91  nWays: Int,
92  saveLevel: Boolean = false,
93  normalPage: Boolean,
94  superPage: Boolean
95)(implicit p: Parameters) extends TlbModule with HasPerfEvents {
96
97  val io = IO(new TlbStorageIO(nSets, nWays, ports))
98  io.r.req.map(_.ready := true.B)
99
100  val v = RegInit(VecInit(Seq.fill(nWays)(false.B)))
101  val entries = Reg(Vec(nWays, new TlbEntry(normalPage, superPage)))
102  val g = entries.map(_.perm.g)
103
104  for (i <- 0 until ports) {
105    val req = io.r.req(i)
106    val resp = io.r.resp(i)
107    val access = io.access(i)
108
109    val vpn = req.bits.vpn
110    val vpn_reg = RegEnable(vpn, req.fire())
111    val vpn_gen_ppn = if(saveLevel) vpn else vpn_reg
112
113    val refill_mask = Mux(io.w.valid, UIntToOH(io.w.bits.wayIdx), 0.U(nWays.W))
114    val hitVec = VecInit((entries.zipWithIndex).zip(v zip refill_mask.asBools).map{case (e, m) => e._1.hit(vpn, io.csr.satp.asid) && m._1 && !m._2 })
115
116    hitVec.suggestName("hitVec")
117
118    val hitVecReg = RegEnable(hitVec, req.fire())
119    // Sector tlb may trigger multi-hit, see def "wbhit"
120    XSPerfAccumulate(s"port${i}_multi_hit", !(!resp.valid || (PopCount(hitVecReg) === 0.U || PopCount(hitVecReg) === 1.U)))
121    // assert(!resp.valid || (PopCount(hitVecReg) === 0.U || PopCount(hitVecReg) === 1.U), s"${parentName} fa port${i} multi-hit")
122
123    resp.valid := RegNext(req.valid)
124    resp.bits.hit := Cat(hitVecReg).orR
125    if (nWays == 1) {
126      resp.bits.ppn(0) := entries(0).genPPN(saveLevel, req.valid)(vpn_gen_ppn)
127      resp.bits.perm(0) := entries(0).perm
128    } else {
129      resp.bits.ppn(0) := ParallelMux(hitVecReg zip entries.map(_.genPPN(saveLevel, req.valid)(vpn_gen_ppn)))
130      resp.bits.perm(0) := ParallelMux(hitVecReg zip entries.map(_.perm))
131    }
132
133    access.sets := get_set_idx(vpn_reg(vpn_reg.getWidth - 1, sectortlbwidth), nSets) // no use
134    access.touch_ways.valid := resp.valid && Cat(hitVecReg).orR
135    access.touch_ways.bits := OHToUInt(hitVecReg)
136
137    resp.bits.hit.suggestName("hit")
138    resp.bits.ppn.suggestName("ppn")
139    resp.bits.perm.suggestName("perm")
140  }
141
142  when (io.w.valid) {
143    v(io.w.bits.wayIdx) := true.B
144    entries(io.w.bits.wayIdx).apply(io.w.bits.data, io.csr.satp.asid, io.w.bits.data_replenish)
145  }
146  // write assert, should not duplicate with the existing entries
147  val w_hit_vec = VecInit(entries.zip(v).map{case (e, vi) => e.wbhit(io.w.bits.data, io.csr.satp.asid) && vi })
148  XSError(io.w.valid && Cat(w_hit_vec).orR, s"${parentName} refill, duplicate with existing entries")
149
150  val refill_vpn_reg = RegNext(io.w.bits.data.entry.tag)
151  val refill_wayIdx_reg = RegNext(io.w.bits.wayIdx)
152  when (RegNext(io.w.valid)) {
153    io.access.map { access =>
154      access.sets := get_set_idx(refill_vpn_reg, nSets)
155      access.touch_ways.valid := true.B
156      access.touch_ways.bits := refill_wayIdx_reg
157    }
158  }
159
160  val sfence = io.sfence
161  val sfence_vpn = sfence.bits.addr.asTypeOf(new VaBundle().cloneType).vpn
162  val sfenceHit = entries.map(_.hit(sfence_vpn, sfence.bits.asid))
163  val sfenceHit_noasid = entries.map(_.hit(sfence_vpn, sfence.bits.asid, ignoreAsid = true))
164  // Sfence will flush all sectors of an entry when hit
165  when (io.sfence.valid) {
166    when (sfence.bits.rs1) { // virtual address *.rs1 <- (rs1===0.U)
167      when (sfence.bits.rs2) { // asid, but i do not want to support asid, *.rs2 <- (rs2===0.U)
168        // all addr and all asid
169        v.map(_ := false.B)
170      }.otherwise {
171        // all addr but specific asid
172        v.zipWithIndex.map{ case (a,i) => a := a & (g(i) | !(entries(i).asid === sfence.bits.asid)) }
173      }
174    }.otherwise {
175      when (sfence.bits.rs2) {
176        // specific addr but all asid
177        v.zipWithIndex.map{ case (a,i) => a := a & !sfenceHit_noasid(i) }
178      }.otherwise {
179        // specific addr and specific asid
180        v.zipWithIndex.map{ case (a,i) => a := a & !(sfenceHit(i) && !g(i)) }
181      }
182    }
183  }
184
185  val victim_idx = io.w.bits.wayIdx
186  io.victim.out.valid := v(victim_idx) && io.w.valid && entries(victim_idx).is_normalentry()
187  io.victim.out.bits.entry := ns_to_n(entries(victim_idx))
188
189  def ns_to_n(ns: TlbEntry): TlbEntry = {
190    val n = Wire(new TlbEntry(pageNormal = true, pageSuper = false))
191    n.perm := ns.perm
192    n.ppn := ns.ppn
193    n.tag := ns.tag
194    n.asid := ns.asid
195    n.valididx := ns.valididx
196    n.ppn_low := ns.ppn_low
197    n
198  }
199
200  XSPerfAccumulate(s"access", io.r.resp.map(_.valid.asUInt()).fold(0.U)(_ + _))
201  XSPerfAccumulate(s"hit", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt() + _.asUInt()))
202
203  for (i <- 0 until nWays) {
204    XSPerfAccumulate(s"access${i}", io.r.resp.zip(io.access.map(acc => UIntToOH(acc.touch_ways.bits))).map{ case (a, b) =>
205      a.valid && a.bits.hit && b(i)}.fold(0.U)(_.asUInt() + _.asUInt()))
206  }
207  for (i <- 0 until nWays) {
208    XSPerfAccumulate(s"refill${i}", io.w.valid && io.w.bits.wayIdx === i.U)
209  }
210
211  val perfEvents = Seq(
212    ("tlbstore_access", io.r.resp.map(_.valid.asUInt()).fold(0.U)(_ + _)                            ),
213    ("tlbstore_hit   ", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt() + _.asUInt())),
214  )
215  generatePerfEvent()
216
217  println(s"${parentName} tlb_fa: nSets${nSets} nWays:${nWays}")
218}
219
220@chiselName
221class TLBSA(
222  parentName: String,
223  ports: Int,
224  nDups: Int,
225  nSets: Int,
226  nWays: Int,
227  normalPage: Boolean,
228  superPage: Boolean
229)(implicit p: Parameters) extends TlbModule {
230  require(!superPage, "super page should use reg/fa")
231  require(nWays == 1, "nWays larger than 1 causes bad timing")
232
233  // timing optimization to divide v select into two cycles.
234  val VPRE_SELECT = min(8, nSets)
235  val VPOST_SELECT = nSets / VPRE_SELECT
236  val nBanks = 8
237
238  val io = IO(new TlbStorageIO(nSets, nWays, ports, nDups))
239
240  io.r.req.map(_.ready :=  true.B)
241  val v = RegInit(VecInit(Seq.fill(nSets)(VecInit(Seq.fill(nWays)(false.B)))))
242  val entries = Module(new BankedAsyncDataModuleTemplateWithDup(new TlbEntry(normalPage, superPage), nSets, ports, nDups, nBanks))
243
244  for (i <- 0 until ports) { // duplicate sram
245    val req = io.r.req(i)
246    val resp = io.r.resp(i)
247    val access = io.access(i)
248
249    val vpn = req.bits.vpn
250    val vpn_reg = RegEnable(vpn, req.fire())
251
252    val ridx = get_set_idx(vpn(vpn.getWidth - 1, sectortlbwidth), nSets)
253    val v_resize = v.asTypeOf(Vec(VPRE_SELECT, Vec(VPOST_SELECT, UInt(nWays.W))))
254    val vidx_resize = RegNext(v_resize(get_set_idx(drop_set_idx(vpn(vpn.getWidth - 1, sectortlbwidth), VPOST_SELECT), VPRE_SELECT)))
255    val vidx = vidx_resize(get_set_idx(vpn_reg(vpn_reg.getWidth - 1, sectortlbwidth), VPOST_SELECT)).asBools.map(_ && RegNext(req.fire()))
256    val vidx_bypass = RegNext((entries.io.waddr === ridx) && entries.io.wen)
257    entries.io.raddr(i) := ridx
258
259    val data = entries.io.rdata(i)
260    val hit = data(0).hit(vpn_reg, io.csr.satp.asid, nSets) && (vidx(0) || vidx_bypass)
261    resp.bits.hit := hit
262    for (d <- 0 until nDups) {
263      resp.bits.ppn(d) := data(d).genPPN()(vpn_reg)
264      resp.bits.perm(d) := data(d).perm
265    }
266
267    resp.valid := { RegNext(req.valid) }
268    resp.bits.hit.suggestName("hit")
269    resp.bits.ppn.suggestName("ppn")
270    resp.bits.perm.suggestName("perm")
271
272    access.sets := get_set_idx(vpn_reg(vpn_reg.getWidth - 1, sectortlbwidth), nSets) // no use
273    access.touch_ways.valid := resp.valid && hit
274    access.touch_ways.bits := 1.U // TODO: set-assoc need no replacer when nset is 1
275  }
276
277  // W ports should be 1, or, check at above will be wrong.
278  entries.io.wen := io.w.valid || io.victim.in.valid
279  entries.io.waddr := Mux(io.w.valid,
280    get_set_idx(io.w.bits.data.entry.tag, nSets),
281    get_set_idx(io.victim.in.bits.entry.tag, nSets))
282  entries.io.wdata := Mux(io.w.valid,
283    (Wire(new TlbEntry(normalPage, superPage)).apply(io.w.bits.data, io.csr.satp.asid, io.w.bits.data_replenish)),
284    io.victim.in.bits.entry)
285
286  when (io.victim.in.valid) {
287    v(get_set_idx(io.victim.in.bits.entry.tag, nSets))(io.w.bits.wayIdx) := true.B
288  }
289  // w has higher priority than victim
290  when (io.w.valid) {
291    v(get_set_idx(io.w.bits.data.entry.tag, nSets))(io.w.bits.wayIdx) := true.B
292  }
293
294  val refill_vpn_reg = RegNext(Mux(io.victim.in.valid, io.victim.in.bits.entry.tag, io.w.bits.data.entry.tag))
295  val refill_wayIdx_reg = RegNext(io.w.bits.wayIdx)
296  when (RegNext(io.w.valid || io.victim.in.valid)) {
297    io.access.map { access =>
298      access.sets := get_set_idx(refill_vpn_reg, nSets)
299      access.touch_ways.valid := true.B
300      access.touch_ways.bits := refill_wayIdx_reg
301    }
302  }
303
304  val sfence = io.sfence
305  val sfence_vpn = sfence.bits.addr.asTypeOf(new VaBundle().cloneType).vpn
306  // Sfence will flush all sectors of an entry when hit
307  when (io.sfence.valid) {
308    when (sfence.bits.rs1) { // virtual address *.rs1 <- (rs1===0.U)
309        v.map(a => a.map(b => b := false.B))
310    }.otherwise {
311        // specific addr but all asid
312        v(get_set_idx(sfence_vpn(sfence_vpn.getWidth - 1, sectortlbwidth), nSets)).map(_ := false.B)
313    }
314  }
315
316  io.victim.out := DontCare
317  io.victim.out.valid := false.B
318
319  XSPerfAccumulate(s"access", io.r.req.map(_.valid.asUInt()).fold(0.U)(_ + _))
320  XSPerfAccumulate(s"hit", io.r.resp.map(a => a.valid && a.bits.hit).fold(0.U)(_.asUInt() + _.asUInt()))
321
322  for (i <- 0 until nSets) {
323    XSPerfAccumulate(s"refill${i}", (io.w.valid || io.victim.in.valid) &&
324        (Mux(io.w.valid, get_set_idx(io.w.bits.data.entry.tag, nSets), get_set_idx(io.victim.in.bits.entry.tag, nSets)) === i.U)
325      )
326  }
327
328  for (i <- 0 until nSets) {
329    XSPerfAccumulate(s"hit${i}", io.r.resp.map(a => a.valid & a.bits.hit)
330      .zip(io.r.req.map(a => RegNext(get_set_idx(a.bits.vpn(a.bits.vpn.getWidth - 1, sectortlbwidth), nSets)) === i.U))
331      .map{a => (a._1 && a._2).asUInt()}
332      .fold(0.U)(_ + _)
333    )
334  }
335
336  for (i <- 0 until nSets) {
337    XSPerfAccumulate(s"access${i}", io.r.resp.map(_.valid)
338      .zip(io.r.req.map(a => RegNext(get_set_idx(a.bits.vpn(a.bits.vpn.getWidth - 1, sectortlbwidth), nSets)) === i.U))
339      .map{a => (a._1 && a._2).asUInt()}
340      .fold(0.U)(_ + _)
341    )
342  }
343
344  println(s"${parentName} tlb_sa: nSets:${nSets} nWays:${nWays}")
345}
346
347@chiselName
348class TLBFakeSP(
349             ports: Int,
350             nSets: Int,
351             nWays: Int,
352             useDmode: Boolean = false
353           )(implicit p: Parameters) extends TlbModule with HasCSRConst{
354
355  val io = IO(new TlbStorageIO(nSets, nWays, ports))
356  io.r.req.map(_.ready := true.B)
357  val mode = if (useDmode) io.csr.priv.dmode else io.csr.priv.imode
358  val vmEnable = if (EnbaleTlbDebug) (io.csr.satp.mode === 8.U)
359    else (io.csr.satp.mode === 8.U && (mode < ModeM))
360
361  for (i <- 0 until ports) {
362    val req = io.r.req(i)
363    val resp = io.r.resp(i)
364
365    val helper = Module(new PTEHelper())
366    helper.clock := clock
367    helper.satp := io.csr.satp.ppn
368    helper.enable := req.fire && vmEnable
369    helper.vpn := req.bits.vpn
370
371    val pte = helper.pte.asTypeOf(new PteBundle)
372    val ppn = pte.ppn
373    val vpn_reg = RegNext(req.bits.vpn)
374    val pf = helper.pf
375    val level = helper.level
376
377    resp.valid := RegNext(req.valid)
378    resp.bits.hit := true.B
379    resp.bits.perm(0).pf := pf
380    resp.bits.perm(0).af := false.B
381    resp.bits.perm(0).d := pte.perm.d
382    resp.bits.perm(0).a := pte.perm.a
383    resp.bits.perm(0).g := pte.perm.g
384    resp.bits.perm(0).u := pte.perm.u
385    resp.bits.perm(0).x := pte.perm.x
386    resp.bits.perm(0).w := pte.perm.w
387    resp.bits.perm(0).r := pte.perm.r
388    resp.bits.perm(0).pm := DontCare
389
390    resp.bits.ppn(0) := MuxLookup(level, 0.U, Seq(
391      0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn_reg(vpnnLen*2-1, 0)),
392      1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn_reg(vpnnLen-1, 0)),
393      2.U -> ppn)
394    )
395  }
396
397  io.access := DontCare
398  io.victim.out := DontCare
399
400}
401
402@chiselName
403class TLBFakeNP(
404             ports: Int,
405             nDups: Int,
406             nSets: Int,
407             nWays: Int
408           )(implicit p: Parameters) extends TlbModule {
409
410  val io = IO(new TlbStorageIO(nSets, nWays, ports, nDups))
411
412  io.r.req.map(_.ready :=  true.B)
413  io.r.resp := DontCare
414  io.access := DontCare
415  io.victim.out := DontCare
416}
417
418object TlbStorage {
419  def apply
420  (
421    parentName: String,
422    associative: String,
423    ports: Int,
424    nDups: Int = 1,
425    nSets: Int,
426    nWays: Int,
427    saveLevel: Boolean = false,
428    normalPage: Boolean,
429    superPage: Boolean,
430    useDmode: Boolean,
431    SoftTLB: Boolean
432  )(implicit p: Parameters) = {
433    if (SoftTLB) {
434      if (superPage == true) {
435        val storage = Module(new TLBFakeSP(ports, nSets, nWays, useDmode))
436        storage.suggestName(s"${parentName}_fakesp")
437        storage.io
438      } else {
439        val storage = Module(new TLBFakeNP(ports, nDups, nSets, nWays))
440        storage.suggestName(s"${parentName}_fakenp")
441        storage.io
442      }
443    } else {
444      if (associative == "fa") {
445        val storage = Module(new TLBFA(parentName, ports, nSets, nWays, saveLevel, normalPage, superPage))
446        storage.suggestName(s"${parentName}_fa")
447        storage.io
448      } else {
449        val storage = Module(new TLBSA(parentName, ports, nDups, nSets, nWays, normalPage, superPage))
450        storage.suggestName(s"${parentName}_sa")
451        storage.io
452      }
453    }
454  }
455}
456
457class TlbStorageWrapper(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends TlbModule {
458  val io = IO(new TlbStorageWrapperIO(ports, q, nDups))
459
460// TODO: wrap Normal page and super page together, wrap the declare & refill dirty codes
461  val normalPage = TlbStorage(
462    parentName = q.name + "_np_storage",
463    associative = q.normalAssociative,
464    ports = ports,
465    nDups = nDups,
466    nSets = q.normalNSets,
467    nWays = q.normalNWays,
468    saveLevel = q.saveLevel,
469    normalPage = true,
470    superPage = false,
471    useDmode = q.useDmode,
472    SoftTLB = coreParams.softTLB
473  )
474  val superPage = TlbStorage(
475    parentName = q.name + "_sp_storage",
476    associative = q.superAssociative,
477    ports = ports,
478    nSets = q.superNSets,
479    nWays = q.superNWays,
480    normalPage = q.normalAsVictim,
481    superPage = true,
482    useDmode = q.useDmode,
483    SoftTLB = coreParams.softTLB
484  )
485
486  for (i <- 0 until ports) {
487    normalPage.r_req_apply(
488      valid = io.r.req(i).valid,
489      vpn = io.r.req(i).bits.vpn,
490      i = i
491    )
492    superPage.r_req_apply(
493      valid = io.r.req(i).valid,
494      vpn = io.r.req(i).bits.vpn,
495      i = i
496    )
497  }
498
499  for (i <- 0 until ports) {
500    val nq = normalPage.r.req(i)
501    val np = normalPage.r.resp(i)
502    val sq = superPage.r.req(i)
503    val sp = superPage.r.resp(i)
504    val rq = io.r.req(i)
505    val rp = io.r.resp(i)
506    rq.ready := nq.ready && sq.ready // actually, not used
507    rp.valid := np.valid && sp.valid // actually, not used
508    rp.bits.hit := np.bits.hit || sp.bits.hit
509    for (d <- 0 until nDups) {
510      rp.bits.ppn(d) := Mux(sp.bits.hit, sp.bits.ppn(0), np.bits.ppn(d))
511      rp.bits.perm(d) := Mux(sp.bits.hit, sp.bits.perm(0), np.bits.perm(d))
512    }
513    rp.bits.super_hit := sp.bits.hit
514    rp.bits.super_ppn := sp.bits.ppn(0)
515    rp.bits.spm := np.bits.perm(0).pm(RegNext(io.r.req(i).bits.vpn(sectortlbwidth - 1, 0)))
516    // Sector tlb may trigger multi-hit, see def "wbhit"
517    XSPerfAccumulate(s"port${i}_np_sp_multi_hit", !(!np.bits.hit || !sp.bits.hit || !rp.valid))
518    //assert(!np.bits.hit || !sp.bits.hit || !rp.valid, s"${q.name} storage ports${i} normal and super multi-hit")
519  }
520
521  normalPage.victim.in <> superPage.victim.out
522  normalPage.victim.out <> superPage.victim.in
523  normalPage.sfence <> io.sfence
524  superPage.sfence <> io.sfence
525  normalPage.csr <> io.csr
526  superPage.csr <> io.csr
527
528  val normal_refill_idx = if (q.outReplace) {
529    io.replace.normalPage.access <> normalPage.access
530    io.replace.normalPage.chosen_set := get_set_idx(io.w.bits.data.entry.tag, q.normalNSets)
531    io.replace.normalPage.refillIdx
532  } else if (q.normalAssociative == "fa") {
533    val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNWays)
534    re.access(normalPage.access.map(_.touch_ways)) // normalhitVecVec.zipWithIndex.map{ case (hv, i) => get_access(hv, validRegVec(i))})
535    re.way
536  } else { // set-acco && plru
537    val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNSets, q.normalNWays)
538    re.access(normalPage.access.map(_.sets), normalPage.access.map(_.touch_ways))
539    re.way(get_set_idx(io.w.bits.data.entry.tag, q.normalNSets))
540  }
541
542  val super_refill_idx = if (q.outReplace) {
543    io.replace.superPage.access <> superPage.access
544    io.replace.superPage.chosen_set := DontCare
545    io.replace.superPage.refillIdx
546  } else {
547    val re = ReplacementPolicy.fromString(q.superReplacer, q.superNWays)
548    re.access(superPage.access.map(_.touch_ways))
549    re.way
550  }
551
552  normalPage.w_apply(
553    valid = { if (q.normalAsVictim) false.B
554    else io.w.valid && io.w.bits.data.entry.level.get === 2.U },
555    wayIdx = normal_refill_idx,
556    data = io.w.bits.data,
557    data_replenish = io.w.bits.data_replenish
558  )
559  superPage.w_apply(
560    valid = { if (q.normalAsVictim) io.w.valid
561    else io.w.valid && io.w.bits.data.entry.level.get =/= 2.U },
562    wayIdx = super_refill_idx,
563    data = io.w.bits.data,
564    data_replenish = io.w.bits.data_replenish
565  )
566
567    // replacement
568  def get_access(one_hot: UInt, valid: Bool): Valid[UInt] = {
569    val res = Wire(Valid(UInt(log2Up(one_hot.getWidth).W)))
570    res.valid := Cat(one_hot).orR && valid
571    res.bits := OHToUInt(one_hot)
572    res
573  }
574}
575