2016-11-28 01:16:37 +01:00
|
|
|
// See LICENSE.SiFive for license details.
|
2016-10-09 06:36:32 +02:00
|
|
|
|
2017-07-07 19:48:16 +02:00
|
|
|
package freechips.rocketchip.tilelink
|
2016-10-09 06:36:32 +02:00
|
|
|
|
|
|
|
import Chisel._
|
2017-07-07 19:48:16 +02:00
|
|
|
import freechips.rocketchip.config.Parameters
|
|
|
|
import freechips.rocketchip.diplomacy._
|
|
|
|
import freechips.rocketchip.util._
|
|
|
|
import freechips.rocketchip.amba.axi4._
|
2016-10-09 06:36:32 +02:00
|
|
|
import scala.math.{min, max}
|
|
|
|
|
2017-10-13 01:41:54 +02:00
|
|
|
case class TLToAXI4Node(stripBits: Int = 0)(implicit valName: ValName) extends MixedAdapterNode(TLImp, AXI4Imp)(
|
2017-04-26 02:55:06 +02:00
|
|
|
dFn = { p =>
|
2017-06-24 01:59:55 +02:00
|
|
|
p.clients.foreach { c =>
|
|
|
|
require (c.sourceId.start % (1 << stripBits) == 0 &&
|
|
|
|
c.sourceId.end % (1 << stripBits) == 0,
|
|
|
|
"Cannot strip bits of aligned client ${c.name}: ${c.sourceId}")
|
|
|
|
}
|
2017-06-02 03:34:04 +02:00
|
|
|
val clients = p.clients.sortWith(TLToAXI4.sortByType _)
|
2017-06-24 01:59:55 +02:00
|
|
|
val idSize = clients.map { c => if (c.requestFifo) 1 else (c.sourceId.size >> stripBits) }
|
2017-04-26 02:55:06 +02:00
|
|
|
val idStart = idSize.scanLeft(0)(_+_).init
|
2017-06-02 03:34:04 +02:00
|
|
|
val masters = ((idStart zip idSize) zip clients) map { case ((start, size), c) =>
|
2016-10-10 20:47:40 +02:00
|
|
|
AXI4MasterParameters(
|
2017-06-03 00:09:35 +02:00
|
|
|
name = c.name,
|
2017-04-26 03:49:33 +02:00
|
|
|
id = IdRange(start, start+size),
|
|
|
|
aligned = true,
|
2017-06-24 01:59:55 +02:00
|
|
|
maxFlight = Some(if (c.requestFifo) c.sourceId.size else (1 << stripBits)),
|
2017-04-26 03:49:33 +02:00
|
|
|
nodePath = c.nodePath)
|
2017-04-26 02:55:06 +02:00
|
|
|
}
|
|
|
|
AXI4MasterPortParameters(
|
|
|
|
masters = masters,
|
2017-10-13 01:41:54 +02:00
|
|
|
userBits = log2Ceil(p.endSourceId) + 4)
|
2016-10-09 06:36:32 +02:00
|
|
|
},
|
2017-01-30 00:17:52 +01:00
|
|
|
uFn = { p => TLManagerPortParameters(
|
2017-01-24 02:54:27 +01:00
|
|
|
managers = p.slaves.map { case s =>
|
2016-10-09 06:36:32 +02:00
|
|
|
TLManagerParameters(
|
|
|
|
address = s.address,
|
2017-03-01 09:03:01 +01:00
|
|
|
resources = s.resources,
|
2016-10-09 06:36:32 +02:00
|
|
|
regionType = s.regionType,
|
|
|
|
executable = s.executable,
|
|
|
|
nodePath = s.nodePath,
|
|
|
|
supportsGet = s.supportsRead,
|
|
|
|
supportsPutFull = s.supportsWrite,
|
2017-04-26 02:55:06 +02:00
|
|
|
supportsPutPartial = s.supportsWrite,
|
|
|
|
fifoId = Some(0))},
|
2017-01-24 02:54:27 +01:00
|
|
|
beatBytes = p.beatBytes,
|
2017-01-30 00:17:52 +01:00
|
|
|
minLatency = p.minLatency)
|
|
|
|
})
|
2016-10-09 06:36:32 +02:00
|
|
|
|
2017-10-13 01:41:54 +02:00
|
|
|
class TLToAXI4(val combinational: Boolean = true, val adapterName: Option[String] = None, val stripBits: Int = 0)(implicit p: Parameters) extends LazyModule
|
2016-10-09 06:36:32 +02:00
|
|
|
{
|
2017-10-13 01:41:54 +02:00
|
|
|
val node = TLToAXI4Node(stripBits)
|
2016-10-09 06:36:32 +02:00
|
|
|
|
|
|
|
lazy val module = new LazyModuleImp(this) {
|
2017-09-14 03:06:03 +02:00
|
|
|
(node.in zip node.out) foreach { case ((in, edgeIn), (out, edgeOut)) =>
|
2017-01-30 00:17:52 +01:00
|
|
|
val slaves = edgeOut.slave.slaves
|
|
|
|
|
|
|
|
// All pairs of slaves must promise that they will never interleave data
|
|
|
|
require (slaves(0).interleavedId.isDefined)
|
|
|
|
slaves.foreach { s => require (s.interleavedId == slaves(0).interleavedId) }
|
|
|
|
|
2017-06-02 03:34:04 +02:00
|
|
|
val axiDigits = String.valueOf(edgeOut.master.endId-1).length()
|
|
|
|
val tlDigits = String.valueOf(edgeIn.client.endSourceId-1).length()
|
|
|
|
|
2017-04-26 02:55:06 +02:00
|
|
|
// Construct the source=>ID mapping table
|
2017-06-02 03:34:04 +02:00
|
|
|
adapterName.foreach { n => println(s"$n AXI4-ID <= TL-Source mapping:") }
|
2017-06-06 01:54:00 +02:00
|
|
|
val sourceStall = Wire(Vec(edgeIn.client.endSourceId, Bool()))
|
|
|
|
val sourceTable = Wire(Vec(edgeIn.client.endSourceId, out.aw.bits.id))
|
|
|
|
val idStall = Wire(init = Vec.fill(edgeOut.master.endId) { Bool(false) })
|
2017-06-27 02:54:17 +02:00
|
|
|
var idCount = Array.fill(edgeOut.master.endId) { None:Option[Int] }
|
2017-06-02 03:34:04 +02:00
|
|
|
val maps = (edgeIn.client.clients.sortWith(TLToAXI4.sortByType) zip edgeOut.master.masters) flatMap { case (c, m) =>
|
2017-04-26 02:55:06 +02:00
|
|
|
for (i <- 0 until c.sourceId.size) {
|
2017-06-24 01:59:55 +02:00
|
|
|
val id = m.id.start + (if (c.requestFifo) 0 else (i >> stripBits))
|
2017-06-06 01:54:00 +02:00
|
|
|
sourceStall(c.sourceId.start + i) := idStall(id)
|
|
|
|
sourceTable(c.sourceId.start + i) := UInt(id)
|
2017-04-26 02:55:06 +02:00
|
|
|
}
|
2017-06-27 02:54:17 +02:00
|
|
|
if (c.requestFifo) { idCount(m.id.start) = Some(c.sourceId.size) }
|
2017-06-02 03:34:04 +02:00
|
|
|
adapterName.map { n =>
|
|
|
|
val fmt = s"\t[%${axiDigits}d, %${axiDigits}d) <= [%${tlDigits}d, %${tlDigits}d) %s%s"
|
|
|
|
println(fmt.format(m.id.start, m.id.end, c.sourceId.start, c.sourceId.end, c.name, if (c.supportsProbe) " CACHE" else ""))
|
|
|
|
s"""{"axi4-id":[${m.id.start},${m.id.end}],"tilelink-id":[${c.sourceId.start},${c.sourceId.end}],"master":["${c.name}"],"cache":[${!(!c.supportsProbe)}]}"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
adapterName.foreach { n =>
|
|
|
|
println("")
|
|
|
|
ElaborationArtefacts.add(s"${n}.axi4.json", s"""{"mapping":[${maps.mkString(",")}]}""")
|
2017-04-26 02:55:06 +02:00
|
|
|
}
|
2017-01-30 00:17:52 +01:00
|
|
|
|
2017-07-27 01:01:21 +02:00
|
|
|
// We need to keep the following state from A => D: (size, source)
|
2017-01-30 00:17:52 +01:00
|
|
|
// All of those fields could potentially require 0 bits (argh. Chisel.)
|
2017-04-26 02:55:06 +02:00
|
|
|
// We will pack all of that extra information into the user bits.
|
2017-01-30 00:17:52 +01:00
|
|
|
|
|
|
|
val sourceBits = log2Ceil(edgeIn.client.endSourceId)
|
|
|
|
val sizeBits = log2Ceil(edgeIn.maxLgSize+1)
|
2017-07-27 01:01:21 +02:00
|
|
|
val stateBits = sizeBits + sourceBits // could be 0
|
2017-04-26 02:55:06 +02:00
|
|
|
require (stateBits <= out.aw.bits.params.userBits)
|
2017-01-30 00:17:52 +01:00
|
|
|
|
|
|
|
val a_address = edgeIn.address(in.a.bits)
|
|
|
|
val a_source = in.a.bits.source
|
|
|
|
val a_size = edgeIn.size(in.a.bits)
|
|
|
|
val a_isPut = edgeIn.hasData(in.a.bits)
|
|
|
|
val a_last = edgeIn.last(in.a)
|
|
|
|
|
|
|
|
// Make sure the fields are within the bounds we assumed
|
|
|
|
assert (a_source < UInt(BigInt(1) << sourceBits))
|
|
|
|
assert (a_size < UInt(BigInt(1) << sizeBits))
|
|
|
|
|
|
|
|
// Carefully pack/unpack fields into the state we send
|
|
|
|
val baseEnd = 0
|
|
|
|
val (sourceEnd, sourceOff) = (sourceBits + baseEnd, baseEnd)
|
|
|
|
val (sizeEnd, sizeOff) = (sizeBits + sourceEnd, sourceEnd)
|
2017-07-27 01:01:21 +02:00
|
|
|
require (sizeEnd == stateBits)
|
2017-01-30 00:17:52 +01:00
|
|
|
|
2017-07-27 01:01:21 +02:00
|
|
|
val a_state = (a_source << sourceOff) | (a_size << sizeOff)
|
2017-01-30 00:17:52 +01:00
|
|
|
|
2017-04-26 02:55:06 +02:00
|
|
|
val r_state = out.r.bits.user.getOrElse(UInt(0))
|
2017-01-30 00:17:52 +01:00
|
|
|
val r_source = if (sourceBits > 0) r_state(sourceEnd-1, sourceOff) else UInt(0)
|
|
|
|
val r_size = if (sizeBits > 0) r_state(sizeEnd -1, sizeOff) else UInt(0)
|
|
|
|
|
2017-04-26 02:55:06 +02:00
|
|
|
val b_state = out.b.bits.user.getOrElse(UInt(0))
|
2017-01-30 00:17:52 +01:00
|
|
|
val b_source = if (sourceBits > 0) b_state(sourceEnd-1, sourceOff) else UInt(0)
|
|
|
|
val b_size = if (sizeBits > 0) b_state(sizeEnd -1, sizeOff) else UInt(0)
|
|
|
|
|
|
|
|
// We need these Queues because AXI4 queues are irrevocable
|
|
|
|
val depth = if (combinational) 1 else 2
|
|
|
|
val out_arw = Wire(Decoupled(new AXI4BundleARW(out.params)))
|
|
|
|
val out_w = Wire(out.w)
|
|
|
|
out.w <> Queue.irrevocable(out_w, entries=depth, flow=combinational)
|
|
|
|
val queue_arw = Queue.irrevocable(out_arw, entries=depth, flow=combinational)
|
|
|
|
|
|
|
|
// Fan out the ARW channel to AR and AW
|
|
|
|
out.ar.bits := queue_arw.bits
|
|
|
|
out.aw.bits := queue_arw.bits
|
|
|
|
out.ar.valid := queue_arw.valid && !queue_arw.bits.wen
|
|
|
|
out.aw.valid := queue_arw.valid && queue_arw.bits.wen
|
|
|
|
queue_arw.ready := Mux(queue_arw.bits.wen, out.aw.ready, out.ar.ready)
|
|
|
|
|
|
|
|
val beatBytes = edgeIn.manager.beatBytes
|
|
|
|
val maxSize = UInt(log2Ceil(beatBytes))
|
|
|
|
val doneAW = RegInit(Bool(false))
|
|
|
|
when (in.a.fire()) { doneAW := !a_last }
|
|
|
|
|
|
|
|
val arw = out_arw.bits
|
|
|
|
arw.wen := a_isPut
|
2017-06-06 01:54:00 +02:00
|
|
|
arw.id := sourceTable(a_source)
|
2017-01-30 00:17:52 +01:00
|
|
|
arw.addr := a_address
|
|
|
|
arw.len := UIntToOH1(a_size, AXI4Parameters.lenBits + log2Ceil(beatBytes)) >> log2Ceil(beatBytes)
|
|
|
|
arw.size := Mux(a_size >= maxSize, maxSize, a_size)
|
|
|
|
arw.burst := AXI4Parameters.BURST_INCR
|
|
|
|
arw.lock := UInt(0) // not exclusive (LR/SC unsupported b/c no forward progress guarantee)
|
|
|
|
arw.cache := UInt(0) // do not allow AXI to modify our transactions
|
|
|
|
arw.prot := AXI4Parameters.PROT_PRIVILEDGED
|
|
|
|
arw.qos := UInt(0) // no QoS
|
2017-04-26 02:55:06 +02:00
|
|
|
arw.user.foreach { _ := a_state }
|
2017-01-30 00:17:52 +01:00
|
|
|
|
2017-06-06 01:54:00 +02:00
|
|
|
val stall = sourceStall(in.a.bits.source)
|
2017-04-28 02:55:51 +02:00
|
|
|
in.a.ready := !stall && Mux(a_isPut, (doneAW || out_arw.ready) && out_w.ready, out_arw.ready)
|
|
|
|
out_arw.valid := !stall && in.a.valid && Mux(a_isPut, !doneAW && out_w.ready, Bool(true))
|
2017-01-30 00:17:52 +01:00
|
|
|
|
2017-04-28 02:55:51 +02:00
|
|
|
out_w.valid := !stall && in.a.valid && a_isPut && (doneAW || out_arw.ready)
|
2017-01-30 00:17:52 +01:00
|
|
|
out_w.bits.data := in.a.bits.data
|
|
|
|
out_w.bits.strb := in.a.bits.mask
|
|
|
|
out_w.bits.last := a_last
|
|
|
|
|
|
|
|
// R and B => D arbitration
|
|
|
|
val r_holds_d = RegInit(Bool(false))
|
|
|
|
when (out.r.fire()) { r_holds_d := !out.r.bits.last }
|
|
|
|
// Give R higher priority than B
|
|
|
|
val r_wins = out.r.valid || r_holds_d
|
|
|
|
|
|
|
|
out.r.ready := in.d.ready
|
2017-04-26 02:55:06 +02:00
|
|
|
out.b.ready := in.d.ready && !r_wins
|
|
|
|
in.d.valid := Mux(r_wins, out.r.valid, out.b.valid)
|
2017-01-30 00:17:52 +01:00
|
|
|
|
|
|
|
val r_error = out.r.bits.resp =/= AXI4Parameters.RESP_OKAY
|
2017-04-26 02:55:06 +02:00
|
|
|
val b_error = out.b.bits.resp =/= AXI4Parameters.RESP_OKAY
|
2017-01-30 00:17:52 +01:00
|
|
|
|
2017-07-27 01:01:21 +02:00
|
|
|
val r_d = edgeIn.AccessAck(r_source, r_size, UInt(0), r_error)
|
|
|
|
val b_d = edgeIn.AccessAck(b_source, b_size, b_error)
|
2017-01-30 00:17:52 +01:00
|
|
|
|
|
|
|
in.d.bits := Mux(r_wins, r_d, b_d)
|
|
|
|
in.d.bits.data := out.r.bits.data // avoid a costly Mux
|
|
|
|
|
2017-04-28 02:55:51 +02:00
|
|
|
// We need to track if any reads or writes are inflight for a given ID.
|
|
|
|
// If the opposite type arrives, we must stall until it completes.
|
|
|
|
val a_sel = UIntToOH(arw.id, edgeOut.master.endId).toBools
|
|
|
|
val d_sel = UIntToOH(Mux(r_wins, out.r.bits.id, out.b.bits.id), edgeOut.master.endId).toBools
|
|
|
|
val d_last = Mux(r_wins, out.r.bits.last, Bool(true))
|
2017-06-27 02:54:17 +02:00
|
|
|
// If FIFO was requested, ensure that R+W ordering is preserved
|
|
|
|
(a_sel zip d_sel zip idStall zip idCount) filter { case (_, n) => n.map(_ > 1).getOrElse(false) } foreach { case (((as, ds), s), n) =>
|
|
|
|
val count = RegInit(UInt(0, width = log2Ceil(n.get + 1)))
|
2017-04-28 02:55:51 +02:00
|
|
|
val write = Reg(Bool())
|
|
|
|
val idle = count === UInt(0)
|
|
|
|
|
|
|
|
val inc = as && out_arw.fire()
|
2017-05-08 09:00:40 +02:00
|
|
|
val dec = ds && d_last && in.d.fire()
|
2017-04-28 02:55:51 +02:00
|
|
|
count := count + inc.asUInt - dec.asUInt
|
|
|
|
|
2017-06-27 02:54:17 +02:00
|
|
|
assert (!dec || count =/= UInt(0)) // underflow
|
|
|
|
assert (!inc || count =/= UInt(n.get)) // overflow
|
2017-04-28 02:55:51 +02:00
|
|
|
|
|
|
|
when (inc) { write := arw.wen }
|
2017-06-06 01:54:00 +02:00
|
|
|
s := !idle && write =/= arw.wen
|
2017-04-28 02:55:51 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 00:17:52 +01:00
|
|
|
// Tie off unused channels
|
|
|
|
in.b.valid := Bool(false)
|
|
|
|
in.c.ready := Bool(true)
|
|
|
|
in.e.ready := Bool(true)
|
2016-10-09 06:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object TLToAXI4
|
|
|
|
{
|
2017-10-27 09:45:21 +02:00
|
|
|
def apply(combinational: Boolean = true, adapterName: Option[String] = None, stripBits: Int = 0)(implicit p: Parameters) =
|
|
|
|
LazyModule(new TLToAXI4(combinational, adapterName, stripBits)).node
|
2017-06-02 03:34:04 +02:00
|
|
|
|
|
|
|
def sortByType(a: TLClientParameters, b: TLClientParameters): Boolean = {
|
|
|
|
if ( a.supportsProbe && !b.supportsProbe) return false
|
|
|
|
if (!a.supportsProbe && b.supportsProbe) return true
|
|
|
|
if ( a.requestFifo && !b.requestFifo ) return false
|
|
|
|
if (!a.requestFifo && b.requestFifo ) return true
|
|
|
|
return false
|
|
|
|
}
|
2016-10-09 06:36:32 +02:00
|
|
|
}
|