io_device.cc revision 3349
1545SN/A/*
22512SN/A * Copyright (c) 2006 The Regents of The University of Michigan
3545SN/A * All rights reserved.
4545SN/A *
5545SN/A * Redistribution and use in source and binary forms, with or without
6545SN/A * modification, are permitted provided that the following conditions are
7545SN/A * met: redistributions of source code must retain the above copyright
8545SN/A * notice, this list of conditions and the following disclaimer;
9545SN/A * redistributions in binary form must reproduce the above copyright
10545SN/A * notice, this list of conditions and the following disclaimer in the
11545SN/A * documentation and/or other materials provided with the distribution;
12545SN/A * neither the name of the copyright holders nor the names of its
13545SN/A * contributors may be used to endorse or promote products derived from
14545SN/A * this software without specific prior written permission.
15545SN/A *
16545SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17545SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18545SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19545SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20545SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21545SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22545SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23545SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24545SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25545SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26545SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Ali Saidi
292665Ssaidi@eecs.umich.edu *          Nathan Binkert
30545SN/A */
31545SN/A
323090Sstever@eecs.umich.edu#include "base/chunk_generator.hh"
332657Ssaidi@eecs.umich.edu#include "base/trace.hh"
34545SN/A#include "dev/io_device.hh"
35679SN/A#include "sim/builder.hh"
362901Ssaidi@eecs.umich.edu#include "sim/system.hh"
37545SN/A
382489SN/A
392901Ssaidi@eecs.umich.eduPioPort::PioPort(PioDevice *dev, System *s, std::string pname)
403091Sstever@eecs.umich.edu    : SimpleTimingPort(dev->name() + pname), device(dev)
412489SN/A{ }
422489SN/A
432489SN/A
442489SN/ATick
453349Sbinkertn@umich.eduPioPort::recvAtomic(PacketPtr pkt)
462489SN/A{
473091Sstever@eecs.umich.edu    return pkt->isRead() ? device->read(pkt) : device->write(pkt);
482489SN/A}
492489SN/A
502489SN/Avoid
512521SN/APioPort::getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
522489SN/A{
532521SN/A    snoop.clear();
542521SN/A    device->addressRanges(resp);
552489SN/A}
562489SN/A
572489SN/A
58545SN/APioDevice::~PioDevice()
59545SN/A{
602384SN/A    if (pioPort)
612489SN/A        delete pioPort;
62545SN/A}
63545SN/A
642542SN/Avoid
652541SN/APioDevice::init()
662541SN/A{
672541SN/A    if (!pioPort)
682541SN/A        panic("Pio port not connected to anything!");
692541SN/A    pioPort->sendStatusChange(Port::RangeChange);
702541SN/A}
712541SN/A
722901Ssaidi@eecs.umich.edu
732901Ssaidi@eecs.umich.eduunsigned int
742901Ssaidi@eecs.umich.eduPioDevice::drain(Event *de)
752901Ssaidi@eecs.umich.edu{
762901Ssaidi@eecs.umich.edu    unsigned int count;
772901Ssaidi@eecs.umich.edu    count = pioPort->drain(de);
782901Ssaidi@eecs.umich.edu    if (count)
792901Ssaidi@eecs.umich.edu        changeState(Draining);
802901Ssaidi@eecs.umich.edu    else
812901Ssaidi@eecs.umich.edu        changeState(Drained);
822901Ssaidi@eecs.umich.edu    return count;
832901Ssaidi@eecs.umich.edu}
842901Ssaidi@eecs.umich.edu
852539SN/Avoid
862539SN/ABasicPioDevice::addressRanges(AddrRangeList &range_list)
872539SN/A{
882539SN/A    assert(pioSize != 0);
892539SN/A    range_list.clear();
902539SN/A    range_list.push_back(RangeSize(pioAddr, pioSize));
912539SN/A}
922539SN/A
932489SN/A
942901Ssaidi@eecs.umich.eduDmaPort::DmaPort(DmaDevice *dev, System *s)
952901Ssaidi@eecs.umich.edu    : Port(dev->name() + "-dmaport"), device(dev), sys(s), pendingCount(0),
962901Ssaidi@eecs.umich.edu      actionInProgress(0), drainEvent(NULL)
972489SN/A{ }
982489SN/A
992489SN/Abool
1003349Sbinkertn@umich.eduDmaPort::recvTiming(PacketPtr pkt)
1012384SN/A{
1022685Ssaidi@eecs.umich.edu
1032685Ssaidi@eecs.umich.edu
1042685Ssaidi@eecs.umich.edu    if (pkt->result == Packet::Nacked) {
1052685Ssaidi@eecs.umich.edu        DPRINTF(DMA, "Received nacked Pkt %#x with State: %#x Addr: %#x\n",
1062685Ssaidi@eecs.umich.edu               pkt, pkt->senderState, pkt->getAddr());
1072685Ssaidi@eecs.umich.edu        pkt->reinitNacked();
1082685Ssaidi@eecs.umich.edu        sendDma(pkt, true);
1092685Ssaidi@eecs.umich.edu    } else if (pkt->senderState) {
1102565SN/A        DmaReqState *state;
1112685Ssaidi@eecs.umich.edu        DPRINTF(DMA, "Received response Pkt %#x with State: %#x Addr: %#x\n",
1122685Ssaidi@eecs.umich.edu               pkt, pkt->senderState, pkt->getAddr());
1132641Sstever@eecs.umich.edu        state = dynamic_cast<DmaReqState*>(pkt->senderState);
1142685Ssaidi@eecs.umich.edu        pendingCount--;
1152685Ssaidi@eecs.umich.edu
1162685Ssaidi@eecs.umich.edu        assert(pendingCount >= 0);
1172657Ssaidi@eecs.umich.edu        assert(state);
1182685Ssaidi@eecs.umich.edu
1192685Ssaidi@eecs.umich.edu        state->numBytes += pkt->req->getSize();
1202685Ssaidi@eecs.umich.edu        if (state->totBytes == state->numBytes) {
1212685Ssaidi@eecs.umich.edu            state->completionEvent->process();
1222685Ssaidi@eecs.umich.edu            delete state;
1232685Ssaidi@eecs.umich.edu        }
1242630SN/A        delete pkt->req;
1252630SN/A        delete pkt;
1262901Ssaidi@eecs.umich.edu
1272901Ssaidi@eecs.umich.edu        if (pendingCount == 0 && drainEvent) {
1282901Ssaidi@eecs.umich.edu            drainEvent->process();
1292901Ssaidi@eecs.umich.edu            drainEvent = NULL;
1302901Ssaidi@eecs.umich.edu        }
1312569SN/A    }  else {
1322685Ssaidi@eecs.umich.edu        panic("Got packet without sender state... huh?\n");
1332565SN/A    }
1342569SN/A
1352657Ssaidi@eecs.umich.edu    return true;
1362384SN/A}
137679SN/A
1382521SN/ADmaDevice::DmaDevice(Params *p)
1392565SN/A    : PioDevice(p), dmaPort(NULL)
1402565SN/A{ }
1412384SN/A
1422901Ssaidi@eecs.umich.edu
1432901Ssaidi@eecs.umich.eduunsigned int
1442901Ssaidi@eecs.umich.eduDmaDevice::drain(Event *de)
1452901Ssaidi@eecs.umich.edu{
1462901Ssaidi@eecs.umich.edu    unsigned int count;
1472901Ssaidi@eecs.umich.edu    count = pioPort->drain(de) + dmaPort->drain(de);
1482901Ssaidi@eecs.umich.edu    if (count)
1492901Ssaidi@eecs.umich.edu        changeState(Draining);
1502901Ssaidi@eecs.umich.edu    else
1512901Ssaidi@eecs.umich.edu        changeState(Drained);
1522901Ssaidi@eecs.umich.edu    return count;
1532901Ssaidi@eecs.umich.edu}
1542901Ssaidi@eecs.umich.edu
1552901Ssaidi@eecs.umich.eduunsigned int
1562901Ssaidi@eecs.umich.eduDmaPort::drain(Event *de)
1572901Ssaidi@eecs.umich.edu{
1582901Ssaidi@eecs.umich.edu    if (pendingCount == 0)
1592901Ssaidi@eecs.umich.edu        return 0;
1602901Ssaidi@eecs.umich.edu    drainEvent = de;
1612901Ssaidi@eecs.umich.edu    return 1;
1622901Ssaidi@eecs.umich.edu}
1632901Ssaidi@eecs.umich.edu
1642901Ssaidi@eecs.umich.edu
1652384SN/Avoid
1662489SN/ADmaPort::recvRetry()
1672489SN/A{
1683349Sbinkertn@umich.edu    PacketPtr pkt = transmitList.front();
1692659Ssaidi@eecs.umich.edu    bool result = true;
1702659Ssaidi@eecs.umich.edu    while (result && transmitList.size()) {
1712659Ssaidi@eecs.umich.edu        DPRINTF(DMA, "Retry on  Packet %#x with senderState: %#x\n",
1722659Ssaidi@eecs.umich.edu                   pkt, pkt->senderState);
1732659Ssaidi@eecs.umich.edu        result = sendTiming(pkt);
1742659Ssaidi@eecs.umich.edu        if (result) {
1752659Ssaidi@eecs.umich.edu            DPRINTF(DMA, "-- Done\n");
1762659Ssaidi@eecs.umich.edu            transmitList.pop_front();
1772659Ssaidi@eecs.umich.edu        } else {
1782659Ssaidi@eecs.umich.edu            DPRINTF(DMA, "-- Failed, queued\n");
1792659Ssaidi@eecs.umich.edu        }
1802657Ssaidi@eecs.umich.edu    }
1812489SN/A}
1822641Sstever@eecs.umich.edu
1832641Sstever@eecs.umich.edu
1842489SN/Avoid
1852641Sstever@eecs.umich.eduDmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1862641Sstever@eecs.umich.edu                   uint8_t *data)
1872384SN/A{
1882384SN/A    assert(event);
1892384SN/A
1902901Ssaidi@eecs.umich.edu    assert(device->getState() == SimObject::Running);
1912901Ssaidi@eecs.umich.edu
1922685Ssaidi@eecs.umich.edu    DmaReqState *reqState = new DmaReqState(event, this, size);
1932384SN/A
1942406SN/A    for (ChunkGenerator gen(addr, size, peerBlockSize());
1952406SN/A         !gen.done(); gen.next()) {
1962663Sstever@eecs.umich.edu            Request *req = new Request(gen.addr(), gen.size(), 0);
1973349Sbinkertn@umich.edu            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
1982641Sstever@eecs.umich.edu
1992384SN/A            // Increment the data pointer on a write
2002566SN/A            if (data)
2012685Ssaidi@eecs.umich.edu                pkt->dataStatic(data + gen.complete());
2022641Sstever@eecs.umich.edu
2032685Ssaidi@eecs.umich.edu            pkt->senderState = reqState;
2042641Sstever@eecs.umich.edu
2052565SN/A            assert(pendingCount >= 0);
2062565SN/A            pendingCount++;
2072566SN/A            sendDma(pkt);
2082384SN/A    }
2092901Ssaidi@eecs.umich.edu
2102384SN/A}
2112384SN/A
2122384SN/A
2132384SN/Avoid
2143349Sbinkertn@umich.eduDmaPort::sendDma(PacketPtr pkt, bool front)
2152384SN/A{
2162901Ssaidi@eecs.umich.edu    // some kind of selction between access methods
2172901Ssaidi@eecs.umich.edu    // more work is going to have to be done to make
2182901Ssaidi@eecs.umich.edu    // switching actually work
2192902Ssaidi@eecs.umich.edu
2202901Ssaidi@eecs.umich.edu    System::MemoryMode state = sys->getMemoryMode();
2212901Ssaidi@eecs.umich.edu    if (state == System::Timing) {
2222901Ssaidi@eecs.umich.edu        DPRINTF(DMA, "Attempting to send Packet %#x with addr: %#x\n",
2232901Ssaidi@eecs.umich.edu                pkt, pkt->getAddr());
2242901Ssaidi@eecs.umich.edu        if (transmitList.size() || !sendTiming(pkt)) {
2252901Ssaidi@eecs.umich.edu            if (front)
2262901Ssaidi@eecs.umich.edu                transmitList.push_front(pkt);
2272901Ssaidi@eecs.umich.edu            else
2282901Ssaidi@eecs.umich.edu                transmitList.push_back(pkt);
2292901Ssaidi@eecs.umich.edu            DPRINTF(DMA, "-- Failed: queued\n");
2302901Ssaidi@eecs.umich.edu        } else {
2312901Ssaidi@eecs.umich.edu            DPRINTF(DMA, "-- Done\n");
2322901Ssaidi@eecs.umich.edu        }
2332901Ssaidi@eecs.umich.edu    } else if (state == System::Atomic) {
2342902Ssaidi@eecs.umich.edu        Tick lat;
2352902Ssaidi@eecs.umich.edu        lat = sendAtomic(pkt);
2362901Ssaidi@eecs.umich.edu        assert(pkt->senderState);
2372901Ssaidi@eecs.umich.edu        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
2382901Ssaidi@eecs.umich.edu        assert(state);
2392384SN/A
2402901Ssaidi@eecs.umich.edu        state->numBytes += pkt->req->getSize();
2412901Ssaidi@eecs.umich.edu        if (state->totBytes == state->numBytes) {
2422902Ssaidi@eecs.umich.edu            state->completionEvent->schedule(curTick + lat);
2432901Ssaidi@eecs.umich.edu            delete state;
2442901Ssaidi@eecs.umich.edu            delete pkt->req;
2452901Ssaidi@eecs.umich.edu        }
2462901Ssaidi@eecs.umich.edu        pendingCount--;
2472901Ssaidi@eecs.umich.edu        assert(pendingCount >= 0);
2482901Ssaidi@eecs.umich.edu        delete pkt;
2492566SN/A
2502901Ssaidi@eecs.umich.edu        if (pendingCount == 0 && drainEvent) {
2512901Ssaidi@eecs.umich.edu            drainEvent->process();
2522901Ssaidi@eecs.umich.edu            drainEvent = NULL;
2532901Ssaidi@eecs.umich.edu        }
2542901Ssaidi@eecs.umich.edu
2552384SN/A   } else
2562384SN/A       panic("Unknown memory command state.");
2572384SN/A}
258545SN/A
259545SN/ADmaDevice::~DmaDevice()
260545SN/A{
2612489SN/A    if (dmaPort)
2622489SN/A        delete dmaPort;
263545SN/A}
264545SN/A
265679SN/A
266