io_device.cc revision 7729
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/system.hh"
36
37
38PioPort::PioPort(PioDevice *dev, System *s, std::string pname)
39    : SimpleTimingPort(dev->name() + pname, dev), device(dev)
40{ }
41
42
43Tick
44PioPort::recvAtomic(PacketPtr pkt)
45{
46    return pkt->isRead() ? device->read(pkt) : device->write(pkt);
47}
48
49void
50PioPort::getDeviceAddressRanges(AddrRangeList &resp, bool &snoop)
51{
52    snoop = false;
53    device->addressRanges(resp);
54    for (AddrRangeIter i = resp.begin(); i != resp.end(); i++)
55         DPRINTF(BusAddrRanges, "Adding Range %#x-%#x\n", i->start, i->end);
56}
57
58
59PioDevice::PioDevice(const Params *p)
60    : MemObject(p), platform(p->platform), sys(p->system), pioPort(NULL)
61{}
62
63PioDevice::~PioDevice()
64{
65    if (pioPort)
66        delete pioPort;
67}
68
69void
70PioDevice::init()
71{
72    if (!pioPort)
73        panic("Pio port not connected to anything!");
74    pioPort->sendStatusChange(Port::RangeChange);
75}
76
77
78unsigned int
79PioDevice::drain(Event *de)
80{
81    unsigned int count;
82    count = pioPort->drain(de);
83    if (count)
84        changeState(Draining);
85    else
86        changeState(Drained);
87    return count;
88}
89
90BasicPioDevice::BasicPioDevice(const Params *p)
91    : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
92      pioDelay(p->pio_latency)
93{}
94
95void
96BasicPioDevice::addressRanges(AddrRangeList &range_list)
97{
98    assert(pioSize != 0);
99    range_list.clear();
100    DPRINTF(BusAddrRanges, "registering range: %#x-%#x\n", pioAddr, pioSize);
101    range_list.push_back(RangeSize(pioAddr, pioSize));
102}
103
104
105DmaPort::DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff)
106    : Port(dev->name() + "-dmaport", dev), device(dev), sys(s),
107      pendingCount(0), actionInProgress(0), drainEvent(NULL),
108      backoffTime(0), minBackoffDelay(min_backoff),
109      maxBackoffDelay(max_backoff), inRetry(false), backoffEvent(this)
110{ }
111
112bool
113DmaPort::recvTiming(PacketPtr pkt)
114{
115    if (pkt->wasNacked()) {
116        DPRINTF(DMA, "Received nacked %s addr %#x\n",
117                pkt->cmdString(), pkt->getAddr());
118
119        if (backoffTime < minBackoffDelay)
120            backoffTime = minBackoffDelay;
121        else if (backoffTime < maxBackoffDelay)
122            backoffTime <<= 1;
123
124        reschedule(backoffEvent, curTick + backoffTime, true);
125
126        DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime);
127
128        pkt->reinitNacked();
129        queueDma(pkt, true);
130    } else if (pkt->senderState) {
131        DmaReqState *state;
132        backoffTime >>= 2;
133
134        DPRINTF(DMA, "Received response %s addr %#x size %#x\n",
135                pkt->cmdString(), pkt->getAddr(), pkt->req->getSize());
136        state = dynamic_cast<DmaReqState*>(pkt->senderState);
137        pendingCount--;
138
139        assert(pendingCount >= 0);
140        assert(state);
141
142        state->numBytes += pkt->req->getSize();
143        assert(state->totBytes >= state->numBytes);
144        if (state->totBytes == state->numBytes) {
145            if (state->completionEvent) {
146                if (state->delay)
147                    schedule(state->completionEvent, curTick + state->delay);
148                else
149                    state->completionEvent->process();
150            }
151            delete state;
152        }
153        delete pkt->req;
154        delete pkt;
155
156        if (pendingCount == 0 && drainEvent) {
157            drainEvent->process();
158            drainEvent = NULL;
159        }
160    }  else {
161        panic("Got packet without sender state... huh?\n");
162    }
163
164    return true;
165}
166
167DmaDevice::DmaDevice(const Params *p)
168    : PioDevice(p), dmaPort(NULL)
169{ }
170
171
172unsigned int
173DmaDevice::drain(Event *de)
174{
175    unsigned int count;
176    count = pioPort->drain(de) + dmaPort->drain(de);
177    if (count)
178        changeState(Draining);
179    else
180        changeState(Drained);
181    return count;
182}
183
184unsigned int
185DmaPort::drain(Event *de)
186{
187    if (pendingCount == 0)
188        return 0;
189    drainEvent = de;
190    return 1;
191}
192
193
194void
195DmaPort::recvRetry()
196{
197    assert(transmitList.size());
198    bool result = true;
199    do {
200        PacketPtr pkt = transmitList.front();
201        DPRINTF(DMA, "Retry on %s addr %#x\n",
202                pkt->cmdString(), pkt->getAddr());
203        result = sendTiming(pkt);
204        if (result) {
205            DPRINTF(DMA, "-- Done\n");
206            transmitList.pop_front();
207            inRetry = false;
208        } else {
209            inRetry = true;
210            DPRINTF(DMA, "-- Failed, queued\n");
211        }
212    } while (!backoffTime &&  result && transmitList.size());
213
214    if (transmitList.size() && backoffTime && !inRetry) {
215        DPRINTF(DMA, "Scheduling backoff for %d\n", curTick+backoffTime);
216        if (!backoffEvent.scheduled())
217            schedule(backoffEvent, backoffTime + curTick);
218    }
219    DPRINTF(DMA, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
220            transmitList.size(), backoffTime, inRetry,
221            backoffEvent.scheduled());
222}
223
224
225void
226DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
227                   uint8_t *data, Tick delay, Request::Flags flag)
228{
229    assert(device->getState() == SimObject::Running);
230
231    DmaReqState *reqState = new DmaReqState(event, this, size, delay);
232
233
234    DPRINTF(DMA, "Starting DMA for addr: %#x size: %d sched: %d\n", addr, size,
235            event ? event->scheduled() : -1 );
236    for (ChunkGenerator gen(addr, size, peerBlockSize());
237         !gen.done(); gen.next()) {
238            Request *req = new Request(gen.addr(), gen.size(), flag);
239            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
240
241            // Increment the data pointer on a write
242            if (data)
243                pkt->dataStatic(data + gen.complete());
244
245            pkt->senderState = reqState;
246
247            assert(pendingCount >= 0);
248            pendingCount++;
249            DPRINTF(DMA, "--Queuing DMA for addr: %#x size: %d\n", gen.addr(),
250                    gen.size());
251            queueDma(pkt);
252    }
253
254}
255
256void
257DmaPort::queueDma(PacketPtr pkt, bool front)
258{
259
260    if (front)
261        transmitList.push_front(pkt);
262    else
263        transmitList.push_back(pkt);
264    sendDma();
265}
266
267
268void
269DmaPort::sendDma()
270{
271    // some kind of selction between access methods
272    // more work is going to have to be done to make
273    // switching actually work
274    assert(transmitList.size());
275    PacketPtr pkt = transmitList.front();
276
277    Enums::MemoryMode state = sys->getMemoryMode();
278    if (state == Enums::timing) {
279        if (backoffEvent.scheduled() || inRetry) {
280            DPRINTF(DMA, "Can't send immediately, waiting for retry or backoff timer\n");
281            return;
282        }
283
284        DPRINTF(DMA, "Attempting to send %s addr %#x\n",
285                pkt->cmdString(), pkt->getAddr());
286
287        bool result;
288        do {
289            result = sendTiming(pkt);
290            if (result) {
291                transmitList.pop_front();
292                DPRINTF(DMA, "-- Done\n");
293            } else {
294                inRetry = true;
295                DPRINTF(DMA, "-- Failed: queued\n");
296            }
297        } while (result && !backoffTime && transmitList.size());
298
299        if (transmitList.size() && backoffTime && !inRetry &&
300                !backoffEvent.scheduled()) {
301            DPRINTF(DMA, "-- Scheduling backoff timer for %d\n",
302                    backoffTime+curTick);
303            schedule(backoffEvent, backoffTime + curTick);
304        }
305    } else if (state == Enums::atomic) {
306        transmitList.pop_front();
307
308        Tick lat;
309        DPRINTF(DMA, "--Sending  DMA for addr: %#x size: %d\n",
310                pkt->req->getPaddr(), pkt->req->getSize());
311        lat = sendAtomic(pkt);
312        assert(pkt->senderState);
313        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
314        assert(state);
315        state->numBytes += pkt->req->getSize();
316
317        DPRINTF(DMA, "--Received response for  DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
318                pkt->req->getPaddr(), pkt->req->getSize(), state->numBytes,
319                state->totBytes,
320                state->completionEvent ? state->completionEvent->scheduled() : 0 );
321
322        if (state->totBytes == state->numBytes) {
323            if (state->completionEvent) {
324                assert(!state->completionEvent->scheduled());
325                schedule(state->completionEvent, curTick + lat + state->delay);
326            }
327            delete state;
328            delete pkt->req;
329        }
330        pendingCount--;
331        assert(pendingCount >= 0);
332        delete pkt;
333
334        if (pendingCount == 0 && drainEvent) {
335            drainEvent->process();
336            drainEvent = NULL;
337        }
338
339   } else
340       panic("Unknown memory command state.");
341}
342
343DmaDevice::~DmaDevice()
344{
345    if (dmaPort)
346        delete dmaPort;
347}
348