simple_mem.hh revision 4626
15420Sgblack@eecs.umich.edu/*
25081Sgblack@eecs.umich.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan
35081Sgblack@eecs.umich.edu * All rights reserved.
45081Sgblack@eecs.umich.edu *
55081Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
65081Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
75081Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
85081Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
95081Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
105081Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
115081Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
125081Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
135081Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
145081Sgblack@eecs.umich.edu * this software without specific prior written permission.
155081Sgblack@eecs.umich.edu *
165081Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175081Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185081Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195081Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205081Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215081Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225081Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235081Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245081Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255081Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265081Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275081Sgblack@eecs.umich.edu *
285081Sgblack@eecs.umich.edu * Authors: Ron Dreslinski
295081Sgblack@eecs.umich.edu */
305081Sgblack@eecs.umich.edu
315081Sgblack@eecs.umich.edu/* @file
325081Sgblack@eecs.umich.edu */
335081Sgblack@eecs.umich.edu
345081Sgblack@eecs.umich.edu#ifndef __PHYSICAL_MEMORY_HH__
355081Sgblack@eecs.umich.edu#define __PHYSICAL_MEMORY_HH__
365081Sgblack@eecs.umich.edu
375081Sgblack@eecs.umich.edu#include "base/range.hh"
385081Sgblack@eecs.umich.edu#include "mem/mem_object.hh"
395081Sgblack@eecs.umich.edu#include "mem/packet.hh"
405081Sgblack@eecs.umich.edu#include "mem/tport.hh"
415081Sgblack@eecs.umich.edu#include "sim/eventq.hh"
425081Sgblack@eecs.umich.edu#include <map>
435081Sgblack@eecs.umich.edu#include <string>
445081Sgblack@eecs.umich.edu
455081Sgblack@eecs.umich.edu//
465081Sgblack@eecs.umich.edu// Functional model for a contiguous block of physical memory. (i.e. RAM)
475081Sgblack@eecs.umich.edu//
485081Sgblack@eecs.umich.educlass PhysicalMemory : public MemObject
495081Sgblack@eecs.umich.edu{
505081Sgblack@eecs.umich.edu    class MemoryPort : public SimpleTimingPort
515081Sgblack@eecs.umich.edu    {
525081Sgblack@eecs.umich.edu        PhysicalMemory *memory;
535081Sgblack@eecs.umich.edu
545081Sgblack@eecs.umich.edu      public:
555081Sgblack@eecs.umich.edu
565111Sgblack@eecs.umich.edu        MemoryPort(const std::string &_name, PhysicalMemory *_memory);
575111Sgblack@eecs.umich.edu
585111Sgblack@eecs.umich.edu      protected:
595111Sgblack@eecs.umich.edu
605111Sgblack@eecs.umich.edu        virtual Tick recvAtomic(PacketPtr pkt);
615111Sgblack@eecs.umich.edu
625111Sgblack@eecs.umich.edu        virtual void recvFunctional(PacketPtr pkt);
635111Sgblack@eecs.umich.edu
645111Sgblack@eecs.umich.edu        virtual void recvStatusChange(Status status);
655111Sgblack@eecs.umich.edu
665111Sgblack@eecs.umich.edu        virtual void getDeviceAddressRanges(AddrRangeList &resp,
675111Sgblack@eecs.umich.edu                                            bool &snoop);
685111Sgblack@eecs.umich.edu
695111Sgblack@eecs.umich.edu        virtual int deviceBlockSize();
705420Sgblack@eecs.umich.edu    };
715661Sgblack@eecs.umich.edu
725111Sgblack@eecs.umich.edu    int numPorts;
735111Sgblack@eecs.umich.edu
745111Sgblack@eecs.umich.edu
755111Sgblack@eecs.umich.edu  private:
765111Sgblack@eecs.umich.edu    // prevent copying of a MainMemory object
775111Sgblack@eecs.umich.edu    PhysicalMemory(const PhysicalMemory &specmem);
785111Sgblack@eecs.umich.edu    const PhysicalMemory &operator=(const PhysicalMemory &specmem);
795111Sgblack@eecs.umich.edu
805111Sgblack@eecs.umich.edu  protected:
815111Sgblack@eecs.umich.edu
825111Sgblack@eecs.umich.edu    class LockedAddr {
835661Sgblack@eecs.umich.edu      public:
845420Sgblack@eecs.umich.edu        // on alpha, minimum LL/SC granularity is 16 bytes, so lower
855111Sgblack@eecs.umich.edu        // bits need to masked off.
865111Sgblack@eecs.umich.edu        static const Addr Addr_Mask = 0xf;
875111Sgblack@eecs.umich.edu
88        static Addr mask(Addr paddr) { return (paddr & ~Addr_Mask); }
89
90        Addr addr; 	// locked address
91        int cpuNum;	// locking CPU
92        int threadNum;	// locking thread ID within CPU
93
94        // check for matching execution context
95        bool matchesContext(Request *req)
96        {
97            return (cpuNum == req->getCpuNum() &&
98                    threadNum == req->getThreadNum());
99        }
100
101        LockedAddr(Request *req)
102            : addr(mask(req->getPaddr())),
103              cpuNum(req->getCpuNum()),
104              threadNum(req->getThreadNum())
105        {
106        }
107    };
108
109    std::list<LockedAddr> lockedAddrList;
110
111    // helper function for checkLockedAddrs(): we really want to
112    // inline a quick check for an empty locked addr list (hopefully
113    // the common case), and do the full list search (if necessary) in
114    // this out-of-line function
115    bool checkLockedAddrList(PacketPtr pkt);
116
117    // Record the address of a load-locked operation so that we can
118    // clear the execution context's lock flag if a matching store is
119    // performed
120    void trackLoadLocked(PacketPtr pkt);
121
122    // Compare a store address with any locked addresses so we can
123    // clear the lock flag appropriately.  Return value set to 'false'
124    // if store operation should be suppressed (because it was a
125    // conditional store and the address was no longer locked by the
126    // requesting execution context), 'true' otherwise.  Note that
127    // this method must be called on *all* stores since even
128    // non-conditional stores must clear any matching lock addresses.
129    bool writeOK(PacketPtr pkt) {
130        Request *req = pkt->req;
131        if (lockedAddrList.empty()) {
132            // no locked addrs: nothing to check, store_conditional fails
133            bool isLocked = pkt->isLocked();
134            if (isLocked) {
135                req->setExtraData(0);
136            }
137            return !isLocked; // only do write if not an sc
138        } else {
139            // iterate over list...
140            return checkLockedAddrList(pkt);
141        }
142    }
143
144    uint8_t *pmemAddr;
145    int pagePtr;
146    Tick lat;
147    std::vector<MemoryPort*> ports;
148    typedef std::vector<MemoryPort*>::iterator PortIterator;
149
150  public:
151    Addr new_page();
152    uint64_t size() { return params()->addrRange.size(); }
153    uint64_t start() { return params()->addrRange.start; }
154
155    struct Params
156    {
157        std::string name;
158        Range<Addr> addrRange;
159        Tick latency;
160        bool zero;
161    };
162
163  protected:
164    Params *_params;
165
166  public:
167    const Params *params() const { return _params; }
168    PhysicalMemory(Params *p);
169    virtual ~PhysicalMemory();
170
171  public:
172    int deviceBlockSize();
173    void getAddressRanges(AddrRangeList &resp, bool &snoop);
174    virtual Port *getPort(const std::string &if_name, int idx = -1);
175    void virtual init();
176    unsigned int drain(Event *de);
177
178  protected:
179    Tick doAtomicAccess(PacketPtr pkt);
180    void doFunctionalAccess(PacketPtr pkt);
181    virtual Tick calculateLatency(PacketPtr pkt);
182    void recvStatusChange(Port::Status status);
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