port.hh revision 4432:5e55857abb01
1/*
2 * Copyright (c) 2002-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/**
32 * @file
33 * Port Object Declaration. Ports are used to interface memory objects to
34 * 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#include <inttypes.h>
45
46#include "base/misc.hh"
47#include "base/range.hh"
48#include "mem/packet.hh"
49#include "mem/request.hh"
50
51/** This typedef is used to clean up the parameter list of
52 * getDeviceAddressRanges() and getPeerAddressRanges().  It's declared
53 * outside the Port object since it's also used by some mem objects.
54 * Eventually we should move this typedef to wherever Addr is
55 * defined.
56 */
57
58typedef std::list<Range<Addr> > AddrRangeList;
59typedef std::list<Range<Addr> >::iterator AddrRangeIter;
60
61class MemObject;
62
63/**
64 * Ports are used to interface memory objects to
65 * each other.  They will always come in pairs, and we refer to the other
66 * port object as the peer.  These are used to make the design more
67 * modular so that a specific interface between every type of objcet doesn't
68 * have to be created.
69 *
70 * Recv accesor functions are being called from the peer interface.
71 * Send accessor functions are being called from the device the port is
72 * associated with, and it will call the peer recv. accessor function.
73 */
74class Port
75{
76  private:
77
78    /** Descriptive name (for DPRINTF output) */
79    mutable std::string portName;
80
81    /** A pointer to the peer port.  Ports always come in pairs, that way they
82        can use a standardized interface to communicate between different
83        memory objects. */
84    Port *peer;
85
86    /** A pointer to the MemObject that owns this port. This may not be set. */
87    MemObject *owner;
88
89  public:
90
91    Port()
92        : peer(NULL), owner(NULL)
93    { }
94
95    /**
96     * Constructor.
97     *
98     * @param _name Port name for DPRINTF output.  Should include name
99     * of memory system object to which the port belongs.
100     * @param _owner Pointer to the MemObject that owns this port.
101     * Will not necessarily be set.
102     */
103    Port(const std::string &_name, MemObject *_owner = NULL)
104        : portName(_name), peer(NULL), owner(_owner)
105    { }
106
107    /** Return port name (for DPRINTF). */
108    const std::string &name() const { return portName; }
109
110    virtual ~Port() {};
111
112    // mey be better to use subclasses & RTTI?
113    /** Holds the ports status.  Currently just that a range recomputation needs
114     * to be done. */
115    enum Status {
116        RangeChange
117    };
118
119    void setName(const std::string &name)
120    { portName = name; }
121
122    /** Function to set the pointer for the peer port. */
123    virtual void setPeer(Port *port);
124
125    /** Function to get the pointer to the peer port. */
126    Port *getPeer() { return peer; }
127
128    /** Function to set the owner of this port. */
129    void setOwner(MemObject *_owner) { owner = _owner; }
130
131    /** Function to return the owner of this port. */
132    MemObject *getOwner() { return owner; }
133
134    /** Inform the peer port to delete itself and notify it's owner about it's
135     * demise. */
136    void removeConn();
137
138
139  protected:
140
141    /** These functions are protected because they should only be
142     * called by a peer port, never directly by any outside object. */
143
144    /** Called to recive a timing call from the peer port. */
145    virtual bool recvTiming(PacketPtr pkt) = 0;
146
147    /** Called to recive a atomic call from the peer port. */
148    virtual Tick recvAtomic(PacketPtr pkt) = 0;
149
150    /** Called to recive a functional call from the peer port. */
151    virtual void recvFunctional(PacketPtr pkt) = 0;
152
153    /** Called to recieve a status change from the peer port. */
154    virtual void recvStatusChange(Status status) = 0;
155
156    /** Called by a peer port if the send was unsuccesful, and had to
157        wait.  This shouldn't be valid for response paths (IO Devices).
158        so it is set to panic if it isn't already defined.
159    */
160    virtual void recvRetry() { panic("??"); }
161
162    /** Called by a peer port in order to determine the block size of the
163        device connected to this port.  It sometimes doesn't make sense for
164        this function to be called, so it just returns 0. Anytthing that is
165        concerned with the size should just ignore that.
166    */
167    virtual int deviceBlockSize() { return 0; }
168
169    /** The peer port is requesting us to reply with a list of the ranges we
170        are responsible for.
171        @param resp is a list of ranges responded to
172        @param snoop is a list of ranges snooped
173    */
174    virtual void getDeviceAddressRanges(AddrRangeList &resp,
175            AddrRangeList &snoop)
176    { panic("??"); }
177
178  public:
179
180    /** Function called by associated memory device (cache, memory, iodevice)
181        in order to send a timing request to the port.  Simply calls the peer
182        port receive function.
183        @return This function returns if the send was succesful in it's
184        recieve. If it was a failure, then the port will wait for a recvRetry
185        at which point it can possibly issue a successful sendTiming.  This is used in
186        case a cache has a higher priority request come in while waiting for
187        the bus to arbitrate.
188    */
189    bool sendTiming(PacketPtr pkt) { return peer->recvTiming(pkt); }
190
191    /** Function called by the associated device to send an atomic
192     *   access, an access in which the data is moved and the state is
193     *   updated in one cycle, without interleaving with other memory
194     *   accesses.  Returns estimated latency of access.
195     */
196    Tick sendAtomic(PacketPtr pkt)
197        { return peer->recvAtomic(pkt); }
198
199    /** Function called by the associated device to send a functional access,
200        an access in which the data is instantly updated everywhere in the
201        memory system, without affecting the current state of any block or
202        moving the block.
203    */
204    void sendFunctional(PacketPtr pkt)
205        { return peer->recvFunctional(pkt); }
206
207    /** Called by the associated device to send a status change to the device
208        connected to the peer interface.
209    */
210    void sendStatusChange(Status status) {peer->recvStatusChange(status); }
211
212    /** When a timing access doesn't return a success, some time later the
213        Retry will be sent.
214    */
215    void sendRetry() { return peer->recvRetry(); }
216
217    /** Called by the associated device if it wishes to find out the blocksize
218        of the device on attached to the peer port.
219    */
220    int peerBlockSize() { return peer->deviceBlockSize(); }
221
222    /** Called by the associated device if it wishes to find out the address
223        ranges connected to the peer ports devices.
224    */
225    void getPeerAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
226    { peer->getDeviceAddressRanges(resp, snoop); }
227
228    /** This function is a wrapper around sendFunctional()
229        that breaks a larger, arbitrarily aligned access into
230        appropriate chunks.  The default implementation can use
231        getBlockSize() to determine the block size and go from there.
232    */
233    virtual void readBlob(Addr addr, uint8_t *p, int size);
234
235    /** This function is a wrapper around sendFunctional()
236        that breaks a larger, arbitrarily aligned access into
237        appropriate chunks.  The default implementation can use
238        getBlockSize() to determine the block size and go from there.
239    */
240    virtual void writeBlob(Addr addr, uint8_t *p, int size);
241
242    /** Fill size bytes starting at addr with byte value val.  This
243        should not need to be virtual, since it can be implemented in
244        terms of writeBlob().  However, it shouldn't be
245        performance-critical either, so it could be if we wanted to.
246    */
247    virtual void memsetBlob(Addr addr, uint8_t val, int size);
248
249  private:
250
251    /** Internal helper function for read/writeBlob().
252     */
253    void blobHelper(Addr addr, uint8_t *p, int size, MemCmd cmd);
254};
255
256/** A simple functional port that is only meant for one way communication to
257 * physical memory. It is only meant to be used to load data into memory before
258 * the simulation begins.
259 */
260
261class FunctionalPort : public Port
262{
263  public:
264    FunctionalPort(const std::string &_name, MemObject *_owner = NULL)
265        : Port(_name, _owner)
266    {}
267
268  protected:
269    virtual bool recvTiming(PacketPtr pkt) { panic("FuncPort is UniDir");
270        M5_DUMMY_RETURN }
271    virtual Tick recvAtomic(PacketPtr pkt) { panic("FuncPort is UniDir");
272        M5_DUMMY_RETURN }
273    virtual void recvFunctional(PacketPtr pkt) { panic("FuncPort is UniDir"); }
274    virtual void recvStatusChange(Status status) {}
275
276  public:
277    /** a write function that also does an endian conversion. */
278    template <typename T>
279    inline void writeHtoG(Addr addr, T d);
280
281    /** a read function that also does an endian conversion. */
282    template <typename T>
283    inline T readGtoH(Addr addr);
284
285    template <typename T>
286    inline void write(Addr addr, T d)
287    {
288        writeBlob(addr, (uint8_t*)&d, sizeof(T));
289    }
290
291    template <typename T>
292    inline T read(Addr addr)
293    {
294        T d;
295        readBlob(addr, (uint8_t*)&d, sizeof(T));
296        return d;
297    }
298};
299
300#endif //__MEM_PORT_HH__
301