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