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 18 19import chisel3._ 20import chisel3.util._ 21import org.chipsalliance.cde.config._ 22import chisel3.util.{Valid, ValidIO} 23import freechips.rocketchip.devices.debug.DebugModuleKey 24import freechips.rocketchip.diplomacy._ 25import freechips.rocketchip.interrupts._ 26import freechips.rocketchip.tile.{BusErrorUnit, BusErrorUnitParams, BusErrors, MaxHartIdBits} 27import freechips.rocketchip.tilelink._ 28import coupledL2.{EnableCHI, L2ParamKey, PrefetchCtrlFromCore} 29import coupledL2.tl2tl.TL2TLCoupledL2 30import coupledL2.tl2chi.{CHIIssue, PortIO, TL2CHICoupledL2} 31import huancun.BankBitsKey 32import system.HasSoCParameter 33import top.BusPerfMonitor 34import utility._ 35import utility.sram.SramMbistBundle 36import xiangshan.cache.mmu.TlbRequestIO 37import xiangshan.backend.fu.PMPRespBundle 38import xiangshan.backend.trace.{Itype, TraceCoreInterface} 39 40class L1BusErrorUnitInfo(implicit val p: Parameters) extends Bundle with HasSoCParameter { 41 val ecc_error = Valid(UInt(soc.PAddrBits.W)) 42} 43 44class XSL1BusErrors()(implicit val p: Parameters) extends BusErrors { 45 val icache = new L1BusErrorUnitInfo 46 val dcache = new L1BusErrorUnitInfo 47 val l2 = new L1BusErrorUnitInfo 48 49 override def toErrorList: List[Option[(ValidIO[UInt], String, String)]] = 50 List( 51 Some(icache.ecc_error, "I_ECC", "Icache ecc error"), 52 Some(dcache.ecc_error, "D_ECC", "Dcache ecc error"), 53 Some(l2.ecc_error, "L2_ECC", "L2Cache ecc error") 54 ) 55} 56 57/** 58 * L2Top contains everything between Core and XSTile-IO 59 */ 60class L2TopInlined()(implicit p: Parameters) extends LazyModule 61 with HasXSParameter 62 with HasSoCParameter 63{ 64 override def shouldBeInlined: Boolean = true 65 66 def chainBuffer(depth: Int, n: String): (Seq[LazyModule], TLNode) = { 67 val buffers = Seq.fill(depth){ LazyModule(new TLBuffer()) } 68 buffers.zipWithIndex.foreach{ case (b, i) => { 69 b.suggestName(s"${n}_${i}") 70 }} 71 val node = buffers.map(_.node.asInstanceOf[TLNode]).reduce(_ :*=* _) 72 (buffers, node) 73 } 74 val enableL2 = coreParams.L2CacheParamsOpt.isDefined 75 // =========== Components ============ 76 val l1_xbar = TLXbar() 77 val mmio_xbar = TLXbar() 78 val mmio_port = TLIdentityNode() // to L3 79 val memory_port = if (enableCHI && enableL2) None else Some(TLIdentityNode()) 80 val beu = LazyModule(new BusErrorUnit( 81 new XSL1BusErrors(), 82 BusErrorUnitParams(soc.BEURange.base, soc.BEURange.mask.toInt + 1) 83 )) 84 85 val i_mmio_port = TLTempNode() 86 val d_mmio_port = TLTempNode() 87 val icachectrl_port_opt = Option.when(icacheParameters.cacheCtrlAddressOpt.nonEmpty)(TLTempNode()) 88 val sep_tl_port_opt = Option.when(SeperateTLBus)(TLTempNode()) 89 90 val misc_l2_pmu = BusPerfMonitor(name = "Misc_L2", enable = !debugOpts.FPGAPlatform) // l1D & l1I & PTW 91 val l2_l3_pmu = BusPerfMonitor(name = "L2_L3", enable = !debugOpts.FPGAPlatform && !enableCHI, stat_latency = true) 92 val xbar_l2_buffer = TLBuffer() 93 94 val enbale_tllog = !debugOpts.FPGAPlatform && debugOpts.AlwaysBasicDB 95 val l1d_logger = TLLogger(s"L2_L1D_${coreParams.HartId}", enbale_tllog) 96 val l1i_logger = TLLogger(s"L2_L1I_${coreParams.HartId}", enbale_tllog) 97 val ptw_logger = TLLogger(s"L2_PTW_${coreParams.HartId}", enbale_tllog) 98 val ptw_to_l2_buffer = LazyModule(new TLBuffer) 99 val i_mmio_buffer = LazyModule(new TLBuffer) 100 101 val clint_int_node = IntIdentityNode() 102 val debug_int_node = IntIdentityNode() 103 val plic_int_node = IntIdentityNode() 104 val nmi_int_node = IntIdentityNode() 105 val beu_local_int_source = IntSourceNode(IntSourcePortSimple()) 106 107 println(s"enableCHI: ${enableCHI}") 108 val l2cache = if (enableL2) { 109 val config = new Config((_, _, _) => { 110 case L2ParamKey => coreParams.L2CacheParamsOpt.get.copy( 111 hartId = p(XSCoreParamsKey).HartId, 112 FPGAPlatform = debugOpts.FPGAPlatform, 113 hasMbist = hasMbist 114 ) 115 case EnableCHI => p(EnableCHI) 116 case CHIIssue => p(CHIIssue) 117 case BankBitsKey => log2Ceil(coreParams.L2NBanks) 118 case MaxHartIdBits => p(MaxHartIdBits) 119 case LogUtilsOptionsKey => p(LogUtilsOptionsKey) 120 case PerfCounterOptionsKey => p(PerfCounterOptionsKey) 121 }) 122 if (enableCHI) Some(LazyModule(new TL2CHICoupledL2()(new Config(config)))) 123 else Some(LazyModule(new TL2TLCoupledL2()(new Config(config)))) 124 } else None 125 val l2_binder = coreParams.L2CacheParamsOpt.map(_ => BankBinder(coreParams.L2NBanks, 64)) 126 127 // =========== Connection ============ 128 // l2 to l2_binder, then to memory_port 129 l2cache match { 130 case Some(l2) => 131 l2_binder.get :*= l2.node :*= xbar_l2_buffer :*= l1_xbar :=* misc_l2_pmu 132 l2 match { 133 case l2: TL2TLCoupledL2 => 134 memory_port.get := l2_l3_pmu := TLClientsMerger() := TLXbar() :=* l2_binder.get 135 case l2: TL2CHICoupledL2 => 136 l2.managerNode := TLXbar() :=* l2_binder.get 137 l2.mmioNode := mmio_port 138 } 139 case None => 140 memory_port.get := l1_xbar 141 } 142 143 mmio_xbar := TLBuffer.chainNode(2) := i_mmio_port 144 mmio_xbar := TLBuffer.chainNode(2) := d_mmio_port 145 beu.node := TLBuffer.chainNode(1) := mmio_xbar 146 if (icacheParameters.cacheCtrlAddressOpt.nonEmpty) { 147 icachectrl_port_opt.get := TLBuffer.chainNode(1) := mmio_xbar 148 } 149 if (SeperateTLBus) { 150 sep_tl_port_opt.get := TLBuffer.chainNode(1) := mmio_xbar 151 } 152 153 // filter out in-core addresses before sent to mmio_port 154 // Option[AddressSet] ++ Option[AddressSet] => List[AddressSet] 155 private def cacheAddressSet: Seq[AddressSet] = (icacheParameters.cacheCtrlAddressOpt ++ dcacheParameters.cacheCtrlAddressOpt).toSeq 156 private def mmioFilters = if(SeperateTLBus) (SeperateTLBusRanges ++ cacheAddressSet) else cacheAddressSet 157 mmio_port := 158 TLFilter(TLFilter.mSubtract(mmioFilters)) := 159 TLBuffer() := 160 mmio_xbar 161 162 class Imp(wrapper: LazyModule) extends LazyModuleImp(wrapper) { 163 val io = IO(new Bundle { 164 val beu_errors = Input(chiselTypeOf(beu.module.io.errors)) 165 val reset_vector = new Bundle { 166 val fromTile = Input(UInt(PAddrBits.W)) 167 val toCore = Output(UInt(PAddrBits.W)) 168 } 169 val hartId = new Bundle() { 170 val fromTile = Input(UInt(64.W)) 171 val toCore = Output(UInt(64.W)) 172 } 173 val msiInfo = new Bundle() { 174 val fromTile = Input(ValidIO(UInt(soc.IMSICParams.MSI_INFO_WIDTH.W))) 175 val toCore = Output(ValidIO(UInt(soc.IMSICParams.MSI_INFO_WIDTH.W))) 176 } 177 val msiAck = new Bundle { 178 val fromCore = Input(Bool()) 179 val toTile = Output(Bool()) 180 } 181 val cpu_halt = new Bundle() { 182 val fromCore = Input(Bool()) 183 val toTile = Output(Bool()) 184 } 185 val cpu_critical_error = new Bundle() { 186 val fromCore = Input(Bool()) 187 val toTile = Output(Bool()) 188 } 189 val hartIsInReset = new Bundle() { 190 val resetInFrontend = Input(Bool()) 191 val toTile = Output(Bool()) 192 } 193 val traceCoreInterface = new Bundle{ 194 val fromCore = Flipped(new TraceCoreInterface) 195 val toTile = new TraceCoreInterface 196 } 197 val debugTopDown = new Bundle() { 198 val robTrueCommit = Input(UInt(64.W)) 199 val robHeadPaddr = Flipped(Valid(UInt(36.W))) 200 val l2MissMatch = Output(Bool()) 201 } 202 val l2Miss = Output(Bool()) 203 val l3Miss = new Bundle { 204 val fromTile = Input(Bool()) 205 val toCore = Output(Bool()) 206 } 207 val clintTime = new Bundle { 208 val fromTile = Input(ValidIO(UInt(64.W))) 209 val toCore = Output(ValidIO(UInt(64.W))) 210 } 211 val chi = if (enableCHI) Some(new PortIO) else None 212 val nodeID = if (enableCHI) Some(Input(UInt(NodeIDWidth.W))) else None 213 val pfCtrlFromCore = Input(new PrefetchCtrlFromCore) 214 val l2_tlb_req = new TlbRequestIO(nRespDups = 2) 215 val l2_pmp_resp = Flipped(new PMPRespBundle) 216 val l2_hint = ValidIO(new L2ToL1Hint()) 217 val perfEvents = Output(Vec(numPCntHc * coreParams.L2NBanks + 1, new PerfEvent)) 218 val l2_flush_en = Option.when(EnablePowerDown) (Input(Bool())) 219 val l2_flush_done = Option.when(EnablePowerDown) (Output(Bool())) 220 val sramTestIn = new Bundle() { 221 val mbist = Option.when(hasMbist)(Input(new SramMbistBundle)) 222 val mbistReset = Option.when(hasMbist)(Input(new DFTResetSignals())) 223 val sramCtl = Option.when(hasSramCtl)(Input(UInt(64.W))) 224 } 225 val sramTestOut = new Bundle() { 226 val mbist = Option.when(hasMbist)(Output(new SramMbistBundle)) 227 val mbistReset = Option.when(hasMbist)(Output(new DFTResetSignals())) 228 val sramCtl = Option.when(hasSramCtl)(Output(UInt(64.W))) 229 } 230 // val reset_core = IO(Output(Reset())) 231 }) 232 io.sramTestOut.mbist.zip(io.sramTestIn.mbist).foreach({case(a, b) => a := b}) 233 io.sramTestOut.mbistReset.zip(io.sramTestIn.mbistReset).foreach({case(a, b) => a := b}) 234 io.sramTestOut.sramCtl.zip(io.sramTestIn.sramCtl).foreach({case(a, b) => a := b}) 235 236 val resetDelayN = Module(new DelayN(UInt(PAddrBits.W), 5)) 237 238 val (beu_int_out, _) = beu_local_int_source.out(0) 239 beu_int_out(0) := beu.module.io.interrupt 240 241 beu.module.io.errors.icache := io.beu_errors.icache 242 beu.module.io.errors.dcache := io.beu_errors.dcache 243 resetDelayN.io.in := io.reset_vector.fromTile 244 io.reset_vector.toCore := resetDelayN.io.out 245 io.hartId.toCore := io.hartId.fromTile 246 io.msiInfo.toCore := io.msiInfo.fromTile 247 io.cpu_halt.toTile := io.cpu_halt.fromCore 248 io.cpu_critical_error.toTile := io.cpu_critical_error.fromCore 249 io.msiAck.toTile := io.msiAck.fromCore 250 io.l3Miss.toCore := io.l3Miss.fromTile 251 io.clintTime.toCore := io.clintTime.fromTile 252 // trace interface 253 val traceToTile = io.traceCoreInterface.toTile 254 val traceFromCore = io.traceCoreInterface.fromCore 255 traceFromCore.fromEncoder := RegNext(traceToTile.fromEncoder) 256 traceToTile.toEncoder.trap := RegEnable( 257 traceFromCore.toEncoder.trap, 258 traceFromCore.toEncoder.groups(0).valid && Itype.isTrap(traceFromCore.toEncoder.groups(0).bits.itype) 259 ) 260 traceToTile.toEncoder.priv := RegEnable( 261 traceFromCore.toEncoder.priv, 262 traceFromCore.toEncoder.groups(0).valid 263 ) 264 (0 until TraceGroupNum).foreach{ i => 265 traceToTile.toEncoder.groups(i).valid := RegNext(traceFromCore.toEncoder.groups(i).valid) 266 traceToTile.toEncoder.groups(i).bits.iretire := RegNext(traceFromCore.toEncoder.groups(i).bits.iretire) 267 traceToTile.toEncoder.groups(i).bits.itype := RegNext(traceFromCore.toEncoder.groups(i).bits.itype) 268 traceToTile.toEncoder.groups(i).bits.ilastsize := RegEnable( 269 traceFromCore.toEncoder.groups(i).bits.ilastsize, 270 traceFromCore.toEncoder.groups(i).valid 271 ) 272 traceToTile.toEncoder.groups(i).bits.iaddr := RegEnable( 273 traceFromCore.toEncoder.groups(i).bits.iaddr, 274 traceFromCore.toEncoder.groups(i).valid 275 ) 276 } 277 278 dontTouch(io.hartId) 279 dontTouch(io.cpu_halt) 280 dontTouch(io.cpu_critical_error) 281 if (!io.chi.isEmpty) { dontTouch(io.chi.get) } 282 283 val hartIsInReset = RegInit(true.B) 284 hartIsInReset := io.hartIsInReset.resetInFrontend || reset.asBool 285 io.hartIsInReset.toTile := hartIsInReset 286 287 if (l2cache.isDefined) { 288 val l2 = l2cache.get.module 289 290 l2.io.pfCtrlFromCore := io.pfCtrlFromCore 291 l2.io.sramTest := io.sramTestIn 292 io.l2_hint := l2.io.l2_hint 293 l2.io.debugTopDown.robHeadPaddr := DontCare 294 l2.io.hartId := io.hartId.fromTile 295 l2.io.debugTopDown.robHeadPaddr := io.debugTopDown.robHeadPaddr 296 l2.io.debugTopDown.robTrueCommit := io.debugTopDown.robTrueCommit 297 io.debugTopDown.l2MissMatch := l2.io.debugTopDown.l2MissMatch 298 io.l2Miss := l2.io.l2Miss 299 io.l2_flush_done.foreach { _ := l2.io.l2FlushDone.getOrElse(false.B) } 300 l2.io.l2Flush.foreach { _ := io.l2_flush_en.getOrElse(false.B) } 301 302 /* l2 tlb */ 303 io.l2_tlb_req.req.bits := DontCare 304 io.l2_tlb_req.req.valid := l2.io.l2_tlb_req.req.valid 305 io.l2_tlb_req.resp.ready := l2.io.l2_tlb_req.resp.ready 306 io.l2_tlb_req.req.bits.vaddr := l2.io.l2_tlb_req.req.bits.vaddr 307 io.l2_tlb_req.req.bits.cmd := l2.io.l2_tlb_req.req.bits.cmd 308 io.l2_tlb_req.req.bits.size := l2.io.l2_tlb_req.req.bits.size 309 io.l2_tlb_req.req.bits.kill := l2.io.l2_tlb_req.req.bits.kill 310 io.l2_tlb_req.req.bits.no_translate := l2.io.l2_tlb_req.req.bits.no_translate 311 io.l2_tlb_req.req_kill := l2.io.l2_tlb_req.req_kill 312 io.perfEvents := l2.io_perf 313 314 val allPerfEvents = l2.getPerfEvents 315 if (printEventCoding) { 316 for (((name, inc), i) <- allPerfEvents.zipWithIndex) { 317 println("L2 Cache perfEvents Set", name, inc, i) 318 } 319 } 320 321 l2.io.l2_tlb_req.resp.valid := io.l2_tlb_req.resp.valid 322 l2.io.l2_tlb_req.req.ready := io.l2_tlb_req.req.ready 323 l2.io.l2_tlb_req.resp.bits.paddr.head := io.l2_tlb_req.resp.bits.paddr.head 324 l2.io.l2_tlb_req.resp.bits.pbmt := io.l2_tlb_req.resp.bits.pbmt.head 325 l2.io.l2_tlb_req.resp.bits.miss := io.l2_tlb_req.resp.bits.miss 326 l2.io.l2_tlb_req.resp.bits.excp.head.gpf := io.l2_tlb_req.resp.bits.excp.head.gpf 327 l2.io.l2_tlb_req.resp.bits.excp.head.pf := io.l2_tlb_req.resp.bits.excp.head.pf 328 l2.io.l2_tlb_req.resp.bits.excp.head.af := io.l2_tlb_req.resp.bits.excp.head.af 329 l2.io.l2_tlb_req.pmp_resp.ld := io.l2_pmp_resp.ld 330 l2.io.l2_tlb_req.pmp_resp.st := io.l2_pmp_resp.st 331 l2.io.l2_tlb_req.pmp_resp.instr := io.l2_pmp_resp.instr 332 l2.io.l2_tlb_req.pmp_resp.mmio := io.l2_pmp_resp.mmio 333 l2.io.l2_tlb_req.pmp_resp.atomic := io.l2_pmp_resp.atomic 334 l2cache.get match { 335 case l2cache: TL2CHICoupledL2 => 336 val l2 = l2cache.module 337 l2.io_nodeID := io.nodeID.get 338 io.chi.get <> l2.io_chi 339 l2.io_cpu_halt.foreach { _:= io.cpu_halt.fromCore } 340 case l2cache: TL2TLCoupledL2 => 341 } 342 343 beu.module.io.errors.l2.ecc_error.valid := l2.io.error.valid 344 beu.module.io.errors.l2.ecc_error.bits := l2.io.error.address 345 } else { 346 io.l2_hint := 0.U.asTypeOf(io.l2_hint) 347 io.debugTopDown <> DontCare 348 io.l2Miss := false.B 349 350 io.l2_tlb_req.req.valid := false.B 351 io.l2_tlb_req.req.bits := DontCare 352 io.l2_tlb_req.req_kill := DontCare 353 io.l2_tlb_req.resp.ready := true.B 354 io.perfEvents := DontCare 355 356 beu.module.io.errors.l2 := 0.U.asTypeOf(beu.module.io.errors.l2) 357 } 358 } 359 360 lazy val module = new Imp(this) 361} 362 363class L2Top()(implicit p: Parameters) extends LazyModule 364 with HasXSParameter 365 with HasSoCParameter { 366 367 override def shouldBeInlined: Boolean = false 368 369 val inner = LazyModule(new L2TopInlined()) 370 371 class Imp(wrapper: LazyModule) extends LazyModuleImp(wrapper) { 372 val io = IO(inner.module.io.cloneType) 373 val reset_core = IO(Output(Reset())) 374 io <> inner.module.io 375 376 if (debugOpts.ResetGen) { 377 ResetGen(ResetGenNode(Seq( 378 CellNode(reset_core), 379 ModuleNode(inner.module) 380 )), reset, sim = false, io.sramTestIn.mbistReset) 381 } else { 382 reset_core := DontCare 383 } 384 } 385 386 lazy val module = new Imp(this) 387} 388