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