io_device.cc revision 4437:b6e304245729
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, AddrRangeList &snoop)
52{
53    snoop.clear();
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 Pkt %#x with State: %#x Addr: %#x\n",
107               pkt, pkt->senderState, 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 Pkt %#x with State: %#x Addr: %#x size: %#x\n",
125               pkt, pkt->senderState, 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  Packet %#x with senderState: %#x\n",
188                   pkt, pkt->senderState);
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    for (ChunkGenerator gen(addr, size, peerBlockSize());
222         !gen.done(); gen.next()) {
223            Request *req = new Request(gen.addr(), gen.size(), 0);
224            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
225
226            // Increment the data pointer on a write
227            if (data)
228                pkt->dataStatic(data + gen.complete());
229
230            pkt->senderState = reqState;
231
232            assert(pendingCount >= 0);
233            pendingCount++;
234            queueDma(pkt);
235    }
236
237}
238
239void
240DmaPort::queueDma(PacketPtr pkt, bool front)
241{
242
243    if (front)
244        transmitList.push_front(pkt);
245    else
246        transmitList.push_back(pkt);
247    sendDma();
248}
249
250
251void
252DmaPort::sendDma()
253{
254    // some kind of selction between access methods
255    // more work is going to have to be done to make
256    // switching actually work
257    assert(transmitList.size());
258    PacketPtr pkt = transmitList.front();
259
260    System::MemoryMode state = sys->getMemoryMode();
261    if (state == System::Timing) {
262        if (backoffEvent.scheduled() || inRetry) {
263            DPRINTF(DMA, "Can't send immediately, waiting for retry or backoff timer\n");
264            return;
265        }
266
267        DPRINTF(DMA, "Attempting to send Packet %#x with addr: %#x\n",
268                pkt, pkt->getAddr());
269
270        bool result;
271        do {
272            result = sendTiming(pkt);
273            if (result) {
274                transmitList.pop_front();
275                DPRINTF(DMA, "-- Done\n");
276            } else {
277                inRetry = true;
278                DPRINTF(DMA, "-- Failed: queued\n");
279            }
280        } while (result && !backoffTime && transmitList.size());
281
282        if (transmitList.size() && backoffTime && !inRetry &&
283                !backoffEvent.scheduled()) {
284            backoffEvent.schedule(backoffTime+curTick);
285        }
286    } else if (state == System::Atomic) {
287        transmitList.pop_front();
288
289        Tick lat;
290        lat = sendAtomic(pkt);
291        assert(pkt->senderState);
292        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
293        assert(state);
294
295        state->numBytes += pkt->req->getSize();
296        if (state->totBytes == state->numBytes) {
297            state->completionEvent->schedule(curTick + lat);
298            delete state;
299            delete pkt->req;
300        }
301        pendingCount--;
302        assert(pendingCount >= 0);
303        delete pkt;
304
305        if (pendingCount == 0 && drainEvent) {
306            drainEvent->process();
307            drainEvent = NULL;
308        }
309
310   } else
311       panic("Unknown memory command state.");
312}
313
314DmaDevice::~DmaDevice()
315{
316    if (dmaPort)
317        delete dmaPort;
318}
319
320
321