XSCore.scala 11.8 KB
Newer Older
L
Lemover 已提交
1 2
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
Y
Yinan Xu 已提交
3
* Copyright (c) 2020-2021 Peng Cheng Laboratory
L
Lemover 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

L
LinJiawei 已提交
17 18
package xiangshan

L
linjiawei 已提交
19
import chipsalliance.rocketchip.config
20
import chipsalliance.rocketchip.config.Parameters
21 22
import chisel3._
import chisel3.util._
23
import freechips.rocketchip.diplomacy.{BundleBridgeSource, LazyModule, LazyModuleImp}
J
Jiawei Lin 已提交
24
import freechips.rocketchip.interrupts.{IntSinkNode, IntSinkPortSimple}
L
LinJiawei 已提交
25
import freechips.rocketchip.tile.HasFPUParameters
J
Jiawei Lin 已提交
26
import freechips.rocketchip.tilelink.TLBuffer
J
Jiawei Lin 已提交
27
import system.HasSoCParameter
28
import utility._
X
Xuan Hu 已提交
29
import utils._
30 31 32
import xiangshan.backend._
import xiangshan.cache.mmu._
import xiangshan.frontend._
X
Xuan Hu 已提交
33
import xiangshan.v2backend._
L
LinJiawei 已提交
34

35
abstract class XSModule(implicit val p: Parameters) extends Module
L
LinJiawei 已提交
36
  with HasXSParameter
37
  with HasFPUParameters
L
LinJiawei 已提交
38

39
//remove this trait after impl module logic
40 41
trait NeedImpl {
  this: RawModule =>
42
  override protected def IO[T <: Data](iodef: T): T = {
L
LinJiawei 已提交
43
    println(s"[Warn]: (${this.name}) please reomve 'NeedImpl' after implement this module")
44 45 46 47 48 49
    val io = chisel3.experimental.IO(iodef)
    io <> DontCare
    io
  }
}

X
Xuan Hu 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
//class WritebackSourceParams(
//  var exuConfigs: Seq[Seq[ExuConfig]] = Seq()
// ) {
//  def length: Int = exuConfigs.length
//  def ++(that: WritebackSourceParams): WritebackSourceParams = {
//    new WritebackSourceParams(exuConfigs ++ that.exuConfigs)
//  }
//}

//trait HasWritebackSource {
//  val writebackSourceParams: Seq[WritebackSourceParams]
//  final def writebackSource(sourceMod: HasWritebackSourceImp): Seq[Seq[Valid[ExuOutput]]] = {
//    require(sourceMod.writebackSource.isDefined, "should not use Valid[ExuOutput]")
//    val source = sourceMod.writebackSource.get
//    require(source.length == writebackSourceParams.length, "length mismatch between sources")
//    for ((s, p) <- source.zip(writebackSourceParams)) {
//      require(s.length == p.length, "params do not match with the exuOutput")
//    }
//    source
//  }
//  final def writebackSource1(sourceMod: HasWritebackSourceImp): Seq[Seq[DecoupledIO[ExuOutput]]] = {
//    require(sourceMod.writebackSource1.isDefined, "should not use DecoupledIO[ExuOutput]")
//    val source = sourceMod.writebackSource1.get
//    require(source.length == writebackSourceParams.length, "length mismatch between sources")
//    for ((s, p) <- source.zip(writebackSourceParams)) {
//      require(s.length == p.length, "params do not match with the exuOutput")
//    }
//    source
//  }
//  val writebackSourceImp: HasWritebackSourceImp
//}

//trait HasWritebackSourceImp {
//  def writebackSource: Option[Seq[Seq[Valid[ExuOutput]]]] = None
//  def writebackSource1: Option[Seq[Seq[DecoupledIO[ExuOutput]]]] = None
//}

