base.hh revision 11870
112070Snikos.nikoleris@arm.com/*
29380SAndreas.Sandberg@ARM.com * Copyright (c) 2012-2014,2016 ARM Limited
39380SAndreas.Sandberg@ARM.com * All rights reserved.
49380SAndreas.Sandberg@ARM.com *
59380SAndreas.Sandberg@ARM.com * The license below extends only to copyright in the software and shall
69380SAndreas.Sandberg@ARM.com * not be construed as granting a license to any other intellectual
79380SAndreas.Sandberg@ARM.com * property including but not limited to intellectual property relating
89380SAndreas.Sandberg@ARM.com * to a hardware implementation of the functionality of the software
99380SAndreas.Sandberg@ARM.com * licensed hereunder.  You may use the software subject to the license
109380SAndreas.Sandberg@ARM.com * terms below provided that you ensure that this notice is replicated
119380SAndreas.Sandberg@ARM.com * unmodified and in its entirety in all distributions of the software,
129380SAndreas.Sandberg@ARM.com * modified or unmodified, in source code or in binary form.
139380SAndreas.Sandberg@ARM.com *
149380SAndreas.Sandberg@ARM.com * Copyright (c) 2003-2005 The Regents of The University of Michigan
159380SAndreas.Sandberg@ARM.com * All rights reserved.
169380SAndreas.Sandberg@ARM.com *
179380SAndreas.Sandberg@ARM.com * Redistribution and use in source and binary forms, with or without
189380SAndreas.Sandberg@ARM.com * modification, are permitted provided that the following conditions are
199380SAndreas.Sandberg@ARM.com * met: redistributions of source code must retain the above copyright
209380SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer;
219380SAndreas.Sandberg@ARM.com * redistributions in binary form must reproduce the above copyright
229380SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer in the
239380SAndreas.Sandberg@ARM.com * documentation and/or other materials provided with the distribution;
249380SAndreas.Sandberg@ARM.com * neither the name of the copyright holders nor the names of its
259380SAndreas.Sandberg@ARM.com * contributors may be used to endorse or promote products derived from
269380SAndreas.Sandberg@ARM.com * this software without specific prior written permission.
279380SAndreas.Sandberg@ARM.com *
289380SAndreas.Sandberg@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
299380SAndreas.Sandberg@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
309380SAndreas.Sandberg@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
319380SAndreas.Sandberg@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
329380SAndreas.Sandberg@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
339380SAndreas.Sandberg@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
349380SAndreas.Sandberg@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
359380SAndreas.Sandberg@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
369380SAndreas.Sandberg@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
379380SAndreas.Sandberg@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
389380SAndreas.Sandberg@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
399380SAndreas.Sandberg@ARM.com *
409380SAndreas.Sandberg@ARM.com * Authors: Erik Hallnor
419380SAndreas.Sandberg@ARM.com *          Ron Dreslinski
4211682Sandreas.hansson@arm.com */
4311682Sandreas.hansson@arm.com
4411682Sandreas.hansson@arm.com/**
459380SAndreas.Sandberg@ARM.com * @file
4611682Sandreas.hansson@arm.com * Declaration of a common base class for cache tagstore objects.
4711682Sandreas.hansson@arm.com */
4810406Sandreas.hansson@arm.com
4910406Sandreas.hansson@arm.com#ifndef __BASE_TAGS_HH__
5010406Sandreas.hansson@arm.com#define __BASE_TAGS_HH__
5110406Sandreas.hansson@arm.com
5210406Sandreas.hansson@arm.com#include <string>
5310406Sandreas.hansson@arm.com
5410406Sandreas.hansson@arm.com#include "base/callback.hh"
5510406Sandreas.hansson@arm.com#include "base/statistics.hh"
5610406Sandreas.hansson@arm.com#include "mem/cache/blk.hh"
5710406Sandreas.hansson@arm.com#include "params/BaseTags.hh"
5810406Sandreas.hansson@arm.com#include "sim/clocked_object.hh"
5910406Sandreas.hansson@arm.com
6010406Sandreas.hansson@arm.comclass BaseCache;
6110406Sandreas.hansson@arm.com
6210406Sandreas.hansson@arm.com/**
6310406Sandreas.hansson@arm.com * A common base class of Cache tagstore objects.
6410406Sandreas.hansson@arm.com */
6510406Sandreas.hansson@arm.comclass BaseTags : public ClockedObject
669380SAndreas.Sandberg@ARM.com{
679380SAndreas.Sandberg@ARM.com  protected:
689380SAndreas.Sandberg@ARM.com    /** The block size of the cache. */
699380SAndreas.Sandberg@ARM.com    const unsigned blkSize;
709380SAndreas.Sandberg@ARM.com    /** The size of the cache. */
719380SAndreas.Sandberg@ARM.com    const unsigned size;
729380SAndreas.Sandberg@ARM.com    /** The tag lookup latency of the cache. */
739380SAndreas.Sandberg@ARM.com    const Cycles lookupLatency;
7410512SAli.Saidi@ARM.com    /**
759380SAndreas.Sandberg@ARM.com     * The total access latency of the cache. This latency
769380SAndreas.Sandberg@ARM.com     * is different depending on the cache access mode
779380SAndreas.Sandberg@ARM.com     * (parallel or sequential)
7810512SAli.Saidi@ARM.com     */
7912070Snikos.nikoleris@arm.com    const Cycles accessLatency;
809380SAndreas.Sandberg@ARM.com    /** Pointer to the parent cache. */
819380SAndreas.Sandberg@ARM.com    BaseCache *cache;
8210512SAli.Saidi@ARM.com
8310512SAli.Saidi@ARM.com    /**
8412070Snikos.nikoleris@arm.com     * The number of tags that need to be touched to meet the warmup
859380SAndreas.Sandberg@ARM.com     * percentage.
869380SAndreas.Sandberg@ARM.com     */
8710512SAli.Saidi@ARM.com    int warmupBound;
889380SAndreas.Sandberg@ARM.com    /** Marked true when the cache is warmed up. */
8910512SAli.Saidi@ARM.com    bool warmedUp;
9012070Snikos.nikoleris@arm.com
919649SAndreas.Sandberg@ARM.com    /** the number of blocks in the cache */
929649SAndreas.Sandberg@ARM.com    unsigned numBlocks;
939649SAndreas.Sandberg@ARM.com
949649SAndreas.Sandberg@ARM.com    // Statistics
959649SAndreas.Sandberg@ARM.com    /**
969649SAndreas.Sandberg@ARM.com     * @addtogroup CacheStatistics
979649SAndreas.Sandberg@ARM.com     * @{
989380SAndreas.Sandberg@ARM.com     */
999380SAndreas.Sandberg@ARM.com
1009380SAndreas.Sandberg@ARM.com    /** Number of replacements of valid blocks per thread. */
1019380SAndreas.Sandberg@ARM.com    Stats::Vector replacements;
1029380SAndreas.Sandberg@ARM.com    /** Per cycle average of the number of tags that hold valid data. */
1039380SAndreas.Sandberg@ARM.com    Stats::Average tagsInUse;
1049380SAndreas.Sandberg@ARM.com
10510512SAli.Saidi@ARM.com    /** The total number of references to a block before it is replaced. */
1069380SAndreas.Sandberg@ARM.com    Stats::Scalar totalRefs;
1079380SAndreas.Sandberg@ARM.com
1089380SAndreas.Sandberg@ARM.com    /**
1099380SAndreas.Sandberg@ARM.com     * The number of reference counts sampled. This is different from
1109380SAndreas.Sandberg@ARM.com     * replacements because we sample all the valid blocks when the simulator
1119380SAndreas.Sandberg@ARM.com     * exits.
1129380SAndreas.Sandberg@ARM.com     */
1139380SAndreas.Sandberg@ARM.com    Stats::Scalar sampledRefs;
1149380SAndreas.Sandberg@ARM.com
11510512SAli.Saidi@ARM.com    /**
1169380SAndreas.Sandberg@ARM.com     * Average number of references to a block before is was replaced.
11710406Sandreas.hansson@arm.com     * @todo This should change to an average stat once we have them.
11810406Sandreas.hansson@arm.com     */
11910406Sandreas.hansson@arm.com    Stats::Formula avgRefs;
12010406Sandreas.hansson@arm.com
12110406Sandreas.hansson@arm.com    /** The cycle that the warmup percentage was hit. */
12210406Sandreas.hansson@arm.com    Stats::Scalar warmupCycle;
1239380SAndreas.Sandberg@ARM.com
1249380SAndreas.Sandberg@ARM.com    /** Average occupancy of each requestor using the cache */
1259380SAndreas.Sandberg@ARM.com    Stats::AverageVector occupancies;
1269380SAndreas.Sandberg@ARM.com
1279380SAndreas.Sandberg@ARM.com    /** Average occ % of each requestor using the cache */
1289380SAndreas.Sandberg@ARM.com    Stats::Formula avgOccs;
1299380SAndreas.Sandberg@ARM.com
1309380SAndreas.Sandberg@ARM.com    /** Occupancy of each context/cpu using the cache */
1319380SAndreas.Sandberg@ARM.com    Stats::Vector occupanciesTaskId;
13210512SAli.Saidi@ARM.com
1339380SAndreas.Sandberg@ARM.com    /** Occupancy of each context/cpu using the cache */
13410512SAli.Saidi@ARM.com    Stats::Vector2d ageTaskId;
1359447SAndreas.Sandberg@ARM.com
1369447SAndreas.Sandberg@ARM.com    /** Occ % of each context/cpu using the cache */
1379447SAndreas.Sandberg@ARM.com    Stats::Formula percentOccsTaskId;
1389447SAndreas.Sandberg@ARM.com
13910512SAli.Saidi@ARM.com    /** Number of tags consulted over all accesses. */
1409447SAndreas.Sandberg@ARM.com    Stats::Scalar tagAccesses;
14110512SAli.Saidi@ARM.com    /** Number of data blocks consulted over all accesses. */
142    Stats::Scalar dataAccesses;
143
144    /**
145     * @}
146     */
147
148  public:
149    typedef BaseTagsParams Params;
150    BaseTags(const Params *p);
151
152    /**
153     * Destructor.
154     */
155    virtual ~BaseTags() {}
156
157    /**
158     * Set the parent cache back pointer.
159     * @param _cache Pointer to parent cache.
160     */
161    void setCache(BaseCache *_cache);
162
163    /**
164     * Register local statistics.
165     */
166    void regStats();
167
168    /**
169     * Average in the reference count for valid blocks when the simulation
170     * exits.
171     */
172    virtual void cleanupRefs() {}
173
174    /**
175     * Computes stats just prior to dump event
176     */
177    virtual void computeStats() {}
178
179    /**
180     * Print all tags used
181     */
182    virtual std::string print() const = 0;
183
184    /**
185     * Find a block using the memory address
186     */
187    virtual CacheBlk * findBlock(Addr addr, bool is_secure) const = 0;
188
189    /**
190     * Calculate the block offset of an address.
191     * @param addr the address to get the offset of.
192     * @return the block offset.
193     */
194    int extractBlkOffset(Addr addr) const
195    {
196        return (addr & (Addr)(blkSize-1));
197    }
198
199    /**
200     * Find the cache block given set and way
201     * @param set The set of the block.
202     * @param way The way of the block.
203     * @return The cache block.
204     */
205    virtual CacheBlk *findBlockBySetAndWay(int set, int way) const = 0;
206
207    /**
208     * Limit the allocation for the cache ways.
209     * @param ways The maximum number of ways available for replacement.
210     */
211    virtual void setWayAllocationMax(int ways)
212    {
213        panic("This tag class does not implement way allocation limit!\n");
214    }
215
216    /**
217     * Get the way allocation mask limit.
218     * @return The maximum number of ways available for replacement.
219     */
220    virtual int getWayAllocationMax() const
221    {
222        panic("This tag class does not implement way allocation limit!\n");
223        return -1;
224    }
225
226    virtual void invalidate(CacheBlk *blk) = 0;
227
228    virtual CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) = 0;
229
230    virtual Addr extractTag(Addr addr) const = 0;
231
232    virtual void insertBlock(PacketPtr pkt, CacheBlk *blk) = 0;
233
234    virtual Addr regenerateBlkAddr(Addr tag, unsigned set) const = 0;
235
236    virtual CacheBlk* findVictim(Addr addr) = 0;
237
238    virtual int extractSet(Addr addr) const = 0;
239
240    virtual void forEachBlk(CacheBlkVisitor &visitor) = 0;
241};
242
243class BaseTagsCallback : public Callback
244{
245    BaseTags *tags;
246  public:
247    BaseTagsCallback(BaseTags *t) : tags(t) {}
248    virtual void process() { tags->cleanupRefs(); };
249};
250
251class BaseTagsDumpCallback : public Callback
252{
253    BaseTags *tags;
254  public:
255    BaseTagsDumpCallback(BaseTags *t) : tags(t) {}
256    virtual void process() { tags->computeStats(); };
257};
258
259#endif //__BASE_TAGS_HH__
260