2016-05-10 09:25:13 +02:00
|
|
|
// See LICENSE for license details.
|
|
|
|
|
2016-06-28 20:21:38 +02:00
|
|
|
package uncore.devices
|
2016-05-10 09:25:13 +02:00
|
|
|
|
|
|
|
import Chisel._
|
|
|
|
import Chisel.ImplicitConversions._
|
|
|
|
|
|
|
|
import junctions._
|
2016-10-26 22:52:23 +02:00
|
|
|
import regmapper._
|
|
|
|
import uncore.tilelink2._
|
2016-05-10 09:25:13 +02:00
|
|
|
import cde.Parameters
|
|
|
|
|
|
|
|
class GatewayPLICIO extends Bundle {
|
|
|
|
val valid = Bool(OUTPUT)
|
|
|
|
val ready = Bool(INPUT)
|
|
|
|
val complete = Bool(INPUT)
|
|
|
|
}
|
|
|
|
|
|
|
|
class LevelGateway extends Module {
|
|
|
|
val io = new Bundle {
|
|
|
|
val interrupt = Bool(INPUT)
|
|
|
|
val plic = new GatewayPLICIO
|
|
|
|
}
|
|
|
|
|
|
|
|
val inFlight = Reg(init=Bool(false))
|
|
|
|
when (io.interrupt && io.plic.ready) { inFlight := true }
|
|
|
|
when (io.plic.complete) { inFlight := false }
|
|
|
|
io.plic.valid := io.interrupt && !inFlight
|
|
|
|
}
|
|
|
|
|
2016-10-27 22:09:11 +02:00
|
|
|
object PLICConsts
|
|
|
|
{
|
2016-05-10 09:25:13 +02:00
|
|
|
def maxDevices = 1023
|
2016-06-06 05:33:51 +02:00
|
|
|
def maxHarts = 15872
|
2016-10-26 22:52:23 +02:00
|
|
|
def priorityBase = 0x0
|
2016-06-06 05:33:51 +02:00
|
|
|
def pendingBase = 0x1000
|
|
|
|
def enableBase = 0x2000
|
|
|
|
def hartBase = 0x200000
|
|
|
|
|
|
|
|
def claimOffset = 4
|
|
|
|
def priorityBytes = 4
|
2016-05-10 09:25:13 +02:00
|
|
|
|
2016-10-27 22:09:11 +02:00
|
|
|
def enableOffset(i: Int) = i * ((maxDevices+7)/8)
|
|
|
|
def hartOffset(i: Int) = i * 0x1000
|
|
|
|
def enableBase(i: Int):Int = enableOffset(i) + enableBase
|
|
|
|
def hartBase(i: Int):Int = hartOffset(i) + hartBase
|
|
|
|
|
|
|
|
def size = hartBase(maxHarts)
|
|
|
|
require(hartBase >= enableBase(maxHarts))
|
|
|
|
}
|
|
|
|
|
|
|
|
case class PLICConfig(nHartsIn: Int, supervisor: Boolean, nDevices: Int, nPriorities: Int) {
|
|
|
|
import PLICConsts._
|
|
|
|
|
|
|
|
def contextsPerHart = if (supervisor) 2 else 1
|
|
|
|
def nHarts = contextsPerHart * nHartsIn
|
|
|
|
def context(i: Int, mode: Char) = mode match {
|
|
|
|
case 'M' => i * contextsPerHart
|
|
|
|
case 'S' => require(supervisor); i * contextsPerHart + 1
|
|
|
|
}
|
|
|
|
def claimAddr(i: Int, mode: Char) = hartBase(context(i, mode)) + claimOffset
|
|
|
|
def threshAddr(i: Int, mode: Char) = hartBase(context(i, mode))
|
|
|
|
def enableAddr(i: Int, mode: Char) = enableBase(context(i, mode))
|
|
|
|
|
2016-09-07 20:20:21 +02:00
|
|
|
require(nDevices <= maxDevices)
|
2016-05-10 09:25:13 +02:00
|
|
|
require(nHarts > 0 && nHarts <= maxHarts)
|
2016-06-02 22:48:29 +02:00
|
|
|
require(nPriorities >= 0 && nPriorities <= nDevices)
|
2016-05-10 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 22:52:23 +02:00
|
|
|
trait HasPLICParamters {
|
2016-10-27 22:09:11 +02:00
|
|
|
val params: (() => PLICConfig, Parameters)
|
|
|
|
val cfg = params._1 ()
|
2016-10-26 22:52:23 +02:00
|
|
|
implicit val p = params._2
|
|
|
|
}
|
|
|
|
|
|
|
|
trait PLICBundle extends Bundle with HasPLICParamters {
|
|
|
|
val devices = Vec(cfg.nDevices, new GatewayPLICIO).flip
|
|
|
|
val harts = Vec(cfg.nHarts, Bool()).asOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
trait PLICModule extends Module with HasRegMap with HasPLICParamters {
|
|
|
|
val io: PLICBundle
|
2016-05-10 09:25:13 +02:00
|
|
|
|
2016-06-02 22:48:29 +02:00
|
|
|
val priority =
|
|
|
|
if (cfg.nPriorities > 0) Reg(Vec(cfg.nDevices+1, UInt(width=log2Up(cfg.nPriorities+1))))
|
|
|
|
else Wire(init=Vec.fill(cfg.nDevices+1)(UInt(1)))
|
2016-06-06 05:33:51 +02:00
|
|
|
val threshold =
|
|
|
|
if (cfg.nPriorities > 0) Reg(Vec(cfg.nHarts, UInt(width = log2Up(cfg.nPriorities+1))))
|
|
|
|
else Wire(init=Vec.fill(cfg.nHarts)(UInt(0)))
|
2016-05-10 09:25:13 +02:00
|
|
|
val pending = Reg(init=Vec.fill(cfg.nDevices+1){Bool(false)})
|
2016-06-06 05:33:51 +02:00
|
|
|
val enables = Reg(Vec(cfg.nHarts, Vec(cfg.nDevices+1, Bool())))
|
2016-05-10 09:25:13 +02:00
|
|
|
|
|
|
|
for ((p, g) <- pending.tail zip io.devices) {
|
|
|
|
g.ready := !p
|
|
|
|
g.complete := false
|
|
|
|
when (g.valid) { p := true }
|
|
|
|
}
|
|
|
|
|
|
|
|
def findMax(x: Seq[UInt]): (UInt, UInt) = {
|
|
|
|
if (x.length > 1) {
|
|
|
|
val half = 1 << (log2Ceil(x.length) - 1)
|
|
|
|
val lMax = findMax(x take half)
|
|
|
|
val rMax = findMax(x drop half)
|
|
|
|
val useLeft = lMax._1 >= rMax._1
|
2016-10-26 22:52:23 +02:00
|
|
|
(Mux(useLeft, lMax._1, rMax._1), Mux(useLeft, lMax._2, UInt(half) | rMax._2))
|
2016-05-10 09:25:13 +02:00
|
|
|
} else (x.head, UInt(0))
|
|
|
|
}
|
|
|
|
|
2016-10-28 06:35:16 +02:00
|
|
|
val maxDevs = Reg(Vec(cfg.nHarts, UInt(width = log2Up(pending.size))))
|
2016-05-10 09:25:13 +02:00
|
|
|
for (hart <- 0 until cfg.nHarts) {
|
|
|
|
val effectivePriority =
|
2016-06-06 05:33:51 +02:00
|
|
|
for (((p, en), pri) <- (pending zip enables(hart) zip priority).tail)
|
2016-05-10 09:25:13 +02:00
|
|
|
yield Cat(p && en, pri)
|
2016-06-06 05:33:51 +02:00
|
|
|
val (maxPri, maxDev) = findMax((UInt(1) << priority(0).getWidth) +: effectivePriority)
|
2016-05-10 09:25:13 +02:00
|
|
|
|
2016-10-28 06:35:16 +02:00
|
|
|
maxDevs(hart) := maxDev
|
2016-06-06 05:33:51 +02:00
|
|
|
io.harts(hart) := Reg(next = maxPri) > Cat(UInt(1), threshold(hart))
|
2016-05-10 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 22:52:23 +02:00
|
|
|
def priorityRegField(x: UInt) = if (cfg.nPriorities > 0) RegField(32, x) else RegField.r(32, x)
|
2016-10-27 22:09:11 +02:00
|
|
|
val piorityRegFields = Seq(PLICConsts.priorityBase -> priority.map(p => priorityRegField(p)))
|
|
|
|
val pendingRegFields = Seq(PLICConsts.pendingBase -> pending .map(b => RegField.r(1, b)))
|
2016-10-26 22:52:23 +02:00
|
|
|
|
|
|
|
val enableRegFields = enables.zipWithIndex.map { case (e, i) =>
|
2016-10-27 22:09:11 +02:00
|
|
|
PLICConsts.enableBase(i) -> e.map(b => RegField(1, b))
|
2016-10-26 22:52:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val hartRegFields = Seq.tabulate(cfg.nHarts) { i =>
|
2016-10-27 22:09:11 +02:00
|
|
|
PLICConsts.hartBase(i) -> Seq(
|
2016-10-26 22:52:23 +02:00
|
|
|
priorityRegField(threshold(i)),
|
|
|
|
RegField(32,
|
|
|
|
RegReadFn { valid =>
|
|
|
|
when (valid) {
|
|
|
|
pending(maxDevs(i)) := Bool(false)
|
|
|
|
maxDevs(i) := UInt(0) // flush pipeline
|
|
|
|
}
|
|
|
|
(Bool(true), maxDevs(i))
|
|
|
|
},
|
|
|
|
RegWriteFn { (valid, data) =>
|
|
|
|
when (valid && enables(i)(data)) {
|
|
|
|
io.devices(data - UInt(1)).complete := Bool(true)
|
|
|
|
}
|
|
|
|
Bool(true)
|
2016-05-10 09:25:13 +02:00
|
|
|
}
|
2016-10-26 22:52:23 +02:00
|
|
|
)
|
|
|
|
)
|
2016-05-10 09:25:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 22:52:23 +02:00
|
|
|
regmap((piorityRegFields ++ pendingRegFields ++ enableRegFields ++ hartRegFields):_*)
|
|
|
|
|
2016-05-10 09:25:13 +02:00
|
|
|
priority(0) := 0
|
|
|
|
pending(0) := false
|
2016-06-06 05:33:51 +02:00
|
|
|
for (e <- enables)
|
|
|
|
e(0) := false
|
2016-05-10 09:25:13 +02:00
|
|
|
}
|
2016-10-26 22:52:23 +02:00
|
|
|
|
|
|
|
/** Platform-Level Interrupt Controller */
|
2016-10-27 22:09:11 +02:00
|
|
|
class TLPLIC(c: () => PLICConfig, address: BigInt = 0xC000000)(implicit val p: Parameters)
|
|
|
|
extends TLRegisterRouter(address, size = PLICConsts.size, beatBytes = p(rocket.XLen)/8, undefZero = false)(
|
2016-10-26 22:52:23 +02:00
|
|
|
new TLRegBundle((c, p), _) with PLICBundle)(
|
|
|
|
new TLRegModule((c, p), _, _) with PLICModule)
|