io_device.hh revision 2982
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
352384SN/A#include "base/chunk_generator.hh"
362542SN/A#include "mem/mem_object.hh"
372592SN/A#include "mem/packet_impl.hh"
382384SN/A#include "sim/eventq.hh"
392489SN/A#include "sim/sim_object.hh"
402914Ssaidi@eecs.umich.edu#include "mem/tport.hh"
41545SN/A
421310SN/Aclass Platform;
432384SN/Aclass PioDevice;
442489SN/Aclass DmaDevice;
452522SN/Aclass System;
46545SN/A
472489SN/A/**
482489SN/A * The PioPort class is a programmed i/o port that all devices that are
492489SN/A * sensitive to an address range use. The port takes all the memory
502489SN/A * access types and roles them into one read() and write() call that the device
512489SN/A * must respond to. The device must also provide the addressRanges() function
522914Ssaidi@eecs.umich.edu * with which it returns the address ranges it is interested in. */
532914Ssaidi@eecs.umich.edu
542914Ssaidi@eecs.umich.educlass PioPort : public SimpleTimingPort
55545SN/A{
56545SN/A  protected:
572489SN/A    /** The device that this port serves. */
582384SN/A    PioDevice *device;
592384SN/A
602901Ssaidi@eecs.umich.edu    /** The system that device/port are in. This is used to select which mode
612489SN/A     * we are currently operating in. */
622901Ssaidi@eecs.umich.edu    System *sys;
632489SN/A
642489SN/A    /** The current status of the peer(bus) that we are connected to. */
652489SN/A    Status peerStatus;
662489SN/A
672630SN/A    virtual bool recvTiming(Packet *pkt);
682384SN/A
692630SN/A    virtual Tick recvAtomic(Packet *pkt);
702384SN/A
712630SN/A    virtual void recvFunctional(Packet *pkt) ;
722384SN/A
732489SN/A    virtual void recvStatusChange(Status status)
742489SN/A    { peerStatus = status; }
752384SN/A
762521SN/A    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop);
772384SN/A
782384SN/A  public:
792901Ssaidi@eecs.umich.edu    PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
802901Ssaidi@eecs.umich.edu
812384SN/A};
822384SN/A
832565SN/A
842384SN/Aclass DmaPort : public Port
852384SN/A{
862384SN/A  protected:
872784Ssaidi@eecs.umich.edu    struct DmaReqState : public Packet::SenderState
882784Ssaidi@eecs.umich.edu    {
892784Ssaidi@eecs.umich.edu        /** Event to call on the device when this transaction (all packets)
902784Ssaidi@eecs.umich.edu         * complete. */
912784Ssaidi@eecs.umich.edu        Event *completionEvent;
922784Ssaidi@eecs.umich.edu
932784Ssaidi@eecs.umich.edu        /** Where we came from for some sanity checking. */
942784Ssaidi@eecs.umich.edu        Port *outPort;
952784Ssaidi@eecs.umich.edu
962784Ssaidi@eecs.umich.edu        /** Total number of bytes that this transaction involves. */
972784Ssaidi@eecs.umich.edu        Addr totBytes;
982784Ssaidi@eecs.umich.edu
992784Ssaidi@eecs.umich.edu        /** Number of bytes that have been acked for this transaction. */
1002784Ssaidi@eecs.umich.edu        Addr numBytes;
1012784Ssaidi@eecs.umich.edu
1022784Ssaidi@eecs.umich.edu        DmaReqState(Event *ce, Port *p, Addr tb)
1032784Ssaidi@eecs.umich.edu            : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0)
1042784Ssaidi@eecs.umich.edu        {}
1052784Ssaidi@eecs.umich.edu    };
1062784Ssaidi@eecs.umich.edu
1072565SN/A    DmaDevice *device;
1082384SN/A    std::list<Packet*> transmitList;
1092384SN/A
1102901Ssaidi@eecs.umich.edu    /** The system that device/port are in. This is used to select which mode
1112565SN/A     * we are currently operating in. */
1122901Ssaidi@eecs.umich.edu    System *sys;
1132565SN/A
1142565SN/A    /** Number of outstanding packets the dma port has. */
1152565SN/A    int pendingCount;
1162384SN/A
1172901Ssaidi@eecs.umich.edu    /** If a dmaAction is in progress. */
1182901Ssaidi@eecs.umich.edu    int actionInProgress;
1192901Ssaidi@eecs.umich.edu
1202901Ssaidi@eecs.umich.edu    /** If we need to drain, keep the drain event around until we're done
1212901Ssaidi@eecs.umich.edu     * here.*/
1222901Ssaidi@eecs.umich.edu    Event *drainEvent;
1232901Ssaidi@eecs.umich.edu
1242630SN/A    virtual bool recvTiming(Packet *pkt);
1252630SN/A    virtual Tick recvAtomic(Packet *pkt)
1262384SN/A    { panic("dma port shouldn't be used for pio access."); }
1272630SN/A    virtual void recvFunctional(Packet *pkt)
1282384SN/A    { panic("dma port shouldn't be used for pio access."); }
1292384SN/A
1302384SN/A    virtual void recvStatusChange(Status status)
1312384SN/A    { ; }
1322384SN/A
1332657Ssaidi@eecs.umich.edu    virtual void recvRetry() ;
1342384SN/A
1352521SN/A    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
1362521SN/A    { resp.clear(); snoop.clear(); }
1372384SN/A
1382685Ssaidi@eecs.umich.edu    void sendDma(Packet *pkt, bool front = false);
1392489SN/A
1402384SN/A  public:
1412901Ssaidi@eecs.umich.edu    DmaPort(DmaDevice *dev, System *s);
1422565SN/A
1432641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1442641Sstever@eecs.umich.edu                   uint8_t *data = NULL);
1452565SN/A
1462565SN/A    bool dmaPending() { return pendingCount > 0; }
1472384SN/A
1482901Ssaidi@eecs.umich.edu    unsigned int drain(Event *de);
1492384SN/A};
1502384SN/A
1512489SN/A/**
1522489SN/A * This device is the base class which all devices senstive to an address range
1532489SN/A * inherit from. There are three pure virtual functions which all devices must
1542489SN/A * implement addressRanges(), read(), and write(). The magic do choose which
1552489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to
1562489SN/A * bother.
1572489SN/A */
1582384SN/A
1592542SN/Aclass PioDevice : public MemObject
1602384SN/A{
1612384SN/A  protected:
1622384SN/A
1632489SN/A    /** The platform we are in. This is used to decide what type of memory
1642489SN/A     * transaction we should perform. */
1651310SN/A    Platform *platform;
1662384SN/A
1672901Ssaidi@eecs.umich.edu    System *sys;
1682901Ssaidi@eecs.umich.edu
1692489SN/A    /** The pioPort that handles the requests for us and provides us requests
1702489SN/A     * that it sees. */
1712384SN/A    PioPort *pioPort;
1722384SN/A
1732521SN/A    virtual void addressRanges(AddrRangeList &range_list) = 0;
1742384SN/A
1752489SN/A    /** As far as the devices are concerned they only accept atomic transactions
1762489SN/A     * which are converted to either a write or a read. */
1772630SN/A    Tick recvAtomic(Packet *pkt)
1782641Sstever@eecs.umich.edu    { return pkt->isRead() ? this->read(pkt) : this->write(pkt); }
1792384SN/A
1802489SN/A    /** Pure virtual function that the device must implement. Called when a read
1812523SN/A     * command is recieved by the port.
1822523SN/A     * @param pkt Packet describing this request
1832523SN/A     * @return number of ticks it took to complete
1842523SN/A     */
1852630SN/A    virtual Tick read(Packet *pkt) = 0;
1862384SN/A
1872489SN/A    /** Pure virtual function that the device must implement. Called when a
1882523SN/A     * write command is recieved by the port.
1892523SN/A     * @param pkt Packet describing this request
1902523SN/A     * @return number of ticks it took to complete
1912523SN/A     */
1922630SN/A    virtual Tick write(Packet *pkt) = 0;
193545SN/A
194545SN/A  public:
1952512SN/A    /** Params struct which is extended through each device based on the
1962512SN/A     * parameters it needs. Since we are re-writing everything, we might as well
1972512SN/A     * start from the bottom this time. */
1982512SN/A
1992512SN/A    struct Params
2002512SN/A    {
2012512SN/A        std::string name;
2022512SN/A        Platform *platform;
2032522SN/A        System *system;
2042512SN/A    };
2052521SN/A
2062512SN/A  protected:
2072512SN/A    Params *_params;
2082512SN/A
2092512SN/A  public:
2102512SN/A    const Params *params() const { return _params; }
2112512SN/A
2122521SN/A    PioDevice(Params *p)
2132901Ssaidi@eecs.umich.edu              : MemObject(p->name),  platform(p->platform), sys(p->system),
2142901Ssaidi@eecs.umich.edu              pioPort(NULL), _params(p)
2152512SN/A              {}
2162384SN/A
217545SN/A    virtual ~PioDevice();
2182384SN/A
2192541SN/A    virtual void init();
2202541SN/A
2212901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2222901Ssaidi@eecs.umich.edu
2232738Sstever@eecs.umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1)
2242384SN/A    {
2252521SN/A        if (if_name == "pio") {
2262512SN/A            if (pioPort != NULL)
2272512SN/A                panic("pio port already connected to.");
2282901Ssaidi@eecs.umich.edu            pioPort = new PioPort(this, sys);
2292384SN/A            return pioPort;
2302521SN/A        } else
2312384SN/A            return NULL;
2322384SN/A    }
2332489SN/A    friend class PioPort;
2342489SN/A
235545SN/A};
236545SN/A
2372512SN/Aclass BasicPioDevice : public PioDevice
2382512SN/A{
2392512SN/A  public:
2402521SN/A    struct Params :  public PioDevice::Params
2412512SN/A    {
2422512SN/A        Addr pio_addr;
2432512SN/A        Tick pio_delay;
2442512SN/A    };
2452512SN/A
2462512SN/A  protected:
2472512SN/A    /** Address that the device listens to. */
2482512SN/A    Addr pioAddr;
2492512SN/A
2502512SN/A    /** Size that the device's address range. */
2512521SN/A    Addr pioSize;
2522512SN/A
2532512SN/A    /** Delay that the device experinces on an access. */
2542512SN/A    Tick pioDelay;
2552512SN/A
2562512SN/A  public:
2572521SN/A    BasicPioDevice(Params *p)
2582521SN/A        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
2592512SN/A    {}
2602512SN/A
2612539SN/A    /** return the address ranges that this device responds to.
2622982Sstever@eecs.umich.edu     * @param range_list range list to populate with ranges
2632539SN/A     */
2642542SN/A    void addressRanges(AddrRangeList &range_list);
2652539SN/A
2662512SN/A};
2672512SN/A
268545SN/Aclass DmaDevice : public PioDevice
269545SN/A{
270545SN/A  protected:
2712384SN/A    DmaPort *dmaPort;
272545SN/A
273545SN/A  public:
2742521SN/A    DmaDevice(Params *p);
275545SN/A    virtual ~DmaDevice();
2762384SN/A
2772565SN/A    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
2782641Sstever@eecs.umich.edu    { dmaPort->dmaAction(Packet::WriteReq, addr, size, event, data) ; }
2792565SN/A
2802565SN/A    void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL)
2812641Sstever@eecs.umich.edu    { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); }
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
2872738Sstever@eecs.umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1)
2882384SN/A    {
2892565SN/A        if (if_name == "pio") {
2902565SN/A            if (pioPort != NULL)
2912565SN/A                panic("pio port already connected to.");
2922901Ssaidi@eecs.umich.edu            pioPort = new PioPort(this, sys);
2932384SN/A            return pioPort;
2942565SN/A        } else if (if_name == "dma") {
2952565SN/A            if (dmaPort != NULL)
2962565SN/A                panic("dma port already connected to.");
2972901Ssaidi@eecs.umich.edu            dmaPort = new DmaPort(this, sys);
2982384SN/A            return dmaPort;
2992565SN/A        } else
3002384SN/A            return NULL;
3012384SN/A    }
3022489SN/A
3032489SN/A    friend class DmaPort;
304545SN/A};
305545SN/A
3062384SN/A
3071310SN/A#endif // __DEV_IO_DEVICE_HH__
308