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