gem5  v21.1.0.2
token_port.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 Advanced Micro Devices, Inc.
3  * All rights reserved.
4  *
5  * For use for simulation and test purposes only
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 
35 #include "mem/token_port.hh"
36 
37 #include "base/trace.hh"
38 #include "debug/TokenPort.hh"
39 
40 namespace gem5
41 {
42 
43 void
45 {
46  RequestPort::bind(peer);
47 }
48 
49 void
51 {
52  panic_if(!tokenManager, "TokenManager not set for %s.\n", name());
53 
54  tokenManager->recvTokens(num_tokens);
55 }
56 
57 bool
59 {
60  panic_if(!tokenManager, "TokenManager not set for %s.\n", name());
61 
62  return tokenManager->haveTokens(num_tokens);
63 }
64 
65 void
67 {
68  panic_if(!tokenManager, "TokenManager not set for %s.\n", name());
69 
70  tokenManager->acquireTokens(num_tokens);
71 }
72 
73 void
75 {
76  tokenManager = _tokenManager;
77 }
78 
79 void
81 {
82  fatal_if(!tokenRequestPort, "Tried sendTokens to non-token requestor!\n");
83 
84  // Send tokens to a requestor
85  tokenRequestPort->recvTokens(num_tokens);
86 }
87 
88 void
90 {
91  // TokenResponsePort is allowed to bind to either TokenRequestPort or a
92  // RequestPort as fallback. If the type is a RequestPort, tokenRequestPort
93  // is set to nullptr to indicate tokens should not be exchanged.
94  auto *token_request_port = dynamic_cast<TokenRequestPort*>(&peer);
95  auto *request_port = dynamic_cast<RequestPort*>(&peer);
96  if (!token_request_port && !request_port) {
97  fatal("Attempt to bind port %s to unsupported response port %s.",
98  name(), peer.name());
99  } else if (token_request_port) {
100  // response port keeps track of the request port
101  tokenRequestPort = token_request_port;
102 
103  // request port also keeps track of response port
104  tokenRequestPort->bind(*this);
105  } else if (request_port) {
106  tokenRequestPort = nullptr;
107  }
108 }
109 
110 void
112 {
114  tokenRequestPort = nullptr;
115 }
116 
117 void
119 {
120  // fallback to QueuedResponsePort-like impl for now
121  panic_if(respQueue.empty(),
122  "Attempted to retry a response when no retry was queued!\n");
123 
124  PacketPtr pkt = respQueue.front();
125  bool success = ResponsePort::sendTimingResp(pkt);
126 
127  if (success) {
128  respQueue.pop_front();
129  }
130 }
131 
132 bool
134 {
135  bool success = ResponsePort::sendTimingResp(pkt);
136 
137  if (!success) {
138  respQueue.push_back(pkt);
139  }
140 
141  return success;
142 }
143 
145 {
146  availableTokens = init_tokens;
147  maxTokens = init_tokens;
148 }
149 
150 int
152 {
153  return maxTokens;
154 }
155 
156 void
158 {
159  availableTokens += num_tokens;
160 
161  DPRINTF(TokenPort, "Received %d tokens, have %d\n",
162  num_tokens, availableTokens);
163 
165  "More tokens available than the maximum after recvTokens!\n");
166 }
167 
168 bool
170 {
171  return (availableTokens >= num_tokens);
172 }
173 
174 void
176 {
177  panic_if(!haveTokens(num_tokens),
178  "Attempted to acquire more tokens than are available!\n");
179 
180  availableTokens -= num_tokens;
181 
182  DPRINTF(TokenPort, "Acquired %d tokens, have %d\n",
183  num_tokens, availableTokens);
184 }
185 
186 } // namespace gem5
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:189
gem5::TokenRequestPort::tokenManager
TokenManager * tokenManager
Definition: token_port.hh:50
gem5::Port::name
const std::string name() const
Return port name (for DPRINTF).
Definition: port.hh:111
gem5::RequestPort::bind
void bind(Port &peer) override
Bind this request port to a response port.
Definition: port.cc:128
gem5::TokenManager::haveTokens
bool haveTokens(int num_tokens)
Query is num_tokens tokens are available.
Definition: token_port.cc:169
gem5::TokenManager
Definition: token_port.hh:132
gem5::TokenManager::availableTokens
int availableTokens
Definition: token_port.hh:139
gem5::ResponsePort::responderUnbind
void responderUnbind()
Called by the request port to unbind.
Definition: port.cc:183
gem5::TokenRequestPort::recvTokens
void recvTokens(int num_tokens)
Receive tokens returned by the response port.
Definition: token_port.cc:50
gem5::TokenRequestPort::bind
void bind(Port &peer) override
Bind this request port to response port.
Definition: token_port.cc:44
gem5::TokenManager::maxTokens
int maxTokens
Definition: token_port.hh:136
gem5::TokenResponsePort::unbind
void unbind() override
Unbind this response port and associated request port.
Definition: token_port.cc:111
gem5::TokenRequestPort::setTokenManager
void setTokenManager(TokenManager *_tokenManager)
Specify a token manger, which will handle tracking of tokens for a TokenRequestPort/ResponseRequestPo...
Definition: token_port.cc:74
gem5::RequestPort
A RequestPort is a specialisation of a Port, which implements the default protocol for the three diff...
Definition: port.hh:77
gem5::TokenRequestPort::haveTokens
bool haveTokens(int num_tokens)
Query if there are at least num_tokens tokens available to acquire.
Definition: token_port.cc:58
gem5::TokenResponsePort::respQueue
std::deque< PacketPtr > respQueue
Definition: token_port.hh:98
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
gem5::TokenManager::TokenManager
TokenManager(int init_tokens)
Definition: token_port.cc:144
gem5::Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:283
gem5::TokenResponsePort::recvRespRetry
void recvRespRetry() override
Called by the peer if sendTimingResp was called on this protocol (causing recvTimingResp to be called...
Definition: token_port.cc:118
gem5::TokenRequestPort::acquireTokens
void acquireTokens(int num_tokens)
Acquire tokens by decrementing the number of available tokens across the port.
Definition: token_port.cc:66
gem5::ResponsePort::sendTimingResp
bool sendTimingResp(PacketPtr pkt)
Attempt to send a timing response to the request port by calling its corresponding receive function.
Definition: port.hh:370
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:203
gem5::Port
Ports are used to interface objects to each other.
Definition: port.hh:61
gem5::TokenResponsePort::sendTokens
void sendTokens(int num_tokens)
Return num_tokens tokens back to the request port.
Definition: token_port.cc:80
gem5::TokenResponsePort::bind
void bind(Port &peer) override
Bind this response port to a request port.
Definition: token_port.cc:89
token_port.hh
gem5::TokenManager::recvTokens
void recvTokens(int num_tokens)
Increment the number of available tokens by num_tokens.
Definition: token_port.cc:157
gem5::TokenManager::getMaxTokenCount
int getMaxTokenCount() const
Return the maximum possible tokens.
Definition: token_port.cc:151
trace.hh
gem5::TokenResponsePort::sendTimingResp
bool sendTimingResp(PacketPtr pkt)
Definition: token_port.cc:133
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:225
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::TokenResponsePort::tokenRequestPort
TokenRequestPort * tokenRequestPort
Definition: token_port.hh:96
gem5::TokenManager::acquireTokens
void acquireTokens(int num_tokens)
Decrement the number of available tokens by num_tokens.
Definition: token_port.cc:175
gem5::TokenRequestPort
Definition: token_port.hh:46

Generated on Tue Sep 21 2021 12:25:47 for gem5 by doxygen 1.8.17