SoC.scala 4.5 KB
Newer Older
Z
Zihao Yu 已提交
1 2
package system

3 4
import chipsalliance.rocketchip.config.Parameters
import device.{AXI4Timer, TLTimer}
Z
Zihao Yu 已提交
5
import chisel3._
Z
zhanglinjuan 已提交
6
import chisel3.util._
7
import freechips.rocketchip.diplomacy.{AddressSet, LazyModule, LazyModuleImp}
8 9
import freechips.rocketchip.tilelink.{TLBuffer, TLFuzzer, TLIdentityNode, TLXbar}
import utils.DebugIdentityNode
10
import xiangshan.{HasXSParameter, XSCore}
Y
Yinan Xu 已提交
11 12 13 14 15
import sifive.blocks.inclusivecache.{CacheParameters, InclusiveCache, InclusiveCacheMicroParameters}
import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp, AddressSet}
import freechips.rocketchip.tilelink.{TLBundleParameters, TLCacheCork, TLBuffer, TLClientNode, TLIdentityNode, TLXbar, TLWidthWidget, TLFilter, TLToAXI4}
import freechips.rocketchip.devices.tilelink.{TLError, DevNullParams}
import freechips.rocketchip.amba.axi4.{AXI4ToTL, AXI4IdentityNode, AXI4UserYanker, AXI4Fragmenter, AXI4IdIndexer, AXI4Deinterleaver}
L
LinJiawei 已提交
16 17 18

case class SoCParameters
(
Y
Yinan Xu 已提交
19
  NumCores: Integer = 1,
L
LinJiawei 已提交
20 21 22 23
  EnableILA: Boolean = false,
  HasL2Cache: Boolean = false,
  HasPrefetch: Boolean = false
)
Z
Zihao Yu 已提交
24

25
trait HasSoCParameter extends HasXSParameter{
26
  val soc = top.Parameters.get.socParameters
Y
Yinan Xu 已提交
27
  val NumCores = soc.NumCores
L
LinJiawei 已提交
28 29 30
  val EnableILA = soc.EnableILA
  val HasL2cache = soc.HasL2Cache
  val HasPrefetch = soc.HasPrefetch
31 32
}

L
LinJiawei 已提交
33
class ILABundle extends Bundle {}
34

Z
Zihao Yu 已提交
35

36 37 38
class DummyCore()(implicit p: Parameters) extends LazyModule {
  val mem = TLFuzzer(nOperations = 10)
  val mmio = TLFuzzer(nOperations = 10)
Z
zhanglinjuan 已提交
39

40
  lazy val module = new LazyModuleImp(this){
41

42 43 44 45 46
  }
}


class XSSoc()(implicit p: Parameters) extends LazyModule with HasSoCParameter {
Y
Yinan Xu 已提交
47
  private val cores = Seq.fill(NumCores)(LazyModule(new XSCore()))
Z
zhanglinjuan 已提交
48

49
  // only mem and extDev visible externally
Y
Yinan Xu 已提交
50
  val dma = AXI4IdentityNode()
A
Allen 已提交
51
  val extDev = AXI4IdentityNode()
Z
nothing  
zhanglinjuan 已提交
52

Y
Yinan Xu 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  // L2 to L3 network
  // -------------------------------------------------
  private val l3_xbar = TLXbar()

  private val l3_banks = (0 until L3NBanks) map (i =>
      LazyModule(new InclusiveCache(
        CacheParameters(
          level = 3,
          ways = L3NWays,
          sets = L3NSets,
          blockBytes = L3BlockSize,
          beatBytes = L2BusWidth / 8,
          cacheName = s"L3_$i"
        ),
      InclusiveCacheMicroParameters(
        writeBytes = 8
      )
    )))

Y
Yinan Xu 已提交
72
  cores.foreach(core => l3_xbar := TLBuffer() := DebugIdentityNode() := core.mem)
Y
Yinan Xu 已提交
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

  // DMA should not go to MMIO
  val mmioRange = AddressSet(base = 0x0000000000L, mask = 0x007fffffffL)
  // AXI4ToTL needs a TLError device to route error requests,
  // add one here to make it happy.
  val tlErrorParams = DevNullParams(
    address = Seq(mmioRange),
    maxAtomic = 8,
    maxTransfer = 64)
  val tlError = LazyModule(new TLError(params = tlErrorParams, beatBytes = L2BusWidth / 8))
  private val tlError_xbar = TLXbar()
  tlError_xbar :=
    AXI4ToTL() :=
    AXI4UserYanker(Some(1)) :=
    AXI4Fragmenter() :=
    AXI4IdIndexer(1) :=
    dma
  tlError.node := tlError_xbar

  l3_xbar :=
    TLBuffer() :=
    DebugIdentityNode() :=
    tlError_xbar

  def bankFilter(bank: Int) = AddressSet(
    base = bank * L3BlockSize,
    mask = ~BigInt((L3NBanks -1) * L3BlockSize))

  for(i <- 0 until L3NBanks) {
    val filter = TLFilter(TLFilter.mSelectIntersect(bankFilter(i)))
    l3_banks(i).node := TLBuffer() := DebugIdentityNode() := filter := l3_xbar
  }


  // L3 to memory network
  // -------------------------------------------------
  private val memory_xbar = TLXbar()

  val mem = Seq.fill(L3NBanks)(AXI4IdentityNode())
  for(i <- 0 until L3NBanks) {
    mem(i) :=
      AXI4UserYanker() :=
      TLToAXI4() :=
      TLWidthWidget(L3BusWidth / 8) :=
      TLCacheCork() :=
      l3_banks(i).node
  }

121 122 123 124 125
  private val mmioXbar = TLXbar()
  private val clint = LazyModule(new TLTimer(
    Seq(AddressSet(0x38000000L, 0x0000ffffL)),
    sim = !env.FPGAPlatform
  ))
126

Y
Yinan Xu 已提交
127 128
  cores.foreach(core =>
    mmioXbar :=
129 130
    TLBuffer() :=
    DebugIdentityNode() :=
Y
Yinan Xu 已提交
131 132
    core.mmio
  )
133 134 135 136 137

  clint.node :=
    mmioXbar

  extDev :=
A
Allen 已提交
138 139
    AXI4UserYanker() :=
    TLToAXI4() :=
140
    mmioXbar
141 142 143 144 145 146

  lazy val module = new LazyModuleImp(this){
    val io = IO(new Bundle{
      val meip = Input(Bool())
      val ila = if(env.FPGAPlatform && EnableILA) Some(Output(new ILABundle)) else None
    })
Y
Yinan Xu 已提交
147 148 149 150 151
    cores.foreach(core => {
      core.module.io.externalInterrupt.mtip := clint.module.io.mtip
      core.module.io.externalInterrupt.msip := clint.module.io.msip
      core.module.io.externalInterrupt.meip := RegNext(RegNext(io.meip))
    })
152 153 154
    // do not let dma AXI signals optimized out
    chisel3.dontTouch(dma.out.head._1)
    chisel3.dontTouch(extDev.out.head._1)
155
  }
Z
Zihao Yu 已提交
156

Y
Yinan Xu 已提交
157
}