rv_ctrl.cc revision 11421
17584SAli.Saidi@arm.com/*
211011SAndreas.Sandberg@ARM.com * Copyright (c) 2010,2013,2015 ARM Limited
37584SAli.Saidi@arm.com * All rights reserved
47584SAli.Saidi@arm.com *
57584SAli.Saidi@arm.com * The license below extends only to copyright in the software and shall
67584SAli.Saidi@arm.com * not be construed as granting a license to any other intellectual
77584SAli.Saidi@arm.com * property including but not limited to intellectual property relating
87584SAli.Saidi@arm.com * to a hardware implementation of the functionality of the software
97584SAli.Saidi@arm.com * licensed hereunder.  You may use the software subject to the license
107584SAli.Saidi@arm.com * terms below provided that you ensure that this notice is replicated
117584SAli.Saidi@arm.com * unmodified and in its entirety in all distributions of the software,
127584SAli.Saidi@arm.com * modified or unmodified, in source code or in binary form.
137584SAli.Saidi@arm.com *
147584SAli.Saidi@arm.com * Redistribution and use in source and binary forms, with or without
157584SAli.Saidi@arm.com * modification, are permitted provided that the following conditions are
167584SAli.Saidi@arm.com * met: redistributions of source code must retain the above copyright
177584SAli.Saidi@arm.com * notice, this list of conditions and the following disclaimer;
187584SAli.Saidi@arm.com * redistributions in binary form must reproduce the above copyright
197584SAli.Saidi@arm.com * notice, this list of conditions and the following disclaimer in the
207584SAli.Saidi@arm.com * documentation and/or other materials provided with the distribution;
217584SAli.Saidi@arm.com * neither the name of the copyright holders nor the names of its
227584SAli.Saidi@arm.com * contributors may be used to endorse or promote products derived from
237584SAli.Saidi@arm.com * this software without specific prior written permission.
247584SAli.Saidi@arm.com *
257584SAli.Saidi@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
267584SAli.Saidi@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
277584SAli.Saidi@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
287584SAli.Saidi@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
297584SAli.Saidi@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
307584SAli.Saidi@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
317584SAli.Saidi@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
327584SAli.Saidi@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
337584SAli.Saidi@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
347584SAli.Saidi@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
357584SAli.Saidi@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
367584SAli.Saidi@arm.com *
377584SAli.Saidi@arm.com * Authors: Ali Saidi
387584SAli.Saidi@arm.com */
397584SAli.Saidi@arm.com
4011421Sdavid.guillen@arm.com#include "dev/arm/rv_ctrl.hh"
4111421Sdavid.guillen@arm.com
427584SAli.Saidi@arm.com#include "base/trace.hh"
439958Smatt.evans@arm.com#include "debug/RVCTRL.hh"
447584SAli.Saidi@arm.com#include "mem/packet.hh"
457584SAli.Saidi@arm.com#include "mem/packet_access.hh"
4611421Sdavid.guillen@arm.com#include "sim/power/thermal_model.hh"
4711421Sdavid.guillen@arm.com#include "sim/system.hh"
4811011SAndreas.Sandberg@ARM.com#include "sim/voltage_domain.hh"
497584SAli.Saidi@arm.com
507584SAli.Saidi@arm.comRealViewCtrl::RealViewCtrl(Params *p)
519958Smatt.evans@arm.com    : BasicPioDevice(p, 0xD4), flags(0), scData(0)
527584SAli.Saidi@arm.com{
537584SAli.Saidi@arm.com}
547584SAli.Saidi@arm.com
557584SAli.Saidi@arm.comTick
567584SAli.Saidi@arm.comRealViewCtrl::read(PacketPtr pkt)
577584SAli.Saidi@arm.com{
587584SAli.Saidi@arm.com    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
597584SAli.Saidi@arm.com    assert(pkt->getSize() == 4);
607584SAli.Saidi@arm.com    Addr daddr = pkt->getAddr() - pioAddr;
617584SAli.Saidi@arm.com
627584SAli.Saidi@arm.com    switch(daddr) {
638524SAli.Saidi@ARM.com      case ProcId0:
648524SAli.Saidi@ARM.com        pkt->set(params()->proc_id0);
658524SAli.Saidi@ARM.com        break;
668524SAli.Saidi@ARM.com      case ProcId1:
678524SAli.Saidi@ARM.com        pkt->set(params()->proc_id1);
687584SAli.Saidi@arm.com        break;
697584SAli.Saidi@arm.com      case Clock24:
707584SAli.Saidi@arm.com        Tick clk;
719004Skoansin.tan@gmail.com        clk = SimClock::Float::MHz * curTick() * 24;
727584SAli.Saidi@arm.com        pkt->set((uint32_t)(clk));
737584SAli.Saidi@arm.com        break;
748060SAli.Saidi@ARM.com      case Clock100:
758060SAli.Saidi@ARM.com        Tick clk100;
769004Skoansin.tan@gmail.com        clk100 = SimClock::Float::MHz * curTick() * 100;
778060SAli.Saidi@ARM.com        pkt->set((uint32_t)(clk100));
788060SAli.Saidi@ARM.com        break;
797584SAli.Saidi@arm.com      case Flash:
807584SAli.Saidi@arm.com        pkt->set<uint32_t>(0);
817584SAli.Saidi@arm.com        break;
827950SAli.Saidi@ARM.com      case Clcd:
837950SAli.Saidi@ARM.com        pkt->set<uint32_t>(0x00001F00);
847950SAli.Saidi@ARM.com        break;
857950SAli.Saidi@ARM.com      case Osc0:
867950SAli.Saidi@ARM.com        pkt->set<uint32_t>(0x00012C5C);
877950SAli.Saidi@ARM.com        break;
887950SAli.Saidi@ARM.com      case Osc1:
897950SAli.Saidi@ARM.com        pkt->set<uint32_t>(0x00002CC0);
907950SAli.Saidi@ARM.com        break;
917950SAli.Saidi@ARM.com      case Osc2:
927950SAli.Saidi@ARM.com        pkt->set<uint32_t>(0x00002C75);
937950SAli.Saidi@ARM.com        break;
947950SAli.Saidi@ARM.com      case Osc3:
957950SAli.Saidi@ARM.com        pkt->set<uint32_t>(0x00020211);
967950SAli.Saidi@ARM.com        break;
977950SAli.Saidi@ARM.com      case Osc4:
987950SAli.Saidi@ARM.com        pkt->set<uint32_t>(0x00002C75);
997950SAli.Saidi@ARM.com        break;
1007950SAli.Saidi@ARM.com      case Lock:
1017950SAli.Saidi@ARM.com        pkt->set<uint32_t>(sysLock);
1027950SAli.Saidi@ARM.com        break;
1038281SAli.Saidi@ARM.com      case Flags:
1048281SAli.Saidi@ARM.com        pkt->set<uint32_t>(flags);
1058281SAli.Saidi@ARM.com        break;
1068299Schander.sudanthi@arm.com      case IdReg:
1078299Schander.sudanthi@arm.com        pkt->set<uint32_t>(params()->idreg);
1088299Schander.sudanthi@arm.com        break;
1098870SAli.Saidi@ARM.com      case CfgStat:
1108870SAli.Saidi@ARM.com        pkt->set<uint32_t>(1);
1118988SAli.Saidi@ARM.com        break;
1129958Smatt.evans@arm.com      case CfgData:
1139958Smatt.evans@arm.com        pkt->set<uint32_t>(scData);
1149958Smatt.evans@arm.com        DPRINTF(RVCTRL, "Read %#x from SCReg\n", scData);
1159958Smatt.evans@arm.com        break;
1169958Smatt.evans@arm.com      case CfgCtrl:
1179958Smatt.evans@arm.com        pkt->set<uint32_t>(0); // not busy
1189958Smatt.evans@arm.com        DPRINTF(RVCTRL, "Read 0 from CfgCtrl\n");
1199958Smatt.evans@arm.com        break;
1207584SAli.Saidi@arm.com      default:
1218299Schander.sudanthi@arm.com        warn("Tried to read RealView I/O at offset %#x that doesn't exist\n",
1228299Schander.sudanthi@arm.com             daddr);
1239958Smatt.evans@arm.com        pkt->set<uint32_t>(0);
1247584SAli.Saidi@arm.com        break;
1257584SAli.Saidi@arm.com    }
1267584SAli.Saidi@arm.com    pkt->makeAtomicResponse();
1277584SAli.Saidi@arm.com    return pioDelay;
1287584SAli.Saidi@arm.com
1297584SAli.Saidi@arm.com}
1307584SAli.Saidi@arm.com
1317584SAli.Saidi@arm.comTick
1327584SAli.Saidi@arm.comRealViewCtrl::write(PacketPtr pkt)
1337584SAli.Saidi@arm.com{
1347584SAli.Saidi@arm.com    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
1357584SAli.Saidi@arm.com
1367584SAli.Saidi@arm.com    Addr daddr = pkt->getAddr() - pioAddr;
1377584SAli.Saidi@arm.com    switch (daddr) {
1387584SAli.Saidi@arm.com      case Flash:
1397950SAli.Saidi@ARM.com      case Clcd:
1407950SAli.Saidi@ARM.com      case Osc0:
1417950SAli.Saidi@ARM.com      case Osc1:
1427950SAli.Saidi@ARM.com      case Osc2:
1437950SAli.Saidi@ARM.com      case Osc3:
1447950SAli.Saidi@ARM.com      case Osc4:
1457950SAli.Saidi@ARM.com        break;
1467950SAli.Saidi@ARM.com      case Lock:
1477950SAli.Saidi@ARM.com        sysLock.lockVal = pkt->get<uint16_t>();
1487584SAli.Saidi@arm.com        break;
1498281SAli.Saidi@ARM.com      case Flags:
1508281SAli.Saidi@ARM.com        flags = pkt->get<uint32_t>();
1518281SAli.Saidi@ARM.com        break;
1528524SAli.Saidi@ARM.com      case FlagsClr:
1538524SAli.Saidi@ARM.com        flags = 0;
1548524SAli.Saidi@ARM.com        break;
1559958Smatt.evans@arm.com      case CfgData:
1569958Smatt.evans@arm.com        scData = pkt->get<uint32_t>();
1579958Smatt.evans@arm.com        break;
1589958Smatt.evans@arm.com      case CfgCtrl: {
1599958Smatt.evans@arm.com          // A request is being submitted to read/write the system control
1609958Smatt.evans@arm.com          // registers.  See
1619958Smatt.evans@arm.com          // http://infocenter.arm.com/help/topic/com.arm.doc.dui0447h/CACDEFGH.html
16211011SAndreas.Sandberg@ARM.com          CfgCtrlReg req = pkt->get<uint32_t>();
16311011SAndreas.Sandberg@ARM.com          if (!req.start) {
16411011SAndreas.Sandberg@ARM.com              DPRINTF(RVCTRL, "SCReg: write %#x to ctrl but not starting\n",
16511011SAndreas.Sandberg@ARM.com                      req);
16611011SAndreas.Sandberg@ARM.com              break;
16711011SAndreas.Sandberg@ARM.com          }
1689958Smatt.evans@arm.com
16911011SAndreas.Sandberg@ARM.com          auto it_dev(devices.find(req & CFG_CTRL_ADDR_MASK));
17011011SAndreas.Sandberg@ARM.com          if (it_dev == devices.end()) {
17111011SAndreas.Sandberg@ARM.com              warn_once("SCReg: Access to unknown device "
17211011SAndreas.Sandberg@ARM.com                        "dcc%d:site%d:pos%d:fn%d:dev%d\n",
17311011SAndreas.Sandberg@ARM.com                        req.dcc, req.site, req.pos, req.func, req.dev);
17411011SAndreas.Sandberg@ARM.com              break;
17511011SAndreas.Sandberg@ARM.com          }
17611011SAndreas.Sandberg@ARM.com
17711011SAndreas.Sandberg@ARM.com          // Service the request as a read or write depending on the
17811011SAndreas.Sandberg@ARM.com          // wr bit in the control register.
17911011SAndreas.Sandberg@ARM.com          Device &dev(*it_dev->second);
18011011SAndreas.Sandberg@ARM.com          if (req.wr) {
18111011SAndreas.Sandberg@ARM.com              DPRINTF(RVCTRL, "SCReg: Writing %#x (ctrlWr %#x)\n",
18211011SAndreas.Sandberg@ARM.com                      scData, req);
18311011SAndreas.Sandberg@ARM.com              dev.write(scData);
18411011SAndreas.Sandberg@ARM.com
1859958Smatt.evans@arm.com          } else {
18611011SAndreas.Sandberg@ARM.com              scData = dev.read();
18711011SAndreas.Sandberg@ARM.com              DPRINTF(RVCTRL, "SCReg: Reading %#x (ctrlRd %#x)\n",
18811011SAndreas.Sandberg@ARM.com                      scData, req);
1899958Smatt.evans@arm.com          }
1909958Smatt.evans@arm.com      } break;
1919958Smatt.evans@arm.com      case CfgStat:     // Weird to write this
1927584SAli.Saidi@arm.com      default:
1939958Smatt.evans@arm.com        warn("Tried to write RVIO at offset %#x (data %#x) that doesn't exist\n",
1949958Smatt.evans@arm.com             daddr, pkt->get<uint32_t>());
1957584SAli.Saidi@arm.com        break;
1967584SAli.Saidi@arm.com    }
1977584SAli.Saidi@arm.com    pkt->makeAtomicResponse();
1987584SAli.Saidi@arm.com    return pioDelay;
1997584SAli.Saidi@arm.com}
2007584SAli.Saidi@arm.com
2017584SAli.Saidi@arm.comvoid
20210905Sandreas.sandberg@arm.comRealViewCtrl::serialize(CheckpointOut &cp) const
2037584SAli.Saidi@arm.com{
2048281SAli.Saidi@ARM.com    SERIALIZE_SCALAR(flags);
2057584SAli.Saidi@arm.com}
2067584SAli.Saidi@arm.com
2077584SAli.Saidi@arm.comvoid
20810905Sandreas.sandberg@arm.comRealViewCtrl::unserialize(CheckpointIn &cp)
2097584SAli.Saidi@arm.com{
2108281SAli.Saidi@ARM.com    UNSERIALIZE_SCALAR(flags);
2117584SAli.Saidi@arm.com}
2127584SAli.Saidi@arm.com
21311011SAndreas.Sandberg@ARM.comvoid
21411011SAndreas.Sandberg@ARM.comRealViewCtrl::registerDevice(DeviceFunc func, uint8_t site, uint8_t pos,
21511011SAndreas.Sandberg@ARM.com                             uint8_t dcc, uint16_t dev,
21611011SAndreas.Sandberg@ARM.com                             Device *handler)
21711011SAndreas.Sandberg@ARM.com{
21811011SAndreas.Sandberg@ARM.com    CfgCtrlReg addr = 0;
21911011SAndreas.Sandberg@ARM.com    addr.func = func;
22011011SAndreas.Sandberg@ARM.com    addr.site = site;
22111011SAndreas.Sandberg@ARM.com    addr.pos = pos;
22211011SAndreas.Sandberg@ARM.com    addr.dcc = dcc;
22311011SAndreas.Sandberg@ARM.com    addr.dev = dev;
22411011SAndreas.Sandberg@ARM.com
22511011SAndreas.Sandberg@ARM.com    if (devices.find(addr) != devices.end()) {
22611011SAndreas.Sandberg@ARM.com        fatal("Platform device dcc%d:site%d:pos%d:fn%d:dev%d "
22711011SAndreas.Sandberg@ARM.com              "already registered.",
22811011SAndreas.Sandberg@ARM.com              addr.dcc, addr.site, addr.pos, addr.func, addr.dev);
22911011SAndreas.Sandberg@ARM.com    }
23011011SAndreas.Sandberg@ARM.com
23111011SAndreas.Sandberg@ARM.com    devices[addr] = handler;
23211011SAndreas.Sandberg@ARM.com}
23311011SAndreas.Sandberg@ARM.com
23411011SAndreas.Sandberg@ARM.com
23511011SAndreas.Sandberg@ARM.comRealViewOsc::RealViewOsc(RealViewOscParams *p)
23611011SAndreas.Sandberg@ARM.com    : ClockDomain(p, p->voltage_domain),
23711011SAndreas.Sandberg@ARM.com      RealViewCtrl::Device(*p->parent, RealViewCtrl::FUNC_OSC,
23811011SAndreas.Sandberg@ARM.com                           p->site, p->position, p->dcc, p->device)
23911011SAndreas.Sandberg@ARM.com{
24011011SAndreas.Sandberg@ARM.com    if (SimClock::Float::s  / p->freq > UINT32_MAX) {
24111011SAndreas.Sandberg@ARM.com        fatal("Oscillator frequency out of range: %f\n",
24211011SAndreas.Sandberg@ARM.com            SimClock::Float::s  / p->freq / 1E6);
24311011SAndreas.Sandberg@ARM.com    }
24411011SAndreas.Sandberg@ARM.com
24511011SAndreas.Sandberg@ARM.com    _clockPeriod = p->freq;
24611011SAndreas.Sandberg@ARM.com}
24711011SAndreas.Sandberg@ARM.com
24811011SAndreas.Sandberg@ARM.comvoid
24911011SAndreas.Sandberg@ARM.comRealViewOsc::startup()
25011011SAndreas.Sandberg@ARM.com{
25111011SAndreas.Sandberg@ARM.com    // Tell dependent object to set their clock frequency
25211011SAndreas.Sandberg@ARM.com    for (auto m : members)
25311011SAndreas.Sandberg@ARM.com        m->updateClockPeriod();
25411011SAndreas.Sandberg@ARM.com}
25511011SAndreas.Sandberg@ARM.com
25611011SAndreas.Sandberg@ARM.comvoid
25711011SAndreas.Sandberg@ARM.comRealViewOsc::serialize(CheckpointOut &cp) const
25811011SAndreas.Sandberg@ARM.com{
25911011SAndreas.Sandberg@ARM.com    SERIALIZE_SCALAR(_clockPeriod);
26011011SAndreas.Sandberg@ARM.com}
26111011SAndreas.Sandberg@ARM.com
26211011SAndreas.Sandberg@ARM.comvoid
26311011SAndreas.Sandberg@ARM.comRealViewOsc::unserialize(CheckpointIn &cp)
26411011SAndreas.Sandberg@ARM.com{
26511011SAndreas.Sandberg@ARM.com    UNSERIALIZE_SCALAR(_clockPeriod);
26611011SAndreas.Sandberg@ARM.com}
26711011SAndreas.Sandberg@ARM.com
26811011SAndreas.Sandberg@ARM.comvoid
26911011SAndreas.Sandberg@ARM.comRealViewOsc::clockPeriod(Tick clock_period)
27011011SAndreas.Sandberg@ARM.com{
27111011SAndreas.Sandberg@ARM.com    panic_if(clock_period == 0, "%s has a clock period of zero\n", name());
27211011SAndreas.Sandberg@ARM.com
27311011SAndreas.Sandberg@ARM.com    // Align all members to the current tick
27411011SAndreas.Sandberg@ARM.com    for (auto m : members)
27511011SAndreas.Sandberg@ARM.com        m->updateClockPeriod();
27611011SAndreas.Sandberg@ARM.com
27711011SAndreas.Sandberg@ARM.com    _clockPeriod = clock_period;
27811011SAndreas.Sandberg@ARM.com
27911011SAndreas.Sandberg@ARM.com    // inform any derived clocks they need to updated their period
28011011SAndreas.Sandberg@ARM.com    for (auto m : children)
28111011SAndreas.Sandberg@ARM.com        m->updateClockPeriod();
28211011SAndreas.Sandberg@ARM.com}
28311011SAndreas.Sandberg@ARM.com
28411011SAndreas.Sandberg@ARM.comuint32_t
28511011SAndreas.Sandberg@ARM.comRealViewOsc::read() const
28611011SAndreas.Sandberg@ARM.com{
28711011SAndreas.Sandberg@ARM.com    const uint32_t freq(SimClock::Float::s / _clockPeriod);
28811011SAndreas.Sandberg@ARM.com    DPRINTF(RVCTRL, "Reading OSC frequency: %f MHz\n", freq / 1E6);
28911011SAndreas.Sandberg@ARM.com    return freq;
29011011SAndreas.Sandberg@ARM.com}
29111011SAndreas.Sandberg@ARM.com
29211011SAndreas.Sandberg@ARM.comvoid
29311011SAndreas.Sandberg@ARM.comRealViewOsc::write(uint32_t freq)
29411011SAndreas.Sandberg@ARM.com{
29511011SAndreas.Sandberg@ARM.com    DPRINTF(RVCTRL, "Setting new OSC frequency: %f MHz\n", freq / 1E6);
29611011SAndreas.Sandberg@ARM.com    clockPeriod(SimClock::Float::s / freq);
29711011SAndreas.Sandberg@ARM.com}
29811011SAndreas.Sandberg@ARM.com
29911421Sdavid.guillen@arm.comuint32_t
30011421Sdavid.guillen@arm.comRealViewTemperatureSensor::read() const
30111421Sdavid.guillen@arm.com{
30211421Sdavid.guillen@arm.com    // Temperature reported in uC
30311421Sdavid.guillen@arm.com    ThermalModel * tm = system->getThermalModel();
30411421Sdavid.guillen@arm.com    if (tm) {
30511421Sdavid.guillen@arm.com        double t = tm->getTemp();
30611421Sdavid.guillen@arm.com        if (t < 0)
30711421Sdavid.guillen@arm.com            warn("Temperature below zero!\n");
30811421Sdavid.guillen@arm.com        return fmax(0, t) * 1000000;
30911421Sdavid.guillen@arm.com    }
31011011SAndreas.Sandberg@ARM.com
31111421Sdavid.guillen@arm.com    // Report a dummy 25 degrees temperature
31211421Sdavid.guillen@arm.com    return 25000000;
31311421Sdavid.guillen@arm.com}
31411011SAndreas.Sandberg@ARM.com
3157584SAli.Saidi@arm.comRealViewCtrl *
3167584SAli.Saidi@arm.comRealViewCtrlParams::create()
3177584SAli.Saidi@arm.com{
3187584SAli.Saidi@arm.com    return new RealViewCtrl(this);
3197584SAli.Saidi@arm.com}
32011011SAndreas.Sandberg@ARM.com
32111011SAndreas.Sandberg@ARM.comRealViewOsc *
32211011SAndreas.Sandberg@ARM.comRealViewOscParams::create()
32311011SAndreas.Sandberg@ARM.com{
32411011SAndreas.Sandberg@ARM.com    return new RealViewOsc(this);
32511011SAndreas.Sandberg@ARM.com}
32611421Sdavid.guillen@arm.com
32711421Sdavid.guillen@arm.comRealViewTemperatureSensor *
32811421Sdavid.guillen@arm.comRealViewTemperatureSensorParams::create()
32911421Sdavid.guillen@arm.com{
33011421Sdavid.guillen@arm.com    return new RealViewTemperatureSensor(this);
33111421Sdavid.guillen@arm.com}
332