2 * Copyright (c) 2012-2014 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Authors: Erik Hallnor
45 * Definitions of a simple cache block class.
48 #ifndef __CACHE_BLK_HH__
49 #define __CACHE_BLK_HH__
53 #include "base/printable.hh"
54 #include "mem/packet.hh"
55 #include "mem/request.hh"
56 #include "sim/core.hh" // for Tick
59 * Cache block status bit assignments
61 enum CacheBlkStatusBits {
62 /** valid, readable */
64 /** write permission */
66 /** read permission (yes, block can be valid but not readable) */
68 /** dirty (modified) */
70 /** block was referenced */
72 /** block was a hardware prefetch yet unaccessed*/
73 BlkHWPrefetched = 0x20,
74 /** block holds data from the secure memory space */
76 /** can the block transition to E? (hasn't been shared with another cache)
77 * used to close a timing gap when handling WriteInvalidate packets */
78 BlkCanGoExclusive = 0x80
82 * A Basic Cache block.
83 * Contains the tag, status, and a pointer to data.
88 /** Task Id associated with this block */
91 /** The address space ID of this block. */
93 /** Data block tag value. */
96 * Contains a copy of the data in this block for easy access. This is used
97 * for efficient execution when the data could be actually stored in
98 * another format (COW, compressed, sub-blocked, etc). In all cases the
99 * data stored here should be kept consistant with the actual data
100 * referenced by this block.
103 /** the number of bytes stored in this block. */
106 /** block state: OR of CacheBlkStatusBit */
107 typedef unsigned State;
109 /** The current status of this block. @sa CacheBlockStatusBits */
112 /** Which curTick() will this block be accessable */
116 * The set this block belongs to.
117 * @todo Move this into subclasses when we fix CacheTags to use them.
121 /** whether this block has been touched */
124 /** Number of references to this block since it was brought in. */
127 /** holds the source requestor ID for this block. */
134 * Represents that the indicated thread context has a "lock" on
135 * the block, in the LL/SC sense.
139 int contextId; // locking context
140 Addr lowAddr; // low address of lock range
141 Addr highAddr; // high address of lock range
143 // check for matching execution context
144 bool matchesContext(Request *req)
146 Addr req_low = req->getPaddr();
147 Addr req_high = req_low + req->getSize() -1;
148 return (contextId == req->contextId()) &&
149 (req_low >= lowAddr) && (req_high <= highAddr);
152 bool overlapping(Request *req)
154 Addr req_low = req->getPaddr();
155 Addr req_high = req_low + req->getSize() - 1;
157 return (req_low <= highAddr) && (req_high >= lowAddr);
161 : contextId(req->contextId()),
162 lowAddr(req->getPaddr()),
163 highAddr(lowAddr + req->getSize() - 1)
168 /** List of thread contexts that have performed a load-locked (LL)
169 * on the block since the last store. */
170 std::list<Lock> lockList;
175 : task_id(ContextSwitchTaskId::Unknown),
176 asid(-1), tag(0), data(0) ,size(0), status(0), whenReady(0),
177 set(-1), isTouched(false), refCount(0),
178 srcMasterId(Request::invldMasterId),
183 * Copy the state of the given block into this one.
184 * @param rhs The block to copy.
185 * @return a const reference to this block.
187 const CacheBlk& operator=(const CacheBlk& rhs)
194 whenReady = rhs.whenReady;
196 refCount = rhs.refCount;
197 task_id = rhs.task_id;
202 * Checks the write permissions of this block.
203 * @return True if the block is writable.
205 bool isWritable() const
207 const State needed_bits = BlkWritable | BlkValid;
208 return (status & needed_bits) == needed_bits;
212 * Checks the read permissions of this block. Note that a block
213 * can be valid but not readable if there is an outstanding write
215 * @return True if the block is readable.
217 bool isReadable() const
219 const State needed_bits = BlkReadable | BlkValid;
220 return (status & needed_bits) == needed_bits;
224 * Checks that a block is valid.
225 * @return True if the block is valid.
229 return (status & BlkValid) != 0;
233 * Invalidate the block and clear all state.
243 * Check to see if a block has been written.
244 * @return True if the block is dirty.
248 return (status & BlkDirty) != 0;
252 * Check if this block has been referenced.
253 * @return True if the block has been referenced.
255 bool isReferenced() const
257 return (status & BlkReferenced) != 0;
261 * Check if this block was the result of a hardware prefetch, yet to
263 * @return True if the block was a hardware prefetch, unaccesed.
265 bool wasPrefetched() const
267 return (status & BlkHWPrefetched) != 0;
271 * Check if this block holds data from the secure memory space.
272 * @return True if the block holds data from the secure memory space.
274 bool isSecure() const
276 return (status & BlkSecure) != 0;
280 * Track the fact that a local locked was issued to the block. If
281 * multiple LLs get issued from the same context we could have
282 * redundant records on the list, but that's OK, as they'll all
283 * get blown away at the next store.
285 void trackLoadLocked(PacketPtr pkt)
287 assert(pkt->isLLSC());
288 lockList.push_front(Lock(pkt->req));
292 * Clear the list of valid load locks. Should be called whenever
293 * block is written to or invalidated.
295 void clearLoadLocks(Request *req = NULL)
298 // No request, invaldate all locks to this line
301 // Only invalidate locks that overlap with this request
302 std::list<Lock>::iterator lock_itr = lockList.begin();
303 while (lock_itr != lockList.end()) {
304 if (lock_itr->overlapping(req)) {
305 lock_itr = lockList.erase(lock_itr);
314 * Pretty-print a tag, and interpret state bits to readable form
315 * including mapping to a MOESI stat.
317 * @return string with basic state information
319 std::string print() const
327 * state writable dirty valid
334 unsigned state = isWritable() << 2 | isDirty() << 1 | isValid();
337 case 0b111: s = 'M'; break;
338 case 0b011: s = 'O'; break;
339 case 0b101: s = 'E'; break;
340 case 0b001: s = 'S'; break;
341 case 0b000: s = 'I'; break;
342 default: s = 'T'; break; // @TODO add other types
344 return csprintf("state: %x (%c) valid: %d writable: %d readable: %d "
345 "dirty: %d tag: %x", status, s, isValid(),
346 isWritable(), isReadable(), isDirty(), tag);
350 * Handle interaction of load-locked operations and stores.
351 * @return True if write should proceed, false otherwise. Returns
352 * false only in the case of a failed store conditional.
354 bool checkWrite(PacketPtr pkt)
356 Request *req = pkt->req;
358 // it's a store conditional... have to check for matching
360 bool success = false;
362 for (std::list<Lock>::iterator i = lockList.begin();
363 i != lockList.end(); ++i)
365 if (i->matchesContext(req)) {
366 // it's a store conditional, and as far as the memory
367 // system can tell, the requesting context's lock is
374 req->setExtraData(success ? 1 : 0);
378 // for *all* stores (conditional or otherwise) we have to
379 // clear the list of load-locks as they're all invalid now.
387 * Simple class to provide virtual print() method on cache blocks
388 * without allocating a vtable pointer for every single cache block.
389 * Just wrap the CacheBlk object in an instance of this before passing
390 * to a function that requires a Printable object.
392 class CacheBlkPrintWrapper : public Printable
396 CacheBlkPrintWrapper(CacheBlk *_blk) : blk(_blk) {}
397 virtual ~CacheBlkPrintWrapper() {}
398 void print(std::ostream &o, int verbosity = 0,
399 const std::string &prefix = "") const;
403 * Wrap a method and present it as a cache block visitor.
405 * For example the forEachBlk method in the tag arrays expects a
406 * callable object/function as their parameter. This class wraps a
407 * method in an object and presents callable object that adheres to
408 * the cache block visitor protocol.
410 template <typename T, typename BlkType>
411 class CacheBlkVisitorWrapper
414 typedef bool (T::*visitorPtr)(BlkType &blk);
416 CacheBlkVisitorWrapper(T &_obj, visitorPtr _visitor)
417 : obj(_obj), visitor(_visitor) {}
419 bool operator()(BlkType &blk) {
420 return (obj.*visitor)(blk);
429 * Cache block visitor that determines if there are dirty blocks in a
432 * Use with the forEachBlk method in the tag array to determine if the
433 * array contains dirty blocks.
435 template <typename BlkType>
436 class CacheBlkIsDirtyVisitor
439 CacheBlkIsDirtyVisitor()
442 bool operator()(BlkType &blk) {
452 * Does the array contain a dirty line?
454 * \return true if yes, false otherwise.
456 bool isDirty() const { return _isDirty; };
462 #endif //__CACHE_BLK_HH__