io_device.cc revision 5534
12207SN/A/*
25254Sksewell@umich.edu * Copyright (c) 2006 The Regents of The University of Michigan
35254Sksewell@umich.edu * All rights reserved.
42207SN/A *
55254Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are
75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
145254Sksewell@umich.edu * this software without specific prior written permission.
152207SN/A *
165254Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
285254Sksewell@umich.edu * Authors: Ali Saidi
295254Sksewell@umich.edu *          Nathan Binkert
305254Sksewell@umich.edu */
312207SN/A
322207SN/A#include "base/chunk_generator.hh"
332474SN/A#include "base/trace.hh"
342207SN/A#include "dev/io_device.hh"
356650Sksewell@umich.edu#include "sim/system.hh"
362454SN/A
376811SMatt DeVuyst
382454SN/APioPort::PioPort(PioDevice *dev, System *s, std::string pname)
392680Sktlim@umich.edu    : SimpleTimingPort(dev->name() + pname, dev), device(dev)
406650Sksewell@umich.edu{ }
416650Sksewell@umich.edu
426650Sksewell@umich.edu
436650Sksewell@umich.eduTick
446650Sksewell@umich.eduPioPort::recvAtomic(PacketPtr pkt)
452474SN/A{
462207SN/A    return pkt->isRead() ? device->read(pkt) : device->write(pkt);
472447SN/A}
482474SN/A
492447SN/Avoid
505154Sgblack@eecs.umich.eduPioPort::getDeviceAddressRanges(AddrRangeList &resp, bool &snoop)
515154Sgblack@eecs.umich.edu{
525154Sgblack@eecs.umich.edu    snoop = false;
532474SN/A    device->addressRanges(resp);
542686Sksewell@umich.edu}
552686Sksewell@umich.edu
562935Sksewell@umich.edu
572474SN/APioDevice::PioDevice(const Params *p)
582474SN/A    : MemObject(p), platform(p->platform), sys(p->system), pioPort(NULL)
592474SN/A{}
602474SN/A
612686Sksewell@umich.eduPioDevice::~PioDevice()
622686Sksewell@umich.edu{
632686Sksewell@umich.edu    if (pioPort)
642686Sksewell@umich.edu        delete pioPort;
656811SMatt DeVuyst}
666811SMatt DeVuyst
672474SN/Avoid
682474SN/APioDevice::init()
692474SN/A{
707532Ssteve.reinhardt@amd.com    if (!pioPort)
712474SN/A        panic("Pio port not connected to anything!");
727532Ssteve.reinhardt@amd.com    pioPort->sendStatusChange(Port::RangeChange);
736650Sksewell@umich.edu}
746811SMatt DeVuyst
752474SN/A
765958Sgblack@eecs.umich.eduunsigned int
776811SMatt DeVuystPioDevice::drain(Event *de)
786650Sksewell@umich.edu{
796811SMatt DeVuyst    unsigned int count;
806650Sksewell@umich.edu    count = pioPort->drain(de);
816811SMatt DeVuyst    if (count)
826811SMatt DeVuyst        changeState(Draining);
836650Sksewell@umich.edu    else
846650Sksewell@umich.edu        changeState(Drained);
856650Sksewell@umich.edu    return count;
866811SMatt DeVuyst}
876811SMatt DeVuyst
886811SMatt DeVuystBasicPioDevice::BasicPioDevice(const Params *p)
896811SMatt DeVuyst    : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
906811SMatt DeVuyst      pioDelay(p->pio_latency)
916811SMatt DeVuyst{}
926811SMatt DeVuyst
936811SMatt DeVuystvoid
946811SMatt DeVuystBasicPioDevice::addressRanges(AddrRangeList &range_list)
956811SMatt DeVuyst{
966811SMatt DeVuyst    assert(pioSize != 0);
976811SMatt DeVuyst    range_list.clear();
986811SMatt DeVuyst    range_list.push_back(RangeSize(pioAddr, pioSize));
996811SMatt DeVuyst}
1006811SMatt DeVuyst
1016811SMatt DeVuyst
1026811SMatt DeVuystDmaPort::DmaPort(DmaDevice *dev, System *s)
1036811SMatt DeVuyst    : Port(dev->name() + "-dmaport", dev), device(dev), sys(s),
1046811SMatt DeVuyst      pendingCount(0), actionInProgress(0), drainEvent(NULL),
1056811SMatt DeVuyst      backoffTime(0), inRetry(false), backoffEvent(this)
1066811SMatt DeVuyst{ }
1076811SMatt DeVuyst
1086811SMatt DeVuystbool
1096811SMatt DeVuystDmaPort::recvTiming(PacketPtr pkt)
1106811SMatt DeVuyst{
1116811SMatt DeVuyst    if (pkt->wasNacked()) {
1126811SMatt DeVuyst        DPRINTF(DMA, "Received nacked %s addr %#x\n",
1136811SMatt DeVuyst                pkt->cmdString(), pkt->getAddr());
1146811SMatt DeVuyst
1156650Sksewell@umich.edu        if (backoffTime < device->minBackoffDelay)
1166650Sksewell@umich.edu            backoffTime = device->minBackoffDelay;
1176811SMatt DeVuyst        else if (backoffTime < device->maxBackoffDelay)
1186811SMatt DeVuyst            backoffTime <<= 1;
1196650Sksewell@umich.edu
1206650Sksewell@umich.edu        backoffEvent.reschedule(curTick + backoffTime, true);
1216650Sksewell@umich.edu
1226650Sksewell@umich.edu        DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime);
1236650Sksewell@umich.edu
1246650Sksewell@umich.edu        pkt->reinitNacked();
1256650Sksewell@umich.edu        queueDma(pkt, true);
1266650Sksewell@umich.edu    } else if (pkt->senderState) {
1276650Sksewell@umich.edu        DmaReqState *state;
1286650Sksewell@umich.edu        backoffTime >>= 2;
1296811SMatt DeVuyst
1306811SMatt DeVuyst        DPRINTF(DMA, "Received response %s addr %#x size %#x\n",
1316811SMatt DeVuyst                pkt->cmdString(), pkt->getAddr(), pkt->req->getSize());
1326811SMatt DeVuyst        state = dynamic_cast<DmaReqState*>(pkt->senderState);
1336811SMatt DeVuyst        pendingCount--;
1346650Sksewell@umich.edu
1356650Sksewell@umich.edu        assert(pendingCount >= 0);
1366650Sksewell@umich.edu        assert(state);
1376650Sksewell@umich.edu
1386650Sksewell@umich.edu        state->numBytes += pkt->req->getSize();
1396650Sksewell@umich.edu        assert(state->totBytes >= state->numBytes);
1406650Sksewell@umich.edu        if (state->totBytes == state->numBytes) {
1416650Sksewell@umich.edu            if (state->delay)
1426650Sksewell@umich.edu                state->completionEvent->schedule(state->delay + curTick);
1436650Sksewell@umich.edu            else
1446811SMatt DeVuyst                state->completionEvent->process();
1456811SMatt DeVuyst            delete state;
1466811SMatt DeVuyst        }
1476811SMatt DeVuyst        delete pkt->req;
1486811SMatt DeVuyst        delete pkt;
1496650Sksewell@umich.edu
1506650Sksewell@umich.edu        if (pendingCount == 0 && drainEvent) {
1516811SMatt DeVuyst            drainEvent->process();
1526650Sksewell@umich.edu            drainEvent = NULL;
1536811SMatt DeVuyst        }
1546650Sksewell@umich.edu    }  else {
1556650Sksewell@umich.edu        panic("Got packet without sender state... huh?\n");
1566650Sksewell@umich.edu    }
1576650Sksewell@umich.edu
1586650Sksewell@umich.edu    return true;
1596650Sksewell@umich.edu}
1606650Sksewell@umich.edu
1616811SMatt DeVuystDmaDevice::DmaDevice(const Params *p)
1626811SMatt DeVuyst    : PioDevice(p), dmaPort(NULL), minBackoffDelay(p->min_backoff_delay),
1636811SMatt DeVuyst      maxBackoffDelay(p->max_backoff_delay)
1646811SMatt DeVuyst{ }
1656811SMatt DeVuyst
1666811SMatt DeVuyst
1676811SMatt DeVuystunsigned int
1686811SMatt DeVuystDmaDevice::drain(Event *de)
1696811SMatt DeVuyst{
1706811SMatt DeVuyst    unsigned int count;
1716811SMatt DeVuyst    count = pioPort->drain(de) + dmaPort->drain(de);
1726811SMatt DeVuyst    if (count)
1736811SMatt DeVuyst        changeState(Draining);
1746811SMatt DeVuyst    else
1756811SMatt DeVuyst        changeState(Drained);
1766650Sksewell@umich.edu    return count;
1776650Sksewell@umich.edu}
1786650Sksewell@umich.edu
1796650Sksewell@umich.eduunsigned int
1806650Sksewell@umich.eduDmaPort::drain(Event *de)
1816650Sksewell@umich.edu{
1826650Sksewell@umich.edu    if (pendingCount == 0)
1836650Sksewell@umich.edu        return 0;
1846650Sksewell@umich.edu    drainEvent = de;
1856650Sksewell@umich.edu    return 1;
1866650Sksewell@umich.edu}
1876650Sksewell@umich.edu
1886650Sksewell@umich.edu
1895958Sgblack@eecs.umich.eduvoid
1906701Sgblack@eecs.umich.eduDmaPort::recvRetry()
1915958Sgblack@eecs.umich.edu{
1925958Sgblack@eecs.umich.edu    assert(transmitList.size());
1936701Sgblack@eecs.umich.edu    PacketPtr pkt = transmitList.front();
1945958Sgblack@eecs.umich.edu    bool result = true;
1955958Sgblack@eecs.umich.edu    do {
1965958Sgblack@eecs.umich.edu        DPRINTF(DMA, "Retry on %s addr %#x\n",
1975958Sgblack@eecs.umich.edu                pkt->cmdString(), pkt->getAddr());
1985958Sgblack@eecs.umich.edu        result = sendTiming(pkt);
1995958Sgblack@eecs.umich.edu        if (result) {
2005958Sgblack@eecs.umich.edu            DPRINTF(DMA, "-- Done\n");
2015958Sgblack@eecs.umich.edu            transmitList.pop_front();
2025958Sgblack@eecs.umich.edu            inRetry = false;
2035958Sgblack@eecs.umich.edu        } else {
2045958Sgblack@eecs.umich.edu            inRetry = true;
2055958Sgblack@eecs.umich.edu            DPRINTF(DMA, "-- Failed, queued\n");
2065958Sgblack@eecs.umich.edu        }
2075958Sgblack@eecs.umich.edu    } while (!backoffTime &&  result && transmitList.size());
2085958Sgblack@eecs.umich.edu
2095958Sgblack@eecs.umich.edu    if (transmitList.size() && backoffTime && !inRetry) {
2105958Sgblack@eecs.umich.edu        DPRINTF(DMA, "Scheduling backoff for %d\n", curTick+backoffTime);
2115958Sgblack@eecs.umich.edu        if (!backoffEvent.scheduled())
2125958Sgblack@eecs.umich.edu            backoffEvent.schedule(backoffTime+curTick);
2135958Sgblack@eecs.umich.edu    }
2145958Sgblack@eecs.umich.edu    DPRINTF(DMA, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
2155958Sgblack@eecs.umich.edu            transmitList.size(), backoffTime, inRetry,
2165958Sgblack@eecs.umich.edu            backoffEvent.scheduled());
2175958Sgblack@eecs.umich.edu}
218
219
220void
221DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
222                   uint8_t *data, Tick delay)
223{
224    assert(event);
225
226    assert(device->getState() == SimObject::Running);
227
228    DmaReqState *reqState = new DmaReqState(event, this, size, delay);
229
230
231    DPRINTF(DMA, "Starting DMA for addr: %#x size: %d sched: %d\n", addr, size,
232            event->scheduled());
233    for (ChunkGenerator gen(addr, size, peerBlockSize());
234         !gen.done(); gen.next()) {
235            Request *req = new Request(gen.addr(), gen.size(), 0);
236            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
237
238            // Increment the data pointer on a write
239            if (data)
240                pkt->dataStatic(data + gen.complete());
241
242            pkt->senderState = reqState;
243
244            assert(pendingCount >= 0);
245            pendingCount++;
246            DPRINTF(DMA, "--Queuing DMA for addr: %#x size: %d\n", gen.addr(),
247                    gen.size());
248            queueDma(pkt);
249    }
250
251}
252
253void
254DmaPort::queueDma(PacketPtr pkt, bool front)
255{
256
257    if (front)
258        transmitList.push_front(pkt);
259    else
260        transmitList.push_back(pkt);
261    sendDma();
262}
263
264
265void
266DmaPort::sendDma()
267{
268    // some kind of selction between access methods
269    // more work is going to have to be done to make
270    // switching actually work
271    assert(transmitList.size());
272    PacketPtr pkt = transmitList.front();
273
274    Enums::MemoryMode state = sys->getMemoryMode();
275    if (state == Enums::timing) {
276        if (backoffEvent.scheduled() || inRetry) {
277            DPRINTF(DMA, "Can't send immediately, waiting for retry or backoff timer\n");
278            return;
279        }
280
281        DPRINTF(DMA, "Attempting to send %s addr %#x\n",
282                pkt->cmdString(), pkt->getAddr());
283
284        bool result;
285        do {
286            result = sendTiming(pkt);
287            if (result) {
288                transmitList.pop_front();
289                DPRINTF(DMA, "-- Done\n");
290            } else {
291                inRetry = true;
292                DPRINTF(DMA, "-- Failed: queued\n");
293            }
294        } while (result && !backoffTime && transmitList.size());
295
296        if (transmitList.size() && backoffTime && !inRetry &&
297                !backoffEvent.scheduled()) {
298            DPRINTF(DMA, "-- Scheduling backoff timer for %d\n",
299                    backoffTime+curTick);
300            backoffEvent.schedule(backoffTime+curTick);
301        }
302    } else if (state == Enums::atomic) {
303        transmitList.pop_front();
304
305        Tick lat;
306        DPRINTF(DMA, "--Sending  DMA for addr: %#x size: %d\n",
307                pkt->req->getPaddr(), pkt->req->getSize());
308        lat = sendAtomic(pkt);
309        assert(pkt->senderState);
310        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
311        assert(state);
312        state->numBytes += pkt->req->getSize();
313
314        DPRINTF(DMA, "--Received response for  DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
315                pkt->req->getPaddr(), pkt->req->getSize(), state->numBytes,
316                state->totBytes, state->completionEvent->scheduled());
317
318        if (state->totBytes == state->numBytes) {
319            assert(!state->completionEvent->scheduled());
320            state->completionEvent->schedule(curTick + lat + state->delay);
321            delete state;
322            delete pkt->req;
323        }
324        pendingCount--;
325        assert(pendingCount >= 0);
326        delete pkt;
327
328        if (pendingCount == 0 && drainEvent) {
329            drainEvent->process();
330            drainEvent = NULL;
331        }
332
333   } else
334       panic("Unknown memory command state.");
335}
336
337DmaDevice::~DmaDevice()
338{
339    if (dmaPort)
340        delete dmaPort;
341}
342