io_device.hh revision 4475
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
593349Sbinkertn@umich.edu    virtual Tick recvAtomic(PacketPtr pkt);
602384SN/A
613090Sstever@eecs.umich.edu    virtual void getDeviceAddressRanges(AddrRangeList &resp,
624475Sstever@eecs.umich.edu                                        bool &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;
943349Sbinkertn@umich.edu    std::list<PacketPtr> 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
1104435Ssaidi@eecs.umich.edu    /** time to wait between sending another packet, increases as NACKs are
1114435Ssaidi@eecs.umich.edu     * recived, decreases as responses are recived. */
1124435Ssaidi@eecs.umich.edu    Tick backoffTime;
1134435Ssaidi@eecs.umich.edu
1144435Ssaidi@eecs.umich.edu    /** If the port is currently waiting for a retry before it can send whatever
1154435Ssaidi@eecs.umich.edu     * it is that it's sending. */
1164435Ssaidi@eecs.umich.edu    bool inRetry;
1174435Ssaidi@eecs.umich.edu
1183349Sbinkertn@umich.edu    virtual bool recvTiming(PacketPtr pkt);
1193349Sbinkertn@umich.edu    virtual Tick recvAtomic(PacketPtr pkt)
1203918Ssaidi@eecs.umich.edu    { panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN }
1213349Sbinkertn@umich.edu    virtual void recvFunctional(PacketPtr pkt)
1222384SN/A    { panic("dma port shouldn't be used for pio access."); }
1232384SN/A
1242384SN/A    virtual void recvStatusChange(Status status)
1252384SN/A    { ; }
1262384SN/A
1272657Ssaidi@eecs.umich.edu    virtual void recvRetry() ;
1282384SN/A
1293090Sstever@eecs.umich.edu    virtual void getDeviceAddressRanges(AddrRangeList &resp,
1304475Sstever@eecs.umich.edu                                        bool &snoop)
1314475Sstever@eecs.umich.edu    { resp.clear(); snoop = false; }
1322384SN/A
1334435Ssaidi@eecs.umich.edu    void queueDma(PacketPtr pkt, bool front = false);
1344435Ssaidi@eecs.umich.edu    void sendDma();
1354435Ssaidi@eecs.umich.edu
1364435Ssaidi@eecs.umich.edu    /** event to give us a kick every time we backoff time is reached. */
1374435Ssaidi@eecs.umich.edu    EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent;
1382489SN/A
1392384SN/A  public:
1402901Ssaidi@eecs.umich.edu    DmaPort(DmaDevice *dev, System *s);
1412565SN/A
1422641Sstever@eecs.umich.edu    void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event,
1432641Sstever@eecs.umich.edu                   uint8_t *data = NULL);
1442565SN/A
1452565SN/A    bool dmaPending() { return pendingCount > 0; }
1462384SN/A
1474263Ssaidi@eecs.umich.edu    int cacheBlockSize() { return peerBlockSize(); }
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 */
1582542SN/Aclass PioDevice : public MemObject
1592384SN/A{
1602384SN/A  protected:
1612384SN/A
1622489SN/A    /** The platform we are in. This is used to decide what type of memory
1632489SN/A     * transaction we should perform. */
1641310SN/A    Platform *platform;
1652384SN/A
1662901Ssaidi@eecs.umich.edu    System *sys;
1672901Ssaidi@eecs.umich.edu
1682489SN/A    /** The pioPort that handles the requests for us and provides us requests
1692489SN/A     * that it sees. */
1702384SN/A    PioPort *pioPort;
1712384SN/A
1722521SN/A    virtual void addressRanges(AddrRangeList &range_list) = 0;
1732384SN/A
1743090Sstever@eecs.umich.edu    /** Pure virtual function that the device must implement. Called
1753090Sstever@eecs.umich.edu     * when a read command is recieved by the port.
1762523SN/A     * @param pkt Packet describing this request
1772523SN/A     * @return number of ticks it took to complete
1782523SN/A     */
1793349Sbinkertn@umich.edu    virtual Tick read(PacketPtr pkt) = 0;
1802384SN/A
1812489SN/A    /** Pure virtual function that the device must implement. Called when a
1822523SN/A     * write command is recieved by the port.
1832523SN/A     * @param pkt Packet describing this request
1842523SN/A     * @return number of ticks it took to complete
1852523SN/A     */
1863349Sbinkertn@umich.edu    virtual Tick write(PacketPtr pkt) = 0;
187545SN/A
188545SN/A  public:
1893090Sstever@eecs.umich.edu    /** Params struct which is extended through each device based on
1903090Sstever@eecs.umich.edu     * the parameters it needs. Since we are re-writing everything, we
1913090Sstever@eecs.umich.edu     * might as well start from the bottom this time. */
1922512SN/A    struct Params
1932512SN/A    {
1942512SN/A        std::string name;
1952512SN/A        Platform *platform;
1962522SN/A        System *system;
1972512SN/A    };
1982521SN/A
1992512SN/A  protected:
2002512SN/A    Params *_params;
2012512SN/A
2022512SN/A  public:
2032512SN/A    const Params *params() const { return _params; }
2042512SN/A
2052521SN/A    PioDevice(Params *p)
2062901Ssaidi@eecs.umich.edu              : MemObject(p->name),  platform(p->platform), sys(p->system),
2072901Ssaidi@eecs.umich.edu              pioPort(NULL), _params(p)
2082512SN/A              {}
2092384SN/A
210545SN/A    virtual ~PioDevice();
2112384SN/A
2122541SN/A    virtual void init();
2132541SN/A
2142901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2152901Ssaidi@eecs.umich.edu
2162738Sstever@eecs.umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1)
2172384SN/A    {
2182521SN/A        if (if_name == "pio") {
2192512SN/A            if (pioPort != NULL)
2202512SN/A                panic("pio port already connected to.");
2212901Ssaidi@eecs.umich.edu            pioPort = new PioPort(this, sys);
2222384SN/A            return pioPort;
2232521SN/A        } else
2242384SN/A            return NULL;
2252384SN/A    }
2262489SN/A    friend class PioPort;
2272489SN/A
228545SN/A};
229545SN/A
2302512SN/Aclass BasicPioDevice : public PioDevice
2312512SN/A{
2322512SN/A  public:
2332521SN/A    struct Params :  public PioDevice::Params
2342512SN/A    {
2352512SN/A        Addr pio_addr;
2362512SN/A        Tick pio_delay;
2372512SN/A    };
2382512SN/A
2392512SN/A  protected:
2402512SN/A    /** Address that the device listens to. */
2412512SN/A    Addr pioAddr;
2422512SN/A
2432512SN/A    /** Size that the device's address range. */
2442521SN/A    Addr pioSize;
2452512SN/A
2462512SN/A    /** Delay that the device experinces on an access. */
2472512SN/A    Tick pioDelay;
2482512SN/A
2492512SN/A  public:
2502521SN/A    BasicPioDevice(Params *p)
2513090Sstever@eecs.umich.edu        : PioDevice(p), pioAddr(p->pio_addr), pioSize(0),
2523090Sstever@eecs.umich.edu          pioDelay(p->pio_delay)
2532512SN/A    {}
2542512SN/A
2552539SN/A    /** return the address ranges that this device responds to.
2562982Sstever@eecs.umich.edu     * @param range_list range list to populate with ranges
2572539SN/A     */
2582542SN/A    void addressRanges(AddrRangeList &range_list);
2592539SN/A
2602512SN/A};
2612512SN/A
262545SN/Aclass DmaDevice : public PioDevice
263545SN/A{
2644435Ssaidi@eecs.umich.edu  public:
2654435Ssaidi@eecs.umich.edu    struct Params :  public PioDevice::Params
2664435Ssaidi@eecs.umich.edu    {
2674435Ssaidi@eecs.umich.edu        Tick min_backoff_delay;
2684435Ssaidi@eecs.umich.edu        Tick max_backoff_delay;
2694435Ssaidi@eecs.umich.edu    };
2704435Ssaidi@eecs.umich.edu
2714435Ssaidi@eecs.umich.edu   protected:
2722384SN/A    DmaPort *dmaPort;
2734435Ssaidi@eecs.umich.edu    Tick minBackoffDelay;
2744435Ssaidi@eecs.umich.edu    Tick maxBackoffDelay;
275545SN/A
276545SN/A  public:
2772521SN/A    DmaDevice(Params *p);
278545SN/A    virtual ~DmaDevice();
2792384SN/A
2802565SN/A    void dmaWrite(Addr addr, int size, Event *event, uint8_t *data)
2814022Sstever@eecs.umich.edu    {
2824022Sstever@eecs.umich.edu        dmaPort->dmaAction(MemCmd::WriteInvalidateReq,
2834022Sstever@eecs.umich.edu                           addr, size, event, data);
2844022Sstever@eecs.umich.edu    }
2852565SN/A
2864263Ssaidi@eecs.umich.edu    void dmaRead(Addr addr, int size, Event *event, uint8_t *data)
2874263Ssaidi@eecs.umich.edu    {
2884263Ssaidi@eecs.umich.edu        dmaPort->dmaAction(MemCmd::ReadReq, addr, size, event, data);
2894263Ssaidi@eecs.umich.edu    }
2902565SN/A
2912565SN/A    bool dmaPending() { return dmaPort->dmaPending(); }
2922565SN/A
2932901Ssaidi@eecs.umich.edu    virtual unsigned int drain(Event *de);
2942901Ssaidi@eecs.umich.edu
2954263Ssaidi@eecs.umich.edu    int cacheBlockSize() { return dmaPort->cacheBlockSize(); }
2964263Ssaidi@eecs.umich.edu
2972738Sstever@eecs.umich.edu    virtual Port *getPort(const std::string &if_name, int idx = -1)
2982384SN/A    {
2992565SN/A        if (if_name == "pio") {
3002565SN/A            if (pioPort != NULL)
3012565SN/A                panic("pio port already connected to.");
3022901Ssaidi@eecs.umich.edu            pioPort = new PioPort(this, sys);
3032384SN/A            return pioPort;
3042565SN/A        } else if (if_name == "dma") {
3052565SN/A            if (dmaPort != NULL)
3062565SN/A                panic("dma port already connected to.");
3072901Ssaidi@eecs.umich.edu            dmaPort = new DmaPort(this, sys);
3082384SN/A            return dmaPort;
3092565SN/A        } else
3102384SN/A            return NULL;
3112384SN/A    }
3122489SN/A
3132489SN/A    friend class DmaPort;
314545SN/A};
315545SN/A
3162384SN/A
3171310SN/A#endif // __DEV_IO_DEVICE_HH__
318