io_device.cc revision 8742
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 "debug/BusAddrRanges.hh"
35#include "debug/DMA.hh"
36#include "dev/io_device.hh"
37#include "sim/system.hh"
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    for (AddrRangeIter i = resp.begin(); i != resp.end(); i++)
56         DPRINTF(BusAddrRanges, "Adding Range %#x-%#x\n", i->start, i->end);
57}
58
59
60PioDevice::PioDevice(const Params *p)
61    : MemObject(p), sys(p->system), pioPort(NULL)
62{}
63
64PioDevice::~PioDevice()
65{
66    if (pioPort)
67        delete pioPort;
68}
69
70void
71PioDevice::init()
72{
73    if (!pioPort)
74        panic("Pio port not connected to anything!");
75    pioPort->sendStatusChange(Port::RangeChange);
76}
77
78
79unsigned int
80PioDevice::drain(Event *de)
81{
82    unsigned int count;
83    count = pioPort->drain(de);
84    if (count)
85        changeState(Draining);
86    else
87        changeState(Drained);
88    return count;
89}
90
91BasicPioDevice::BasicPioDevice(const Params *p)
92    : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
93      pioDelay(p->pio_latency)
94{}
95
96void
97BasicPioDevice::addressRanges(AddrRangeList &range_list)
98{
99    assert(pioSize != 0);
100    range_list.clear();
101    DPRINTF(BusAddrRanges, "registering range: %#x-%#x\n", pioAddr, pioSize);
102    range_list.push_back(RangeSize(pioAddr, pioSize));
103}
104
105
106DmaPort::DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff)
107    : Port(dev->name() + "-dmaport", dev), device(dev), sys(s),
108      pendingCount(0), actionInProgress(0), drainEvent(NULL),
109      backoffTime(0), minBackoffDelay(min_backoff),
110      maxBackoffDelay(max_backoff), inRetry(false), backoffEvent(this)
111{ }
112
113bool
114DmaPort::recvTiming(PacketPtr pkt)
115{
116    if (pkt->wasNacked()) {
117        DPRINTF(DMA, "Received nacked %s addr %#x\n",
118                pkt->cmdString(), pkt->getAddr());
119
120        if (backoffTime < minBackoffDelay)
121            backoffTime = minBackoffDelay;
122        else if (backoffTime < maxBackoffDelay)
123            backoffTime <<= 1;
124
125        reschedule(backoffEvent, curTick() + backoffTime, true);
126
127        DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime);
128
129        pkt->reinitNacked();
130        queueDma(pkt, true);
131    } else if (pkt->senderState) {
132        DmaReqState *state;
133        backoffTime >>= 2;
134
135        DPRINTF(DMA, "Received response %s addr %#x size %#x\n",
136                pkt->cmdString(), pkt->getAddr(), pkt->req->getSize());
137        state = dynamic_cast<DmaReqState*>(pkt->senderState);
138        pendingCount--;
139
140        assert(pendingCount >= 0);
141        assert(state);
142
143        // We shouldn't ever get a block in ownership state
144        assert(!(pkt->memInhibitAsserted() && !pkt->sharedAsserted()));
145
146        state->numBytes += pkt->req->getSize();
147        assert(state->totBytes >= state->numBytes);
148        if (state->totBytes == state->numBytes) {
149            if (state->completionEvent) {
150                if (state->delay)
151                    schedule(state->completionEvent, curTick() + state->delay);
152                else
153                    state->completionEvent->process();
154            }
155            delete state;
156        }
157        delete pkt->req;
158        delete pkt;
159
160        if (pendingCount == 0 && drainEvent) {
161            drainEvent->process();
162            drainEvent = NULL;
163        }
164    }  else {
165        panic("Got packet without sender state... huh?\n");
166    }
167
168    return true;
169}
170
171DmaDevice::DmaDevice(const Params *p)
172    : PioDevice(p), dmaPort(NULL)
173{ }
174
175
176unsigned int
177DmaDevice::drain(Event *de)
178{
179    unsigned int count;
180    count = pioPort->drain(de) + dmaPort->drain(de);
181    if (count)
182        changeState(Draining);
183    else
184        changeState(Drained);
185    return count;
186}
187
188unsigned int
189DmaPort::drain(Event *de)
190{
191    if (pendingCount == 0)
192        return 0;
193    drainEvent = de;
194    return 1;
195}
196
197
198void
199DmaPort::recvRetry()
200{
201    assert(transmitList.size());
202    bool result = true;
203    do {
204        PacketPtr pkt = transmitList.front();
205        DPRINTF(DMA, "Retry on %s addr %#x\n",
206                pkt->cmdString(), pkt->getAddr());
207        result = sendTiming(pkt);
208        if (result) {
209            DPRINTF(DMA, "-- Done\n");
210            transmitList.pop_front();
211            inRetry = false;
212        } else {
213            inRetry = true;
214            DPRINTF(DMA, "-- Failed, queued\n");
215        }
216    } while (!backoffTime &&  result && transmitList.size());
217
218    if (transmitList.size() && backoffTime && !inRetry) {
219        DPRINTF(DMA, "Scheduling backoff for %d\n", curTick()+backoffTime);
220        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