io_device.hh revision 3918
14202Sbinkertn@umich.edu/* 24202Sbinkertn@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan 34202Sbinkertn@umich.edu * All rights reserved. 44202Sbinkertn@umich.edu * 54202Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without 64202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are 74202Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright 84202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer; 94202Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright 104202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the 114202Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution; 124202Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its 134202Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from 144202Sbinkertn@umich.edu * this software without specific prior written permission. 154202Sbinkertn@umich.edu * 164202Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 174202Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 184202Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 194202Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 204202Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 214202Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 224202Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 234202Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 244202Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 254202Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 264202Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274202Sbinkertn@umich.edu * 284202Sbinkertn@umich.edu * Authors: Ali Saidi 294202Sbinkertn@umich.edu * Nathan Binkert 304202Sbinkertn@umich.edu */ 314202Sbinkertn@umich.edu 324202Sbinkertn@umich.edu#ifndef __DEV_IO_DEVICE_HH__ 334486Sbinkertn@umich.edu#define __DEV_IO_DEVICE_HH__ 344486Sbinkertn@umich.edu 356165Ssanchezd@stanford.edu#include "mem/mem_object.hh" 366168Snate@binkert.org#include "mem/packet.hh" 374202Sbinkertn@umich.edu#include "mem/tport.hh" 384202Sbinkertn@umich.edu#include "sim/sim_object.hh" 394202Sbinkertn@umich.edu 404202Sbinkertn@umich.educlass Event; 414202Sbinkertn@umich.educlass Platform; 424202Sbinkertn@umich.educlass PioDevice; 435650Sgblack@eecs.umich.educlass DmaDevice; 446168Snate@binkert.orgclass System; 457768SAli.Saidi@ARM.com 467768SAli.Saidi@ARM.com/** 477768SAli.Saidi@ARM.com * The PioPort class is a programmed i/o port that all devices that are 487768SAli.Saidi@ARM.com * sensitive to an address range use. The port takes all the memory 497768SAli.Saidi@ARM.com * access types and roles them into one read() and write() call that the device 504202Sbinkertn@umich.edu * must respond to. The device must also provide the addressRanges() function 514202Sbinkertn@umich.edu * with which it returns the address ranges it is interested in. 527768SAli.Saidi@ARM.com */ 534202Sbinkertn@umich.educlass PioPort : public SimpleTimingPort 544202Sbinkertn@umich.edu{ 555192Ssaidi@eecs.umich.edu protected: 565192Ssaidi@eecs.umich.edu /** The device that this port serves. */ 575192Ssaidi@eecs.umich.edu PioDevice *device; 585192Ssaidi@eecs.umich.edu 595192Ssaidi@eecs.umich.edu virtual Tick recvAtomic(PacketPtr pkt); 605192Ssaidi@eecs.umich.edu 615192Ssaidi@eecs.umich.edu virtual void getDeviceAddressRanges(AddrRangeList &resp, 627780Snilay@cs.wisc.edu AddrRangeList &snoop); 637832Snate@binkert.org 647780Snilay@cs.wisc.edu public: 657780Snilay@cs.wisc.edu 667780Snilay@cs.wisc.edu PioPort(PioDevice *dev, System *s, std::string pname = "-pioport"); 677780Snilay@cs.wisc.edu}; 687780Snilay@cs.wisc.edu 697780Snilay@cs.wisc.edu 707780Snilay@cs.wisc.educlass DmaPort : public Port 717780Snilay@cs.wisc.edu{ 727780Snilay@cs.wisc.edu protected: 737780Snilay@cs.wisc.edu struct DmaReqState : public Packet::SenderState 747780Snilay@cs.wisc.edu { 757780Snilay@cs.wisc.edu /** Event to call on the device when this transaction (all packets) 767780Snilay@cs.wisc.edu * complete. */ 777780Snilay@cs.wisc.edu Event *completionEvent; 78 79 /** Where we came from for some sanity checking. */ 80 Port *outPort; 81 82 /** Total number of bytes that this transaction involves. */ 83 Addr totBytes; 84 85 /** Number of bytes that have been acked for this transaction. */ 86 Addr numBytes; 87 88 DmaReqState(Event *ce, Port *p, Addr tb) 89 : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0) 90 {} 91 }; 92 93 DmaDevice *device; 94 std::list<PacketPtr> transmitList; 95 96 /** The system that device/port are in. This is used to select which mode 97 * we are currently operating in. */ 98 System *sys; 99 100 /** Number of outstanding packets the dma port has. */ 101 int pendingCount; 102 103 /** If a dmaAction is in progress. */ 104 int actionInProgress; 105 106 /** If we need to drain, keep the drain event around until we're done 107 * here.*/ 108 Event *drainEvent; 109 110 virtual bool recvTiming(PacketPtr pkt); 111 virtual Tick recvAtomic(PacketPtr pkt) 112 { panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN } 113 virtual void recvFunctional(PacketPtr pkt) 114 { panic("dma port shouldn't be used for pio access."); } 115 116 virtual void recvStatusChange(Status status) 117 { ; } 118 119 virtual void recvRetry() ; 120 121 virtual void getDeviceAddressRanges(AddrRangeList &resp, 122 AddrRangeList &snoop) 123 { resp.clear(); snoop.clear(); } 124 125 void sendDma(PacketPtr pkt, bool front = false); 126 127 public: 128 DmaPort(DmaDevice *dev, System *s); 129 130 void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event, 131 uint8_t *data = NULL); 132 133 bool dmaPending() { return pendingCount > 0; } 134 135 unsigned int drain(Event *de); 136}; 137 138/** 139 * This device is the base class which all devices senstive to an address range 140 * inherit from. There are three pure virtual functions which all devices must 141 * implement addressRanges(), read(), and write(). The magic do choose which 142 * mode we are in, etc is handled by the PioPort so the device doesn't have to 143 * bother. 144 */ 145class PioDevice : public MemObject 146{ 147 protected: 148 149 /** The platform we are in. This is used to decide what type of memory 150 * transaction we should perform. */ 151 Platform *platform; 152 153 System *sys; 154 155 /** The pioPort that handles the requests for us and provides us requests 156 * that it sees. */ 157 PioPort *pioPort; 158 159 virtual void addressRanges(AddrRangeList &range_list) = 0; 160 161 /** Pure virtual function that the device must implement. Called 162 * when a read command is recieved by the port. 163 * @param pkt Packet describing this request 164 * @return number of ticks it took to complete 165 */ 166 virtual Tick read(PacketPtr pkt) = 0; 167 168 /** Pure virtual function that the device must implement. Called when a 169 * write command is recieved by the port. 170 * @param pkt Packet describing this request 171 * @return number of ticks it took to complete 172 */ 173 virtual Tick write(PacketPtr pkt) = 0; 174 175 public: 176 /** Params struct which is extended through each device based on 177 * the parameters it needs. Since we are re-writing everything, we 178 * might as well start from the bottom this time. */ 179 struct Params 180 { 181 std::string name; 182 Platform *platform; 183 System *system; 184 }; 185 186 protected: 187 Params *_params; 188 189 public: 190 const Params *params() const { return _params; } 191 192 PioDevice(Params *p) 193 : MemObject(p->name), platform(p->platform), sys(p->system), 194 pioPort(NULL), _params(p) 195 {} 196 197 virtual ~PioDevice(); 198 199 virtual void init(); 200 201 virtual unsigned int drain(Event *de); 202 203 virtual Port *getPort(const std::string &if_name, int idx = -1) 204 { 205 if (if_name == "pio") { 206 if (pioPort != NULL) 207 panic("pio port already connected to."); 208 pioPort = new PioPort(this, sys); 209 return pioPort; 210 } else 211 return NULL; 212 } 213 friend class PioPort; 214 215}; 216 217class BasicPioDevice : public PioDevice 218{ 219 public: 220 struct Params : public PioDevice::Params 221 { 222 Addr pio_addr; 223 Tick pio_delay; 224 }; 225 226 protected: 227 /** Address that the device listens to. */ 228 Addr pioAddr; 229 230 /** Size that the device's address range. */ 231 Addr pioSize; 232 233 /** Delay that the device experinces on an access. */ 234 Tick pioDelay; 235 236 public: 237 BasicPioDevice(Params *p) 238 : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), 239 pioDelay(p->pio_delay) 240 {} 241 242 /** return the address ranges that this device responds to. 243 * @param range_list range list to populate with ranges 244 */ 245 void addressRanges(AddrRangeList &range_list); 246 247}; 248 249class DmaDevice : public PioDevice 250{ 251 protected: 252 DmaPort *dmaPort; 253 254 public: 255 DmaDevice(Params *p); 256 virtual ~DmaDevice(); 257 258 void dmaWrite(Addr addr, int size, Event *event, uint8_t *data) 259 { dmaPort->dmaAction(Packet::WriteInvalidateReq, addr, size, event, data) ; } 260 261 void dmaRead(Addr addr, int size, Event *event, uint8_t *data = NULL) 262 { dmaPort->dmaAction(Packet::ReadReq, addr, size, event, data); } 263 264 bool dmaPending() { return dmaPort->dmaPending(); } 265 266 virtual unsigned int drain(Event *de); 267 268 virtual Port *getPort(const std::string &if_name, int idx = -1) 269 { 270 if (if_name == "pio") { 271 if (pioPort != NULL) 272 panic("pio port already connected to."); 273 pioPort = new PioPort(this, sys); 274 return pioPort; 275 } else if (if_name == "dma") { 276 if (dmaPort != NULL) 277 panic("dma port already connected to."); 278 dmaPort = new DmaPort(this, sys); 279 return dmaPort; 280 } else 281 return NULL; 282 } 283 284 friend class DmaPort; 285}; 286 287 288#endif // __DEV_IO_DEVICE_HH__ 289