io_device.hh revision 8711
1545SN/A/*
21762SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
3545SN/A * All rights reserved.
4545SN/A *
5545SN/A * Redistribution and use in source and binary forms, with or without
6545SN/A * modification, are permitted provided that the following conditions are
7545SN/A * met: redistributions of source code must retain the above copyright
8545SN/A * notice, this list of conditions and the following disclaimer;
9545SN/A * redistributions in binary form must reproduce the above copyright
10545SN/A * notice, this list of conditions and the following disclaimer in the
11545SN/A * documentation and/or other materials provided with the distribution;
12545SN/A * neither the name of the copyright holders nor the names of its
13545SN/A * contributors may be used to endorse or promote products derived from
14545SN/A * this software without specific prior written permission.
15545SN/A *
16545SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17545SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18545SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19545SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20545SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21545SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22545SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23545SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24545SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25545SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26545SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Ali Saidi
292665Ssaidi@eecs.umich.edu *          Nathan Binkert
30545SN/A */
31545SN/A
321310SN/A#ifndef __DEV_IO_DEVICE_HH__
331310SN/A#define __DEV_IO_DEVICE_HH__
34545SN/A
355386Sstever@gmail.com#include "base/fast_alloc.hh"
362542SN/A#include "mem/mem_object.hh"
373348Sbinkertn@umich.edu#include "mem/packet.hh"
383348Sbinkertn@umich.edu#include "mem/tport.hh"
394762Snate@binkert.org#include "params/BasicPioDevice.hh"
404762Snate@binkert.org#include "params/DmaDevice.hh"
414762Snate@binkert.org#include "params/PioDevice.hh"
422489SN/A#include "sim/sim_object.hh"
43545SN/A
443090Sstever@eecs.umich.educlass Event;
451310SN/Aclass Platform;
462384SN/Aclass PioDevice;
472489SN/Aclass DmaDevice;
482522SN/Aclass System;
49545SN/A
502489SN/A/**
512489SN/A * The PioPort class is a programmed i/o port that all devices that are
522489SN/A * sensitive to an address range use. The port takes all the memory
532489SN/A * access types and roles them into one read() and write() call that the device
548711Sandreas.hansson@arm.com * must respond to. The device must also provide getAddrRanges() function
553090Sstever@eecs.umich.edu * with which it returns the address ranges it is interested in.
563090Sstever@eecs.umich.edu */
572914Ssaidi@eecs.umich.educlass PioPort : public SimpleTimingPort
58545SN/A{
59545SN/A  protected:
602489SN/A    /** The device that this port serves. */
612384SN/A    PioDevice *device;
622384SN/A
633349Sbinkertn@umich.edu    virtual Tick recvAtomic(PacketPtr pkt);
642384SN/A
658711Sandreas.hansson@arm.com    virtual AddrRangeList getAddrRanges();
662384SN/A
672384SN/A  public:
683091Sstever@eecs.umich.edu
692901Ssaidi@eecs.umich.edu    PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
702384SN/A};
712384SN/A
722565SN/A
732384SN/Aclass DmaPort : public Port
742384SN/A{
752384SN/A  protected:
765386Sstever@gmail.com    struct DmaReqState : public Packet::SenderState, public FastAlloc
772784Ssaidi@eecs.umich.edu    {
782784Ssaidi@eecs.umich.edu        /** Event to call on the device when this transaction (all packets)
792784Ssaidi@eecs.umich.edu         * complete. */
802784Ssaidi@eecs.umich.edu        Event *completionEvent;
812784Ssaidi@eecs.umich.edu
822784Ssaidi@eecs.umich.edu        /** Where we came from for some sanity checking. */
832784Ssaidi@eecs.umich.edu        Port *outPort;
842784Ssaidi@eecs.umich.edu
852784Ssaidi@eecs.umich.edu        /** Total number of bytes that this transaction involves. */
862784Ssaidi@eecs.umich.edu        Addr totBytes;
872784Ssaidi@eecs.umich.edu
882784Ssaidi@eecs.umich.edu        /** Number of bytes that have been acked for this transaction. */
892784Ssaidi@eecs.umich.edu        Addr numBytes;
902784Ssaidi@eecs.umich.edu
915534Ssaidi@eecs.umich.edu        /** Amount to delay completion of dma by */
925534Ssaidi@eecs.umich.edu        Tick delay;
935534Ssaidi@eecs.umich.edu
947403SAli.Saidi@ARM.com
955534Ssaidi@eecs.umich.edu        DmaReqState(Event *ce, Port *p, Addr tb, Tick _delay)
965534Ssaidi@eecs.umich.edu            : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0),
975534Ssaidi@eecs.umich.edu              delay(_delay)
982784Ssaidi@eecs.umich.edu        {}
992784Ssaidi@eecs.umich.edu    };
1002784Ssaidi@eecs.umich.edu
1017403SAli.Saidi@ARM.com    MemObject *device;
1023349Sbinkertn@umich.edu    std::list<PacketPtr> transmitList;
1032384SN/A
1042901Ssaidi@eecs.umich.edu    /** The system that device/port are in. This is used to select which mode
1052565SN/A     * we are currently operating in. */
1062901Ssaidi@eecs.umich.edu    System *sys;
1072565SN/A
1082565SN/A    /** Number of outstanding packets the dma port has. */
1092565SN/A    int pendingCount;
1102384SN/A
1112901Ssaidi@eecs.umich.edu    /** If a dmaAction is in progress. */
1122901Ssaidi@eecs.umich.edu    int actionInProgress;
1132901Ssaidi@eecs.umich.edu
1142901Ssaidi@eecs.umich.edu    /** If we need to drain, keep the drain event around until we're done
1152901Ssaidi@eecs.umich.edu     * here.*/
1162901Ssaidi@eecs.umich.edu    Event *drainEvent;
1172901Ssaidi@eecs.umich.edu
1184435Ssaidi@eecs.umich.edu    /** time to wait between sending another packet, increases as NACKs are
1194435Ssaidi@eecs.umich.edu     * recived, decreases as responses are recived. */
1204435Ssaidi@eecs.umich.edu    Tick backoffTime;
1214435Ssaidi@eecs.umich.edu
1227403SAli.Saidi@ARM.com    /** Minimum time that device should back off for after failed sendTiming */
1237403SAli.Saidi@ARM.com    Tick minBackoffDelay;
1247403SAli.Saidi@ARM.com
1257403SAli.Saidi@ARM.com    /** Maximum time that device should back off for after failed sendTiming */
1267403SAli.Saidi@ARM.com    Tick maxBackoffDelay;
1277403SAli.Saidi@ARM.com
1284435Ssaidi@eecs.umich.edu    /** If the port is currently waiting for a retry before it can send whatever
1294435Ssaidi@eecs.umich.edu     * it is that it's sending. */
1304435Ssaidi@eecs.umich.edu    bool inRetry;
1314435Ssaidi@eecs.umich.edu
1328630SMitchell.Hayenga@ARM.com    /** Port accesses a cache which requires snooping */
1338630SMitchell.Hayenga@ARM.com    bool recvSnoops;
1348630SMitchell.Hayenga@ARM.com
1353349Sbinkertn@umich.edu    virtual bool recvTiming(PacketPtr pkt);
1363349Sbinkertn@umich.edu    virtual Tick recvAtomic(PacketPtr pkt)
1378630SMitchell.Hayenga@ARM.com    {
1388630SMitchell.Hayenga@ARM.com        if (recvSnoops) return 0;
1398630SMitchell.Hayenga@ARM.com
1408630SMitchell.Hayenga@ARM.com        panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN
1418630SMitchell.Hayenga@ARM.com    }
1423349Sbinkertn@umich.edu    virtual void recvFunctional(PacketPtr pkt)
1438630SMitchell.Hayenga@ARM.com    {
1448630SMitchell.Hayenga@ARM.com        if (recvSnoops) return;
1458630SMitchell.Hayenga@ARM.com
1468630SMitchell.Hayenga@ARM.com        panic("dma port shouldn't be used for pio access.");
1478630SMitchell.Hayenga@ARM.com    }
1482384SN/A
1498711Sandreas.hansson@arm.com    virtual void recvRangeChange()
1508630SMitchell.Hayenga@ARM.com    {
1518711Sandreas.hansson@arm.com        // DMA port is a master with a single slave so there is no choice and
1528711Sandreas.hansson@arm.com        // thus no need to worry about any address changes
1538630SMitchell.Hayenga@ARM.com    }
1542384SN/A
1552657Ssaidi@eecs.umich.edu    virtual void recvRetry() ;
1562384SN/A
1578711Sandreas.hansson@arm.com    virtual bool isSnooping()
1588711Sandreas.hansson@arm.com    { return recvSnoops; }
1592384SN/A
1604435Ssaidi@eecs.umich.edu    void queueDma(PacketPtr pkt, bool front = false);
1614435Ssaidi@eecs.umich.edu    void sendDma();
1624435Ssaidi@eecs.umich.edu
1634435Ssaidi@eecs.umich.edu    /** event to give us a kick every time we backoff time is reached. */
1644435Ssaidi@eecs.umich.edu    EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent;
1652489SN/A
1662384SN/A  public:
1678630SMitchell.Hayenga@ARM.com    DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff,
1688630SMitchell.Hayenga@ARM.com            bool recv_snoops = false);
1692565SN/A
1702641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1717607SGene.Wu@arm.com                   uint8_t *data, Tick delay, Request::Flags flag = 0);
1722565SN/A
1732565SN/A    bool dmaPending() { return pendingCount > 0; }
1742384SN/A
1756227Snate@binkert.org    unsigned cacheBlockSize() const { return peerBlockSize(); }
1762901Ssaidi@eecs.umich.edu    unsigned int drain(Event *de);
1772384SN/A};
1782384SN/A
1792489SN/A/**
1802489SN/A * This device is the base class which all devices senstive to an address range
1812489SN/A * inherit from. There are three pure virtual functions which all devices must
1828711Sandreas.hansson@arm.com * implement getAddrRanges(), read(), and write(). The magic do choose which
1832489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to
1842489SN/A * bother.
1852489SN/A */
1862542SN/Aclass PioDevice : public MemObject
1872384SN/A{
1882384SN/A  protected:
1892384SN/A
1902489SN/A    /** The platform we are in. This is used to decide what type of memory
1912489SN/A     * transaction we should perform. */
1921310SN/A    Platform *platform;
1932384SN/A
1942901Ssaidi@eecs.umich.edu    System *sys;
1952901Ssaidi@eecs.umich.edu
1962489SN/A    /** The pioPort that handles the requests for us and provides us requests
1972489SN/A     * that it sees. */
1982384SN/A    PioPort *pioPort;
1992384SN/A
2008711Sandreas.hansson@arm.com    /**
2018711Sandreas.hansson@arm.com     * Every PIO device is obliged to provide an implementation that
2028711Sandreas.hansson@arm.com     * returns the address ranges the device responds to.
2038711Sandreas.hansson@arm.com     *
2048711Sandreas.hansson@arm.com     * @return a list of non-overlapping address ranges
2058711Sandreas.hansson@arm.com     */
2068711Sandreas.hansson@arm.com    virtual AddrRangeList getAddrRanges() = 0;
2072384SN/A
2083090Sstever@eecs.umich.edu    /** Pure virtual function that the device must implement. Called
2093090Sstever@eecs.umich.edu     * when a read command is recieved by the port.
2102523SN/A     * @param pkt Packet describing this request
2112523SN/A     * @return number of ticks it took to complete
2122523SN/A     */
2133349Sbinkertn@umich.edu    virtual Tick read(PacketPtr pkt) = 0;
2142384SN/A
2152489SN/A    /** Pure virtual function that the device must implement. Called when a
2162523SN/A     * write command is recieved by the port.
2172523SN/A     * @param pkt Packet describing this request
2182523SN/A     * @return number of ticks it took to complete
2192523SN/A     */
2203349Sbinkertn@umich.edu    virtual Tick write(PacketPtr pkt) = 0;
221545SN/A
222545SN/A  public:
2234762Snate@binkert.org    typedef PioDeviceParams Params;
2244762Snate@binkert.org    PioDevice(const Params *p);
2254762Snate@binkert.org    virtual ~PioDevice();
2264762Snate@binkert.org
2274762Snate@binkert.org    const Params *
2284762Snate@binkert.org    params() const
2292512SN/A    {
2304762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2314762Snate@binkert.org    }
2322384SN/A
2332541SN/A    virtual void init();
2342541SN/A
2352901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2362901Ssaidi@eecs.umich.edu
2378598Ssteve.reinhardt@amd.com    virtual Port *getPort(const std::string &if_name, int idx = -1);
2388598Ssteve.reinhardt@amd.com
2392489SN/A    friend class PioPort;
2402489SN/A
241545SN/A};
242545SN/A
2432512SN/Aclass BasicPioDevice : public PioDevice
2442512SN/A{
2452512SN/A  protected:
2462512SN/A    /** Address that the device listens to. */
2472512SN/A    Addr pioAddr;
2482512SN/A
2492512SN/A    /** Size that the device's address range. */
2502521SN/A    Addr pioSize;
2512512SN/A
2522512SN/A    /** Delay that the device experinces on an access. */
2532512SN/A    Tick pioDelay;
2542512SN/A
2552512SN/A  public:
2564762Snate@binkert.org    typedef BasicPioDeviceParams Params;
2574762Snate@binkert.org    BasicPioDevice(const Params *p);
2584762Snate@binkert.org
2594762Snate@binkert.org    const Params *
2604762Snate@binkert.org    params() const
2614762Snate@binkert.org    {
2624762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2634762Snate@binkert.org    }
2642512SN/A
2658711Sandreas.hansson@arm.com    /**
2668711Sandreas.hansson@arm.com     * Determine the address ranges that this device responds to.
2678711Sandreas.hansson@arm.com     *
2688711Sandreas.hansson@arm.com     * @return a list of non-overlapping address ranges
2692539SN/A     */
2708711Sandreas.hansson@arm.com    virtual AddrRangeList getAddrRanges();
2712539SN/A
2722512SN/A};
2732512SN/A
274545SN/Aclass DmaDevice : public PioDevice
275545SN/A{
2764435Ssaidi@eecs.umich.edu   protected:
2772384SN/A    DmaPort *dmaPort;
278545SN/A
279545SN/A  public:
2804762Snate@binkert.org    typedef DmaDeviceParams Params;
2814762Snate@binkert.org    DmaDevice(const Params *p);
282545SN/A    virtual ~DmaDevice();
2832384SN/A
2844762Snate@binkert.org    const Params *
2854762Snate@binkert.org    params() const
2864762Snate@binkert.org    {
2874762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2884762Snate@binkert.org    }
2894762Snate@binkert.org
2905534Ssaidi@eecs.umich.edu    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
2914022Sstever@eecs.umich.edu    {
2925534Ssaidi@eecs.umich.edu        dmaPort->dmaAction(MemCmd::WriteReq, addr, size, event, data, delay);
2934022Sstever@eecs.umich.edu    }
2942565SN/A
2955534Ssaidi@eecs.umich.edu    void dmaRead(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
2964263Ssaidi@eecs.umich.edu    {
2975534Ssaidi@eecs.umich.edu        dmaPort->dmaAction(MemCmd::ReadReq, addr, size, event, data, delay);
2984263Ssaidi@eecs.umich.edu    }
2992565SN/A
3002565SN/A    bool dmaPending() { return dmaPort->dmaPending(); }
3012565SN/A
3022901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
3032901Ssaidi@eecs.umich.edu
3046227Snate@binkert.org    unsigned cacheBlockSize() const { return dmaPort->cacheBlockSize(); }
3054263Ssaidi@eecs.umich.edu
3068598Ssteve.reinhardt@amd.com    virtual Port *getPort(const std::string &if_name, int idx = -1);
3072489SN/A
3082489SN/A    friend class DmaPort;
309545SN/A};
310545SN/A
3112384SN/A
3121310SN/A#endif // __DEV_IO_DEVICE_HH__
313