io_device.cc revision 3349:fec4a86fa212
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/builder.hh"
36#include "sim/system.hh"
37
38
39PioPort::PioPort(PioDevice *dev, System *s, std::string pname)
40    : SimpleTimingPort(dev->name() + pname), 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, AddrRangeList &snoop)
52{
53    snoop.clear();
54    device->addressRanges(resp);
55}
56
57
58PioDevice::~PioDevice()
59{
60    if (pioPort)
61        delete pioPort;
62}
63
64void
65PioDevice::init()
66{
67    if (!pioPort)
68        panic("Pio port not connected to anything!");
69    pioPort->sendStatusChange(Port::RangeChange);
70}
71
72
73unsigned int
74PioDevice::drain(Event *de)
75{
76    unsigned int count;
77    count = pioPort->drain(de);
78    if (count)
79        changeState(Draining);
80    else
81        changeState(Drained);
82    return count;
83}
84
85void
86BasicPioDevice::addressRanges(AddrRangeList &range_list)
87{
88    assert(pioSize != 0);
89    range_list.clear();
90    range_list.push_back(RangeSize(pioAddr, pioSize));
91}
92
93
94DmaPort::DmaPort(DmaDevice *dev, System *s)
95    : Port(dev->name() + "-dmaport"), device(dev), sys(s), pendingCount(0),
96      actionInProgress(0), drainEvent(NULL)
97{ }
98
99bool
100DmaPort::recvTiming(PacketPtr pkt)
101{
102
103
104    if (pkt->result == Packet::Nacked) {
105        DPRINTF(DMA, "Received nacked Pkt %#x with State: %#x Addr: %#x\n",
106               pkt, pkt->senderState, pkt->getAddr());
107        pkt->reinitNacked();
108        sendDma(pkt, true);
109    } else if (pkt->senderState) {
110        DmaReqState *state;
111        DPRINTF(DMA, "Received response Pkt %#x with State: %#x Addr: %#x\n",
112               pkt, pkt->senderState, pkt->getAddr());
113        state = dynamic_cast<DmaReqState*>(pkt->senderState);
114        pendingCount--;
115
116        assert(pendingCount >= 0);
117        assert(state);
118
119        state->numBytes += pkt->req->getSize();
120        if (state->totBytes == state->numBytes) {
121            state->completionEvent->process();
122            delete state;
123        }
124        delete pkt->req;
125        delete pkt;
126
127        if (pendingCount == 0 && drainEvent) {
128            drainEvent->process();
129            drainEvent = NULL;
130        }
131    }  else {
132        panic("Got packet without sender state... huh?\n");
133    }
134
135    return true;
136}
137
138DmaDevice::DmaDevice(Params *p)
139    : PioDevice(p), dmaPort(NULL)
140{ }
141
142
143unsigned int
144DmaDevice::drain(Event *de)
145{
146    unsigned int count;
147    count = pioPort->drain(de) + dmaPort->drain(de);
148    if (count)
149        changeState(Draining);
150    else
151        changeState(Drained);
152    return count;
153}
154
155unsigned int
156DmaPort::drain(Event *de)
157{
158    if (pendingCount == 0)
159        return 0;
160    drainEvent = de;
161    return 1;
162}
163
164
165void
166DmaPort::recvRetry()
167{
168    PacketPtr pkt = transmitList.front();
169    bool result = true;
170    while (result && transmitList.size()) {
171        DPRINTF(DMA, "Retry on  Packet %#x with senderState: %#x\n",
172                   pkt, pkt->senderState);
173        result = sendTiming(pkt);
174        if (result) {
175            DPRINTF(DMA, "-- Done\n");
176            transmitList.pop_front();
177        } else {
178            DPRINTF(DMA, "-- Failed, queued\n");
179        }
180    }
181}
182
183
184void
185DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
186                   uint8_t *data)
187{
188    assert(event);
189
190    assert(device->getState() == SimObject::Running);
191
192    DmaReqState *reqState = new DmaReqState(event, this, size);
193
194    for (ChunkGenerator gen(addr, size, peerBlockSize());
195         !gen.done(); gen.next()) {
196            Request *req = new Request(gen.addr(), gen.size(), 0);
197            PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast);
198
199            // Increment the data pointer on a write
200            if (data)
201                pkt->dataStatic(data + gen.complete());
202
203            pkt->senderState = reqState;
204
205            assert(pendingCount >= 0);
206            pendingCount++;
207            sendDma(pkt);
208    }
209
210}
211
212
213void
214DmaPort::sendDma(PacketPtr pkt, bool front)
215{
216    // some kind of selction between access methods
217    // more work is going to have to be done to make
218    // switching actually work
219
220    System::MemoryMode state = sys->getMemoryMode();
221    if (state == System::Timing) {
222        DPRINTF(DMA, "Attempting to send Packet %#x with addr: %#x\n",
223                pkt, pkt->getAddr());
224        if (transmitList.size() || !sendTiming(pkt)) {
225            if (front)
226                transmitList.push_front(pkt);
227            else
228                transmitList.push_back(pkt);
229            DPRINTF(DMA, "-- Failed: queued\n");
230        } else {
231            DPRINTF(DMA, "-- Done\n");
232        }
233    } else if (state == System::Atomic) {
234        Tick lat;
235        lat = sendAtomic(pkt);
236        assert(pkt->senderState);
237        DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState);
238        assert(state);
239
240        state->numBytes += pkt->req->getSize();
241        if (state->totBytes == state->numBytes) {
242            state->completionEvent->schedule(curTick + lat);
243            delete state;
244            delete pkt->req;
245        }
246        pendingCount--;
247        assert(pendingCount >= 0);
248        delete pkt;
249
250        if (pendingCount == 0 && drainEvent) {
251            drainEvent->process();
252            drainEvent = NULL;
253        }
254
255   } else
256       panic("Unknown memory command state.");
257}
258
259DmaDevice::~DmaDevice()
260{
261    if (dmaPort)
262        delete dmaPort;
263}
264
265
266