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 = 8, 56 l2nWays: Int = 4, 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) 70 71trait HasTlbConst extends HasXSParameter { 72 val Level = 3 73 74 val offLen = 12 75 val ppnLen = PAddrBits - offLen 76 val vpnnLen = 9 77 val vpnLen = VAddrBits - offLen 78 val flagLen = 8 79 val pteResLen = XLEN - ppnLen - 2 - flagLen 80 val asidLen = 16 81 82 val sramSinglePort = true 83 84 def get_idx(vpn: UInt, nSets: Int): UInt = { 85 vpn(log2Up(nSets)-1, 0) 86 } 87 88 def replaceWrapper(v: UInt, lruIdx: UInt): UInt = { 89 val width = v.getWidth 90 val emptyIdx = ParallelPriorityMux((0 until width).map( i => (!v(i), i.U))) 91 val full = Cat(v).andR 92 Mux(full, lruIdx, emptyIdx) 93 } 94 95 def replaceWrapper(v: Seq[Bool], lruIdx: UInt): UInt = { 96 replaceWrapper(VecInit(v).asUInt, lruIdx) 97 } 98 99} 100 101trait HasPtwConst extends HasTlbConst with MemoryOpConstants{ 102 val PtwWidth = 2 103 val blockBits = l2tlbParams.blockBytes * 8 104 105 val bPtwWidth = log2Up(PtwWidth) 106 107 // ptwl1: fully-associated 108 val PtwL1TagLen = vpnnLen 109 110 /* +-------+----------+-------------+ 111 * | Tag | SetIdx | SectorIdx | 112 * +-------+----------+-------------+ 113 */ 114 // ptwl2: 8-way group-associated 115 val l2tlbParams.l2nWays = l2tlbParams.l2nWays 116 val PtwL2SetNum = l2tlbParams.l2nSets 117 val PtwL2SectorSize = blockBits /XLEN 118 val PtwL2IdxLen = log2Up(PtwL2SetNum * PtwL2SectorSize) 119 val PtwL2SectorIdxLen = log2Up(PtwL2SectorSize) 120 val PtwL2SetIdxLen = log2Up(PtwL2SetNum) 121 val PtwL2TagLen = vpnnLen * 2 - PtwL2IdxLen 122 123 // ptwl3: 16-way group-associated 124 val l2tlbParams.l3nWays = l2tlbParams.l3nWays 125 val PtwL3SetNum = l2tlbParams.l3nSets 126 val PtwL3SectorSize = blockBits / XLEN 127 val PtwL3IdxLen = log2Up(PtwL3SetNum * PtwL3SectorSize) 128 val PtwL3SectorIdxLen = log2Up(PtwL3SectorSize) 129 val PtwL3SetIdxLen = log2Up(PtwL3SetNum) 130 val PtwL3TagLen = vpnnLen * 3 - PtwL3IdxLen 131 132 // super page, including 1GB and 2MB page 133 val SPTagLen = vpnnLen * 2 134 135 val MSHRSize = l2tlbParams.missQueueSize 136 val MemReqWidth = MSHRSize + 1 137 val bMemID = log2Up(MSHRSize + 1) 138 139 def genPtwL2Idx(vpn: UInt) = { 140 (vpn(vpnLen - 1, vpnnLen))(PtwL2IdxLen - 1, 0) 141 } 142 143 def genPtwL2SectorIdx(vpn: UInt) = { 144 genPtwL2Idx(vpn)(PtwL2SectorIdxLen - 1, 0) 145 } 146 147 def genPtwL2SetIdx(vpn: UInt) = { 148 genPtwL2Idx(vpn)(PtwL2SetIdxLen + PtwL2SectorIdxLen - 1, PtwL2SectorIdxLen) 149 } 150 151 def genPtwL3Idx(vpn: UInt) = { 152 vpn(PtwL3IdxLen - 1, 0) 153 } 154 155 def genPtwL3SectorIdx(vpn: UInt) = { 156 genPtwL3Idx(vpn)(PtwL3SectorIdxLen - 1, 0) 157 } 158 159 def dropL3SectorBits(vpn: UInt) = { 160 vpn(vpn.getWidth-1, PtwL3SectorIdxLen) 161 } 162 163 def genPtwL3SetIdx(vpn: UInt) = { 164 genPtwL3Idx(vpn)(PtwL3SetIdxLen + PtwL3SectorIdxLen - 1, PtwL3SectorIdxLen) 165 } 166 167 def MakeAddr(ppn: UInt, off: UInt) = { 168 require(off.getWidth == 9) 169 Cat(ppn, off, 0.U(log2Up(XLEN/8).W))(PAddrBits-1, 0) 170 } 171 172 def getVpnn(vpn: UInt, idx: Int): UInt = { 173 vpn(vpnnLen*(idx+1)-1, vpnnLen*idx) 174 } 175 176 def getVpnClip(vpn: UInt, level: Int) = { 177 // level 0 /* vpnn2 */ 178 // level 1 /* vpnn2 * vpnn1 */ 179 // level 2 /* vpnn2 * vpnn1 * vpnn0*/ 180 vpn(vpnLen - 1, (2 - level) * vpnnLen) 181 } 182 183 def printVec[T <: Data](x: Seq[T]): Printable = { 184 (0 until x.length).map(i => p"(${i.U})${x(i)} ").reduce(_+_) 185 } 186 187} 188