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