113223Sodanrc@yahoo.com.br/*
213223Sodanrc@yahoo.com.br * Copyright (c) 2012-2018 ARM Limited
313223Sodanrc@yahoo.com.br * All rights reserved.
413223Sodanrc@yahoo.com.br *
513223Sodanrc@yahoo.com.br * The license below extends only to copyright in the software and shall
613223Sodanrc@yahoo.com.br * not be construed as granting a license to any other intellectual
713223Sodanrc@yahoo.com.br * property including but not limited to intellectual property relating
813223Sodanrc@yahoo.com.br * to a hardware implementation of the functionality of the software
913223Sodanrc@yahoo.com.br * licensed hereunder.  You may use the software subject to the license
1013223Sodanrc@yahoo.com.br * terms below provided that you ensure that this notice is replicated
1113223Sodanrc@yahoo.com.br * unmodified and in its entirety in all distributions of the software,
1213223Sodanrc@yahoo.com.br * modified or unmodified, in source code or in binary form.
1313223Sodanrc@yahoo.com.br *
1413223Sodanrc@yahoo.com.br * Copyright (c) 2003-2005 The Regents of The University of Michigan
1513223Sodanrc@yahoo.com.br * All rights reserved.
1613223Sodanrc@yahoo.com.br *
1713223Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without
1813223Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are
1913223Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright
2013223Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer;
2113223Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright
2213223Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the
2313223Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution;
2413223Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its
2513223Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from
2613223Sodanrc@yahoo.com.br * this software without specific prior written permission.
2713223Sodanrc@yahoo.com.br *
2813223Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2913223Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3013223Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3113223Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3213223Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3313223Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3413223Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3513223Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3613223Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3713223Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3813223Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3913223Sodanrc@yahoo.com.br *
4013223Sodanrc@yahoo.com.br * Authors: Erik Hallnor
4113223Sodanrc@yahoo.com.br *          Andreas Sandberg
4213223Sodanrc@yahoo.com.br */
4313223Sodanrc@yahoo.com.br
4413223Sodanrc@yahoo.com.br/** @file
4513223Sodanrc@yahoo.com.br * Definitions of a simple cache block class.
4613223Sodanrc@yahoo.com.br */
4713223Sodanrc@yahoo.com.br
4813223Sodanrc@yahoo.com.br#ifndef __MEM_CACHE_CACHE_BLK_HH__
4913223Sodanrc@yahoo.com.br#define __MEM_CACHE_CACHE_BLK_HH__
5013223Sodanrc@yahoo.com.br
5113223Sodanrc@yahoo.com.br#include <cassert>
5213223Sodanrc@yahoo.com.br#include <cstdint>
5313223Sodanrc@yahoo.com.br#include <iosfwd>
5413223Sodanrc@yahoo.com.br#include <list>
5513223Sodanrc@yahoo.com.br#include <string>
5613223Sodanrc@yahoo.com.br
5713223Sodanrc@yahoo.com.br#include "base/printable.hh"
5813223Sodanrc@yahoo.com.br#include "base/types.hh"
5913223Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/base.hh"
6013223Sodanrc@yahoo.com.br#include "mem/packet.hh"
6113223Sodanrc@yahoo.com.br#include "mem/request.hh"
6213223Sodanrc@yahoo.com.br
6313223Sodanrc@yahoo.com.br/**
6413223Sodanrc@yahoo.com.br * Cache block status bit assignments
6513223Sodanrc@yahoo.com.br */
6613223Sodanrc@yahoo.com.brenum CacheBlkStatusBits : unsigned {
6713223Sodanrc@yahoo.com.br    /** valid, readable */
6813223Sodanrc@yahoo.com.br    BlkValid =          0x01,
6913223Sodanrc@yahoo.com.br    /** write permission */
7013223Sodanrc@yahoo.com.br    BlkWritable =       0x02,
7113223Sodanrc@yahoo.com.br    /** read permission (yes, block can be valid but not readable) */
7213223Sodanrc@yahoo.com.br    BlkReadable =       0x04,
7313223Sodanrc@yahoo.com.br    /** dirty (modified) */
7413223Sodanrc@yahoo.com.br    BlkDirty =          0x08,
7513223Sodanrc@yahoo.com.br    /** block was a hardware prefetch yet unaccessed*/
7613223Sodanrc@yahoo.com.br    BlkHWPrefetched =   0x20,
7713223Sodanrc@yahoo.com.br    /** block holds data from the secure memory space */
7813223Sodanrc@yahoo.com.br    BlkSecure =         0x40,
7913940Sodanrc@yahoo.com.br    /** block holds compressed data */
8013940Sodanrc@yahoo.com.br    BlkCompressed =     0x80
8113223Sodanrc@yahoo.com.br};
8213223Sodanrc@yahoo.com.br
8313223Sodanrc@yahoo.com.br/**
8413223Sodanrc@yahoo.com.br * A Basic Cache block.
8513223Sodanrc@yahoo.com.br * Contains the tag, status, and a pointer to data.
8613223Sodanrc@yahoo.com.br */
8713223Sodanrc@yahoo.com.brclass CacheBlk : public ReplaceableEntry
8813223Sodanrc@yahoo.com.br{
8913223Sodanrc@yahoo.com.br  public:
9013223Sodanrc@yahoo.com.br    /** Task Id associated with this block */
9113223Sodanrc@yahoo.com.br    uint32_t task_id;
9213223Sodanrc@yahoo.com.br
9313223Sodanrc@yahoo.com.br    /** Data block tag value. */
9413223Sodanrc@yahoo.com.br    Addr tag;
9513223Sodanrc@yahoo.com.br    /**
9613223Sodanrc@yahoo.com.br     * Contains a copy of the data in this block for easy access. This is used
9713223Sodanrc@yahoo.com.br     * for efficient execution when the data could be actually stored in
9813223Sodanrc@yahoo.com.br     * another format (COW, compressed, sub-blocked, etc). In all cases the
9913223Sodanrc@yahoo.com.br     * data stored here should be kept consistant with the actual data
10013223Sodanrc@yahoo.com.br     * referenced by this block.
10113223Sodanrc@yahoo.com.br     */
10213223Sodanrc@yahoo.com.br    uint8_t *data;
10313223Sodanrc@yahoo.com.br
10413223Sodanrc@yahoo.com.br    /** block state: OR of CacheBlkStatusBit */
10513223Sodanrc@yahoo.com.br    typedef unsigned State;
10613223Sodanrc@yahoo.com.br
10713223Sodanrc@yahoo.com.br    /** The current status of this block. @sa CacheBlockStatusBits */
10813223Sodanrc@yahoo.com.br    State status;
10913223Sodanrc@yahoo.com.br
11013477Sodanrc@yahoo.com.br    /**
11113477Sodanrc@yahoo.com.br     * Which curTick() will this block be accessible. Its value is only
11213477Sodanrc@yahoo.com.br     * meaningful if the block is valid.
11313477Sodanrc@yahoo.com.br     */
11413223Sodanrc@yahoo.com.br    Tick whenReady;
11513223Sodanrc@yahoo.com.br
11613223Sodanrc@yahoo.com.br    /** Number of references to this block since it was brought in. */
11713223Sodanrc@yahoo.com.br    unsigned refCount;
11813223Sodanrc@yahoo.com.br
11913223Sodanrc@yahoo.com.br    /** holds the source requestor ID for this block. */
12013223Sodanrc@yahoo.com.br    int srcMasterId;
12113223Sodanrc@yahoo.com.br
12213477Sodanrc@yahoo.com.br    /**
12313477Sodanrc@yahoo.com.br     * Tick on which the block was inserted in the cache. Its value is only
12413477Sodanrc@yahoo.com.br     * meaningful if the block is valid.
12513477Sodanrc@yahoo.com.br     */
12613223Sodanrc@yahoo.com.br    Tick tickInserted;
12713223Sodanrc@yahoo.com.br
12813223Sodanrc@yahoo.com.br  protected:
12913223Sodanrc@yahoo.com.br    /**
13013223Sodanrc@yahoo.com.br     * Represents that the indicated thread context has a "lock" on
13113223Sodanrc@yahoo.com.br     * the block, in the LL/SC sense.
13213223Sodanrc@yahoo.com.br     */
13313223Sodanrc@yahoo.com.br    class Lock {
13413223Sodanrc@yahoo.com.br      public:
13513223Sodanrc@yahoo.com.br        ContextID contextId;     // locking context
13613223Sodanrc@yahoo.com.br        Addr lowAddr;      // low address of lock range
13713223Sodanrc@yahoo.com.br        Addr highAddr;     // high address of lock range
13813223Sodanrc@yahoo.com.br
13913223Sodanrc@yahoo.com.br        // check for matching execution context, and an address that
14013223Sodanrc@yahoo.com.br        // is within the lock
14113223Sodanrc@yahoo.com.br        bool matches(const RequestPtr &req) const
14213223Sodanrc@yahoo.com.br        {
14313223Sodanrc@yahoo.com.br            Addr req_low = req->getPaddr();
14413223Sodanrc@yahoo.com.br            Addr req_high = req_low + req->getSize() -1;
14513223Sodanrc@yahoo.com.br            return (contextId == req->contextId()) &&
14613223Sodanrc@yahoo.com.br                   (req_low >= lowAddr) && (req_high <= highAddr);
14713223Sodanrc@yahoo.com.br        }
14813223Sodanrc@yahoo.com.br
14913223Sodanrc@yahoo.com.br        // check if a request is intersecting and thus invalidating the lock
15013223Sodanrc@yahoo.com.br        bool intersects(const RequestPtr &req) const
15113223Sodanrc@yahoo.com.br        {
15213223Sodanrc@yahoo.com.br            Addr req_low = req->getPaddr();
15313223Sodanrc@yahoo.com.br            Addr req_high = req_low + req->getSize() - 1;
15413223Sodanrc@yahoo.com.br
15513223Sodanrc@yahoo.com.br            return (req_low <= highAddr) && (req_high >= lowAddr);
15613223Sodanrc@yahoo.com.br        }
15713223Sodanrc@yahoo.com.br
15813223Sodanrc@yahoo.com.br        Lock(const RequestPtr &req)
15913223Sodanrc@yahoo.com.br            : contextId(req->contextId()),
16013223Sodanrc@yahoo.com.br              lowAddr(req->getPaddr()),
16113223Sodanrc@yahoo.com.br              highAddr(lowAddr + req->getSize() - 1)
16213223Sodanrc@yahoo.com.br        {
16313223Sodanrc@yahoo.com.br        }
16413223Sodanrc@yahoo.com.br    };
16513223Sodanrc@yahoo.com.br
16613223Sodanrc@yahoo.com.br    /** List of thread contexts that have performed a load-locked (LL)
16713223Sodanrc@yahoo.com.br     * on the block since the last store. */
16813223Sodanrc@yahoo.com.br    std::list<Lock> lockList;
16913223Sodanrc@yahoo.com.br
17013223Sodanrc@yahoo.com.br  public:
17113477Sodanrc@yahoo.com.br    CacheBlk() : data(nullptr), tickInserted(0)
17213223Sodanrc@yahoo.com.br    {
17313223Sodanrc@yahoo.com.br        invalidate();
17413223Sodanrc@yahoo.com.br    }
17513223Sodanrc@yahoo.com.br
17613223Sodanrc@yahoo.com.br    CacheBlk(const CacheBlk&) = delete;
17713223Sodanrc@yahoo.com.br    CacheBlk& operator=(const CacheBlk&) = delete;
17813223Sodanrc@yahoo.com.br    virtual ~CacheBlk() {};
17913223Sodanrc@yahoo.com.br
18013223Sodanrc@yahoo.com.br    /**
18113223Sodanrc@yahoo.com.br     * Checks the write permissions of this block.
18213223Sodanrc@yahoo.com.br     * @return True if the block is writable.
18313223Sodanrc@yahoo.com.br     */
18413223Sodanrc@yahoo.com.br    bool isWritable() const
18513223Sodanrc@yahoo.com.br    {
18613223Sodanrc@yahoo.com.br        const State needed_bits = BlkWritable | BlkValid;
18713223Sodanrc@yahoo.com.br        return (status & needed_bits) == needed_bits;
18813223Sodanrc@yahoo.com.br    }
18913223Sodanrc@yahoo.com.br
19013223Sodanrc@yahoo.com.br    /**
19113223Sodanrc@yahoo.com.br     * Checks the read permissions of this block.  Note that a block
19213223Sodanrc@yahoo.com.br     * can be valid but not readable if there is an outstanding write
19313223Sodanrc@yahoo.com.br     * upgrade miss.
19413223Sodanrc@yahoo.com.br     * @return True if the block is readable.
19513223Sodanrc@yahoo.com.br     */
19613223Sodanrc@yahoo.com.br    bool isReadable() const
19713223Sodanrc@yahoo.com.br    {
19813223Sodanrc@yahoo.com.br        const State needed_bits = BlkReadable | BlkValid;
19913223Sodanrc@yahoo.com.br        return (status & needed_bits) == needed_bits;
20013223Sodanrc@yahoo.com.br    }
20113223Sodanrc@yahoo.com.br
20213223Sodanrc@yahoo.com.br    /**
20313223Sodanrc@yahoo.com.br     * Checks that a block is valid.
20413223Sodanrc@yahoo.com.br     * @return True if the block is valid.
20513223Sodanrc@yahoo.com.br     */
20613223Sodanrc@yahoo.com.br    bool isValid() const
20713223Sodanrc@yahoo.com.br    {
20813223Sodanrc@yahoo.com.br        return (status & BlkValid) != 0;
20913223Sodanrc@yahoo.com.br    }
21013223Sodanrc@yahoo.com.br
21113223Sodanrc@yahoo.com.br    /**
21213223Sodanrc@yahoo.com.br     * Invalidate the block and clear all state.
21313223Sodanrc@yahoo.com.br     */
21413223Sodanrc@yahoo.com.br    virtual void invalidate()
21513223Sodanrc@yahoo.com.br    {
21613223Sodanrc@yahoo.com.br        tag = MaxAddr;
21713223Sodanrc@yahoo.com.br        task_id = ContextSwitchTaskId::Unknown;
21813223Sodanrc@yahoo.com.br        status = 0;
21913223Sodanrc@yahoo.com.br        whenReady = MaxTick;
22013223Sodanrc@yahoo.com.br        refCount = 0;
22113223Sodanrc@yahoo.com.br        srcMasterId = Request::invldMasterId;
22213223Sodanrc@yahoo.com.br        lockList.clear();
22313223Sodanrc@yahoo.com.br    }
22413223Sodanrc@yahoo.com.br
22513223Sodanrc@yahoo.com.br    /**
22613223Sodanrc@yahoo.com.br     * Check to see if a block has been written.
22713223Sodanrc@yahoo.com.br     * @return True if the block is dirty.
22813223Sodanrc@yahoo.com.br     */
22913223Sodanrc@yahoo.com.br    bool isDirty() const
23013223Sodanrc@yahoo.com.br    {
23113223Sodanrc@yahoo.com.br        return (status & BlkDirty) != 0;
23213223Sodanrc@yahoo.com.br    }
23313223Sodanrc@yahoo.com.br
23413223Sodanrc@yahoo.com.br    /**
23513223Sodanrc@yahoo.com.br     * Check if this block was the result of a hardware prefetch, yet to
23613223Sodanrc@yahoo.com.br     * be touched.
23713223Sodanrc@yahoo.com.br     * @return True if the block was a hardware prefetch, unaccesed.
23813223Sodanrc@yahoo.com.br     */
23913223Sodanrc@yahoo.com.br    bool wasPrefetched() const
24013223Sodanrc@yahoo.com.br    {
24113223Sodanrc@yahoo.com.br        return (status & BlkHWPrefetched) != 0;
24213223Sodanrc@yahoo.com.br    }
24313223Sodanrc@yahoo.com.br
24413223Sodanrc@yahoo.com.br    /**
24513223Sodanrc@yahoo.com.br     * Check if this block holds data from the secure memory space.
24613223Sodanrc@yahoo.com.br     * @return True if the block holds data from the secure memory space.
24713223Sodanrc@yahoo.com.br     */
24813223Sodanrc@yahoo.com.br    bool isSecure() const
24913223Sodanrc@yahoo.com.br    {
25013223Sodanrc@yahoo.com.br        return (status & BlkSecure) != 0;
25113223Sodanrc@yahoo.com.br    }
25213223Sodanrc@yahoo.com.br
25313223Sodanrc@yahoo.com.br    /**
25413445Sodanrc@yahoo.com.br     * Set valid bit.
25513445Sodanrc@yahoo.com.br     */
25613445Sodanrc@yahoo.com.br    virtual void setValid()
25713445Sodanrc@yahoo.com.br    {
25813445Sodanrc@yahoo.com.br        assert(!isValid());
25913445Sodanrc@yahoo.com.br        status |= BlkValid;
26013445Sodanrc@yahoo.com.br    }
26113445Sodanrc@yahoo.com.br
26213445Sodanrc@yahoo.com.br    /**
26313445Sodanrc@yahoo.com.br     * Set secure bit.
26413445Sodanrc@yahoo.com.br     */
26513445Sodanrc@yahoo.com.br    virtual void setSecure()
26613445Sodanrc@yahoo.com.br    {
26713445Sodanrc@yahoo.com.br        status |= BlkSecure;
26813445Sodanrc@yahoo.com.br    }
26913445Sodanrc@yahoo.com.br
27013445Sodanrc@yahoo.com.br    /**
27113477Sodanrc@yahoo.com.br     * Get tick at which block's data will be available for access.
27213477Sodanrc@yahoo.com.br     *
27313477Sodanrc@yahoo.com.br     * @return Data ready tick.
27413477Sodanrc@yahoo.com.br     */
27513477Sodanrc@yahoo.com.br    Tick getWhenReady() const
27613477Sodanrc@yahoo.com.br    {
27713477Sodanrc@yahoo.com.br        return whenReady;
27813477Sodanrc@yahoo.com.br    }
27913477Sodanrc@yahoo.com.br
28013477Sodanrc@yahoo.com.br    /**
28113477Sodanrc@yahoo.com.br     * Set tick at which block's data will be available for access. The new
28213477Sodanrc@yahoo.com.br     * tick must be chronologically sequential with respect to previous
28313477Sodanrc@yahoo.com.br     * accesses.
28413477Sodanrc@yahoo.com.br     *
28513477Sodanrc@yahoo.com.br     * @param tick New data ready tick.
28613477Sodanrc@yahoo.com.br     */
28713477Sodanrc@yahoo.com.br    void setWhenReady(const Tick tick)
28813477Sodanrc@yahoo.com.br    {
28913477Sodanrc@yahoo.com.br        assert(tick >= tickInserted);
29013477Sodanrc@yahoo.com.br        whenReady = tick;
29113477Sodanrc@yahoo.com.br    }
29213477Sodanrc@yahoo.com.br
29313477Sodanrc@yahoo.com.br    /**
29413223Sodanrc@yahoo.com.br     * Set member variables when a block insertion occurs. Resets reference
29513223Sodanrc@yahoo.com.br     * count to 1 (the insertion counts as a reference), and touch block if
29613223Sodanrc@yahoo.com.br     * it hadn't been touched previously. Sets the insertion tick to the
29713445Sodanrc@yahoo.com.br     * current tick. Marks the block valid.
29813223Sodanrc@yahoo.com.br     *
29913223Sodanrc@yahoo.com.br     * @param tag Block address tag.
30013223Sodanrc@yahoo.com.br     * @param is_secure Whether the block is in secure space or not.
30113223Sodanrc@yahoo.com.br     * @param src_master_ID The source requestor ID.
30213223Sodanrc@yahoo.com.br     * @param task_ID The new task ID.
30313223Sodanrc@yahoo.com.br     */
30413223Sodanrc@yahoo.com.br    virtual void insert(const Addr tag, const bool is_secure,
30513223Sodanrc@yahoo.com.br                        const int src_master_ID, const uint32_t task_ID);
30613223Sodanrc@yahoo.com.br
30713223Sodanrc@yahoo.com.br    /**
30813223Sodanrc@yahoo.com.br     * Track the fact that a local locked was issued to the
30913223Sodanrc@yahoo.com.br     * block. Invalidate any previous LL to the same address.
31013223Sodanrc@yahoo.com.br     */
31113223Sodanrc@yahoo.com.br    void trackLoadLocked(PacketPtr pkt)
31213223Sodanrc@yahoo.com.br    {
31313223Sodanrc@yahoo.com.br        assert(pkt->isLLSC());
31413223Sodanrc@yahoo.com.br        auto l = lockList.begin();
31513223Sodanrc@yahoo.com.br        while (l != lockList.end()) {
31613223Sodanrc@yahoo.com.br            if (l->intersects(pkt->req))
31713223Sodanrc@yahoo.com.br                l = lockList.erase(l);
31813223Sodanrc@yahoo.com.br            else
31913223Sodanrc@yahoo.com.br                ++l;
32013223Sodanrc@yahoo.com.br        }
32113223Sodanrc@yahoo.com.br
32213223Sodanrc@yahoo.com.br        lockList.emplace_front(pkt->req);
32313223Sodanrc@yahoo.com.br    }
32413223Sodanrc@yahoo.com.br
32513223Sodanrc@yahoo.com.br    /**
32613223Sodanrc@yahoo.com.br     * Clear the any load lock that intersect the request, and is from
32713223Sodanrc@yahoo.com.br     * a different context.
32813223Sodanrc@yahoo.com.br     */
32913223Sodanrc@yahoo.com.br    void clearLoadLocks(const RequestPtr &req)
33013223Sodanrc@yahoo.com.br    {
33113223Sodanrc@yahoo.com.br        auto l = lockList.begin();
33213223Sodanrc@yahoo.com.br        while (l != lockList.end()) {
33313223Sodanrc@yahoo.com.br            if (l->intersects(req) && l->contextId != req->contextId()) {
33413223Sodanrc@yahoo.com.br                l = lockList.erase(l);
33513223Sodanrc@yahoo.com.br            } else {
33613223Sodanrc@yahoo.com.br                ++l;
33713223Sodanrc@yahoo.com.br            }
33813223Sodanrc@yahoo.com.br        }
33913223Sodanrc@yahoo.com.br    }
34013223Sodanrc@yahoo.com.br
34113223Sodanrc@yahoo.com.br    /**
34213223Sodanrc@yahoo.com.br     * Pretty-print tag, set and way, and interpret state bits to readable form
34313223Sodanrc@yahoo.com.br     * including mapping to a MOESI state.
34413223Sodanrc@yahoo.com.br     *
34513223Sodanrc@yahoo.com.br     * @return string with basic state information
34613223Sodanrc@yahoo.com.br     */
34713223Sodanrc@yahoo.com.br    virtual std::string print() const
34813223Sodanrc@yahoo.com.br    {
34913223Sodanrc@yahoo.com.br        /**
35013223Sodanrc@yahoo.com.br         *  state       M   O   E   S   I
35113223Sodanrc@yahoo.com.br         *  writable    1   0   1   0   0
35213223Sodanrc@yahoo.com.br         *  dirty       1   1   0   0   0
35313223Sodanrc@yahoo.com.br         *  valid       1   1   1   1   0
35413223Sodanrc@yahoo.com.br         *
35513223Sodanrc@yahoo.com.br         *  state   writable    dirty   valid
35613223Sodanrc@yahoo.com.br         *  M       1           1       1
35713223Sodanrc@yahoo.com.br         *  O       0           1       1
35813223Sodanrc@yahoo.com.br         *  E       1           0       1
35913223Sodanrc@yahoo.com.br         *  S       0           0       1
36013223Sodanrc@yahoo.com.br         *  I       0           0       0
36113223Sodanrc@yahoo.com.br         *
36213223Sodanrc@yahoo.com.br         * Note that only one cache ever has a block in Modified or
36313223Sodanrc@yahoo.com.br         * Owned state, i.e., only one cache owns the block, or
36413223Sodanrc@yahoo.com.br         * equivalently has the BlkDirty bit set. However, multiple
36513223Sodanrc@yahoo.com.br         * caches on the same path to memory can have a block in the
36613223Sodanrc@yahoo.com.br         * Exclusive state (despite the name). Exclusive means this
36713223Sodanrc@yahoo.com.br         * cache has the only copy at this level of the hierarchy,
36813223Sodanrc@yahoo.com.br         * i.e., there may be copies in caches above this cache (in
36913223Sodanrc@yahoo.com.br         * various states), but there are no peers that have copies on
37013223Sodanrc@yahoo.com.br         * this branch of the hierarchy, and no caches at or above
37113223Sodanrc@yahoo.com.br         * this level on any other branch have copies either.
37213223Sodanrc@yahoo.com.br         **/
37313223Sodanrc@yahoo.com.br        unsigned state = isWritable() << 2 | isDirty() << 1 | isValid();
37413223Sodanrc@yahoo.com.br        char s = '?';
37513223Sodanrc@yahoo.com.br        switch (state) {
37613223Sodanrc@yahoo.com.br          case 0b111: s = 'M'; break;
37713223Sodanrc@yahoo.com.br          case 0b011: s = 'O'; break;
37813223Sodanrc@yahoo.com.br          case 0b101: s = 'E'; break;
37913223Sodanrc@yahoo.com.br          case 0b001: s = 'S'; break;
38013223Sodanrc@yahoo.com.br          case 0b000: s = 'I'; break;
38113223Sodanrc@yahoo.com.br          default:    s = 'T'; break; // @TODO add other types
38213223Sodanrc@yahoo.com.br        }
38313223Sodanrc@yahoo.com.br        return csprintf("state: %x (%c) valid: %d writable: %d readable: %d "
38413223Sodanrc@yahoo.com.br                        "dirty: %d | tag: %#x set: %#x way: %#x", status, s,
38513223Sodanrc@yahoo.com.br                        isValid(), isWritable(), isReadable(), isDirty(), tag,
38613223Sodanrc@yahoo.com.br                        getSet(), getWay());
38713223Sodanrc@yahoo.com.br    }
38813223Sodanrc@yahoo.com.br
38913223Sodanrc@yahoo.com.br    /**
39013223Sodanrc@yahoo.com.br     * Handle interaction of load-locked operations and stores.
39113223Sodanrc@yahoo.com.br     * @return True if write should proceed, false otherwise.  Returns
39213223Sodanrc@yahoo.com.br     * false only in the case of a failed store conditional.
39313223Sodanrc@yahoo.com.br     */
39413223Sodanrc@yahoo.com.br    bool checkWrite(PacketPtr pkt)
39513223Sodanrc@yahoo.com.br    {
39613223Sodanrc@yahoo.com.br        assert(pkt->isWrite());
39713223Sodanrc@yahoo.com.br
39813223Sodanrc@yahoo.com.br        // common case
39913223Sodanrc@yahoo.com.br        if (!pkt->isLLSC() && lockList.empty())
40013223Sodanrc@yahoo.com.br            return true;
40113223Sodanrc@yahoo.com.br
40213223Sodanrc@yahoo.com.br        const RequestPtr &req = pkt->req;
40313223Sodanrc@yahoo.com.br
40413223Sodanrc@yahoo.com.br        if (pkt->isLLSC()) {
40513223Sodanrc@yahoo.com.br            // it's a store conditional... have to check for matching
40613223Sodanrc@yahoo.com.br            // load locked.
40713223Sodanrc@yahoo.com.br            bool success = false;
40813223Sodanrc@yahoo.com.br
40913223Sodanrc@yahoo.com.br            auto l = lockList.begin();
41013223Sodanrc@yahoo.com.br            while (!success && l != lockList.end()) {
41113223Sodanrc@yahoo.com.br                if (l->matches(pkt->req)) {
41213223Sodanrc@yahoo.com.br                    // it's a store conditional, and as far as the
41313223Sodanrc@yahoo.com.br                    // memory system can tell, the requesting
41413223Sodanrc@yahoo.com.br                    // context's lock is still valid.
41513223Sodanrc@yahoo.com.br                    success = true;
41613223Sodanrc@yahoo.com.br                    lockList.erase(l);
41713223Sodanrc@yahoo.com.br                } else {
41813223Sodanrc@yahoo.com.br                    ++l;
41913223Sodanrc@yahoo.com.br                }
42013223Sodanrc@yahoo.com.br            }
42113223Sodanrc@yahoo.com.br
42213223Sodanrc@yahoo.com.br            req->setExtraData(success ? 1 : 0);
42313223Sodanrc@yahoo.com.br            // clear any intersected locks from other contexts (our LL
42413223Sodanrc@yahoo.com.br            // should already have cleared them)
42513223Sodanrc@yahoo.com.br            clearLoadLocks(req);
42613223Sodanrc@yahoo.com.br            return success;
42713223Sodanrc@yahoo.com.br        } else {
42813223Sodanrc@yahoo.com.br            // a normal write, if there is any lock not from this
42913223Sodanrc@yahoo.com.br            // context we clear the list, thus for a private cache we
43013223Sodanrc@yahoo.com.br            // never clear locks on normal writes
43113223Sodanrc@yahoo.com.br            clearLoadLocks(req);
43213223Sodanrc@yahoo.com.br            return true;
43313223Sodanrc@yahoo.com.br        }
43413223Sodanrc@yahoo.com.br    }
43513223Sodanrc@yahoo.com.br};
43613223Sodanrc@yahoo.com.br
43713223Sodanrc@yahoo.com.br/**
43813223Sodanrc@yahoo.com.br * Special instance of CacheBlk for use with tempBlk that deals with its
43913223Sodanrc@yahoo.com.br * block address regeneration.
44013223Sodanrc@yahoo.com.br * @sa Cache
44113223Sodanrc@yahoo.com.br */
44213223Sodanrc@yahoo.com.brclass TempCacheBlk final : public CacheBlk
44313223Sodanrc@yahoo.com.br{
44413223Sodanrc@yahoo.com.br  private:
44513223Sodanrc@yahoo.com.br    /**
44613223Sodanrc@yahoo.com.br     * Copy of the block's address, used to regenerate tempBlock's address.
44713223Sodanrc@yahoo.com.br     */
44813223Sodanrc@yahoo.com.br    Addr _addr;
44913223Sodanrc@yahoo.com.br
45013223Sodanrc@yahoo.com.br  public:
45113223Sodanrc@yahoo.com.br    /**
45213223Sodanrc@yahoo.com.br     * Creates a temporary cache block, with its own storage.
45313223Sodanrc@yahoo.com.br     * @param size The size (in bytes) of this cache block.
45413223Sodanrc@yahoo.com.br     */
45513223Sodanrc@yahoo.com.br    TempCacheBlk(unsigned size) : CacheBlk()
45613223Sodanrc@yahoo.com.br    {
45713223Sodanrc@yahoo.com.br        data = new uint8_t[size];
45813223Sodanrc@yahoo.com.br    }
45913223Sodanrc@yahoo.com.br    TempCacheBlk(const TempCacheBlk&) = delete;
46013223Sodanrc@yahoo.com.br    TempCacheBlk& operator=(const TempCacheBlk&) = delete;
46113223Sodanrc@yahoo.com.br    ~TempCacheBlk() { delete [] data; };
46213223Sodanrc@yahoo.com.br
46313223Sodanrc@yahoo.com.br    /**
46413223Sodanrc@yahoo.com.br     * Invalidate the block and clear all state.
46513223Sodanrc@yahoo.com.br     */
46613223Sodanrc@yahoo.com.br    void invalidate() override {
46713223Sodanrc@yahoo.com.br        CacheBlk::invalidate();
46813223Sodanrc@yahoo.com.br
46913223Sodanrc@yahoo.com.br        _addr = MaxAddr;
47013223Sodanrc@yahoo.com.br    }
47113223Sodanrc@yahoo.com.br
47213223Sodanrc@yahoo.com.br    void insert(const Addr addr, const bool is_secure,
47313223Sodanrc@yahoo.com.br                const int src_master_ID=0, const uint32_t task_ID=0) override
47413223Sodanrc@yahoo.com.br    {
47513445Sodanrc@yahoo.com.br        // Make sure that the block has been properly invalidated
47613445Sodanrc@yahoo.com.br        assert(status == 0);
47713445Sodanrc@yahoo.com.br
47813223Sodanrc@yahoo.com.br        // Set block address
47913223Sodanrc@yahoo.com.br        _addr = addr;
48013223Sodanrc@yahoo.com.br
48113223Sodanrc@yahoo.com.br        // Set secure state
48213223Sodanrc@yahoo.com.br        if (is_secure) {
48313445Sodanrc@yahoo.com.br            setSecure();
48413223Sodanrc@yahoo.com.br        }
48513445Sodanrc@yahoo.com.br
48613445Sodanrc@yahoo.com.br        // Validate block
48713445Sodanrc@yahoo.com.br        setValid();
48813223Sodanrc@yahoo.com.br    }
48913223Sodanrc@yahoo.com.br
49013223Sodanrc@yahoo.com.br    /**
49113223Sodanrc@yahoo.com.br     * Get block's address.
49213223Sodanrc@yahoo.com.br     *
49313223Sodanrc@yahoo.com.br     * @return addr Address value.
49413223Sodanrc@yahoo.com.br     */
49513223Sodanrc@yahoo.com.br    Addr getAddr() const
49613223Sodanrc@yahoo.com.br    {
49713223Sodanrc@yahoo.com.br        return _addr;
49813223Sodanrc@yahoo.com.br    }
49913223Sodanrc@yahoo.com.br};
50013223Sodanrc@yahoo.com.br
50113223Sodanrc@yahoo.com.br/**
50213223Sodanrc@yahoo.com.br * Simple class to provide virtual print() method on cache blocks
50313223Sodanrc@yahoo.com.br * without allocating a vtable pointer for every single cache block.
50413223Sodanrc@yahoo.com.br * Just wrap the CacheBlk object in an instance of this before passing
50513223Sodanrc@yahoo.com.br * to a function that requires a Printable object.
50613223Sodanrc@yahoo.com.br */
50713223Sodanrc@yahoo.com.brclass CacheBlkPrintWrapper : public Printable
50813223Sodanrc@yahoo.com.br{
50913223Sodanrc@yahoo.com.br    CacheBlk *blk;
51013223Sodanrc@yahoo.com.br  public:
51113223Sodanrc@yahoo.com.br    CacheBlkPrintWrapper(CacheBlk *_blk) : blk(_blk) {}
51213223Sodanrc@yahoo.com.br    virtual ~CacheBlkPrintWrapper() {}
51313223Sodanrc@yahoo.com.br    void print(std::ostream &o, int verbosity = 0,
51413223Sodanrc@yahoo.com.br               const std::string &prefix = "") const;
51513223Sodanrc@yahoo.com.br};
51613223Sodanrc@yahoo.com.br
51713223Sodanrc@yahoo.com.br#endif //__MEM_CACHE_CACHE_BLK_HH__
518