xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/TLB.scala (revision 066ac8a465b27b54ba22458ff1a67bcd28215d73)
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.internal.naming.chiselName
22import chisel3.util._
23import freechips.rocketchip.util.SRAMAnnotation
24import xiangshan._
25import utils._
26import xiangshan.backend.fu.{PMPChecker, PMPReqBundle}
27import xiangshan.backend.rob.RobPtr
28import xiangshan.backend.fu.util.HasCSRConst
29
30
31@chiselName
32class TLB(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule with HasCSRConst {
33  val io = IO(new TlbIO(Width, q))
34
35  require(q.superAssociative == "fa")
36  if (q.sameCycle || q.missSameCycle) {
37    require(q.normalAssociative == "fa")
38  }
39
40  val req = io.requestor.map(_.req)
41  val resp = io.requestor.map(_.resp)
42  val ptw = io.ptw
43  val pmp = io.pmp
44
45  val sfence = io.sfence
46  val csr = io.csr
47  val satp = csr.satp
48  val priv = csr.priv
49  val ifecth = if (q.fetchi) true.B else false.B
50  val mode = if (q.useDmode) priv.dmode else priv.imode
51  // val vmEnable = satp.mode === 8.U // && (mode < ModeM) // FIXME: fix me when boot xv6/linux...
52  val vmEnable = if (EnbaleTlbDebug) (satp.mode === 8.U)
53  else (satp.mode === 8.U && (mode < ModeM))
54
55  val reqAddr = req.map(_.bits.vaddr.asTypeOf((new VaBundle).cloneType))
56  val vpn = reqAddr.map(_.vpn)
57  val cmd = req.map(_.bits.cmd)
58  val valid = req.map(_.valid)
59
60  def widthMapSeq[T <: Seq[Data]](f: Int => T) = (0 until Width).map(f)
61
62  def widthMap[T <: Data](f: Int => T) = (0 until Width).map(f)
63
64  // Normal page && Super page
65  val normalPage = TlbStorage(
66    name = "normal",
67    associative = q.normalAssociative,
68    sameCycle = q.sameCycle,
69    ports = Width,
70    nSets = q.normalNSets,
71    nWays = q.normalNWays,
72    sramSinglePort = sramSinglePort,
73    saveLevel = q.saveLevel,
74    normalPage = true,
75    superPage = false
76  )
77  val superPage = TlbStorage(
78    name = "super",
79    associative = q.superAssociative,
80    sameCycle = q.sameCycle,
81    ports = Width,
82    nSets = q.superNSets,
83    nWays = q.superNWays,
84    sramSinglePort = sramSinglePort,
85    saveLevel = q.saveLevel,
86    normalPage = q.normalAsVictim,
87    superPage = true,
88  )
89
90
91  for (i <- 0 until Width) {
92    normalPage.r_req_apply(
93      valid = io.requestor(i).req.valid,
94      vpn = vpn(i),
95      asid = csr.satp.asid,
96      i = i
97    )
98    superPage.r_req_apply(
99      valid = io.requestor(i).req.valid,
100      vpn = vpn(i),
101      asid = csr.satp.asid,
102      i = i
103    )
104  }
105
106  normalPage.victim.in <> superPage.victim.out
107  normalPage.victim.out <> superPage.victim.in
108  normalPage.sfence <> io.sfence
109  superPage.sfence <> io.sfence
110  normalPage.csr <> io.csr
111  superPage.csr <> io.csr
112
113  def TLBNormalRead(i: Int) = {
114    val (n_hit_sameCycle, normal_hit, normal_ppn, normal_perm) = normalPage.r_resp_apply(i)
115    val (s_hit_sameCycle, super_hit, super_ppn, super_perm) = superPage.r_resp_apply(i)
116    assert(!(normal_hit && super_hit && vmEnable && RegNext(req(i).valid, init = false.B)))
117
118    val hit = normal_hit || super_hit
119    val hit_sameCycle = n_hit_sameCycle || s_hit_sameCycle
120    val ppn = Mux(normal_hit, normal_ppn, super_ppn)
121    val perm = Mux(normal_hit, normal_perm, super_perm)
122
123    val pf = perm.pf
124    val af = perm.af
125    val cmdReg = if (!q.sameCycle) RegNext(cmd(i)) else cmd(i)
126    val validReg = if (!q.sameCycle) RegNext(valid(i)) else valid(i)
127    val offReg = if (!q.sameCycle) RegNext(reqAddr(i).off) else reqAddr(i).off
128    val sizeReg = if (!q.sameCycle) RegNext(req(i).bits.size) else req(i).bits.size
129
130    /** *************** next cycle when two cycle is false******************* */
131    val miss = !hit && vmEnable
132    val miss_sameCycle = !hit_sameCycle && vmEnable
133    hit.suggestName(s"hit_${i}")
134    miss.suggestName(s"miss_${i}")
135
136    XSDebug(validReg, p"(${i.U}) hit:${hit} miss:${miss} ppn:${Hexadecimal(ppn)} perm:${perm}\n")
137
138    val paddr = Cat(ppn, offReg)
139    val vaddr = SignExt(req(i).bits.vaddr, PAddrBits)
140
141    req(i).ready := resp(i).ready
142    resp(i).valid := validReg
143    resp(i).bits.paddr := Mux(vmEnable, paddr, if (!q.sameCycle) RegNext(vaddr) else vaddr)
144    resp(i).bits.miss := { if (q.missSameCycle) miss_sameCycle else miss }
145    resp(i).bits.ptwBack := io.ptw.resp.fire()
146
147    pmp(i).valid := resp(i).valid
148    pmp(i).bits.addr := resp(i).bits.paddr
149    pmp(i).bits.size := sizeReg
150    pmp(i).bits.cmd := cmdReg
151
152    val ldUpdate = !perm.a && TlbCmd.isRead(cmdReg) && !TlbCmd.isAmo(cmdReg) // update A/D through exception
153    val stUpdate = (!perm.a || !perm.d) && (TlbCmd.isWrite(cmdReg) || TlbCmd.isAmo(cmdReg)) // update A/D through exception
154    val instrUpdate = !perm.a && TlbCmd.isExec(cmdReg) // update A/D through exception
155    val modeCheck = !(mode === ModeU && !perm.u || mode === ModeS && perm.u && (!priv.sum || ifecth))
156    val ldPermFail = !(modeCheck && (perm.r || priv.mxr && perm.x))
157    val stPermFail = !(modeCheck && perm.w)
158    val instrPermFail = !(modeCheck && perm.x)
159    val ldPf = (ldPermFail || pf) && (TlbCmd.isRead(cmdReg) && !TlbCmd.isAmo(cmdReg))
160    val stPf = (stPermFail || pf) && (TlbCmd.isWrite(cmdReg) || TlbCmd.isAmo(cmdReg))
161    val fault_valid = vmEnable
162    val instrPf = (instrPermFail || pf) && TlbCmd.isExec(cmdReg)
163    resp(i).bits.excp.pf.ld := (ldPf || ldUpdate) && fault_valid && !af
164    resp(i).bits.excp.pf.st := (stPf || stUpdate) && fault_valid && !af
165    resp(i).bits.excp.pf.instr := (instrPf || instrUpdate) && fault_valid && !af
166    // NOTE: pf need && with !af, page fault has higher priority than access fault
167    // but ptw may also have access fault, then af happens, the translation is wrong.
168    // In this case, pf has lower priority than af
169
170    resp(i).bits.excp.af.ld := af && TlbCmd.isRead(cmdReg) && fault_valid
171    resp(i).bits.excp.af.st := af && TlbCmd.isWrite(cmdReg) && fault_valid
172    resp(i).bits.excp.af.instr := af && TlbCmd.isExec(cmdReg) && fault_valid
173
174    (hit, miss, validReg)
175  }
176
177  val readResult = (0 until Width).map(TLBNormalRead(_))
178  val hitVec = readResult.map(_._1)
179  val missVec = readResult.map(_._2)
180  val validRegVec = readResult.map(_._3)
181
182  // replacement
183  def get_access(one_hot: UInt, valid: Bool): Valid[UInt] = {
184    val res = Wire(Valid(UInt(log2Up(one_hot.getWidth).W)))
185    res.valid := Cat(one_hot).orR && valid
186    res.bits := OHToUInt(one_hot)
187    res
188  }
189
190  val normal_refill_idx = if (q.outReplace) {
191    io.replace.normalPage.access <> normalPage.access
192    io.replace.normalPage.chosen_set := get_set_idx(io.ptw.resp.bits.entry.tag, q.normalNSets)
193    io.replace.normalPage.refillIdx
194  } else if (q.normalAssociative == "fa") {
195    val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNWays)
196    re.access(normalPage.access.map(_.touch_ways)) // normalhitVecVec.zipWithIndex.map{ case (hv, i) => get_access(hv, validRegVec(i))})
197    re.way
198  } else { // set-acco && plru
199    val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNSets, q.normalNWays)
200    re.access(normalPage.access.map(_.sets), normalPage.access.map(_.touch_ways))
201    re.way(get_set_idx(io.ptw.resp.bits.entry.tag, q.normalNSets))
202  }
203
204  val super_refill_idx = if (q.outReplace) {
205    io.replace.superPage.access <> superPage.access
206    io.replace.superPage.chosen_set := DontCare
207    io.replace.superPage.refillIdx
208  } else {
209    val re = ReplacementPolicy.fromString(q.superReplacer, q.superNWays)
210    re.access(superPage.access.map(_.touch_ways))
211    re.way
212  }
213
214  val refill = ptw.resp.fire() && !sfence.valid && !satp.changed
215  normalPage.w_apply(
216    valid = { if (q.normalAsVictim) false.B
217    else refill && ptw.resp.bits.entry.level.get === 2.U },
218    wayIdx = normal_refill_idx,
219    data = ptw.resp.bits
220  )
221  superPage.w_apply(
222    valid = { if (q.normalAsVictim) refill
223    else refill && ptw.resp.bits.entry.level.get =/= 2.U },
224    wayIdx = super_refill_idx,
225    data = ptw.resp.bits
226  )
227
228  for (i <- 0 until Width) {
229    io.ptw.req(i).valid := validRegVec(i) && missVec(i) && !RegNext(refill)
230    io.ptw.req(i).bits.vpn := RegNext(reqAddr(i).vpn)
231  }
232  io.ptw.resp.ready := true.B
233
234  if (!q.shouldBlock) {
235    for (i <- 0 until Width) {
236      XSPerfAccumulate("first_access" + Integer.toString(i, 10), validRegVec(i) && vmEnable && RegNext(req(i).bits.debug.isFirstIssue))
237      XSPerfAccumulate("access" + Integer.toString(i, 10), validRegVec(i) && vmEnable)
238    }
239    for (i <- 0 until Width) {
240      XSPerfAccumulate("first_miss" + Integer.toString(i, 10), validRegVec(i) && vmEnable && missVec(i) && RegNext(req(i).bits.debug.isFirstIssue))
241      XSPerfAccumulate("miss" + Integer.toString(i, 10), validRegVec(i) && vmEnable && missVec(i))
242    }
243  } else {
244    // NOTE: ITLB is blocked, so every resp will be valid only when hit
245    // every req will be ready only when hit
246    for (i <- 0 until Width) {
247      XSPerfAccumulate(s"access${i}", io.requestor(i).req.fire() && vmEnable)
248      XSPerfAccumulate(s"miss${i}", ptw.req(i).fire())
249    }
250
251  }
252  //val reqCycleCnt = Reg(UInt(16.W))
253  //reqCycleCnt := reqCycleCnt + BoolStopWatch(ptw.req(0).fire(), ptw.resp.fire || sfence.valid)
254  //XSPerfAccumulate("ptw_req_count", ptw.req.fire())
255  //XSPerfAccumulate("ptw_req_cycle", Mux(ptw.resp.fire(), reqCycleCnt, 0.U))
256  XSPerfAccumulate("ptw_resp_count", ptw.resp.fire())
257  XSPerfAccumulate("ptw_resp_pf_count", ptw.resp.fire() && ptw.resp.bits.pf)
258
259  // Log
260  for(i <- 0 until Width) {
261    XSDebug(req(i).valid, p"req(${i.U}): (${req(i).valid} ${req(i).ready}) ${req(i).bits}\n")
262    XSDebug(resp(i).valid, p"resp(${i.U}): (${resp(i).valid} ${resp(i).ready}) ${resp(i).bits}\n")
263  }
264
265  XSDebug(sfence.valid, p"Sfence: ${sfence}\n")
266  XSDebug(ParallelOR(valid)|| ptw.resp.valid, p"CSR: ${csr}\n")
267  XSDebug(ParallelOR(valid) || ptw.resp.valid, p"vmEnable:${vmEnable} hit:${Binary(VecInit(hitVec).asUInt)} miss:${Binary(VecInit(missVec).asUInt)}\n")
268  for (i <- ptw.req.indices) {
269    XSDebug(ptw.req(i).fire(), p"PTW req:${ptw.req(i).bits}\n")
270  }
271  XSDebug(ptw.resp.valid, p"PTW resp:${ptw.resp.bits} (v:${ptw.resp.valid}r:${ptw.resp.ready}) \n")
272
273  println(s"${q.name}: normal page: ${q.normalNWays} ${q.normalAssociative} ${q.normalReplacer.get} super page: ${q.superNWays} ${q.superAssociative} ${q.superReplacer.get}")
274
275//   // NOTE: just for simple tlb debug, comment it after tlb's debug
276  // assert(!io.ptw.resp.valid || io.ptw.resp.bits.entry.tag === io.ptw.resp.bits.entry.ppn, "Simple tlb debug requires vpn === ppn")
277  val perfinfo = IO(new Bundle(){
278    val perfEvents = Output(new PerfEventsBundle(2))
279  })
280    if(!q.shouldBlock) {
281      val perfEvents = Seq(
282        ("access         ", PopCount((0 until Width).map(i => vmEnable && validRegVec(i)))                                         ),
283        ("miss           ", PopCount((0 until Width).map(i => vmEnable && validRegVec(i) && missVec(i)))                           ),
284        )
285      for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) {
286        perf_out.incr_step := RegNext(perf)
287      }
288    } else {
289      val perfEvents = Seq(
290        ("access         ", PopCount((0 until Width).map(i => io.requestor(i).req.fire()))                           ),
291        ("miss           ", PopCount((0 until Width).map(i => ptw.req(i).fire()))                                    ),
292      )
293      for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) {
294        perf_out.incr_step := RegNext(perf)
295      }
296    }
297}
298
299class TlbReplace(Width: Int, q: TLBParameters)(implicit p: Parameters) extends TlbModule {
300  val io = IO(new TlbReplaceIO(Width, q))
301
302  if (q.normalAssociative == "fa") {
303    val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNWays)
304    re.access(io.normalPage.access.map(_.touch_ways))
305    io.normalPage.refillIdx := re.way
306  } else { // set-acco && plru
307    val re = ReplacementPolicy.fromString(q.normalReplacer, q.normalNSets, q.normalNWays)
308    re.access(io.normalPage.access.map(_.sets), io.normalPage.access.map(_.touch_ways))
309    io.normalPage.refillIdx := { if (q.normalNWays == 1) 0.U else re.way(io.normalPage.chosen_set) }
310  }
311
312  if (q.superAssociative == "fa") {
313    val re = ReplacementPolicy.fromString(q.superReplacer, q.superNWays)
314    re.access(io.superPage.access.map(_.touch_ways))
315    io.superPage.refillIdx := re.way
316  } else { // set-acco && plru
317    val re = ReplacementPolicy.fromString(q.superReplacer, q.superNSets, q.superNWays)
318    re.access(io.superPage.access.map(_.sets), io.superPage.access.map(_.touch_ways))
319    io.superPage.refillIdx := { if (q.superNWays == 1) 0.U else re.way(io.superPage.chosen_set) }
320  }
321}
322
323object TLB {
324  def apply
325  (
326    in: Seq[BlockTlbRequestIO],
327    sfence: SfenceBundle,
328    csr: TlbCsrBundle,
329    width: Int,
330    shouldBlock: Boolean,
331    q: TLBParameters
332  )(implicit p: Parameters) = {
333    require(in.length == width)
334
335    val tlb = Module(new TLB(width, q))
336
337    tlb.io.sfence <> sfence
338    tlb.io.csr <> csr
339    tlb.suggestName(s"tlb_${q.name}")
340
341    if (!shouldBlock) { // dtlb
342      for (i <- 0 until width) {
343        tlb.io.requestor(i) <> in(i)
344        // tlb.io.requestor(i).req.valid := in(i).req.valid
345        // tlb.io.requestor(i).req.bits := in(i).req.bits
346        // in(i).req.ready := tlb.io.requestor(i).req.ready
347
348        // in(i).resp.valid := tlb.io.requestor(i).resp.valid
349        // in(i).resp.bits := tlb.io.requestor(i).resp.bits
350        // tlb.io.requestor(i).resp.ready := in(i).resp.ready
351      }
352    } else { // itlb
353      //require(width == 1)
354      (0 until width).map{ i =>
355        tlb.io.requestor(i).req.valid := in(i).req.valid
356        tlb.io.requestor(i).req.bits := in(i).req.bits
357        in(i).req.ready := !tlb.io.requestor(i).resp.bits.miss && in(i).resp.ready && tlb.io.requestor(i).req.ready
358
359        require(q.missSameCycle || q.sameCycle)
360        // NOTE: the resp.valid seems to be useless, it must be true when need
361        //       But don't know what happens when true but not need, so keep it correct value, not just true.B
362        if (q.missSameCycle && !q.sameCycle) {
363          in(i).resp.valid := tlb.io.requestor(i).resp.valid && !RegNext(tlb.io.requestor(i).resp.bits.miss)
364        } else {
365          in(i).resp.valid := tlb.io.requestor(i).resp.valid && !tlb.io.requestor(i).resp.bits.miss
366        }
367        in(i).resp.bits := tlb.io.requestor(i).resp.bits
368        tlb.io.requestor(i).resp.ready := in(i).resp.ready
369      }
370    }
371    tlb.io.ptw
372  }
373}
374