//trait HasWritebackSink {
//  // Caches all sources. The selected source will be the one with smallest length.
//  var writebackSinks = ListBuffer.empty[(Seq[HasWritebackSource], Seq[Int])]
//  def addWritebackSink(source: Seq[HasWritebackSource], index: Option[Seq[Int]] = None): HasWritebackSink = {
//    val realIndex = if (index.isDefined) index.get else Seq.fill(source.length)(0)
//    writebackSinks += ((source, realIndex))
//    this
//  }
//
//  def writebackSinksParams: Seq[WritebackSourceParams] = {
//    writebackSinks.map{ case (s, i) => s.zip(i).map(x => x._1.writebackSourceParams(x._2)).reduce(_ ++ _) }
//  }
//  final def writebackSinksMod(
//     thisMod: Option[HasWritebackSource] = None,
//     thisModImp: Option[HasWritebackSourceImp] = None
//   ): Seq[Seq[HasWritebackSourceImp]] = {
//    require(thisMod.isDefined == thisModImp.isDefined)
//    writebackSinks.map(_._1.map(source =>
//      if (thisMod.isDefined && source == thisMod.get) thisModImp.get else source.writebackSourceImp)
//    )
//  }
//  final def writebackSinksImp(
//    thisMod: Option[HasWritebackSource] = None,
//    thisModImp: Option[HasWritebackSourceImp] = None
//  ): Seq[Seq[ValidIO[ExuOutput]]] = {
//    val sourceMod = writebackSinksMod(thisMod, thisModImp)
//    writebackSinks.zip(sourceMod).map{ case ((s, i), m) =>
//      s.zip(i).zip(m).flatMap(x => x._1._1.writebackSource(x._2)(x._1._2))
//    }
//  }
//  def selWritebackSinks(func: WritebackSourceParams => Int): Int = {
//    writebackSinksParams.zipWithIndex.minBy(params => func(params._1))._2
//  }
//  def generateWritebackIO(
//    thisMod: Option[HasWritebackSource] = None,
//    thisModImp: Option[HasWritebackSourceImp] = None
//   ): Unit
//}
125

126
abstract class XSBundle(implicit val p: Parameters) extends Bundle
L
LinJiawei 已提交
127 128
  with HasXSParameter

129
abstract class XSCoreBase()(implicit p: config.Parameters) extends LazyModule
X
Xuan Hu 已提交
130
  with HasXSParameter
131
{
J
Jiawei Lin 已提交
132 133 134
  // interrupt sinks
  val clint_int_sink = IntSinkNode(IntSinkPortSimple(1, 2))
  val debug_int_sink = IntSinkNode(IntSinkPortSimple(1, 1))
135
  val plic_int_sink = IntSinkNode(IntSinkPortSimple(2, 1))
Y
Yinan Xu 已提交
136
  // outer facing nodes
J
jinyue110 已提交
137
  val frontend = LazyModule(new Frontend())
138
  val ptw = LazyModule(new L2TLBWrapper())
139
  val ptw_to_l2_buffer = if (!coreParams.softPTW) LazyModule(new TLBuffer) else null
140
  val csrOut = BundleBridgeSource(Some(() => new DistributedCSRIO()))
X
Xuan Hu 已提交
141
  val backend = LazyModule(new Backend(backendParams))
142

143 144 145
  if (!coreParams.softPTW) {
    ptw_to_l2_buffer.node := ptw.node
  }
J
Jiawei Lin 已提交
146

147 148
  val memBlock = LazyModule(new MemBlock()(p.alter((site, here, up) => {
    case XSCoreParamsKey => up(XSCoreParamsKey).copy(
X
Xuan Hu 已提交
149
      IssQueSize = 16 // Todo
150 151
    )
  })))
152 153 154 155 156
}

class XSCore()(implicit p: config.Parameters) extends XSCoreBase
  with HasXSDts
{
L
linjiawei 已提交
157 158 159
  lazy val module = new XSCoreImp(this)
}

160
class XSCoreImp(outer: XSCoreBase) extends LazyModuleImp(outer)
161
  with HasXSParameter
