port.hh revision 8948
1/*
2 * Copyright (c) 2011-2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ron Dreslinski
41 *          Andreas Hansson
42 *          William Wang
43 */
44
45/**
46 * @file
47 * Port Object Declaration.
48 */
49
50#ifndef __MEM_PORT_HH__
51#define __MEM_PORT_HH__
52
53#include <list>
54
55#include "base/range.hh"
56#include "mem/packet.hh"
57
58/**
59 * This typedef is used to clean up getAddrRanges(). It's declared
60 * outside the Port object since it's also used by some mem objects.
61 * Eventually we should move this typedef to wherever Addr is
62 * defined.
63 */
64
65typedef std::list<Range<Addr> > AddrRangeList;
66typedef std::list<Range<Addr> >::iterator AddrRangeIter;
67
68class MemObject;
69
70/**
71 * Ports are used to interface memory objects to each other. A port is
72 * either a master or a slave and the connected peer is always of the
73 * opposite role.
74 *
75 * Each port has a name and an owner, and enables three basic types of
76 * accesses to the peer port: sendFunctional, sendAtomic and
77 * sendTiming.
78 */
79class Port
80{
81
82  private:
83
84    /** Descriptive name (for DPRINTF output) */
85    std::string portName;
86
87  protected:
88
89    /** A pointer to the peer port.  */
90    Port* peer;
91
92    /** A reference to the MemObject that owns this port. */
93    MemObject& owner;
94
95    /**
96     * Abstract base class for ports
97     *
98     * @param _name Port name including the owners name
99     * @param _owner The MemObject that is the structural owner of this port
100     */
101    Port(const std::string& _name, MemObject& _owner);
102
103    /**
104     * Virtual destructor due to inheritance.
105     */
106    virtual ~Port();
107
108  public:
109
110    /** Return port name (for DPRINTF). */
111    const std::string name() const { return portName; }
112
113  protected:
114
115    /** These functions are protected because they should only be
116     * called by a peer port, never directly by any outside object. */
117
118    /**
119     * Receive a timing request or response packet from the peer port.
120     */
121    virtual bool recvTiming(PacketPtr pkt) = 0;
122
123    /**
124     * Receive a timing snoop request or snoop response packet from
125     * the peer port.
126     */
127    virtual bool recvTimingSnoop(PacketPtr pkt)
128    {
129        panic("%s was not expecting a timing snoop\n", name());
130        return false;
131    }
132
133    /**
134     * Called by a peer port if sendTiming or sendTimingSnoop was
135     * unsuccesful, and had to wait.
136     */
137    virtual void recvRetry() = 0;
138
139  public:
140
141    /**
142     * Attempt to send a timing request or response packet to the peer
143     * port by calling its receive function. If the send does not
144     * succeed, as indicated by the return value, then the sender must
145     * wait for a recvRetry at which point it can re-issue a
146     * sendTiming.
147     *
148     * @param pkt Packet to send.
149     *
150     * @return If the send was succesful or not.
151    */
152    bool sendTiming(PacketPtr pkt) { return peer->recvTiming(pkt); }
153
154    /**
155     * Attempt to send a timing snoop request or snoop response packet
156     * to the peer port by calling its receive function. If the send
157     * does not succeed, as indicated by the return value, then the
158     * sender must wait for a recvRetry at which point it can re-issue
159     * a sendTimingSnoop.
160     *
161     * @param pkt Packet to send.
162     *
163     * @return If the send was succesful or not.
164    */
165    bool sendTimingSnoop(PacketPtr pkt) { return peer->recvTimingSnoop(pkt); }
166
167    /**
168     * Send a retry to a peer port that previously attempted a
169     * sendTiming or sendTimingSnoop which was unsuccessful.
170     */
171    void sendRetry() { return peer->recvRetry(); }
172
173};
174
175/** Forward declaration */
176class SlavePort;
177
178/**
179 * A MasterPort is a specialisation of a port. In addition to the
180 * basic functionality of sending packets to its slave peer, it also
181 * has functions specific to a master, e.g. to receive range changes
182 * or determine if the port is snooping or not.
183 */
184class MasterPort : public Port
185{
186
187  private:
188
189    SlavePort* _slavePort;
190
191  public:
192
193    MasterPort(const std::string& name, MemObject* owner);
194    virtual ~MasterPort();
195
196    void bind(SlavePort& slave_port);
197    SlavePort& getSlavePort() const;
198    bool isConnected() const;
199
200    /**
201     * Send an atomic request packet, where the data is moved and the
202     * state is updated in zero time, without interleaving with other
203     * memory accesses.
204     *
205     * @param pkt Packet to send.
206     *
207     * @return Estimated latency of access.
208     */
209    Tick sendAtomic(PacketPtr pkt);
210
211    /**
212     * Send a functional request packet, where the data is instantly
213     * updated everywhere in the memory system, without affecting the
214     * current state of any block or moving the block.
215     *
216     * @param pkt Packet to send.
217     */
218    void sendFunctional(PacketPtr pkt);
219
220    /**
221     * Receive an atomic snoop request packet from the slave port.
222     */
223    virtual Tick recvAtomicSnoop(PacketPtr pkt)
224    {
225        panic("%s was not expecting an atomic snoop\n", name());
226        return 0;
227    }
228
229    /**
230     * Receive a functional snoop request packet from the slave port.
231     */
232    virtual void recvFunctionalSnoop(PacketPtr pkt)
233    {
234        panic("%s was not expecting a functional snoop\n", name());
235    }
236
237    /**
238     * Called to receive an address range change from the peer slave
239     * port. the default implementation ignored the change and does
240     * nothing. Override this function in a derived class if the owner
241     * needs to be aware of he laesddress ranges, e.g. in an
242     * interconnect component like a bus.
243     */
244    virtual void recvRangeChange() { }
245
246    /**
247     * Determine if this master port is snooping or not. The default
248     * implementation returns false and thus tells the neighbour we
249     * are not snooping. Any master port that wants to receive snoop
250     * requests (e.g. a cache connected to a bus) has to override this
251     * function.
252     *
253     * @return true if the port should be considered a snooper
254     */
255    virtual bool isSnooping() const { return false; }
256
257    /**
258     * Called by a peer port in order to determine the block size of
259     * the owner of this port.
260     */
261    virtual unsigned deviceBlockSize() const { return 0; }
262
263    /** Called by the associated device if it wishes to find out the blocksize
264        of the device on attached to the peer port.
265    */
266    unsigned peerBlockSize() const;
267
268    /** Inject a PrintReq for the given address to print the state of
269     * that address throughout the memory system.  For debugging.
270     */
271    void printAddr(Addr a);
272};
273
274/**
275 * A SlavePort is a specialisation of a port. In addition to the
276 * basic functionality of sending packets to its master peer, it also
277 * has functions specific to a slave, e.g. to send range changes
278 * and get the address ranges that the port responds to.
279 */
280class SlavePort : public Port
281{
282
283  private:
284
285    MasterPort* _masterPort;
286
287  public:
288
289    SlavePort(const std::string& name, MemObject* owner);
290    virtual ~SlavePort();
291
292    void bind(MasterPort& master_port);
293    MasterPort& getMasterPort() const;
294    bool isConnected() const;
295
296    /**
297     * Send an atomic snoop request packet, where the data is moved
298     * and the state is updated in zero time, without interleaving
299     * with other memory accesses.
300     *
301     * @param pkt Snoop packet to send.
302     *
303     * @return Estimated latency of access.
304     */
305    Tick sendAtomicSnoop(PacketPtr pkt);
306
307    /**
308     * Send a functional snoop request packet, where the data is
309     * instantly updated everywhere in the memory system, without
310     * affecting the current state of any block or moving the block.
311     *
312     * @param pkt Snoop packet to send.
313     */
314    void sendFunctionalSnoop(PacketPtr pkt);
315
316    /**
317     * Receive an atomic request packet from the master port.
318     */
319    virtual Tick recvAtomic(PacketPtr pkt) = 0;
320
321    /**
322     * Receive a functional request packet from the master port.
323     */
324    virtual void recvFunctional(PacketPtr pkt) = 0;
325
326    /**
327     * Called by a peer port in order to determine the block size of
328     * the owner of this port.
329     */
330    virtual unsigned deviceBlockSize() const { return 0; }
331
332    /** Called by the associated device if it wishes to find out the blocksize
333        of the device on attached to the peer port.
334    */
335    unsigned peerBlockSize() const;
336
337    /**
338     * Called by the owner to send a range change
339     */
340    void sendRangeChange() const { _masterPort->recvRangeChange(); }
341
342    /**
343     * Get a list of the non-overlapping address ranges the owner is
344     * responsible for. All slave ports must override this function
345     * and return a populated list with at least one item.
346     *
347     * @return a list of ranges responded to
348     */
349    virtual AddrRangeList getAddrRanges() = 0;
350};
351
352#endif //__MEM_PORT_HH__
353