io_device.cc revision 8914
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)
40    : SimpleTimingPort(dev->name() + "-pioport", dev), device(dev)
41{
42}
43
44
45Tick
46PioPort::recvAtomic(PacketPtr pkt)
47{
48    return pkt->isRead() ? device->read(pkt) : device->write(pkt);
49}
50
51AddrRangeList
52PioPort::getAddrRanges()
53{
54    return device->getAddrRanges();
55}
56
57
58PioDevice::PioDevice(const Params *p)
59    : MemObject(p), sys(p->system), pioPort(this)
60{}
61
62PioDevice::~PioDevice()
63{
64}
65
66void
67PioDevice::init()
68{
69    if (!pioPort.isConnected())
70        panic("Pio port of %s not connected to anything!", name());
71    pioPort.sendRangeChange();
72}
73
74Port *
75PioDevice::getPort(const std::string &if_name, int idx)
76{
77    if (if_name == "pio") {
78        return &pioPort;
79    }
80    panic("PioDevice %s has no port named %s\n", name(), if_name);
81    return NULL;
82}
83
84unsigned int
85PioDevice::drain(Event *de)
86{
87    unsigned int count;
88    count = pioPort.drain(de);
89    if (count)
90        changeState(Draining);
91    else
92        changeState(Drained);
93    return count;
94}
95
96BasicPioDevice::BasicPioDevice(const Params *p)
97    : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
98      pioDelay(p->pio_latency)
99{}
100
101AddrRangeList
102BasicPioDevice::getAddrRanges()
103{
104    assert(pioSize != 0);
105    AddrRangeList ranges;
106    DPRINTF(BusAddrRanges, "registering range: %#x-%#x\n", pioAddr, pioSize);
107    ranges.push_back(RangeSize(pioAddr, pioSize));
108    return ranges;
109}
110
111
112DmaPort::DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff,
113                 bool recv_snoops)
114    : Port(dev->name() + "-dmaport", dev), device(dev), sys(s),
115      masterId(s->getMasterId(dev->name())),
116      pendingCount(0), actionInProgress(0), drainEvent(NULL),
117      backoffTime(0), minBackoffDelay(min_backoff),
118      maxBackoffDelay(max_backoff), inRetry(false), recvSnoops(recv_snoops),
119      backoffEvent(this)
120{ }
121
122bool
123DmaPort::recvTiming(PacketPtr pkt)
124{
125    if (pkt->wasNacked()) {
126        DPRINTF(DMA, "Received nacked %s addr %#x\n",
127                pkt->cmdString(), pkt->getAddr());
128
129        if (backoffTime < minBackoffDelay)
130            backoffTime = minBackoffDelay;
131        else if (backoffTime < maxBackoffDelay)
132            backoffTime <<= 1;
133
134        device->reschedule(backoffEvent, curTick() + backoffTime, true);
135
136        DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime);
137
138        pkt->reinitNacked();
139        queueDma(pkt, true);
140    } else if (pkt->isRequest() && recvSnoops) {
141        return true;
142    } else if (pkt->senderState) {
143        DmaReqState *state;
144        backoffTime >>= 2;
145
146        DPRINTF(DMA, "Received response %s addr %#x size %#x\n",
147                pkt->cmdString(), pkt->getAddr(), pkt->req->getSize());
148        state = dynamic_cast<DmaReqState*>(pkt->senderState);
149        pendingCount--;
150
151        assert(pendingCount >= 0);
152        assert(state);
153
154        // We shouldn't ever get a block in ownership state
155        assert(!(pkt->memInhibitAsserted() && !pkt->sharedAsserted()));
156
157        state->numBytes += pkt->req->getSize();
158        assert(state->totBytes >= state->numBytes);
159        if (state->totBytes == state->numBytes) {
160            if (state->completionEvent) {
161                if (state->delay)
162                    device->schedule(state->completionEvent,
163                                     curTick() + state->delay);
164                else
165                    state->completionEvent->process();
166            }
167            delete state;
168        }
169        delete pkt->req;
170        delete pkt;
171
172        if (pendingCount == 0 && drainEvent) {
173            drainEvent->process();
174            drainEvent = NULL;
175        }
176    }  else {
177        panic("Got packet without sender state... huh?\n");
178    }
179
180    return true;
181}
182
183DmaDevice::DmaDevice(const Params *p)
184    : PioDevice(p), dmaPort(this, sys, params()->min_backoff_delay,
185                            params()->max_backoff_delay)
186{ }
187
188void
189DmaDevice::init()
190{
191    if (!dmaPort.isConnected())
192        panic("DMA port of %s not connected to anything!", name());
193    PioDevice::init();
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            device->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, masterId);
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            device->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                device->schedule(state->completionEvent,
350                                 curTick() + lat + state->delay);
351            }
352            delete state;
353            delete pkt->req;
354        }
355        pendingCount--;
356        assert(pendingCount >= 0);
357        delete pkt;
358
359        if (pendingCount == 0 && drainEvent) {
360            drainEvent->process();
361            drainEvent = NULL;
362        }
363
364   } else
365       panic("Unknown memory command state.");
366}
367
368DmaDevice::~DmaDevice()
369{
370}
371
372
373Port *
374DmaDevice::getPort(const std::string &if_name, int idx)
375{
376    if (if_name == "dma") {
377        return &dmaPort;
378    }
379    return PioDevice::getPort(if_name, idx);
380}
381
382