cache.hh revision 5314
12155SN/A/*
22155SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32155SN/A * All rights reserved.
42155SN/A *
52155SN/A * Redistribution and use in source and binary forms, with or without
62155SN/A * modification, are permitted provided that the following conditions are
72155SN/A * met: redistributions of source code must retain the above copyright
82155SN/A * notice, this list of conditions and the following disclaimer;
92155SN/A * redistributions in binary form must reproduce the above copyright
102155SN/A * notice, this list of conditions and the following disclaimer in the
112155SN/A * documentation and/or other materials provided with the distribution;
122155SN/A * neither the name of the copyright holders nor the names of its
132155SN/A * contributors may be used to endorse or promote products derived from
142155SN/A * this software without specific prior written permission.
152155SN/A *
162155SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172155SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182155SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192155SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202155SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212155SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222155SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232155SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242155SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252155SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262155SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272155SN/A *
282665Ssaidi@eecs.umich.edu * Authors: Erik Hallnor
292665Ssaidi@eecs.umich.edu *          Dave Greene
302155SN/A *          Steve Reinhardt
314202Sbinkertn@umich.edu *          Ron Dreslinski
322155SN/A */
339850Sandreas.hansson@arm.com
349850Sandreas.hansson@arm.com/**
359850Sandreas.hansson@arm.com * @file
367768SAli.Saidi@ARM.com * Describes a cache based on template policies.
377768SAli.Saidi@ARM.com */
388887Sgeoffrey.blake@arm.com
392766Sktlim@umich.edu#ifndef __CACHE_HH__
404486Sbinkertn@umich.edu#define __CACHE_HH__
414486Sbinkertn@umich.edu
424776Sgblack@eecs.umich.edu#include "base/misc.hh" // fatal, panic, and warn
434776Sgblack@eecs.umich.edu
448739Sgblack@eecs.umich.edu#include "mem/cache/base_cache.hh"
456365Sgblack@eecs.umich.edu#include "mem/cache/cache_blk.hh"
4610259SAndrew.Bardsley@arm.com#include "mem/cache/miss/mshr.hh"
474486Sbinkertn@umich.edu
484202Sbinkertn@umich.edu#include "sim/eventq.hh"
494202Sbinkertn@umich.edu
504202Sbinkertn@umich.edu//Forward decleration
514202Sbinkertn@umich.educlass BasePrefetcher;
5210319SAndreas.Sandberg@ARM.com
534202Sbinkertn@umich.edu/**
544776Sgblack@eecs.umich.edu * A template-policy based cache. The behavior of the cache can be altered by
558739Sgblack@eecs.umich.edu * supplying different template policies. TagStore handles all tag and data
566365Sgblack@eecs.umich.edu * storage @sa TagStore.
574202Sbinkertn@umich.edu */
588777Sgblack@eecs.umich.edutemplate <class TagStore>
594202Sbinkertn@umich.educlass Cache : public BaseCache
609913Ssteve.reinhardt@amd.com{
614202Sbinkertn@umich.edu  public:
624202Sbinkertn@umich.edu    /** Define the type of cache block to use. */
635217Ssaidi@eecs.umich.edu    typedef typename TagStore::BlkType BlkType;
644202Sbinkertn@umich.edu    /** A typedef for a list of BlkType pointers. */
6510259SAndrew.Bardsley@arm.com    typedef typename TagStore::BlkList BlkList;
662155SN/A
678793Sgblack@eecs.umich.edu    bool prefetchAccess;
688793Sgblack@eecs.umich.edu
698793Sgblack@eecs.umich.edu  protected:
704776Sgblack@eecs.umich.edu
718887Sgeoffrey.blake@arm.com    class CpuSidePort : public CachePort
7210201SAndrew.Bardsley@arm.com    {
738887Sgeoffrey.blake@arm.com      public:
749340SAndreas.Sandberg@arm.com        CpuSidePort(const std::string &_name,
758887Sgeoffrey.blake@arm.com                    Cache<TagStore> *_cache,
765192Ssaidi@eecs.umich.edu                    const std::string &_label,
778335Snate@binkert.org                    std::vector<Range<Addr> > filterRanges);
788335Snate@binkert.org
798335Snate@binkert.org        // BaseCache::CachePort just has a BaseCache *; this function
808335Snate@binkert.org        // lets us get back the type info we lost when we stored the
818335Snate@binkert.org        // cache pointer there.
829534SAndreas.Sandberg@ARM.com        Cache<TagStore> *myCache() {
839534SAndreas.Sandberg@ARM.com            return static_cast<Cache<TagStore> *>(cache);
849534SAndreas.Sandberg@ARM.com        }
858335Snate@binkert.org
869534SAndreas.Sandberg@ARM.com        virtual void getDeviceAddressRanges(AddrRangeList &resp,
879534SAndreas.Sandberg@ARM.com                                            bool &snoop);
888335Snate@binkert.org
899534SAndreas.Sandberg@ARM.com        virtual bool recvTiming(PacketPtr pkt);
909534SAndreas.Sandberg@ARM.com
919534SAndreas.Sandberg@ARM.com        virtual Tick recvAtomic(PacketPtr pkt);
929534SAndreas.Sandberg@ARM.com
939534SAndreas.Sandberg@ARM.com        virtual void recvFunctional(PacketPtr pkt);
949534SAndreas.Sandberg@ARM.com    };
959534SAndreas.Sandberg@ARM.com
969534SAndreas.Sandberg@ARM.com    class MemSidePort : public CachePort
979534SAndreas.Sandberg@ARM.com    {
989534SAndreas.Sandberg@ARM.com      public:
9910383Smitch.hayenga@arm.com        MemSidePort(const std::string &_name,
1008335Snate@binkert.org                    Cache<TagStore> *_cache,
1018335Snate@binkert.org                    const std::string &_label,
1028471SGiacomo.Gabrielli@arm.com                    std::vector<Range<Addr> > filterRanges);
1038335Snate@binkert.org
1048335Snate@binkert.org        // BaseCache::CachePort just has a BaseCache *; this function
1055192Ssaidi@eecs.umich.edu        // lets us get back the type info we lost when we stored the
1068232Snate@binkert.org        // cache pointer there.
1078232Snate@binkert.org        Cache<TagStore> *myCache() {
1088232Snate@binkert.org            return static_cast<Cache<TagStore> *>(cache);
1098300Schander.sudanthi@arm.com        }
11010383Smitch.hayenga@arm.com
1115192Ssaidi@eecs.umich.edu        void sendPacket();
1128300Schander.sudanthi@arm.com
1138300Schander.sudanthi@arm.com        void processSendEvent();
1146036Sksewell@umich.edu
1158300Schander.sudanthi@arm.com        virtual void getDeviceAddressRanges(AddrRangeList &resp,
1168300Schander.sudanthi@arm.com                                            bool &snoop);
117
118        virtual bool recvTiming(PacketPtr pkt);
119
120        virtual void recvRetry();
121
122        virtual Tick recvAtomic(PacketPtr pkt);
123
124        virtual void recvFunctional(PacketPtr pkt);
125
126        typedef EventWrapper<MemSidePort, &MemSidePort::processSendEvent>
127                SendEvent;
128    };
129
130    /** Tag and data Storage */
131    TagStore *tags;
132
133    /** Prefetcher */
134    BasePrefetcher *prefetcher;
135
136    /** Temporary cache block for occasional transitory use */
137    BlkType *tempBlock;
138
139    /**
140     * Can this cache should allocate a block on a line-sized write miss.
141     */
142    const bool doFastWrites;
143
144    const bool prefetchMiss;
145
146    /**
147     * Handle a replacement for the given request.
148     * @param blk A pointer to the block, usually NULL
149     * @param pkt The memory request to satisfy.
150     * @param new_state The new state of the block.
151     * @param writebacks A list to store any generated writebacks.
152     */
153    BlkType* doReplacement(BlkType *blk, PacketPtr pkt,
154                           CacheBlk::State new_state, PacketList &writebacks);
155
156    /**
157     * Does all the processing necessary to perform the provided request.
158     * @param pkt The memory request to perform.
159     * @param lat The latency of the access.
160     * @param writebacks List for any writebacks that need to be performed.
161     * @param update True if the replacement data should be updated.
162     * @return Pointer to the cache block touched by the request. NULL if it
163     * was a miss.
164     */
165    bool access(PacketPtr pkt, BlkType *&blk, int &lat);
166
167    /**
168     *Handle doing the Compare and Swap function for SPARC.
169     */
170    void cmpAndSwap(BlkType *blk, PacketPtr pkt);
171
172    /**
173     * Populates a cache block and handles all outstanding requests for the
174     * satisfied fill request. This version takes two memory requests. One
175     * contains the fill data, the other is an optional target to satisfy.
176     * Used for Cache::probe.
177     * @param pkt The memory request with the fill data.
178     * @param blk The cache block if it already exists.
179     * @param writebacks List for any writebacks that need to be performed.
180     * @return Pointer to the new cache block.
181     */
182    BlkType *handleFill(PacketPtr pkt, BlkType *blk,
183                        PacketList &writebacks);
184
185    void satisfyCpuSideRequest(PacketPtr pkt, BlkType *blk);
186    bool satisfyMSHR(MSHR *mshr, PacketPtr pkt, BlkType *blk);
187
188    void doTimingSupplyResponse(PacketPtr req_pkt, uint8_t *blk_data,
189                                bool already_copied);
190
191    /**
192     * Sets the blk to the new state.
193     * @param blk The cache block being snooped.
194     * @param new_state The new coherence state for the block.
195     */
196    void handleSnoop(PacketPtr ptk, BlkType *blk,
197                     bool is_timing, bool is_deferred);
198
199    /**
200     * Create a writeback request for the given block.
201     * @param blk The block to writeback.
202     * @return The writeback request for the block.
203     */
204    PacketPtr writebackBlk(BlkType *blk);
205
206  public:
207    /** Instantiates a basic cache object. */
208    Cache(const Params *p, TagStore *tags, BasePrefetcher *prefetcher);
209
210    virtual Port *getPort(const std::string &if_name, int idx = -1);
211    virtual void deletePortRefs(Port *p);
212
213    void regStats();
214
215    /**
216     * Performs the access specified by the request.
217     * @param pkt The request to perform.
218     * @return The result of the access.
219     */
220    bool timingAccess(PacketPtr pkt);
221
222    /**
223     * Performs the access specified by the request.
224     * @param pkt The request to perform.
225     * @return The result of the access.
226     */
227    Tick atomicAccess(PacketPtr pkt);
228
229    /**
230     * Performs the access specified by the request.
231     * @param pkt The request to perform.
232     * @return The result of the access.
233     */
234    void functionalAccess(PacketPtr pkt, CachePort *incomingPort,
235                          CachePort *otherSidePort);
236
237    /**
238     * Handles a response (cache line fill/write ack) from the bus.
239     * @param pkt The request being responded to.
240     */
241    void handleResponse(PacketPtr pkt);
242
243    /**
244     * Snoops bus transactions to maintain coherence.
245     * @param pkt The current bus transaction.
246     */
247    void snoopTiming(PacketPtr pkt);
248
249    /**
250     * Snoop for the provided request in the cache and return the estimated
251     * time of completion.
252     * @param pkt The memory request to snoop
253     * @return The estimated completion time.
254     */
255    Tick snoopAtomic(PacketPtr pkt);
256
257    /**
258     * Squash all requests associated with specified thread.
259     * intended for use by I-cache.
260     * @param threadNum The thread to squash.
261     */
262    void squash(int threadNum);
263
264    /**
265     * Selects a outstanding request to service.
266     * @return The request to service, NULL if none found.
267     */
268    PacketPtr getBusPacket(PacketPtr cpu_pkt, BlkType *blk,
269                           bool needsExclusive);
270    MSHR *getNextMSHR();
271    PacketPtr getTimingPacket();
272
273    /**
274     * Marks a request as in service (sent on the bus). This can have side
275     * effect since storage for no response commands is deallocated once they
276     * are successfully sent.
277     * @param pkt The request that was sent on the bus.
278     */
279    void markInService(MSHR *mshr);
280
281    /**
282     * Perform the given writeback request.
283     * @param pkt The writeback request.
284     */
285    void doWriteback(PacketPtr pkt);
286
287    /**
288     * Return whether there are any outstanding misses.
289     */
290    bool outstandingMisses() const
291    {
292        return mshrQueue.allocated != 0;
293    }
294
295    CacheBlk *findBlock(Addr addr) {
296        return tags->findBlock(addr);
297    }
298
299    bool inCache(Addr addr) {
300        return (tags->findBlock(addr) != 0);
301    }
302
303    bool inMissQueue(Addr addr) {
304        return (mshrQueue.findMatch(addr) != 0);
305    }
306};
307
308#endif // __CACHE_HH__
309