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