dma_device.cc revision 4739
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 *          Nathan Binkert
30 */
31
32#include "base/chunk_generator.hh"
33#include "base/trace.hh"
34#include "dev/io_device.hh"
35#include "sim/builder.hh"
36#include "sim/system.hh"
37
38
39PioPort::PioPort(PioDevice *dev, System *s, std::string pname)
40    : SimpleTimingPort(dev->name() + pname, dev), device(dev)
41{ }
42
43
44Tick
45PioPort::recvAtomic(PacketPtr pkt)
46{
47    return pkt->isRead() ? device->read(pkt) : device->write(pkt);
48}
49
50void
51PioPort::getDeviceAddressRanges(AddrRangeList &resp, bool &snoop)
52{
53    snoop = false;
54    device->addressRanges(resp);
55}
56
57
58PioDevice::~PioDevice()
59{
60    if (pioPort)
61        delete pioPort;
62}
63
64void
65PioDevice::init()
66{
67    if (!pioPort)
68        panic("Pio port not connected to anything!");
69    pioPort->sendStatusChange(Port::RangeChange);
70}
71
72
73unsigned int
74PioDevice::drain(Event *de)
75{
76    unsigned int count;
77    count = pioPort->drain(de);
78    if (count)
79        changeState(Draining);
80    else
81        changeState(Drained);
82    return count;
83}
84
85void
86BasicPioDevice::addressRanges(AddrRangeList &range_list)
87{
88    assert(pioSize != 0);
89    range_list.clear();
90    range_list.push_back(RangeSize(pioAddr, pioSize));
91}
92
93
94DmaPort::DmaPort(DmaDevice *dev, System *s)
95    : Port(dev->name() + "-dmaport", dev), device(dev), sys(s),
96      pendingCount(0), actionInProgress(0), drainEvent(NULL),
97      backoffTime(0), inRetry(false), backoffEvent(this)
98{ }
99
100bool
101DmaPort::recvTiming(PacketPtr pkt)
102{
103
104
105    if (pkt->result == Packet::Nacked) {
106        DPRINTF(DMA, "Received nacked %s addr %#x\n",
107                pkt->cmdString(), pkt->getAddr());
108
109        if (backoffTime < device->minBackoffDelay)
110            backoffTime = device->minBackoffDelay;
111        else if (backoffTime < device->maxBackoffDelay)
112            backoffTime <<= 1;
113
114        backoffEvent.reschedule(curTick + backoffTime, true);
115
116        DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime);
117
118        pkt->reinitNacked();
119        queueDma(pkt, true);
120    } else if (pkt->senderState) {
121        DmaReqState *state;
122        backoffTime >>= 2;
123
124        DPRINTF(DMA, "Received response %s addr %#x size %#x\n",
125                pkt->cmdString(), pkt->getAddr(), pkt->req->getSize());
126        state = dynamic_cast<DmaReqState*>(pkt->senderState);
127        pendingCount--;
128
129        assert(pendingCount >= 0);
130        assert(state);
131
132        state->numBytes += pkt->req->getSize();
133        assert(state->totBytes >= state->numBytes);
134        if (state->totBytes == state->numBytes) {
135            state->completionEvent->process();
136            delete state;
137        }
138        delete pkt->req;
139        delete pkt;
140
141        if (pendingCount == 0 && drainEvent) {
142            drainEvent->process();
143            drainEvent = NULL;
144        }
145    }  else {
146        panic("Got packet without sender state... huh?\n");
147    }
148
149    return true;
150}
151
152DmaDevice::DmaDevice(Params *p)
153    : PioDevice(p), dmaPort(NULL), minBackoffDelay(p->min_backoff_delay),
154      maxBackoffDelay(p->max_backoff_delay)
155{ }
156
157
158unsigned int
159DmaDevice::drain(Event *de)
160{
161    unsigned int count;
162    count = pioPort->drain(de) + dmaPort->drain(de);
163    if (count)
164        changeState(Draining);
165    else
166        changeState(Drained);
167    return count;
168}
169
170unsigned int
171DmaPort::drain(Event *de)
172{
173    if (pendingCount == 0)
174        return 0;
175    drainEvent = de;
176    return 1;
177}
178
179
180void
181DmaPort::recvRetry()
182{
183    assert(transmitList.size());
184    PacketPtr pkt = transmitList.front();
185    bool result = true;
186    do {
187        DPRINTF(DMA, "Retry on %s addr %#x\n",
188                pkt->cmdString(), pkt->getAddr());
189        result = sendTiming(pkt);
190        if (result) {
191            DPRINTF(DMA, "-- Done\n");
192            transmitList.pop_front();
193            inRetry = false;
194        } else {
195            inRetry = true;
196            DPRINTF(DMA, "-- Failed, queued\n");
197        }
198    } while (!backoffTime &&  result && transmitList.size());
199
200    if (transmitList.size() && backoffTime && !inRetry) {
201        DPRINTF(DMA, "Scheduling backoff for %d\n", curTick+backoffTime);
202        if (!backoffEvent.scheduled())
203            backoffEvent.schedule(backoffTime+curTick);
204    }
205    DPRINTF(DMA, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
206            transmitList.size(), backoffTime, inRetry,
207            backoffEvent.scheduled());
208}
209
210
211void
212DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
213                   uint8_t *data)
214{
215    assert(event);
216
217    assert(device->getState() == SimObject::Running);
218
219    DmaReqState *reqState = new DmaReqState(event, this, size);
220
221
222    DPRINTF(DMA, "Starting DMA for addr: %#x size: %d sched: %d\n", addr, size,
223            event->scheduled());
224    for (ChunkGenerator gen(addr, size, peerBlockSize());
225         !gen.done(); gen.next()) {
226            Request *req = new Request(gen.addr(), gen.size(), 0);
227            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
228
229            // Increment the data pointer on a write
230            if (data)
231                pkt->dataStatic(data + gen.complete());
232
233            pkt->senderState = reqState;
234
235            assert(pendingCount >= 0);
236            pendingCount++;
237            DPRINTF(DMA, "--Queuing DMA for addr: %#x size: %d\n", gen.addr(),
238                    gen.size());
239            queueDma(pkt);
240    }
241
242}
243
244void
245DmaPort::queueDma(PacketPtr pkt, bool front)
246{
247
248    if (front)
249        transmitList.push_front(pkt);
250    else
251        transmitList.push_back(pkt);
252    sendDma();
253}
254
255
256void
257DmaPort::sendDma()
258{
259    // some kind of selction between access methods
260    // more work is going to have to be done to make
261    // switching actually work
262    assert(transmitList.size());
263    PacketPtr pkt = transmitList.front();
264
265    System::MemoryMode state = sys->getMemoryMode();
266    if (state == System::Timing) {
267        if (backoffEvent.scheduled() || inRetry) {
268            DPRINTF(DMA, "Can't send immediately, waiting for retry or backoff timer\n");
269            return;
270        }
271
272        DPRINTF(DMA, "Attempting to send %s addr %#x\n",
273                pkt->cmdString(), pkt->getAddr());
274
275        bool result;
276        do {
277            result = sendTiming(pkt);
278            if (result) {
279                transmitList.pop_front();
280                DPRINTF(DMA, "-- Done\n");
281            } else {
282                inRetry = true;
283                DPRINTF(DMA, "-- Failed: queued\n");
284            }
285        } while (result && !backoffTime && transmitList.size());
286
287        if (transmitList.size() && backoffTime && !inRetry &&
288                !backoffEvent.scheduled()) {
289            DPRINTF(DMA, "-- Scheduling backoff timer for %d\n",
290                    backoffTime+curTick);
291            backoffEvent.schedule(backoffTime+curTick);
292        }
293    } else if (state == System::Atomic) {
294        transmitList.pop_front();
295
296        Tick lat;
297        DPRINTF(DMA, "--Sending  DMA for addr: %#x size: %d\n",
298                pkt->req->getPaddr(), pkt->req->getSize());
299        lat = sendAtomic(pkt);
300        assert(pkt->senderState);
301        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
302        assert(state);
303        state->numBytes += pkt->req->getSize();
304
305        DPRINTF(DMA, "--Received response for  DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
306                pkt->req->getPaddr(), pkt->req->getSize(), state->numBytes,
307                state->totBytes, state->completionEvent->scheduled());
308
309        if (state->totBytes == state->numBytes) {
310            assert(!state->completionEvent->scheduled());
311            state->completionEvent->schedule(curTick + lat);
312            delete state;
313            delete pkt->req;
314        }
315        pendingCount--;
316        assert(pendingCount >= 0);
317        delete pkt;
318
319        if (pendingCount == 0 && drainEvent) {
320            drainEvent->process();
321            drainEvent = NULL;
322        }
323
324   } else
325       panic("Unknown memory command state.");
326}
327
328DmaDevice::~DmaDevice()
329{
330    if (dmaPort)
331        delete dmaPort;
332}
333
334
335