2018-11-08 13:31:28 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2017, 2018 MINRES Technologies GmbH
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
2017-09-21 13:13:01 +02:00
|
|
|
|
2017-10-04 23:15:04 +02:00
|
|
|
#include <sysc/SiFive/plic.h>
|
2017-10-04 14:30:25 +02:00
|
|
|
|
2017-10-04 23:15:04 +02:00
|
|
|
#include <scc/report.h>
|
2018-11-08 13:31:28 +01:00
|
|
|
#include <scc/utilities.h>
|
2017-10-04 23:15:04 +02:00
|
|
|
#include <sysc/SiFive/gen/plic_regs.h>
|
2017-09-21 13:13:01 +02:00
|
|
|
|
|
|
|
namespace sysc {
|
|
|
|
|
|
|
|
plic::plic(sc_core::sc_module_name nm)
|
2017-09-26 17:10:10 +02:00
|
|
|
: sc_core::sc_module(nm)
|
|
|
|
, tlm_target<>(clk)
|
|
|
|
, NAMED(clk_i)
|
|
|
|
, NAMED(rst_i)
|
2017-10-04 23:15:04 +02:00
|
|
|
, NAMED(global_interrupts_i, 256)
|
|
|
|
, NAMED(core_interrupt_o)
|
2018-11-08 13:31:28 +01:00
|
|
|
, NAMEDD(regs, plic_regs)
|
2017-10-04 23:15:04 +02:00
|
|
|
|
|
|
|
{
|
2017-09-21 13:13:01 +02:00
|
|
|
regs->registerResources(*this);
|
2017-10-04 23:15:04 +02:00
|
|
|
// register callbacks
|
2019-06-28 22:43:17 +02:00
|
|
|
<<<<<<< HEAD
|
2019-04-11 07:40:02 +02:00
|
|
|
regs->claim_complete.set_write_cb([this](scc::sc_register<uint32_t>& reg, const uint32_t& v, sc_core::sc_time d) -> bool {
|
2019-06-28 22:43:17 +02:00
|
|
|
=======
|
2019-01-10 12:08:57 +01:00
|
|
|
regs->claim_complete.set_write_cb([this](scc::sc_register<uint32_t>& reg, uint32_t v, sc_core::sc_time d) -> bool {
|
2019-06-28 22:43:17 +02:00
|
|
|
>>>>>>> branch 'master' of https://git.minres.com/VP/RISCV.git
|
2018-11-08 13:31:28 +01:00
|
|
|
reg.put(v);
|
|
|
|
reset_pending_int(v);
|
|
|
|
// std::cout << "Value of register: 0x" << std::hex << reg << std::endl;
|
|
|
|
// todo: reset related interrupt and find next high-prio interrupt
|
|
|
|
return true;
|
|
|
|
});
|
2017-10-04 23:15:04 +02:00
|
|
|
|
|
|
|
// port callbacks
|
|
|
|
SC_METHOD(global_int_port_cb);
|
|
|
|
for (uint8_t i = 0; i < 255; i++) {
|
|
|
|
sensitive << global_interrupts_i[i].pos();
|
|
|
|
}
|
|
|
|
dont_initialize();
|
|
|
|
|
|
|
|
// register event callbacks
|
2017-09-21 13:13:01 +02:00
|
|
|
SC_METHOD(clock_cb);
|
2017-09-22 11:23:23 +02:00
|
|
|
sensitive << clk_i;
|
2017-09-21 13:13:01 +02:00
|
|
|
SC_METHOD(reset_cb);
|
2017-09-22 11:23:23 +02:00
|
|
|
sensitive << rst_i;
|
2017-10-04 10:31:11 +02:00
|
|
|
dont_initialize();
|
2017-09-21 13:13:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 13:31:28 +01:00
|
|
|
plic::~plic() {}// NOLINT
|
2017-10-04 23:15:04 +02:00
|
|
|
|
2018-11-08 13:31:28 +01:00
|
|
|
void plic::clock_cb() { this->clk = clk_i.read(); }
|
2017-09-21 13:13:01 +02:00
|
|
|
|
|
|
|
void plic::reset_cb() {
|
2017-09-22 11:23:23 +02:00
|
|
|
if (rst_i.read())
|
2017-09-21 13:13:01 +02:00
|
|
|
regs->reset_start();
|
|
|
|
else
|
|
|
|
regs->reset_stop();
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:15:04 +02:00
|
|
|
// Functional handling of interrupts:
|
|
|
|
// - global_int_port_cb()
|
|
|
|
// - set pending register bits
|
|
|
|
// - called by: incoming global_int
|
|
|
|
// - handle_pending_int()
|
|
|
|
// - update claim register content
|
|
|
|
// - generate core-interrupt pulse
|
|
|
|
// - called by:
|
|
|
|
// - incoming global_int
|
|
|
|
// - complete-register write access
|
|
|
|
// - reset_pending_int(int-id)
|
|
|
|
// - reset pending bit
|
|
|
|
// - call next handle_pending_int()
|
|
|
|
// - called by:
|
|
|
|
// - complete-reg write register content
|
|
|
|
|
|
|
|
void plic::global_int_port_cb() {
|
2018-11-08 13:31:28 +01:00
|
|
|
auto handle_pending = false;
|
2017-10-04 23:15:04 +02:00
|
|
|
// set related pending bit if enable is set for incoming global_interrupt
|
2017-11-10 22:40:24 +01:00
|
|
|
for (uint32_t i = 1; i < 256; i++) {
|
2018-11-08 13:31:28 +01:00
|
|
|
auto reg_idx = i >> 5;
|
|
|
|
auto bit_ofs = i & 0x1F;
|
2017-11-10 22:40:24 +01:00
|
|
|
bool enable = regs->r_enabled[reg_idx] & (0x1 << bit_ofs); // read enable bit
|
2017-10-04 23:15:04 +02:00
|
|
|
|
|
|
|
if (enable && global_interrupts_i[i].read() == 1) {
|
2017-11-10 22:40:24 +01:00
|
|
|
regs->r_pending[reg_idx] = regs->r_pending[reg_idx] | (0x1 << bit_ofs);
|
2018-11-08 13:31:28 +01:00
|
|
|
handle_pending = true;
|
|
|
|
SCDEBUG(this->name()) << "pending interrupt identified: " << i;
|
2017-10-04 23:15:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:31:28 +01:00
|
|
|
if (handle_pending) handle_pending_int();
|
2017-10-04 23:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void plic::handle_pending_int() {
|
|
|
|
// identify high-prio pending interrupt and raise a core-interrupt
|
2018-11-08 13:31:28 +01:00
|
|
|
auto claim_int = 0U; // claim interrupt
|
|
|
|
auto claim_prio = 0U; // related priority (highest prio interrupt wins the race)
|
|
|
|
auto raise_int = false;
|
|
|
|
auto thold = regs->r_threshold.threshold; // threshold value
|
|
|
|
|
|
|
|
for (size_t i = 1; i < 255; i++) {
|
|
|
|
auto reg_idx = i >> 5;
|
|
|
|
auto bit_ofs = i & 0x1F;
|
2017-11-10 22:40:24 +01:00
|
|
|
bool pending = (regs->r_pending[reg_idx] & (0x1 << bit_ofs)) ? true : false;
|
2018-11-08 13:31:28 +01:00
|
|
|
auto prio = regs->r_priority[i].priority; // read priority value
|
2017-10-04 23:15:04 +02:00
|
|
|
|
|
|
|
if (pending && thold < prio) {
|
|
|
|
// below condition ensures implicitly that lowest id is selected in case of multiple identical
|
|
|
|
// priority-interrupts
|
|
|
|
if (prio > claim_prio) {
|
|
|
|
claim_prio = prio;
|
|
|
|
claim_int = i;
|
2018-11-08 13:31:28 +01:00
|
|
|
raise_int = true;
|
|
|
|
SCDEBUG(this->name()) << "pending interrupt activated: " << i;
|
2017-10-04 23:15:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (raise_int) {
|
|
|
|
regs->r_claim_complete = claim_int;
|
2018-11-08 13:31:28 +01:00
|
|
|
core_interrupt_o.write(true);
|
2017-10-04 23:15:04 +02:00
|
|
|
// todo: evluate clock period
|
|
|
|
} else {
|
|
|
|
regs->r_claim_complete = 0;
|
2018-11-08 13:31:28 +01:00
|
|
|
SCDEBUG(this->name()) << "no further pending interrupt.";
|
2017-10-04 23:15:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void plic::reset_pending_int(uint32_t irq) {
|
|
|
|
// todo: evaluate enable register (see spec)
|
|
|
|
// todo: make sure that pending is set, otherwise don't reset irq ... read spec.
|
2018-11-08 13:31:28 +01:00
|
|
|
SCTRACE(this->name()) << "reset pending interrupt: " << irq;
|
2017-10-04 23:15:04 +02:00
|
|
|
// reset related pending bit
|
2018-11-08 13:31:28 +01:00
|
|
|
auto reg_idx = irq >> 5;
|
|
|
|
auto bit_ofs = irq & 0x1F;
|
2017-11-10 22:40:24 +01:00
|
|
|
regs->r_pending[reg_idx] &= ~(0x1 << bit_ofs);
|
2018-11-08 13:31:28 +01:00
|
|
|
core_interrupt_o.write(false);
|
2017-10-04 23:15:04 +02:00
|
|
|
|
|
|
|
// evaluate next pending interrupt
|
|
|
|
handle_pending_int();
|
|
|
|
}
|
|
|
|
|
2017-09-21 13:13:01 +02:00
|
|
|
} /* namespace sysc */
|