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