44#include "debug/Checkpoint.hh"
45#include "debug/PMUVerbose.hh"
48#include "params/ArmPMU.hh"
60 use64bitCounters(
p.use64bitCounters),
61 reg_pmcnten(0), reg_pmcr(0),
62 reg_pmselr(0), reg_pminten(0), reg_pmovsr(0),
63 reg_pmceid0(0),reg_pmceid1(0),
65 maximumCounterCount(
p.eventCounters),
66 cycleCounter(*this, maximumCounterCount,
p.use64bitCounters),
67 cycleCounterEventId(
p.cycleEventId),
68 swIncrementEvent(nullptr),
71 exitOnPMUControl(
p.exitOnPMUControl),
72 exitOnPMUInterrupt(
p.exitOnPMUInterrupt)
74 DPRINTF(PMUVerbose,
"Initializing the PMU.\n");
77 fatal(
"The PMU can only accept 31 counters, %d counters requested.\n",
81 warn_if(!
p.interrupt,
"ARM PMU: No interrupt specified, interrupt " \
82 "delivery disabled.\n");
102 const auto &pmu_params =
static_cast<const ArmPMUParams &
>(
params());
104 if (pmu_params.interrupt)
105 interrupt = pmu_params.interrupt->get(tc);
112 DPRINTF(PMUVerbose,
"PMU: Adding SW increment event with id '0x%x'\n",
id);
117 "Trying to add a software increment event with multiple"
118 "IDs. This is not supported.\n");
123 "been previously defined\n",
id);
134 DPRINTF(PMUVerbose,
"PMU: Adding Probe Driven event with id '0x%x'"
135 "as probe %s:%s\n",
id, obj->
name(), probe_name);
137 std::shared_ptr<RegularEvent>
event;
138 auto event_entry =
eventMap.find(
id);
139 if (event_entry ==
eventMap.end()) {
140 event = std::make_shared<RegularEvent>();
143 event = std::dynamic_pointer_cast<RegularEvent>(event_entry->second);
144 fatal_if(!
event,
"Event with id %d is not probe driven\n",
id);
146 event->addMicroarchitectureProbe(obj, probe_name);
159 }
else if (
id > 0x20 &&
id < 0x40) {
161 }
else if (
id >= 0x4000 &&
id < 0x4020) {
162 reg_pmceid0 |= ((uint64_t)1) << (
id - 0x4000 + 32);
163 }
else if (
id >= 0x4020 &&
id < 0x4040) {
164 reg_pmceid1 |= ((uint64_t)1) << (
id - 0x4020 + 32);
194 DPRINTF(PMUVerbose,
"setMiscReg(%s, 0x%x)\n",
253 DPRINTF(PMUVerbose,
"Setting PMCCFILTR: 0x%x\n",
val);
260 DPRINTF(PMUVerbose,
"Setting counter type: "
261 "[PMSELR: 0x%x, PMSELER.sel: 0x%x, EVTYPER: 0x%x]\n",
302 warn(
"Not doing anything for write to miscreg %s\n",
310 DPRINTF(PMUVerbose,
"readMiscReg(%s): 0x%x\n",
399 warn(
"Not doing anything for read from miscreg %s\n",
407 DPRINTF(PMUVerbose,
"Set Control Reg 0x%08x.\n",
val);
410 DPRINTF(PMUVerbose,
"PMU reset all events to zero.\n");
415 DPRINTF(PMUVerbose,
"PMU reset cycle counter to zero.\n");
427 inform(
"Exiting simulation: PMU enable detected");
430 inform(
"Exiting simulation: PMU disable detected");
433 inform(
"Exiting simulation: PMU reset detected");
445 const bool global_enable(
reg_pmcr.e);
476 for (
auto& counter: userCounters) {
484 userCounters.erase(user);
486 if (userCounters.empty()) {
494 parentEvent->increment(
val);
500 for (
auto& subEvents: microArchitectureEventSet) {
501 attachedProbePointList.emplace_back(
502 new RegularProbe(
this, subEvents.first, subEvents.second));
509 attachedProbePointList.clear();
517 const PMEVTYPER_t filter(this->filter);
519 const bool secure(pmu.isa->inSecureState());
523 return secure ? filter.u : (filter.u != filter.nsu);
526 return secure ? filter.p : (filter.p != filter.nsk);
532 return filter.p != filter.m;
535 panic(
"Unexpected execution level in PMU::isFiltered.\n");
543 sourceEvent->detachEvent(
this);
544 sourceEvent =
nullptr;
547 "detaching event %d not currently attached to any event"
548 " [counterId = %d]\n", eventId, counterId);
560 sourceEvent->attachEvent(
this);
567 sourceEvent->updateAttachedCounters();
569 debugCounter(
"attempted to get value from a counter without"
570 " an associated event\n");
582 sourceEvent->updateAttachedCounters();
584 debugCounter(
"attempted to set value from a counter without"
585 " an associated event\n");
593 DPRINTF(PMUVerbose,
"updateCounter(%i): Disabling counter\n",
598 DPRINTF(PMUVerbose,
"updateCounter(%i): Enable event id 0x%x\n",
602 if (sourceEvent ==
eventMap.end()) {
603 warn(
"Can't enable PMU counter of type '0x%x': "
604 "No such event type.\n", ctr.
eventId);
607 ctr.
attach(sourceEvent->second);
624 warn_once(
"Can't change counter value: Counter %i does not exist.\n",
650 DPRINTF(PMUVerbose,
"Set Event [%d] = 0x%08x\n",
id,
val);
652 warn_once(
"Can't change counter type: Counter %i does not exist.\n",
665 if (
id !=
PMCCNTR && old_event_id !=
val.evtCount) {
675 const bool int_new = new_val != 0;
678 if (int_old && !int_new) {
689 inform(
"Exiting simulation: PMU interrupt detected");
693 DPRINTF(PMUVerbose,
"Delivering PMU interrupt.\n");
696 warn_once(
"Dropping PMU interrupt as no interrupt has "
705 DPRINTF(PMUVerbose,
"Clearing PMU interrupt.\n");
708 warn_once(
"Dropping PMU interrupt as no interrupt has "
716 DPRINTF(Checkpoint,
"Serializing Arm PMU\n");
737 DPRINTF(Checkpoint,
"Unserializing Arm PMU\n");
763std::shared_ptr<PMU::PMUEvent>
766 auto entry =
eventMap.find(eventId);
769 warn(
"event %d does not exist\n", eventId);
772 return entry->second;
795 uint64_t value_until_overflow;
797 value_until_overflow = UINT64_MAX - value;
799 value_until_overflow = UINT32_MAX - (uint32_t)value;
803 return value_until_overflow;
812 if (delta > value_until_overflow) {
816 pmu.reg_pmovsr |= (1 << counterId);
820 if (pmu.reg_pminten & (1 << counterId)) {
821 pmu.raiseInterrupt();
823 return overflow64 ? UINT64_MAX : UINT32_MAX;
825 return value_until_overflow - delta + 1;
831 for (
auto& counter: userCounters) {
832 if (
val & (0x1 << counter->getCounterId())) {
#define DPRINTFS(x, s,...)
Base class for ARM GIC implementations.
Base class for devices that use the MiscReg interfaces.
void write(uint64_t val)
write on the sw increment register inducing an increment of the counters with this event selected acc...
void regProbeListeners() override
Register probe listeners for this object.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
void setControlReg(PMCR_t val)
PMCR write handling.
void raiseInterrupt()
Deliver a PMU interrupt to the GIC.
PMCR_t reg_pmcr_conf
Constant (configuration-dependent) part of the PMCR.
RegVal readMiscReg(int misc_reg) override
Read a register within the PMU.
RegVal reg_pmcnten
Performance Monitor Count Enable Register.
std::shared_ptr< PMUEvent > getEvent(uint64_t eventId)
Obtain the event of a given id.
void clearInterrupt()
Clear a PMU interrupt.
const uint64_t cycleCounterEventId
The id of the counter hardwired to the cpu cycle counter.
uint64_t maximumCounterCount
The number of regular event counters.
static const RegVal reg_pmcr_wr_mask
PMCR write mask when accessed from the guest.
bool isFiltered(const CounterState &ctr) const
Check if a counter's settings allow it to be counted.
PMEVTYPER_t getCounterTypeRegister(CounterId id) const
Get the type and filter settings of a counter (PMEVTYPER)
unsigned clock_remainder
Remainder part when the clock counter is divided by 64.
void updateAllCounters()
Call updateCounter() for each counter in the PMU if the counter's state has changed.
void resetEventCounts()
Reset all event counters excluding the cycle counter to zero.
void registerEvent(uint32_t id)
void setOverflowStatus(RegVal new_val)
Used for writing the Overflow Flag Status Register (SET/CLR)
PMSELR_t reg_pmselr
Performance Monitor Selection Register.
std::map< EventTypeId, std::shared_ptr< PMUEvent > > eventMap
List of event types supported by this PMU.
static const CounterId PMCCNTR
Cycle Count Register Number.
void setThreadContext(ThreadContext *tc) override
std::vector< CounterState > counters
State of all general-purpose counters supported by PMU.
void setCounterTypeRegister(CounterId id, PMEVTYPER_t type)
Set the type and filter settings of a performance counter (PMEVTYPER)
void drainResume() override
Resume execution after a successful drain.
PMU(const ArmPMUParams &p)
RegVal readMiscRegInt(int misc_reg)
void setMiscReg(int misc_reg, RegVal val) override
Set a register within the PMU.
CounterState cycleCounter
State of the cycle counter.
uint64_t reg_pmceid0
Performance counter ID register.
bool isValidCounter(CounterId id) const
Is this a valid counter ID?
PMCR_t reg_pmcr
Performance Monitor Control Register.
RegVal reg_pmovsr
Performance Monitor Overflow Status Register.
ArmInterruptPin * interrupt
Performance monitor interrupt number.
void addSoftwareIncrementEvent(unsigned int id)
CounterState & getCounter(CounterId id)
Return the state of a counter.
bool use64bitCounters
Determine whether to use 64-bit or 32-bit counters.
std::shared_ptr< SWIncrementEvent > swIncrementEvent
The event that implements the software increment.
uint64_t getCounterValue(CounterId id) const
Get the value of a performance counter.
bool exitOnPMUInterrupt
Exit simloop on PMU interrupt.
unsigned int EventTypeId
Event type ID.
void setCounterValue(CounterId id, uint64_t val)
Set the value of a performance counter.
RegVal reg_pminten
Performance Monitor Interrupt Enable Register.
void updateCounter(CounterState &ctr)
Depending on counter configuration, add or remove the probes driving the counter.
void serialize(CheckpointOut &cp) const override
Serialize an object.
const bool exitOnPMUControl
Exit simloop on PMU reset or disable.
void addEventProbe(unsigned int id, SimObject *obj, const char *name)
virtual void clear()=0
Clear a signalled interrupt.
virtual void raise()=0
Signal an interrupt.
virtual std::string name() const
Abstract superclass for simulation objects.
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual ContextID contextId() const =0
This module implements the global system counter and the local per-CPU architected timers as specifie...
#define panic(...)
This implements a cprintf based panic() function.
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
#define fatal(...)
This implements a cprintf based fatal() function.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
#define UNSERIALIZE_OPT_SCALAR(scalar)
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
const Params & params() const
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
@ MISCREG_PMXEVTYPER_PMCCFILTR
int unflattenMiscReg(int reg)
const char *const miscRegName[]
Copyright (c) 2024 Arm Limited All rights reserved.
std::ostream CheckpointOut
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
void exitSimLoop(const std::string &message, int exit_code, Tick when, Tick repeat, bool serialize)
Schedule an event to exit the simulation loop (returning to Python) at the end of the current cycle (...
std::string csprintf(const char *format, const Args &...args)
#define UNSERIALIZE_SCALAR(scalar)
#define SERIALIZE_SCALAR(scalar)
State of a counter within the PMU.
void attach(const std::shared_ptr< PMUEvent > &event)
Attach this counter to an event.
PMEVTYPER_t filter
Filtering settings (evtCount is unused)
void detach()
Detach the counter from its event.
EventTypeId eventId
Counter event ID.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
uint64_t getCounterId() const
Obtain the counter id.
uint64_t getValue() const
rReturn the counter value
bool enabled
Is the counter enabled?
uint64_t add(uint64_t delta)
Add an event count to the counter and check for overflow.
void serialize(CheckpointOut &cp) const override
Serialize an object.
void setValue(uint64_t val)
overwrite the value of the counter
void attachEvent(PMU::CounterState *user)
attach this event to a given counter
virtual void enable()=0
Enable the current event.
std::set< PMU::CounterState * > userCounters
set of counters using this event
virtual void updateAttachedCounters()
Method called immediately before a counter access in order for the associated event to update its sta...
void detachEvent(PMU::CounterState *user)
detach this event from a given counter
virtual void increment(const uint64_t val)
notify an event increment of val units, all the attached counters' value is incremented by val units.
void notify(const uint64_t &val)
void enable() override
Enable the current event.
void disable() override
Disable the current event.