2 * Copyright (c) 2007 MIPS Technologies, Inc.
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.
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.
28 * Authors: Korey Sewell
32 #ifndef __CPU_INORDER_CACHE_UNIT_HH__
33 #define __CPU_INORDER_CACHE_UNIT_HH__
39 #include "arch/tlb.hh"
40 #include "arch/predecoder.hh"
41 #include "cpu/inorder/resource.hh"
42 #include "cpu/inorder/inorder_dyn_inst.hh"
43 #include "mem/packet.hh"
44 #include "mem/packet_access.hh"
45 #include "mem/port.hh"
46 #include "cpu/inorder/pipeline_traits.hh"
47 #include "sim/sim_object.hh"
49 #include "params/InOrderCPU.hh"
52 typedef CacheRequest* CacheReqPtr;
55 typedef CacheReqPacket* CacheReqPktPtr;
57 class CacheUnit : public Resource
60 typedef ThePipeline::DynInstPtr DynInstPtr;
63 CacheUnit(std::string res_name, int res_id, int res_width,
64 int res_latency, InOrderCPU *_cpu, ThePipeline::Params *params);
65 virtual ~CacheUnit() {}
80 /** CachePort class for the Cache Unit. Handles doing the
81 * communication with the cache/memory.
83 class CachePort : public Port
86 /** Pointer to cache port unit */
87 CacheUnit *cachePortUnit;
90 /** Default constructor. */
91 CachePort(CacheUnit *_cachePortUnit)
92 : Port(_cachePortUnit->name() + "-cache-port",
93 (MemObject*)_cachePortUnit->cpu),
94 cachePortUnit(_cachePortUnit)
100 /** Atomic version of receive. Panics. */
101 virtual Tick recvAtomic(PacketPtr pkt);
103 /** Functional version of receive. Panics. */
104 virtual void recvFunctional(PacketPtr pkt);
106 /** Receives status change. Other than range changing, panics. */
107 virtual void recvStatusChange(Status status);
109 /** Returns the address ranges of this device. */
110 virtual void getDeviceAddressRanges(AddrRangeList &resp,
111 AddrRangeList &snoop)
112 { resp.clear(); snoop.clear(); }
114 /** Timing version of receive. Handles setting fetch to the
115 * proper status to start fetching. */
116 virtual bool recvTiming(PacketPtr pkt);
118 /** Handles doing a retry of a failed fetch. */
119 virtual void recvRetry();
122 enum CachePortStatus {
130 virtual ResourceRequest* getRequest(DynInstPtr _inst, int stage_num,
131 int res_idx, int slot_num,
134 void requestAgain(DynInstPtr inst, bool &try_request);
136 int getSlot(DynInstPtr inst);
138 void freeSlot(int slot_num);
140 /** Execute the function of this resource. The Default is action
141 * is to do nothing. More specific models will derive from this
142 * class and define their own execute function.
144 void execute(int slot_num);
146 void squash(DynInstPtr inst, int stage_num,
147 InstSeqNum squash_seq_num, unsigned tid);
149 /** Processes cache completion event. */
150 void processCacheCompletion(PacketPtr pkt);
154 /** Align a PC to the start of an I-cache block. */
155 Addr cacheBlockAlignPC(Addr addr)
157 return (addr & ~(cacheBlkMask));
160 /** Returns a specific port. */
161 Port *getPort(const std::string &if_name, int idx);
164 Fault read(DynInstPtr inst, Addr addr, T &data, unsigned flags);
167 Fault write(DynInstPtr inst, T data, Addr addr, unsigned flags,
170 Fault doTLBAccess(DynInstPtr inst, CacheReqPtr cache_req, int acc_size,
171 int flags, TheISA::TLB::Mode tlb_mode);
173 /** Read/Write on behalf of an instruction.
174 * curResSlot needs to be a valid value in instruction.
176 Fault doCacheAccess(DynInstPtr inst, uint64_t *write_result=NULL);
178 void prefetch(DynInstPtr inst);
180 void writeHint(DynInstPtr inst);
182 uint64_t getMemData(Packet *packet);
185 /** Cache interface. */
186 CachePort *cachePort;
188 CachePortStatus cacheStatus;
190 CacheReqPtr retryReq;
198 std::vector<Addr> addrList;
200 std::map<Addr, InstSeqNum> addrMap;
207 /** Align a PC to the start of the Cache block. */
208 Addr cacheBlockAlign(Addr addr)
210 return (addr & ~(cacheBlkMask));
213 /** The mem line being fetched. */
214 uint8_t *fetchData[ThePipeline::MaxThreads];
216 /** @TODO: Move functionaly of fetching more than
217 one instruction to 'fetch unit'*/
218 /** The Addr of the cacheline that has been loaded. */
219 //Addr cacheBlockAddr[ThePipeline::MaxThreads];
220 //unsigned fetchOffset[ThePipeline::MaxThreads];
222 TheISA::Predecoder predecoder;
224 bool tlbBlocked[ThePipeline::MaxThreads];
231 class CacheUnitEvent : public ResourceEvent {
233 const std::string name() const
235 return "CacheUnitEvent";
239 /** Constructs a resource event. */
241 virtual ~CacheUnitEvent() {}
243 /** Processes a resource event. */
244 virtual void process();
247 class CacheRequest : public ResourceRequest
250 CacheRequest(CacheUnit *cres, DynInstPtr inst, int stage_num, int res_idx,
251 int slot_num, unsigned cmd, int req_size,
252 MemCmd::Command pkt_cmd, unsigned flags, int cpu_id)
253 : ResourceRequest(cres, inst, stage_num, res_idx, slot_num, cmd),
254 pktCmd(pkt_cmd), memReq(NULL), reqData(NULL), dataPkt(NULL),
255 retryPkt(NULL), memAccComplete(false), memAccPending(false),
260 virtual ~CacheRequest()
267 virtual PacketDataPtr getData()
271 setMemAccCompleted(bool completed = true)
273 memAccComplete = completed;
276 bool isMemAccComplete() { return memAccComplete; }
278 void setMemAccPending(bool pending = true) { memAccPending = pending; }
279 bool isMemAccPending() { return memAccPending; }
281 //Make this data private/protected!
282 MemCmd::Command pktCmd;
284 PacketDataPtr reqData;
293 class CacheReqPacket : public Packet
296 CacheReqPacket(CacheRequest *_req,
297 Command _cmd, short _dest)
298 : Packet(_req->memReq, _cmd, _dest), cacheReq(_req)
303 CacheRequest *cacheReq;
306 #endif //__CPU_CACHE_UNIT_HH__