162
  with HasSoCParameter {
Y
Yinan Xu 已提交
163
  val io = IO(new Bundle {
164
    val hartId = Input(UInt(64.W))
165
    val reset_vector = Input(UInt(PAddrBits.W))
Y
Yinan Xu 已提交
166
    val cpu_halt = Output(Bool())
167
    val l2_pf_enable = Output(Bool())
168
    val perfEvents = Input(Vec(numPCntHc * coreParams.L2NBanks, new PerfEvent))
J
Jiawei Lin 已提交
169
    val beu_errors = Output(new XSL1BusErrors())
Y
Yinan Xu 已提交
170
  })
171

Z
ZhangZifei 已提交
172 173
  println(s"FPGAPlatform:${env.FPGAPlatform} EnableDebug:${env.EnableDebug}")

J
jinyue110 已提交
174
  val frontend = outer.frontend.module
X
Xuan Hu 已提交
175
  val backend = outer.backend.module
176
  val memBlock = outer.memBlock.module
L
linjiawei 已提交
177
  val ptw = outer.ptw.module
178
  val ptw_to_l2_buffer = if (!coreParams.softPTW) outer.ptw_to_l2_buffer.module else null
X
Xuan Hu 已提交
179 180

  val fenceio = backend.io.fenceio
181

182
  frontend.io.hartId  := io.hartId
X
Xuan Hu 已提交
183 184 185 186 187 188 189
  frontend.io.backend <> backend.io.frontend
  frontend.io.sfence <> backend.io.frontendSfence
  frontend.io.tlbCsr <> backend.io.frontendTlbCsr
  frontend.io.csrCtrl <> backend.io.frontendCsrCtrl
  frontend.io.fencei <> fenceio.fencei

  backend.io.fromTop.hartId := io.hartId
X
Xuan Hu 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  backend.io.fromTop.externalInterrupt.msip := outer.clint_int_sink.in.head._1(0)
  backend.io.fromTop.externalInterrupt.mtip := outer.clint_int_sink.in.head._1(1)
  backend.io.fromTop.externalInterrupt.meip := outer.plic_int_sink.in.head._1(0)
  backend.io.fromTop.externalInterrupt.seip := outer.plic_int_sink.in.last._1(0)
  backend.io.fromTop.externalInterrupt.debug := outer.debug_int_sink.in.head._1(0)

  backend.io.frontendCsrDistributedUpdate := frontend.io.csrUpdate

  backend.io.mem.stIn.zip(memBlock.io.stIn).foreach { case (sink, source) =>
    sink.valid := source.valid
    sink.bits := 0.U.asTypeOf(sink.bits)
    sink.bits.robIdx := source.bits.uop.robIdx
    sink.bits.ssid := source.bits.uop.ssid
    sink.bits.storeSetHit := source.bits.uop.storeSetHit
    // The other signals have not been used
  }
X
Xuan Hu 已提交
206
  backend.io.mem.memoryViolation <> memBlock.io.memoryViolation
X
Xuan Hu 已提交
207
  backend.io.mem.lsqEnqIO <> memBlock.io.enqLsq
X
Xuan Hu 已提交
208 209 210 211
  backend.io.mem.sqDeq := memBlock.io.sqDeq
  backend.io.mem.lqCancelCnt := memBlock.io.lqCancelCnt
  backend.io.mem.sqCancelCnt := memBlock.io.sqCancelCnt
  backend.io.mem.otherFastWakeup := memBlock.io.otherFastWakeup
X
Xuan Hu 已提交
212
  backend.io.mem.writeBack <> memBlock.io.writeback
X
Xuan Hu 已提交
213

214
  frontend.io.reset_vector := io.reset_vector
J
Jiawei Lin 已提交
215

X
Xuan Hu 已提交
216
  io.cpu_halt := backend.io.toTop.cpuHalted
217

218
  // memblock error exception writeback, 1 cycle after normal writeback
X
Xuan Hu 已提交
219
  backend.io.mem.s3_delayed_load_error <> memBlock.io.s3_delayed_load_error
220

221 222
  io.beu_errors.icache <> frontend.io.error.toL1BusErrorUnitInfo()
  io.beu_errors.dcache <> memBlock.io.error.toL1BusErrorUnitInfo()
223

X
Xuan Hu 已提交
224
  memBlock.io.hartId := io.hartId
X
Xuan Hu 已提交
225
  memBlock.io.issue <> backend.io.mem.issueUops
226 227
  // By default, instructions do not have exceptions when they enter the function units.
  memBlock.io.issue.map(_.bits.uop.clearExceptions())
X
Xuan Hu 已提交
228 229
  backend.io.mem.loadFastMatch <> memBlock.io.loadFastMatch
  backend.io.mem.loadFastImm <> memBlock.io.loadFastImm
X
Xuan Hu 已提交
230 231 232 233 234 235 236 237
  backend.io.mem.exceptionVAddr := memBlock.io.lsqio.exceptionAddr.vaddr
  backend.io.mem.csrDistributedUpdate := memBlock.io.csrUpdate

  backend.io.perf.frontendInfo := frontend.io.frontendInfo
  backend.io.perf.memInfo := memBlock.io.memInfo
  backend.io.perf.perfEventsFrontend := frontend.getPerf
  backend.io.perf.perfEventsLsu := memBlock.getPerf
  backend.io.perf.perfEventsHc := io.perfEvents
238

X
Xuan Hu 已提交
239
  //  XSPerfHistogram("fastIn_count", PopCount(allFastUop1.map(_.valid)), true.B, 0, allFastUop1.length, 1)
X
Xuan Hu 已提交
240 241 242 243
//  XSPerfHistogram("wakeup_count", PopCount(rfWriteback.map(_.valid)), true.B, 0, rfWriteback.length, 1)

//  ctrlBlock.perfinfo.perfEventsEu0 := intExuBlock.getPerf.dropRight(outer.intExuBlock.scheduler.numRs)
//  ctrlBlock.perfinfo.perfEventsEu1 := vecExuBlock.getPerf.dropRight(outer.vecExuBlock.scheduler.numRs)
244 245 246 247 248
  if (!coreParams.softPTW) {
    memBlock.io.perfEventsPTW := ptw.getPerf
  } else {
    memBlock.io.perfEventsPTW := DontCare
  }
X
Xuan Hu 已提交
249
//  ctrlBlock.perfinfo.perfEventsRs  := outer.exuBlocks.flatMap(b => b.module.getPerf.takeRight(b.scheduler.numRs))
250

X
Xuan Hu 已提交
251 252
  memBlock.io.sfence <> backend.io.mem.sfence
  memBlock.io.fenceToSbuffer <> backend.io.mem.toSbuffer
Y
Yinan Xu 已提交
253

X
Xuan Hu 已提交
254
  memBlock.io.redirect <> backend.io.mem.redirect
X
Xuan Hu 已提交
255 256 257
  memBlock.io.rsfeedback <> backend.io.mem.rsFeedBack
  memBlock.io.csrCtrl <> backend.io.mem.csrCtrl
  memBlock.io.tlbCsr <> backend.io.mem.tlbCsr
X
Xuan Hu 已提交
258
  memBlock.io.lsqio.rob <> backend.io.mem.robLsqIO
X
Xuan Hu 已提交
259
  memBlock.io.lsqio.exceptionAddr.isStore := backend.io.mem.isStoreException
260

X
Xuan Hu 已提交
261 262 263 264
  val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay,frontend.io.ptw, fenceio.sfence, backend.io.tlb, l2tlbParams.ifilterSize)
  val itlbRepeater2 = PTWRepeaterNB(passReady = false, itlbParams.fenceDelay, itlbRepeater1.io.ptw, ptw.io.tlb(0), fenceio.sfence, backend.io.tlb)
  val dtlbRepeater1  = PTWFilter(ldtlbParams.fenceDelay, memBlock.io.ptw, fenceio.sfence, backend.io.tlb, l2tlbParams.dfilterSize)
  val dtlbRepeater2  = PTWRepeaterNB(passReady = false, ldtlbParams.fenceDelay, dtlbRepeater1.io.ptw, ptw.io.tlb(1), fenceio.sfence, backend.io.tlb)
