From dd06c8a68f593b641d71bd19e0f6abd9be3c9a6a Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Thu, 2 Feb 2023 13:23:10 -0800 Subject: [PATCH 01/17] Bump boom --- generators/boom | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/boom b/generators/boom index 98487c68cc..a9c944859d 160000 --- a/generators/boom +++ b/generators/boom @@ -1 +1 @@ -Subproject commit 98487c68cc4229cd991eac6a4f94d2fe8e5fdc1d +Subproject commit a9c944859d4ff5b934e2ddc092bf22cb283b9bf4 From 61654456e691d9602cc8effb158d9a4a90f036ee Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Thu, 2 Feb 2023 13:24:14 -0800 Subject: [PATCH 02/17] Bump spike --- generators/chipyard/src/main/resources/csrc/spiketile.cc | 4 ++-- toolchains/riscv-tools/riscv-isa-sim | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/generators/chipyard/src/main/resources/csrc/spiketile.cc b/generators/chipyard/src/main/resources/csrc/spiketile.cc index 06b57ab3f9..bedc41f346 100644 --- a/generators/chipyard/src/main/resources/csrc/spiketile.cc +++ b/generators/chipyard/src/main/resources/csrc/spiketile.cc @@ -260,11 +260,11 @@ extern "C" void spike_tile(int hartid, char* isa, "vlen:128,elen:64", false, endianness_little, - false, pmpregions, std::vector(), std::vector(), - false); + false, + 0); processor_t* p = new processor_t(isa_parser, cfg, simif, diff --git a/toolchains/riscv-tools/riscv-isa-sim b/toolchains/riscv-tools/riscv-isa-sim index 0eec0c9119..e2a364adfd 160000 --- a/toolchains/riscv-tools/riscv-isa-sim +++ b/toolchains/riscv-tools/riscv-isa-sim @@ -1 +1 @@ -Subproject commit 0eec0c911933ccc722d4b4479a6c4b8ac68f2f52 +Subproject commit e2a364adfd65488623e4cb23e7dde43a52f66c30 From e33102ac0865944998d1b4633a7444a9b5e14352 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Thu, 2 Feb 2023 13:25:19 -0800 Subject: [PATCH 03/17] Add spike-based cosim --- .../src/main/resources/csrc/cospike.cc | 241 ++++++++++++++++++ .../src/main/resources/vsrc/cospike.v | 78 ++++++ .../chipyard/src/main/scala/Cospike.scala | 104 ++++++++ .../chipyard/src/main/scala/DigitalTop.scala | 1 + .../src/main/scala/HarnessBinders.scala | 9 +- .../src/main/scala/config/BoomConfigs.scala | 6 + 6 files changed, 438 insertions(+), 1 deletion(-) create mode 100644 generators/chipyard/src/main/resources/csrc/cospike.cc create mode 100644 generators/chipyard/src/main/resources/vsrc/cospike.v create mode 100644 generators/chipyard/src/main/scala/Cospike.scala diff --git a/generators/chipyard/src/main/resources/csrc/cospike.cc b/generators/chipyard/src/main/resources/csrc/cospike.cc new file mode 100644 index 0000000000..2f6e74901d --- /dev/null +++ b/generators/chipyard/src/main/resources/csrc/cospike.cc @@ -0,0 +1,241 @@ +#include +#include +#include +#include +#include +#include +#include + +typedef struct system_info_t { + std::string isa; + int pmpregions; + uint64_t mem0_base; + uint64_t mem0_size; + int nharts; + std::vector bootrom; +}; + +system_info_t* info = NULL; +sim_t* sim = NULL; +reg_t tohost_addr = 0; +reg_t fromhost_addr = 0; +std::set magic_addrs; +cfg_t* cfg; + +static std::vector> make_mems(const std::vector &layout) +{ + std::vector> mems; + mems.reserve(layout.size()); + for (const auto &cfg : layout) { + mems.push_back(std::make_pair(cfg.get_base(), new mem_t(cfg.get_size()))); + } + return mems; +} + +extern "C" void cospike_set_sysinfo(char* isa, int pmpregions, + long long int mem0_base, long long int mem0_size, + int nharts, + char* bootrom + ) { + if (!info) { + info = new system_info_t; + info->isa = std::string(isa); + info->pmpregions = pmpregions; + info->mem0_base = mem0_base; + info->mem0_size = mem0_size; + info->nharts = nharts; + std::stringstream ss(bootrom); + std::string s; + while (ss >> s) { + info->bootrom.push_back(std::stoi(s)); + } + } +} + +extern "C" void cospike_cosim(long long int cycle, + long long int hartid, + int has_wdata, + int valid, + long long int iaddr, + unsigned long int insn, + int exception, + int interrupt, + unsigned long long int cause, + unsigned long long int wdata) +{ + assert(info); + if (!sim) { + printf("Configuring spike cosim\n"); + std::vector mem_cfg; + std::vector hartids; + mem_cfg.push_back(mem_cfg_t(info->mem0_base, info->mem0_size)); + for (int i = 0; i < info->nharts; i++) + hartids.push_back(i); + + cfg = new cfg_t(std::make_pair(0, 0), + nullptr, + info->isa.c_str(), + "MSU", + "vlen:128,elen:64", + false, + endianness_little, + info->pmpregions, + mem_cfg, + hartids, + false, + 0 + ); + + std::vector> mems = make_mems(cfg->mem_layout()); + rom_device_t *boot_rom = new rom_device_t(info->bootrom); + mem_t *boot_addr_reg = new mem_t(0x1000); + uint64_t default_boot_addr = 0x80000000; + std::vector> plugin_devices; + boot_addr_reg->store(0, 8, (const uint8_t*)(&default_boot_addr)); + plugin_devices.push_back(std::pair(0x10000, boot_rom)); + plugin_devices.push_back(std::pair(0x4000, boot_addr_reg)); + + s_vpi_vlog_info vinfo; + if (!vpi_get_vlog_info(&vinfo)) + abort(); + std::vector htif_args; + bool in_permissive = false; + bool cospike_debug = false; + for (int i = 1; i < vinfo.argc; i++) { + std::string arg(vinfo.argv[i]); + if (arg == "+permissive") { + in_permissive = true; + } else if (arg == "+permissive-off") { + in_permissive = false; + } else if (arg == "+cospike_debug") { + cospike_debug = true; + } else if (!in_permissive) { + htif_args.push_back(arg); + } + } + + debug_module_config_t dm_config = { + .progbufsize = 2, + .max_sba_data_width = 0, + .require_authentication = false, + .abstract_rti = 0, + .support_hasel = true, + .support_abstract_csr_access = true, + .support_abstract_fpr_access = true, + .support_haltgroups = true, + .support_impebreak = true + }; + + printf("%s\n", info->isa.c_str()); + for (int i = 0; i < htif_args.size(); i++) { + printf("%s\n", htif_args[i].c_str()); + } + + sim = new sim_t(cfg, false, + mems, + plugin_devices, + htif_args, + dm_config, + nullptr, + false, + nullptr, + false, + nullptr + ); + + sim->configure_log(true, true); + // Use our own reset vector + for (int i = 0; i < info->nharts; i++) { + sim->get_core(hartid)->get_state()->pc = 0x10040; + } + sim->set_debug(cospike_debug); + printf("Setting up htif for spike cosim\n"); + ((htif_t*)sim)->start(); + printf("Spike cosim started\n"); + tohost_addr = ((htif_t*)sim)->get_tohost_addr(); + fromhost_addr = ((htif_t*)sim)->get_fromhost_addr(); + printf("Tohost : %lx\n", tohost_addr); + printf("Fromhost: %lx\n", fromhost_addr); + } + + processor_t* p = sim->get_core(hartid); + state_t* s = p->get_state(); + uint64_t s_pc = s->pc; + if (interrupt) { + printf("%d interrupt %lx\n", cycle, cause); + uint64_t interrupt_cause = cause & 0x7FFFFFFFFFFFFFFF; + if (interrupt_cause == 3) { + s->mip->backdoor_write_with_mask(MIP_MSIP, MIP_MSIP); + } else { + printf("Unknown interrupt %lx\n", interrupt_cause); + } + } + if (exception) + printf("%d exception %lx\n", cycle, cause); + if (valid) { + printf("%d Cosim: %lx", cycle, iaddr); + if (has_wdata) { + printf(" %lx", wdata); + } + printf("\n"); + } + if (valid || interrupt || exception) + p->step(1); + + if (valid) { + if (s_pc != iaddr) { + printf("%d PC mismatch %lx != %lx\n", cycle, s_pc, iaddr); + exit(1); + } + + // Try to remember magic_mem addrs, and ignore these in the future + auto& mem_write = s->log_mem_write; + if (!mem_write.empty() && tohost_addr && std::get<0>(mem_write[0]) == tohost_addr) { + reg_t wdata = std::get<1>(mem_write[0]); + if (wdata >= info->mem0_base && wdata < (info->mem0_base + info->mem0_size)) { + printf("Probable magic mem %x\n", wdata); + magic_addrs.insert(wdata); + } + } + + if (has_wdata) { + auto& log = s->log_reg_write; + auto& mem_read = s->log_mem_read; + + for (auto regwrite : log) { + int rd = regwrite.first >> 4; + int type = regwrite.first & 0xf; + // 0 => int + // 1 => fp + // 2 => vec + // 3 => vec hint + // 4 => csr + if ((rd != 0 && type == 0) || type == 1) { + // Override reads from some CSRs + if ((insn & 0xfff0007f) == 0xf1300073 || // mimpid + (insn & 0xfff0007f) == 0xf1200073 || // marchid + (insn & 0xfff0007f) == 0xf1100073 || // mvendorid + (insn & 0xfff0007f) == 0xb0000073 || // mcycle + (insn & 0xfff0007f) == 0xb0200073 // minstret + ) { + printf("CSR override\n"); + s->XPR.write(rd, wdata); + } else if (!mem_read.empty() && + ((magic_addrs.count(std::get<0>(mem_read[0])) || + tohost_addr && std::get<0>(mem_read[0]) == tohost_addr) || + (fromhost_addr && std::get<0>(mem_read[0]) == fromhost_addr))) { + // Don't check reads from tohost, or reads from magic memory + // Technically this could be buggy because log_mem_read only reports vaddrs, but + // no software ever should access tohost/fromhost with vaddrs anyways + printf("To/From host read override\n"); + s->XPR.write(rd, wdata); + } else if (wdata != regwrite.second.v[0]) { + printf("%d wdata mismatch reg %d %lx != %lx\n", cycle, rd, regwrite.second.v[0], wdata); + exit(1); + } + } + } + } + } +} +// } diff --git a/generators/chipyard/src/main/resources/vsrc/cospike.v b/generators/chipyard/src/main/resources/vsrc/cospike.v new file mode 100644 index 0000000000..260506758d --- /dev/null +++ b/generators/chipyard/src/main/resources/vsrc/cospike.v @@ -0,0 +1,78 @@ +import "DPI-C" function void cospike_set_sysinfo( + input string isa, + input int pmpregions, + input longint mem0_base, + input longint mem0_size, + input int nharts, + input string bootrom + ); + +import "DPI-C" function void cospike_cosim(input longint cycle, + input longint hartid, + input bit has_wdata, + input bit valid, + input longint iaddr, + input int insn, + input bit exception, + input bit interrupt, + input longint cause, + input longint wdata + ); + + +module CospikeResources #( + parameter ISA, + parameter PMPREGIONS, + parameter MEM0_BASE, + parameter MEM0_SIZE, + parameter NHARTS, + parameter BOOTROM) + (); + initial begin + cospike_set_sysinfo(ISA, PMPREGIONS, MEM0_BASE, MEM0_SIZE, NHARTS, BOOTROM); + end; +endmodule; // CospikeResources + + +module SpikeCosim ( + input clock, + input reset, + + input [63:0] cycle, + + input [63:0] hartid, + + input trace_0_valid, + input [63:0] trace_0_iaddr, + input [31:0] trace_0_insn, + input trace_0_exception, + input trace_0_interrupt, + input [63:0] trace_0_cause, + input trace_0_has_wdata, + input [63:0] trace_0_wdata, + + input trace_1_valid, + input [63:0] trace_1_iaddr, + input [31:0] trace_1_insn, + input trace_1_exception, + input trace_1_interrupt, + input [63:0] trace_1_cause, + input trace_1_has_wdata, + input [63:0] trace_1_wdata + ); + + always @(posedge clock) begin + if (!reset) begin + if (trace_0_valid || trace_0_exception || trace_0_cause) begin + cospike_cosim(cycle, hartid, trace_0_has_wdata, trace_0_valid, trace_0_iaddr, + trace_0_insn, trace_0_exception, trace_0_interrupt, trace_0_cause, + trace_0_wdata); + end + if (trace_1_valid || trace_1_exception || trace_1_cause) begin + cospike_cosim(cycle, hartid, trace_1_has_wdata, trace_1_valid, trace_1_iaddr, + trace_1_insn, trace_1_exception, trace_1_interrupt, trace_1_cause, + trace_1_wdata); + end + end + end +endmodule; // CospikeCosim diff --git a/generators/chipyard/src/main/scala/Cospike.scala b/generators/chipyard/src/main/scala/Cospike.scala new file mode 100644 index 0000000000..1ebf62536f --- /dev/null +++ b/generators/chipyard/src/main/scala/Cospike.scala @@ -0,0 +1,104 @@ +package chipyard + +import chisel3._ +import chisel3.experimental.{IntParam, StringParam, IO} +import chisel3.util._ + +import freechips.rocketchip.config.{Parameters, Field, Config} +import freechips.rocketchip.subsystem._ +import freechips.rocketchip.diplomacy._ +import freechips.rocketchip.devices.tilelink._ +import freechips.rocketchip.util._ + +import testchipip.TileTraceIO + +class CospikeResources( + isa: String, + pmpregions: Int, + mem0_base: BigInt, + mem0_size: BigInt, + nharts: Int, + bootrom: String +) extends BlackBox(Map( + "ISA" -> StringParam(isa), + "PMPREGIONS" -> IntParam(pmpregions), + "MEM0_BASE" -> IntParam(mem0_base), + "MEM0_SIZE" -> IntParam(mem0_size), + "NHARTS" -> IntParam(nharts), + "BOOTROM" -> StringParam(bootrom) +)) with HasBlackBoxResource { + val io = IO(new Bundle {}) + addResource("/csrc/cospike.cc") + addResource("/vsrc/cospike.v") +} + +trait CanHaveSpikeCosim { this: ChipyardSystem => + InModuleBody { + val isa = tiles.headOption.map(_.isaDTS).getOrElse("") + val mem0_base = p(ExtMem).map(_.master.base).getOrElse(BigInt(0)) + val mem0_size = p(ExtMem).map(_.master.size).getOrElse(BigInt(0)) + val pmpregions = tiles.headOption.map(_.tileParams.core.nPMPs).getOrElse(0) + val nharts = tiles.size + val bootrom = bootROM.map(_.module.contents.toArray.mkString(" ")).getOrElse("") + val resources = Module(new CospikeResources(isa, pmpregions, mem0_base, mem0_size, nharts, bootrom)) + } +} + +class SpikeCosim extends BlackBox with HasBlackBoxResource +{ + addResource("/csrc/cospike.cc") + addResource("/vsrc/cospike.v") + val io = IO(new Bundle { + val clock = Input(Clock()) + val reset = Input(Bool()) + val cycle = Input(UInt(64.W)) + val hartid = Input(UInt(64.W)) + val trace = Input(Vec(2, new Bundle { + val valid = Bool() + val iaddr = UInt(64.W) + val insn = UInt(32.W) + val exception = Bool() + val interrupt = Bool() + val cause = UInt(64.W) + val has_wdata = Bool() + val wdata = UInt(64.W) + })) + }) +} + +object SpikeCosim +{ + def apply(trace: TileTraceIO, hartid: Int) = { + val cosim = Module(new SpikeCosim) + val cycle = withClockAndReset(trace.clock, trace.reset) { + val r = RegInit(0.U(64.W)) + r := r + 1.U + r + } + cosim.io.clock := trace.clock + cosim.io.reset := trace.reset + require(trace.numInsns <= 2) + cosim.io.cycle := cycle + cosim.io.trace.map(t => { + t.valid := false.B + t.iaddr := 0.U + t.insn := 0.U + t.exception := false.B + t.interrupt := false.B + t.cause := 0.U + }) + cosim.io.hartid := hartid.U + for (i <- 0 until trace.numInsns) { + cosim.io.trace(i).valid := trace.insns(i).valid + val signed = Wire(SInt(64.W)) + signed := trace.insns(i).iaddr.asSInt + cosim.io.trace(i).iaddr := signed.asUInt + cosim.io.trace(i).insn := trace.insns(i).insn + cosim.io.trace(i).exception := trace.insns(i).exception + cosim.io.trace(i).interrupt := trace.insns(i).interrupt + cosim.io.trace(i).cause := trace.insns(i).cause + cosim.io.trace(i).has_wdata := trace.insns(i).wdata.isDefined.B + cosim.io.trace(i).wdata := trace.insns(i).wdata.getOrElse(0.U) + } + } +} diff --git a/generators/chipyard/src/main/scala/DigitalTop.scala b/generators/chipyard/src/main/scala/DigitalTop.scala index 2777ba3608..dc01d200e6 100644 --- a/generators/chipyard/src/main/scala/DigitalTop.scala +++ b/generators/chipyard/src/main/scala/DigitalTop.scala @@ -34,6 +34,7 @@ class DigitalTop(implicit p: Parameters) extends ChipyardSystem with chipyard.clocking.HasChipyardPRCI // Use Chipyard reset/clock distribution with fftgenerator.CanHavePeripheryFFT // Enables optionally having an MMIO-based FFT block with constellation.soc.CanHaveGlobalNoC // Support instantiating a global NoC interconnect + with chipyard.CanHaveSpikeCosim // Support instantiating spike-based co-simulation { override lazy val module = new DigitalTopModule(this) } diff --git a/generators/chipyard/src/main/scala/HarnessBinders.scala b/generators/chipyard/src/main/scala/HarnessBinders.scala index 5ff78a65d5..627644c337 100644 --- a/generators/chipyard/src/main/scala/HarnessBinders.scala +++ b/generators/chipyard/src/main/scala/HarnessBinders.scala @@ -21,7 +21,7 @@ import barstools.iocell.chisel._ import testchipip._ -import chipyard.{HasHarnessSignalReferences, HarnessClockInstantiatorKey} +import chipyard._ import chipyard.clocking.{HasChipyardPRCI} import chipyard.iobinders.{GetSystemParameters, JTAGChipIO, ClockWithFreq} @@ -333,6 +333,13 @@ class WithSimDromajoBridge extends ComposeHarnessBinder({ } }) +class WithCospikeBridge extends ComposeHarnessBinder({ + (system: CanHaveTraceIOModuleImp, th: HasHarnessSignalReferences, ports: Seq[TraceOutputTop]) => { + ports.map { p => p.traces.zipWithIndex.map(t => SpikeCosim(t._1, t._2)) } + } +}) + + class WithCustomBootPinPlusArg extends OverrideHarnessBinder({ (system: CanHavePeripheryCustomBootPin, th: HasHarnessSignalReferences, ports: Seq[Bool]) => { val pin = PlusArg("custom_boot_pin", width=1) diff --git a/generators/chipyard/src/main/scala/config/BoomConfigs.scala b/generators/chipyard/src/main/scala/config/BoomConfigs.scala index bbc816f77c..e212fd1a1f 100644 --- a/generators/chipyard/src/main/scala/config/BoomConfigs.scala +++ b/generators/chipyard/src/main/scala/config/BoomConfigs.scala @@ -54,3 +54,9 @@ class DromajoBoomConfig extends Config( new boom.common.WithNSmallBooms(1) ++ new chipyard.config.WithSystemBusWidth(128) ++ new chipyard.config.AbstractConfig) + +class MediumBoomCosimConfig extends Config( + new chipyard.harness.WithCospikeBridge ++ // attach spike-cosim + new chipyard.config.WithTraceIO ++ // enable the traceio + new boom.common.WithNMediumBooms(1) ++ + new chipyard.config.AbstractConfig) From 48e2ec6cc929376d651a03bcea97b4894fbc6be6 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Thu, 2 Feb 2023 13:27:10 -0800 Subject: [PATCH 04/17] Switch chipyard-boom CI to also test spike-cosim --- .github/scripts/defaults.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/defaults.sh b/.github/scripts/defaults.sh index e6671996c7..f1a2704928 100755 --- a/.github/scripts/defaults.sh +++ b/.github/scripts/defaults.sh @@ -44,7 +44,7 @@ mapping["chipyard-digitaltop"]=" TOP=DigitalTop" mapping["chipyard-streaming-fir"]=" CONFIG=StreamingFIRRocketConfig" mapping["chipyard-streaming-passthrough"]=" CONFIG=StreamingPassthroughRocketConfig" mapping["chipyard-hetero"]=" CONFIG=LargeBoomAndRocketConfig" -mapping["chipyard-boom"]=" CONFIG=SmallBoomConfig" +mapping["chipyard-boom"]=" CONFIG=MediumBoomCosimConfig" mapping["chipyard-spike"]=" CONFIG=SpikeConfig" mapping["chipyard-blkdev"]=" CONFIG=SimBlockDeviceRocketConfig" mapping["chipyard-hwacha"]=" CONFIG=HwachaRocketConfig" From d4c569cc8adc2cd451c0ea12e2424cc0e790f8f0 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Thu, 2 Feb 2023 15:00:20 -0800 Subject: [PATCH 05/17] Bump spike --- toolchains/riscv-tools/riscv-isa-sim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchains/riscv-tools/riscv-isa-sim b/toolchains/riscv-tools/riscv-isa-sim index e2a364adfd..f4035e2d87 160000 --- a/toolchains/riscv-tools/riscv-isa-sim +++ b/toolchains/riscv-tools/riscv-isa-sim @@ -1 +1 @@ -Subproject commit e2a364adfd65488623e4cb23e7dde43a52f66c30 +Subproject commit f4035e2d87358f97a50e61a5edc6055f77e88219 From 043526a855bcb072504f5270b924f21e8395824f Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Thu, 2 Feb 2023 15:59:03 -0800 Subject: [PATCH 06/17] Remove esp-tools from CI --- .github/workflows/chipyard-run-tests.yml | 29 ------------------------ 1 file changed, 29 deletions(-) diff --git a/.github/workflows/chipyard-run-tests.yml b/.github/workflows/chipyard-run-tests.yml index 750f35a231..ccac2b9822 100644 --- a/.github/workflows/chipyard-run-tests.yml +++ b/.github/workflows/chipyard-run-tests.yml @@ -320,7 +320,6 @@ jobs: uses: ./.github/actions/prepare-rtl with: group-key: "group-accels" - toolchain: "esp-tools" prepare-chipyard-tracegen: name: prepare-chipyard-tracegen @@ -689,7 +688,6 @@ jobs: with: group-key: "group-accels" project-key: "chipyard-sha3" - toolchain: "esp-tools" chipyard-streaming-fir-run-tests: name: chipyard-streaming-fir-run-tests @@ -737,30 +735,6 @@ jobs: group-key: "group-accels" project-key: "chipyard-streaming-passthrough" - chipyard-hwacha-run-tests: - name: chipyard-hwacha-run-tests - needs: prepare-chipyard-accels - runs-on: self-hosted - steps: - - name: Delete old checkout - run: | - ls -alh . - rm -rf ${{ github.workspace }}/* || true - rm -rf ${{ github.workspace }}/.* || true - ls -alh . - - name: Checkout - uses: actions/checkout@v3 - - name: Git workaround - uses: ./.github/actions/git-workaround - - name: Create conda env - uses: ./.github/actions/create-conda-env - - name: Run tests - uses: ./.github/actions/run-tests - with: - group-key: "group-accels" - project-key: "chipyard-hwacha" - toolchain: "esp-tools" - chipyard-gemmini-run-tests: name: chipyard-gemmini-run-tests needs: prepare-chipyard-accels @@ -783,7 +757,6 @@ jobs: with: group-key: "group-accels" project-key: "chipyard-gemmini" - toolchain: "esp-tools" chipyard-nvdla-run-tests: name: chipyard-nvdla-run-tests @@ -830,7 +803,6 @@ jobs: with: group-key: "group-accels" project-key: "chipyard-mempress" - toolchain: "esp-tools" tracegen-boom-run-tests: @@ -1043,7 +1015,6 @@ jobs: chipyard-sha3-run-tests, chipyard-streaming-fir-run-tests, chipyard-streaming-passthrough-run-tests, - chipyard-hwacha-run-tests, chipyard-gemmini-run-tests, chipyard-nvdla-run-tests, chipyard-mempress-run-tests, From 2968151d24ee318cbd8160a9dec6a73761d8b1fc Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Thu, 2 Feb 2023 19:07:56 -0800 Subject: [PATCH 07/17] Rename interrupt/exception to raise_interrupt/raise_exception Verilator takes issue with a parameter named "interrupt" in DPI calls --- generators/chipyard/src/main/resources/csrc/cospike.cc | 10 +++++----- generators/chipyard/src/main/resources/vsrc/cospike.v | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/generators/chipyard/src/main/resources/csrc/cospike.cc b/generators/chipyard/src/main/resources/csrc/cospike.cc index 2f6e74901d..7c8f249e20 100644 --- a/generators/chipyard/src/main/resources/csrc/cospike.cc +++ b/generators/chipyard/src/main/resources/csrc/cospike.cc @@ -58,8 +58,8 @@ extern "C" void cospike_cosim(long long int cycle, int valid, long long int iaddr, unsigned long int insn, - int exception, - int interrupt, + int raise_exception, + int raise_interrupt, unsigned long long int cause, unsigned long long int wdata) { @@ -161,7 +161,7 @@ extern "C" void cospike_cosim(long long int cycle, processor_t* p = sim->get_core(hartid); state_t* s = p->get_state(); uint64_t s_pc = s->pc; - if (interrupt) { + if (raise_interrupt) { printf("%d interrupt %lx\n", cycle, cause); uint64_t interrupt_cause = cause & 0x7FFFFFFFFFFFFFFF; if (interrupt_cause == 3) { @@ -170,7 +170,7 @@ extern "C" void cospike_cosim(long long int cycle, printf("Unknown interrupt %lx\n", interrupt_cause); } } - if (exception) + if (raise_exception) printf("%d exception %lx\n", cycle, cause); if (valid) { printf("%d Cosim: %lx", cycle, iaddr); @@ -179,7 +179,7 @@ extern "C" void cospike_cosim(long long int cycle, } printf("\n"); } - if (valid || interrupt || exception) + if (valid || raise_interrupt || raise_exception) p->step(1); if (valid) { diff --git a/generators/chipyard/src/main/resources/vsrc/cospike.v b/generators/chipyard/src/main/resources/vsrc/cospike.v index 260506758d..0b70d365f5 100644 --- a/generators/chipyard/src/main/resources/vsrc/cospike.v +++ b/generators/chipyard/src/main/resources/vsrc/cospike.v @@ -13,8 +13,8 @@ import "DPI-C" function void cospike_cosim(input longint cycle, input bit valid, input longint iaddr, input int insn, - input bit exception, - input bit interrupt, + input bit raise_exception, + input bit raise_interrupt, input longint cause, input longint wdata ); From 071ba7620913b25068d95f60efc1c7e45217c549 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Fri, 3 Feb 2023 11:27:14 -0800 Subject: [PATCH 08/17] Fix spike-cosim causing problems for firesim --- .../chipyard/src/main/scala/Cospike.scala | 20 +++++++++++-------- .../src/main/scala/config/BoomConfigs.scala | 1 + .../config/fragments/TileFragments.scala | 6 +++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/generators/chipyard/src/main/scala/Cospike.scala b/generators/chipyard/src/main/scala/Cospike.scala index 1ebf62536f..7adf3030e4 100644 --- a/generators/chipyard/src/main/scala/Cospike.scala +++ b/generators/chipyard/src/main/scala/Cospike.scala @@ -32,15 +32,19 @@ class CospikeResources( addResource("/vsrc/cospike.v") } +case object SpikeCosimKey extends Field[Boolean](false) + trait CanHaveSpikeCosim { this: ChipyardSystem => - InModuleBody { - val isa = tiles.headOption.map(_.isaDTS).getOrElse("") - val mem0_base = p(ExtMem).map(_.master.base).getOrElse(BigInt(0)) - val mem0_size = p(ExtMem).map(_.master.size).getOrElse(BigInt(0)) - val pmpregions = tiles.headOption.map(_.tileParams.core.nPMPs).getOrElse(0) - val nharts = tiles.size - val bootrom = bootROM.map(_.module.contents.toArray.mkString(" ")).getOrElse("") - val resources = Module(new CospikeResources(isa, pmpregions, mem0_base, mem0_size, nharts, bootrom)) + if (p(SpikeCosimKey)) { + InModuleBody { + val isa = tiles.headOption.map(_.isaDTS).getOrElse("") + val mem0_base = p(ExtMem).map(_.master.base).getOrElse(BigInt(0)) + val mem0_size = p(ExtMem).map(_.master.size).getOrElse(BigInt(0)) + val pmpregions = tiles.headOption.map(_.tileParams.core.nPMPs).getOrElse(0) + val nharts = tiles.size + val bootrom = bootROM.map(_.module.contents.toArray.mkString(" ")).getOrElse("") + val resources = Module(new CospikeResources(isa, pmpregions, mem0_base, mem0_size, nharts, bootrom)) + } } } diff --git a/generators/chipyard/src/main/scala/config/BoomConfigs.scala b/generators/chipyard/src/main/scala/config/BoomConfigs.scala index e212fd1a1f..84011931fa 100644 --- a/generators/chipyard/src/main/scala/config/BoomConfigs.scala +++ b/generators/chipyard/src/main/scala/config/BoomConfigs.scala @@ -57,6 +57,7 @@ class DromajoBoomConfig extends Config( class MediumBoomCosimConfig extends Config( new chipyard.harness.WithCospikeBridge ++ // attach spike-cosim + new chipyard.config.EnableSpikeCosim ++ // enable co-sim new chipyard.config.WithTraceIO ++ // enable the traceio new boom.common.WithNMediumBooms(1) ++ new chipyard.config.AbstractConfig) diff --git a/generators/chipyard/src/main/scala/config/fragments/TileFragments.scala b/generators/chipyard/src/main/scala/config/fragments/TileFragments.scala index eac274f090..e0367b4de3 100644 --- a/generators/chipyard/src/main/scala/config/fragments/TileFragments.scala +++ b/generators/chipyard/src/main/scala/config/fragments/TileFragments.scala @@ -9,7 +9,7 @@ import freechips.rocketchip.rocket.{RocketCoreParams, MulDivParams, DCacheParams import boom.common.{BoomTileAttachParams} import cva6.{CVA6TileAttachParams} - +import chipyard.{SpikeCosimKey} import testchipip._ class WithL2TLBs(entries: Int) extends Config((site, here, up) => { @@ -79,3 +79,7 @@ class WithRocketDCacheScratchpad extends Config((site, here, up) => { )) } }) + +class EnableSpikeCosim extends Config((site, here, up) => { + case SpikeCosimKey => true +}) From b88e583472a426e733bff8734d590cb0550c1c43 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Sat, 4 Feb 2023 11:48:59 -0800 Subject: [PATCH 09/17] Fix pmp cosim --- .../chipyard/src/main/resources/csrc/cospike.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/generators/chipyard/src/main/resources/csrc/cospike.cc b/generators/chipyard/src/main/resources/csrc/cospike.cc index 7c8f249e20..4e81b708dd 100644 --- a/generators/chipyard/src/main/resources/csrc/cospike.cc +++ b/generators/chipyard/src/main/resources/csrc/cospike.cc @@ -212,12 +212,16 @@ extern "C" void cospike_cosim(long long int cycle, // 4 => csr if ((rd != 0 && type == 0) || type == 1) { // Override reads from some CSRs - if ((insn & 0xfff0007f) == 0xf1300073 || // mimpid - (insn & 0xfff0007f) == 0xf1200073 || // marchid - (insn & 0xfff0007f) == 0xf1100073 || // mvendorid - (insn & 0xfff0007f) == 0xb0000073 || // mcycle - (insn & 0xfff0007f) == 0xb0200073 // minstret - ) { + uint64_t csr_addr = insn >> 20; + bool csr_read = (insn & 0x7f) == 0x73; + if (csr_read && ( + (csr_addr == 0xf13) || // mimpid + (csr_addr == 0xf12) || // marchid + (csr_addr == 0xf11) || // mvendorid + (csr_addr == 0xb00) || // mcycle + (csr_addr == 0xb02) || // minstret + (csr_addr >= 0x3b0 && csr_addr <= 0x3ef) // pmpaddr + )) { printf("CSR override\n"); s->XPR.write(rd, wdata); } else if (!mem_read.empty() && From e27f1a3195a4c8d67ca5509458b251f19330b411 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Sat, 4 Feb 2023 16:17:02 -0800 Subject: [PATCH 10/17] Make SpikeTile ipc a plusarg --- generators/chipyard/src/main/scala/SpikeTile.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/generators/chipyard/src/main/scala/SpikeTile.scala b/generators/chipyard/src/main/scala/SpikeTile.scala index 44a6229aa9..cc83a46c8a 100644 --- a/generators/chipyard/src/main/scala/SpikeTile.scala +++ b/generators/chipyard/src/main/scala/SpikeTile.scala @@ -16,7 +16,6 @@ import freechips.rocketchip.tile._ import freechips.rocketchip.prci.ClockSinkParameters case class SpikeCoreParams( - val maxInsnsPerCycle: Int = 10000 ) extends CoreParams { val useVM = true val useHypervisor = false @@ -305,7 +304,7 @@ class SpikeTileModuleImp(outer: SpikeTile) extends BaseTileModuleImp(outer) { spike.io.msip := int_bundle.msip spike.io.meip := int_bundle.meip spike.io.seip := int_bundle.seip.get - spike.io.ipc := outer.spikeTileParams.core.maxInsnsPerCycle.U + spike.io.ipc := PlusArg("spike-ipc", 10000, width=64) val blockBits = log2Ceil(p(CacheBlockBytes)) spike.io.icache.a.ready := icache_tl.a.ready From c505f3011fe17e138eeffed51a7197e821d52983 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Sat, 4 Feb 2023 16:17:53 -0800 Subject: [PATCH 11/17] Improve spiketile performance for CI --- .github/scripts/defaults.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/defaults.sh b/.github/scripts/defaults.sh index f1a2704928..576c690bc3 100755 --- a/.github/scripts/defaults.sh +++ b/.github/scripts/defaults.sh @@ -45,7 +45,7 @@ mapping["chipyard-streaming-fir"]=" CONFIG=StreamingFIRRocketConfig" mapping["chipyard-streaming-passthrough"]=" CONFIG=StreamingPassthroughRocketConfig" mapping["chipyard-hetero"]=" CONFIG=LargeBoomAndRocketConfig" mapping["chipyard-boom"]=" CONFIG=MediumBoomCosimConfig" -mapping["chipyard-spike"]=" CONFIG=SpikeConfig" +mapping["chipyard-spike"]=" CONFIG=SpikeFastUARTConfig EXTRA_SIM_FLAGS='+spike-ipc=10'" mapping["chipyard-blkdev"]=" CONFIG=SimBlockDeviceRocketConfig" mapping["chipyard-hwacha"]=" CONFIG=HwachaRocketConfig" mapping["chipyard-gemmini"]=" CONFIG=GemminiRocketConfig" From fc611ef507f795f8ffb9405ded2d2346aed8b7e3 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Sat, 4 Feb 2023 16:55:43 -0800 Subject: [PATCH 12/17] Fix CSR overrides in spike cosim --- generators/chipyard/src/main/resources/csrc/cospike.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/generators/chipyard/src/main/resources/csrc/cospike.cc b/generators/chipyard/src/main/resources/csrc/cospike.cc index 4e81b708dd..799f79929b 100644 --- a/generators/chipyard/src/main/resources/csrc/cospike.cc +++ b/generators/chipyard/src/main/resources/csrc/cospike.cc @@ -212,8 +212,9 @@ extern "C" void cospike_cosim(long long int cycle, // 4 => csr if ((rd != 0 && type == 0) || type == 1) { // Override reads from some CSRs - uint64_t csr_addr = insn >> 20; + uint64_t csr_addr = (insn >> 20) & 0xfff; bool csr_read = (insn & 0x7f) == 0x73; + if (csr_read) printf("CSR read %lx\n", csr_addr); if (csr_read && ( (csr_addr == 0xf13) || // mimpid (csr_addr == 0xf12) || // marchid From 2a2de5850f03cc76516ea827a7e563ff6dbe8ad5 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Sun, 5 Feb 2023 20:59:14 -0800 Subject: [PATCH 13/17] Implement cosim purely in a HarnessBindeR --- generators/chipyard/src/main/scala/Cospike.scala | 16 ---------------- .../chipyard/src/main/scala/DigitalTop.scala | 1 - .../chipyard/src/main/scala/HarnessBinders.scala | 12 +++++++++++- .../src/main/scala/config/BoomConfigs.scala | 3 +-- .../scala/config/fragments/TileFragments.scala | 4 ---- 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/generators/chipyard/src/main/scala/Cospike.scala b/generators/chipyard/src/main/scala/Cospike.scala index 7adf3030e4..14eb018dce 100644 --- a/generators/chipyard/src/main/scala/Cospike.scala +++ b/generators/chipyard/src/main/scala/Cospike.scala @@ -32,22 +32,6 @@ class CospikeResources( addResource("/vsrc/cospike.v") } -case object SpikeCosimKey extends Field[Boolean](false) - -trait CanHaveSpikeCosim { this: ChipyardSystem => - if (p(SpikeCosimKey)) { - InModuleBody { - val isa = tiles.headOption.map(_.isaDTS).getOrElse("") - val mem0_base = p(ExtMem).map(_.master.base).getOrElse(BigInt(0)) - val mem0_size = p(ExtMem).map(_.master.size).getOrElse(BigInt(0)) - val pmpregions = tiles.headOption.map(_.tileParams.core.nPMPs).getOrElse(0) - val nharts = tiles.size - val bootrom = bootROM.map(_.module.contents.toArray.mkString(" ")).getOrElse("") - val resources = Module(new CospikeResources(isa, pmpregions, mem0_base, mem0_size, nharts, bootrom)) - } - } -} - class SpikeCosim extends BlackBox with HasBlackBoxResource { addResource("/csrc/cospike.cc") diff --git a/generators/chipyard/src/main/scala/DigitalTop.scala b/generators/chipyard/src/main/scala/DigitalTop.scala index dc01d200e6..2777ba3608 100644 --- a/generators/chipyard/src/main/scala/DigitalTop.scala +++ b/generators/chipyard/src/main/scala/DigitalTop.scala @@ -34,7 +34,6 @@ class DigitalTop(implicit p: Parameters) extends ChipyardSystem with chipyard.clocking.HasChipyardPRCI // Use Chipyard reset/clock distribution with fftgenerator.CanHavePeripheryFFT // Enables optionally having an MMIO-based FFT block with constellation.soc.CanHaveGlobalNoC // Support instantiating a global NoC interconnect - with chipyard.CanHaveSpikeCosim // Support instantiating spike-based co-simulation { override lazy val module = new DigitalTopModule(this) } diff --git a/generators/chipyard/src/main/scala/HarnessBinders.scala b/generators/chipyard/src/main/scala/HarnessBinders.scala index 627644c337..f8e940449c 100644 --- a/generators/chipyard/src/main/scala/HarnessBinders.scala +++ b/generators/chipyard/src/main/scala/HarnessBinders.scala @@ -333,8 +333,18 @@ class WithSimDromajoBridge extends ComposeHarnessBinder({ } }) -class WithCospikeBridge extends ComposeHarnessBinder({ +class WithCospike extends ComposeHarnessBinder({ (system: CanHaveTraceIOModuleImp, th: HasHarnessSignalReferences, ports: Seq[TraceOutputTop]) => { + implicit val p = chipyard.iobinders.GetSystemParameters(system) + val chipyardSystem = system.asInstanceOf[ChipyardSystemModule[_]].outer.asInstanceOf[ChipyardSystem] + val tiles = chipyardSystem.tiles + val isa = tiles.headOption.map(_.isaDTS).getOrElse("") + val mem0_base = p(ExtMem).map(_.master.base).getOrElse(BigInt(0)) + val mem0_size = p(ExtMem).map(_.master.size).getOrElse(BigInt(0)) + val pmpregions = tiles.headOption.map(_.tileParams.core.nPMPs).getOrElse(0) + val nharts = tiles.size + val bootrom = chipyardSystem.bootROM.map(_.module.contents.toArray.mkString(" ")).getOrElse("") + val resources = Module(new CospikeResources(isa, pmpregions, mem0_base, mem0_size, nharts, bootrom)) ports.map { p => p.traces.zipWithIndex.map(t => SpikeCosim(t._1, t._2)) } } }) diff --git a/generators/chipyard/src/main/scala/config/BoomConfigs.scala b/generators/chipyard/src/main/scala/config/BoomConfigs.scala index 84011931fa..da224a9b3c 100644 --- a/generators/chipyard/src/main/scala/config/BoomConfigs.scala +++ b/generators/chipyard/src/main/scala/config/BoomConfigs.scala @@ -56,8 +56,7 @@ class DromajoBoomConfig extends Config( new chipyard.config.AbstractConfig) class MediumBoomCosimConfig extends Config( - new chipyard.harness.WithCospikeBridge ++ // attach spike-cosim - new chipyard.config.EnableSpikeCosim ++ // enable co-sim + new chipyard.harness.WithCospike ++ // attach spike-cosim new chipyard.config.WithTraceIO ++ // enable the traceio new boom.common.WithNMediumBooms(1) ++ new chipyard.config.AbstractConfig) diff --git a/generators/chipyard/src/main/scala/config/fragments/TileFragments.scala b/generators/chipyard/src/main/scala/config/fragments/TileFragments.scala index e0367b4de3..f19759cb46 100644 --- a/generators/chipyard/src/main/scala/config/fragments/TileFragments.scala +++ b/generators/chipyard/src/main/scala/config/fragments/TileFragments.scala @@ -9,7 +9,6 @@ import freechips.rocketchip.rocket.{RocketCoreParams, MulDivParams, DCacheParams import boom.common.{BoomTileAttachParams} import cva6.{CVA6TileAttachParams} -import chipyard.{SpikeCosimKey} import testchipip._ class WithL2TLBs(entries: Int) extends Config((site, here, up) => { @@ -80,6 +79,3 @@ class WithRocketDCacheScratchpad extends Config((site, here, up) => { } }) -class EnableSpikeCosim extends Config((site, here, up) => { - case SpikeCosimKey => true -}) From 6de7256299b645e313665c2e310cf8f8e04889fc Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Tue, 7 Feb 2023 11:55:52 -0800 Subject: [PATCH 14/17] Bump spike --- toolchains/riscv-tools/riscv-isa-sim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolchains/riscv-tools/riscv-isa-sim b/toolchains/riscv-tools/riscv-isa-sim index 461767199a..e7d6aff19a 160000 --- a/toolchains/riscv-tools/riscv-isa-sim +++ b/toolchains/riscv-tools/riscv-isa-sim @@ -1 +1 @@ -Subproject commit 461767199aac9c15894513254ae4844de5ee78d0 +Subproject commit e7d6aff19a071a059f1b9c2328ee4dac83bc677a From d5bf00aab112ec5025b4721bd21c863486c427a6 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Tue, 7 Feb 2023 14:04:55 -0800 Subject: [PATCH 15/17] Bump spike-cosim for spike changes --- .../src/main/resources/csrc/cospike.cc | 103 ++++++++++-------- 1 file changed, 57 insertions(+), 46 deletions(-) diff --git a/generators/chipyard/src/main/resources/csrc/cospike.cc b/generators/chipyard/src/main/resources/csrc/cospike.cc index 799f79929b..084e1b2877 100644 --- a/generators/chipyard/src/main/resources/csrc/cospike.cc +++ b/generators/chipyard/src/main/resources/csrc/cospike.cc @@ -6,6 +6,9 @@ #include #include +#define CLINT_BASE (0x2000000) +#define CLINT_SIZE (0x1000) + typedef struct system_info_t { std::string isa; int pmpregions; @@ -33,10 +36,10 @@ static std::vector> make_mems(const std::vectorisa = std::string(isa); @@ -54,14 +57,14 @@ extern "C" void cospike_set_sysinfo(char* isa, int pmpregions, extern "C" void cospike_cosim(long long int cycle, long long int hartid, - int has_wdata, - int valid, - long long int iaddr, - unsigned long int insn, - int raise_exception, - int raise_interrupt, - unsigned long long int cause, - unsigned long long int wdata) + int has_wdata, + int valid, + long long int iaddr, + unsigned long int insn, + int raise_exception, + int raise_interrupt, + unsigned long long int cause, + unsigned long long int wdata) { assert(info); if (!sim) { @@ -87,13 +90,20 @@ extern "C" void cospike_cosim(long long int cycle, ); std::vector> mems = make_mems(cfg->mem_layout()); + rom_device_t *boot_rom = new rom_device_t(info->bootrom); mem_t *boot_addr_reg = new mem_t(0x1000); uint64_t default_boot_addr = 0x80000000; - std::vector> plugin_devices; boot_addr_reg->store(0, 8, (const uint8_t*)(&default_boot_addr)); - plugin_devices.push_back(std::pair(0x10000, boot_rom)); + + // Don't actually build a clint + mem_t* clint_mem = new mem_t(CLINT_SIZE); + + std::vector> plugin_devices; + // The device map is hardcoded here for now plugin_devices.push_back(std::pair(0x4000, boot_addr_reg)); + plugin_devices.push_back(std::pair(0x10000, boot_rom)); + plugin_devices.push_back(std::pair(CLINT_BASE, clint_mem)); s_vpi_vlog_info vinfo; if (!vpi_get_vlog_info(&vinfo)) @@ -104,13 +114,13 @@ extern "C" void cospike_cosim(long long int cycle, for (int i = 1; i < vinfo.argc; i++) { std::string arg(vinfo.argv[i]); if (arg == "+permissive") { - in_permissive = true; + in_permissive = true; } else if (arg == "+permissive-off") { - in_permissive = false; + in_permissive = false; } else if (arg == "+cospike_debug") { cospike_debug = true; } else if (!in_permissive) { - htif_args.push_back(arg); + htif_args.push_back(arg); } } @@ -132,16 +142,16 @@ extern "C" void cospike_cosim(long long int cycle, } sim = new sim_t(cfg, false, - mems, - plugin_devices, - htif_args, - dm_config, - nullptr, - false, - nullptr, - false, - nullptr - ); + mems, + plugin_devices, + htif_args, + dm_config, + nullptr, + false, + nullptr, + false, + nullptr + ); sim->configure_log(true, true); // Use our own reset vector @@ -193,25 +203,25 @@ extern "C" void cospike_cosim(long long int cycle, if (!mem_write.empty() && tohost_addr && std::get<0>(mem_write[0]) == tohost_addr) { reg_t wdata = std::get<1>(mem_write[0]); if (wdata >= info->mem0_base && wdata < (info->mem0_base + info->mem0_size)) { - printf("Probable magic mem %x\n", wdata); - magic_addrs.insert(wdata); + printf("Probable magic mem %x\n", wdata); + magic_addrs.insert(wdata); } } if (has_wdata) { auto& log = s->log_reg_write; auto& mem_read = s->log_mem_read; - + reg_t mem_read_addr = mem_read.empty() ? 0 : std::get<0>(mem_read[0]); for (auto regwrite : log) { - int rd = regwrite.first >> 4; - int type = regwrite.first & 0xf; - // 0 => int - // 1 => fp - // 2 => vec - // 3 => vec hint - // 4 => csr - if ((rd != 0 && type == 0) || type == 1) { - // Override reads from some CSRs + int rd = regwrite.first >> 4; + int type = regwrite.first & 0xf; + // 0 => int + // 1 => fp + // 2 => vec + // 3 => vec hint + // 4 => csr + if ((rd != 0 && type == 0) || type == 1) { + // Override reads from some CSRs uint64_t csr_addr = (insn >> 20) & 0xfff; bool csr_read = (insn & 0x7f) == 0x73; if (csr_read) printf("CSR read %lx\n", csr_addr); @@ -225,14 +235,15 @@ extern "C" void cospike_cosim(long long int cycle, )) { printf("CSR override\n"); s->XPR.write(rd, wdata); - } else if (!mem_read.empty() && - ((magic_addrs.count(std::get<0>(mem_read[0])) || - tohost_addr && std::get<0>(mem_read[0]) == tohost_addr) || - (fromhost_addr && std::get<0>(mem_read[0]) == fromhost_addr))) { - // Don't check reads from tohost, or reads from magic memory + } else if (!mem_read.empty() && ((magic_addrs.count(mem_read_addr) || + (tohost_addr && mem_read_addr == tohost_addr) || + (fromhost_addr && mem_read_addr == fromhost_addr) || + (CLINT_BASE <= mem_read_addr && mem_read_addr < (CLINT_BASE + CLINT_SIZE)) + ))) { + // Don't check reads from tohost, reads from magic memory, or reads from clint // Technically this could be buggy because log_mem_read only reports vaddrs, but - // no software ever should access tohost/fromhost with vaddrs anyways - printf("To/From host read override\n"); + // no software ever should access tohost/fromhost/clint with vaddrs anyways + printf("Read override %lx\n", mem_read_addr); s->XPR.write(rd, wdata); } else if (wdata != regwrite.second.v[0]) { printf("%d wdata mismatch reg %d %lx != %lx\n", cycle, rd, regwrite.second.v[0], wdata); From 1766501795295d745d470eee85076276e3f87d1b Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Sat, 11 Feb 2023 12:29:45 -0800 Subject: [PATCH 16/17] Fix Spike DPI decoupled interface --- .../chipyard/src/main/scala/SpikeTile.scala | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/generators/chipyard/src/main/scala/SpikeTile.scala b/generators/chipyard/src/main/scala/SpikeTile.scala index cc83a46c8a..c7e5b69d9c 100644 --- a/generators/chipyard/src/main/scala/SpikeTile.scala +++ b/generators/chipyard/src/main/scala/SpikeTile.scala @@ -307,20 +307,26 @@ class SpikeTileModuleImp(outer: SpikeTile) extends BaseTileModuleImp(outer) { spike.io.ipc := PlusArg("spike-ipc", 10000, width=64) val blockBits = log2Ceil(p(CacheBlockBytes)) - spike.io.icache.a.ready := icache_tl.a.ready - icache_tl.a.valid := spike.io.icache.a.valid - icache_tl.a.bits := icacheEdge.Get( + + val icache_a_q = Module(new Queue(new TLBundleA(icacheEdge.bundle), 1, flow=true, pipe=true)) + spike.io.icache.a.ready := icache_a_q.io.enq.ready && icache_a_q.io.count === 0.U + icache_tl.a <> icache_a_q.io.deq + icache_a_q.io.enq.valid := spike.io.icache.a.valid + icache_a_q.io.enq.bits := icacheEdge.Get( fromSource = spike.io.icache.a.sourceid, toAddress = (spike.io.icache.a.address >> blockBits) << blockBits, lgSize = blockBits.U)._2 + icache_tl.d.ready := true.B spike.io.icache.d.valid := icache_tl.d.valid spike.io.icache.d.sourceid := icache_tl.d.bits.source spike.io.icache.d.data := icache_tl.d.bits.data.asTypeOf(Vec(8, UInt(64.W))) - spike.io.dcache.a.ready := dcache_tl.a.ready - dcache_tl.a.valid := spike.io.dcache.a.valid - dcache_tl.a.bits := dcacheEdge.AcquireBlock( + val dcache_a_q = Module(new Queue(new TLBundleA(dcacheEdge.bundle), 1, flow=true, pipe=true)) + spike.io.dcache.a.ready := dcache_a_q.io.enq.ready && dcache_a_q.io.count === 0.U + dcache_tl.a <> dcache_a_q.io.deq + dcache_a_q.io.enq.valid := spike.io.dcache.a.valid + dcache_a_q.io.enq.bits := dcacheEdge.AcquireBlock( fromSource = spike.io.dcache.a.sourceid, toAddress = (spike.io.dcache.a.address >> blockBits) << blockBits, lgSize = blockBits.U, @@ -332,9 +338,11 @@ class SpikeTileModuleImp(outer: SpikeTile) extends BaseTileModuleImp(outer) { spike.io.dcache.b.source := dcache_tl.b.bits.source spike.io.dcache.b.param := dcache_tl.b.bits.param - spike.io.dcache.c.ready := dcache_tl.c.ready - dcache_tl.c.valid := spike.io.dcache.c.valid - dcache_tl.c.bits := Mux(spike.io.dcache.c.voluntary, + val dcache_c_q = Module(new Queue(new TLBundleC(dcacheEdge.bundle), 1, flow=true, pipe=true)) + spike.io.dcache.c.ready := dcache_c_q.io.enq.ready && dcache_c_q.io.count === 0.U + dcache_tl.c <> dcache_c_q.io.deq + dcache_c_q.io.enq.valid := spike.io.dcache.c.valid + dcache_c_q.io.enq.bits := Mux(spike.io.dcache.c.voluntary, dcacheEdge.Release( fromSource = spike.io.dcache.c.sourceid, toAddress = spike.io.dcache.c.address, @@ -368,10 +376,12 @@ class SpikeTileModuleImp(outer: SpikeTile) extends BaseTileModuleImp(outer) { dcache_tl.e.valid := dcache_tl.d.valid && should_finish dcache_tl.e.bits := dcacheEdge.GrantAck(dcache_tl.d.bits) - spike.io.mmio.a.ready := mmio_tl.a.ready - mmio_tl.a.valid := spike.io.mmio.a.valid + val mmio_a_q = Module(new Queue(new TLBundleA(mmioEdge.bundle), 1, flow=true, pipe=true)) + spike.io.mmio.a.ready := mmio_a_q.io.enq.ready && mmio_a_q.io.count === 0.U + mmio_tl.a <> mmio_a_q.io.deq + mmio_a_q.io.enq.valid := spike.io.mmio.a.valid val log_size = MuxCase(0.U, (0 until 3).map { i => (spike.io.mmio.a.size === (1 << i).U) -> i.U }) - mmio_tl.a.bits := Mux(spike.io.mmio.a.store, + mmio_a_q.io.enq.bits := Mux(spike.io.mmio.a.store, mmioEdge.Put(0.U, spike.io.mmio.a.address, log_size, spike.io.mmio.a.data)._2, mmioEdge.Get(0.U, spike.io.mmio.a.address, log_size)._2) From 89090f6b909880e1218916e5351ee20d2836533f Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Wed, 15 Feb 2023 10:17:08 -0800 Subject: [PATCH 17/17] Remove need for separate SpikeCosimResources --- .../src/main/resources/vsrc/cospike.v | 66 +++++++++---------- .../chipyard/src/main/scala/Cospike.scala | 28 ++++---- .../src/main/scala/HarnessBinders.scala | 17 ++--- 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/generators/chipyard/src/main/resources/vsrc/cospike.v b/generators/chipyard/src/main/resources/vsrc/cospike.v index 0b70d365f5..f9d2322c54 100644 --- a/generators/chipyard/src/main/resources/vsrc/cospike.v +++ b/generators/chipyard/src/main/resources/vsrc/cospike.v @@ -20,46 +20,42 @@ import "DPI-C" function void cospike_cosim(input longint cycle, ); -module CospikeResources #( - parameter ISA, - parameter PMPREGIONS, - parameter MEM0_BASE, - parameter MEM0_SIZE, - parameter NHARTS, - parameter BOOTROM) - (); - initial begin - cospike_set_sysinfo(ISA, PMPREGIONS, MEM0_BASE, MEM0_SIZE, NHARTS, BOOTROM); - end; -endmodule; // CospikeResources - +module SpikeCosim #( + parameter ISA, + parameter PMPREGIONS, + parameter MEM0_BASE, + parameter MEM0_SIZE, + parameter NHARTS, + parameter BOOTROM) ( + input clock, + input reset, -module SpikeCosim ( - input clock, - input reset, + input [63:0] cycle, - input [63:0] cycle, + input [63:0] hartid, - input [63:0] hartid, + input trace_0_valid, + input [63:0] trace_0_iaddr, + input [31:0] trace_0_insn, + input trace_0_exception, + input trace_0_interrupt, + input [63:0] trace_0_cause, + input trace_0_has_wdata, + input [63:0] trace_0_wdata, - input trace_0_valid, - input [63:0] trace_0_iaddr, - input [31:0] trace_0_insn, - input trace_0_exception, - input trace_0_interrupt, - input [63:0] trace_0_cause, - input trace_0_has_wdata, - input [63:0] trace_0_wdata, + input trace_1_valid, + input [63:0] trace_1_iaddr, + input [31:0] trace_1_insn, + input trace_1_exception, + input trace_1_interrupt, + input [63:0] trace_1_cause, + input trace_1_has_wdata, + input [63:0] trace_1_wdata + ); - input trace_1_valid, - input [63:0] trace_1_iaddr, - input [31:0] trace_1_insn, - input trace_1_exception, - input trace_1_interrupt, - input [63:0] trace_1_cause, - input trace_1_has_wdata, - input [63:0] trace_1_wdata - ); + initial begin + cospike_set_sysinfo(ISA, PMPREGIONS, MEM0_BASE, MEM0_SIZE, NHARTS, BOOTROM); + end; always @(posedge clock) begin if (!reset) begin diff --git a/generators/chipyard/src/main/scala/Cospike.scala b/generators/chipyard/src/main/scala/Cospike.scala index 14eb018dce..7b663b2455 100644 --- a/generators/chipyard/src/main/scala/Cospike.scala +++ b/generators/chipyard/src/main/scala/Cospike.scala @@ -12,27 +12,23 @@ import freechips.rocketchip.util._ import testchipip.TileTraceIO -class CospikeResources( +case class SpikeCosimConfig( isa: String, pmpregions: Int, mem0_base: BigInt, mem0_size: BigInt, nharts: Int, bootrom: String -) extends BlackBox(Map( - "ISA" -> StringParam(isa), - "PMPREGIONS" -> IntParam(pmpregions), - "MEM0_BASE" -> IntParam(mem0_base), - "MEM0_SIZE" -> IntParam(mem0_size), - "NHARTS" -> IntParam(nharts), - "BOOTROM" -> StringParam(bootrom) -)) with HasBlackBoxResource { - val io = IO(new Bundle {}) - addResource("/csrc/cospike.cc") - addResource("/vsrc/cospike.v") -} +) -class SpikeCosim extends BlackBox with HasBlackBoxResource +class SpikeCosim(cfg: SpikeCosimConfig) extends BlackBox(Map( + "ISA" -> StringParam(cfg.isa), + "PMPREGIONS" -> IntParam(cfg.pmpregions), + "MEM0_BASE" -> IntParam(cfg.mem0_base), + "MEM0_SIZE" -> IntParam(cfg.mem0_size), + "NHARTS" -> IntParam(cfg.nharts), + "BOOTROM" -> StringParam(cfg.bootrom) +)) with HasBlackBoxResource { addResource("/csrc/cospike.cc") addResource("/vsrc/cospike.v") @@ -56,8 +52,8 @@ class SpikeCosim extends BlackBox with HasBlackBoxResource object SpikeCosim { - def apply(trace: TileTraceIO, hartid: Int) = { - val cosim = Module(new SpikeCosim) + def apply(trace: TileTraceIO, hartid: Int, cfg: SpikeCosimConfig) = { + val cosim = Module(new SpikeCosim(cfg)) val cycle = withClockAndReset(trace.clock, trace.reset) { val r = RegInit(0.U(64.W)) r := r + 1.U diff --git a/generators/chipyard/src/main/scala/HarnessBinders.scala b/generators/chipyard/src/main/scala/HarnessBinders.scala index f8e940449c..a47b08e0db 100644 --- a/generators/chipyard/src/main/scala/HarnessBinders.scala +++ b/generators/chipyard/src/main/scala/HarnessBinders.scala @@ -338,14 +338,15 @@ class WithCospike extends ComposeHarnessBinder({ implicit val p = chipyard.iobinders.GetSystemParameters(system) val chipyardSystem = system.asInstanceOf[ChipyardSystemModule[_]].outer.asInstanceOf[ChipyardSystem] val tiles = chipyardSystem.tiles - val isa = tiles.headOption.map(_.isaDTS).getOrElse("") - val mem0_base = p(ExtMem).map(_.master.base).getOrElse(BigInt(0)) - val mem0_size = p(ExtMem).map(_.master.size).getOrElse(BigInt(0)) - val pmpregions = tiles.headOption.map(_.tileParams.core.nPMPs).getOrElse(0) - val nharts = tiles.size - val bootrom = chipyardSystem.bootROM.map(_.module.contents.toArray.mkString(" ")).getOrElse("") - val resources = Module(new CospikeResources(isa, pmpregions, mem0_base, mem0_size, nharts, bootrom)) - ports.map { p => p.traces.zipWithIndex.map(t => SpikeCosim(t._1, t._2)) } + val cfg = SpikeCosimConfig( + isa = tiles.headOption.map(_.isaDTS).getOrElse(""), + mem0_base = p(ExtMem).map(_.master.base).getOrElse(BigInt(0)), + mem0_size = p(ExtMem).map(_.master.size).getOrElse(BigInt(0)), + pmpregions = tiles.headOption.map(_.tileParams.core.nPMPs).getOrElse(0), + nharts = tiles.size, + bootrom = chipyardSystem.bootROM.map(_.module.contents.toArray.mkString(" ")).getOrElse("") + ) + ports.map { p => p.traces.zipWithIndex.map(t => SpikeCosim(t._1, t._2, cfg)) } } })