io_device.hh revision 3348
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
352542SN/A#include "mem/mem_object.hh"
363348Sbinkertn@umich.edu#include "mem/packet.hh"
373348Sbinkertn@umich.edu#include "mem/tport.hh"
382489SN/A#include "sim/sim_object.hh"
39545SN/A
403090Sstever@eecs.umich.educlass Event;
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
513090Sstever@eecs.umich.edu * with which it returns the address ranges it is interested in.
523090Sstever@eecs.umich.edu */
532914Ssaidi@eecs.umich.educlass PioPort : public SimpleTimingPort
54545SN/A{
55545SN/A  protected:
562489SN/A    /** The device that this port serves. */
572384SN/A    PioDevice *device;
582384SN/A
592630SN/A    virtual Tick recvAtomic(Packet *pkt);
602384SN/A
613090Sstever@eecs.umich.edu    virtual void getDeviceAddressRanges(AddrRangeList &resp,
623090Sstever@eecs.umich.edu                                        AddrRangeList &snoop);
632384SN/A
642384SN/A  public:
653091Sstever@eecs.umich.edu
662901Ssaidi@eecs.umich.edu    PioPort(PioDevice *dev, System *s, std::string pname = "-pioport");
672384SN/A};
682384SN/A
692565SN/A
702384SN/Aclass DmaPort : public Port
712384SN/A{
722384SN/A  protected:
732784Ssaidi@eecs.umich.edu    struct DmaReqState : public Packet::SenderState
742784Ssaidi@eecs.umich.edu    {
752784Ssaidi@eecs.umich.edu        /** Event to call on the device when this transaction (all packets)
762784Ssaidi@eecs.umich.edu         * complete. */
772784Ssaidi@eecs.umich.edu        Event *completionEvent;
782784Ssaidi@eecs.umich.edu
792784Ssaidi@eecs.umich.edu        /** Where we came from for some sanity checking. */
802784Ssaidi@eecs.umich.edu        Port *outPort;
812784Ssaidi@eecs.umich.edu
822784Ssaidi@eecs.umich.edu        /** Total number of bytes that this transaction involves. */
832784Ssaidi@eecs.umich.edu        Addr totBytes;
842784Ssaidi@eecs.umich.edu
852784Ssaidi@eecs.umich.edu        /** Number of bytes that have been acked for this transaction. */
862784Ssaidi@eecs.umich.edu        Addr numBytes;
872784Ssaidi@eecs.umich.edu
882784Ssaidi@eecs.umich.edu        DmaReqState(Event *ce, Port *p, Addr tb)
892784Ssaidi@eecs.umich.edu            : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0)
902784Ssaidi@eecs.umich.edu        {}
912784Ssaidi@eecs.umich.edu    };
922784Ssaidi@eecs.umich.edu
932565SN/A    DmaDevice *device;
942384SN/A    std::list<Packet*> transmitList;
952384SN/A
962901Ssaidi@eecs.umich.edu    /** The system that device/port are in. This is used to select which mode
972565SN/A     * we are currently operating in. */
982901Ssaidi@eecs.umich.edu    System *sys;
992565SN/A
1002565SN/A    /** Number of outstanding packets the dma port has. */
1012565SN/A    int pendingCount;
1022384SN/A
1032901Ssaidi@eecs.umich.edu    /** If a dmaAction is in progress. */
1042901Ssaidi@eecs.umich.edu    int actionInProgress;
1052901Ssaidi@eecs.umich.edu
1062901Ssaidi@eecs.umich.edu    /** If we need to drain, keep the drain event around until we're done
1072901Ssaidi@eecs.umich.edu     * here.*/
1082901Ssaidi@eecs.umich.edu    Event *drainEvent;
1092901Ssaidi@eecs.umich.edu
1102630SN/A    virtual bool recvTiming(Packet *pkt);
1112630SN/A    virtual Tick recvAtomic(Packet *pkt)
1122384SN/A    { panic("dma port shouldn't be used for pio access."); }
1132630SN/A    virtual void recvFunctional(Packet *pkt)
1142384SN/A    { panic("dma port shouldn't be used for pio access."); }
1152384SN/A
1162384SN/A    virtual void recvStatusChange(Status status)
1172384SN/A    { ; }
1182384SN/A
1192657Ssaidi@eecs.umich.edu    virtual void recvRetry() ;
1202384SN/A
1213090Sstever@eecs.umich.edu    virtual void getDeviceAddressRanges(AddrRangeList &resp,
1223090Sstever@eecs.umich.edu                                        AddrRangeList &snoop)
1232521SN/A    { resp.clear(); snoop.clear(); }
1242384SN/A
1252685Ssaidi@eecs.umich.edu    void sendDma(Packet *pkt, bool front = false);
1262489SN/A
1272384SN/A  public:
1282901Ssaidi@eecs.umich.edu    DmaPort(DmaDevice *dev, System *s);
1292565SN/A
1302641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1312641Sstever@eecs.umich.edu                   uint8_t *data = NULL);
1322565SN/A
1332565SN/A    bool dmaPending() { return pendingCount > 0; }
1342384SN/A
1352901Ssaidi@eecs.umich.edu    unsigned int drain(Event *de);
1362384SN/A};
1372384SN/A
1382489SN/A/**
1392489SN/A * This device is the base class which all devices senstive to an address range
1402489SN/A * inherit from. There are three pure virtual functions which all devices must
1412489SN/A * implement addressRanges(), read(), and write(). The magic do choose which
1422489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to
1432489SN/A * bother.
1442489SN/A */
1452542SN/Aclass PioDevice : public MemObject
1462384SN/A{
1472384SN/A  protected:
1482384SN/A
1492489SN/A    /** The platform we are in. This is used to decide what type of memory
1502489SN/A     * transaction we should perform. */
1511310SN/A    Platform *platform;
1522384SN/A
1532901Ssaidi@eecs.umich.edu    System *sys;
1542901Ssaidi@eecs.umich.edu
1552489SN/A    /** The pioPort that handles the requests for us and provides us requests
1562489SN/A     * that it sees. */
1572384SN/A    PioPort *pioPort;
1582384SN/A
1592521SN/A    virtual void addressRanges(AddrRangeList &range_list) = 0;
1602384SN/A
1613090Sstever@eecs.umich.edu    /** Pure virtual function that the device must implement. Called
1623090Sstever@eecs.umich.edu     * when a read command is recieved by the port.
1632523SN/A     * @param pkt Packet describing this request
1642523SN/A     * @return number of ticks it took to complete
1652523SN/A     */
1662630SN/A    virtual Tick read(Packet *pkt) = 0;
1672384SN/A
1682489SN/A    /** Pure virtual function that the device must implement. Called when a
1692523SN/A     * write command is recieved by the port.
1702523SN/A     * @param pkt Packet describing this request
1712523SN/A     * @return number of ticks it took to complete
1722523SN/A     */
1732630SN/A    virtual Tick write(Packet *pkt) = 0;
174545SN/A
175545SN/A  public:
1763090Sstever@eecs.umich.edu    /** Params struct which is extended through each device based on
1773090Sstever@eecs.umich.edu     * the parameters it needs. Since we are re-writing everything, we
1783090Sstever@eecs.umich.edu     * might as well start from the bottom this time. */
1792512SN/A    struct Params
1802512SN/A    {
1812512SN/A        std::string name;
1822512SN/A        Platform *platform;
1832522SN/A        System *system;
1842512SN/A    };
1852521SN/A
1862512SN/A  protected:
1872512SN/A    Params *_params;
1882512SN/A
1892512SN/A  public:
1902512SN/A    const Params *params() const { return _params; }
1912512SN/A
1922521SN/A    PioDevice(Params *p)
1932901Ssaidi@eecs.umich.edu              : MemObject(p->name),  platform(p->platform), sys(p->system),
1942901Ssaidi@eecs.umich.edu              pioPort(NULL), _params(p)
1952512SN/A              {}
1962384SN/A
197545SN/A    virtual ~PioDevice();
1982384SN/A
1992541SN/A    virtual void init();
2002541SN/A
2012901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2022901Ssaidi@eecs.umich.edu
2032738Sstever@eecs.umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1)
2042384SN/A    {
2052521SN/A        if (if_name == "pio") {
2062512SN/A            if (pioPort != NULL)
2072512SN/A                panic("pio port already connected to.");
2082901Ssaidi@eecs.umich.edu            pioPort = new PioPort(this, sys);
2092384SN/A            return pioPort;
2102521SN/A        } else
2112384SN/A            return NULL;
2122384SN/A    }
2132489SN/A    friend class PioPort;
2142489SN/A
215545SN/A};
216545SN/A
2172512SN/Aclass BasicPioDevice : public PioDevice
2182512SN/A{
2192512SN/A  public:
2202521SN/A    struct Params :  public PioDevice::Params
2212512SN/A    {
2222512SN/A        Addr pio_addr;
2232512SN/A        Tick pio_delay;
2242512SN/A    };
2252512SN/A
2262512SN/A  protected:
2272512SN/A    /** Address that the device listens to. */
2282512SN/A    Addr pioAddr;
2292512SN/A
2302512SN/A    /** Size that the device's address range. */
2312521SN/A    Addr pioSize;
2322512SN/A
2332512SN/A    /** Delay that the device experinces on an access. */
2342512SN/A    Tick pioDelay;
2352512SN/A
2362512SN/A  public:
2372521SN/A    BasicPioDevice(Params *p)
2383090Sstever@eecs.umich.edu        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
2393090Sstever@eecs.umich.edu          pioDelay(p->pio_delay)
2402512SN/A    {}
2412512SN/A
2422539SN/A    /** return the address ranges that this device responds to.
2432982Sstever@eecs.umich.edu     * @param range_list range list to populate with ranges
2442539SN/A     */
2452542SN/A    void addressRanges(AddrRangeList &range_list);
2462539SN/A
2472512SN/A};
2482512SN/A
249545SN/Aclass DmaDevice : public PioDevice
250545SN/A{
251545SN/A  protected:
2522384SN/A    DmaPort *dmaPort;
253545SN/A
254545SN/A  public:
2552521SN/A    DmaDevice(Params *p);
256545SN/A    virtual ~DmaDevice();
2572384SN/A
2582565SN/A    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
2593293Srdreslin@umich.edu    { dmaPort->dmaAction(Packet::WriteInvalidateReq, addr, size, event, data) ; }
2602565SN/A
2612565SN/A    void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL)
2622641Sstever@eecs.umich.edu    { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); }
2632565SN/A
2642565SN/A    bool dmaPending() { return dmaPort->dmaPending(); }
2652565SN/A
2662901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2672901Ssaidi@eecs.umich.edu
2682738Sstever@eecs.umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1)
2692384SN/A    {
2702565SN/A        if (if_name == "pio") {
2712565SN/A            if (pioPort != NULL)
2722565SN/A                panic("pio port already connected to.");
2732901Ssaidi@eecs.umich.edu            pioPort = new PioPort(this, sys);
2742384SN/A            return pioPort;
2752565SN/A        } else if (if_name == "dma") {
2762565SN/A            if (dmaPort != NULL)
2772565SN/A                panic("dma port already connected to.");
2782901Ssaidi@eecs.umich.edu            dmaPort = new DmaPort(this, sys);
2792384SN/A            return dmaPort;
2802565SN/A        } else
2812384SN/A            return NULL;
2822384SN/A    }
2832489SN/A
2842489SN/A    friend class DmaPort;
285545SN/A};
286545SN/A
2872384SN/A
2881310SN/A#endif // __DEV_IO_DEVICE_HH__
289