xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/MMUConst.scala (revision 33177a7c6ea22740da90c7bdc8eed306ef2cfda3)
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 xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
24import utils._
25import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
26import freechips.rocketchip.tilelink._
27
28case class TLBParameters
29(
30  name: String = "none",
31  fetchi: Boolean = false, // TODO: remove it
32  useDmode: Boolean = true,
33  sameCycle: Boolean = false,
34  normalNSets: Int = 1, // when da or sa
35  normalNWays: Int = 8, // when fa or sa
36  superNSets: Int = 1,
37  superNWays: Int = 2,
38  normalReplacer: Option[String] = Some("random"),
39  superReplacer: Option[String] = Some("plru"),
40  normalAssociative: String = "fa", // "fa", "sa", "da", "sa" is not supported
41  superAssociative: String = "fa", // must be fa
42  normalAsVictim: Boolean = false, // when get replace from fa, store it into sram
43  outReplace: Boolean = false,
44  shouldBlock: Boolean = false // only for perf, not support for io
45)
46
47case class L2TLBParameters
48(
49  name: String = "l2tlb",
50  // l1
51  l1Size: Int = 16,
52  l1Associative: String = "fa",
53  l1Replacer: Option[String] = Some("plru"),
54  // l2
55  l2nSets: Int = 32,
56  l2nWays: Int = 2,
57  l2Replacer: Option[String] = Some("setplru"),
58  // l3
59  l3nSets: Int = 128,
60  l3nWays: Int = 4,
61  l3Replacer: Option[String] = Some("setplru"),
62  // sp
63  spSize: Int = 16,
64  spReplacer: Option[String] = Some("plru"),
65  // miss queue
66  missQueueSize: Int = 9,
67  // way size
68  blockBytes: Int = 64,
69  // ecc
70  ecc: Option[String] = Some("secded")
71)
72
73trait HasTlbConst extends HasXSParameter {
74  val Level = 3
75
76  val offLen  = 12
77  val ppnLen  = PAddrBits - offLen
78  val vpnnLen = 9
79  val vpnLen  = VAddrBits - offLen
80  val flagLen = 8
81  val pteResLen = XLEN - ppnLen - 2 - flagLen
82  val asidLen = 16
83
84  val sramSinglePort = true
85
86  val timeOutThreshold = 2000
87
88  def get_idx(vpn: UInt, nSets: Int): UInt = {
89    vpn(log2Up(nSets)-1, 0)
90  }
91
92  def replaceWrapper(v: UInt, lruIdx: UInt): UInt = {
93    val width = v.getWidth
94    val emptyIdx = ParallelPriorityMux((0 until width).map( i => (!v(i), i.U)))
95    val full = Cat(v).andR
96    Mux(full, lruIdx, emptyIdx)
97  }
98
99  def replaceWrapper(v: Seq[Bool], lruIdx: UInt): UInt = {
100    replaceWrapper(VecInit(v).asUInt, lruIdx)
101  }
102
103}
104
105trait HasPtwConst extends HasTlbConst with MemoryOpConstants{
106  val PtwWidth = 2
107  val blockBits = l2tlbParams.blockBytes * 8
108
109  val bPtwWidth = log2Up(PtwWidth)
110
111  // ptwl1: fully-associated
112  val PtwL1TagLen = vpnnLen
113
114  /* +-------+----------+-------------+
115   * |  Tag  |  SetIdx  |  SectorIdx  |
116   * +-------+----------+-------------+
117   */
118  // ptwl2: 8-way group-associated
119  val l2tlbParams.l2nWays = l2tlbParams.l2nWays
120  val PtwL2SetNum = l2tlbParams.l2nSets
121  val PtwL2SectorSize = blockBits /XLEN
122  val PtwL2IdxLen = log2Up(PtwL2SetNum * PtwL2SectorSize)
123  val PtwL2SectorIdxLen = log2Up(PtwL2SectorSize)
124  val PtwL2SetIdxLen = log2Up(PtwL2SetNum)
125  val PtwL2TagLen = vpnnLen * 2 - PtwL2IdxLen
126
127  // ptwl3: 16-way group-associated
128  val l2tlbParams.l3nWays = l2tlbParams.l3nWays
129  val PtwL3SetNum = l2tlbParams.l3nSets
130  val PtwL3SectorSize =  blockBits / XLEN
131  val PtwL3IdxLen = log2Up(PtwL3SetNum * PtwL3SectorSize)
132  val PtwL3SectorIdxLen = log2Up(PtwL3SectorSize)
133  val PtwL3SetIdxLen = log2Up(PtwL3SetNum)
134  val PtwL3TagLen = vpnnLen * 3 - PtwL3IdxLen
135
136  // super page, including 1GB and 2MB page
137  val SPTagLen = vpnnLen * 2
138
139  val MSHRSize = l2tlbParams.missQueueSize
140  val MemReqWidth = MSHRSize + 1
141  val bMemID = log2Up(MSHRSize + 1)
142
143  def genPtwL2Idx(vpn: UInt) = {
144    (vpn(vpnLen - 1, vpnnLen))(PtwL2IdxLen - 1, 0)
145  }
146
147  def genPtwL2SectorIdx(vpn: UInt) = {
148    genPtwL2Idx(vpn)(PtwL2SectorIdxLen - 1, 0)
149  }
150
151  def genPtwL2SetIdx(vpn: UInt) = {
152    genPtwL2Idx(vpn)(PtwL2SetIdxLen + PtwL2SectorIdxLen - 1, PtwL2SectorIdxLen)
153  }
154
155  def genPtwL3Idx(vpn: UInt) = {
156    vpn(PtwL3IdxLen - 1, 0)
157  }
158
159  def genPtwL3SectorIdx(vpn: UInt) = {
160    genPtwL3Idx(vpn)(PtwL3SectorIdxLen - 1, 0)
161  }
162
163  def dropL3SectorBits(vpn: UInt) = {
164    vpn(vpn.getWidth-1, PtwL3SectorIdxLen)
165  }
166
167  def genPtwL3SetIdx(vpn: UInt) = {
168    genPtwL3Idx(vpn)(PtwL3SetIdxLen + PtwL3SectorIdxLen - 1, PtwL3SectorIdxLen)
169  }
170
171  def MakeAddr(ppn: UInt, off: UInt) = {
172    require(off.getWidth == 9)
173    Cat(ppn, off, 0.U(log2Up(XLEN/8).W))(PAddrBits-1, 0)
174  }
175
176  def getVpnn(vpn: UInt, idx: Int): UInt = {
177    vpn(vpnnLen*(idx+1)-1, vpnnLen*idx)
178  }
179
180  def getVpnClip(vpn: UInt, level: Int) = {
181    // level 0  /* vpnn2 */
182    // level 1  /* vpnn2 * vpnn1 */
183    // level 2  /* vpnn2 * vpnn1 * vpnn0*/
184    vpn(vpnLen - 1, (2 - level) * vpnnLen)
185  }
186
187  def printVec[T <: Data](x: Seq[T]): Printable = {
188    (0 until x.length).map(i => p"(${i.U})${x(i)} ").reduce(_+_)
189  }
190
191}
192