io_device.hh revision 2665
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"
40545SN/A
411310SN/Aclass Platform;
422384SN/Aclass PioDevice;
432489SN/Aclass DmaDevice;
442522SN/Aclass System;
45545SN/A
462489SN/A/**
472489SN/A * The PioPort class is a programmed i/o port that all devices that are
482489SN/A * sensitive to an address range use. The port takes all the memory
492489SN/A * access types and roles them into one read() and write() call that the device
502489SN/A * must respond to. The device must also provide the addressRanges() function
512489SN/A * with which it returns the address ranges it is interested in. An extra
522489SN/A * sendTiming() function is implemented which takes an delay. In this way the
532489SN/A * device can immediatly call sendTiming(pkt, time) after processing a request
542489SN/A * and the request will be handled by the port even if the port bus the device
552489SN/A * connects to is blocked.
562489SN/A */
572384SN/Aclass PioPort : public Port
58545SN/A{
59545SN/A  protected:
602489SN/A    /** The device that this port serves. */
612384SN/A    PioDevice *device;
622384SN/A
632489SN/A    /** The platform that device/port are in. This is used to select which mode
642489SN/A     * we are currently operating in. */
652497SN/A    Platform *platform;
662489SN/A
672489SN/A    /** A list of outgoing timing response packets that haven't been serviced
682489SN/A     * yet. */
692489SN/A    std::list<Packet*> transmitList;
702489SN/A
712489SN/A    /** The current status of the peer(bus) that we are connected to. */
722489SN/A    Status peerStatus;
732489SN/A
742630SN/A    virtual bool recvTiming(Packet *pkt);
752384SN/A
762630SN/A    virtual Tick recvAtomic(Packet *pkt);
772384SN/A
782630SN/A    virtual void recvFunctional(Packet *pkt) ;
792384SN/A
802489SN/A    virtual void recvStatusChange(Status status)
812489SN/A    { peerStatus = status; }
822384SN/A
832521SN/A    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop);
842384SN/A
852489SN/A    /**
862489SN/A     * This class is used to implemented sendTiming() with a delay. When a delay
872489SN/A     * is requested a new event is created. When the event time expires it
882489SN/A     * attempts to send the packet. If it cannot, the packet is pushed onto the
892489SN/A     * transmit list to be sent when recvRetry() is called. */
902384SN/A    class SendEvent : public Event
912384SN/A    {
922384SN/A        PioPort *port;
932630SN/A        Packet *packet;
942384SN/A
952630SN/A        SendEvent(PioPort *p, Packet *pkt, Tick t)
962631SN/A            : Event(&mainEventQueue), port(p), packet(pkt)
972384SN/A        { schedule(curTick + t); }
982384SN/A
992384SN/A        virtual void process();
1002384SN/A
1012384SN/A        virtual const char *description()
1022384SN/A        { return "Future scheduled sendTiming event"; }
1032384SN/A
1042384SN/A        friend class PioPort;
1052489SN/A    };
1062489SN/A
1072489SN/A    /** Schedule a sendTiming() event to be called in the future. */
1082630SN/A    void sendTiming(Packet *pkt, Tick time)
1092489SN/A    { new PioPort::SendEvent(this, pkt, time); }
1102489SN/A
1112657Ssaidi@eecs.umich.edu    /** This function is notification that the device should attempt to send a
1122657Ssaidi@eecs.umich.edu     * packet again. */
1132657Ssaidi@eecs.umich.edu    virtual void recvRetry();
1142384SN/A
1152384SN/A  public:
1162489SN/A    PioPort(PioDevice *dev, Platform *p);
1172384SN/A
1182489SN/A  friend class PioPort::SendEvent;
1192384SN/A};
1202384SN/A
1212565SN/A
1222641Sstever@eecs.umich.edustruct DmaReqState : public Packet::SenderState
1232565SN/A{
1242565SN/A    Event *completionEvent;
1252565SN/A    bool final;
1262565SN/A    DmaReqState(Event *ce, bool f)
1272565SN/A        : completionEvent(ce), final(f)
1282565SN/A    {}
1292565SN/A};
1302565SN/A
1312384SN/Aclass DmaPort : public Port
1322384SN/A{
1332384SN/A  protected:
1342565SN/A    DmaDevice *device;
1352384SN/A    std::list<Packet*> transmitList;
1362384SN/A
1372565SN/A    /** The platform that device/port are in. This is used to select which mode
1382565SN/A     * we are currently operating in. */
1392565SN/A    Platform *platform;
1402565SN/A
1412565SN/A    /** Number of outstanding packets the dma port has. */
1422565SN/A    int pendingCount;
1432384SN/A
1442630SN/A    virtual bool recvTiming(Packet *pkt);
1452630SN/A    virtual Tick recvAtomic(Packet *pkt)
1462384SN/A    { panic("dma port shouldn't be used for pio access."); }
1472630SN/A    virtual void recvFunctional(Packet *pkt)
1482384SN/A    { panic("dma port shouldn't be used for pio access."); }
1492384SN/A
1502384SN/A    virtual void recvStatusChange(Status status)
1512384SN/A    { ; }
1522384SN/A
1532657Ssaidi@eecs.umich.edu    virtual void recvRetry() ;
1542384SN/A
1552521SN/A    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
1562521SN/A    { resp.clear(); snoop.clear(); }
1572384SN/A
1582569SN/A    void sendDma(Packet *pkt);
1592489SN/A
1602384SN/A  public:
1612565SN/A    DmaPort(DmaDevice *dev, Platform *p);
1622565SN/A
1632641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1642641Sstever@eecs.umich.edu                   uint8_t *data = NULL);
1652565SN/A
1662565SN/A    bool dmaPending() { return pendingCount > 0; }
1672384SN/A
1682384SN/A};
1692384SN/A
1702489SN/A/**
1712489SN/A * This device is the base class which all devices senstive to an address range
1722489SN/A * inherit from. There are three pure virtual functions which all devices must
1732489SN/A * implement addressRanges(), read(), and write(). The magic do choose which
1742489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to
1752489SN/A * bother.
1762489SN/A */
1772384SN/A
1782542SN/Aclass PioDevice : public MemObject
1792384SN/A{
1802384SN/A  protected:
1812384SN/A
1822489SN/A    /** The platform we are in. This is used to decide what type of memory
1832489SN/A     * transaction we should perform. */
1841310SN/A    Platform *platform;
1852384SN/A
1862489SN/A    /** The pioPort that handles the requests for us and provides us requests
1872489SN/A     * that it sees. */
1882384SN/A    PioPort *pioPort;
1892384SN/A
1902521SN/A    virtual void addressRanges(AddrRangeList &range_list) = 0;
1912384SN/A
1922489SN/A    /** As far as the devices are concerned they only accept atomic transactions
1932489SN/A     * which are converted to either a write or a read. */
1942630SN/A    Tick recvAtomic(Packet *pkt)
1952641Sstever@eecs.umich.edu    { return pkt->isRead() ? this->read(pkt) : this->write(pkt); }
1962384SN/A
1972489SN/A    /** Pure virtual function that the device must implement. Called when a read
1982523SN/A     * command is recieved by the port.
1992523SN/A     * @param pkt Packet describing this request
2002523SN/A     * @return number of ticks it took to complete
2012523SN/A     */
2022630SN/A    virtual Tick read(Packet *pkt) = 0;
2032384SN/A
2042489SN/A    /** Pure virtual function that the device must implement. Called when a
2052523SN/A     * write command is recieved by the port.
2062523SN/A     * @param pkt Packet describing this request
2072523SN/A     * @return number of ticks it took to complete
2082523SN/A     */
2092630SN/A    virtual Tick write(Packet *pkt) = 0;
210545SN/A
211545SN/A  public:
2122512SN/A    /** Params struct which is extended through each device based on the
2132512SN/A     * parameters it needs. Since we are re-writing everything, we might as well
2142512SN/A     * start from the bottom this time. */
2152512SN/A
2162512SN/A    struct Params
2172512SN/A    {
2182512SN/A        std::string name;
2192512SN/A        Platform *platform;
2202522SN/A        System *system;
2212512SN/A    };
2222521SN/A
2232512SN/A  protected:
2242512SN/A    Params *_params;
2252512SN/A
2262512SN/A  public:
2272512SN/A    const Params *params() const { return _params; }
2282512SN/A
2292521SN/A    PioDevice(Params *p)
2302542SN/A              : MemObject(p->name),  platform(p->platform), pioPort(NULL),
2312542SN/A                _params(p)
2322512SN/A              {}
2332384SN/A
234545SN/A    virtual ~PioDevice();
2352384SN/A
2362541SN/A    virtual void init();
2372541SN/A
2382499SN/A    virtual Port *getPort(const std::string &if_name)
2392384SN/A    {
2402521SN/A        if (if_name == "pio") {
2412512SN/A            if (pioPort != NULL)
2422512SN/A                panic("pio port already connected to.");
2432512SN/A            pioPort = new PioPort(this, params()->platform);
2442384SN/A            return pioPort;
2452521SN/A        } else
2462384SN/A            return NULL;
2472384SN/A    }
2482489SN/A    friend class PioPort;
2492489SN/A
250545SN/A};
251545SN/A
2522512SN/Aclass BasicPioDevice : public PioDevice
2532512SN/A{
2542512SN/A  public:
2552521SN/A    struct Params :  public PioDevice::Params
2562512SN/A    {
2572512SN/A        Addr pio_addr;
2582512SN/A        Tick pio_delay;
2592512SN/A    };
2602512SN/A
2612512SN/A  protected:
2622512SN/A    /** Address that the device listens to. */
2632512SN/A    Addr pioAddr;
2642512SN/A
2652512SN/A    /** Size that the device's address range. */
2662521SN/A    Addr pioSize;
2672512SN/A
2682512SN/A    /** Delay that the device experinces on an access. */
2692512SN/A    Tick pioDelay;
2702512SN/A
2712512SN/A  public:
2722521SN/A    BasicPioDevice(Params *p)
2732521SN/A        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
2742512SN/A    {}
2752512SN/A
2762539SN/A    /** return the address ranges that this device responds to.
2772539SN/A     * @params range_list range list to populate with ranges
2782539SN/A     */
2792542SN/A    void addressRanges(AddrRangeList &range_list);
2802539SN/A
2812512SN/A};
2822512SN/A
283545SN/Aclass DmaDevice : public PioDevice
284545SN/A{
285545SN/A  protected:
2862384SN/A    DmaPort *dmaPort;
287545SN/A
288545SN/A  public:
2892521SN/A    DmaDevice(Params *p);
290545SN/A    virtual ~DmaDevice();
2912384SN/A
2922565SN/A    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
2932641Sstever@eecs.umich.edu    { dmaPort->dmaAction(Packet::WriteReq, addr, size, event, data) ; }
2942565SN/A
2952565SN/A    void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL)
2962641Sstever@eecs.umich.edu    { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); }
2972565SN/A
2982565SN/A    bool dmaPending() { return dmaPort->dmaPending(); }
2992565SN/A
3002499SN/A    virtual Port *getPort(const std::string &if_name)
3012384SN/A    {
3022565SN/A        if (if_name == "pio") {
3032565SN/A            if (pioPort != NULL)
3042565SN/A                panic("pio port already connected to.");
3052565SN/A            pioPort = new PioPort(this, params()->platform);
3062384SN/A            return pioPort;
3072565SN/A        } else if (if_name == "dma") {
3082565SN/A            if (dmaPort != NULL)
3092565SN/A                panic("dma port already connected to.");
3102565SN/A            dmaPort = new DmaPort(this, params()->platform);
3112384SN/A            return dmaPort;
3122565SN/A        } else
3132384SN/A            return NULL;
3142384SN/A    }
3152489SN/A
3162489SN/A    friend class DmaPort;
317545SN/A};
318545SN/A
3192384SN/A
3201310SN/A#endif // __DEV_IO_DEVICE_HH__
321