blob: c4a5dc0bdc39b1bd7f4ed99ec12496a324331ea8 [file] [log] [blame]
Korey Sewell973d8b82009-02-10 15:49:29 -08001/*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
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: Korey Sewell
29 *
30 */
31
32#ifndef __CPU_INORDER_CACHE_UNIT_HH__
33#define __CPU_INORDER_CACHE_UNIT_HH__
34
Korey Sewell973d8b82009-02-10 15:49:29 -080035#include <list>
36#include <string>
Nathan Binkert39a05562011-04-15 10:44:06 -070037#include <vector>
Korey Sewell973d8b82009-02-10 15:49:29 -080038
Korey Sewell3603dd22009-05-12 15:01:15 -040039#include "arch/predecoder.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070040#include "arch/tlb.hh"
41#include "config/the_isa.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080042#include "cpu/inorder/inorder_dyn_inst.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070043#include "cpu/inorder/pipeline_traits.hh"
44#include "cpu/inorder/resource.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080045#include "mem/packet.hh"
46#include "mem/packet_access.hh"
47#include "mem/port.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080048#include "params/InOrderCPU.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070049#include "sim/sim_object.hh"
Korey Sewell973d8b82009-02-10 15:49:29 -080050
51class CacheRequest;
52typedef CacheRequest* CacheReqPtr;
53
54class CacheReqPacket;
55typedef CacheReqPacket* CacheReqPktPtr;
56
Korey Sewellcf4a00c2009-02-10 15:49:29 -080057class CacheUnit : public Resource
58{
Korey Sewell973d8b82009-02-10 15:49:29 -080059 public:
60 typedef ThePipeline::DynInstPtr DynInstPtr;
61
62 public:
63 CacheUnit(std::string res_name, int res_id, int res_width,
64 int res_latency, InOrderCPU *_cpu, ThePipeline::Params *params);
Korey Sewell973d8b82009-02-10 15:49:29 -080065
66 enum Command {
Korey Sewell973d8b82009-02-10 15:49:29 -080067 InitiateReadData,
68 CompleteReadData,
69 InitiateWriteData,
70 CompleteWriteData,
Korey Sewell69394822010-01-31 18:30:35 -050071 InitSecondSplitRead,
72 InitSecondSplitWrite,
Korey Sewellbe6724f2010-01-31 18:30:43 -050073 CompleteSecondSplitRead,
74 CompleteSecondSplitWrite
Korey Sewell973d8b82009-02-10 15:49:29 -080075 };
76
77 public:
78 /** CachePort class for the Cache Unit. Handles doing the
79 * communication with the cache/memory.
80 */
81 class CachePort : public Port
82 {
83 protected:
84 /** Pointer to cache port unit */
85 CacheUnit *cachePortUnit;
86
87 public:
88 /** Default constructor. */
89 CachePort(CacheUnit *_cachePortUnit)
Korey Sewellcf4a00c2009-02-10 15:49:29 -080090 : Port(_cachePortUnit->name() + "-cache-port",
91 (MemObject*)_cachePortUnit->cpu),
92 cachePortUnit(_cachePortUnit)
Korey Sewell973d8b82009-02-10 15:49:29 -080093 { }
94
95 bool snoopRangeSent;
96
97 protected:
98 /** Atomic version of receive. Panics. */
Korey Sewell71b67d42010-06-24 15:34:19 -040099 Tick recvAtomic(PacketPtr pkt);
Korey Sewell973d8b82009-02-10 15:49:29 -0800100
101 /** Functional version of receive. Panics. */
Korey Sewell71b67d42010-06-24 15:34:19 -0400102 void recvFunctional(PacketPtr pkt);
Korey Sewell973d8b82009-02-10 15:49:29 -0800103
104 /** Receives status change. Other than range changing, panics. */
Korey Sewell71b67d42010-06-24 15:34:19 -0400105 void recvStatusChange(Status status);
Korey Sewell973d8b82009-02-10 15:49:29 -0800106
107 /** Returns the address ranges of this device. */
Korey Sewell71b67d42010-06-24 15:34:19 -0400108 void getDeviceAddressRanges(AddrRangeList &resp,
Korey Sewell973d8b82009-02-10 15:49:29 -0800109 AddrRangeList &snoop)
110 { resp.clear(); snoop.clear(); }
111
112 /** Timing version of receive. Handles setting fetch to the
113 * proper status to start fetching. */
Korey Sewell71b67d42010-06-24 15:34:19 -0400114 bool recvTiming(PacketPtr pkt);
Korey Sewell973d8b82009-02-10 15:49:29 -0800115
116 /** Handles doing a retry of a failed fetch. */
Korey Sewell71b67d42010-06-24 15:34:19 -0400117 void recvRetry();
Korey Sewell973d8b82009-02-10 15:49:29 -0800118 };
119
Korey Sewelldb2b7212009-05-12 15:01:16 -0400120 void init();
Korey Sewell973d8b82009-02-10 15:49:29 -0800121
Korey Sewell71b67d42010-06-24 15:34:19 -0400122 ResourceRequest* getRequest(DynInstPtr _inst, int stage_num,
Korey Sewellab3d37d2011-02-04 00:08:20 -0500123 int res_idx, int slot_num,
124 unsigned cmd);
Korey Sewell973d8b82009-02-10 15:49:29 -0800125
Korey Sewell69394822010-01-31 18:30:35 -0500126 ResReqPtr findRequest(DynInstPtr inst);
Korey Sewell56ce8ac2011-02-04 00:08:21 -0500127 ResReqPtr findRequest(DynInstPtr inst, int idx);
Korey Sewell69394822010-01-31 18:30:35 -0500128
Korey Sewell973d8b82009-02-10 15:49:29 -0800129 void requestAgain(DynInstPtr inst, bool &try_request);
130
Korey Sewellab3d37d2011-02-04 00:08:20 -0500131 virtual int getSlot(DynInstPtr inst);
Korey Sewell973d8b82009-02-10 15:49:29 -0800132
Korey Sewellab3d37d2011-02-04 00:08:20 -0500133 /** Executes one of the commands from the "Command" enum */
134 virtual void execute(int slot_num);
Korey Sewell973d8b82009-02-10 15:49:29 -0800135
Korey Sewellab3d37d2011-02-04 00:08:20 -0500136 virtual void squash(DynInstPtr inst, int stage_num,
Nathan Binkert47877cf2009-05-26 09:23:13 -0700137 InstSeqNum squash_seq_num, ThreadID tid);
Korey Sewell973d8b82009-02-10 15:49:29 -0800138
Korey Sewelleac5eac2010-01-31 18:26:13 -0500139 void squashDueToMemStall(DynInstPtr inst, int stage_num,
140 InstSeqNum squash_seq_num, ThreadID tid);
141
Korey Sewell68d962f2011-02-04 00:08:22 -0500142 virtual void squashCacheRequest(CacheReqPtr req_ptr);
143
Korey Sewellab3d37d2011-02-04 00:08:20 -0500144 /** After memory request is completedd in the cache, then do final
145 processing to complete the request in the CPU.
146 */
Korey Sewell68d962f2011-02-04 00:08:22 -0500147 virtual void processCacheCompletion(PacketPtr pkt);
148
149 /** Create request that will interface w/TLB and Memory objects */
150 virtual void setupMemRequest(DynInstPtr inst, CacheReqPtr cache_req,
151 int acc_size, int flags);
Korey Sewell973d8b82009-02-10 15:49:29 -0800152
153 void recvRetry();
154
Korey Sewell973d8b82009-02-10 15:49:29 -0800155 /** Returns a specific port. */
156 Port *getPort(const std::string &if_name, int idx);
Korey Sewell69394822010-01-31 18:30:35 -0500157
Gabe Blackaa8c6e92010-08-13 06:16:02 -0700158 Fault read(DynInstPtr inst, Addr addr,
159 uint8_t *data, unsigned size, unsigned flags);
Korey Sewelldb2b7212009-05-12 15:01:16 -0400160
Gabe Blackaa8c6e92010-08-13 06:16:02 -0700161 Fault write(DynInstPtr inst, uint8_t *data, unsigned size,
162 Addr addr, unsigned flags, uint64_t *res);
Korey Sewelldb2b7212009-05-12 15:01:16 -0400163
Korey Sewelle396a342011-02-04 00:09:20 -0500164 void doTLBAccess(DynInstPtr inst, CacheReqPtr cache_req, int acc_size,
Korey Sewelldb2b7212009-05-12 15:01:16 -0400165 int flags, TheISA::TLB::Mode tlb_mode);
166
Korey Sewell973d8b82009-02-10 15:49:29 -0800167 /** Read/Write on behalf of an instruction.
168 * curResSlot needs to be a valid value in instruction.
169 */
Korey Sewelle396a342011-02-04 00:09:20 -0500170 void doCacheAccess(DynInstPtr inst, uint64_t *write_result=NULL,
Korey Sewellf95430d2010-06-24 15:34:12 -0400171 CacheReqPtr split_req=NULL);
Korey Sewell973d8b82009-02-10 15:49:29 -0800172
173 uint64_t getMemData(Packet *packet);
174
Korey Sewell9357e352010-01-31 18:30:48 -0500175 void setAddrDependency(DynInstPtr inst);
Korey Sewell68d962f2011-02-04 00:08:22 -0500176 virtual void removeAddrDependency(DynInstPtr inst);
Korey Sewell9357e352010-01-31 18:30:48 -0500177
Korey Sewell973d8b82009-02-10 15:49:29 -0800178 protected:
179 /** Cache interface. */
180 CachePort *cachePort;
181
Korey Sewell82c5a752010-01-31 18:29:18 -0500182 bool cachePortBlocked;
Korey Sewell973d8b82009-02-10 15:49:29 -0800183
Korey Sewell611a8642010-01-31 18:27:12 -0500184 std::vector<Addr> addrList[ThePipeline::MaxThreads];
Korey Sewell973d8b82009-02-10 15:49:29 -0800185
Korey Sewell611a8642010-01-31 18:27:12 -0500186 std::map<Addr, InstSeqNum> addrMap[ThePipeline::MaxThreads];
Korey Sewell973d8b82009-02-10 15:49:29 -0800187
188 public:
189 int cacheBlkSize;
190
191 int cacheBlkMask;
192
193 /** Align a PC to the start of the Cache block. */
194 Addr cacheBlockAlign(Addr addr)
195 {
196 return (addr & ~(cacheBlkMask));
197 }
198
Korey Sewelldb2b7212009-05-12 15:01:16 -0400199 bool tlbBlocked[ThePipeline::MaxThreads];
200
201 TheISA::TLB* tlb();
202
203 TheISA::TLB *_tlb;
Korey Sewell973d8b82009-02-10 15:49:29 -0800204};
205
Korey Sewelldb2b7212009-05-12 15:01:16 -0400206class CacheUnitEvent : public ResourceEvent {
207 public:
208 const std::string name() const
209 {
210 return "CacheUnitEvent";
211 }
Korey Sewell973d8b82009-02-10 15:49:29 -0800212
Korey Sewell973d8b82009-02-10 15:49:29 -0800213
Korey Sewelldb2b7212009-05-12 15:01:16 -0400214 /** Constructs a resource event. */
215 CacheUnitEvent();
216 virtual ~CacheUnitEvent() {}
217
218 /** Processes a resource event. */
Korey Sewell71b67d42010-06-24 15:34:19 -0400219 void process();
Korey Sewell973d8b82009-02-10 15:49:29 -0800220};
221
Korey Sewelld6422672011-02-18 14:28:30 -0500222//@todo: Move into CacheUnit Class for private access to "valid" field
Korey Sewellcf4a00c2009-02-10 15:49:29 -0800223class CacheRequest : public ResourceRequest
224{
Korey Sewell973d8b82009-02-10 15:49:29 -0800225 public:
Korey Sewelld6422672011-02-18 14:28:30 -0500226 CacheRequest(CacheUnit *cres)
227 : ResourceRequest(cres), memReq(NULL), reqData(NULL),
Korey Sewelle0a02102011-02-23 16:30:45 -0500228 dataPkt(NULL), memAccComplete(false),
Korey Sewelld6422672011-02-18 14:28:30 -0500229 memAccPending(false), tlbStall(false), splitAccess(false),
230 splitAccessNum(-1), split2ndAccess(false),
231 fetchBufferFill(false)
Korey Sewelldb2b7212009-05-12 15:01:16 -0400232 { }
Korey Sewell973d8b82009-02-10 15:49:29 -0800233
Korey Sewell973d8b82009-02-10 15:49:29 -0800234 virtual ~CacheRequest()
235 {
Korey Sewelle0a02102011-02-23 16:30:45 -0500236 if (reqData && !splitAccess)
Korey Sewelldb2b7212009-05-12 15:01:16 -0400237 delete [] reqData;
Korey Sewell973d8b82009-02-10 15:49:29 -0800238 }
239
Korey Sewelld6422672011-02-18 14:28:30 -0500240 void setRequest(DynInstPtr _inst, int stage_num, int res_idx, int slot_num,
241 unsigned _cmd, MemCmd::Command pkt_cmd, int idx)
242 {
243 pktCmd = pkt_cmd;
244 instIdx = idx;
245
246 ResourceRequest::setRequest(_inst, stage_num, res_idx, slot_num, _cmd);
247 }
248
249 void clearRequest()
250 {
Korey Sewelle0a02102011-02-23 16:30:45 -0500251 if (reqData && !splitAccess)
Korey Sewell8b4b4a12011-02-18 14:29:17 -0500252 delete [] reqData;
Korey Sewell8b4b4a12011-02-18 14:29:17 -0500253
Korey Sewelld6422672011-02-18 14:28:30 -0500254 memReq = NULL;
255 reqData = NULL;
256 dataPkt = NULL;
Korey Sewelld6422672011-02-18 14:28:30 -0500257 memAccComplete = false;
258 memAccPending = false;
259 tlbStall = false;
260 splitAccess = false;
261 splitAccessNum = -1;
262 split2ndAccess = false;
263 instIdx = 0;
264 fetchBufferFill = false;
265
266 ResourceRequest::clearRequest();
267 }
268
Korey Sewell973d8b82009-02-10 15:49:29 -0800269 virtual PacketDataPtr getData()
Gabe Black672d6a42010-12-20 16:24:40 -0500270 { return reqData; }
Korey Sewell973d8b82009-02-10 15:49:29 -0800271
Korey Sewellcf4a00c2009-02-10 15:49:29 -0800272 void
273 setMemAccCompleted(bool completed = true)
274 {
275 memAccComplete = completed;
276 }
277
Korey Sewell69394822010-01-31 18:30:35 -0500278 bool is2ndSplit()
279 {
280 return split2ndAccess;
281 }
282
Korey Sewell973d8b82009-02-10 15:49:29 -0800283 bool isMemAccComplete() { return memAccComplete; }
284
285 void setMemAccPending(bool pending = true) { memAccPending = pending; }
286 bool isMemAccPending() { return memAccPending; }
287
288 //Make this data private/protected!
289 MemCmd::Command pktCmd;
290 RequestPtr memReq;
291 PacketDataPtr reqData;
Korey Sewelle0a02102011-02-23 16:30:45 -0500292 CacheReqPacket *dataPkt;
Korey Sewell973d8b82009-02-10 15:49:29 -0800293
294 bool memAccComplete;
295 bool memAccPending;
Korey Sewelldb2b7212009-05-12 15:01:16 -0400296 bool tlbStall;
Korey Sewell69394822010-01-31 18:30:35 -0500297
298 bool splitAccess;
299 int splitAccessNum;
300 bool split2ndAccess;
301 int instIdx;
Korey Sewell68d962f2011-02-04 00:08:22 -0500302
303 /** Should we expect block from cache access or fetch buffer? */
304 bool fetchBufferFill;
Korey Sewell973d8b82009-02-10 15:49:29 -0800305};
306
Korey Sewellcf4a00c2009-02-10 15:49:29 -0800307class CacheReqPacket : public Packet
308{
Korey Sewell973d8b82009-02-10 15:49:29 -0800309 public:
310 CacheReqPacket(CacheRequest *_req,
Korey Sewell69394822010-01-31 18:30:35 -0500311 Command _cmd, short _dest, int _idx = 0)
312 : Packet(_req->memReq, _cmd, _dest), cacheReq(_req), instIdx(_idx)
Korey Sewell973d8b82009-02-10 15:49:29 -0800313 {
314
315 }
316
317 CacheRequest *cacheReq;
Korey Sewell69394822010-01-31 18:30:35 -0500318 int instIdx;
319
Korey Sewell973d8b82009-02-10 15:49:29 -0800320};
321
322#endif //__CPU_CACHE_UNIT_HH__