SoC.scala 6.5 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
import freechips.rocketchip.tilelink.{BankBinder, TLBuffer, TLBundleParameters, TLCacheCork, TLClientNode, TLFilter, TLFuzzer, TLIdentityNode, TLToAXI4, TLWidthWidget, TLXbar}
9
import utils.{DebugIdentityNode, DataDontCareNode}
10
import utils.XSInfo
11
import xiangshan.{HasXSParameter, XSCore, HasXSLog, DifftestBundle}
Y
Yinan Xu 已提交
12
import sifive.blocks.inclusivecache.{CacheParameters, InclusiveCache, InclusiveCacheMicroParameters}
13 14 15
import freechips.rocketchip.diplomacy.{AddressSet, LazyModule, LazyModuleImp}
import freechips.rocketchip.devices.tilelink.{DevNullParams, TLError}
import freechips.rocketchip.amba.axi4.{AXI4Deinterleaver, AXI4Fragmenter, AXI4IdIndexer, AXI4IdentityNode, AXI4ToTL, AXI4UserYanker}
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
  // 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(
A
Allen 已提交
64
      writeBytes = 32
Y
Yinan Xu 已提交
65 66
    )
  )))
Z
nothing  
zhanglinjuan 已提交
67

Y
Yinan Xu 已提交
68 69 70 71
  // L2 to L3 network
  // -------------------------------------------------
  private val l3_xbar = TLXbar()

72 73 74 75 76 77 78 79 80 81
  private val l3_node = LazyModule(new InclusiveCache(
    CacheParameters(
      level = 3,
      ways = L3NWays,
      sets = L3NSets,
      blockBytes = L3BlockSize,
      beatBytes = L2BusWidth / 8,
      cacheName = "L3"
    ),
    InclusiveCacheMicroParameters(
A
Allen 已提交
82
      writeBytes = 32
83 84
    )
  )).node
Y
Yinan Xu 已提交
85

Y
Yinan Xu 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
  // 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) {
99
    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).memBlock.dcache.clientNode
Y
Yinan Xu 已提交
100 101
    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).l1pluscache.clientNode
    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).ptw.node
102
    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).l2Prefetcher.clientNode
103
    mmioXbar   := TLBuffer() := DebugIdentityNode() := xs_core(i).memBlock.uncache.clientNode
104
    mmioXbar   := TLBuffer() := DebugIdentityNode() := xs_core(i).frontend.instrUncache.clientNode
105
    l2cache(i).node := DataDontCareNode(a = true, b = true) := TLBuffer() := DebugIdentityNode() := l2_xbar(i)
Y
Yinan Xu 已提交
106 107
    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

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

132 133
  val bankedNode =
    BankBinder(L3NBanks, L3BlockSize) :*= l3_node :*= TLBuffer() :*= DebugIdentityNode() :*= l3_xbar
Y
Yinan Xu 已提交
134 135 136 137 138 139 140

  for(i <- 0 until L3NBanks) {
    mem(i) :=
      AXI4UserYanker() :=
      TLToAXI4() :=
      TLWidthWidget(L3BusWidth / 8) :=
      TLCacheCork() :=
141
      bankedNode
Y
Yinan Xu 已提交
142 143
  }

144 145 146 147
  private val clint = LazyModule(new TLTimer(
    Seq(AddressSet(0x38000000L, 0x0000ffffL)),
    sim = !env.FPGAPlatform
  ))
148

Y
Yinan Xu 已提交
149 150
  clint.node := mmioXbar
  extDev := AXI4UserYanker() := TLToAXI4() := mmioXbar
151

W
wangkaifan 已提交
152 153 154 155 156 157 158
  val plic = LazyModule(new AXI4Plic(
    Seq(AddressSet(0x3c000000L, 0x03ffffffL)),
    sim = !env.FPGAPlatform
  ))
  val plicIdentity = AXI4IdentityNode()
  plic.node := plicIdentity := AXI4UserYanker() := TLToAXI4() := mmioXbar

159 160
  lazy val module = new LazyModuleImp(this){
    val io = IO(new Bundle{
161
      val extIntrs = Input(UInt(NrExtIntr.W))
W
wangkaifan 已提交
162
      // val meip = Input(Vec(NumCores, Bool()))
163 164
      val ila = if(env.FPGAPlatform && EnableILA) Some(Output(new ILABundle)) else None
    })
165 166 167
    val difftestIO0 = IO(new DifftestBundle())
    val difftestIO1 = IO(new DifftestBundle())
    val difftestIO = Seq(difftestIO0, difftestIO1)
168 169 170 171 172

    val trapIO0 = IO(new xiangshan.TrapIO())
    val trapIO1 = IO(new xiangshan.TrapIO())
    val trapIO = Seq(trapIO0, trapIO1)

173
    plic.module.io.extra.get.intrVec <> RegNext(RegNext(io.extIntrs))
W
wangkaifan 已提交
174

Y
Yinan Xu 已提交
175
    for (i <- 0 until NumCores) {
176
      xs_core(i).module.io.hartId := i.U
W
wangkaifan 已提交
177 178
      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 已提交
179 180
      // 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)
181
      xs_core(i).module.io.l2ToPrefetcher <> l2cache(i).module.io
Y
Yinan Xu 已提交
182
    }
183
    difftestIO0 <> xs_core(0).module.difftestIO
184
    difftestIO1 <> DontCare
185 186 187 188
    trapIO0 <> xs_core(0).module.trapIO
    trapIO1 <> DontCare
    
    if (env.DualCore) {
189
      difftestIO1 <> xs_core(1).module.difftestIO
190
      trapIO1 <> xs_core(1).module.trapIO
191
    }
192
    // do not let dma AXI signals optimized out
193 194 195
    dontTouch(dma.out.head._1)
    dontTouch(extDev.out.head._1)
    dontTouch(io.extIntrs)
196
  }
Z
Zihao Yu 已提交
197

Y
Yinan Xu 已提交
198
}