io_device.hh revision 2901
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 632901Ssaidi@eecs.umich.edu /** The system that device/port are in. This is used to select which mode 642489SN/A * we are currently operating in. */ 652901Ssaidi@eecs.umich.edu System *sys; 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 852846Ssaidi@eecs.umich.edu void resendNacked(Packet *pkt); 862846Ssaidi@eecs.umich.edu 872489SN/A /** 882489SN/A * This class is used to implemented sendTiming() with a delay. When a delay 892489SN/A * is requested a new event is created. When the event time expires it 902489SN/A * attempts to send the packet. If it cannot, the packet is pushed onto the 912489SN/A * transmit list to be sent when recvRetry() is called. */ 922384SN/A class SendEvent : public Event 932384SN/A { 942384SN/A PioPort *port; 952630SN/A Packet *packet; 962384SN/A 972630SN/A SendEvent(PioPort *p, Packet *pkt, Tick t) 982631SN/A : Event(&mainEventQueue), port(p), packet(pkt) 992384SN/A { schedule(curTick + t); } 1002384SN/A 1012384SN/A virtual void process(); 1022384SN/A 1032384SN/A virtual const char *description() 1042384SN/A { return "Future scheduled sendTiming event"; } 1052384SN/A 1062384SN/A friend class PioPort; 1072489SN/A }; 1082489SN/A 1092901Ssaidi@eecs.umich.edu /** Number of timing requests that are emulating the device timing before 1102901Ssaidi@eecs.umich.edu * attempting to end up on the bus. 1112901Ssaidi@eecs.umich.edu */ 1122901Ssaidi@eecs.umich.edu int outTiming; 1132901Ssaidi@eecs.umich.edu 1142901Ssaidi@eecs.umich.edu /** If we need to drain, keep the drain event around until we're done 1152901Ssaidi@eecs.umich.edu * here.*/ 1162901Ssaidi@eecs.umich.edu Event *drainEvent; 1172901Ssaidi@eecs.umich.edu 1182489SN/A /** Schedule a sendTiming() event to be called in the future. */ 1192630SN/A void sendTiming(Packet *pkt, Tick time) 1202901Ssaidi@eecs.umich.edu { outTiming++; new PioPort::SendEvent(this, pkt, time); } 1212489SN/A 1222657Ssaidi@eecs.umich.edu /** This function is notification that the device should attempt to send a 1232657Ssaidi@eecs.umich.edu * packet again. */ 1242657Ssaidi@eecs.umich.edu virtual void recvRetry(); 1252384SN/A 1262384SN/A public: 1272901Ssaidi@eecs.umich.edu PioPort(PioDevice *dev, System *s, std::string pname = "-pioport"); 1282901Ssaidi@eecs.umich.edu 1292901Ssaidi@eecs.umich.edu unsigned int drain(Event *de); 1302384SN/A 1312489SN/A friend class PioPort::SendEvent; 1322384SN/A}; 1332384SN/A 1342565SN/A 1352384SN/Aclass DmaPort : public Port 1362384SN/A{ 1372384SN/A protected: 1382784Ssaidi@eecs.umich.edu struct DmaReqState : public Packet::SenderState 1392784Ssaidi@eecs.umich.edu { 1402784Ssaidi@eecs.umich.edu /** Event to call on the device when this transaction (all packets) 1412784Ssaidi@eecs.umich.edu * complete. */ 1422784Ssaidi@eecs.umich.edu Event *completionEvent; 1432784Ssaidi@eecs.umich.edu 1442784Ssaidi@eecs.umich.edu /** Where we came from for some sanity checking. */ 1452784Ssaidi@eecs.umich.edu Port *outPort; 1462784Ssaidi@eecs.umich.edu 1472784Ssaidi@eecs.umich.edu /** Total number of bytes that this transaction involves. */ 1482784Ssaidi@eecs.umich.edu Addr totBytes; 1492784Ssaidi@eecs.umich.edu 1502784Ssaidi@eecs.umich.edu /** Number of bytes that have been acked for this transaction. */ 1512784Ssaidi@eecs.umich.edu Addr numBytes; 1522784Ssaidi@eecs.umich.edu 1532784Ssaidi@eecs.umich.edu DmaReqState(Event *ce, Port *p, Addr tb) 1542784Ssaidi@eecs.umich.edu : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0) 1552784Ssaidi@eecs.umich.edu {} 1562784Ssaidi@eecs.umich.edu }; 1572784Ssaidi@eecs.umich.edu 1582565SN/A DmaDevice *device; 1592384SN/A std::list<Packet*> transmitList; 1602384SN/A 1612901Ssaidi@eecs.umich.edu /** The system that device/port are in. This is used to select which mode 1622565SN/A * we are currently operating in. */ 1632901Ssaidi@eecs.umich.edu System *sys; 1642565SN/A 1652565SN/A /** Number of outstanding packets the dma port has. */ 1662565SN/A int pendingCount; 1672384SN/A 1682901Ssaidi@eecs.umich.edu /** If a dmaAction is in progress. */ 1692901Ssaidi@eecs.umich.edu int actionInProgress; 1702901Ssaidi@eecs.umich.edu 1712901Ssaidi@eecs.umich.edu /** If we need to drain, keep the drain event around until we're done 1722901Ssaidi@eecs.umich.edu * here.*/ 1732901Ssaidi@eecs.umich.edu Event *drainEvent; 1742901Ssaidi@eecs.umich.edu 1752630SN/A virtual bool recvTiming(Packet *pkt); 1762630SN/A virtual Tick recvAtomic(Packet *pkt) 1772384SN/A { panic("dma port shouldn't be used for pio access."); } 1782630SN/A virtual void recvFunctional(Packet *pkt) 1792384SN/A { panic("dma port shouldn't be used for pio access."); } 1802384SN/A 1812384SN/A virtual void recvStatusChange(Status status) 1822384SN/A { ; } 1832384SN/A 1842657Ssaidi@eecs.umich.edu virtual void recvRetry() ; 1852384SN/A 1862521SN/A virtual void getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop) 1872521SN/A { resp.clear(); snoop.clear(); } 1882384SN/A 1892685Ssaidi@eecs.umich.edu void sendDma(Packet *pkt, bool front = false); 1902489SN/A 1912384SN/A public: 1922901Ssaidi@eecs.umich.edu DmaPort(DmaDevice *dev, System *s); 1932565SN/A 1942641Sstever@eecs.umich.edu void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event, 1952641Sstever@eecs.umich.edu uint8_t *data = NULL); 1962565SN/A 1972565SN/A bool dmaPending() { return pendingCount > 0; } 1982384SN/A 1992901Ssaidi@eecs.umich.edu unsigned int drain(Event *de); 2002384SN/A}; 2012384SN/A 2022489SN/A/** 2032489SN/A * This device is the base class which all devices senstive to an address range 2042489SN/A * inherit from. There are three pure virtual functions which all devices must 2052489SN/A * implement addressRanges(), read(), and write(). The magic do choose which 2062489SN/A * mode we are in, etc is handled by the PioPort so the device doesn't have to 2072489SN/A * bother. 2082489SN/A */ 2092384SN/A 2102542SN/Aclass PioDevice : public MemObject 2112384SN/A{ 2122384SN/A protected: 2132384SN/A 2142489SN/A /** The platform we are in. This is used to decide what type of memory 2152489SN/A * transaction we should perform. */ 2161310SN/A Platform *platform; 2172384SN/A 2182901Ssaidi@eecs.umich.edu System *sys; 2192901Ssaidi@eecs.umich.edu 2202489SN/A /** The pioPort that handles the requests for us and provides us requests 2212489SN/A * that it sees. */ 2222384SN/A PioPort *pioPort; 2232384SN/A 2242521SN/A virtual void addressRanges(AddrRangeList &range_list) = 0; 2252384SN/A 2262489SN/A /** As far as the devices are concerned they only accept atomic transactions 2272489SN/A * which are converted to either a write or a read. */ 2282630SN/A Tick recvAtomic(Packet *pkt) 2292641Sstever@eecs.umich.edu { return pkt->isRead() ? this->read(pkt) : this->write(pkt); } 2302384SN/A 2312489SN/A /** Pure virtual function that the device must implement. Called when a read 2322523SN/A * command is recieved by the port. 2332523SN/A * @param pkt Packet describing this request 2342523SN/A * @return number of ticks it took to complete 2352523SN/A */ 2362630SN/A virtual Tick read(Packet *pkt) = 0; 2372384SN/A 2382489SN/A /** Pure virtual function that the device must implement. Called when a 2392523SN/A * write command is recieved by the port. 2402523SN/A * @param pkt Packet describing this request 2412523SN/A * @return number of ticks it took to complete 2422523SN/A */ 2432630SN/A virtual Tick write(Packet *pkt) = 0; 244545SN/A 245545SN/A public: 2462512SN/A /** Params struct which is extended through each device based on the 2472512SN/A * parameters it needs. Since we are re-writing everything, we might as well 2482512SN/A * start from the bottom this time. */ 2492512SN/A 2502512SN/A struct Params 2512512SN/A { 2522512SN/A std::string name; 2532512SN/A Platform *platform; 2542522SN/A System *system; 2552512SN/A }; 2562521SN/A 2572512SN/A protected: 2582512SN/A Params *_params; 2592512SN/A 2602512SN/A public: 2612512SN/A const Params *params() const { return _params; } 2622512SN/A 2632521SN/A PioDevice(Params *p) 2642901Ssaidi@eecs.umich.edu : MemObject(p->name), platform(p->platform), sys(p->system), 2652901Ssaidi@eecs.umich.edu pioPort(NULL), _params(p) 2662512SN/A {} 2672384SN/A 268545SN/A virtual ~PioDevice(); 2692384SN/A 2702541SN/A virtual void init(); 2712541SN/A 2722901Ssaidi@eecs.umich.edu virtual unsigned int drain(Event *de); 2732901Ssaidi@eecs.umich.edu 2742738Sstever@eecs.umich.edu virtual Port *getPort(const std::string &if_name, int idx = -1) 2752384SN/A { 2762521SN/A if (if_name == "pio") { 2772512SN/A if (pioPort != NULL) 2782512SN/A panic("pio port already connected to."); 2792901Ssaidi@eecs.umich.edu pioPort = new PioPort(this, sys); 2802384SN/A return pioPort; 2812521SN/A } else 2822384SN/A return NULL; 2832384SN/A } 2842489SN/A friend class PioPort; 2852489SN/A 286545SN/A}; 287545SN/A 2882512SN/Aclass BasicPioDevice : public PioDevice 2892512SN/A{ 2902512SN/A public: 2912521SN/A struct Params : public PioDevice::Params 2922512SN/A { 2932512SN/A Addr pio_addr; 2942512SN/A Tick pio_delay; 2952512SN/A }; 2962512SN/A 2972512SN/A protected: 2982512SN/A /** Address that the device listens to. */ 2992512SN/A Addr pioAddr; 3002512SN/A 3012512SN/A /** Size that the device's address range. */ 3022521SN/A Addr pioSize; 3032512SN/A 3042512SN/A /** Delay that the device experinces on an access. */ 3052512SN/A Tick pioDelay; 3062512SN/A 3072512SN/A public: 3082521SN/A BasicPioDevice(Params *p) 3092521SN/A : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), pioDelay(p->pio_delay) 3102512SN/A {} 3112512SN/A 3122539SN/A /** return the address ranges that this device responds to. 3132539SN/A * @params range_list range list to populate with ranges 3142539SN/A */ 3152542SN/A void addressRanges(AddrRangeList &range_list); 3162539SN/A 3172512SN/A}; 3182512SN/A 319545SN/Aclass DmaDevice : public PioDevice 320545SN/A{ 321545SN/A protected: 3222384SN/A DmaPort *dmaPort; 323545SN/A 324545SN/A public: 3252521SN/A DmaDevice(Params *p); 326545SN/A virtual ~DmaDevice(); 3272384SN/A 3282565SN/A void dmaWrite(Addr addr, int size, Event *event, uint8_t *data) 3292641Sstever@eecs.umich.edu { dmaPort->dmaAction(Packet::WriteReq, addr, size, event, data) ; } 3302565SN/A 3312565SN/A void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL) 3322641Sstever@eecs.umich.edu { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); } 3332565SN/A 3342565SN/A bool dmaPending() { return dmaPort->dmaPending(); } 3352565SN/A 3362901Ssaidi@eecs.umich.edu virtual unsigned int drain(Event *de); 3372901Ssaidi@eecs.umich.edu 3382738Sstever@eecs.umich.edu virtual Port *getPort(const std::string &if_name, int idx = -1) 3392384SN/A { 3402565SN/A if (if_name == "pio") { 3412565SN/A if (pioPort != NULL) 3422565SN/A panic("pio port already connected to."); 3432901Ssaidi@eecs.umich.edu pioPort = new PioPort(this, sys); 3442384SN/A return pioPort; 3452565SN/A } else if (if_name == "dma") { 3462565SN/A if (dmaPort != NULL) 3472565SN/A panic("dma port already connected to."); 3482901Ssaidi@eecs.umich.edu dmaPort = new DmaPort(this, sys); 3492384SN/A return dmaPort; 3502565SN/A } else 3512384SN/A return NULL; 3522384SN/A } 3532489SN/A 3542489SN/A friend class DmaPort; 355545SN/A}; 356545SN/A 3572384SN/A 3581310SN/A#endif // __DEV_IO_DEVICE_HH__ 359