io_device.hh revision 8922
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;
452384SN/Aclass PioDevice;
462489SN/Aclass DmaDevice;
472522SN/Aclass System;
48545SN/A
492489SN/A/**
502489SN/A * The PioPort class is a programmed i/o port that all devices that are
512489SN/A * sensitive to an address range use. The port takes all the memory
522489SN/A * access types and roles them into one read() and write() call that the device
538711Sandreas.hansson@arm.com * must respond to. The device must also provide getAddrRanges() function
543090Sstever@eecs.umich.edu * with which it returns the address ranges it is interested in.
553090Sstever@eecs.umich.edu */
562914Ssaidi@eecs.umich.educlass PioPort : public SimpleTimingPort
57545SN/A{
58545SN/A  protected:
592489SN/A    /** The device that this port serves. */
602384SN/A    PioDevice *device;
612384SN/A
623349Sbinkertn@umich.edu    virtual Tick recvAtomic(PacketPtr pkt);
632384SN/A
648711Sandreas.hansson@arm.com    virtual AddrRangeList getAddrRanges();
652384SN/A
662384SN/A  public:
673091Sstever@eecs.umich.edu
688914Sandreas.hansson@arm.com    PioPort(PioDevice *dev);
692384SN/A};
702384SN/A
712565SN/A
728922Swilliam.wang@arm.comclass DmaPort : public MasterPort
732384SN/A{
742384SN/A  protected:
755386Sstever@gmail.com    struct DmaReqState : public Packet::SenderState, public FastAlloc
762784Ssaidi@eecs.umich.edu    {
772784Ssaidi@eecs.umich.edu        /** Event to call on the device when this transaction (all packets)
782784Ssaidi@eecs.umich.edu         * complete. */
792784Ssaidi@eecs.umich.edu        Event *completionEvent;
802784Ssaidi@eecs.umich.edu
812784Ssaidi@eecs.umich.edu        /** Where we came from for some sanity checking. */
822784Ssaidi@eecs.umich.edu        Port *outPort;
832784Ssaidi@eecs.umich.edu
842784Ssaidi@eecs.umich.edu        /** Total number of bytes that this transaction involves. */
852784Ssaidi@eecs.umich.edu        Addr totBytes;
862784Ssaidi@eecs.umich.edu
872784Ssaidi@eecs.umich.edu        /** Number of bytes that have been acked for this transaction. */
882784Ssaidi@eecs.umich.edu        Addr numBytes;
892784Ssaidi@eecs.umich.edu
905534Ssaidi@eecs.umich.edu        /** Amount to delay completion of dma by */
915534Ssaidi@eecs.umich.edu        Tick delay;
925534Ssaidi@eecs.umich.edu
937403SAli.Saidi@ARM.com
945534Ssaidi@eecs.umich.edu        DmaReqState(Event *ce, Port *p, Addr tb, Tick _delay)
955534Ssaidi@eecs.umich.edu            : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0),
965534Ssaidi@eecs.umich.edu              delay(_delay)
972784Ssaidi@eecs.umich.edu        {}
982784Ssaidi@eecs.umich.edu    };
992784Ssaidi@eecs.umich.edu
1007403SAli.Saidi@ARM.com    MemObject *device;
1013349Sbinkertn@umich.edu    std::list<PacketPtr> transmitList;
1022384SN/A
1032901Ssaidi@eecs.umich.edu    /** The system that device/port are in. This is used to select which mode
1042565SN/A     * we are currently operating in. */
1052901Ssaidi@eecs.umich.edu    System *sys;
1062565SN/A
1078832SAli.Saidi@ARM.com    /** Id for all requests */
1088832SAli.Saidi@ARM.com    MasterID masterId;
1098832SAli.Saidi@ARM.com
1102565SN/A    /** Number of outstanding packets the dma port has. */
1112565SN/A    int pendingCount;
1122384SN/A
1132901Ssaidi@eecs.umich.edu    /** If a dmaAction is in progress. */
1142901Ssaidi@eecs.umich.edu    int actionInProgress;
1152901Ssaidi@eecs.umich.edu
1162901Ssaidi@eecs.umich.edu    /** If we need to drain, keep the drain event around until we're done
1172901Ssaidi@eecs.umich.edu     * here.*/
1182901Ssaidi@eecs.umich.edu    Event *drainEvent;
1192901Ssaidi@eecs.umich.edu
1204435Ssaidi@eecs.umich.edu    /** time to wait between sending another packet, increases as NACKs are
1214435Ssaidi@eecs.umich.edu     * recived, decreases as responses are recived. */
1224435Ssaidi@eecs.umich.edu    Tick backoffTime;
1234435Ssaidi@eecs.umich.edu
1247403SAli.Saidi@ARM.com    /** Minimum time that device should back off for after failed sendTiming */
1257403SAli.Saidi@ARM.com    Tick minBackoffDelay;
1267403SAli.Saidi@ARM.com
1277403SAli.Saidi@ARM.com    /** Maximum time that device should back off for after failed sendTiming */
1287403SAli.Saidi@ARM.com    Tick maxBackoffDelay;
1297403SAli.Saidi@ARM.com
1304435Ssaidi@eecs.umich.edu    /** If the port is currently waiting for a retry before it can send whatever
1314435Ssaidi@eecs.umich.edu     * it is that it's sending. */
1324435Ssaidi@eecs.umich.edu    bool inRetry;
1334435Ssaidi@eecs.umich.edu
1348630SMitchell.Hayenga@ARM.com    /** Port accesses a cache which requires snooping */
1358630SMitchell.Hayenga@ARM.com    bool recvSnoops;
1368630SMitchell.Hayenga@ARM.com
1373349Sbinkertn@umich.edu    virtual bool recvTiming(PacketPtr pkt);
1383349Sbinkertn@umich.edu    virtual Tick recvAtomic(PacketPtr pkt)
1398630SMitchell.Hayenga@ARM.com    {
1408630SMitchell.Hayenga@ARM.com        if (recvSnoops) return 0;
1418630SMitchell.Hayenga@ARM.com
1428630SMitchell.Hayenga@ARM.com        panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN
1438630SMitchell.Hayenga@ARM.com    }
1443349Sbinkertn@umich.edu    virtual void recvFunctional(PacketPtr pkt)
1458630SMitchell.Hayenga@ARM.com    {
1468630SMitchell.Hayenga@ARM.com        if (recvSnoops) return;
1478630SMitchell.Hayenga@ARM.com
1488630SMitchell.Hayenga@ARM.com        panic("dma port shouldn't be used for pio access.");
1498630SMitchell.Hayenga@ARM.com    }
1502384SN/A
1512657Ssaidi@eecs.umich.edu    virtual void recvRetry() ;
1522384SN/A
1538922Swilliam.wang@arm.com    virtual bool isSnooping() const { return recvSnoops; }
1542384SN/A
1554435Ssaidi@eecs.umich.edu    void queueDma(PacketPtr pkt, bool front = false);
1564435Ssaidi@eecs.umich.edu    void sendDma();
1574435Ssaidi@eecs.umich.edu
1584435Ssaidi@eecs.umich.edu    /** event to give us a kick every time we backoff time is reached. */
1594435Ssaidi@eecs.umich.edu    EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent;
1602489SN/A
1612384SN/A  public:
1628630SMitchell.Hayenga@ARM.com    DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff,
1638630SMitchell.Hayenga@ARM.com            bool recv_snoops = false);
1642565SN/A
1652641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1667607SGene.Wu@arm.com                   uint8_t *data, Tick delay, Request::Flags flag = 0);
1672565SN/A
1682565SN/A    bool dmaPending() { return pendingCount > 0; }
1692384SN/A
1706227Snate@binkert.org    unsigned cacheBlockSize() const { return peerBlockSize(); }
1712901Ssaidi@eecs.umich.edu    unsigned int drain(Event *de);
1722384SN/A};
1732384SN/A
1742489SN/A/**
1752489SN/A * This device is the base class which all devices senstive to an address range
1762489SN/A * inherit from. There are three pure virtual functions which all devices must
1778711Sandreas.hansson@arm.com * implement getAddrRanges(), read(), and write(). The magic do choose which
1782489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to
1792489SN/A * bother.
1802489SN/A */
1812542SN/Aclass PioDevice : public MemObject
1822384SN/A{
1832384SN/A  protected:
1842901Ssaidi@eecs.umich.edu    System *sys;
1852901Ssaidi@eecs.umich.edu
1862489SN/A    /** The pioPort that handles the requests for us and provides us requests
1872489SN/A     * that it sees. */
1888851Sandreas.hansson@arm.com    PioPort pioPort;
1892384SN/A
1908711Sandreas.hansson@arm.com    /**
1918711Sandreas.hansson@arm.com     * Every PIO device is obliged to provide an implementation that
1928711Sandreas.hansson@arm.com     * returns the address ranges the device responds to.
1938711Sandreas.hansson@arm.com     *
1948711Sandreas.hansson@arm.com     * @return a list of non-overlapping address ranges
1958711Sandreas.hansson@arm.com     */
1968711Sandreas.hansson@arm.com    virtual AddrRangeList getAddrRanges() = 0;
1972384SN/A
1983090Sstever@eecs.umich.edu    /** Pure virtual function that the device must implement. Called
1993090Sstever@eecs.umich.edu     * when a read command is recieved by the port.
2002523SN/A     * @param pkt Packet describing this request
2012523SN/A     * @return number of ticks it took to complete
2022523SN/A     */
2033349Sbinkertn@umich.edu    virtual Tick read(PacketPtr pkt) = 0;
2042384SN/A
2052489SN/A    /** Pure virtual function that the device must implement. Called when a
2062523SN/A     * write command is recieved by the port.
2072523SN/A     * @param pkt Packet describing this request
2082523SN/A     * @return number of ticks it took to complete
2092523SN/A     */
2103349Sbinkertn@umich.edu    virtual Tick write(PacketPtr pkt) = 0;
211545SN/A
212545SN/A  public:
2134762Snate@binkert.org    typedef PioDeviceParams Params;
2144762Snate@binkert.org    PioDevice(const Params *p);
2154762Snate@binkert.org    virtual ~PioDevice();
2164762Snate@binkert.org
2174762Snate@binkert.org    const Params *
2184762Snate@binkert.org    params() const
2192512SN/A    {
2204762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2214762Snate@binkert.org    }
2222384SN/A
2232541SN/A    virtual void init();
2242541SN/A
2252901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2262901Ssaidi@eecs.umich.edu
2278922Swilliam.wang@arm.com    virtual SlavePort &getSlavePort(const std::string &if_name, int idx = -1);
2288598Ssteve.reinhardt@amd.com
2292489SN/A    friend class PioPort;
2302489SN/A
231545SN/A};
232545SN/A
2332512SN/Aclass BasicPioDevice : public PioDevice
2342512SN/A{
2352512SN/A  protected:
2362512SN/A    /** Address that the device listens to. */
2372512SN/A    Addr pioAddr;
2382512SN/A
2392512SN/A    /** Size that the device's address range. */
2402521SN/A    Addr pioSize;
2412512SN/A
2422512SN/A    /** Delay that the device experinces on an access. */
2432512SN/A    Tick pioDelay;
2442512SN/A
2452512SN/A  public:
2464762Snate@binkert.org    typedef BasicPioDeviceParams Params;
2474762Snate@binkert.org    BasicPioDevice(const Params *p);
2484762Snate@binkert.org
2494762Snate@binkert.org    const Params *
2504762Snate@binkert.org    params() const
2514762Snate@binkert.org    {
2524762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2534762Snate@binkert.org    }
2542512SN/A
2558711Sandreas.hansson@arm.com    /**
2568711Sandreas.hansson@arm.com     * Determine the address ranges that this device responds to.
2578711Sandreas.hansson@arm.com     *
2588711Sandreas.hansson@arm.com     * @return a list of non-overlapping address ranges
2592539SN/A     */
2608711Sandreas.hansson@arm.com    virtual AddrRangeList getAddrRanges();
2612539SN/A
2622512SN/A};
2632512SN/A
264545SN/Aclass DmaDevice : public PioDevice
265545SN/A{
2664435Ssaidi@eecs.umich.edu   protected:
2678851Sandreas.hansson@arm.com    DmaPort dmaPort;
268545SN/A
269545SN/A  public:
2704762Snate@binkert.org    typedef DmaDeviceParams Params;
2714762Snate@binkert.org    DmaDevice(const Params *p);
272545SN/A    virtual ~DmaDevice();
2732384SN/A
2744762Snate@binkert.org    const Params *
2754762Snate@binkert.org    params() const
2764762Snate@binkert.org    {
2774762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2784762Snate@binkert.org    }
2794762Snate@binkert.org
2808851Sandreas.hansson@arm.com    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data,
2818851Sandreas.hansson@arm.com                  Tick delay = 0)
2824022Sstever@eecs.umich.edu    {
2838851Sandreas.hansson@arm.com        dmaPort.dmaAction(MemCmd::WriteReq, addr, size, event, data, delay);
2844022Sstever@eecs.umich.edu    }
2852565SN/A
2868851Sandreas.hansson@arm.com    void dmaRead(Addr addr, int size, Event *event, uint8_t *data,
2878851Sandreas.hansson@arm.com                 Tick delay = 0)
2884263Ssaidi@eecs.umich.edu    {
2898851Sandreas.hansson@arm.com        dmaPort.dmaAction(MemCmd::ReadReq, addr, size, event, data, delay);
2904263Ssaidi@eecs.umich.edu    }
2912565SN/A
2928851Sandreas.hansson@arm.com    bool dmaPending() { return dmaPort.dmaPending(); }
2938851Sandreas.hansson@arm.com
2948851Sandreas.hansson@arm.com    virtual void init();
2952565SN/A
2962901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2972901Ssaidi@eecs.umich.edu
2988851Sandreas.hansson@arm.com    unsigned cacheBlockSize() const { return dmaPort.cacheBlockSize(); }
2994263Ssaidi@eecs.umich.edu
3008922Swilliam.wang@arm.com    virtual MasterPort &getMasterPort(const std::string &if_name,
3018922Swilliam.wang@arm.com                                      int idx = -1);
3022489SN/A
3032489SN/A    friend class DmaPort;
304545SN/A};
305545SN/A
3062384SN/A
3071310SN/A#endif // __DEV_IO_DEVICE_HH__
308