dma_device.cc revision 8232
112839Sgabeblack@google.com/*
212839Sgabeblack@google.com * Copyright (c) 2006 The Regents of The University of Michigan
312839Sgabeblack@google.com * All rights reserved.
412839Sgabeblack@google.com *
512839Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612839Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712839Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812839Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912839Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012839Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112839Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212839Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312839Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412839Sgabeblack@google.com * this software without specific prior written permission.
1512839Sgabeblack@google.com *
1612839Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712839Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812839Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912839Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012839Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112839Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212839Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312839Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412839Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512839Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612839Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712839Sgabeblack@google.com *
2812839Sgabeblack@google.com * Authors: Ali Saidi
2912839Sgabeblack@google.com *          Nathan Binkert
3012839Sgabeblack@google.com */
3112839Sgabeblack@google.com
3212839Sgabeblack@google.com#include "base/chunk_generator.hh"
3312993Sgabeblack@google.com#include "base/trace.hh"
3412993Sgabeblack@google.com#include "debug/BusAddrRanges.hh"
3513196Sgabeblack@google.com#include "debug/DMA.hh"
3612839Sgabeblack@google.com#include "dev/io_device.hh"
3712839Sgabeblack@google.com#include "sim/system.hh"
3812839Sgabeblack@google.com
3912839Sgabeblack@google.comPioPort::PioPort(PioDevice *dev, System *s, std::string pname)
4012839Sgabeblack@google.com    : SimpleTimingPort(dev->name() + pname, dev), device(dev)
4112993Sgabeblack@google.com{ }
4212993Sgabeblack@google.com
4312993Sgabeblack@google.com
4412993Sgabeblack@google.comTick
4512993Sgabeblack@google.comPioPort::recvAtomic(PacketPtr pkt)
4612993Sgabeblack@google.com{
4712993Sgabeblack@google.com    return pkt->isRead() ? device->read(pkt) : device->write(pkt);
4812993Sgabeblack@google.com}
4912993Sgabeblack@google.com
5012993Sgabeblack@google.comvoid
5112993Sgabeblack@google.comPioPort::getDeviceAddressRanges(AddrRangeList &resp, bool &snoop)
5212993Sgabeblack@google.com{
5312993Sgabeblack@google.com    snoop = false;
5412993Sgabeblack@google.com    device->addressRanges(resp);
5512993Sgabeblack@google.com    for (AddrRangeIter i = resp.begin(); i != resp.end(); i++)
5612993Sgabeblack@google.com         DPRINTF(BusAddrRanges, "Adding Range %#x-%#x\n", i->start, i->end);
5712993Sgabeblack@google.com}
5812993Sgabeblack@google.com
5912993Sgabeblack@google.com
6012993Sgabeblack@google.comPioDevice::PioDevice(const Params *p)
6112993Sgabeblack@google.com    : MemObject(p), platform(p->platform), sys(p->system), pioPort(NULL)
6212993Sgabeblack@google.com{}
6312993Sgabeblack@google.com
6412993Sgabeblack@google.comPioDevice::~PioDevice()
6512993Sgabeblack@google.com{
6612993Sgabeblack@google.com    if (pioPort)
6712993Sgabeblack@google.com        delete pioPort;
6812993Sgabeblack@google.com}
6912993Sgabeblack@google.com
7012993Sgabeblack@google.comvoid
7112993Sgabeblack@google.comPioDevice::init()
7212993Sgabeblack@google.com{
7312993Sgabeblack@google.com    if (!pioPort)
7412993Sgabeblack@google.com        panic("Pio port not connected to anything!");
7512993Sgabeblack@google.com    pioPort->sendStatusChange(Port::RangeChange);
7612993Sgabeblack@google.com}
7712993Sgabeblack@google.com
7812993Sgabeblack@google.com
7912839Sgabeblack@google.comunsigned int
8012839Sgabeblack@google.comPioDevice::drain(Event *de)
8112839Sgabeblack@google.com{
8212839Sgabeblack@google.com    unsigned int count;
8312839Sgabeblack@google.com    count = pioPort->drain(de);
8412839Sgabeblack@google.com    if (count)
8512839Sgabeblack@google.com        changeState(Draining);
8612839Sgabeblack@google.com    else
8712839Sgabeblack@google.com        changeState(Drained);
8812839Sgabeblack@google.com    return count;
8912839Sgabeblack@google.com}
9012839Sgabeblack@google.com
9112839Sgabeblack@google.comBasicPioDevice::BasicPioDevice(const Params *p)
9212839Sgabeblack@google.com    : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
9312839Sgabeblack@google.com      pioDelay(p->pio_latency)
9412839Sgabeblack@google.com{}
9512839Sgabeblack@google.com
9612839Sgabeblack@google.comvoid
9712993Sgabeblack@google.comBasicPioDevice::addressRanges(AddrRangeList &range_list)
9812993Sgabeblack@google.com{
9912993Sgabeblack@google.com    assert(pioSize != 0);
10012993Sgabeblack@google.com    range_list.clear();
10112839Sgabeblack@google.com    DPRINTF(BusAddrRanges, "registering range: %#x-%#x\n", pioAddr, pioSize);
10212839Sgabeblack@google.com    range_list.push_back(RangeSize(pioAddr, pioSize));
10312839Sgabeblack@google.com}
10412839Sgabeblack@google.com
10512839Sgabeblack@google.com
10612839Sgabeblack@google.comDmaPort::DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff)
10712839Sgabeblack@google.com    : Port(dev->name() + "-dmaport", dev), device(dev), sys(s),
10812839Sgabeblack@google.com      pendingCount(0), actionInProgress(0), drainEvent(NULL),
10912839Sgabeblack@google.com      backoffTime(0), minBackoffDelay(min_backoff),
11012839Sgabeblack@google.com      maxBackoffDelay(max_backoff), inRetry(false), backoffEvent(this)
11112839Sgabeblack@google.com{ }
11212839Sgabeblack@google.com
11312839Sgabeblack@google.combool
11412839Sgabeblack@google.comDmaPort::recvTiming(PacketPtr pkt)
11512839Sgabeblack@google.com{
11612839Sgabeblack@google.com    if (pkt->wasNacked()) {
11712839Sgabeblack@google.com        DPRINTF(DMA, "Received nacked %s addr %#x\n",
11812839Sgabeblack@google.com                pkt->cmdString(), pkt->getAddr());
11912839Sgabeblack@google.com
12012839Sgabeblack@google.com        if (backoffTime < minBackoffDelay)
12112839Sgabeblack@google.com            backoffTime = minBackoffDelay;
12212839Sgabeblack@google.com        else if (backoffTime < maxBackoffDelay)
12312839Sgabeblack@google.com            backoffTime <<= 1;
12412993Sgabeblack@google.com
12512993Sgabeblack@google.com        reschedule(backoffEvent, curTick() + backoffTime, true);
12612993Sgabeblack@google.com
12712993Sgabeblack@google.com        DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime);
12812993Sgabeblack@google.com
12912993Sgabeblack@google.com        pkt->reinitNacked();
13012993Sgabeblack@google.com        queueDma(pkt, true);
13112993Sgabeblack@google.com    } else if (pkt->senderState) {
13212993Sgabeblack@google.com        DmaReqState *state;
13313260Sgabeblack@google.com        backoffTime >>= 2;
13413260Sgabeblack@google.com
13513260Sgabeblack@google.com        DPRINTF(DMA, "Received response %s addr %#x size %#x\n",
13613260Sgabeblack@google.com                pkt->cmdString(), pkt->getAddr(), pkt->req->getSize());
13713260Sgabeblack@google.com        state = dynamic_cast<DmaReqState*>(pkt->senderState);
13813260Sgabeblack@google.com        pendingCount--;
13913260Sgabeblack@google.com
14013260Sgabeblack@google.com        assert(pendingCount >= 0);
14113260Sgabeblack@google.com        assert(state);
14213260Sgabeblack@google.com
14313260Sgabeblack@google.com        // We shouldn't ever get a block in ownership state
14413260Sgabeblack@google.com        assert(!(pkt->memInhibitAsserted() && !pkt->sharedAsserted()));
14513260Sgabeblack@google.com
14613260Sgabeblack@google.com        state->numBytes += pkt->req->getSize();
14713260Sgabeblack@google.com        assert(state->totBytes >= state->numBytes);
14812839Sgabeblack@google.com        if (state->totBytes == state->numBytes) {
14912839Sgabeblack@google.com            if (state->completionEvent) {
15012839Sgabeblack@google.com                if (state->delay)
15112839Sgabeblack@google.com                    schedule(state->completionEvent, curTick() + state->delay);
15212839Sgabeblack@google.com                else
15312839Sgabeblack@google.com                    state->completionEvent->process();
15412839Sgabeblack@google.com            }
15512839Sgabeblack@google.com            delete state;
15612839Sgabeblack@google.com        }
15712839Sgabeblack@google.com        delete pkt->req;
15812839Sgabeblack@google.com        delete pkt;
15912839Sgabeblack@google.com
16012993Sgabeblack@google.com        if (pendingCount == 0 && drainEvent) {
16112993Sgabeblack@google.com            drainEvent->process();
16212993Sgabeblack@google.com            drainEvent = NULL;
16312839Sgabeblack@google.com        }
16412839Sgabeblack@google.com    }  else {
16512839Sgabeblack@google.com        panic("Got packet without sender state... huh?\n");
16612839Sgabeblack@google.com    }
16712839Sgabeblack@google.com
16812839Sgabeblack@google.com    return true;
16912839Sgabeblack@google.com}
17012993Sgabeblack@google.com
17112993Sgabeblack@google.comDmaDevice::DmaDevice(const Params *p)
17212993Sgabeblack@google.com    : PioDevice(p), dmaPort(NULL)
17312993Sgabeblack@google.com{ }
17412839Sgabeblack@google.com
17512839Sgabeblack@google.com
17612839Sgabeblack@google.comunsigned int
17712839Sgabeblack@google.comDmaDevice::drain(Event *de)
17812839Sgabeblack@google.com{
17912839Sgabeblack@google.com    unsigned int count;
18012879Sgabeblack@google.com    count = pioPort->drain(de) + dmaPort->drain(de);
18112879Sgabeblack@google.com    if (count)
18212879Sgabeblack@google.com        changeState(Draining);
18312879Sgabeblack@google.com    else
18412879Sgabeblack@google.com        changeState(Drained);
18513196Sgabeblack@google.com    return count;
18613196Sgabeblack@google.com}
18713196Sgabeblack@google.com
18813196Sgabeblack@google.comunsigned int
18913196Sgabeblack@google.comDmaPort::drain(Event *de)
19012879Sgabeblack@google.com{
19112879Sgabeblack@google.com    if (pendingCount == 0)
19212879Sgabeblack@google.com        return 0;
19313196Sgabeblack@google.com    drainEvent = de;
19413196Sgabeblack@google.com    return 1;
19513196Sgabeblack@google.com}
19613196Sgabeblack@google.com
19713196Sgabeblack@google.com
19813196Sgabeblack@google.comvoid
19913196Sgabeblack@google.comDmaPort::recvRetry()
20013196Sgabeblack@google.com{
20112839Sgabeblack@google.com    assert(transmitList.size());
20212839Sgabeblack@google.com    bool result = true;
20312839Sgabeblack@google.com    do {
20412839Sgabeblack@google.com        PacketPtr pkt = transmitList.front();
20512839Sgabeblack@google.com        DPRINTF(DMA, "Retry on %s addr %#x\n",
20612839Sgabeblack@google.com                pkt->cmdString(), pkt->getAddr());
20712839Sgabeblack@google.com        result = sendTiming(pkt);
20812839Sgabeblack@google.com        if (result) {
20912839Sgabeblack@google.com            DPRINTF(DMA, "-- Done\n");
21012839Sgabeblack@google.com            transmitList.pop_front();
21112839Sgabeblack@google.com            inRetry = false;
21212839Sgabeblack@google.com        } else {
21312839Sgabeblack@google.com            inRetry = true;
21412839Sgabeblack@google.com            DPRINTF(DMA, "-- Failed, queued\n");
21512839Sgabeblack@google.com        }
21612839Sgabeblack@google.com    } while (!backoffTime &&  result && transmitList.size());
21712839Sgabeblack@google.com
21812839Sgabeblack@google.com    if (transmitList.size() && backoffTime && !inRetry) {
21912839Sgabeblack@google.com        DPRINTF(DMA, "Scheduling backoff for %d\n", curTick()+backoffTime);
22012839Sgabeblack@google.com        if (!backoffEvent.scheduled())
221            schedule(backoffEvent, backoffTime + curTick());
222    }
223    DPRINTF(DMA, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
224            transmitList.size(), backoffTime, inRetry,
225            backoffEvent.scheduled());
226}
227
228
229void
230DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
231                   uint8_t *data, Tick delay, Request::Flags flag)
232{
233    assert(device->getState() == SimObject::Running);
234
235    DmaReqState *reqState = new DmaReqState(event, this, size, delay);
236
237
238    DPRINTF(DMA, "Starting DMA for addr: %#x size: %d sched: %d\n", addr, size,
239            event ? event->scheduled() : -1 );
240    for (ChunkGenerator gen(addr, size, peerBlockSize());
241         !gen.done(); gen.next()) {
242            Request *req = new Request(gen.addr(), gen.size(), flag);
243            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
244
245            // Increment the data pointer on a write
246            if (data)
247                pkt->dataStatic(data + gen.complete());
248
249            pkt->senderState = reqState;
250
251            assert(pendingCount >= 0);
252            pendingCount++;
253            DPRINTF(DMA, "--Queuing DMA for addr: %#x size: %d\n", gen.addr(),
254                    gen.size());
255            queueDma(pkt);
256    }
257
258}
259
260void
261DmaPort::queueDma(PacketPtr pkt, bool front)
262{
263
264    if (front)
265        transmitList.push_front(pkt);
266    else
267        transmitList.push_back(pkt);
268    sendDma();
269}
270
271
272void
273DmaPort::sendDma()
274{
275    // some kind of selction between access methods
276    // more work is going to have to be done to make
277    // switching actually work
278    assert(transmitList.size());
279    PacketPtr pkt = transmitList.front();
280
281    Enums::MemoryMode state = sys->getMemoryMode();
282    if (state == Enums::timing) {
283        if (backoffEvent.scheduled() || inRetry) {
284            DPRINTF(DMA, "Can't send immediately, waiting for retry or backoff timer\n");
285            return;
286        }
287
288        DPRINTF(DMA, "Attempting to send %s addr %#x\n",
289                pkt->cmdString(), pkt->getAddr());
290
291        bool result;
292        do {
293            result = sendTiming(pkt);
294            if (result) {
295                transmitList.pop_front();
296                DPRINTF(DMA, "-- Done\n");
297            } else {
298                inRetry = true;
299                DPRINTF(DMA, "-- Failed: queued\n");
300            }
301        } while (result && !backoffTime && transmitList.size());
302
303        if (transmitList.size() && backoffTime && !inRetry &&
304                !backoffEvent.scheduled()) {
305            DPRINTF(DMA, "-- Scheduling backoff timer for %d\n",
306                    backoffTime+curTick());
307            schedule(backoffEvent, backoffTime + curTick());
308        }
309    } else if (state == Enums::atomic) {
310        transmitList.pop_front();
311
312        Tick lat;
313        DPRINTF(DMA, "--Sending  DMA for addr: %#x size: %d\n",
314                pkt->req->getPaddr(), pkt->req->getSize());
315        lat = sendAtomic(pkt);
316        assert(pkt->senderState);
317        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
318        assert(state);
319        state->numBytes += pkt->req->getSize();
320
321        DPRINTF(DMA, "--Received response for  DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
322                pkt->req->getPaddr(), pkt->req->getSize(), state->numBytes,
323                state->totBytes,
324                state->completionEvent ? state->completionEvent->scheduled() : 0 );
325
326        if (state->totBytes == state->numBytes) {
327            if (state->completionEvent) {
328                assert(!state->completionEvent->scheduled());
329                schedule(state->completionEvent, curTick() + lat + state->delay);
330            }
331            delete state;
332            delete pkt->req;
333        }
334        pendingCount--;
335        assert(pendingCount >= 0);
336        delete pkt;
337
338        if (pendingCount == 0 && drainEvent) {
339            drainEvent->process();
340            drainEvent = NULL;
341        }
342
343   } else
344       panic("Unknown memory command state.");
345}
346
347DmaDevice::~DmaDevice()
348{
349    if (dmaPort)
350        delete dmaPort;
351}
352