1/*
2 * Copyright (c) 2002-2005 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: Erik Hallnor
29 * Dave Greene
30 * Steve Reinhardt
31 */
32
33/**
34 * @file
35 * Describes a cache based on template policies.
36 */
37
38#ifndef __CACHE_HH__
39#define __CACHE_HH__
40
41#include "base/misc.hh" // fatal, panic, and warn
42#include "cpu/smt.hh" // SMT_MAX_THREADS
43
44#include "mem/cache/base_cache.hh"
45#include "mem/cache/prefetch/prefetcher.hh"
46
47// forward declarations
48class Bus;
49class ExecContext;
49
50/**
51 * A template-policy based cache. The behavior of the cache can be altered by
52 * supplying different template policies. TagStore handles all tag and data
53 * storage @sa TagStore. Buffering handles all misses and writes/writebacks
54 * @sa MissQueue. Coherence handles all coherence policy details @sa
55 * UniCoherence, SimpleMultiCoherence.
56 */
57template <class TagStore, class Buffering, class Coherence>
58class Cache : public BaseCache
59{
60 public:
61 /** Define the type of cache block to use. */
62 typedef typename TagStore::BlkType BlkType;
63
64 bool prefetchAccess;
65 protected:
66
67 /** Tag and data Storage */
68 TagStore *tags;
69 /** Miss and Writeback handler */
70 Buffering *missQueue;
71 /** Coherence protocol. */
72 Coherence *coherence;
73
74 /** Prefetcher */
75 Prefetcher<TagStore, Buffering> *prefetcher;
76
77 /** Do fast copies in this cache. */
78 bool doCopy;
79
80 /** Block on a delayed copy. */
81 bool blockOnCopy;
82
83 /**
84 * The clock ratio of the outgoing bus.
85 * Used for calculating critical word first.
86 */
87 int busRatio;
88
89 /**
90 * The bus width in bytes of the outgoing bus.
91 * Used for calculating critical word first.
92 */
93 int busWidth;
94
95 /**
96 * A permanent mem req to always be used to cause invalidations.
97 * Used to append to target list, to cause an invalidation.
98 */
99 Packet * invalidatePkt;
100
101 /**
102 * Temporarily move a block into a MSHR.
103 * @todo Remove this when LSQ/SB are fixed and implemented in memtest.
104 */
105 void pseudoFill(Addr addr, int asid);
106
107 /**
108 * Temporarily move a block into an existing MSHR.
109 * @todo Remove this when LSQ/SB are fixed and implemented in memtest.
110 */
111 void pseudoFill(MSHR *mshr);
112
113 public:
114
115 class Params
116 {
117 public:
118 TagStore *tags;
119 Buffering *missQueue;
120 Coherence *coherence;
121 bool doCopy;
122 bool blockOnCopy;
123 BaseCache::Params baseParams;
124 Bus *in;
125 Bus *out;
126 Prefetcher<TagStore, Buffering> *prefetcher;
127 bool prefetchAccess;
128
129 Params(TagStore *_tags, Buffering *mq, Coherence *coh,
130 bool do_copy, BaseCache::Params params, Bus * in_bus,
131 Bus * out_bus, Prefetcher<TagStore, Buffering> *_prefetcher,
132 bool prefetch_access)
133 : tags(_tags), missQueue(mq), coherence(coh), doCopy(do_copy),
134 blockOnCopy(false), baseParams(params), in(in_bus), out(out_bus),
135 prefetcher(_prefetcher), prefetchAccess(prefetch_access)
136 {
137 }
138 };
139
140 /** Instantiates a basic cache object. */
141 Cache(const std::string &_name, HierParams *hier_params, Params &params);
142
143 void regStats();
144
145 /**
146 * Performs the access specified by the request.
147 * @param req The request to perform.
148 * @return The result of the access.
149 */
150 MemAccessResult access(Packet * &pkt);
151
152 /**
153 * Selects a request to send on the bus.
154 * @return The memory request to service.
155 */
156 Packet * getPacket();
157
158 /**
159 * Was the request was sent successfully?
160 * @param req The request.
161 * @param success True if the request was sent successfully.
162 */
163 void sendResult(Packet * &pkt, bool success);
164
165 /**
166 * Handles a response (cache line fill/write ack) from the bus.
167 * @param req The request being responded to.
168 */
169 void handleResponse(Packet * &pkt);
170
171 /**
172 * Start handling a copy transaction.
173 * @param req The copy request to perform.
174 */
175 void startCopy(Packet * &pkt);
176
177 /**
178 * Handle a delayed copy transaction.
179 * @param req The delayed copy request to continue.
180 * @param addr The address being responded to.
181 * @param blk The block of the current response.
182 * @param mshr The mshr being handled.
183 */
184 void handleCopy(Packet * &pkt, Addr addr, BlkType *blk, MSHR *mshr);
185
186 /**
187 * Selects a coherence message to forward to lower levels of the hierarchy.
188 * @return The coherence message to forward.
189 */
190 Packet * getCoherenceReq();
191
192 /**
193 * Snoops bus transactions to maintain coherence.
194 * @param req The current bus transaction.
195 */
196 void snoop(Packet * &pkt);
197
198 void snoopResponse(Packet * &pkt);
199
200 /**
201 * Invalidates the block containing address if found.
202 * @param addr The address to look for.
203 * @param asid The address space ID of the address.
204 * @todo Is this function necessary?
205 */
206 void invalidateBlk(Addr addr, int asid);
207
208 /**
209 * Aquash all requests associated with specified thread.
210 * intended for use by I-cache.
212 * @param thread_number The thread to squash.
211 * @param req->getThreadNum()ber The thread to squash.
212 */
214 void squash(int thread_number)
213 void squash(int threadNum)
214 {
216 missQueue->squash(thread_number);
215 missQueue->squash(threadNum);
216 }
217
218 /**
219 * Return the number of outstanding misses in a Cache.
220 * Default returns 0.
221 *
222 * @retval unsigned The number of missing still outstanding.
223 */
224 unsigned outstandingMisses() const
225 {
226 return missQueue->getMisses();
227 }
228
229 /**
230 * Send a response to the slave interface.
231 * @param req The request being responded to.
232 * @param time The time the response is ready.
233 */
234 void respond(Packet * &pkt, Tick time)
235 {
236 si->respond(pkt,time);
237 }
238
239 /**
240 * Perform the access specified in the request and return the estimated
241 * time of completion. This function can either update the hierarchy state
242 * or just perform the access wherever the data is found depending on the
243 * state of the update flag.
244 * @param req The memory request to satisfy
245 * @param update If true, update the hierarchy, otherwise just perform the
246 * request.
247 * @return The estimated completion time.
248 */
249 Tick probe(Packet * &pkt, bool update);
250
251 /**
252 * Snoop for the provided request in the cache and return the estimated
253 * time of completion.
254 * @todo Can a snoop probe not change state?
255 * @param req The memory request to satisfy
256 * @param update If true, update the hierarchy, otherwise just perform the
257 * request.
258 * @return The estimated completion time.
259 */
260 Tick snoopProbe(Packet * &pkt, bool update);
261};
262
263#endif // __CACHE_HH__