265
  ptw.io.sfence <> fenceio.sfence
X
Xuan Hu 已提交
266 267
  ptw.io.csr.tlb <> backend.io.tlb
  ptw.io.csr.distribute_csr <> backend.io.csrCustomCtrl.distribute_csr
268

269
  // if l2 prefetcher use stream prefetch, it should be placed in XSCore
X
Xuan Hu 已提交
270
  io.l2_pf_enable := backend.io.csrCustomCtrl.l2_pf_enable
271

272
  // Modules are reset one by one
J
Jiawei Lin 已提交
273 274 275 276 277 278 279 280 281 282
  val resetTree = ResetGenNode(
    Seq(
      ModuleNode(memBlock), ModuleNode(dtlbRepeater1),
      ResetGenNode(Seq(
        ModuleNode(itlbRepeater2),
        ModuleNode(ptw),
        ModuleNode(dtlbRepeater2),
        ModuleNode(ptw_to_l2_buffer),
      )),
      ResetGenNode(Seq(
X
Xuan Hu 已提交
283
        ModuleNode(backend),
J
Jiawei Lin 已提交
284 285 286 287 288 289 290
        ResetGenNode(Seq(
          ResetGenNode(Seq(
            ModuleNode(frontend), ModuleNode(itlbRepeater1)
          ))
        ))
      ))
    )
291
  )
J
Jiawei Lin 已提交
292

293
  ResetGen(resetTree, reset, !debugOpts.FPGAPlatform)
J
Jiawei Lin 已提交
294

L
LinJiawei 已提交
295
}