io_device.hh revision 6227
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
542489SN/A * must respond to. The device must also provide the addressRanges() 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
653090Sstever@eecs.umich.edu    virtual void getDeviceAddressRanges(AddrRangeList &resp,
664475Sstever@eecs.umich.edu                                        bool &snoop);
672384SN/A
682384SN/A  public:
693091Sstever@eecs.umich.edu
702901Ssaidi@eecs.umich.edu    PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
712384SN/A};
722384SN/A
732565SN/A
742384SN/Aclass DmaPort : public Port
752384SN/A{
762384SN/A  protected:
775386Sstever@gmail.com    struct DmaReqState : public Packet::SenderState, public FastAlloc
782784Ssaidi@eecs.umich.edu    {
792784Ssaidi@eecs.umich.edu        /** Event to call on the device when this transaction (all packets)
802784Ssaidi@eecs.umich.edu         * complete. */
812784Ssaidi@eecs.umich.edu        Event *completionEvent;
822784Ssaidi@eecs.umich.edu
832784Ssaidi@eecs.umich.edu        /** Where we came from for some sanity checking. */
842784Ssaidi@eecs.umich.edu        Port *outPort;
852784Ssaidi@eecs.umich.edu
862784Ssaidi@eecs.umich.edu        /** Total number of bytes that this transaction involves. */
872784Ssaidi@eecs.umich.edu        Addr totBytes;
882784Ssaidi@eecs.umich.edu
892784Ssaidi@eecs.umich.edu        /** Number of bytes that have been acked for this transaction. */
902784Ssaidi@eecs.umich.edu        Addr numBytes;
912784Ssaidi@eecs.umich.edu
925534Ssaidi@eecs.umich.edu        /** Amount to delay completion of dma by */
935534Ssaidi@eecs.umich.edu        Tick delay;
945534Ssaidi@eecs.umich.edu
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
1012565SN/A    DmaDevice *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
1224435Ssaidi@eecs.umich.edu    /** If the port is currently waiting for a retry before it can send whatever
1234435Ssaidi@eecs.umich.edu     * it is that it's sending. */
1244435Ssaidi@eecs.umich.edu    bool inRetry;
1254435Ssaidi@eecs.umich.edu
1263349Sbinkertn@umich.edu    virtual bool recvTiming(PacketPtr pkt);
1273349Sbinkertn@umich.edu    virtual Tick recvAtomic(PacketPtr pkt)
1283918Ssaidi@eecs.umich.edu    { panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN }
1293349Sbinkertn@umich.edu    virtual void recvFunctional(PacketPtr pkt)
1302384SN/A    { panic("dma port shouldn't be used for pio access."); }
1312384SN/A
1322384SN/A    virtual void recvStatusChange(Status status)
1332384SN/A    { ; }
1342384SN/A
1352657Ssaidi@eecs.umich.edu    virtual void recvRetry() ;
1362384SN/A
1373090Sstever@eecs.umich.edu    virtual void getDeviceAddressRanges(AddrRangeList &resp,
1384475Sstever@eecs.umich.edu                                        bool &snoop)
1394475Sstever@eecs.umich.edu    { resp.clear(); snoop = false; }
1402384SN/A
1414435Ssaidi@eecs.umich.edu    void queueDma(PacketPtr pkt, bool front = false);
1424435Ssaidi@eecs.umich.edu    void sendDma();
1434435Ssaidi@eecs.umich.edu
1444435Ssaidi@eecs.umich.edu    /** event to give us a kick every time we backoff time is reached. */
1454435Ssaidi@eecs.umich.edu    EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent;
1462489SN/A
1472384SN/A  public:
1482901Ssaidi@eecs.umich.edu    DmaPort(DmaDevice *dev, System *s);
1492565SN/A
1502641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1515534Ssaidi@eecs.umich.edu                   uint8_t *data, Tick delay);
1522565SN/A
1532565SN/A    bool dmaPending() { return pendingCount > 0; }
1542384SN/A
1556227Snate@binkert.org    unsigned cacheBlockSize() const { return peerBlockSize(); }
1562901Ssaidi@eecs.umich.edu    unsigned int drain(Event *de);
1572384SN/A};
1582384SN/A
1592489SN/A/**
1602489SN/A * This device is the base class which all devices senstive to an address range
1612489SN/A * inherit from. There are three pure virtual functions which all devices must
1622489SN/A * implement addressRanges(), read(), and write(). The magic do choose which
1632489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to
1642489SN/A * bother.
1652489SN/A */
1662542SN/Aclass PioDevice : public MemObject
1672384SN/A{
1682384SN/A  protected:
1692384SN/A
1702489SN/A    /** The platform we are in. This is used to decide what type of memory
1712489SN/A     * transaction we should perform. */
1721310SN/A    Platform *platform;
1732384SN/A
1742901Ssaidi@eecs.umich.edu    System *sys;
1752901Ssaidi@eecs.umich.edu
1762489SN/A    /** The pioPort that handles the requests for us and provides us requests
1772489SN/A     * that it sees. */
1782384SN/A    PioPort *pioPort;
1792384SN/A
1802521SN/A    virtual void addressRanges(AddrRangeList &range_list) = 0;
1812384SN/A
1823090Sstever@eecs.umich.edu    /** Pure virtual function that the device must implement. Called
1833090Sstever@eecs.umich.edu     * when a read command is recieved by the port.
1842523SN/A     * @param pkt Packet describing this request
1852523SN/A     * @return number of ticks it took to complete
1862523SN/A     */
1873349Sbinkertn@umich.edu    virtual Tick read(PacketPtr pkt) = 0;
1882384SN/A
1892489SN/A    /** Pure virtual function that the device must implement. Called when a
1902523SN/A     * write command is recieved by the port.
1912523SN/A     * @param pkt Packet describing this request
1922523SN/A     * @return number of ticks it took to complete
1932523SN/A     */
1943349Sbinkertn@umich.edu    virtual Tick write(PacketPtr pkt) = 0;
195545SN/A
196545SN/A  public:
1974762Snate@binkert.org    typedef PioDeviceParams Params;
1984762Snate@binkert.org    PioDevice(const Params *p);
1994762Snate@binkert.org    virtual ~PioDevice();
2004762Snate@binkert.org
2014762Snate@binkert.org    const Params *
2024762Snate@binkert.org    params() const
2032512SN/A    {
2044762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2054762Snate@binkert.org    }
2062384SN/A
2072541SN/A    virtual void init();
2082541SN/A
2092901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2102901Ssaidi@eecs.umich.edu
2112738Sstever@eecs.umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1)
2122384SN/A    {
2132521SN/A        if (if_name == "pio") {
2142512SN/A            if (pioPort != NULL)
2155578SSteve.Reinhardt@amd.com                fatal("%s: pio port already connected to %s",
2165578SSteve.Reinhardt@amd.com                      name(), pioPort->getPeer()->name());
2172901Ssaidi@eecs.umich.edu            pioPort = new PioPort(this, sys);
2182384SN/A            return pioPort;
2192521SN/A        } else
2202384SN/A            return NULL;
2212384SN/A    }
2222489SN/A    friend class PioPort;
2232489SN/A
224545SN/A};
225545SN/A
2262512SN/Aclass BasicPioDevice : public PioDevice
2272512SN/A{
2282512SN/A  protected:
2292512SN/A    /** Address that the device listens to. */
2302512SN/A    Addr pioAddr;
2312512SN/A
2322512SN/A    /** Size that the device's address range. */
2332521SN/A    Addr pioSize;
2342512SN/A
2352512SN/A    /** Delay that the device experinces on an access. */
2362512SN/A    Tick pioDelay;
2372512SN/A
2382512SN/A  public:
2394762Snate@binkert.org    typedef BasicPioDeviceParams Params;
2404762Snate@binkert.org    BasicPioDevice(const Params *p);
2414762Snate@binkert.org
2424762Snate@binkert.org    const Params *
2434762Snate@binkert.org    params() const
2444762Snate@binkert.org    {
2454762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2464762Snate@binkert.org    }
2472512SN/A
2482539SN/A    /** return the address ranges that this device responds to.
2492982Sstever@eecs.umich.edu     * @param range_list range list to populate with ranges
2502539SN/A     */
2512542SN/A    void addressRanges(AddrRangeList &range_list);
2522539SN/A
2532512SN/A};
2542512SN/A
255545SN/Aclass DmaDevice : public PioDevice
256545SN/A{
2574435Ssaidi@eecs.umich.edu   protected:
2582384SN/A    DmaPort *dmaPort;
2594435Ssaidi@eecs.umich.edu    Tick minBackoffDelay;
2604435Ssaidi@eecs.umich.edu    Tick maxBackoffDelay;
261545SN/A
262545SN/A  public:
2634762Snate@binkert.org    typedef DmaDeviceParams Params;
2644762Snate@binkert.org    DmaDevice(const Params *p);
265545SN/A    virtual ~DmaDevice();
2662384SN/A
2674762Snate@binkert.org    const Params *
2684762Snate@binkert.org    params() const
2694762Snate@binkert.org    {
2704762Snate@binkert.org        return dynamic_cast<const Params *>(_params);
2714762Snate@binkert.org    }
2724762Snate@binkert.org
2735534Ssaidi@eecs.umich.edu    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
2744022Sstever@eecs.umich.edu    {
2755534Ssaidi@eecs.umich.edu        dmaPort->dmaAction(MemCmd::WriteReq, addr, size, event, data, delay);
2764022Sstever@eecs.umich.edu    }
2772565SN/A
2785534Ssaidi@eecs.umich.edu    void dmaRead(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0)
2794263Ssaidi@eecs.umich.edu    {
2805534Ssaidi@eecs.umich.edu        dmaPort->dmaAction(MemCmd::ReadReq, addr, size, event, data, delay);
2814263Ssaidi@eecs.umich.edu    }
2822565SN/A
2832565SN/A    bool dmaPending() { return dmaPort->dmaPending(); }
2842565SN/A
2852901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2862901Ssaidi@eecs.umich.edu
2876227Snate@binkert.org    unsigned cacheBlockSize() const { return dmaPort->cacheBlockSize(); }
2884263Ssaidi@eecs.umich.edu
2892738Sstever@eecs.umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1)
2902384SN/A    {
2912565SN/A        if (if_name == "pio") {
2922565SN/A            if (pioPort != NULL)
2935578SSteve.Reinhardt@amd.com                fatal("%s: pio port already connected to %s",
2945578SSteve.Reinhardt@amd.com                      name(), pioPort->getPeer()->name());
2952901Ssaidi@eecs.umich.edu            pioPort = new PioPort(this, sys);
2962384SN/A            return pioPort;
2972565SN/A        } else if (if_name == "dma") {
2982565SN/A            if (dmaPort != NULL)
2995578SSteve.Reinhardt@amd.com                fatal("%s: dma port already connected to %s",
3005732SSteve.Reinhardt@amd.com                      name(), dmaPort->getPeer()->name());
3012901Ssaidi@eecs.umich.edu            dmaPort = new DmaPort(this, sys);
3022384SN/A            return dmaPort;
3032565SN/A        } else
3042384SN/A            return NULL;
3052384SN/A    }
3062489SN/A
3072489SN/A    friend class DmaPort;
308545SN/A};
309545SN/A
3102384SN/A
3111310SN/A#endif // __DEV_IO_DEVICE_HH__
312