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