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

3
import chipsalliance.rocketchip.config.Parameters
W
wangkaifan 已提交
4
import device.{AXI4Timer, TLTimer, AXI4Plic}
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 48
  // CPU Cores
  private val xs_core = Seq.fill(NumCores)(LazyModule(new XSCore()))
Z
zhanglinjuan 已提交
49

Y
Yinan Xu 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  // L1 to L2 network
  // -------------------------------------------------
  private val l2_xbar = Seq.fill(NumCores)(TLXbar())

  private val l2cache = Seq.fill(NumCores)(LazyModule(new InclusiveCache(
    CacheParameters(
      level = 2,
      ways = L2NWays,
      sets = L2NSets,
      blockBytes = L2BlockSize,
      beatBytes = L1BusWidth / 8, // beatBytes = l1BusDataWidth / 8
      cacheName = s"L2"
    ),
    InclusiveCacheMicroParameters(
      writeBytes = 8
    )
  )))
Z
nothing  
zhanglinjuan 已提交
67

Y
Yinan Xu 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  // 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 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
  // L3 to memory network
  // -------------------------------------------------
  private val memory_xbar = TLXbar()
  private val mmioXbar = TLXbar()

  // only mem, dma and extDev are visible externally
  val mem = Seq.fill(L3NBanks)(AXI4IdentityNode())
  val dma = AXI4IdentityNode()
  val extDev = AXI4IdentityNode()

  // connections
  // -------------------------------------------------
  for (i <- 0 until NumCores) {
100
    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).memBlock.dcache.clientNode
Y
Yinan Xu 已提交
101 102
    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).l1pluscache.clientNode
    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).ptw.node
103
    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).l2Prefetcher.clientNode
104
    mmioXbar   := TLBuffer() := DebugIdentityNode() := xs_core(i).memBlock.uncache.clientNode
Y
Yinan Xu 已提交
105 106 107
    l2cache(i).node := TLBuffer() := DebugIdentityNode() := l2_xbar(i)
    l3_xbar := TLBuffer() := DebugIdentityNode() := l2cache(i).node
  }
Y
Yinan Xu 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

  // 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
  }

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

150 151 152 153
  private val clint = LazyModule(new TLTimer(
    Seq(AddressSet(0x38000000L, 0x0000ffffL)),
    sim = !env.FPGAPlatform
  ))
154

Y
Yinan Xu 已提交
155 156
  clint.node := mmioXbar
  extDev := AXI4UserYanker() := TLToAXI4() := mmioXbar
157

W
wangkaifan 已提交
158 159 160 161 162 163 164
  val plic = LazyModule(new AXI4Plic(
    Seq(AddressSet(0x3c000000L, 0x03ffffffL)),
    sim = !env.FPGAPlatform
  ))
  val plicIdentity = AXI4IdentityNode()
  plic.node := plicIdentity := AXI4UserYanker() := TLToAXI4() := mmioXbar

165 166
  lazy val module = new LazyModuleImp(this){
    val io = IO(new Bundle{
W
wangkaifan 已提交
167 168
      val extIntrs = Input(Vec(NrExtIntr, Bool()))
      // val meip = Input(Vec(NumCores, Bool()))
169 170
      val ila = if(env.FPGAPlatform && EnableILA) Some(Output(new ILABundle)) else None
    })
W
wangkaifan 已提交
171 172 173

    plic.module.io.extra.get.intrVec <> RegNext(RegNext(Cat(io.extIntrs)))

Y
Yinan Xu 已提交
174
    for (i <- 0 until NumCores) {
W
wangkaifan 已提交
175 176
      xs_core(i).module.io.externalInterrupt.mtip := clint.module.io.mtip(i)
      xs_core(i).module.io.externalInterrupt.msip := clint.module.io.msip(i)
W
wangkaifan 已提交
177 178
      // xs_core(i).module.io.externalInterrupt.meip := RegNext(RegNext(io.meip(i)))
      xs_core(i).module.io.externalInterrupt.meip := plic.module.io.extra.get.meip(i)
Y
Yinan Xu 已提交
179
    }
180 181 182
    // do not let dma AXI signals optimized out
    chisel3.dontTouch(dma.out.head._1)
    chisel3.dontTouch(extDev.out.head._1)
183
  }
Z
Zihao Yu 已提交
184

Y
Yinan Xu 已提交
185
}