io_device.hh revision 2641
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.
27545SN/A */
28545SN/A
291310SN/A#ifndef __DEV_IO_DEVICE_HH__
301310SN/A#define __DEV_IO_DEVICE_HH__
31545SN/A
322384SN/A#include "base/chunk_generator.hh"
332542SN/A#include "mem/mem_object.hh"
342592SN/A#include "mem/packet_impl.hh"
352384SN/A#include "sim/eventq.hh"
362489SN/A#include "sim/sim_object.hh"
37545SN/A
381310SN/Aclass Platform;
392384SN/Aclass PioDevice;
402489SN/Aclass DmaDevice;
412522SN/Aclass System;
42545SN/A
432489SN/A/**
442489SN/A * The PioPort class is a programmed i/o port that all devices that are
452489SN/A * sensitive to an address range use. The port takes all the memory
462489SN/A * access types and roles them into one read() and write() call that the device
472489SN/A * must respond to. The device must also provide the addressRanges() function
482489SN/A * with which it returns the address ranges it is interested in. An extra
492489SN/A * sendTiming() function is implemented which takes an delay. In this way the
502489SN/A * device can immediatly call sendTiming(pkt, time) after processing a request
512489SN/A * and the request will be handled by the port even if the port bus the device
522489SN/A * connects to is blocked.
532489SN/A */
542384SN/Aclass PioPort : public Port
55545SN/A{
56545SN/A  protected:
572489SN/A    /** The device that this port serves. */
582384SN/A    PioDevice *device;
592384SN/A
602489SN/A    /** The platform that device/port are in. This is used to select which mode
612489SN/A     * we are currently operating in. */
622497SN/A    Platform *platform;
632489SN/A
642489SN/A    /** A list of outgoing timing response packets that haven't been serviced
652489SN/A     * yet. */
662489SN/A    std::list<Packet*> transmitList;
672489SN/A
682489SN/A    /** The current status of the peer(bus) that we are connected to. */
692489SN/A    Status peerStatus;
702489SN/A
712630SN/A    virtual bool recvTiming(Packet *pkt);
722384SN/A
732630SN/A    virtual Tick recvAtomic(Packet *pkt);
742384SN/A
752630SN/A    virtual void recvFunctional(Packet *pkt) ;
762384SN/A
772489SN/A    virtual void recvStatusChange(Status status)
782489SN/A    { peerStatus = status; }
792384SN/A
802521SN/A    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop);
812384SN/A
822489SN/A    /**
832489SN/A     * This class is used to implemented sendTiming() with a delay. When a delay
842489SN/A     * is requested a new event is created. When the event time expires it
852489SN/A     * attempts to send the packet. If it cannot, the packet is pushed onto the
862489SN/A     * transmit list to be sent when recvRetry() is called. */
872384SN/A    class SendEvent : public Event
882384SN/A    {
892384SN/A        PioPort *port;
902630SN/A        Packet *packet;
912384SN/A
922630SN/A        SendEvent(PioPort *p, Packet *pkt, Tick t)
932631SN/A            : Event(&mainEventQueue), port(p), packet(pkt)
942384SN/A        { schedule(curTick + t); }
952384SN/A
962384SN/A        virtual void process();
972384SN/A
982384SN/A        virtual const char *description()
992384SN/A        { return "Future scheduled sendTiming event"; }
1002384SN/A
1012384SN/A        friend class PioPort;
1022489SN/A    };
1032489SN/A
1042489SN/A    /** Schedule a sendTiming() event to be called in the future. */
1052630SN/A    void sendTiming(Packet *pkt, Tick time)
1062489SN/A    { new PioPort::SendEvent(this, pkt, time); }
1072489SN/A
1082489SN/A    /** This function pops the last element off the transmit list and sends it.*/
1092489SN/A    virtual Packet *recvRetry();
1102384SN/A
1112384SN/A  public:
1122489SN/A    PioPort(PioDevice *dev, Platform *p);
1132384SN/A
1142489SN/A  friend class PioPort::SendEvent;
1152384SN/A};
1162384SN/A
1172565SN/A
1182641Sstever@eecs.umich.edustruct DmaReqState : public Packet::SenderState
1192565SN/A{
1202565SN/A    Event *completionEvent;
1212565SN/A    bool final;
1222565SN/A    DmaReqState(Event *ce, bool f)
1232565SN/A        : completionEvent(ce), final(f)
1242565SN/A    {}
1252565SN/A};
1262565SN/A
1272384SN/Aclass DmaPort : public Port
1282384SN/A{
1292384SN/A  protected:
1302565SN/A    DmaDevice *device;
1312384SN/A    std::list<Packet*> transmitList;
1322384SN/A
1332565SN/A    /** The platform that device/port are in. This is used to select which mode
1342565SN/A     * we are currently operating in. */
1352565SN/A    Platform *platform;
1362565SN/A
1372565SN/A    /** Number of outstanding packets the dma port has. */
1382565SN/A    int pendingCount;
1392384SN/A
1402630SN/A    virtual bool recvTiming(Packet *pkt);
1412630SN/A    virtual Tick recvAtomic(Packet *pkt)
1422384SN/A    { panic("dma port shouldn't be used for pio access."); }
1432630SN/A    virtual void recvFunctional(Packet *pkt)
1442384SN/A    { panic("dma port shouldn't be used for pio access."); }
1452384SN/A
1462384SN/A    virtual void recvStatusChange(Status status)
1472384SN/A    { ; }
1482384SN/A
1492489SN/A    virtual Packet *recvRetry() ;
1502384SN/A
1512521SN/A    virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
1522521SN/A    { resp.clear(); snoop.clear(); }
1532384SN/A
1542384SN/A    class SendEvent : public Event
1552384SN/A    {
1562489SN/A        DmaPort *port;
1572630SN/A        Packet *packet;
1582384SN/A
1592630SN/A        SendEvent(PioPort *p, Packet *pkt, Tick t)
1602384SN/A            : Event(&mainEventQueue), packet(pkt)
1612384SN/A        { schedule(curTick + t); }
1622384SN/A
1632384SN/A        virtual void process();
1642384SN/A
1652384SN/A        virtual const char *description()
1662384SN/A        { return "Future scheduled sendTiming event"; }
1672384SN/A
1682489SN/A        friend class DmaPort;
1692489SN/A    };
1702489SN/A
1712569SN/A    void sendDma(Packet *pkt);
1722489SN/A
1732384SN/A  public:
1742565SN/A    DmaPort(DmaDevice *dev, Platform *p);
1752565SN/A
1762641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1772641Sstever@eecs.umich.edu                   uint8_t *data = NULL);
1782565SN/A
1792565SN/A    bool dmaPending() { return pendingCount > 0; }
1802384SN/A
1812489SN/A  friend class DmaPort::SendEvent;
1822384SN/A
1832384SN/A};
1842384SN/A
1852489SN/A/**
1862489SN/A * This device is the base class which all devices senstive to an address range
1872489SN/A * inherit from. There are three pure virtual functions which all devices must
1882489SN/A * implement addressRanges(), read(), and write(). The magic do choose which
1892489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to
1902489SN/A * bother.
1912489SN/A */
1922384SN/A
1932542SN/Aclass PioDevice : public MemObject
1942384SN/A{
1952384SN/A  protected:
1962384SN/A
1972489SN/A    /** The platform we are in. This is used to decide what type of memory
1982489SN/A     * transaction we should perform. */
1991310SN/A    Platform *platform;
2002384SN/A
2012489SN/A    /** The pioPort that handles the requests for us and provides us requests
2022489SN/A     * that it sees. */
2032384SN/A    PioPort *pioPort;
2042384SN/A
2052521SN/A    virtual void addressRanges(AddrRangeList &range_list) = 0;
2062384SN/A
2072489SN/A    /** As far as the devices are concerned they only accept atomic transactions
2082489SN/A     * which are converted to either a write or a read. */
2092630SN/A    Tick recvAtomic(Packet *pkt)
2102641Sstever@eecs.umich.edu    { return pkt->isRead() ? this->read(pkt) : this->write(pkt); }
2112384SN/A
2122489SN/A    /** Pure virtual function that the device must implement. Called when a read
2132523SN/A     * 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     */
2172630SN/A    virtual Tick read(Packet *pkt) = 0;
2182384SN/A
2192489SN/A    /** Pure virtual function that the device must implement. Called when a
2202523SN/A     * write command is recieved by the port.
2212523SN/A     * @param pkt Packet describing this request
2222523SN/A     * @return number of ticks it took to complete
2232523SN/A     */
2242630SN/A    virtual Tick write(Packet *pkt) = 0;
225545SN/A
226545SN/A  public:
2272512SN/A    /** Params struct which is extended through each device based on the
2282512SN/A     * parameters it needs. Since we are re-writing everything, we might as well
2292512SN/A     * start from the bottom this time. */
2302512SN/A
2312512SN/A    struct Params
2322512SN/A    {
2332512SN/A        std::string name;
2342512SN/A        Platform *platform;
2352522SN/A        System *system;
2362512SN/A    };
2372521SN/A
2382512SN/A  protected:
2392512SN/A    Params *_params;
2402512SN/A
2412512SN/A  public:
2422512SN/A    const Params *params() const { return _params; }
2432512SN/A
2442521SN/A    PioDevice(Params *p)
2452542SN/A              : MemObject(p->name),  platform(p->platform), pioPort(NULL),
2462542SN/A                _params(p)
2472512SN/A              {}
2482384SN/A
249545SN/A    virtual ~PioDevice();
2502384SN/A
2512541SN/A    virtual void init();
2522541SN/A
2532499SN/A    virtual Port *getPort(const std::string &if_name)
2542384SN/A    {
2552521SN/A        if (if_name == "pio") {
2562512SN/A            if (pioPort != NULL)
2572512SN/A                panic("pio port already connected to.");
2582512SN/A            pioPort = new PioPort(this, params()->platform);
2592384SN/A            return pioPort;
2602521SN/A        } else
2612384SN/A            return NULL;
2622384SN/A    }
2632489SN/A    friend class PioPort;
2642489SN/A
265545SN/A};
266545SN/A
2672512SN/Aclass BasicPioDevice : public PioDevice
2682512SN/A{
2692512SN/A  public:
2702521SN/A    struct Params :  public PioDevice::Params
2712512SN/A    {
2722512SN/A        Addr pio_addr;
2732512SN/A        Tick pio_delay;
2742512SN/A    };
2752512SN/A
2762512SN/A  protected:
2772512SN/A    /** Address that the device listens to. */
2782512SN/A    Addr pioAddr;
2792512SN/A
2802512SN/A    /** Size that the device's address range. */
2812521SN/A    Addr pioSize;
2822512SN/A
2832512SN/A    /** Delay that the device experinces on an access. */
2842512SN/A    Tick pioDelay;
2852512SN/A
2862512SN/A  public:
2872521SN/A    BasicPioDevice(Params *p)
2882521SN/A        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay)
2892512SN/A    {}
2902512SN/A
2912539SN/A    /** return the address ranges that this device responds to.
2922539SN/A     * @params range_list range list to populate with ranges
2932539SN/A     */
2942542SN/A    void addressRanges(AddrRangeList &range_list);
2952539SN/A
2962512SN/A};
2972512SN/A
298545SN/Aclass DmaDevice : public PioDevice
299545SN/A{
300545SN/A  protected:
3012384SN/A    DmaPort *dmaPort;
302545SN/A
303545SN/A  public:
3042521SN/A    DmaDevice(Params *p);
305545SN/A    virtual ~DmaDevice();
3062384SN/A
3072565SN/A    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
3082641Sstever@eecs.umich.edu    { dmaPort->dmaAction(Packet::WriteReq, addr, size, event, data) ; }
3092565SN/A
3102565SN/A    void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL)
3112641Sstever@eecs.umich.edu    { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); }
3122565SN/A
3132565SN/A    bool dmaPending() { return dmaPort->dmaPending(); }
3142565SN/A
3152499SN/A    virtual Port *getPort(const std::string &if_name)
3162384SN/A    {
3172565SN/A        if (if_name == "pio") {
3182565SN/A            if (pioPort != NULL)
3192565SN/A                panic("pio port already connected to.");
3202565SN/A            pioPort = new PioPort(this, params()->platform);
3212384SN/A            return pioPort;
3222565SN/A        } else if (if_name == "dma") {
3232565SN/A            if (dmaPort != NULL)
3242565SN/A                panic("dma port already connected to.");
3252565SN/A            dmaPort = new DmaPort(this, params()->platform);
3262384SN/A            return dmaPort;
3272565SN/A        } else
3282384SN/A            return NULL;
3292384SN/A    }
3302489SN/A
3312489SN/A    friend class DmaPort;
332545SN/A};
333545SN/A
3342384SN/A
3351310SN/A#endif // __DEV_IO_DEVICE_HH__
336