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