2 * Copyright (c) 2012-2015 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 __MEM_CACHE_BLK_HH__
49 #define __MEM_CACHE_BLK_HH__
53 #include "base/printable.hh"
54 #include "mem/packet.hh"
55 #include "mem/request.hh"
58 * Cache block status bit assignments
60 enum CacheBlkStatusBits : unsigned {
61 /** valid, readable */
63 /** write permission */
65 /** read permission (yes, block can be valid but not readable) */
67 /** dirty (modified) */
69 /** block was a hardware prefetch yet unaccessed*/
70 BlkHWPrefetched = 0x20,
71 /** block holds data from the secure memory space */
76 * A Basic Cache block.
77 * Contains the tag, status, and a pointer to data.
82 /** Task Id associated with this block */
85 /** The address space ID of this block. */
87 /** Data block tag value. */
90 * Contains a copy of the data in this block for easy access. This is used
91 * for efficient execution when the data could be actually stored in
92 * another format (COW, compressed, sub-blocked, etc). In all cases the
93 * data stored here should be kept consistant with the actual data
94 * referenced by this block.
97 /** the number of bytes stored in this block. */
100 /** block state: OR of CacheBlkStatusBit */
101 typedef unsigned State;
103 /** The current status of this block. @sa CacheBlockStatusBits */
106 /** Which curTick() will this block be accessable */
110 * The set and way this block belongs to.
111 * @todo Move this into subclasses when we fix CacheTags to use them.
115 /** whether this block has been touched */
118 /** Number of references to this block since it was brought in. */
121 /** holds the source requestor ID for this block. */
128 * Represents that the indicated thread context has a "lock" on
129 * the block, in the LL/SC sense.
133 ContextID contextId; // locking context
134 Addr lowAddr; // low address of lock range
135 Addr highAddr; // high address of lock range
137 // check for matching execution context
138 bool matchesContext(const RequestPtr req) const
140 Addr req_low = req->getPaddr();
141 Addr req_high = req_low + req->getSize() -1;
142 return (contextId == req->contextId()) &&
143 (req_low >= lowAddr) && (req_high <= highAddr);
146 bool overlapping(const RequestPtr req) const
148 Addr req_low = req->getPaddr();
149 Addr req_high = req_low + req->getSize() - 1;
151 return (req_low <= highAddr) && (req_high >= lowAddr);
154 Lock(const RequestPtr req)
155 : contextId(req->contextId()),
156 lowAddr(req->getPaddr()),
157 highAddr(lowAddr + req->getSize() - 1)
162 /** List of thread contexts that have performed a load-locked (LL)
163 * on the block since the last store. */
164 std::list<Lock> lockList;
169 : task_id(ContextSwitchTaskId::Unknown),
170 asid(-1), tag(0), data(0) ,size(0), status(0), whenReady(0),
171 set(-1), way(-1), isTouched(false), refCount(0),
172 srcMasterId(Request::invldMasterId),
176 CacheBlk(const CacheBlk&) = delete;
177 CacheBlk& operator=(const CacheBlk&) = delete;
180 * Checks the write permissions of this block.
181 * @return True if the block is writable.
183 bool isWritable() const
185 const State needed_bits = BlkWritable | BlkValid;
186 return (status & needed_bits) == needed_bits;
190 * Checks the read permissions of this block. Note that a block
191 * can be valid but not readable if there is an outstanding write
193 * @return True if the block is readable.
195 bool isReadable() const
197 const State needed_bits = BlkReadable | BlkValid;
198 return (status & needed_bits) == needed_bits;
202 * Checks that a block is valid.
203 * @return True if the block is valid.
207 return (status & BlkValid) != 0;
211 * Invalidate the block and clear all state.
221 * Check to see if a block has been written.
222 * @return True if the block is dirty.
226 return (status & BlkDirty) != 0;
230 * Check if this block was the result of a hardware prefetch, yet to
232 * @return True if the block was a hardware prefetch, unaccesed.
234 bool wasPrefetched() const
236 return (status & BlkHWPrefetched) != 0;
240 * Check if this block holds data from the secure memory space.
241 * @return True if the block holds data from the secure memory space.
243 bool isSecure() const
245 return (status & BlkSecure) != 0;
249 * Track the fact that a local locked was issued to the block. If
250 * multiple LLs get issued from the same context we could have
251 * redundant records on the list, but that's OK, as they'll all
252 * get blown away at the next store.
254 void trackLoadLocked(PacketPtr pkt)
256 assert(pkt->isLLSC());
257 lockList.emplace_front(pkt->req);
261 * Clear the list of valid load locks. Should be called whenever
262 * block is written to or invalidated.
264 void clearLoadLocks(RequestPtr req = nullptr)
267 // No request, invaldate all locks to this line
270 // Only invalidate locks that overlap with this request
271 auto lock_itr = lockList.begin();
272 while (lock_itr != lockList.end()) {
273 if (lock_itr->overlapping(req)) {
274 lock_itr = lockList.erase(lock_itr);
283 * Pretty-print a tag, and interpret state bits to readable form
284 * including mapping to a MOESI stat.
286 * @return string with basic state information
288 std::string print() const
296 * state writable dirty valid
303 unsigned state = isWritable() << 2 | isDirty() << 1 | isValid();
306 case 0b111: s = 'M'; break;
307 case 0b011: s = 'O'; break;
308 case 0b101: s = 'E'; break;
309 case 0b001: s = 'S'; break;
310 case 0b000: s = 'I'; break;
311 default: s = 'T'; break; // @TODO add other types
313 return csprintf("state: %x (%c) valid: %d writable: %d readable: %d "
314 "dirty: %d tag: %x", status, s, isValid(),
315 isWritable(), isReadable(), isDirty(), tag);
319 * Handle interaction of load-locked operations and stores.
320 * @return True if write should proceed, false otherwise. Returns
321 * false only in the case of a failed store conditional.
323 bool checkWrite(PacketPtr pkt)
326 if (!pkt->isLLSC() && lockList.empty())
329 RequestPtr req = pkt->req;
332 // it's a store conditional... have to check for matching
334 bool success = false;
336 for (const auto& l : lockList) {
337 if (l.matchesContext(req)) {
338 // it's a store conditional, and as far as the memory
339 // system can tell, the requesting context's lock is
346 req->setExtraData(success ? 1 : 0);
350 // for *all* stores (conditional or otherwise) we have to
351 // clear the list of load-locks as they're all invalid now.
359 * Simple class to provide virtual print() method on cache blocks
360 * without allocating a vtable pointer for every single cache block.
361 * Just wrap the CacheBlk object in an instance of this before passing
362 * to a function that requires a Printable object.
364 class CacheBlkPrintWrapper : public Printable
368 CacheBlkPrintWrapper(CacheBlk *_blk) : blk(_blk) {}
369 virtual ~CacheBlkPrintWrapper() {}
370 void print(std::ostream &o, int verbosity = 0,
371 const std::string &prefix = "") const;
375 * Base class for cache block visitor, operating on the cache block
376 * base class (later subclassed for the various tag classes). This
377 * visitor class is used as part of the forEachBlk interface in the
380 class CacheBlkVisitor
385 virtual ~CacheBlkVisitor() {}
387 virtual bool operator()(CacheBlk &blk) = 0;
390 #endif //__MEM_CACHE_BLK_HH__