port.hh revision 13782
1/*
2 * Copyright (c) 2011-2012,2015,2017 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 "base/addr_range.hh"
54#include "mem/packet.hh"
55#include "sim/port.hh"
56
57class MemObject;
58
59/** Forward declaration */
60class BaseSlavePort;
61
62/**
63 * A BaseMasterPort is a protocol-agnostic master port, responsible
64 * only for the structural connection to a slave port. The final
65 * master port that inherits from the base class must override the
66 * bind member function for the specific slave port class.
67 */
68class BaseMasterPort : public Port
69{
70
71  protected:
72
73    BaseSlavePort* _baseSlavePort;
74
75    BaseMasterPort(const std::string& name, PortID id=InvalidPortID);
76    virtual ~BaseMasterPort();
77
78  public:
79
80    BaseSlavePort& getSlavePort() const;
81
82};
83
84/**
85 * A BaseSlavePort is a protocol-agnostic slave port, responsible
86 * only for the structural connection to a master port.
87 */
88class BaseSlavePort : public Port
89{
90
91  protected:
92
93    BaseMasterPort* _baseMasterPort;
94
95    BaseSlavePort(const std::string& name, PortID id=InvalidPortID);
96    virtual ~BaseSlavePort();
97
98  public:
99
100    BaseMasterPort& getMasterPort() const;
101
102};
103
104/** Forward declaration */
105class SlavePort;
106
107/**
108 * A MasterPort is a specialisation of a BaseMasterPort, which
109 * implements the default protocol for the three different level of
110 * transport functions. In addition to the basic functionality of
111 * sending packets, it also has functions to receive range changes or
112 * determine if the port is snooping or not.
113 */
114class MasterPort : public BaseMasterPort
115{
116
117    friend class SlavePort;
118
119  private:
120
121    SlavePort* _slavePort;
122
123  protected:
124
125    MemObject& owner;
126
127  public:
128
129    MasterPort(const std::string& name, MemObject* _owner,
130               PortID id=InvalidPortID);
131    virtual ~MasterPort();
132
133    /**
134     * Bind this master port to a slave port. This also does the
135     * mirror action and binds the slave port to the master port.
136     */
137    void bind(Port &peer) override;
138
139    /**
140     * Unbind this master port and the associated slave port.
141     */
142    void unbind() override;
143
144    /**
145     * Send an atomic request packet, where the data is moved and the
146     * state is updated in zero time, without interleaving with other
147     * memory accesses.
148     *
149     * @param pkt Packet to send.
150     *
151     * @return Estimated latency of access.
152     */
153    Tick sendAtomic(PacketPtr pkt);
154
155    /**
156     * Send a functional request packet, where the data is instantly
157     * updated everywhere in the memory system, without affecting the
158     * current state of any block or moving the block.
159     *
160     * @param pkt Packet to send.
161     */
162    void sendFunctional(PacketPtr pkt);
163
164    /**
165     * Attempt to send a timing request to the slave port by calling
166     * its corresponding receive function. If the send does not
167     * succeed, as indicated by the return value, then the sender must
168     * wait for a recvReqRetry at which point it can re-issue a
169     * sendTimingReq.
170     *
171     * @param pkt Packet to send.
172     *
173     * @return If the send was succesful or not.
174    */
175    bool sendTimingReq(PacketPtr pkt);
176
177    /**
178     * Check if the slave can handle a timing request.
179     *
180     * If the send cannot be handled at the moment, as indicated by
181     * the return value, then the sender will receive a recvReqRetry
182     * at which point it can re-issue a sendTimingReq.
183     *
184     * @param pkt Packet to send.
185     *
186     * @return If the send was succesful or not.
187     */
188    bool tryTiming(PacketPtr pkt) const;
189
190    /**
191     * Attempt to send a timing snoop response packet to the slave
192     * port by calling its corresponding receive function. If the send
193     * does not succeed, as indicated by the return value, then the
194     * sender must wait for a recvRetrySnoop at which point it can
195     * re-issue a sendTimingSnoopResp.
196     *
197     * @param pkt Packet to send.
198     */
199    bool sendTimingSnoopResp(PacketPtr pkt);
200
201    /**
202     * Send a retry to the slave port that previously attempted a
203     * sendTimingResp to this master port and failed. Note that this
204     * is virtual so that the "fake" snoop response port in the
205     * coherent crossbar can override the behaviour.
206     */
207    virtual void sendRetryResp();
208
209    /**
210     * Determine if this master port is snooping or not. The default
211     * implementation returns false and thus tells the neighbour we
212     * are not snooping. Any master port that wants to receive snoop
213     * requests (e.g. a cache connected to a bus) has to override this
214     * function.
215     *
216     * @return true if the port should be considered a snooper
217     */
218    virtual bool isSnooping() const { return false; }
219
220    /**
221     * Get the address ranges of the connected slave port.
222     */
223    AddrRangeList getAddrRanges() const;
224
225    /** Inject a PrintReq for the given address to print the state of
226     * that address throughout the memory system.  For debugging.
227     */
228    void printAddr(Addr a);
229
230  protected:
231
232    /**
233     * Receive an atomic snoop request packet from the slave port.
234     */
235    virtual Tick recvAtomicSnoop(PacketPtr pkt)
236    {
237        panic("%s was not expecting an atomic snoop request\n", name());
238        return 0;
239    }
240
241    /**
242     * Receive a functional snoop request packet from the slave port.
243     */
244    virtual void recvFunctionalSnoop(PacketPtr pkt)
245    {
246        panic("%s was not expecting a functional snoop request\n", name());
247    }
248
249    /**
250     * Receive a timing response from the slave port.
251     */
252    virtual bool recvTimingResp(PacketPtr pkt) = 0;
253
254    /**
255     * Receive a timing snoop request from the slave port.
256     */
257    virtual void recvTimingSnoopReq(PacketPtr pkt)
258    {
259        panic("%s was not expecting a timing snoop request\n", name());
260    }
261
262    /**
263     * Called by the slave port if sendTimingReq was called on this
264     * master port (causing recvTimingReq to be called on the slave
265     * port) and was unsuccesful.
266     */
267    virtual void recvReqRetry() = 0;
268
269    /**
270     * Called by the slave port if sendTimingSnoopResp was called on this
271     * master port (causing recvTimingSnoopResp to be called on the slave
272     * port) and was unsuccesful.
273     */
274    virtual void recvRetrySnoopResp()
275    {
276        panic("%s was not expecting a snoop retry\n", name());
277    }
278
279    /**
280     * Called to receive an address range change from the peer slave
281     * port. The default implementation ignores the change and does
282     * nothing. Override this function in a derived class if the owner
283     * needs to be aware of the address ranges, e.g. in an
284     * interconnect component like a bus.
285     */
286    virtual void recvRangeChange() { }
287};
288
289/**
290 * A SlavePort is a specialisation of a port. In addition to the
291 * basic functionality of sending packets to its master peer, it also
292 * has functions specific to a slave, e.g. to send range changes
293 * and get the address ranges that the port responds to.
294 */
295class SlavePort : public BaseSlavePort
296{
297
298    friend class MasterPort;
299
300  private:
301
302    MasterPort* _masterPort;
303
304  protected:
305
306    MemObject& owner;
307
308  public:
309
310    SlavePort(const std::string& name, MemObject* _owner,
311              PortID id=InvalidPortID);
312    virtual ~SlavePort();
313
314    /**
315     * Send an atomic snoop request packet, where the data is moved
316     * and the state is updated in zero time, without interleaving
317     * with other memory accesses.
318     *
319     * @param pkt Snoop packet to send.
320     *
321     * @return Estimated latency of access.
322     */
323    Tick sendAtomicSnoop(PacketPtr pkt);
324
325    /**
326     * Send a functional snoop request packet, where the data is
327     * instantly updated everywhere in the memory system, without
328     * affecting the current state of any block or moving the block.
329     *
330     * @param pkt Snoop packet to send.
331     */
332    void sendFunctionalSnoop(PacketPtr pkt);
333
334    /**
335     * Attempt to send a timing response to the master port by calling
336     * its corresponding receive function. If the send does not
337     * succeed, as indicated by the return value, then the sender must
338     * wait for a recvRespRetry at which point it can re-issue a
339     * sendTimingResp.
340     *
341     * @param pkt Packet to send.
342     *
343     * @return If the send was succesful or not.
344    */
345    bool sendTimingResp(PacketPtr pkt);
346
347    /**
348     * Attempt to send a timing snoop request packet to the master port
349     * by calling its corresponding receive function. Snoop requests
350     * always succeed and hence no return value is needed.
351     *
352     * @param pkt Packet to send.
353     */
354    void sendTimingSnoopReq(PacketPtr pkt);
355
356    /**
357     * Send a retry to the master port that previously attempted a
358     * sendTimingReq to this slave port and failed.
359     */
360    void sendRetryReq();
361
362    /**
363     * Send a retry to the master port that previously attempted a
364     * sendTimingSnoopResp to this slave port and failed.
365     */
366    void sendRetrySnoopResp();
367
368    /**
369     * Find out if the peer master port is snooping or not.
370     *
371     * @return true if the peer master port is snooping
372     */
373    bool isSnooping() const { return _masterPort->isSnooping(); }
374
375    /**
376     * Called by the owner to send a range change
377     */
378    void sendRangeChange() const {
379        if (!_masterPort)
380            fatal("%s cannot sendRangeChange() without master port", name());
381        _masterPort->recvRangeChange();
382    }
383
384    /**
385     * Get a list of the non-overlapping address ranges the owner is
386     * responsible for. All slave ports must override this function
387     * and return a populated list with at least one item.
388     *
389     * @return a list of ranges responded to
390     */
391    virtual AddrRangeList getAddrRanges() const = 0;
392
393    /**
394     * We let the master port do the work, so these don't do anything.
395     */
396    void unbind() override {}
397    void bind(Port &peer) override {}
398
399  protected:
400
401    /**
402     * Called by the master port to unbind. Should never be called
403     * directly.
404     */
405    void slaveUnbind();
406
407    /**
408     * Called by the master port to bind. Should never be called
409     * directly.
410     */
411    void slaveBind(MasterPort& master_port);
412
413    /**
414     * Receive an atomic request packet from the master port.
415     */
416    virtual Tick recvAtomic(PacketPtr pkt) = 0;
417
418    /**
419     * Receive a functional request packet from the master port.
420     */
421    virtual void recvFunctional(PacketPtr pkt) = 0;
422
423    /**
424     * Receive a timing request from the master port.
425     */
426    virtual bool recvTimingReq(PacketPtr pkt) = 0;
427
428    /**
429     * Availability request from the master port.
430     */
431    virtual bool tryTiming(PacketPtr pkt) {
432        panic("%s was not expecting a %s\n", name(), __func__);
433    }
434
435    /**
436     * Receive a timing snoop response from the master port.
437     */
438    virtual bool recvTimingSnoopResp(PacketPtr pkt)
439    {
440        panic("%s was not expecting a timing snoop response\n", name());
441    }
442
443    /**
444     * Called by the master port if sendTimingResp was called on this
445     * slave port (causing recvTimingResp to be called on the master
446     * port) and was unsuccesful.
447     */
448    virtual void recvRespRetry() = 0;
449
450};
451
452#endif //__MEM_PORT_HH__
453