io_device.cc revision 8742
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"
348232Snate@binkert.org#include "debug/BusAddrRanges.hh"
358232Snate@binkert.org#include "debug/DMA.hh"
36545SN/A#include "dev/io_device.hh"
372901Ssaidi@eecs.umich.edu#include "sim/system.hh"
38545SN/A
392901Ssaidi@eecs.umich.eduPioPort::PioPort(PioDevice *dev, System *s, std::string pname)
403401Sktlim@umich.edu    : SimpleTimingPort(dev->name() + pname, dev), 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
514475Sstever@eecs.umich.eduPioPort::getDeviceAddressRanges(AddrRangeList &resp, bool &snoop)
522489SN/A{
534475Sstever@eecs.umich.edu    snoop = false;
542521SN/A    device->addressRanges(resp);
557729SAli.Saidi@ARM.com    for (AddrRangeIter i = resp.begin(); i != resp.end(); i++)
567729SAli.Saidi@ARM.com         DPRINTF(BusAddrRanges, "Adding Range %#x-%#x\n", i->start, i->end);
572489SN/A}
582489SN/A
592489SN/A
604762Snate@binkert.orgPioDevice::PioDevice(const Params *p)
618742Sgblack@eecs.umich.edu    : MemObject(p), sys(p->system), pioPort(NULL)
624762Snate@binkert.org{}
634762Snate@binkert.org
64545SN/APioDevice::~PioDevice()
65545SN/A{
662384SN/A    if (pioPort)
672489SN/A        delete pioPort;
68545SN/A}
69545SN/A
702542SN/Avoid
712541SN/APioDevice::init()
722541SN/A{
732541SN/A    if (!pioPort)
742541SN/A        panic("Pio port not connected to anything!");
752541SN/A    pioPort->sendStatusChange(Port::RangeChange);
762541SN/A}
772541SN/A
782901Ssaidi@eecs.umich.edu
792901Ssaidi@eecs.umich.eduunsigned int
802901Ssaidi@eecs.umich.eduPioDevice::drain(Event *de)
812901Ssaidi@eecs.umich.edu{
822901Ssaidi@eecs.umich.edu    unsigned int count;
832901Ssaidi@eecs.umich.edu    count = pioPort->drain(de);
842901Ssaidi@eecs.umich.edu    if (count)
852901Ssaidi@eecs.umich.edu        changeState(Draining);
862901Ssaidi@eecs.umich.edu    else
872901Ssaidi@eecs.umich.edu        changeState(Drained);
882901Ssaidi@eecs.umich.edu    return count;
892901Ssaidi@eecs.umich.edu}
902901Ssaidi@eecs.umich.edu
914762Snate@binkert.orgBasicPioDevice::BasicPioDevice(const Params *p)
924762Snate@binkert.org    : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
934762Snate@binkert.org      pioDelay(p->pio_latency)
944762Snate@binkert.org{}
954762Snate@binkert.org
962539SN/Avoid
972539SN/ABasicPioDevice::addressRanges(AddrRangeList &range_list)
982539SN/A{
992539SN/A    assert(pioSize != 0);
1002539SN/A    range_list.clear();
1017729SAli.Saidi@ARM.com    DPRINTF(BusAddrRanges, "registering range: %#x-%#x\n", pioAddr, pioSize);
1022539SN/A    range_list.push_back(RangeSize(pioAddr, pioSize));
1032539SN/A}
1042539SN/A
1052489SN/A
1067403SAli.Saidi@ARM.comDmaPort::DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff)
1073401Sktlim@umich.edu    : Port(dev->name() + "-dmaport", dev), device(dev), sys(s),
1084435Ssaidi@eecs.umich.edu      pendingCount(0), actionInProgress(0), drainEvent(NULL),
1097403SAli.Saidi@ARM.com      backoffTime(0), minBackoffDelay(min_backoff),
1107403SAli.Saidi@ARM.com      maxBackoffDelay(max_backoff), inRetry(false), backoffEvent(this)
1112489SN/A{ }
1122489SN/A
1132489SN/Abool
1143349Sbinkertn@umich.eduDmaPort::recvTiming(PacketPtr pkt)
1152384SN/A{
1164870Sstever@eecs.umich.edu    if (pkt->wasNacked()) {
1174739Sstever@eecs.umich.edu        DPRINTF(DMA, "Received nacked %s addr %#x\n",
1184739Sstever@eecs.umich.edu                pkt->cmdString(), pkt->getAddr());
1194435Ssaidi@eecs.umich.edu
1207403SAli.Saidi@ARM.com        if (backoffTime < minBackoffDelay)
1217403SAli.Saidi@ARM.com            backoffTime = minBackoffDelay;
1227403SAli.Saidi@ARM.com        else if (backoffTime < maxBackoffDelay)
1234435Ssaidi@eecs.umich.edu            backoffTime <<= 1;
1244435Ssaidi@eecs.umich.edu
1257823Ssteve.reinhardt@amd.com        reschedule(backoffEvent, curTick() + backoffTime, true);
1264435Ssaidi@eecs.umich.edu
1274435Ssaidi@eecs.umich.edu        DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime);
1284435Ssaidi@eecs.umich.edu
1292685Ssaidi@eecs.umich.edu        pkt->reinitNacked();
1304435Ssaidi@eecs.umich.edu        queueDma(pkt, true);
1312685Ssaidi@eecs.umich.edu    } else if (pkt->senderState) {
1322565SN/A        DmaReqState *state;
1334435Ssaidi@eecs.umich.edu        backoffTime >>= 2;
1344435Ssaidi@eecs.umich.edu
1354739Sstever@eecs.umich.edu        DPRINTF(DMA, "Received response %s addr %#x size %#x\n",
1364739Sstever@eecs.umich.edu                pkt->cmdString(), pkt->getAddr(), pkt->req->getSize());
1372641Sstever@eecs.umich.edu        state = dynamic_cast<DmaReqState*>(pkt->senderState);
1382685Ssaidi@eecs.umich.edu        pendingCount--;
1392685Ssaidi@eecs.umich.edu
1402685Ssaidi@eecs.umich.edu        assert(pendingCount >= 0);
1412657Ssaidi@eecs.umich.edu        assert(state);
1422685Ssaidi@eecs.umich.edu
1438134SAli.Saidi@ARM.com        // We shouldn't ever get a block in ownership state
1448134SAli.Saidi@ARM.com        assert(!(pkt->memInhibitAsserted() && !pkt->sharedAsserted()));
1458134SAli.Saidi@ARM.com
1462685Ssaidi@eecs.umich.edu        state->numBytes += pkt->req->getSize();
1474435Ssaidi@eecs.umich.edu        assert(state->totBytes >= state->numBytes);
1482685Ssaidi@eecs.umich.edu        if (state->totBytes == state->numBytes) {
1497403SAli.Saidi@ARM.com            if (state->completionEvent) {
1507403SAli.Saidi@ARM.com                if (state->delay)
1517823Ssteve.reinhardt@amd.com                    schedule(state->completionEvent, curTick() + state->delay);
1527403SAli.Saidi@ARM.com                else
1537403SAli.Saidi@ARM.com                    state->completionEvent->process();
1547403SAli.Saidi@ARM.com            }
1552685Ssaidi@eecs.umich.edu            delete state;
1562685Ssaidi@eecs.umich.edu        }
1572630SN/A        delete pkt->req;
1582630SN/A        delete pkt;
1592901Ssaidi@eecs.umich.edu
1602901Ssaidi@eecs.umich.edu        if (pendingCount == 0 && drainEvent) {
1612901Ssaidi@eecs.umich.edu            drainEvent->process();
1622901Ssaidi@eecs.umich.edu            drainEvent = NULL;
1632901Ssaidi@eecs.umich.edu        }
1642569SN/A    }  else {
1652685Ssaidi@eecs.umich.edu        panic("Got packet without sender state... huh?\n");
1662565SN/A    }
1672569SN/A
1682657Ssaidi@eecs.umich.edu    return true;
1692384SN/A}
170679SN/A
1714762Snate@binkert.orgDmaDevice::DmaDevice(const Params *p)
1727403SAli.Saidi@ARM.com    : PioDevice(p), dmaPort(NULL)
1732565SN/A{ }
1742384SN/A
1752901Ssaidi@eecs.umich.edu
1762901Ssaidi@eecs.umich.eduunsigned int
1772901Ssaidi@eecs.umich.eduDmaDevice::drain(Event *de)
1782901Ssaidi@eecs.umich.edu{
1792901Ssaidi@eecs.umich.edu    unsigned int count;
1802901Ssaidi@eecs.umich.edu    count = pioPort->drain(de) + dmaPort->drain(de);
1812901Ssaidi@eecs.umich.edu    if (count)
1822901Ssaidi@eecs.umich.edu        changeState(Draining);
1832901Ssaidi@eecs.umich.edu    else
1842901Ssaidi@eecs.umich.edu        changeState(Drained);
1852901Ssaidi@eecs.umich.edu    return count;
1862901Ssaidi@eecs.umich.edu}
1872901Ssaidi@eecs.umich.edu
1882901Ssaidi@eecs.umich.eduunsigned int
1892901Ssaidi@eecs.umich.eduDmaPort::drain(Event *de)
1902901Ssaidi@eecs.umich.edu{
1912901Ssaidi@eecs.umich.edu    if (pendingCount == 0)
1922901Ssaidi@eecs.umich.edu        return 0;
1932901Ssaidi@eecs.umich.edu    drainEvent = de;
1942901Ssaidi@eecs.umich.edu    return 1;
1952901Ssaidi@eecs.umich.edu}
1962901Ssaidi@eecs.umich.edu
1972901Ssaidi@eecs.umich.edu
1982384SN/Avoid
1992489SN/ADmaPort::recvRetry()
2002489SN/A{
2014435Ssaidi@eecs.umich.edu    assert(transmitList.size());
2022659Ssaidi@eecs.umich.edu    bool result = true;
2034435Ssaidi@eecs.umich.edu    do {
2045539Scws3k@cs.virginia.edu        PacketPtr pkt = transmitList.front();
2054739Sstever@eecs.umich.edu        DPRINTF(DMA, "Retry on %s addr %#x\n",
2064739Sstever@eecs.umich.edu                pkt->cmdString(), pkt->getAddr());
2072659Ssaidi@eecs.umich.edu        result = sendTiming(pkt);
2082659Ssaidi@eecs.umich.edu        if (result) {
2092659Ssaidi@eecs.umich.edu            DPRINTF(DMA, "-- Done\n");
2102659Ssaidi@eecs.umich.edu            transmitList.pop_front();
2114435Ssaidi@eecs.umich.edu            inRetry = false;
2122659Ssaidi@eecs.umich.edu        } else {
2134435Ssaidi@eecs.umich.edu            inRetry = true;
2142659Ssaidi@eecs.umich.edu            DPRINTF(DMA, "-- Failed, queued\n");
2152659Ssaidi@eecs.umich.edu        }
2164435Ssaidi@eecs.umich.edu    } while (!backoffTime &&  result && transmitList.size());
2174435Ssaidi@eecs.umich.edu
2184435Ssaidi@eecs.umich.edu    if (transmitList.size() && backoffTime && !inRetry) {
2197823Ssteve.reinhardt@amd.com        DPRINTF(DMA, "Scheduling backoff for %d\n", curTick()+backoffTime);
2204435Ssaidi@eecs.umich.edu        if (!backoffEvent.scheduled())
2217823Ssteve.reinhardt@amd.com            schedule(backoffEvent, backoffTime + curTick());
2222657Ssaidi@eecs.umich.edu    }
2234435Ssaidi@eecs.umich.edu    DPRINTF(DMA, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
2244435Ssaidi@eecs.umich.edu            transmitList.size(), backoffTime, inRetry,
2254435Ssaidi@eecs.umich.edu            backoffEvent.scheduled());
2262489SN/A}
2272641Sstever@eecs.umich.edu
2282641Sstever@eecs.umich.edu
2292489SN/Avoid
2302641Sstever@eecs.umich.eduDmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
2317607SGene.Wu@arm.com                   uint8_t *data, Tick delay, Request::Flags flag)
2322384SN/A{
2332901Ssaidi@eecs.umich.edu    assert(device->getState() == SimObject::Running);
2342901Ssaidi@eecs.umich.edu
2355534Ssaidi@eecs.umich.edu    DmaReqState *reqState = new DmaReqState(event, this, size, delay);
2362384SN/A
2374451Ssaidi@eecs.umich.edu
2384451Ssaidi@eecs.umich.edu    DPRINTF(DMA, "Starting DMA for addr: %#x size: %d sched: %d\n", addr, size,
2397607SGene.Wu@arm.com            event ? event->scheduled() : -1 );
2402406SN/A    for (ChunkGenerator gen(addr, size, peerBlockSize());
2412406SN/A         !gen.done(); gen.next()) {
2427607SGene.Wu@arm.com            Request *req = new Request(gen.addr(), gen.size(), flag);
2433349Sbinkertn@umich.edu            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
2442641Sstever@eecs.umich.edu
2452384SN/A            // Increment the data pointer on a write
2462566SN/A            if (data)
2472685Ssaidi@eecs.umich.edu                pkt->dataStatic(data + gen.complete());
2482641Sstever@eecs.umich.edu
2492685Ssaidi@eecs.umich.edu            pkt->senderState = reqState;
2502641Sstever@eecs.umich.edu
2512565SN/A            assert(pendingCount >= 0);
2522565SN/A            pendingCount++;
2534451Ssaidi@eecs.umich.edu            DPRINTF(DMA, "--Queuing DMA for addr: %#x size: %d\n", gen.addr(),
2544451Ssaidi@eecs.umich.edu                    gen.size());
2554435Ssaidi@eecs.umich.edu            queueDma(pkt);
2562384SN/A    }
2572901Ssaidi@eecs.umich.edu
2582384SN/A}
2592384SN/A
2604435Ssaidi@eecs.umich.eduvoid
2614435Ssaidi@eecs.umich.eduDmaPort::queueDma(PacketPtr pkt, bool front)
2624435Ssaidi@eecs.umich.edu{
2634435Ssaidi@eecs.umich.edu
2644435Ssaidi@eecs.umich.edu    if (front)
2654435Ssaidi@eecs.umich.edu        transmitList.push_front(pkt);
2664435Ssaidi@eecs.umich.edu    else
2674435Ssaidi@eecs.umich.edu        transmitList.push_back(pkt);
2684435Ssaidi@eecs.umich.edu    sendDma();
2694435Ssaidi@eecs.umich.edu}
2704435Ssaidi@eecs.umich.edu
2712384SN/A
2722384SN/Avoid
2734435Ssaidi@eecs.umich.eduDmaPort::sendDma()
2742384SN/A{
2752901Ssaidi@eecs.umich.edu    // some kind of selction between access methods
2762901Ssaidi@eecs.umich.edu    // more work is going to have to be done to make
2772901Ssaidi@eecs.umich.edu    // switching actually work
2784435Ssaidi@eecs.umich.edu    assert(transmitList.size());
2794435Ssaidi@eecs.umich.edu    PacketPtr pkt = transmitList.front();
2802902Ssaidi@eecs.umich.edu
2814762Snate@binkert.org    Enums::MemoryMode state = sys->getMemoryMode();
2824762Snate@binkert.org    if (state == Enums::timing) {
2834435Ssaidi@eecs.umich.edu        if (backoffEvent.scheduled() || inRetry) {
2844435Ssaidi@eecs.umich.edu            DPRINTF(DMA, "Can't send immediately, waiting for retry or backoff timer\n");
2854435Ssaidi@eecs.umich.edu            return;
2864435Ssaidi@eecs.umich.edu        }
2874435Ssaidi@eecs.umich.edu
2884739Sstever@eecs.umich.edu        DPRINTF(DMA, "Attempting to send %s addr %#x\n",
2894739Sstever@eecs.umich.edu                pkt->cmdString(), pkt->getAddr());
2904435Ssaidi@eecs.umich.edu
2914435Ssaidi@eecs.umich.edu        bool result;
2924435Ssaidi@eecs.umich.edu        do {
2934435Ssaidi@eecs.umich.edu            result = sendTiming(pkt);
2944435Ssaidi@eecs.umich.edu            if (result) {
2954435Ssaidi@eecs.umich.edu                transmitList.pop_front();
2964435Ssaidi@eecs.umich.edu                DPRINTF(DMA, "-- Done\n");
2974435Ssaidi@eecs.umich.edu            } else {
2984435Ssaidi@eecs.umich.edu                inRetry = true;
2994435Ssaidi@eecs.umich.edu                DPRINTF(DMA, "-- Failed: queued\n");
3004435Ssaidi@eecs.umich.edu            }
3014435Ssaidi@eecs.umich.edu        } while (result && !backoffTime && transmitList.size());
3024435Ssaidi@eecs.umich.edu
3034435Ssaidi@eecs.umich.edu        if (transmitList.size() && backoffTime && !inRetry &&
3044435Ssaidi@eecs.umich.edu                !backoffEvent.scheduled()) {
3054451Ssaidi@eecs.umich.edu            DPRINTF(DMA, "-- Scheduling backoff timer for %d\n",
3067823Ssteve.reinhardt@amd.com                    backoffTime+curTick());
3077823Ssteve.reinhardt@amd.com            schedule(backoffEvent, backoffTime + curTick());
3082901Ssaidi@eecs.umich.edu        }
3094762Snate@binkert.org    } else if (state == Enums::atomic) {
3104435Ssaidi@eecs.umich.edu        transmitList.pop_front();
3114435Ssaidi@eecs.umich.edu
3122902Ssaidi@eecs.umich.edu        Tick lat;
3134451Ssaidi@eecs.umich.edu        DPRINTF(DMA, "--Sending  DMA for addr: %#x size: %d\n",
3144451Ssaidi@eecs.umich.edu                pkt->req->getPaddr(), pkt->req->getSize());
3152902Ssaidi@eecs.umich.edu        lat = sendAtomic(pkt);
3162901Ssaidi@eecs.umich.edu        assert(pkt->senderState);
3172901Ssaidi@eecs.umich.edu        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
3182901Ssaidi@eecs.umich.edu        assert(state);
3194451Ssaidi@eecs.umich.edu        state->numBytes += pkt->req->getSize();
3202384SN/A
3214451Ssaidi@eecs.umich.edu        DPRINTF(DMA, "--Received response for  DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
3224451Ssaidi@eecs.umich.edu                pkt->req->getPaddr(), pkt->req->getSize(), state->numBytes,
3237403SAli.Saidi@ARM.com                state->totBytes,
3247403SAli.Saidi@ARM.com                state->completionEvent ? state->completionEvent->scheduled() : 0 );
3254451Ssaidi@eecs.umich.edu
3262901Ssaidi@eecs.umich.edu        if (state->totBytes == state->numBytes) {
3277403SAli.Saidi@ARM.com            if (state->completionEvent) {
3287403SAli.Saidi@ARM.com                assert(!state->completionEvent->scheduled());
3297823Ssteve.reinhardt@amd.com                schedule(state->completionEvent, curTick() + lat + state->delay);
3307403SAli.Saidi@ARM.com            }
3312901Ssaidi@eecs.umich.edu            delete state;
3322901Ssaidi@eecs.umich.edu            delete pkt->req;
3332901Ssaidi@eecs.umich.edu        }
3342901Ssaidi@eecs.umich.edu        pendingCount--;
3352901Ssaidi@eecs.umich.edu        assert(pendingCount >= 0);
3362901Ssaidi@eecs.umich.edu        delete pkt;
3372566SN/A
3382901Ssaidi@eecs.umich.edu        if (pendingCount == 0 && drainEvent) {
3392901Ssaidi@eecs.umich.edu            drainEvent->process();
3402901Ssaidi@eecs.umich.edu            drainEvent = NULL;
3412901Ssaidi@eecs.umich.edu        }
3422901Ssaidi@eecs.umich.edu
3432384SN/A   } else
3442384SN/A       panic("Unknown memory command state.");
3452384SN/A}
346545SN/A
347545SN/ADmaDevice::~DmaDevice()
348545SN/A{
3492489SN/A    if (dmaPort)
3502489SN/A        delete dmaPort;
351545SN/A}
352