port.hh revision 2632:1bb2f91485ea
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
29/**
30 * @file
31 * Port Object Decleration. Ports are used to interface memory objects to
32 * each other.  They will always come in pairs, and we refer to the other
33 * port object as the peer.  These are used to make the design more
34 * modular so that a specific interface between every type of objcet doesn't
35 * have to be created.
36 */
37
38#ifndef __MEM_PORT_HH__
39#define __MEM_PORT_HH__
40
41#include <list>
42#include <inttypes.h>
43
44#include "base/misc.hh"
45#include "base/range.hh"
46#include "mem/packet.hh"
47#include "mem/request.hh"
48
49/** This typedef is used to clean up the parameter list of
50 * getDeviceAddressRanges() and getPeerAddressRanges().  It's declared
51 * outside the Port object since it's also used by some mem objects.
52 * Eventually we should move this typedef to wherever Addr is
53 * defined.
54 */
55
56typedef std::list<Range<Addr> > AddrRangeList;
57typedef std::list<Range<Addr> >::iterator AddrRangeIter;
58
59/**
60 * Ports are used to interface memory objects to
61 * each other.  They will always come in pairs, and we refer to the other
62 * port object as the peer.  These are used to make the design more
63 * modular so that a specific interface between every type of objcet doesn't
64 * have to be created.
65 *
66 * Recv accesor functions are being called from the peer interface.
67 * Send accessor functions are being called from the device the port is
68 * associated with, and it will call the peer recv. accessor function.
69 */
70class Port
71{
72  public:
73
74    virtual ~Port() {};
75    // mey be better to use subclasses & RTTI?
76    /** Holds the ports status.  Keeps track if it is blocked, or has
77        calculated a range change. */
78    enum Status {
79        Blocked,
80        Unblocked,
81        RangeChange
82    };
83
84  private:
85
86    /** A pointer to the peer port.  Ports always come in pairs, that way they
87        can use a standardized interface to communicate between different
88        memory objects. */
89    Port *peer;
90
91  public:
92
93    /** Function to set the pointer for the peer port.
94        @todo should be called by the configuration stuff (python).
95    */
96    void setPeer(Port *port) { peer = port; }
97
98        /** Function to set the pointer for the peer port.
99        @todo should be called by the configuration stuff (python).
100    */
101    Port *getPeer() { return peer; }
102
103  protected:
104
105    /** These functions are protected because they should only be
106     * called by a peer port, never directly by any outside object. */
107
108    /** Called to recive a timing call from the peer port. */
109    virtual bool recvTiming(Packet *pkt) = 0;
110
111    /** Called to recive a atomic call from the peer port. */
112    virtual Tick recvAtomic(Packet *pkt) = 0;
113
114    /** Called to recive a functional call from the peer port. */
115    virtual void recvFunctional(Packet *pkt) = 0;
116
117    /** Called to recieve a status change from the peer port. */
118    virtual void recvStatusChange(Status status) = 0;
119
120    /** Called by a peer port if the send was unsuccesful, and had to
121        wait.  This shouldn't be valid for response paths (IO Devices).
122        so it is set to panic if it isn't already defined.
123    */
124    virtual Packet *recvRetry() { panic("??"); }
125
126    /** Called by a peer port in order to determine the block size of the
127        device connected to this port.  It sometimes doesn't make sense for
128        this function to be called, a DMA interface doesn't really have a
129        block size, so it is defaulted to a panic.
130    */
131    virtual int deviceBlockSize() { panic("??"); }
132
133    /** The peer port is requesting us to reply with a list of the ranges we
134        are responsible for.
135        @param resp is a list of ranges responded to
136        @param snoop is a list of ranges snooped
137    */
138    virtual void getDeviceAddressRanges(AddrRangeList &resp,
139            AddrRangeList &snoop)
140    { panic("??"); }
141
142  public:
143
144    /** Function called by associated memory device (cache, memory, iodevice)
145        in order to send a timing request to the port.  Simply calls the peer
146        port receive function.
147        @return This function returns if the send was succesful in it's
148        recieve. If it was a failure, then the port will wait for a recvRetry
149        at which point it can issue a successful sendTiming.  This is used in
150        case a cache has a higher priority request come in while waiting for
151        the bus to arbitrate.
152    */
153    bool sendTiming(Packet *pkt) { return peer->recvTiming(pkt); }
154
155    /** Function called by the associated device to send an atomic access,
156        an access in which the data is moved and the state is updated in one
157        cycle, without interleaving with other memory accesses.
158    */
159    Tick sendAtomic(Packet *pkt)
160        { return peer->recvAtomic(pkt); }
161
162    /** Function called by the associated device to send a functional access,
163        an access in which the data is instantly updated everywhere in the
164        memory system, without affecting the current state of any block or
165        moving the block.
166    */
167    void sendFunctional(Packet *pkt)
168        { return peer->recvFunctional(pkt); }
169
170    /** Called by the associated device to send a status change to the device
171        connected to the peer interface.
172    */
173    void sendStatusChange(Status status) {peer->recvStatusChange(status); }
174
175    /** When a timing access doesn't return a success, some time later the
176        Retry will be sent.
177    */
178    Packet *sendRetry() { return peer->recvRetry(); }
179
180    /** Called by the associated device if it wishes to find out the blocksize
181        of the device on attached to the peer port.
182    */
183    int peerBlockSize() { return peer->deviceBlockSize(); }
184
185    /** Called by the associated device if it wishes to find out the address
186        ranges connected to the peer ports devices.
187    */
188    void getPeerAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
189    { peer->getDeviceAddressRanges(resp, snoop); }
190
191    /** This function is a wrapper around sendFunctional()
192        that breaks a larger, arbitrarily aligned access into
193        appropriate chunks.  The default implementation can use
194        getBlockSize() to determine the block size and go from there.
195    */
196    virtual void readBlob(Addr addr, uint8_t *p, int size);
197
198    /** This function is a wrapper around sendFunctional()
199        that breaks a larger, arbitrarily aligned access into
200        appropriate chunks.  The default implementation can use
201        getBlockSize() to determine the block size and go from there.
202    */
203    virtual void writeBlob(Addr addr, uint8_t *p, int size);
204
205    /** Fill size bytes starting at addr with byte value val.  This
206        should not need to be virtual, since it can be implemented in
207        terms of writeBlob().  However, it shouldn't be
208        performance-critical either, so it could be if we wanted to.
209    */
210    virtual void memsetBlob(Addr addr, uint8_t val, int size);
211
212  private:
213
214    /** Internal helper function for read/writeBlob().
215     */
216    void blobHelper(Addr addr, uint8_t *p, int size, Command cmd);
217};
218
219/** A simple functional port that is only meant for one way communication to
220 * physical memory. It is only meant to be used to load data into memory before
221 * the simulation begins.
222 */
223
224class FunctionalPort : public Port
225{
226  public:
227    virtual bool recvTiming(Packet *pkt) { panic("FuncPort is UniDir"); }
228    virtual Tick recvAtomic(Packet *pkt) { panic("FuncPort is UniDir"); }
229    virtual void recvFunctional(Packet *pkt) { panic("FuncPort is UniDir"); }
230    virtual void recvStatusChange(Status status) {}
231
232    template <typename T>
233    inline void write(Addr addr, T d)
234    {
235        writeBlob(addr, (uint8_t*)&d, sizeof(T));
236    }
237
238    template <typename T>
239    inline T read(Addr addr)
240    {
241        T d;
242        readBlob(addr, (uint8_t*)&d, sizeof(T));
243        return d;
244    }
245};
246
247#endif //__MEM_PORT_HH__
248