abstract_mem.hh revision 11169
111663Stushar@ece.gatech.edu/*
211663Stushar@ece.gatech.edu * Copyright (c) 2012 ARM Limited
311663Stushar@ece.gatech.edu * All rights reserved
411663Stushar@ece.gatech.edu *
511663Stushar@ece.gatech.edu * The license below extends only to copyright in the software and shall
611663Stushar@ece.gatech.edu * not be construed as granting a license to any other intellectual
711663Stushar@ece.gatech.edu * property including but not limited to intellectual property relating
811663Stushar@ece.gatech.edu * to a hardware implementation of the functionality of the software
911663Stushar@ece.gatech.edu * licensed hereunder.  You may use the software subject to the license
1011663Stushar@ece.gatech.edu * terms below provided that you ensure that this notice is replicated
1111663Stushar@ece.gatech.edu * unmodified and in its entirety in all distributions of the software,
1211663Stushar@ece.gatech.edu * modified or unmodified, in source code or in binary form.
1311663Stushar@ece.gatech.edu *
1411663Stushar@ece.gatech.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan
1511663Stushar@ece.gatech.edu * All rights reserved.
1611663Stushar@ece.gatech.edu *
1711663Stushar@ece.gatech.edu * Redistribution and use in source and binary forms, with or without
1811663Stushar@ece.gatech.edu * modification, are permitted provided that the following conditions are
1911663Stushar@ece.gatech.edu * met: redistributions of source code must retain the above copyright
2011663Stushar@ece.gatech.edu * notice, this list of conditions and the following disclaimer;
2111663Stushar@ece.gatech.edu * redistributions in binary form must reproduce the above copyright
2211663Stushar@ece.gatech.edu * notice, this list of conditions and the following disclaimer in the
2311663Stushar@ece.gatech.edu * documentation and/or other materials provided with the distribution;
2411663Stushar@ece.gatech.edu * neither the name of the copyright holders nor the names of its
2511663Stushar@ece.gatech.edu * contributors may be used to endorse or promote products derived from
2611663Stushar@ece.gatech.edu * this software without specific prior written permission.
2711663Stushar@ece.gatech.edu *
2811663Stushar@ece.gatech.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2911663Stushar@ece.gatech.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3011663Stushar@ece.gatech.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3111663Stushar@ece.gatech.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3211663Stushar@ece.gatech.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3311663Stushar@ece.gatech.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3411663Stushar@ece.gatech.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3511663Stushar@ece.gatech.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3611663Stushar@ece.gatech.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3711663Stushar@ece.gatech.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3811663Stushar@ece.gatech.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3911663Stushar@ece.gatech.edu *
4011663Stushar@ece.gatech.edu * Authors: Ron Dreslinski
4111663Stushar@ece.gatech.edu *          Andreas Hansson
4211663Stushar@ece.gatech.edu */
4311663Stushar@ece.gatech.edu
4411663Stushar@ece.gatech.edu/**
4511663Stushar@ece.gatech.edu * @file
4611663Stushar@ece.gatech.edu * AbstractMemory declaration
4711663Stushar@ece.gatech.edu */
4811663Stushar@ece.gatech.edu
4911663Stushar@ece.gatech.edu#ifndef __ABSTRACT_MEMORY_HH__
5011663Stushar@ece.gatech.edu#define __ABSTRACT_MEMORY_HH__
5111663Stushar@ece.gatech.edu
5211663Stushar@ece.gatech.edu#include "mem/mem_object.hh"
5311663Stushar@ece.gatech.edu#include "params/AbstractMemory.hh"
5411663Stushar@ece.gatech.edu#include "sim/stats.hh"
5511663Stushar@ece.gatech.edu
5611663Stushar@ece.gatech.edu
5711663Stushar@ece.gatech.educlass System;
5811663Stushar@ece.gatech.edu
5911663Stushar@ece.gatech.edu/**
6011663Stushar@ece.gatech.edu * Locked address class that represents a physical address and a
6111666Stushar@ece.gatech.edu * context id.
6211666Stushar@ece.gatech.edu */
6311666Stushar@ece.gatech.educlass LockedAddr {
6411666Stushar@ece.gatech.edu
6511666Stushar@ece.gatech.edu  private:
6611663Stushar@ece.gatech.edu
6711663Stushar@ece.gatech.edu    // on alpha, minimum LL/SC granularity is 16 bytes, so lower
6811663Stushar@ece.gatech.edu    // bits need to masked off.
6911666Stushar@ece.gatech.edu    static const Addr Addr_Mask = 0xf;
7011663Stushar@ece.gatech.edu
7111663Stushar@ece.gatech.edu  public:
7211663Stushar@ece.gatech.edu
7311663Stushar@ece.gatech.edu    // locked address
7411666Stushar@ece.gatech.edu    Addr addr;
7511666Stushar@ece.gatech.edu
7611663Stushar@ece.gatech.edu    // locking hw context
7711663Stushar@ece.gatech.edu    const ContextID contextId;
7811663Stushar@ece.gatech.edu
7911663Stushar@ece.gatech.edu    static Addr mask(Addr paddr) { return (paddr & ~Addr_Mask); }
8011663Stushar@ece.gatech.edu
8111663Stushar@ece.gatech.edu    // check for matching execution context
8211663Stushar@ece.gatech.edu    bool matchesContext(Request *req) const
8311663Stushar@ece.gatech.edu    {
8411663Stushar@ece.gatech.edu        return (contextId == req->contextId());
8513731Sandreas.sandberg@arm.com    }
8611663Stushar@ece.gatech.edu
8711663Stushar@ece.gatech.edu    LockedAddr(Request *req) : addr(mask(req->getPaddr())),
8811663Stushar@ece.gatech.edu                               contextId(req->contextId())
8911663Stushar@ece.gatech.edu    {}
9011663Stushar@ece.gatech.edu
9111663Stushar@ece.gatech.edu    // constructor for unserialization use
9211663Stushar@ece.gatech.edu    LockedAddr(Addr _addr, int _cid) : addr(_addr), contextId(_cid)
9311663Stushar@ece.gatech.edu    {}
9411663Stushar@ece.gatech.edu};
9511663Stushar@ece.gatech.edu
9611663Stushar@ece.gatech.edu/**
9711666Stushar@ece.gatech.edu * An abstract memory represents a contiguous block of physical
9811666Stushar@ece.gatech.edu * memory, with an associated address range, and also provides basic
9911663Stushar@ece.gatech.edu * functionality for reading and writing this memory without any
10011663Stushar@ece.gatech.edu * timing information. It is a MemObject since any subclass must have
10111663Stushar@ece.gatech.edu * at least one slave port.
10211663Stushar@ece.gatech.edu */
10311663Stushar@ece.gatech.educlass AbstractMemory : public MemObject
10411663Stushar@ece.gatech.edu{
10511663Stushar@ece.gatech.edu  protected:
10611663Stushar@ece.gatech.edu
10711666Stushar@ece.gatech.edu    // Address range of this memory
10811666Stushar@ece.gatech.edu    AddrRange range;
10911663Stushar@ece.gatech.edu
11011663Stushar@ece.gatech.edu    // Pointer to host memory used to implement this memory
11111663Stushar@ece.gatech.edu    uint8_t* pmemAddr;
11211663Stushar@ece.gatech.edu
11311663Stushar@ece.gatech.edu    // Enable specific memories to be reported to the configuration table
11411663Stushar@ece.gatech.edu    bool confTableReported;
11511663Stushar@ece.gatech.edu
11611663Stushar@ece.gatech.edu    // Should the memory appear in the global address map
11713731Sandreas.sandberg@arm.com    bool inAddrMap;
11813731Sandreas.sandberg@arm.com
11911663Stushar@ece.gatech.edu    std::list<LockedAddr> lockedAddrList;
12011663Stushar@ece.gatech.edu
12111663Stushar@ece.gatech.edu    // helper function for checkLockedAddrs(): we really want to
12211663Stushar@ece.gatech.edu    // inline a quick check for an empty locked addr list (hopefully
12311663Stushar@ece.gatech.edu    // the common case), and do the full list search (if necessary) in
12411663Stushar@ece.gatech.edu    // this out-of-line function
12511666Stushar@ece.gatech.edu    bool checkLockedAddrList(PacketPtr pkt);
12611663Stushar@ece.gatech.edu
12711663Stushar@ece.gatech.edu    // Record the address of a load-locked operation so that we can
12811663Stushar@ece.gatech.edu    // clear the execution context's lock flag if a matching store is
12911663Stushar@ece.gatech.edu    // performed
13013731Sandreas.sandberg@arm.com    void trackLoadLocked(PacketPtr pkt);
13113731Sandreas.sandberg@arm.com
13211663Stushar@ece.gatech.edu    // Compare a store address with any locked addresses so we can
13311663Stushar@ece.gatech.edu    // clear the lock flag appropriately.  Return value set to 'false'
13411663Stushar@ece.gatech.edu    // if store operation should be suppressed (because it was a
13511663Stushar@ece.gatech.edu    // conditional store and the address was no longer locked by the
13611663Stushar@ece.gatech.edu    // requesting execution context), 'true' otherwise.  Note that
13711663Stushar@ece.gatech.edu    // this method must be called on *all* stores since even
13811666Stushar@ece.gatech.edu    // non-conditional stores must clear any matching lock addresses.
13911663Stushar@ece.gatech.edu    bool writeOK(PacketPtr pkt) {
14011663Stushar@ece.gatech.edu        Request *req = pkt->req;
14111663Stushar@ece.gatech.edu        if (lockedAddrList.empty()) {
14211663Stushar@ece.gatech.edu            // no locked addrs: nothing to check, store_conditional fails
14311663Stushar@ece.gatech.edu            bool isLLSC = pkt->isLLSC();
14413731Sandreas.sandberg@arm.com            if (isLLSC) {
14513731Sandreas.sandberg@arm.com                req->setExtraData(0);
14611663Stushar@ece.gatech.edu            }
14711663Stushar@ece.gatech.edu            return !isLLSC; // only do write if not an sc
14811663Stushar@ece.gatech.edu        } else {
14911663Stushar@ece.gatech.edu            // iterate over list...
15011663Stushar@ece.gatech.edu            return checkLockedAddrList(pkt);
15111663Stushar@ece.gatech.edu        }
15211666Stushar@ece.gatech.edu    }
15311663Stushar@ece.gatech.edu
15411663Stushar@ece.gatech.edu    /** Number of total bytes read from this memory */
15511663Stushar@ece.gatech.edu    Stats::Vector bytesRead;
15611663Stushar@ece.gatech.edu    /** Number of instruction bytes read from this memory */
15713731Sandreas.sandberg@arm.com    Stats::Vector bytesInstRead;
15813731Sandreas.sandberg@arm.com    /** Number of bytes written to this memory */
15911663Stushar@ece.gatech.edu    Stats::Vector bytesWritten;
16011663Stushar@ece.gatech.edu    /** Number of read requests */
16111663Stushar@ece.gatech.edu    Stats::Vector numReads;
16211663Stushar@ece.gatech.edu    /** Number of write requests */
16311663Stushar@ece.gatech.edu    Stats::Vector numWrites;
16411663Stushar@ece.gatech.edu    /** Number of other requests */
16511666Stushar@ece.gatech.edu    Stats::Vector numOther;
16611663Stushar@ece.gatech.edu    /** Read bandwidth from this memory */
16711663Stushar@ece.gatech.edu    Stats::Formula bwRead;
16811663Stushar@ece.gatech.edu    /** Read bandwidth from this memory */
16911663Stushar@ece.gatech.edu    Stats::Formula bwInstRead;
17011663Stushar@ece.gatech.edu    /** Write bandwidth from this memory */
171    Stats::Formula bwWrite;
172    /** Total bandwidth from this memory */
173    Stats::Formula bwTotal;
174
175    /** Pointor to the System object.
176     * This is used for getting the number of masters in the system which is
177     * needed when registering stats
178     */
179    System *_system;
180
181
182  private:
183
184    // Prevent copying
185    AbstractMemory(const AbstractMemory&);
186
187    // Prevent assignment
188    AbstractMemory& operator=(const AbstractMemory&);
189
190  public:
191
192    typedef AbstractMemoryParams Params;
193
194    AbstractMemory(const Params* p);
195    virtual ~AbstractMemory() {}
196
197    /**
198     * Initialise this memory.
199     */
200    void init() override;
201
202    /**
203     * See if this is a null memory that should never store data and
204     * always return zero.
205     *
206     * @return true if null
207     */
208    bool isNull() const { return params()->null; }
209
210    /**
211     * Set the host memory backing store to be used by this memory
212     * controller.
213     *
214     * @param pmem_addr Pointer to a segment of host memory
215     */
216    void setBackingStore(uint8_t* pmem_addr);
217
218    /**
219     * Get the list of locked addresses to allow checkpointing.
220     */
221    const std::list<LockedAddr>& getLockedAddrList() const
222    { return lockedAddrList; }
223
224    /**
225     * Add a locked address to allow for checkpointing.
226     */
227    void addLockedAddr(LockedAddr addr) { lockedAddrList.push_back(addr); }
228
229    /** read the system pointer
230     * Implemented for completeness with the setter
231     * @return pointer to the system object */
232    System* system() const { return _system; }
233
234    /** Set the system pointer on this memory
235     * This can't be done via a python parameter because the system needs
236     * pointers to all the memories and the reverse would create a cycle in the
237     * object graph. An init() this is set.
238     * @param sys system pointer to set
239     */
240    void system(System *sys) { _system = sys; }
241
242    const Params *
243    params() const
244    {
245        return dynamic_cast<const Params *>(_params);
246    }
247
248    /**
249     * Get the address range
250     *
251     * @return a single contigous address range
252     */
253    AddrRange getAddrRange() const;
254
255    /**
256     * Get the memory size.
257     *
258     * @return the size of the memory
259     */
260    uint64_t size() const { return range.size(); }
261
262    /**
263     * Get the start address.
264     *
265     * @return the start address of the memory
266     */
267    Addr start() const { return range.start(); }
268
269    /**
270     *  Should this memory be passed to the kernel and part of the OS
271     *  physical memory layout.
272     *
273     * @return if this memory is reported
274     */
275    bool isConfReported() const { return confTableReported; }
276
277    /**
278     * Some memories are used as shadow memories or should for other
279     * reasons not be part of the global address map.
280     *
281     * @return if this memory is part of the address map
282     */
283    bool isInAddrMap() const { return inAddrMap; }
284
285    /**
286     * Perform an untimed memory access and update all the state
287     * (e.g. locked addresses) and statistics accordingly. The packet
288     * is turned into a response if required.
289     *
290     * @param pkt Packet performing the access
291     */
292    void access(PacketPtr pkt);
293
294    /**
295     * Perform an untimed memory read or write without changing
296     * anything but the memory itself. No stats are affected by this
297     * access. In addition to normal accesses this also facilitates
298     * print requests.
299     *
300     * @param pkt Packet performing the access
301     */
302    void functionalAccess(PacketPtr pkt);
303
304    /**
305     * Register Statistics
306     */
307    void regStats() override;
308
309};
310
311#endif //__ABSTRACT_MEMORY_HH__
312