physical.hh revision 8711
1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ron Dreslinski
29 */
30
31/* @file
32 */
33
34#ifndef __PHYSICAL_MEMORY_HH__
35#define __PHYSICAL_MEMORY_HH__
36
37#include <map>
38#include <string>
39
40#include "base/range.hh"
41#include "mem/mem_object.hh"
42#include "mem/packet.hh"
43#include "mem/tport.hh"
44#include "params/PhysicalMemory.hh"
45#include "sim/eventq.hh"
46
47//
48// Functional model for a contiguous block of physical memory. (i.e. RAM)
49//
50class PhysicalMemory : public MemObject
51{
52  protected:
53
54    class MemoryPort : public SimpleTimingPort
55    {
56        PhysicalMemory *memory;
57
58      public:
59
60        MemoryPort(const std::string &_name, PhysicalMemory *_memory);
61
62      protected:
63
64        virtual Tick recvAtomic(PacketPtr pkt);
65
66        virtual void recvFunctional(PacketPtr pkt);
67
68        virtual void recvRangeChange();
69
70        virtual AddrRangeList getAddrRanges();
71
72        virtual unsigned deviceBlockSize() const;
73    };
74
75    int numPorts;
76
77
78  private:
79    // prevent copying of a MainMemory object
80    PhysicalMemory(const PhysicalMemory &specmem);
81    const PhysicalMemory &operator=(const PhysicalMemory &specmem);
82
83  protected:
84
85    class LockedAddr {
86      public:
87        // on alpha, minimum LL/SC granularity is 16 bytes, so lower
88        // bits need to masked off.
89        static const Addr Addr_Mask = 0xf;
90
91        static Addr mask(Addr paddr) { return (paddr & ~Addr_Mask); }
92
93        Addr addr;      // locked address
94        int contextId;     // locking hw context
95
96        // check for matching execution context
97        bool matchesContext(Request *req)
98        {
99            return (contextId == req->contextId());
100        }
101
102        LockedAddr(Request *req)
103            : addr(mask(req->getPaddr())),
104              contextId(req->contextId())
105        {
106        }
107        // constructor for unserialization use
108        LockedAddr(Addr _addr, int _cid)
109            : addr(_addr), contextId(_cid)
110        {
111        }
112    };
113
114    std::list<LockedAddr> lockedAddrList;
115
116    // helper function for checkLockedAddrs(): we really want to
117    // inline a quick check for an empty locked addr list (hopefully
118    // the common case), and do the full list search (if necessary) in
119    // this out-of-line function
120    bool checkLockedAddrList(PacketPtr pkt);
121
122    // Record the address of a load-locked operation so that we can
123    // clear the execution context's lock flag if a matching store is
124    // performed
125    void trackLoadLocked(PacketPtr pkt);
126
127    // Compare a store address with any locked addresses so we can
128    // clear the lock flag appropriately.  Return value set to 'false'
129    // if store operation should be suppressed (because it was a
130    // conditional store and the address was no longer locked by the
131    // requesting execution context), 'true' otherwise.  Note that
132    // this method must be called on *all* stores since even
133    // non-conditional stores must clear any matching lock addresses.
134    bool writeOK(PacketPtr pkt) {
135        Request *req = pkt->req;
136        if (lockedAddrList.empty()) {
137            // no locked addrs: nothing to check, store_conditional fails
138            bool isLLSC = pkt->isLLSC();
139            if (isLLSC) {
140                req->setExtraData(0);
141            }
142            return !isLLSC; // only do write if not an sc
143        } else {
144            // iterate over list...
145            return checkLockedAddrList(pkt);
146        }
147    }
148
149    uint8_t *pmemAddr;
150    Tick lat;
151    Tick lat_var;
152    std::vector<MemoryPort*> ports;
153    typedef std::vector<MemoryPort*>::iterator PortIterator;
154
155    uint64_t _size;
156    uint64_t _start;
157  public:
158    uint64_t size() { return _size; }
159    uint64_t start() { return _start; }
160
161  public:
162    typedef PhysicalMemoryParams Params;
163    PhysicalMemory(const Params *p);
164    virtual ~PhysicalMemory();
165
166    const Params *
167    params() const
168    {
169        return dynamic_cast<const Params *>(_params);
170    }
171
172  public:
173    unsigned deviceBlockSize() const;
174    AddrRangeList getAddrRanges();
175    virtual Port *getPort(const std::string &if_name, int idx = -1);
176    void virtual init();
177    unsigned int drain(Event *de);
178
179  protected:
180    Tick doAtomicAccess(PacketPtr pkt);
181    void doFunctionalAccess(PacketPtr pkt);
182    virtual Tick calculateLatency(PacketPtr pkt);
183
184  public:
185    virtual void serialize(std::ostream &os);
186    virtual void unserialize(Checkpoint *cp, const std::string &section);
187
188};
189
190#endif //__PHYSICAL_MEMORY_HH__
191