38fc89e3fecdb46b9a39f4f8b02395aee94fa7ba
[gem5.git] / src / cpu / ozone / front_end.hh
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: Kevin Lim
29 */
30
31 #ifndef __CPU_OZONE_FRONT_END_HH__
32 #define __CPU_OZONE_FRONT_END_HH__
33
34 #include <deque>
35
36 #include "arch/utility.hh"
37 #include "base/timebuf.hh"
38 #include "cpu/inst_seq.hh"
39 #include "cpu/o3/bpred_unit.hh"
40 #include "cpu/ozone/rename_table.hh"
41 #include "mem/port.hh"
42 #include "mem/request.hh"
43 #include "sim/eventq.hh"
44 #include "sim/stats.hh"
45
46 class ThreadContext;
47 class MemObject;
48 template <class>
49 class OzoneThreadState;
50 class PageTable;
51 template <class>
52 class TimeBuffer;
53
54 template <class Impl>
55 class FrontEnd
56 {
57 public:
58 typedef typename Impl::Params Params;
59 typedef typename Impl::DynInst DynInst;
60 typedef typename Impl::DynInstPtr DynInstPtr;
61 typedef typename Impl::CPUType CPUType;
62 typedef typename Impl::BackEnd BackEnd;
63
64 typedef typename Impl::CPUType::OzoneTC OzoneTC;
65 typedef typename Impl::CPUType::CommStruct CommStruct;
66
67 /** IcachePort class. Handles doing the communication with the
68 * cache/memory.
69 */
70 class IcachePort : public Port
71 {
72 protected:
73 /** Pointer to FE. */
74 FrontEnd<Impl> *fe;
75
76 public:
77 /** Default constructor. */
78 IcachePort(FrontEnd<Impl> *_fe)
79 : fe(_fe)
80 { }
81
82 protected:
83 /** Atomic version of receive. Panics. */
84 virtual Tick recvAtomic(PacketPtr pkt);
85
86 /** Functional version of receive. Panics. */
87 virtual void recvFunctional(PacketPtr pkt);
88
89 /** Receives status change. Other than range changing, panics. */
90 virtual void recvStatusChange(Status status);
91
92 /** Returns the address ranges of this device. */
93 virtual void getDeviceAddressRanges(AddrRangeList &resp,
94 bool &snoop)
95 { resp.clear(); snoop = true; }
96
97 /** Timing version of receive. Handles setting fetch to the
98 * proper status to start fetching. */
99 virtual bool recvTiming(PacketPtr pkt);
100
101 /** Handles doing a retry of a failed fetch. */
102 virtual void recvRetry();
103 };
104
105 FrontEnd(Params *params);
106
107 std::string name() const;
108
109 void setCPU(CPUType *cpu_ptr);
110
111 void setBackEnd(BackEnd *back_end_ptr)
112 { backEnd = back_end_ptr; }
113
114 void setCommBuffer(TimeBuffer<CommStruct> *_comm);
115
116 void setTC(ThreadContext *tc_ptr);
117
118 void setThreadState(OzoneThreadState<Impl> *thread_ptr)
119 { thread = thread_ptr; }
120
121 void regStats();
122
123 Port *getIcachePort() { return &icachePort; }
124
125 void tick();
126 Fault fetchCacheLine();
127 void processInst(DynInstPtr &inst);
128 void squash(const InstSeqNum &squash_num, const Addr &next_PC,
129 const bool is_branch = false, const bool branch_taken = false);
130 DynInstPtr getInst();
131
132 void processCacheCompletion(PacketPtr pkt);
133
134 void addFreeRegs(int num_freed);
135
136 bool isEmpty() { return instBuffer.empty(); }
137
138 void switchOut();
139
140 void doSwitchOut();
141
142 void takeOverFrom(ThreadContext *old_tc = NULL);
143
144 bool isSwitchedOut() { return switchedOut; }
145
146 bool switchedOut;
147
148 private:
149 void recvRetry();
150
151 bool updateStatus();
152
153 void checkBE();
154 DynInstPtr getInstFromCacheline();
155 void renameInst(DynInstPtr &inst);
156 // Returns true if we need to stop the front end this cycle
157 bool processBarriers(DynInstPtr &inst);
158
159 void handleFault(Fault &fault);
160 public:
161 Fault getFault() { return fetchFault; }
162 private:
163 Fault fetchFault;
164
165 // Align an address (typically a PC) to the start of an I-cache block.
166 // We fold in the PISA 64- to 32-bit conversion here as well.
167 Addr icacheBlockAlignPC(Addr addr)
168 {
169 addr = TheISA::realPCToFetchPC(addr);
170 return (addr & ~(cacheBlkMask));
171 }
172
173 InstSeqNum getAndIncrementInstSeq()
174 { return cpu->globalSeqNum++; }
175
176 public:
177 CPUType *cpu;
178
179 BackEnd *backEnd;
180
181 ThreadContext *tc;
182
183 OzoneThreadState<Impl> *thread;
184
185 enum Status {
186 Running,
187 Idle,
188 IcacheWaitResponse,
189 IcacheWaitRetry,
190 IcacheAccessComplete,
191 SerializeBlocked,
192 SerializeComplete,
193 RenameBlocked,
194 QuiescePending,
195 TrapPending,
196 BEBlocked
197 };
198
199 Status status;
200
201 private:
202 TimeBuffer<CommStruct> *comm;
203 typename TimeBuffer<CommStruct>::wire fromCommit;
204
205 typedef typename Impl::BranchPred BranchPred;
206
207 BranchPred branchPred;
208
209 IcachePort icachePort;
210
211 RequestPtr memReq;
212
213 /** Mask to get a cache block's address. */
214 Addr cacheBlkMask;
215
216 unsigned cacheBlkSize;
217
218 Addr cacheBlkPC;
219
220 /** The cache line being fetched. */
221 uint8_t *cacheData;
222
223 bool fetchCacheLineNextCycle;
224
225 bool cacheBlkValid;
226
227 bool cacheBlocked;
228
229 /** The packet that is waiting to be retried. */
230 PacketPtr retryPkt;
231
232 public:
233 RenameTable<Impl> renameTable;
234
235 private:
236 Addr PC;
237 Addr nextPC;
238
239 public:
240 void setPC(Addr val) { PC = val; }
241 void setNextPC(Addr val) { nextPC = val; }
242
243 void wakeFromQuiesce();
244
245 void dumpInsts();
246
247 private:
248 TimeBuffer<int> numInstsReady;
249
250 typedef typename std::deque<DynInstPtr> InstBuff;
251 typedef typename InstBuff::iterator InstBuffIt;
252
253 InstBuff feBuffer;
254
255 InstBuff instBuffer;
256
257 int instBufferSize;
258
259 int maxInstBufferSize;
260
261 int latency;
262
263 int width;
264
265 int freeRegs;
266
267 int numPhysRegs;
268
269 bool serializeNext;
270
271 DynInstPtr barrierInst;
272
273 public:
274 bool interruptPending;
275 private:
276 // number of idle cycles
277 /*
278 Stats::Average notIdleFraction;
279 Stats::Formula idleFraction;
280 */
281 // @todo: Consider making these vectors and tracking on a per thread basis.
282 /** Stat for total number of cycles stalled due to an icache miss. */
283 Stats::Scalar icacheStallCycles;
284 /** Stat for total number of fetched instructions. */
285 Stats::Scalar fetchedInsts;
286 Stats::Scalar fetchedBranches;
287 /** Stat for total number of predicted branches. */
288 Stats::Scalar predictedBranches;
289 /** Stat for total number of cycles spent fetching. */
290 Stats::Scalar fetchCycles;
291
292 Stats::Scalar fetchIdleCycles;
293 /** Stat for total number of cycles spent squashing. */
294 Stats::Scalar fetchSquashCycles;
295 /** Stat for total number of cycles spent blocked due to other stages in
296 * the pipeline.
297 */
298 Stats::Scalar fetchBlockedCycles;
299 /** Stat for total number of fetched cache lines. */
300 Stats::Scalar fetchedCacheLines;
301
302 Stats::Scalar fetchIcacheSquashes;
303 /** Distribution of number of instructions fetched each cycle. */
304 Stats::Distribution fetchNisnDist;
305 // Stats::Vector qfull_iq_occupancy;
306 // Stats::VectorDistribution qfull_iq_occ_dist_;
307 Stats::Formula idleRate;
308 Stats::Formula branchRate;
309 Stats::Formula fetchRate;
310 Stats::Scalar IFQCount; // cumulative IFQ occupancy
311 Stats::Formula IFQOccupancy;
312 Stats::Formula IFQLatency;
313 Stats::Scalar IFQFcount; // cumulative IFQ full count
314 Stats::Formula IFQFullRate;
315
316 Stats::Scalar dispatchCountStat;
317 Stats::Scalar dispatchedSerializing;
318 Stats::Scalar dispatchedTempSerializing;
319 Stats::Scalar dispatchSerializeStallCycles;
320 Stats::Formula dispatchRate;
321 Stats::Formula regIntFull;
322 Stats::Formula regFpFull;
323 };
324
325 #endif // __CPU_OZONE_FRONT_END_HH__