18512Sgeoffrey.blake@arm.com/*
213106Sgiacomo.travaglini@arm.com * Copyright (c) 2010-2013,2018 ARM Limited
38512Sgeoffrey.blake@arm.com * All rights reserved
48512Sgeoffrey.blake@arm.com *
58512Sgeoffrey.blake@arm.com * The license below extends only to copyright in the software and shall
68512Sgeoffrey.blake@arm.com * not be construed as granting a license to any other intellectual
78512Sgeoffrey.blake@arm.com * property including but not limited to intellectual property relating
88512Sgeoffrey.blake@arm.com * to a hardware implementation of the functionality of the software
98512Sgeoffrey.blake@arm.com * licensed hereunder.  You may use the software subject to the license
108512Sgeoffrey.blake@arm.com * terms below provided that you ensure that this notice is replicated
118512Sgeoffrey.blake@arm.com * unmodified and in its entirety in all distributions of the software,
128512Sgeoffrey.blake@arm.com * modified or unmodified, in source code or in binary form.
138512Sgeoffrey.blake@arm.com *
148512Sgeoffrey.blake@arm.com * Redistribution and use in source and binary forms, with or without
158512Sgeoffrey.blake@arm.com * modification, are permitted provided that the following conditions are
168512Sgeoffrey.blake@arm.com * met: redistributions of source code must retain the above copyright
178512Sgeoffrey.blake@arm.com * notice, this list of conditions and the following disclaimer;
188512Sgeoffrey.blake@arm.com * redistributions in binary form must reproduce the above copyright
198512Sgeoffrey.blake@arm.com * notice, this list of conditions and the following disclaimer in the
208512Sgeoffrey.blake@arm.com * documentation and/or other materials provided with the distribution;
218512Sgeoffrey.blake@arm.com * neither the name of the copyright holders nor the names of its
228512Sgeoffrey.blake@arm.com * contributors may be used to endorse or promote products derived from
238512Sgeoffrey.blake@arm.com * this software without specific prior written permission.
248512Sgeoffrey.blake@arm.com *
258512Sgeoffrey.blake@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
268512Sgeoffrey.blake@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
278512Sgeoffrey.blake@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
288512Sgeoffrey.blake@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
298512Sgeoffrey.blake@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
308512Sgeoffrey.blake@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
318512Sgeoffrey.blake@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
328512Sgeoffrey.blake@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
338512Sgeoffrey.blake@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
348512Sgeoffrey.blake@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
358512Sgeoffrey.blake@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
368512Sgeoffrey.blake@arm.com *
378512Sgeoffrey.blake@arm.com * Authors: Ali Saidi
388512Sgeoffrey.blake@arm.com *          Geoffrey Blake
398512Sgeoffrey.blake@arm.com */
408512Sgeoffrey.blake@arm.com
4111793Sbrandon.potter@amd.com#include "dev/arm/timer_cpulocal.hh"
4211793Sbrandon.potter@amd.com
4313106Sgiacomo.travaglini@arm.com#include "arch/arm/system.hh"
448512Sgeoffrey.blake@arm.com#include "base/intmath.hh"
458512Sgeoffrey.blake@arm.com#include "base/trace.hh"
468512Sgeoffrey.blake@arm.com#include "debug/Checkpoint.hh"
478512Sgeoffrey.blake@arm.com#include "debug/Timer.hh"
489525SAndreas.Sandberg@ARM.com#include "dev/arm/base_gic.hh"
498512Sgeoffrey.blake@arm.com#include "mem/packet.hh"
508512Sgeoffrey.blake@arm.com#include "mem/packet_access.hh"
518512Sgeoffrey.blake@arm.com
528512Sgeoffrey.blake@arm.comCpuLocalTimer::CpuLocalTimer(Params *p)
5313106Sgiacomo.travaglini@arm.com    : BasicPioDevice(p, 0x38)
548512Sgeoffrey.blake@arm.com{
5513106Sgiacomo.travaglini@arm.com}
5613106Sgiacomo.travaglini@arm.com
5713106Sgiacomo.travaglini@arm.comvoid
5813106Sgiacomo.travaglini@arm.comCpuLocalTimer::init()
5913106Sgiacomo.travaglini@arm.com{
6013106Sgiacomo.travaglini@arm.com   auto p = params();
618512Sgeoffrey.blake@arm.com   // Initialize the timer registers for each per cpu timer
6213106Sgiacomo.travaglini@arm.com   for (int i = 0; i < sys->numContexts(); i++) {
6313106Sgiacomo.travaglini@arm.com        ThreadContext* tc = sys->getThreadContext(i);
648512Sgeoffrey.blake@arm.com        std::stringstream oss;
658512Sgeoffrey.blake@arm.com        oss << name() << ".timer" << i;
6613106Sgiacomo.travaglini@arm.com
6713106Sgiacomo.travaglini@arm.com        localTimer.emplace_back(
6813106Sgiacomo.travaglini@arm.com            new Timer(oss.str(), this,
6913106Sgiacomo.travaglini@arm.com                      p->int_timer->get(tc),
7013106Sgiacomo.travaglini@arm.com                      p->int_watchdog->get(tc)));
718512Sgeoffrey.blake@arm.com    }
7213106Sgiacomo.travaglini@arm.com
7313106Sgiacomo.travaglini@arm.com    BasicPioDevice::init();
748512Sgeoffrey.blake@arm.com}
758512Sgeoffrey.blake@arm.com
7613106Sgiacomo.travaglini@arm.comCpuLocalTimer::Timer::Timer(const std::string &timer_name,
7713106Sgiacomo.travaglini@arm.com                            CpuLocalTimer* _parent,
7813106Sgiacomo.travaglini@arm.com                            ArmInterruptPin* int_timer,
7913106Sgiacomo.travaglini@arm.com                            ArmInterruptPin* int_watchdog)
8013106Sgiacomo.travaglini@arm.com    : _name(timer_name), parent(_parent), intTimer(int_timer),
8113106Sgiacomo.travaglini@arm.com      intWatchdog(int_watchdog), timerControl(0x0), watchdogControl(0x0),
8213106Sgiacomo.travaglini@arm.com      rawIntTimer(false), rawIntWatchdog(false),
8313106Sgiacomo.travaglini@arm.com      rawResetWatchdog(false), watchdogDisableReg(0x0),
8413106Sgiacomo.travaglini@arm.com      pendingIntTimer(false), pendingIntWatchdog(false),
8512086Sspwilson2@wisc.edu      timerLoadValue(0x0), watchdogLoadValue(0x0),
8612086Sspwilson2@wisc.edu      timerZeroEvent([this]{ timerAtZero(); }, name()),
8712086Sspwilson2@wisc.edu      watchdogZeroEvent([this]{ watchdogAtZero(); }, name())
888512Sgeoffrey.blake@arm.com{
898512Sgeoffrey.blake@arm.com}
908512Sgeoffrey.blake@arm.com
918512Sgeoffrey.blake@arm.comTick
928512Sgeoffrey.blake@arm.comCpuLocalTimer::read(PacketPtr pkt)
938512Sgeoffrey.blake@arm.com{
948512Sgeoffrey.blake@arm.com    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
958512Sgeoffrey.blake@arm.com    assert(pkt->getSize() == 4);
968512Sgeoffrey.blake@arm.com    Addr daddr = pkt->getAddr() - pioAddr;
9711005Sandreas.sandberg@arm.com    ContextID cpu_id = pkt->req->contextId();
988512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "Reading from CpuLocalTimer at offset: %#x\n", daddr);
998512Sgeoffrey.blake@arm.com    assert(cpu_id >= 0);
10013106Sgiacomo.travaglini@arm.com    assert(cpu_id < localTimer.size());
1018512Sgeoffrey.blake@arm.com
1028512Sgeoffrey.blake@arm.com    if (daddr < Timer::Size)
10313106Sgiacomo.travaglini@arm.com        localTimer[cpu_id]->read(pkt, daddr);
1048512Sgeoffrey.blake@arm.com    else
1058512Sgeoffrey.blake@arm.com        panic("Tried to read CpuLocalTimer at offset %#x that doesn't exist\n", daddr);
1068512Sgeoffrey.blake@arm.com    pkt->makeAtomicResponse();
1078512Sgeoffrey.blake@arm.com    return pioDelay;
1088512Sgeoffrey.blake@arm.com}
1098512Sgeoffrey.blake@arm.com
1108512Sgeoffrey.blake@arm.com
1118512Sgeoffrey.blake@arm.comvoid
1128512Sgeoffrey.blake@arm.comCpuLocalTimer::Timer::read(PacketPtr pkt, Addr daddr)
1138512Sgeoffrey.blake@arm.com{
1148512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "Reading from CpuLocalTimer at offset: %#x\n", daddr);
1158512Sgeoffrey.blake@arm.com    Tick time;
1168512Sgeoffrey.blake@arm.com
1178512Sgeoffrey.blake@arm.com    switch(daddr) {
1188512Sgeoffrey.blake@arm.com      case TimerLoadReg:
11913230Sgabeblack@google.com        pkt->setLE<uint32_t>(timerLoadValue);
1208512Sgeoffrey.blake@arm.com        break;
1218512Sgeoffrey.blake@arm.com      case TimerCounterReg:
1228512Sgeoffrey.blake@arm.com        DPRINTF(Timer, "Event schedule for timer %d, clock=%d, prescale=%d\n",
1239545Sandreas.hansson@arm.com                timerZeroEvent.when(), parent->clockPeriod(),
1249545Sandreas.hansson@arm.com                timerControl.prescalar);
1258512Sgeoffrey.blake@arm.com        time = timerZeroEvent.when() - curTick();
1269545Sandreas.hansson@arm.com        time = time / parent->clockPeriod() /
1279545Sandreas.hansson@arm.com            power(16, timerControl.prescalar);
1288512Sgeoffrey.blake@arm.com        DPRINTF(Timer, "-- returning counter at %d\n", time);
12913230Sgabeblack@google.com        pkt->setLE<uint32_t>(time);
1308512Sgeoffrey.blake@arm.com        break;
1318512Sgeoffrey.blake@arm.com      case TimerControlReg:
13213230Sgabeblack@google.com        pkt->setLE<uint32_t>(timerControl);
1338512Sgeoffrey.blake@arm.com        break;
1348512Sgeoffrey.blake@arm.com      case TimerIntStatusReg:
13513230Sgabeblack@google.com        pkt->setLE<uint32_t>(rawIntTimer);
1368512Sgeoffrey.blake@arm.com        break;
1378512Sgeoffrey.blake@arm.com      case WatchdogLoadReg:
13813230Sgabeblack@google.com        pkt->setLE<uint32_t>(watchdogLoadValue);
1398512Sgeoffrey.blake@arm.com        break;
1408512Sgeoffrey.blake@arm.com      case WatchdogCounterReg:
1419545Sandreas.hansson@arm.com        DPRINTF(Timer,
1429545Sandreas.hansson@arm.com                "Event schedule for watchdog %d, clock=%d, prescale=%d\n",
1439545Sandreas.hansson@arm.com                watchdogZeroEvent.when(), parent->clockPeriod(),
1449545Sandreas.hansson@arm.com                watchdogControl.prescalar);
1458512Sgeoffrey.blake@arm.com        time = watchdogZeroEvent.when() - curTick();
1469545Sandreas.hansson@arm.com        time = time / parent->clockPeriod() /
1479545Sandreas.hansson@arm.com            power(16, watchdogControl.prescalar);
1488512Sgeoffrey.blake@arm.com        DPRINTF(Timer, "-- returning counter at %d\n", time);
14913230Sgabeblack@google.com        pkt->setLE<uint32_t>(time);
1508512Sgeoffrey.blake@arm.com        break;
1518512Sgeoffrey.blake@arm.com      case WatchdogControlReg:
15213230Sgabeblack@google.com        pkt->setLE<uint32_t>(watchdogControl);
1538512Sgeoffrey.blake@arm.com        break;
1548512Sgeoffrey.blake@arm.com      case WatchdogIntStatusReg:
15513230Sgabeblack@google.com        pkt->setLE<uint32_t>(rawIntWatchdog);
1568512Sgeoffrey.blake@arm.com        break;
1578512Sgeoffrey.blake@arm.com      case WatchdogResetStatusReg:
15813230Sgabeblack@google.com        pkt->setLE<uint32_t>(rawResetWatchdog);
1598512Sgeoffrey.blake@arm.com        break;
1608512Sgeoffrey.blake@arm.com      case WatchdogDisableReg:
1618512Sgeoffrey.blake@arm.com        panic("Tried to read from WatchdogDisableRegister\n");
1628512Sgeoffrey.blake@arm.com        break;
1638512Sgeoffrey.blake@arm.com      default:
1648512Sgeoffrey.blake@arm.com        panic("Tried to read CpuLocalTimer at offset %#x\n", daddr);
1658512Sgeoffrey.blake@arm.com        break;
1668512Sgeoffrey.blake@arm.com    }
1678512Sgeoffrey.blake@arm.com}
1688512Sgeoffrey.blake@arm.com
1698512Sgeoffrey.blake@arm.comTick
1708512Sgeoffrey.blake@arm.comCpuLocalTimer::write(PacketPtr pkt)
1718512Sgeoffrey.blake@arm.com{
1728512Sgeoffrey.blake@arm.com    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
1738512Sgeoffrey.blake@arm.com    assert(pkt->getSize() == 4);
1748512Sgeoffrey.blake@arm.com    Addr daddr = pkt->getAddr() - pioAddr;
17511005Sandreas.sandberg@arm.com    ContextID cpu_id = pkt->req->contextId();
1768512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "Writing to CpuLocalTimer at offset: %#x\n", daddr);
1778512Sgeoffrey.blake@arm.com    assert(cpu_id >= 0);
17813106Sgiacomo.travaglini@arm.com    assert(cpu_id < localTimer.size());
1798512Sgeoffrey.blake@arm.com
1808512Sgeoffrey.blake@arm.com    if (daddr < Timer::Size)
18113106Sgiacomo.travaglini@arm.com        localTimer[cpu_id]->write(pkt, daddr);
1828512Sgeoffrey.blake@arm.com    else
1838512Sgeoffrey.blake@arm.com        panic("Tried to write CpuLocalTimer at offset %#x that doesn't exist\n", daddr);
1848512Sgeoffrey.blake@arm.com    pkt->makeAtomicResponse();
1858512Sgeoffrey.blake@arm.com    return pioDelay;
1868512Sgeoffrey.blake@arm.com}
1878512Sgeoffrey.blake@arm.com
1888512Sgeoffrey.blake@arm.comvoid
1898512Sgeoffrey.blake@arm.comCpuLocalTimer::Timer::write(PacketPtr pkt, Addr daddr)
1908512Sgeoffrey.blake@arm.com{
1918512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "Writing to CpuLocalTimer at offset: %#x\n", daddr);
1928512Sgeoffrey.blake@arm.com    bool old_enable;
1938512Sgeoffrey.blake@arm.com    bool old_wd_mode;
1948512Sgeoffrey.blake@arm.com    uint32_t old_val;
1958512Sgeoffrey.blake@arm.com
1968512Sgeoffrey.blake@arm.com    switch (daddr) {
1978512Sgeoffrey.blake@arm.com      case TimerLoadReg:
1988512Sgeoffrey.blake@arm.com        // Writing to this register also resets the counter register and
1998512Sgeoffrey.blake@arm.com        // starts decrementing if the counter is enabled.
20013230Sgabeblack@google.com        timerLoadValue = pkt->getLE<uint32_t>();
2018512Sgeoffrey.blake@arm.com        restartTimerCounter(timerLoadValue);
2028512Sgeoffrey.blake@arm.com        break;
2038512Sgeoffrey.blake@arm.com      case TimerCounterReg:
2048512Sgeoffrey.blake@arm.com        // Can be written, doesn't start counting unless the timer is enabled
20513230Sgabeblack@google.com        restartTimerCounter(pkt->getLE<uint32_t>());
2068512Sgeoffrey.blake@arm.com        break;
2078512Sgeoffrey.blake@arm.com      case TimerControlReg:
2088512Sgeoffrey.blake@arm.com        old_enable = timerControl.enable;
20913230Sgabeblack@google.com        timerControl = pkt->getLE<uint32_t>();
2108512Sgeoffrey.blake@arm.com        if ((old_enable == 0) && timerControl.enable)
2118512Sgeoffrey.blake@arm.com            restartTimerCounter(timerLoadValue);
2128512Sgeoffrey.blake@arm.com        break;
2138512Sgeoffrey.blake@arm.com      case TimerIntStatusReg:
2148512Sgeoffrey.blake@arm.com        rawIntTimer = false;
2158512Sgeoffrey.blake@arm.com        if (pendingIntTimer) {
2168512Sgeoffrey.blake@arm.com            pendingIntTimer = false;
2178512Sgeoffrey.blake@arm.com            DPRINTF(Timer, "Clearing interrupt\n");
2188512Sgeoffrey.blake@arm.com        }
2198512Sgeoffrey.blake@arm.com        break;
2208512Sgeoffrey.blake@arm.com      case WatchdogLoadReg:
22113230Sgabeblack@google.com        watchdogLoadValue = pkt->getLE<uint32_t>();
2228512Sgeoffrey.blake@arm.com        restartWatchdogCounter(watchdogLoadValue);
2238512Sgeoffrey.blake@arm.com        break;
2248512Sgeoffrey.blake@arm.com      case WatchdogCounterReg:
2258512Sgeoffrey.blake@arm.com        // Can't be written when in watchdog mode, but can in timer mode
2268512Sgeoffrey.blake@arm.com        if (!watchdogControl.watchdogMode) {
22713230Sgabeblack@google.com            restartWatchdogCounter(pkt->getLE<uint32_t>());
2288512Sgeoffrey.blake@arm.com        }
2298512Sgeoffrey.blake@arm.com        break;
2308512Sgeoffrey.blake@arm.com      case WatchdogControlReg:
2318512Sgeoffrey.blake@arm.com        old_enable = watchdogControl.enable;
2328512Sgeoffrey.blake@arm.com        old_wd_mode = watchdogControl.watchdogMode;
23313230Sgabeblack@google.com        watchdogControl = pkt->getLE<uint32_t>();
2348512Sgeoffrey.blake@arm.com        if ((old_enable == 0) && watchdogControl.enable)
2358512Sgeoffrey.blake@arm.com            restartWatchdogCounter(watchdogLoadValue);
2368512Sgeoffrey.blake@arm.com        // cannot disable watchdog using control register
2378512Sgeoffrey.blake@arm.com        if ((old_wd_mode == 1) && watchdogControl.watchdogMode == 0)
2388512Sgeoffrey.blake@arm.com            watchdogControl.watchdogMode = 1;
2398512Sgeoffrey.blake@arm.com        break;
2408512Sgeoffrey.blake@arm.com      case WatchdogIntStatusReg:
2418512Sgeoffrey.blake@arm.com        rawIntWatchdog = false;
2428512Sgeoffrey.blake@arm.com        if (pendingIntWatchdog) {
2438512Sgeoffrey.blake@arm.com            pendingIntWatchdog = false;
2448512Sgeoffrey.blake@arm.com            DPRINTF(Timer, "Clearing watchdog interrupt\n");
2458512Sgeoffrey.blake@arm.com        }
2468512Sgeoffrey.blake@arm.com        break;
2478512Sgeoffrey.blake@arm.com      case WatchdogResetStatusReg:
2488512Sgeoffrey.blake@arm.com        rawResetWatchdog = false;
2498512Sgeoffrey.blake@arm.com        DPRINTF(Timer, "Clearing watchdog reset flag\n");
2508512Sgeoffrey.blake@arm.com        break;
2518512Sgeoffrey.blake@arm.com      case WatchdogDisableReg:
2528512Sgeoffrey.blake@arm.com        old_val = watchdogDisableReg;
25313230Sgabeblack@google.com        watchdogDisableReg = pkt->getLE<uint32_t>();
2548512Sgeoffrey.blake@arm.com        // if this sequence is observed, turn off watchdog mode
2558512Sgeoffrey.blake@arm.com        if (old_val == 0x12345678 && watchdogDisableReg == 0x87654321)
2568512Sgeoffrey.blake@arm.com            watchdogControl.watchdogMode = 0;
2578512Sgeoffrey.blake@arm.com        break;
2588512Sgeoffrey.blake@arm.com      default:
2598512Sgeoffrey.blake@arm.com        panic("Tried to write CpuLocalTimer timer at offset %#x\n", daddr);
2608512Sgeoffrey.blake@arm.com        break;
2618512Sgeoffrey.blake@arm.com    }
2628512Sgeoffrey.blake@arm.com}
2638512Sgeoffrey.blake@arm.com
2648512Sgeoffrey.blake@arm.com//XXX: Two functions are needed because the control registers are different types
2658512Sgeoffrey.blake@arm.comvoid
2668512Sgeoffrey.blake@arm.comCpuLocalTimer::Timer::restartTimerCounter(uint32_t val)
2678512Sgeoffrey.blake@arm.com{
2688512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "Resetting timer counter with value %#x\n", val);
2698512Sgeoffrey.blake@arm.com    if (!timerControl.enable)
2708512Sgeoffrey.blake@arm.com        return;
2718512Sgeoffrey.blake@arm.com
2729545Sandreas.hansson@arm.com    Tick time = parent->clockPeriod() * power(16, timerControl.prescalar);
2738512Sgeoffrey.blake@arm.com    time *= val;
2748512Sgeoffrey.blake@arm.com
2758512Sgeoffrey.blake@arm.com    if (timerZeroEvent.scheduled()) {
2768512Sgeoffrey.blake@arm.com        DPRINTF(Timer, "-- Event was already schedule, de-scheduling\n");
2778512Sgeoffrey.blake@arm.com        parent->deschedule(timerZeroEvent);
2788512Sgeoffrey.blake@arm.com    }
2798512Sgeoffrey.blake@arm.com    parent->schedule(timerZeroEvent, curTick() + time);
2808512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "-- Scheduling new event for: %d\n", curTick() + time);
2818512Sgeoffrey.blake@arm.com}
2828512Sgeoffrey.blake@arm.com
2838512Sgeoffrey.blake@arm.comvoid
2848512Sgeoffrey.blake@arm.comCpuLocalTimer::Timer::restartWatchdogCounter(uint32_t val)
2858512Sgeoffrey.blake@arm.com{
2868512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "Resetting watchdog counter with value %#x\n", val);
2878512Sgeoffrey.blake@arm.com    if (!watchdogControl.enable)
2888512Sgeoffrey.blake@arm.com        return;
2898512Sgeoffrey.blake@arm.com
2909545Sandreas.hansson@arm.com    Tick time = parent->clockPeriod() * power(16, watchdogControl.prescalar);
2918512Sgeoffrey.blake@arm.com    time *= val;
2928512Sgeoffrey.blake@arm.com
2938512Sgeoffrey.blake@arm.com    if (watchdogZeroEvent.scheduled()) {
2948512Sgeoffrey.blake@arm.com        DPRINTF(Timer, "-- Event was already schedule, de-scheduling\n");
2958512Sgeoffrey.blake@arm.com        parent->deschedule(watchdogZeroEvent);
2968512Sgeoffrey.blake@arm.com    }
2978512Sgeoffrey.blake@arm.com    parent->schedule(watchdogZeroEvent, curTick() + time);
2988512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "-- Scheduling new event for: %d\n", curTick() + time);
2998512Sgeoffrey.blake@arm.com}
3008512Sgeoffrey.blake@arm.com//////
3018512Sgeoffrey.blake@arm.com
3028512Sgeoffrey.blake@arm.comvoid
3038512Sgeoffrey.blake@arm.comCpuLocalTimer::Timer::timerAtZero()
3048512Sgeoffrey.blake@arm.com{
3058512Sgeoffrey.blake@arm.com    if (!timerControl.enable)
3068512Sgeoffrey.blake@arm.com        return;
3078512Sgeoffrey.blake@arm.com
3088512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "Timer Counter reached zero\n");
3098512Sgeoffrey.blake@arm.com
3108512Sgeoffrey.blake@arm.com    rawIntTimer = true;
3118512Sgeoffrey.blake@arm.com    bool old_pending = pendingIntTimer;
3128512Sgeoffrey.blake@arm.com    if (timerControl.intEnable)
3138512Sgeoffrey.blake@arm.com        pendingIntTimer = true;
3148993SAli.Saidi@ARM.com    if (pendingIntTimer && !old_pending) {
3158512Sgeoffrey.blake@arm.com        DPRINTF(Timer, "-- Causing interrupt\n");
31613106Sgiacomo.travaglini@arm.com        intTimer->raise();
3178512Sgeoffrey.blake@arm.com    }
3188512Sgeoffrey.blake@arm.com
3198512Sgeoffrey.blake@arm.com    if (!timerControl.autoReload)
3208512Sgeoffrey.blake@arm.com        return;
3218512Sgeoffrey.blake@arm.com    else
3228512Sgeoffrey.blake@arm.com        restartTimerCounter(timerLoadValue);
3238512Sgeoffrey.blake@arm.com}
3248512Sgeoffrey.blake@arm.com
3258512Sgeoffrey.blake@arm.comvoid
3268512Sgeoffrey.blake@arm.comCpuLocalTimer::Timer::watchdogAtZero()
3278512Sgeoffrey.blake@arm.com{
3288512Sgeoffrey.blake@arm.com    if (!watchdogControl.enable)
3298512Sgeoffrey.blake@arm.com        return;
3308512Sgeoffrey.blake@arm.com
3318512Sgeoffrey.blake@arm.com    DPRINTF(Timer, "Watchdog Counter reached zero\n");
3328512Sgeoffrey.blake@arm.com
3338512Sgeoffrey.blake@arm.com    rawIntWatchdog = true;
3348512Sgeoffrey.blake@arm.com    bool old_pending = pendingIntWatchdog;
3358512Sgeoffrey.blake@arm.com    // generates an interrupt only if the watchdog is in timer
3368512Sgeoffrey.blake@arm.com    // mode.
3378512Sgeoffrey.blake@arm.com    if (watchdogControl.intEnable && !watchdogControl.watchdogMode)
3388512Sgeoffrey.blake@arm.com        pendingIntWatchdog = true;
3398512Sgeoffrey.blake@arm.com    else if (watchdogControl.watchdogMode) {
3408512Sgeoffrey.blake@arm.com        rawResetWatchdog = true;
3418512Sgeoffrey.blake@arm.com        fatal("gem5 ARM Model does not support true watchdog operation!\n");
3428512Sgeoffrey.blake@arm.com        //XXX: Should we ever support a true watchdog reset?
3438512Sgeoffrey.blake@arm.com    }
3448512Sgeoffrey.blake@arm.com
3458993SAli.Saidi@ARM.com    if (pendingIntWatchdog && !old_pending) {
3468512Sgeoffrey.blake@arm.com        DPRINTF(Timer, "-- Causing interrupt\n");
34713106Sgiacomo.travaglini@arm.com        intWatchdog->raise();
3488512Sgeoffrey.blake@arm.com    }
3498512Sgeoffrey.blake@arm.com
3508512Sgeoffrey.blake@arm.com    if (watchdogControl.watchdogMode)
3518512Sgeoffrey.blake@arm.com        return;
3528512Sgeoffrey.blake@arm.com    else if (watchdogControl.autoReload)
3538512Sgeoffrey.blake@arm.com        restartWatchdogCounter(watchdogLoadValue);
3548512Sgeoffrey.blake@arm.com}
3558512Sgeoffrey.blake@arm.com
3568512Sgeoffrey.blake@arm.comvoid
35710905Sandreas.sandberg@arm.comCpuLocalTimer::Timer::serialize(CheckpointOut &cp) const
3588512Sgeoffrey.blake@arm.com{
3598512Sgeoffrey.blake@arm.com    DPRINTF(Checkpoint, "Serializing Arm CpuLocalTimer\n");
3608512Sgeoffrey.blake@arm.com
3618512Sgeoffrey.blake@arm.com    uint32_t timer_control_serial = timerControl;
3628512Sgeoffrey.blake@arm.com    uint32_t watchdog_control_serial = watchdogControl;
3638512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(timer_control_serial);
3648512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(watchdog_control_serial);
3658512Sgeoffrey.blake@arm.com
3668512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(rawIntTimer);
3678512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(rawIntWatchdog);
3688512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(rawResetWatchdog);
3698512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(watchdogDisableReg);
3708512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(pendingIntTimer);
3718512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(pendingIntWatchdog);
3728512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(timerLoadValue);
3738512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(watchdogLoadValue);
3748512Sgeoffrey.blake@arm.com
3758512Sgeoffrey.blake@arm.com    bool timer_is_in_event = timerZeroEvent.scheduled();
3768512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(timer_is_in_event);
3778512Sgeoffrey.blake@arm.com    bool watchdog_is_in_event = watchdogZeroEvent.scheduled();
3788512Sgeoffrey.blake@arm.com    SERIALIZE_SCALAR(watchdog_is_in_event);
3798512Sgeoffrey.blake@arm.com
3808512Sgeoffrey.blake@arm.com    Tick timer_event_time;
3818512Sgeoffrey.blake@arm.com    if (timer_is_in_event){
3828512Sgeoffrey.blake@arm.com        timer_event_time = timerZeroEvent.when();
3838512Sgeoffrey.blake@arm.com        SERIALIZE_SCALAR(timer_event_time);
3848512Sgeoffrey.blake@arm.com    }
3858512Sgeoffrey.blake@arm.com    Tick watchdog_event_time;
3868512Sgeoffrey.blake@arm.com    if (watchdog_is_in_event){
3878512Sgeoffrey.blake@arm.com        watchdog_event_time = watchdogZeroEvent.when();
3888512Sgeoffrey.blake@arm.com        SERIALIZE_SCALAR(watchdog_event_time);
3898512Sgeoffrey.blake@arm.com    }
3908512Sgeoffrey.blake@arm.com}
3918512Sgeoffrey.blake@arm.com
3928512Sgeoffrey.blake@arm.comvoid
39310905Sandreas.sandberg@arm.comCpuLocalTimer::Timer::unserialize(CheckpointIn &cp)
3948512Sgeoffrey.blake@arm.com{
3958512Sgeoffrey.blake@arm.com    DPRINTF(Checkpoint, "Unserializing Arm CpuLocalTimer\n");
3968512Sgeoffrey.blake@arm.com
3978512Sgeoffrey.blake@arm.com    uint32_t timer_control_serial;
3988512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(timer_control_serial);
3998512Sgeoffrey.blake@arm.com    timerControl = timer_control_serial;
4008512Sgeoffrey.blake@arm.com    uint32_t watchdog_control_serial;
4018512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(watchdog_control_serial);
4028512Sgeoffrey.blake@arm.com    watchdogControl = watchdog_control_serial;
4038512Sgeoffrey.blake@arm.com
4048512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(rawIntTimer);
4058512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(rawIntWatchdog);
4068512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(rawResetWatchdog);
4078512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(watchdogDisableReg);
4088512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(pendingIntTimer);
4098512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(pendingIntWatchdog);
4108512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(timerLoadValue);
4118512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(watchdogLoadValue);
4128512Sgeoffrey.blake@arm.com
4138512Sgeoffrey.blake@arm.com    bool timer_is_in_event;
4148512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(timer_is_in_event);
4158512Sgeoffrey.blake@arm.com    bool watchdog_is_in_event;
4168512Sgeoffrey.blake@arm.com    UNSERIALIZE_SCALAR(watchdog_is_in_event);
4178512Sgeoffrey.blake@arm.com
4188512Sgeoffrey.blake@arm.com    Tick timer_event_time;
4198512Sgeoffrey.blake@arm.com    if (timer_is_in_event){
4208512Sgeoffrey.blake@arm.com        UNSERIALIZE_SCALAR(timer_event_time);
4218512Sgeoffrey.blake@arm.com        parent->schedule(timerZeroEvent, timer_event_time);
4228512Sgeoffrey.blake@arm.com    }
4238512Sgeoffrey.blake@arm.com    Tick watchdog_event_time;
4248512Sgeoffrey.blake@arm.com    if (watchdog_is_in_event) {
4258512Sgeoffrey.blake@arm.com        UNSERIALIZE_SCALAR(watchdog_event_time);
4268512Sgeoffrey.blake@arm.com        parent->schedule(watchdogZeroEvent, watchdog_event_time);
4278512Sgeoffrey.blake@arm.com    }
4288512Sgeoffrey.blake@arm.com}
4298512Sgeoffrey.blake@arm.com
4308512Sgeoffrey.blake@arm.com
4318512Sgeoffrey.blake@arm.com
4328512Sgeoffrey.blake@arm.comvoid
43310905Sandreas.sandberg@arm.comCpuLocalTimer::serialize(CheckpointOut &cp) const
4348512Sgeoffrey.blake@arm.com{
43513106Sgiacomo.travaglini@arm.com    for (int i = 0; i < sys->numContexts(); i++)
43613106Sgiacomo.travaglini@arm.com        localTimer[i]->serializeSection(cp, csprintf("timer%d", i));
4378512Sgeoffrey.blake@arm.com}
4388512Sgeoffrey.blake@arm.com
4398512Sgeoffrey.blake@arm.comvoid
44010905Sandreas.sandberg@arm.comCpuLocalTimer::unserialize(CheckpointIn &cp)
4418512Sgeoffrey.blake@arm.com{
44213106Sgiacomo.travaglini@arm.com    for (int i = 0; i < sys->numContexts(); i++)
44313106Sgiacomo.travaglini@arm.com        localTimer[i]->unserializeSection(cp, csprintf("timer%d", i));
4448512Sgeoffrey.blake@arm.com}
4458512Sgeoffrey.blake@arm.com
4468512Sgeoffrey.blake@arm.comCpuLocalTimer *
4478512Sgeoffrey.blake@arm.comCpuLocalTimerParams::create()
4488512Sgeoffrey.blake@arm.com{
4498512Sgeoffrey.blake@arm.com    return new CpuLocalTimer(this);
4508512Sgeoffrey.blake@arm.com}
451