misc: Standardize the way create() constructs SimObjects.
[gem5.git] / src / cpu / o3 / fu_pool.cc
1 /*
2 * Copyright (c) 2012-2013 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include "cpu/o3/fu_pool.hh"
42
43 #include <sstream>
44
45 #include "cpu/func_unit.hh"
46
47 using namespace std;
48
49 ////////////////////////////////////////////////////////////////////////////
50 //
51 // A pool of function units
52 //
53
54 inline void
55 FUPool::FUIdxQueue::addFU(int fu_idx)
56 {
57 funcUnitsIdx.push_back(fu_idx);
58 ++size;
59 }
60
61 inline int
62 FUPool::FUIdxQueue::getFU()
63 {
64 int retval = funcUnitsIdx[idx++];
65
66 if (idx == size)
67 idx = 0;
68
69 return retval;
70 }
71
72 FUPool::~FUPool()
73 {
74 fuListIterator i = funcUnits.begin();
75 fuListIterator end = funcUnits.end();
76 for (; i != end; ++i)
77 delete *i;
78 }
79
80
81 // Constructor
82 FUPool::FUPool(const Params &p)
83 : SimObject(p)
84 {
85 numFU = 0;
86
87 funcUnits.clear();
88
89 maxOpLatencies.fill(Cycles(0));
90 pipelined.fill(true);
91
92 //
93 // Iterate through the list of FUDescData structures
94 //
95 const vector<FUDesc *> &paramList = p.FUList;
96 for (FUDDiterator i = paramList.begin(); i != paramList.end(); ++i) {
97
98 //
99 // Don't bother with this if we're not going to create any FU's
100 //
101 if ((*i)->number) {
102 //
103 // Create the FuncUnit object from this structure
104 // - add the capabilities listed in the FU's operation
105 // description
106 //
107 // We create the first unit, then duplicate it as needed
108 //
109 FuncUnit *fu = new FuncUnit;
110
111 OPDDiterator j = (*i)->opDescList.begin();
112 OPDDiterator end = (*i)->opDescList.end();
113 for (; j != end; ++j) {
114 // indicate that this pool has this capability
115 capabilityList.set((*j)->opClass);
116
117 // Add each of the FU's that will have this capability to the
118 // appropriate queue.
119 for (int k = 0; k < (*i)->number; ++k)
120 fuPerCapList[(*j)->opClass].addFU(numFU + k);
121
122 // indicate that this FU has the capability
123 fu->addCapability((*j)->opClass, (*j)->opLat, (*j)->pipelined);
124
125 if ((*j)->opLat > maxOpLatencies[(*j)->opClass])
126 maxOpLatencies[(*j)->opClass] = (*j)->opLat;
127
128 if (!(*j)->pipelined)
129 pipelined[(*j)->opClass] = false;
130 }
131
132 numFU++;
133
134 // Add the appropriate number of copies of this FU to the list
135 fu->name = (*i)->name() + "(0)";
136 funcUnits.push_back(fu);
137
138 for (int c = 1; c < (*i)->number; ++c) {
139 ostringstream s;
140 numFU++;
141 FuncUnit *fu2 = new FuncUnit(*fu);
142
143 s << (*i)->name() << "(" << c << ")";
144 fu2->name = s.str();
145 funcUnits.push_back(fu2);
146 }
147 }
148 }
149
150 unitBusy.resize(numFU);
151
152 for (int i = 0; i < numFU; i++) {
153 unitBusy[i] = false;
154 }
155 }
156
157 int
158 FUPool::getUnit(OpClass capability)
159 {
160 // If this pool doesn't have the specified capability,
161 // return this information to the caller
162 if (!capabilityList[capability])
163 return -2;
164
165 int fu_idx = fuPerCapList[capability].getFU();
166 int start_idx = fu_idx;
167
168 // Iterate through the circular queue if needed, stopping if we've reached
169 // the first element again.
170 while (unitBusy[fu_idx]) {
171 fu_idx = fuPerCapList[capability].getFU();
172 if (fu_idx == start_idx) {
173 // No FU available
174 return -1;
175 }
176 }
177
178 assert(fu_idx < numFU);
179
180 unitBusy[fu_idx] = true;
181
182 return fu_idx;
183 }
184
185 void
186 FUPool::freeUnitNextCycle(int fu_idx)
187 {
188 assert(unitBusy[fu_idx]);
189 unitsToBeFreed.push_back(fu_idx);
190 }
191
192 void
193 FUPool::processFreeUnits()
194 {
195 while (!unitsToBeFreed.empty()) {
196 int fu_idx = unitsToBeFreed.back();
197 unitsToBeFreed.pop_back();
198
199 assert(unitBusy[fu_idx]);
200
201 unitBusy[fu_idx] = false;
202 }
203 }
204
205 void
206 FUPool::dump()
207 {
208 cout << "Function Unit Pool (" << name() << ")\n";
209 cout << "======================================\n";
210 cout << "Free List:\n";
211
212 for (int i = 0; i < numFU; ++i) {
213 if (unitBusy[i]) {
214 continue;
215 }
216
217 cout << " [" << i << "] : ";
218
219 cout << funcUnits[i]->name << " ";
220
221 cout << "\n";
222 }
223
224 cout << "======================================\n";
225 cout << "Busy List:\n";
226 for (int i = 0; i < numFU; ++i) {
227 if (!unitBusy[i]) {
228 continue;
229 }
230
231 cout << " [" << i << "] : ";
232
233 cout << funcUnits[i]->name << " ";
234
235 cout << "\n";
236 }
237 }
238
239 bool
240 FUPool::isDrained() const
241 {
242 bool is_drained = true;
243 for (int i = 0; i < numFU; i++)
244 is_drained = is_drained && !unitBusy[i];
245
246 return is_drained;
247 }
248
249 //
250
251 ////////////////////////////////////////////////////////////////////////////
252 //
253 // The SimObjects we use to get the FU information into the simulator
254 //
255 ////////////////////////////////////////////////////////////////////////////
256
257 //
258 // FUPool - Contails a list of FUDesc objects to make available
259 //
260
261 //
262 // The FuPool object
263 //
264 FUPool *
265 FUPoolParams::create() const
266 {
267 return new FUPool(*this);
268 }