port.hh revision 6227
14762Snate@binkert.org/*
24762Snate@binkert.org * Copyright (c) 2002-2005 The Regents of The University of Michigan
34762Snate@binkert.org * All rights reserved.
44762Snate@binkert.org *
54762Snate@binkert.org * Redistribution and use in source and binary forms, with or without
64762Snate@binkert.org * modification, are permitted provided that the following conditions are
74762Snate@binkert.org * met: redistributions of source code must retain the above copyright
84762Snate@binkert.org * notice, this list of conditions and the following disclaimer;
94762Snate@binkert.org * redistributions in binary form must reproduce the above copyright
104762Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
114762Snate@binkert.org * documentation and/or other materials provided with the distribution;
124762Snate@binkert.org * neither the name of the copyright holders nor the names of its
134762Snate@binkert.org * contributors may be used to endorse or promote products derived from
144762Snate@binkert.org * this software without specific prior written permission.
154762Snate@binkert.org *
164762Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174762Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184762Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194762Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204762Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214762Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224762Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234762Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244762Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254762Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264762Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274762Snate@binkert.org *
284762Snate@binkert.org * Authors: Ron Dreslinski
294762Snate@binkert.org */
304762Snate@binkert.org
314762Snate@binkert.org/**
324762Snate@binkert.org * @file
334762Snate@binkert.org * Port Object Declaration. Ports are used to interface memory objects to
344762Snate@binkert.org * each other.  They will always come in pairs, and we refer to the other
35 * port object as the peer.  These are used to make the design more
36 * modular so that a specific interface between every type of objcet doesn't
37 * have to be created.
38 */
39
40#ifndef __MEM_PORT_HH__
41#define __MEM_PORT_HH__
42
43#include <list>
44
45#include "base/misc.hh"
46#include "base/range.hh"
47#include "base/types.hh"
48#include "mem/packet.hh"
49#include "mem/request.hh"
50#include "sim/eventq.hh"
51
52/** This typedef is used to clean up the parameter list of
53 * getDeviceAddressRanges() and getPeerAddressRanges().  It's declared
54 * outside the Port object since it's also used by some mem objects.
55 * Eventually we should move this typedef to wherever Addr is
56 * defined.
57 */
58
59typedef std::list<Range<Addr> > AddrRangeList;
60typedef std::list<Range<Addr> >::iterator AddrRangeIter;
61
62class EventQueue;
63class MemObject;
64
65/**
66 * Ports are used to interface memory objects to
67 * each other.  They will always come in pairs, and we refer to the other
68 * port object as the peer.  These are used to make the design more
69 * modular so that a specific interface between every type of objcet doesn't
70 * have to be created.
71 *
72 * Recv accesor functions are being called from the peer interface.
73 * Send accessor functions are being called from the device the port is
74 * associated with, and it will call the peer recv. accessor function.
75 */
76class Port : public EventManager
77{
78  protected:
79    /** Descriptive name (for DPRINTF output) */
80    mutable std::string portName;
81
82    /** A pointer to the peer port.  Ports always come in pairs, that way they
83        can use a standardized interface to communicate between different
84        memory objects. */
85    Port *peer;
86
87    /** A pointer to the MemObject that owns this port. This may not be set. */
88    MemObject *owner;
89
90  public:
91    /**
92     * Constructor.
93     *
94     * @param _name Port name for DPRINTF output.  Should include name
95     * of memory system object to which the port belongs.
96     * @param _owner Pointer to the MemObject that owns this port.
97     * Will not necessarily be set.
98     */
99    Port(const std::string &_name, MemObject *_owner);
100
101    /** Return port name (for DPRINTF). */
102    const std::string &name() const { return portName; }
103
104    virtual ~Port();
105
106    // mey be better to use subclasses & RTTI?
107    /** Holds the ports status.  Currently just that a range recomputation needs
108     * to be done. */
109    enum Status {
110        RangeChange
111    };
112
113    void setName(const std::string &name)
114    { portName = name; }
115
116    /** Function to set the pointer for the peer port. */
117    virtual void setPeer(Port *port);
118
119    /** Function to get the pointer to the peer port. */
120    Port *getPeer() { return peer; }
121
122    /** Function to set the owner of this port. */
123    void setOwner(MemObject *_owner);
124
125    /** Function to return the owner of this port. */
126    MemObject *getOwner() { return owner; }
127
128    /** Inform the peer port to delete itself and notify it's owner about it's
129     * demise. */
130    void removeConn();
131
132    virtual bool isDefaultPort() const { return false; }
133
134    bool isConnected() { return peer && !peer->isDefaultPort(); }
135
136  protected:
137
138    /** These functions are protected because they should only be
139     * called by a peer port, never directly by any outside object. */
140
141    /** Called to recive a timing call from the peer port. */
142    virtual bool recvTiming(PacketPtr pkt) = 0;
143
144    /** Called to recive a atomic call from the peer port. */
145    virtual Tick recvAtomic(PacketPtr pkt) = 0;
146
147    /** Called to recive a functional call from the peer port. */
148    virtual void recvFunctional(PacketPtr pkt) = 0;
149
150    /** Called to recieve a status change from the peer port. */
151    virtual void recvStatusChange(Status status) = 0;
152
153    /** Called by a peer port if the send was unsuccesful, and had to
154        wait.  This shouldn't be valid for response paths (IO Devices).
155        so it is set to panic if it isn't already defined.
156    */
157    virtual void recvRetry() { panic("??"); }
158
159    /** Called by a peer port in order to determine the block size of the
160        device connected to this port.  It sometimes doesn't make sense for
161        this function to be called, so it just returns 0. Anytthing that is
162        concerned with the size should just ignore that.
163    */
164    virtual unsigned deviceBlockSize() const { return 0; }
165
166    /** The peer port is requesting us to reply with a list of the ranges we
167        are responsible for.
168        @param resp is a list of ranges responded to
169        @param snoop is a list of ranges snooped
170    */
171    virtual void getDeviceAddressRanges(AddrRangeList &resp,
172                                        bool &snoop)
173    { panic("??"); }
174
175  public:
176
177    /** Function called by associated memory device (cache, memory, iodevice)
178        in order to send a timing request to the port.  Simply calls the peer
179        port receive function.
180        @return This function returns if the send was succesful in it's
181        recieve. If it was a failure, then the port will wait for a recvRetry
182        at which point it can possibly issue a successful sendTiming.  This is used in
183        case a cache has a higher priority request come in while waiting for
184        the bus to arbitrate.
185    */
186    bool sendTiming(PacketPtr pkt) { return peer->recvTiming(pkt); }
187
188    /** Function called by the associated device to send an atomic
189     *   access, an access in which the data is moved and the state is
190     *   updated in one cycle, without interleaving with other memory
191     *   accesses.  Returns estimated latency of access.
192     */
193    Tick sendAtomic(PacketPtr pkt)
194        { return peer->recvAtomic(pkt); }
195
196    /** Function called by the associated device to send a functional access,
197        an access in which the data is instantly updated everywhere in the
198        memory system, without affecting the current state of any block or
199        moving the block.
200    */
201    void sendFunctional(PacketPtr pkt)
202        { return peer->recvFunctional(pkt); }
203
204    /** Called by the associated device to send a status change to the device
205        connected to the peer interface.
206    */
207    void sendStatusChange(Status status) {peer->recvStatusChange(status); }
208
209    /** When a timing access doesn't return a success, some time later the
210        Retry will be sent.
211    */
212    void sendRetry() { return peer->recvRetry(); }
213
214    /** Called by the associated device if it wishes to find out the blocksize
215        of the device on attached to the peer port.
216    */
217    unsigned peerBlockSize() const { return peer->deviceBlockSize(); }
218
219    /** Called by the associated device if it wishes to find out the address
220        ranges connected to the peer ports devices.
221    */
222    void getPeerAddressRanges(AddrRangeList &resp, bool &snoop)
223    { peer->getDeviceAddressRanges(resp, snoop); }
224
225    /** This function is a wrapper around sendFunctional()
226        that breaks a larger, arbitrarily aligned access into
227        appropriate chunks.  The default implementation can use
228        getBlockSize() to determine the block size and go from there.
229    */
230    virtual void readBlob(Addr addr, uint8_t *p, int size);
231
232    /** This function is a wrapper around sendFunctional()
233        that breaks a larger, arbitrarily aligned access into
234        appropriate chunks.  The default implementation can use
235        getBlockSize() to determine the block size and go from there.
236    */
237    virtual void writeBlob(Addr addr, uint8_t *p, int size);
238
239    /** Fill size bytes starting at addr with byte value val.  This
240        should not need to be virtual, since it can be implemented in
241        terms of writeBlob().  However, it shouldn't be
242        performance-critical either, so it could be if we wanted to.
243    */
244    virtual void memsetBlob(Addr addr, uint8_t val, int size);
245
246    /** Inject a PrintReq for the given address to print the state of
247     * that address throughout the memory system.  For debugging.
248     */
249    void printAddr(Addr a);
250
251  private:
252
253    /** Internal helper function for read/writeBlob().
254     */
255    void blobHelper(Addr addr, uint8_t *p, int size, MemCmd cmd);
256};
257
258/** A simple functional port that is only meant for one way communication to
259 * physical memory. It is only meant to be used to load data into memory before
260 * the simulation begins.
261 */
262
263class FunctionalPort : public Port
264{
265  public:
266    FunctionalPort(const std::string &_name, MemObject *_owner = NULL)
267        : Port(_name, _owner)
268    {}
269
270  protected:
271    virtual bool recvTiming(PacketPtr pkt) { panic("FuncPort is UniDir");
272        M5_DUMMY_RETURN }
273    virtual Tick recvAtomic(PacketPtr pkt) { panic("FuncPort is UniDir");
274        M5_DUMMY_RETURN }
275    virtual void recvFunctional(PacketPtr pkt) { panic("FuncPort is UniDir"); }
276    virtual void recvStatusChange(Status status) {}
277
278  public:
279    /** a write function that also does an endian conversion. */
280    template <typename T>
281    inline void writeHtoG(Addr addr, T d);
282
283    /** a read function that also does an endian conversion. */
284    template <typename T>
285    inline T readGtoH(Addr addr);
286
287    template <typename T>
288    inline void write(Addr addr, T d)
289    {
290        writeBlob(addr, (uint8_t*)&d, sizeof(T));
291    }
292
293    template <typename T>
294    inline T read(Addr addr)
295    {
296        T d;
297        readBlob(addr, (uint8_t*)&d, sizeof(T));
298        return d;
299    }
300};
301
302#endif //__MEM_PORT_HH__
303