tport.cc revision 4870
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 */
30
31#include "mem/tport.hh"
32
33void
34SimpleTimingPort::checkFunctional(PacketPtr pkt)
35{
36    DeferredPacketIterator i = transmitList.begin();
37    DeferredPacketIterator end = transmitList.end();
38
39    for (; i != end; ++i) {
40        PacketPtr target = i->pkt;
41        // If the target contains data, and it overlaps the
42        // probed request, need to update data
43        if (target->intersect(pkt)) {
44            if (!fixPacket(pkt, target)) {
45                // fixPacket returns true for continue, false for done
46                return;
47            }
48        }
49    }
50}
51
52void
53SimpleTimingPort::recvFunctional(PacketPtr pkt)
54{
55    checkFunctional(pkt);
56
57    // Just do an atomic access and throw away the returned latency
58    if (!pkt->isResponse())
59        recvAtomic(pkt);
60}
61
62bool
63SimpleTimingPort::recvTiming(PacketPtr pkt)
64{
65    // If the device is only a slave, it should only be sending
66    // responses, which should never get nacked.  There used to be
67    // code to hanldle nacks here, but I'm pretty sure it didn't work
68    // correctly with the drain code, so that would need to be fixed
69    // if we ever added it back.
70    assert(pkt->isRequest());
71
72    if (pkt->memInhibitAsserted()) {
73        // snooper will supply based on copy of packet
74        // still target's responsibility to delete packet
75        delete pkt->req;
76        delete pkt;
77        return true;
78    }
79
80    bool needsResponse = pkt->needsResponse();
81    Tick latency = recvAtomic(pkt);
82    // turn packet around to go back to requester if response expected
83    if (needsResponse) {
84        // recvAtomic() should already have turned packet into
85        // atomic response
86        assert(pkt->isResponse());
87        schedSendTiming(pkt, curTick + latency);
88    } else {
89        delete pkt->req;
90        delete pkt;
91    }
92
93    return true;
94}
95
96
97void
98SimpleTimingPort::schedSendTiming(PacketPtr pkt, Tick when)
99{
100    assert(when > curTick);
101
102    // Nothing is on the list: add it and schedule an event
103    if (transmitList.empty() || when < transmitList.front().tick) {
104        transmitList.push_front(DeferredPacket(when, pkt));
105        schedSendEvent(when);
106        return;
107    }
108
109    // list is non-empty and this is not the head, so event should
110    // already be scheduled
111    assert(waitingOnRetry ||
112           (sendEvent->scheduled() && sendEvent->when() <= when));
113
114    // list is non-empty & this belongs at the end
115    if (when >= transmitList.back().tick) {
116        transmitList.push_back(DeferredPacket(when, pkt));
117        return;
118    }
119
120    // this belongs in the middle somewhere
121    DeferredPacketIterator i = transmitList.begin();
122    i++; // already checked for insertion at front
123    DeferredPacketIterator end = transmitList.end();
124
125    for (; i != end; ++i) {
126        if (when < i->tick) {
127            transmitList.insert(i, DeferredPacket(when, pkt));
128            return;
129        }
130    }
131    assert(false); // should never get here
132}
133
134
135void
136SimpleTimingPort::sendDeferredPacket()
137{
138    assert(deferredPacketReady());
139    // take packet off list here; if recvTiming() on the other side
140    // calls sendTiming() back on us (like SimpleTimingCpu does), then
141    // we get confused by having a non-active packet on transmitList
142    DeferredPacket dp = transmitList.front();
143    transmitList.pop_front();
144    bool success = sendTiming(dp.pkt);
145
146    if (success) {
147        if (!transmitList.empty() && !sendEvent->scheduled()) {
148            Tick time = transmitList.front().tick;
149            sendEvent->schedule(time <= curTick ? curTick+1 : time);
150        }
151
152        if (transmitList.empty() && drainEvent) {
153            drainEvent->process();
154            drainEvent = NULL;
155        }
156    } else {
157        // Unsuccessful, need to put back on transmitList.  Callee
158        // should not have messed with it (since it didn't accept that
159        // packet), so we can just push it back on the front.
160        assert(!sendEvent->scheduled());
161        transmitList.push_front(dp);
162    }
163
164    waitingOnRetry = !success;
165
166    if (waitingOnRetry) {
167        DPRINTF(Bus, "Send failed, waiting on retry\n");
168    }
169}
170
171
172void
173SimpleTimingPort::recvRetry()
174{
175    DPRINTF(Bus, "Received retry\n");
176    assert(waitingOnRetry);
177    sendDeferredPacket();
178}
179
180
181void
182SimpleTimingPort::processSendEvent()
183{
184    assert(!waitingOnRetry);
185    sendDeferredPacket();
186}
187
188
189unsigned int
190SimpleTimingPort::drain(Event *de)
191{
192    if (transmitList.size() == 0)
193        return 0;
194    drainEvent = de;
195    return 1;
196}
197