apb: diplomatic APB framework
This commit is contained in:
parent
4d87d07343
commit
ed091f55e6
32
src/main/scala/uncore/apb/Bundles.scala
Normal file
32
src/main/scala/uncore/apb/Bundles.scala
Normal file
@ -0,0 +1,32 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore.apb
|
||||
|
||||
import Chisel._
|
||||
import util.GenericParameterizedBundle
|
||||
|
||||
abstract class APBBundleBase(params: APBBundleParameters) extends GenericParameterizedBundle(params)
|
||||
|
||||
// Signal directions are from the master's point-of-view
|
||||
class APBBundle(params: APBBundleParameters) extends APBBundleBase(params)
|
||||
{
|
||||
// Flow control signals from the master
|
||||
val psel = Bool(OUTPUT)
|
||||
val penable = Bool(OUTPUT)
|
||||
|
||||
// Payload signals
|
||||
val pwrite = Bool(OUTPUT)
|
||||
val paddr = UInt(OUTPUT, width = params.addrBits)
|
||||
val pprot = UInt(OUTPUT, width = params.protBits)
|
||||
val pwdata = UInt(OUTPUT, width = params.dataBits)
|
||||
val pstrb = UInt(OUTPUT, width = params.dataBits/8)
|
||||
|
||||
val pready = Bool(INPUT)
|
||||
val pslverr = Bool(INPUT)
|
||||
val prdata = UInt(INPUT, width = params.dataBits)
|
||||
}
|
||||
|
||||
object APBBundle
|
||||
{
|
||||
def apply(params: APBBundleParameters) = new APBBundle(params)
|
||||
}
|
59
src/main/scala/uncore/apb/Nodes.scala
Normal file
59
src/main/scala/uncore/apb/Nodes.scala
Normal file
@ -0,0 +1,59 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore.apb
|
||||
|
||||
import Chisel._
|
||||
import chisel3.internal.sourceinfo.SourceInfo
|
||||
import config._
|
||||
import diplomacy._
|
||||
|
||||
object APBImp extends NodeImp[APBMasterPortParameters, APBSlavePortParameters, APBEdgeParameters, APBEdgeParameters, APBBundle]
|
||||
{
|
||||
def edgeO(pd: APBMasterPortParameters, pu: APBSlavePortParameters): APBEdgeParameters = APBEdgeParameters(pd, pu)
|
||||
def edgeI(pd: APBMasterPortParameters, pu: APBSlavePortParameters): APBEdgeParameters = APBEdgeParameters(pd, pu)
|
||||
def bundleO(eo: Seq[APBEdgeParameters]): Vec[APBBundle] = {
|
||||
require (!eo.isEmpty)
|
||||
Vec(eo.size, APBBundle(eo.map(_.bundle).reduce(_.union(_))))
|
||||
}
|
||||
def bundleI(ei: Seq[APBEdgeParameters]): Vec[APBBundle] = {
|
||||
require (!ei.isEmpty)
|
||||
Vec(ei.size, APBBundle(ei.map(_.bundle).reduce(_.union(_))))
|
||||
}
|
||||
|
||||
def colour = "#00ccff" // bluish
|
||||
override def labelI(ei: APBEdgeParameters) = (ei.slave.beatBytes * 8).toString
|
||||
override def labelO(eo: APBEdgeParameters) = (eo.slave.beatBytes * 8).toString
|
||||
|
||||
def connect(bo: => APBBundle, bi: => APBBundle, ei: => APBEdgeParameters)(implicit p: Parameters, sourceInfo: SourceInfo): (Option[LazyModule], () => Unit) = {
|
||||
(None, () => { bi <> bo })
|
||||
}
|
||||
|
||||
override def mixO(pd: APBMasterPortParameters, node: OutwardNode[APBMasterPortParameters, APBSlavePortParameters, APBBundle]): APBMasterPortParameters =
|
||||
pd.copy(masters = pd.masters.map { c => c.copy (nodePath = node +: c.nodePath) })
|
||||
override def mixI(pu: APBSlavePortParameters, node: InwardNode[APBMasterPortParameters, APBSlavePortParameters, APBBundle]): APBSlavePortParameters =
|
||||
pu.copy(slaves = pu.slaves.map { m => m.copy (nodePath = node +: m.nodePath) })
|
||||
}
|
||||
|
||||
// Nodes implemented inside modules
|
||||
case class APBIdentityNode() extends IdentityNode(APBImp)
|
||||
case class APBMasterNode(portParams: APBMasterPortParameters, numPorts: Range.Inclusive = 1 to 1)
|
||||
extends SourceNode(APBImp)(portParams, numPorts)
|
||||
case class APBSlaveNode(portParams: APBSlavePortParameters, numPorts: Range.Inclusive = 1 to 1)
|
||||
extends SinkNode(APBImp)(portParams, numPorts)
|
||||
case class APBAdapterNode(
|
||||
masterFn: Seq[APBMasterPortParameters] => APBMasterPortParameters,
|
||||
slaveFn: Seq[APBSlavePortParameters] => APBSlavePortParameters,
|
||||
numMasterPorts: Range.Inclusive = 1 to 1,
|
||||
numSlavePorts: Range.Inclusive = 1 to 1)
|
||||
extends InteriorNode(APBImp)(masterFn, slaveFn, numMasterPorts, numSlavePorts)
|
||||
|
||||
// Nodes passed from an inner module
|
||||
case class APBOutputNode() extends OutputNode(APBImp)
|
||||
case class APBInputNode() extends InputNode(APBImp)
|
||||
|
||||
// Nodes used for external ports
|
||||
case class APBBlindOutputNode(portParams: APBSlavePortParameters) extends BlindOutputNode(APBImp)(portParams)
|
||||
case class APBBlindInputNode(portParams: APBMasterPortParameters) extends BlindInputNode(APBImp)(portParams)
|
||||
|
||||
case class APBInternalOutputNode(portParams: APBSlavePortParameters) extends InternalOutputNode(APBImp)(portParams)
|
||||
case class APBInternalInputNode(portParams: APBMasterPortParameters) extends InternalInputNode(APBImp)(portParams)
|
86
src/main/scala/uncore/apb/Parameters.scala
Normal file
86
src/main/scala/uncore/apb/Parameters.scala
Normal file
@ -0,0 +1,86 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore.apb
|
||||
|
||||
import Chisel._
|
||||
import config._
|
||||
import diplomacy._
|
||||
import scala.math.max
|
||||
|
||||
case class APBSlaveParameters(
|
||||
address: Seq[AddressSet],
|
||||
regionType: RegionType.T = RegionType.GET_EFFECTS,
|
||||
executable: Boolean = false, // processor can execute from this memory
|
||||
nodePath: Seq[BaseNode] = Seq(),
|
||||
supportsWrite: Boolean = true,
|
||||
supportsRead: Boolean = true)
|
||||
{
|
||||
address.foreach { a => require (a.finite) }
|
||||
address.combinations(2).foreach { case Seq(x,y) => require (!x.overlaps(y)) }
|
||||
|
||||
val name = nodePath.lastOption.map(_.lazyModule.name).getOrElse("disconnected")
|
||||
val maxAddress = address.map(_.max).max
|
||||
val minAlignment = address.map(_.alignment).min
|
||||
}
|
||||
|
||||
case class APBSlavePortParameters(
|
||||
slaves: Seq[APBSlaveParameters],
|
||||
beatBytes: Int)
|
||||
{
|
||||
require (!slaves.isEmpty)
|
||||
require (isPow2(beatBytes))
|
||||
|
||||
val maxAddress = slaves.map(_.maxAddress).max
|
||||
|
||||
lazy val routingMask = AddressDecoder(slaves.map(_.address))
|
||||
def findSafe(address: UInt) = Vec(slaves.map(_.address.map(_.contains(address)).reduce(_ || _)))
|
||||
def findFast(address: UInt) = Vec(slaves.map(_.address.map(_.widen(~routingMask)).distinct.map(_.contains(address)).reduce(_ || _)))
|
||||
|
||||
// Require disjoint ranges for addresses
|
||||
slaves.combinations(2).foreach { case Seq(x,y) =>
|
||||
x.address.foreach { a => y.address.foreach { b =>
|
||||
require (!a.overlaps(b))
|
||||
} }
|
||||
}
|
||||
}
|
||||
|
||||
case class APBMasterParameters(
|
||||
nodePath: Seq[BaseNode] = Seq())
|
||||
{
|
||||
val name = nodePath.lastOption.map(_.lazyModule.name).getOrElse("disconnected")
|
||||
}
|
||||
|
||||
case class APBMasterPortParameters(
|
||||
masters: Seq[APBMasterParameters])
|
||||
|
||||
case class APBBundleParameters(
|
||||
addrBits: Int,
|
||||
dataBits: Int)
|
||||
{
|
||||
require (dataBits >= 8)
|
||||
require (addrBits >= 1)
|
||||
require (isPow2(dataBits))
|
||||
|
||||
// Bring the globals into scope
|
||||
val protBits = APBParameters.protBits
|
||||
|
||||
def union(x: APBBundleParameters) =
|
||||
APBBundleParameters(
|
||||
max(addrBits, x.addrBits),
|
||||
max(dataBits, x.dataBits))
|
||||
}
|
||||
|
||||
object APBBundleParameters
|
||||
{
|
||||
def apply(master: APBMasterPortParameters, slave: APBSlavePortParameters) =
|
||||
new APBBundleParameters(
|
||||
addrBits = log2Up(slave.maxAddress+1),
|
||||
dataBits = slave.beatBytes * 8)
|
||||
}
|
||||
|
||||
case class APBEdgeParameters(
|
||||
master: APBMasterPortParameters,
|
||||
slave: APBSlavePortParameters)
|
||||
{
|
||||
val bundle = APBBundleParameters(master, slave)
|
||||
}
|
16
src/main/scala/uncore/apb/Protocol.scala
Normal file
16
src/main/scala/uncore/apb/Protocol.scala
Normal file
@ -0,0 +1,16 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore.apb
|
||||
|
||||
import Chisel._
|
||||
|
||||
object APBParameters
|
||||
{
|
||||
// These are all fixed by the AHB standard:
|
||||
val protBits = 3
|
||||
|
||||
val PROT_PRIVILEDGED = UInt(1, width = protBits)
|
||||
val PROT_NONSECURE = UInt(2, width = protBits)
|
||||
val PROT_INSTRUCTION = UInt(4, width = protBits)
|
||||
def PROT_DEFAULT = PROT_PRIVILEDGED
|
||||
}
|
96
src/main/scala/uncore/apb/RegisterRouter.scala
Normal file
96
src/main/scala/uncore/apb/RegisterRouter.scala
Normal file
@ -0,0 +1,96 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore.apb
|
||||
|
||||
import Chisel._
|
||||
import config._
|
||||
import diplomacy._
|
||||
import regmapper._
|
||||
import scala.math.{min,max}
|
||||
|
||||
class APBRegisterNode(address: AddressSet, concurrency: Int = 0, beatBytes: Int = 4, undefZero: Boolean = true, executable: Boolean = false)
|
||||
extends APBSlaveNode(APBSlavePortParameters(
|
||||
Seq(APBSlaveParameters(
|
||||
address = Seq(address),
|
||||
executable = executable,
|
||||
supportsWrite = true,
|
||||
supportsRead = true)),
|
||||
beatBytes = beatBytes))
|
||||
{
|
||||
require (address.contiguous)
|
||||
|
||||
// Calling this method causes the matching APB bundle to be
|
||||
// configured to route all requests to the listed RegFields.
|
||||
def regmap(mapping: RegField.Map*) = {
|
||||
val apb = bundleIn(0)
|
||||
|
||||
val indexBits = log2Up((address.mask+1)/beatBytes)
|
||||
val params = RegMapperParams(indexBits, beatBytes, 1)
|
||||
val in = Wire(Decoupled(new RegMapperInput(params)))
|
||||
val out = RegMapper(beatBytes, concurrency, undefZero, in, mapping:_*)
|
||||
|
||||
// Only send the request to the RR once
|
||||
val taken = RegInit(Bool(false))
|
||||
when (in.fire()) { taken := Bool(true) }
|
||||
when (out.fire()) { taken := Bool(false) }
|
||||
|
||||
in.bits.read := !apb.pwrite
|
||||
in.bits.index := apb.paddr >> log2Ceil(beatBytes)
|
||||
in.bits.data := apb.pwdata
|
||||
in.bits.mask := Mux(apb.pwrite, apb.pstrb, UInt((1<<beatBytes) - 1))
|
||||
in.bits.extra := UInt(0)
|
||||
|
||||
in.valid := apb.psel && !taken
|
||||
out.ready := apb.penable
|
||||
|
||||
apb.pready := out.valid
|
||||
apb.pslverr := Bool(false)
|
||||
apb.prdata := out.bits.data
|
||||
}
|
||||
}
|
||||
|
||||
object APBRegisterNode
|
||||
{
|
||||
def apply(address: AddressSet, concurrency: Int = 0, beatBytes: Int = 4, undefZero: Boolean = true, executable: Boolean = false) =
|
||||
new APBRegisterNode(address, concurrency, beatBytes, undefZero, executable)
|
||||
}
|
||||
|
||||
// These convenience methods below combine to make it possible to create a APB
|
||||
// register mapped device from a totally abstract register mapped device.
|
||||
|
||||
abstract class APBRegisterRouterBase(address: AddressSet, interrupts: Int, concurrency: Int, beatBytes: Int, undefZero: Boolean, executable: Boolean)(implicit p: Parameters) extends LazyModule
|
||||
{
|
||||
val node = APBRegisterNode(address, concurrency, beatBytes, undefZero, executable)
|
||||
val intnode = uncore.tilelink2.IntSourceNode(interrupts)
|
||||
}
|
||||
|
||||
case class APBRegBundleArg(interrupts: Vec[Vec[Bool]], in: Vec[APBBundle])(implicit val p: Parameters)
|
||||
|
||||
class APBRegBundleBase(arg: APBRegBundleArg) extends Bundle
|
||||
{
|
||||
implicit val p = arg.p
|
||||
val interrupts = arg.interrupts
|
||||
val in = arg.in
|
||||
}
|
||||
|
||||
class APBRegBundle[P](val params: P, arg: APBRegBundleArg) extends APBRegBundleBase(arg)
|
||||
|
||||
class APBRegModule[P, B <: APBRegBundleBase](val params: P, bundleBuilder: => B, router: APBRegisterRouterBase)
|
||||
extends LazyModuleImp(router) with HasRegMap
|
||||
{
|
||||
val io = bundleBuilder
|
||||
val interrupts = if (io.interrupts.isEmpty) Vec(0, Bool()) else io.interrupts(0)
|
||||
def regmap(mapping: RegField.Map*) = router.node.regmap(mapping:_*)
|
||||
}
|
||||
|
||||
class APBRegisterRouter[B <: APBRegBundleBase, M <: LazyModuleImp]
|
||||
(val base: BigInt, val interrupts: Int = 0, val size: BigInt = 4096, val concurrency: Int = 0, val beatBytes: Int = 4, undefZero: Boolean = true, executable: Boolean = false)
|
||||
(bundleBuilder: APBRegBundleArg => B)
|
||||
(moduleBuilder: (=> B, APBRegisterRouterBase) => M)(implicit p: Parameters)
|
||||
extends APBRegisterRouterBase(AddressSet(base, size-1), interrupts, concurrency, beatBytes, undefZero, executable)
|
||||
{
|
||||
require (isPow2(size))
|
||||
// require (size >= 4096) ... not absolutely required, but highly recommended
|
||||
|
||||
lazy val module = moduleBuilder(bundleBuilder(APBRegBundleArg(intnode.bundleOut, node.bundleIn)), this)
|
||||
}
|
48
src/main/scala/uncore/apb/SRAM.scala
Normal file
48
src/main/scala/uncore/apb/SRAM.scala
Normal file
@ -0,0 +1,48 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore.apb
|
||||
|
||||
import Chisel._
|
||||
import config._
|
||||
import diplomacy._
|
||||
|
||||
class APBRAM(address: AddressSet, executable: Boolean = true, beatBytes: Int = 4)(implicit p: Parameters) extends LazyModule
|
||||
{
|
||||
val node = APBSlaveNode(APBSlavePortParameters(
|
||||
Seq(APBSlaveParameters(
|
||||
address = List(address),
|
||||
regionType = RegionType.UNCACHED,
|
||||
executable = executable,
|
||||
supportsRead = true,
|
||||
supportsWrite = true)),
|
||||
beatBytes = beatBytes))
|
||||
|
||||
// We require the address range to include an entire beat (for the write mask)
|
||||
require ((address.mask & (beatBytes-1)) == beatBytes-1)
|
||||
|
||||
lazy val module = new LazyModuleImp(this) {
|
||||
val io = new Bundle {
|
||||
val in = node.bundleIn
|
||||
}
|
||||
|
||||
val in = io.in(0)
|
||||
|
||||
def bigBits(x: BigInt, tail: List[Boolean] = List.empty[Boolean]): List[Boolean] =
|
||||
if (x == 0) tail.reverse else bigBits(x >> 1, ((x & 1) == 1) :: tail)
|
||||
val mask = bigBits(address.mask >> log2Ceil(beatBytes))
|
||||
val paddr = Cat((mask zip (in.paddr >> log2Ceil(beatBytes)).toBools).filter(_._1).map(_._2).reverse)
|
||||
|
||||
// Use single-ported memory with byte-write enable
|
||||
val mem = SeqMem(1 << mask.filter(b=>b).size, Vec(beatBytes, Bits(width = 8)))
|
||||
def holdUnless[T <: Data](in : T, enable: Bool): T = Mux(!enable, RegEnable(in, enable), in)
|
||||
|
||||
val read = in.psel && !in.penable && !in.pwrite
|
||||
when (in.psel && !in.penable && in.pwrite) {
|
||||
mem.write(paddr, Vec.tabulate(beatBytes) { i => in.pwdata(8*(i+1)-1, 8*i) }, in.pstrb.toBools)
|
||||
}
|
||||
|
||||
in.pready := Bool(true)
|
||||
in.pslverr := Bool(false)
|
||||
in.prdata := holdUnless(mem.read(paddr, read).asUInt, RegNext(read))
|
||||
}
|
||||
}
|
40
src/main/scala/uncore/apb/Test.scala
Normal file
40
src/main/scala/uncore/apb/Test.scala
Normal file
@ -0,0 +1,40 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore.apb
|
||||
|
||||
import Chisel._
|
||||
import config._
|
||||
import diplomacy._
|
||||
import uncore.tilelink2._
|
||||
import unittest._
|
||||
|
||||
class RRTest0(address: BigInt)(implicit p: Parameters) extends APBRegisterRouter(address, 0, 32, 0, 4)(
|
||||
new APBRegBundle((), _) with RRTest0Bundle)(
|
||||
new APBRegModule((), _, _) with RRTest0Module)
|
||||
|
||||
class RRTest1(address: BigInt)(implicit p: Parameters) extends APBRegisterRouter(address, 0, 32, 1, 4, false)(
|
||||
new APBRegBundle((), _) with RRTest1Bundle)(
|
||||
new APBRegModule((), _, _) with RRTest1Module)
|
||||
|
||||
class APBFuzzBridge()(implicit p: Parameters) extends LazyModule
|
||||
{
|
||||
val fuzz = LazyModule(new TLFuzzer(5000))
|
||||
val model = LazyModule(new TLRAMModel("APBFuzzMaster"))
|
||||
var xbar = LazyModule(new APBFanout)
|
||||
val ram = LazyModule(new APBRAM(AddressSet(0x0, 0xff)))
|
||||
val gpio = LazyModule(new RRTest0(0x100))
|
||||
|
||||
model.node := fuzz.node
|
||||
// xbar.node := TLToAPB()(model.node)
|
||||
ram.node := xbar.node
|
||||
gpio.node := xbar.node
|
||||
|
||||
lazy val module = new LazyModuleImp(this) with HasUnitTestIO {
|
||||
io.finished := fuzz.module.io.finished
|
||||
}
|
||||
}
|
||||
|
||||
class APBBridgeTest()(implicit p: Parameters) extends UnitTest(500000) {
|
||||
val dut = Module(LazyModule(new APBFuzzBridge).module)
|
||||
io.finished := dut.io.finished
|
||||
}
|
49
src/main/scala/uncore/apb/Xbar.scala
Normal file
49
src/main/scala/uncore/apb/Xbar.scala
Normal file
@ -0,0 +1,49 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore.apb
|
||||
|
||||
import Chisel._
|
||||
import config._
|
||||
import diplomacy._
|
||||
import regmapper._
|
||||
import scala.math.{min,max}
|
||||
|
||||
class APBFanout()(implicit p: Parameters) extends LazyModule {
|
||||
val node = APBAdapterNode(
|
||||
numSlavePorts = 1 to 1,
|
||||
numMasterPorts = 1 to 32,
|
||||
masterFn = { case Seq(m) => m },
|
||||
slaveFn = { seq => seq(0).copy(slaves = seq.flatMap(_.slaves)) })
|
||||
|
||||
lazy val module = new LazyModuleImp(this) {
|
||||
val io = new Bundle {
|
||||
val in = node.bundleIn
|
||||
val out = node.bundleOut
|
||||
}
|
||||
|
||||
val in = io.in(0)
|
||||
|
||||
// Require consistent bus widths
|
||||
val port0 = node.edgesOut(0).slave
|
||||
node.edgesOut.foreach { edge =>
|
||||
val port = edge.slave
|
||||
require (port.beatBytes == port0.beatBytes,
|
||||
s"${port.slaves.map(_.name)} ${port.beatBytes} vs ${port0.slaves.map(_.name)} ${port0.beatBytes}")
|
||||
}
|
||||
|
||||
val port_addrs = node.edgesOut.map(_.slave.slaves.map(_.address).flatten)
|
||||
val routingMask = AddressDecoder(port_addrs)
|
||||
val route_addrs = port_addrs.map(_.map(_.widen(~routingMask)).distinct)
|
||||
|
||||
val sel = Vec(route_addrs.map(seq => seq.map(_.contains(in.paddr)).reduce(_ || _)))
|
||||
(sel zip io.out) foreach { case (sel, out) =>
|
||||
out := in
|
||||
out.psel := sel && in.psel
|
||||
out.penable := sel && in.penable
|
||||
}
|
||||
|
||||
in.pready := !Mux1H(sel, io.out.map(!_.pready))
|
||||
in.pslverr := Mux1H(sel, io.out.map(_.pslverr))
|
||||
in.prdata := Mux1H(sel, io.out.map(_.prdata))
|
||||
}
|
||||
}
|
11
src/main/scala/uncore/apb/package.scala
Normal file
11
src/main/scala/uncore/apb/package.scala
Normal file
@ -0,0 +1,11 @@
|
||||
// See LICENSE.SiFive for license details.
|
||||
|
||||
package uncore
|
||||
|
||||
import Chisel._
|
||||
import diplomacy._
|
||||
|
||||
package object apb
|
||||
{
|
||||
type APBOutwardNode = OutwardNodeHandle[APBMasterPortParameters, APBSlavePortParameters, APBBundle]
|
||||
}
|
Loading…
Reference in New Issue
Block a user