7b1c14467d4373aba314007e48fbfc4244813e2e
[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 (target->intersect(pkt)) {
44 if (!fixPacket(pkt, target)) {
45 // fixPacket returns true for continue, false for done
46 return;
47 }
48 }
49 }
50 }
51
52 void
53 SimpleTimingPort::recvFunctional(PacketPtr pkt)
54 {
55 checkFunctional(pkt);
56
57 // Just do an atomic access and throw away the returned latency
58 if (pkt->result != Packet::Success)
59 recvAtomic(pkt);
60 }
61
62 bool
63 SimpleTimingPort::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->result != Packet::Nacked);
71 Tick latency = recvAtomic(pkt);
72 // turn packet around to go back to requester if response expected
73 if (pkt->needsResponse()) {
74 pkt->makeTimingResponse();
75 schedSendTiming(pkt, latency);
76 }
77 else if (pkt->cmd != MemCmd::UpgradeReq) {
78 delete pkt->req;
79 delete pkt;
80 }
81 return true;
82 }
83
84
85 void
86 SimpleTimingPort::schedSendTiming(PacketPtr pkt, Tick when)
87 {
88 assert(when > curTick);
89
90 // Nothing is on the list: add it and schedule an event
91 if (transmitList.empty()) {
92 assert(!sendEvent->scheduled());
93 sendEvent->schedule(when);
94 transmitList.push_back(DeferredPacket(when, pkt));
95 return;
96 }
97
98 // something is on the list and this belongs at the end
99 if (when >= transmitList.back().tick) {
100 transmitList.push_back(DeferredPacket(when, pkt));
101 return;
102 }
103 // Something is on the list and this belongs somewhere else
104 DeferredPacketIterator i = transmitList.begin();
105 DeferredPacketIterator end = transmitList.end();
106
107 for (; i != end; ++i) {
108 if (when < i->tick) {
109 if (i == transmitList.begin()) {
110 //Inserting at begining, reschedule
111 sendEvent->reschedule(when);
112 }
113 transmitList.insert(i, DeferredPacket(when, pkt));
114 return;
115 }
116 }
117 assert(false); // should never get here
118 }
119
120
121 void
122 SimpleTimingPort::sendDeferredPacket()
123 {
124 assert(deferredPacketReady());
125 bool success = sendTiming(transmitList.front().pkt);
126
127 if (success) {
128 //send successful, remove packet
129 transmitList.pop_front();
130 if (!transmitList.empty()) {
131 Tick time = transmitList.front().tick;
132 sendEvent->schedule(time <= curTick ? curTick+1 : time);
133 }
134
135 if (transmitList.empty() && drainEvent) {
136 drainEvent->process();
137 drainEvent = NULL;
138 }
139 }
140
141 waitingOnRetry = !success;
142
143 if (waitingOnRetry) {
144 DPRINTF(Bus, "Send failed, waiting on retry\n");
145 }
146 }
147
148
149 void
150 SimpleTimingPort::recvRetry()
151 {
152 DPRINTF(Bus, "Received retry\n");
153 assert(waitingOnRetry);
154 sendDeferredPacket();
155 }
156
157
158 void
159 SimpleTimingPort::processSendEvent()
160 {
161 assert(!waitingOnRetry);
162 sendDeferredPacket();
163 }
164
165
166 unsigned int
167 SimpleTimingPort::drain(Event *de)
168 {
169 if (transmitList.size() == 0)
170 return 0;
171 drainEvent = de;
172 return 1;
173 }