io_device.hh revision 8851
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
682901Ssaidi@eecs.umich.edu    PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
692384SN/A};
702384SN/A
712565SN/A
722384SN/Aclass DmaPort : public Port
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
1518711Sandreas.hansson@arm.com    virtual void recvRangeChange()
1528630SMitchell.Hayenga@ARM.com    {
1538711Sandreas.hansson@arm.com        // DMA port is a master with a single slave so there is no choice and
1548711Sandreas.hansson@arm.com        // thus no need to worry about any address changes
1558630SMitchell.Hayenga@ARM.com    }
1562384SN/A
1572657Ssaidi@eecs.umich.edu    virtual void recvRetry() ;
1582384SN/A
1598711Sandreas.hansson@arm.com    virtual bool isSnooping()
1608711Sandreas.hansson@arm.com    { return recvSnoops; }
1612384SN/A
1624435Ssaidi@eecs.umich.edu    void queueDma(PacketPtr pkt, bool front = false);
1634435Ssaidi@eecs.umich.edu    void sendDma();
1644435Ssaidi@eecs.umich.edu
1654435Ssaidi@eecs.umich.edu    /** event to give us a kick every time we backoff time is reached. */
1664435Ssaidi@eecs.umich.edu    EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent;
1672489SN/A
1682384SN/A  public:
1698630SMitchell.Hayenga@ARM.com    DmaPort(MemObject *dev, System *s, Tick min_backoff, Tick max_backoff,
1708630SMitchell.Hayenga@ARM.com            bool recv_snoops = false);
1712565SN/A
1722641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1737607SGene.Wu@arm.com                   uint8_t *data, Tick delay, Request::Flags flag = 0);
1742565SN/A
1752565SN/A    bool dmaPending() { return pendingCount > 0; }
1762384SN/A
1776227Snate@binkert.org    unsigned cacheBlockSize() const { return peerBlockSize(); }
1782901Ssaidi@eecs.umich.edu    unsigned int drain(Event *de);
1792384SN/A};
1802384SN/A
1812489SN/A/**
1822489SN/A * This device is the base class which all devices senstive to an address range
1832489SN/A * inherit from. There are three pure virtual functions which all devices must
1848711Sandreas.hansson@arm.com * implement getAddrRanges(), read(), and write(). The magic do choose which
1852489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to
1862489SN/A * bother.
1872489SN/A */
1882542SN/Aclass PioDevice : public MemObject
1892384SN/A{
1902384SN/A  protected:
1912901Ssaidi@eecs.umich.edu    System *sys;
1922901Ssaidi@eecs.umich.edu
1932489SN/A    /** The pioPort that handles the requests for us and provides us requests
1942489SN/A     * that it sees. */
1958851Sandreas.hansson@arm.com    PioPort pioPort;
1962384SN/A
1978711Sandreas.hansson@arm.com    /**
1988711Sandreas.hansson@arm.com     * Every PIO device is obliged to provide an implementation that
1998711Sandreas.hansson@arm.com     * returns the address ranges the device responds to.
2008711Sandreas.hansson@arm.com     *
2018711Sandreas.hansson@arm.com     * @return a list of non-overlapping address ranges
2028711Sandreas.hansson@arm.com     */
2038711Sandreas.hansson@arm.com    virtual AddrRangeList getAddrRanges() = 0;
2042384SN/A
2053090Sstever@eecs.umich.edu    /** Pure virtual function that the device must implement. Called
2063090Sstever@eecs.umich.edu     * when a read 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 read(PacketPtr pkt) = 0;
2112384SN/A
2122489SN/A    /** Pure virtual function that the device must implement. Called when a
2132523SN/A     * write command is recieved by the port.
2142523SN/A     * @param pkt Packet describing this request
2152523SN/A     * @return number of ticks it took to complete
2162523SN/A     */
2173349Sbinkertn@umich.edu    virtual Tick write(PacketPtr pkt) = 0;
218545SN/A
219545SN/A  public:
2204762Snate@binkert.org    typedef PioDeviceParams Params;
2214762Snate@binkert.org    PioDevice(const Params *p);
2224762Snate@binkert.org    virtual ~PioDevice();
2234762Snate@binkert.org
2244762Snate@binkert.org    const Params *
2254762Snate@binkert.org    params() const
2262512SN/A    {
2274762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2284762Snate@binkert.org    }
2292384SN/A
2302541SN/A    virtual void init();
2312541SN/A
2322901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2332901Ssaidi@eecs.umich.edu
2348598Ssteve.reinhardt@amd.com    virtual Port *getPort(const std::string &if_name, int idx = -1);
2358598Ssteve.reinhardt@amd.com
2362489SN/A    friend class PioPort;
2372489SN/A
238545SN/A};
239545SN/A
2402512SN/Aclass BasicPioDevice : public PioDevice
2412512SN/A{
2422512SN/A  protected:
2432512SN/A    /** Address that the device listens to. */
2442512SN/A    Addr pioAddr;
2452512SN/A
2462512SN/A    /** Size that the device's address range. */
2472521SN/A    Addr pioSize;
2482512SN/A
2492512SN/A    /** Delay that the device experinces on an access. */
2502512SN/A    Tick pioDelay;
2512512SN/A
2522512SN/A  public:
2534762Snate@binkert.org    typedef BasicPioDeviceParams Params;
2544762Snate@binkert.org    BasicPioDevice(const Params *p);
2554762Snate@binkert.org
2564762Snate@binkert.org    const Params *
2574762Snate@binkert.org    params() const
2584762Snate@binkert.org    {
2594762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2604762Snate@binkert.org    }
2612512SN/A
2628711Sandreas.hansson@arm.com    /**
2638711Sandreas.hansson@arm.com     * Determine the address ranges that this device responds to.
2648711Sandreas.hansson@arm.com     *
2658711Sandreas.hansson@arm.com     * @return a list of non-overlapping address ranges
2662539SN/A     */
2678711Sandreas.hansson@arm.com    virtual AddrRangeList getAddrRanges();
2682539SN/A
2692512SN/A};
2702512SN/A
271545SN/Aclass DmaDevice : public PioDevice
272545SN/A{
2734435Ssaidi@eecs.umich.edu   protected:
2748851Sandreas.hansson@arm.com    DmaPort dmaPort;
275545SN/A
276545SN/A  public:
2774762Snate@binkert.org    typedef DmaDeviceParams Params;
2784762Snate@binkert.org    DmaDevice(const Params *p);
279545SN/A    virtual ~DmaDevice();
2802384SN/A
2814762Snate@binkert.org    const Params *
2824762Snate@binkert.org    params() const
2834762Snate@binkert.org    {
2844762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2854762Snate@binkert.org    }
2864762Snate@binkert.org
2878851Sandreas.hansson@arm.com    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data,
2888851Sandreas.hansson@arm.com                  Tick delay = 0)
2894022Sstever@eecs.umich.edu    {
2908851Sandreas.hansson@arm.com        dmaPort.dmaAction(MemCmd::WriteReq, addr, size, event, data, delay);
2914022Sstever@eecs.umich.edu    }
2922565SN/A
2938851Sandreas.hansson@arm.com    void dmaRead(Addr addr, int size, Event *event, uint8_t *data,
2948851Sandreas.hansson@arm.com                 Tick delay = 0)
2954263Ssaidi@eecs.umich.edu    {
2968851Sandreas.hansson@arm.com        dmaPort.dmaAction(MemCmd::ReadReq, addr, size, event, data, delay);
2974263Ssaidi@eecs.umich.edu    }
2982565SN/A
2998851Sandreas.hansson@arm.com    bool dmaPending() { return dmaPort.dmaPending(); }
3008851Sandreas.hansson@arm.com
3018851Sandreas.hansson@arm.com    virtual void init();
3022565SN/A
3032901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
3042901Ssaidi@eecs.umich.edu
3058851Sandreas.hansson@arm.com    unsigned cacheBlockSize() const { return dmaPort.cacheBlockSize(); }
3064263Ssaidi@eecs.umich.edu
3078598Ssteve.reinhardt@amd.com    virtual Port *getPort(const std::string &if_name, int idx = -1);
3082489SN/A
3092489SN/A    friend class DmaPort;
310545SN/A};
311545SN/A
3122384SN/A
3131310SN/A#endif // __DEV_IO_DEVICE_HH__
314