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.backend.fu 18 19import chisel3._ 20import chisel3.util._ 21import freechips.rocketchip.regmapper.{RegField, RegFieldDesc, RegReadFn, RegWriteFn} 22import utils.{ParallelPriorityMux, ZeroExt} 23import xiangshan.cache.mmu.TlbCmd 24 25/* Memory Mapped PMA */ 26case class MMPMAConfig 27( 28 address: BigInt, 29 mask: BigInt, 30 lgMaxSize: Int, 31 sameCycle: Boolean, 32 num: Int 33) 34 35trait PMAConst extends PMPConst 36 37trait MMPMAMethod extends PMAConst with PMAMethod with PMPReadWriteMethodBare { 38 def gen_mmpma_mapping(num: Int) = { 39 val pmaCfgPerCSR = PMXLEN / new PMPConfig().getWidth 40 def pmaCfgLogicIndex(i: Int) = (PMXLEN / 32) * (i / pmaCfgPerCSR) 41 def pmaCfgIndex(i: Int) = (i / pmaCfgPerCSR) 42 43 val pma = Wire(Vec(num, new PMPEntry)) 44 45 /* pma init value */ 46 val init_value = pma_init() 47 48 val pmaCfgMerged = RegInit(init_value._1) 49 val addr = RegInit(init_value._2) 50 val mask = RegInit(init_value._3) 51 val cfg = WireInit(pmaCfgMerged).asTypeOf(Vec(num, new PMPConfig())) 52 // pmaMask are implicit regs that just used for timing optimization 53 for (i <- pma.indices) { 54 pma(i).gen(cfg(i), addr(i), mask(i)) 55 } 56 57 val blankCfg = PMXLEN == 32 58 val cfg_index_wrapper = (0 until num by 4).zip((0 until num by 4).map(a => blankCfg || (a % pmaCfgPerCSR == 0))) 59 val cfg_map = (cfg_index_wrapper).map{ case(i, notempty) => { 60// println(s"tlbpma i:$i notempty:$notempty") 61 RegField.apply(n = PMXLEN, r = RegReadFn((ivalid, oready) => 62 if (notempty) { (true.B, ivalid, pmaCfgMerged(pmaCfgIndex(i))) } 63 else { (true.B, ivalid, 0.U) } 64 ), w = RegWriteFn((valid, data) => { 65 if (notempty) { when (valid) { pmaCfgMerged(pmaCfgIndex(i)) := write_cfg_vec(mask, addr, i)(data) } } 66 true.B 67 }), desc = RegFieldDesc(s"MMPMA_config_${i}", s"pma config register #${i}")) 68 }} 69 70 val addr_map = (0 until num).map{ i => { 71 val next_cfg = if (i == 0) 0.U.asTypeOf(new PMPConfig()) else cfg(i-1) 72 RegField( 73 n = PMXLEN, 74 r = ZeroExt(read_addr(cfg(i))(addr(i)), PMXLEN), 75 w = RegWriteFn((valid, data) => { 76 when (valid) { addr(i) := write_addr(next_cfg, mask(i))(data(addr(0).getWidth-1, 0), cfg(i), addr(i))} 77 true.B 78 }), 79 desc = RegFieldDesc(s"MMPMA_addr_${i}", s"pma addr register #${i}") 80 ) 81 }} 82 83 (cfg_map, addr_map, pma) 84 } 85 86} 87 88trait PMAMethod extends PMAConst { 89 /** 90 def SimpleMemMapList = List( 91 // Base address Top address Width Description Mode (RWXIDSAC) 92 MemMap("h00_0000_0000", "h00_0FFF_FFFF", "h0", "Reserved", "RW"), 93 MemMap("h00_1000_0000", "h00_1FFF_FFFF", "h0", "QSPI_Flash", "RWX"), 94 MemMap("h00_2000_0000", "h00_2FFF_FFFF", "h0", "Reserved", "RW"), 95 MemMap("h00_3000_0000", "h00_3000_FFFF", "h0", "DMA", "RW"), 96 MemMap("h00_3001_0000", "h00_3004_FFFF", "h0", "GPU", "RWC"), 97 MemMap("h00_3005_0000", "h00_3006_FFFF", "h0", "USB/SDMMC", "RW"), 98 MemMap("h00_3007_0000", "h00_30FF_FFFF", "h0", "Reserved", "RW"), 99 MemMap("h00_3100_0000", "h00_3111_FFFF", "h0", "MMIO", "RW"), 100 MemMap("h00_3112_0000", "h00_37FF_FFFF", "h0", "Reserved", "RW"), 101 MemMap("h00_3800_0000", "h00_3800_FFFF", "h0", "CLINT", "RW"), 102 MemMap("h00_3801_0000", "h00_3801_FFFF", "h0", "BEU", "RW"), 103 MemMap("h00_3802_0000", "h00_3802_0FFF", "h0", "DebugModule", "RWX"), 104 MemMap("h00_3802_1000", "h00_3802_11FF", "h0", "MMPMA", "RW"), 105 MemMap("h00_3802_1200", "h00_3900_0FFF", "h0", "Reserved", ""), 106 MemMap("h00_3900_1000", "h00_3900_103F", "h0", "Core_reset", "RW"), 107 MemMap("h00_3900_1020", "h00_39FF_FFFF", "h0", "Reserved", ""), 108 MemMap("h00_3A00_0000", "h00_3A00_003F", "h0", "PLL0", "RW), 109 MemMap('h00_3A00_0020", "h00_3BFF_FFFF", "h0", "Reserved", ""), 110 MemMap("h00_3C00_0000", "h00_3FFF_FFFF", "h0", "PLIC", "RW"), 111 MemMap("h00_4000_0000", "h00_7FFF_FFFF", "h0", "PCIe", "RW"), 112 MemMap("h00_8000_0000", "h0F_FFFF_FFFF", "h0", "DDR", "RWXIDSA"), 113 ) 114 */ 115 116 def pma_init() : (Vec[UInt], Vec[UInt], Vec[UInt]) = { 117 // the init value is zero 118 // from 0 to num(default 16) - 1, lower priority 119 // according to simple map, 9 entries is needed, pick 6-14, leave 0-5 & 15 unusedcfgMerged.map(_ := 0.U) 120 121 val num = NumPMA 122 require(num >= 16) 123 val cfg = WireInit(0.U.asTypeOf(Vec(num, new PMPConfig()))) 124 125 val addr = Wire(Vec(num, UInt((PMPAddrBits-PMPOffBits).W))) 126 val mask = Wire(Vec(num, UInt(PMPAddrBits.W))) 127 addr := DontCare 128 mask := DontCare 129 130 var idx = num-1 131 132 // TODO: turn to napot to save entries 133 // use tor instead of napot, for napot may be confusing and hard to understand 134 // NOTE: all the addr space are default set to DDR, RWXCA 135 idx = idx - 1 136 addr(idx) := shift_addr(0xFFFFFFFFFL) // all the addr are default ddr, whicn means rwxca 137 cfg(idx).a := 3.U; cfg(idx).r := true.B; cfg(idx).w := true.B; cfg(idx).x := true.B; cfg(idx).c := true.B; cfg(idx).atomic := true.B 138 mask(idx) := match_mask(addr(idx), cfg(idx)) 139 idx = idx - 1 140 141 // NOTE: (0x0_0000_0000L, 0x0_8000_0000L) are default set to MMIO, only RW 142 addr(idx) := get_napot(0x00000000L, 0x80000000L) 143 cfg(idx).a := 3.U; cfg(idx).r := true.B; cfg(idx).w := true.B 144 mask(idx) := match_mask(addr(idx), cfg(idx)) 145 idx = idx - 1 146 147 addr(idx) := shift_addr(0x3C000000) 148 cfg(idx).a := 1.U 149 idx = idx - 1 150 151 addr(idx) := shift_addr(0x3A000040) 152 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B 153 idx = idx - 1 154 155 addr(idx) := shift_addr(0x3A000000) 156 cfg(idx).a := 1.U 157 idx = idx - 1 158 159 addr(idx) := shift_addr(0x39001040) 160 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B 161 idx = idx - 1 162 163 addr(idx) := shift_addr(0x39001000) 164 cfg(idx).a := 1.U 165 idx = idx - 1 166 167 addr(idx) := shift_addr(0x38021200) 168 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B 169 idx = idx - 1 170 171 addr(idx) := shift_addr(0x38021000) 172 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B; cfg(idx).x := true.B 173 idx = idx - 1 174 175 addr(idx) := shift_addr(0x38020000) 176 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B 177 idx = idx - 1 178 179 addr(idx) := shift_addr( 0x30050000) 180 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B; cfg(idx).c := true.B 181 idx = idx - 1 182 183 addr(idx) := shift_addr( 0x30010000) 184 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B 185 idx = idx - 1 186 187 addr(idx) := shift_addr( 0x20000000) 188 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B; cfg(idx).x := true.B 189 idx = idx - 1 190 191 addr(idx) := shift_addr( 0x10000000) 192 cfg(idx).a := 1.U; cfg(idx).r := true.B; cfg(idx).w := true.B 193 idx = idx - 1 194 195 addr(idx) := shift_addr(0) 196 197 require(idx >= 0) 198 199 val cfgInitMerge = cfg.asTypeOf(Vec(num/8, UInt(PMXLEN.W))) 200 (cfgInitMerge, addr, mask) 201 } 202 203 def get_napot(base: BigInt, range: BigInt) = { 204 val PlatformGrainBytes = (1 << PlatformGrain) 205 if ((base % PlatformGrainBytes) != 0) { 206 println("base:%x", base) 207 } 208 if ((range % PlatformGrainBytes) != 0) { 209 println("range: %x", range) 210 } 211 require((base % PlatformGrainBytes) == 0) 212 require((range % PlatformGrainBytes) == 0) 213 214 ((base + (range/2 - 1)) >> PMPOffBits).U 215 } 216 217 def match_mask(paddr: UInt, cfg: PMPConfig) = { 218 val match_mask_addr: UInt = Cat(paddr, cfg.a(0)).asUInt() | (((1 << PlatformGrain) - 1) >> PMPOffBits).U((paddr.getWidth + 1).W) 219 Cat(match_mask_addr & ~(match_mask_addr + 1.U), ((1 << PMPOffBits) - 1).U(PMPOffBits.W)) 220 } 221 222 def shift_addr(addr: BigInt) = { 223 (addr >> 2).U 224 } 225} 226 227trait PMACheckMethod extends PMPConst { 228 def pma_check(cmd: UInt, cfg: PMPConfig) = { 229 val resp = Wire(new PMPRespBundle) 230 resp.ld := TlbCmd.isRead(cmd) && !TlbCmd.isAtom(cmd) && !cfg.r 231 resp.st := (TlbCmd.isWrite(cmd) || TlbCmd.isAtom(cmd) && cfg.atomic) && !cfg.w 232 resp.instr := TlbCmd.isExec(cmd) && !cfg.x 233 resp.mmio := !cfg.c 234 resp 235 } 236 237 def pma_match_res(leaveHitMux: Boolean = false, valid: Bool = true.B)( 238 addr: UInt, 239 size: UInt, 240 pmaEntries: Vec[PMPEntry], 241 mode: UInt, 242 lgMaxSize: Int 243 ) = { 244 val num = pmaEntries.size 245 require(num == NumPMA) 246 // pma should always be checked, could not be ignored 247 // like amo and cached, it is the attribute not protection 248 // so it must have initialization. 249 require(!pmaEntries.isEmpty) 250 251 val pmaDefault = WireInit(0.U.asTypeOf(new PMPEntry())) 252 val match_vec = Wire(Vec(num+1, Bool())) 253 val cfg_vec = Wire(Vec(num+1, new PMPEntry())) 254 255 pmaEntries.zip(pmaDefault +: pmaEntries.take(num-1)).zipWithIndex.foreach{ case ((pma, last_pma), i) => 256 val is_match = pma.is_match(addr, size, lgMaxSize, last_pma) 257 val aligned = pma.aligned(addr, size, lgMaxSize, last_pma) 258 259 val cur = WireInit(pma) 260 cur.cfg.r := aligned && pma.cfg.r 261 cur.cfg.w := aligned && pma.cfg.w 262 cur.cfg.x := aligned && pma.cfg.x 263 cur.cfg.atomic := aligned && pma.cfg.atomic 264 cur.cfg.c := aligned && pma.cfg.c 265 266 match_vec(i) := is_match 267 cfg_vec(i) := cur 268 } 269 270 match_vec(num) := true.B 271 cfg_vec(num) := pmaDefault 272 if (leaveHitMux) { 273 ParallelPriorityMux(match_vec.map(RegEnable(_, init = false.B, valid)), RegEnable(cfg_vec, valid)) 274 } else { 275 ParallelPriorityMux(match_vec, cfg_vec) 276 } 277 } 278} 279