io_device.cc revision 8630
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), platform(p->platform), 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
78Port *
79PioDevice::getPort(const std::string &if_name, int idx)
80{
81    if (if_name == "pio") {
82        if (pioPort != NULL)
83            fatal("%s: pio port already connected to %s",
84                  name(), pioPort->getPeer()->name());
85        pioPort = new PioPort(this, sys);
86        return pioPort;
87    }
88    return NULL;
89}
90
91unsigned int
92PioDevice::drain(Event *de)
93{
94    unsigned int count;
95    count = pioPort->drain(de);
96    if (count)
97        changeState(Draining);
98    else
99        changeState(Drained);
100    return count;
101}
102
103BasicPioDevice::BasicPioDevice(const Params *p)
104    : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
105      pioDelay(p->pio_latency)
106{}
107
108void
109BasicPioDevice::addressRanges(AddrRangeList &range_list)
110{
111    assert(pioSize != 0);
112    range_list.clear();
113    DPRINTF(BusAddrRanges, "registering range: %#x-%#x\n", pioAddr, pioSize);
114    range_list.push_back(RangeSize(pioAddr, pioSize));
115}
116
117
118DmaPort::DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff,
119                 bool recv_snoops)
120    : Port(dev->name() + "-dmaport", dev), device(dev), sys(s),
121      pendingCount(0), actionInProgress(0), drainEvent(NULL),
122      backoffTime(0), minBackoffDelay(min_backoff),
123      maxBackoffDelay(max_backoff), inRetry(false), recvSnoops(recv_snoops),
124      snoopRangeSent(false), backoffEvent(this)
125{ }
126
127bool
128DmaPort::recvTiming(PacketPtr pkt)
129{
130    if (pkt->wasNacked()) {
131        DPRINTF(DMA, "Received nacked %s addr %#x\n",
132                pkt->cmdString(), pkt->getAddr());
133
134        if (backoffTime < minBackoffDelay)
135            backoffTime = minBackoffDelay;
136        else if (backoffTime < maxBackoffDelay)
137            backoffTime <<= 1;
138
139        reschedule(backoffEvent, curTick() + backoffTime, true);
140
141        DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime);
142
143        pkt->reinitNacked();
144        queueDma(pkt, true);
145    } else if (pkt->senderState) {
146        if (recvSnoops) {
147            if (pkt->isRequest()) {
148                return true;
149            }
150        }
151
152        DmaReqState *state;
153        backoffTime >>= 2;
154
155        DPRINTF(DMA, "Received response %s addr %#x size %#x\n",
156                pkt->cmdString(), pkt->getAddr(), pkt->req->getSize());
157        state = dynamic_cast<DmaReqState*>(pkt->senderState);
158        pendingCount--;
159
160        assert(pendingCount >= 0);
161        assert(state);
162
163        // We shouldn't ever get a block in ownership state
164        assert(!(pkt->memInhibitAsserted() && !pkt->sharedAsserted()));
165
166        state->numBytes += pkt->req->getSize();
167        assert(state->totBytes >= state->numBytes);
168        if (state->totBytes == state->numBytes) {
169            if (state->completionEvent) {
170                if (state->delay)
171                    schedule(state->completionEvent, curTick() + state->delay);
172                else
173                    state->completionEvent->process();
174            }
175            delete state;
176        }
177        delete pkt->req;
178        delete pkt;
179
180        if (pendingCount == 0 && drainEvent) {
181            drainEvent->process();
182            drainEvent = NULL;
183        }
184    }  else {
185        panic("Got packet without sender state... huh?\n");
186    }
187
188    return true;
189}
190
191DmaDevice::DmaDevice(const Params *p)
192    : PioDevice(p), dmaPort(NULL)
193{ }
194
195
196unsigned int
197DmaDevice::drain(Event *de)
198{
199    unsigned int count;
200    count = pioPort->drain(de) + dmaPort->drain(de);
201    if (count)
202        changeState(Draining);
203    else
204        changeState(Drained);
205    return count;
206}
207
208unsigned int
209DmaPort::drain(Event *de)
210{
211    if (pendingCount == 0)
212        return 0;
213    drainEvent = de;
214    return 1;
215}
216
217
218void
219DmaPort::recvRetry()
220{
221    assert(transmitList.size());
222    bool result = true;
223    do {
224        PacketPtr pkt = transmitList.front();
225        DPRINTF(DMA, "Retry on %s addr %#x\n",
226                pkt->cmdString(), pkt->getAddr());
227        result = sendTiming(pkt);
228        if (result) {
229            DPRINTF(DMA, "-- Done\n");
230            transmitList.pop_front();
231            inRetry = false;
232        } else {
233            inRetry = true;
234            DPRINTF(DMA, "-- Failed, queued\n");
235        }
236    } while (!backoffTime &&  result && transmitList.size());
237
238    if (transmitList.size() && backoffTime && !inRetry) {
239        DPRINTF(DMA, "Scheduling backoff for %d\n", curTick()+backoffTime);
240        if (!backoffEvent.scheduled())
241            schedule(backoffEvent, backoffTime + curTick());
242    }
243    DPRINTF(DMA, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n",
244            transmitList.size(), backoffTime, inRetry,
245            backoffEvent.scheduled());
246}
247
248
249void
250DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
251                   uint8_t *data, Tick delay, Request::Flags flag)
252{
253    assert(device->getState() == SimObject::Running);
254
255    DmaReqState *reqState = new DmaReqState(event, this, size, delay);
256
257
258    DPRINTF(DMA, "Starting DMA for addr: %#x size: %d sched: %d\n", addr, size,
259            event ? event->scheduled() : -1 );
260    for (ChunkGenerator gen(addr, size, peerBlockSize());
261         !gen.done(); gen.next()) {
262            Request *req = new Request(gen.addr(), gen.size(), flag);
263            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
264
265            // Increment the data pointer on a write
266            if (data)
267                pkt->dataStatic(data + gen.complete());
268
269            pkt->senderState = reqState;
270
271            assert(pendingCount >= 0);
272            pendingCount++;
273            DPRINTF(DMA, "--Queuing DMA for addr: %#x size: %d\n", gen.addr(),
274                    gen.size());
275            queueDma(pkt);
276    }
277
278}
279
280void
281DmaPort::queueDma(PacketPtr pkt, bool front)
282{
283
284    if (front)
285        transmitList.push_front(pkt);
286    else
287        transmitList.push_back(pkt);
288    sendDma();
289}
290
291
292void
293DmaPort::sendDma()
294{
295    // some kind of selction between access methods
296    // more work is going to have to be done to make
297    // switching actually work
298    assert(transmitList.size());
299    PacketPtr pkt = transmitList.front();
300
301    Enums::MemoryMode state = sys->getMemoryMode();
302    if (state == Enums::timing) {
303        if (backoffEvent.scheduled() || inRetry) {
304            DPRINTF(DMA, "Can't send immediately, waiting for retry or backoff timer\n");
305            return;
306        }
307
308        DPRINTF(DMA, "Attempting to send %s addr %#x\n",
309                pkt->cmdString(), pkt->getAddr());
310
311        bool result;
312        do {
313            result = sendTiming(pkt);
314            if (result) {
315                transmitList.pop_front();
316                DPRINTF(DMA, "-- Done\n");
317            } else {
318                inRetry = true;
319                DPRINTF(DMA, "-- Failed: queued\n");
320            }
321        } while (result && !backoffTime && transmitList.size());
322
323        if (transmitList.size() && backoffTime && !inRetry &&
324                !backoffEvent.scheduled()) {
325            DPRINTF(DMA, "-- Scheduling backoff timer for %d\n",
326                    backoffTime+curTick());
327            schedule(backoffEvent, backoffTime + curTick());
328        }
329    } else if (state == Enums::atomic) {
330        transmitList.pop_front();
331
332        Tick lat;
333        DPRINTF(DMA, "--Sending  DMA for addr: %#x size: %d\n",
334                pkt->req->getPaddr(), pkt->req->getSize());
335        lat = sendAtomic(pkt);
336        assert(pkt->senderState);
337        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
338        assert(state);
339        state->numBytes += pkt->req->getSize();
340
341        DPRINTF(DMA, "--Received response for  DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n",
342                pkt->req->getPaddr(), pkt->req->getSize(), state->numBytes,
343                state->totBytes,
344                state->completionEvent ? state->completionEvent->scheduled() : 0 );
345
346        if (state->totBytes == state->numBytes) {
347            if (state->completionEvent) {
348                assert(!state->completionEvent->scheduled());
349                schedule(state->completionEvent, curTick() + lat + state->delay);
350            }
351            delete state;
352            delete pkt->req;
353        }
354        pendingCount--;
355        assert(pendingCount >= 0);
356        delete pkt;
357
358        if (pendingCount == 0 && drainEvent) {
359            drainEvent->process();
360            drainEvent = NULL;
361        }
362
363   } else
364       panic("Unknown memory command state.");
365}
366
367DmaDevice::~DmaDevice()
368{
369    if (dmaPort)
370        delete dmaPort;
371}
372
373
374Port *
375DmaDevice::getPort(const std::string &if_name, int idx)
376{
377    if (if_name == "dma") {
378        if (dmaPort != NULL)
379            fatal("%s: dma port already connected to %s",
380                  name(), dmaPort->getPeer()->name());
381        dmaPort = new DmaPort(this, sys, params()->min_backoff_delay,
382                              params()->max_backoff_delay);
383        return dmaPort;
384    }
385    return PioDevice::getPort(if_name, idx);
386}
387
388