io_device.hh revision 5578:db6756431717
19651SAndreas.Sandberg@ARM.com/* 29651SAndreas.Sandberg@ARM.com * Copyright (c) 2004-2005 The Regents of The University of Michigan 39651SAndreas.Sandberg@ARM.com * All rights reserved. 49651SAndreas.Sandberg@ARM.com * 59651SAndreas.Sandberg@ARM.com * Redistribution and use in source and binary forms, with or without 69651SAndreas.Sandberg@ARM.com * modification, are permitted provided that the following conditions are 79651SAndreas.Sandberg@ARM.com * met: redistributions of source code must retain the above copyright 89651SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer; 99651SAndreas.Sandberg@ARM.com * redistributions in binary form must reproduce the above copyright 109651SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer in the 119651SAndreas.Sandberg@ARM.com * documentation and/or other materials provided with the distribution; 129651SAndreas.Sandberg@ARM.com * neither the name of the copyright holders nor the names of its 139651SAndreas.Sandberg@ARM.com * contributors may be used to endorse or promote products derived from 149651SAndreas.Sandberg@ARM.com * this software without specific prior written permission. 159651SAndreas.Sandberg@ARM.com * 169651SAndreas.Sandberg@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 179651SAndreas.Sandberg@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 189651SAndreas.Sandberg@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 199651SAndreas.Sandberg@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 209651SAndreas.Sandberg@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 219651SAndreas.Sandberg@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 229651SAndreas.Sandberg@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 239651SAndreas.Sandberg@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 249651SAndreas.Sandberg@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 259651SAndreas.Sandberg@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 269651SAndreas.Sandberg@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 279651SAndreas.Sandberg@ARM.com * 289651SAndreas.Sandberg@ARM.com * Authors: Ali Saidi 299651SAndreas.Sandberg@ARM.com * Nathan Binkert 309651SAndreas.Sandberg@ARM.com */ 319651SAndreas.Sandberg@ARM.com 329651SAndreas.Sandberg@ARM.com#ifndef __DEV_IO_DEVICE_HH__ 339651SAndreas.Sandberg@ARM.com#define __DEV_IO_DEVICE_HH__ 349651SAndreas.Sandberg@ARM.com 359651SAndreas.Sandberg@ARM.com#include "base/fast_alloc.hh" 369651SAndreas.Sandberg@ARM.com#include "mem/mem_object.hh" 379651SAndreas.Sandberg@ARM.com#include "mem/packet.hh" 389651SAndreas.Sandberg@ARM.com#include "mem/tport.hh" 399651SAndreas.Sandberg@ARM.com#include "params/BasicPioDevice.hh" 409881Sandreas@sandberg.pp.se#include "params/DmaDevice.hh" 419651SAndreas.Sandberg@ARM.com#include "params/PioDevice.hh" 429651SAndreas.Sandberg@ARM.com#include "sim/sim_object.hh" 439651SAndreas.Sandberg@ARM.com 449651SAndreas.Sandberg@ARM.comclass Event; 459651SAndreas.Sandberg@ARM.comclass Platform; 469651SAndreas.Sandberg@ARM.comclass PioDevice; 479651SAndreas.Sandberg@ARM.comclass DmaDevice; 489651SAndreas.Sandberg@ARM.comclass System; 499881Sandreas@sandberg.pp.se 509881Sandreas@sandberg.pp.se/** 519881Sandreas@sandberg.pp.se * The PioPort class is a programmed i/o port that all devices that are 529881Sandreas@sandberg.pp.se * sensitive to an address range use. The port takes all the memory 539881Sandreas@sandberg.pp.se * access types and roles them into one read() and write() call that the device 549881Sandreas@sandberg.pp.se * must respond to. The device must also provide the addressRanges() function 559881Sandreas@sandberg.pp.se * with which it returns the address ranges it is interested in. 569881Sandreas@sandberg.pp.se */ 579651SAndreas.Sandberg@ARM.comclass PioPort : public SimpleTimingPort 589651SAndreas.Sandberg@ARM.com{ 599651SAndreas.Sandberg@ARM.com protected: 609651SAndreas.Sandberg@ARM.com /** The device that this port serves. */ 619651SAndreas.Sandberg@ARM.com PioDevice *device; 629651SAndreas.Sandberg@ARM.com 639651SAndreas.Sandberg@ARM.com virtual Tick recvAtomic(PacketPtr pkt); 649651SAndreas.Sandberg@ARM.com 659651SAndreas.Sandberg@ARM.com virtual void getDeviceAddressRanges(AddrRangeList &resp, 669651SAndreas.Sandberg@ARM.com bool &snoop); 679651SAndreas.Sandberg@ARM.com 689651SAndreas.Sandberg@ARM.com public: 699651SAndreas.Sandberg@ARM.com 709651SAndreas.Sandberg@ARM.com PioPort(PioDevice *dev, System *s, std::string pname = "-pioport"); 719734Sandreas@sandberg.pp.se}; 729734Sandreas@sandberg.pp.se 739734Sandreas@sandberg.pp.se 749734Sandreas@sandberg.pp.seclass DmaPort : public Port 759734Sandreas@sandberg.pp.se{ 769651SAndreas.Sandberg@ARM.com protected: 779651SAndreas.Sandberg@ARM.com struct DmaReqState : public Packet::SenderState, public FastAlloc 789651SAndreas.Sandberg@ARM.com { 799651SAndreas.Sandberg@ARM.com /** Event to call on the device when this transaction (all packets) 809651SAndreas.Sandberg@ARM.com * complete. */ 819651SAndreas.Sandberg@ARM.com Event *completionEvent; 829651SAndreas.Sandberg@ARM.com 839651SAndreas.Sandberg@ARM.com /** Where we came from for some sanity checking. */ 849651SAndreas.Sandberg@ARM.com Port *outPort; 859651SAndreas.Sandberg@ARM.com 869651SAndreas.Sandberg@ARM.com /** Total number of bytes that this transaction involves. */ 879651SAndreas.Sandberg@ARM.com Addr totBytes; 889651SAndreas.Sandberg@ARM.com 899651SAndreas.Sandberg@ARM.com /** Number of bytes that have been acked for this transaction. */ 909651SAndreas.Sandberg@ARM.com Addr numBytes; 919651SAndreas.Sandberg@ARM.com 929651SAndreas.Sandberg@ARM.com /** Amount to delay completion of dma by */ 939651SAndreas.Sandberg@ARM.com Tick delay; 949881Sandreas@sandberg.pp.se 959881Sandreas@sandberg.pp.se DmaReqState(Event *ce, Port *p, Addr tb, Tick _delay) 969651SAndreas.Sandberg@ARM.com : completionEvent(ce), outPort(p), totBytes(tb), numBytes(0), 979651SAndreas.Sandberg@ARM.com delay(_delay) 989651SAndreas.Sandberg@ARM.com {} 999651SAndreas.Sandberg@ARM.com }; 1009651SAndreas.Sandberg@ARM.com 1019651SAndreas.Sandberg@ARM.com DmaDevice *device; 1029651SAndreas.Sandberg@ARM.com std::list<PacketPtr> transmitList; 1039651SAndreas.Sandberg@ARM.com 1049651SAndreas.Sandberg@ARM.com /** The system that device/port are in. This is used to select which mode 1059651SAndreas.Sandberg@ARM.com * we are currently operating in. */ 1069651SAndreas.Sandberg@ARM.com System *sys; 1079651SAndreas.Sandberg@ARM.com 1089651SAndreas.Sandberg@ARM.com /** Number of outstanding packets the dma port has. */ 1099651SAndreas.Sandberg@ARM.com int pendingCount; 1109651SAndreas.Sandberg@ARM.com 1119651SAndreas.Sandberg@ARM.com /** If a dmaAction is in progress. */ 1129651SAndreas.Sandberg@ARM.com int actionInProgress; 1139651SAndreas.Sandberg@ARM.com 1149651SAndreas.Sandberg@ARM.com /** If we need to drain, keep the drain event around until we're done 1159651SAndreas.Sandberg@ARM.com * here.*/ 1169651SAndreas.Sandberg@ARM.com Event *drainEvent; 1179651SAndreas.Sandberg@ARM.com 1189651SAndreas.Sandberg@ARM.com /** time to wait between sending another packet, increases as NACKs are 1199651SAndreas.Sandberg@ARM.com * recived, decreases as responses are recived. */ 1209651SAndreas.Sandberg@ARM.com Tick backoffTime; 1219651SAndreas.Sandberg@ARM.com 1229651SAndreas.Sandberg@ARM.com /** If the port is currently waiting for a retry before it can send whatever 1239881Sandreas@sandberg.pp.se * it is that it's sending. */ 1249881Sandreas@sandberg.pp.se bool inRetry; 1259881Sandreas@sandberg.pp.se 1269881Sandreas@sandberg.pp.se virtual bool recvTiming(PacketPtr pkt); 1279881Sandreas@sandberg.pp.se virtual Tick recvAtomic(PacketPtr pkt) 1289881Sandreas@sandberg.pp.se { panic("dma port shouldn't be used for pio access."); M5_DUMMY_RETURN } 1299881Sandreas@sandberg.pp.se virtual void recvFunctional(PacketPtr pkt) 1309881Sandreas@sandberg.pp.se { panic("dma port shouldn't be used for pio access."); } 1319881Sandreas@sandberg.pp.se 1329881Sandreas@sandberg.pp.se virtual void recvStatusChange(Status status) 1339881Sandreas@sandberg.pp.se { ; } 1349881Sandreas@sandberg.pp.se 1359881Sandreas@sandberg.pp.se virtual void recvRetry() ; 1369881Sandreas@sandberg.pp.se 1379881Sandreas@sandberg.pp.se virtual void getDeviceAddressRanges(AddrRangeList &resp, 1389651SAndreas.Sandberg@ARM.com bool &snoop) 1399881Sandreas@sandberg.pp.se { resp.clear(); snoop = false; } 1409651SAndreas.Sandberg@ARM.com 1419655SAndreas.Sandberg@ARM.com void queueDma(PacketPtr pkt, bool front = false); 1429655SAndreas.Sandberg@ARM.com void sendDma(); 1439655SAndreas.Sandberg@ARM.com 1449655SAndreas.Sandberg@ARM.com /** event to give us a kick every time we backoff time is reached. */ 1459655SAndreas.Sandberg@ARM.com EventWrapper<DmaPort, &DmaPort::sendDma> backoffEvent; 1469655SAndreas.Sandberg@ARM.com 1479655SAndreas.Sandberg@ARM.com public: 1489655SAndreas.Sandberg@ARM.com DmaPort(DmaDevice *dev, System *s); 1499655SAndreas.Sandberg@ARM.com 1509655SAndreas.Sandberg@ARM.com void dmaAction(Packet::Command cmd, Addr addr, int size, Event *event, 1519655SAndreas.Sandberg@ARM.com uint8_t *data, Tick delay); 1529655SAndreas.Sandberg@ARM.com 1539655SAndreas.Sandberg@ARM.com bool dmaPending() { return pendingCount > 0; } 1549655SAndreas.Sandberg@ARM.com 1559655SAndreas.Sandberg@ARM.com int cacheBlockSize() { return peerBlockSize(); } 1569655SAndreas.Sandberg@ARM.com unsigned int drain(Event *de); 1579655SAndreas.Sandberg@ARM.com}; 1589655SAndreas.Sandberg@ARM.com 1599655SAndreas.Sandberg@ARM.com/** 1609655SAndreas.Sandberg@ARM.com * This device is the base class which all devices senstive to an address range 1619655SAndreas.Sandberg@ARM.com * inherit from. There are three pure virtual functions which all devices must 1629655SAndreas.Sandberg@ARM.com * implement addressRanges(), read(), and write(). The magic do choose which 1639655SAndreas.Sandberg@ARM.com * mode we are in, etc is handled by the PioPort so the device doesn't have to 1649655SAndreas.Sandberg@ARM.com * bother. 1659655SAndreas.Sandberg@ARM.com */ 1669655SAndreas.Sandberg@ARM.comclass PioDevice : public MemObject 1679655SAndreas.Sandberg@ARM.com{ 1689655SAndreas.Sandberg@ARM.com protected: 1699655SAndreas.Sandberg@ARM.com 1709655SAndreas.Sandberg@ARM.com /** The platform we are in. This is used to decide what type of memory 1719881Sandreas@sandberg.pp.se * transaction we should perform. */ 1729655SAndreas.Sandberg@ARM.com Platform *platform; 173 174 System *sys; 175 176 /** The pioPort that handles the requests for us and provides us requests 177 * that it sees. */ 178 PioPort *pioPort; 179 180 virtual void addressRanges(AddrRangeList &range_list) = 0; 181 182 /** Pure virtual function that the device must implement. Called 183 * when a read command is recieved by the port. 184 * @param pkt Packet describing this request 185 * @return number of ticks it took to complete 186 */ 187 virtual Tick read(PacketPtr pkt) = 0; 188 189 /** Pure virtual function that the device must implement. Called when a 190 * write command is recieved by the port. 191 * @param pkt Packet describing this request 192 * @return number of ticks it took to complete 193 */ 194 virtual Tick write(PacketPtr pkt) = 0; 195 196 public: 197 typedef PioDeviceParams Params; 198 PioDevice(const Params *p); 199 virtual ~PioDevice(); 200 201 const Params * 202 params() const 203 { 204 return dynamic_cast<const Params *>(_params); 205 } 206 207 virtual void init(); 208 209 virtual unsigned int drain(Event *de); 210 211 virtual Port *getPort(const std::string &if_name, int idx = -1) 212 { 213 if (if_name == "pio") { 214 if (pioPort != NULL) 215 fatal("%s: pio port already connected to %s", 216 name(), pioPort->getPeer()->name()); 217 pioPort = new PioPort(this, sys); 218 return pioPort; 219 } else 220 return NULL; 221 } 222 friend class PioPort; 223 224}; 225 226class BasicPioDevice : public PioDevice 227{ 228 protected: 229 /** Address that the device listens to. */ 230 Addr pioAddr; 231 232 /** Size that the device's address range. */ 233 Addr pioSize; 234 235 /** Delay that the device experinces on an access. */ 236 Tick pioDelay; 237 238 public: 239 typedef BasicPioDeviceParams Params; 240 BasicPioDevice(const Params *p); 241 242 const Params * 243 params() const 244 { 245 return dynamic_cast<const Params *>(_params); 246 } 247 248 /** return the address ranges that this device responds to. 249 * @param range_list range list to populate with ranges 250 */ 251 void addressRanges(AddrRangeList &range_list); 252 253}; 254 255class DmaDevice : public PioDevice 256{ 257 protected: 258 DmaPort *dmaPort; 259 Tick minBackoffDelay; 260 Tick maxBackoffDelay; 261 262 public: 263 typedef DmaDeviceParams Params; 264 DmaDevice(const Params *p); 265 virtual ~DmaDevice(); 266 267 const Params * 268 params() const 269 { 270 return dynamic_cast<const Params *>(_params); 271 } 272 273 void dmaWrite(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0) 274 { 275 dmaPort->dmaAction(MemCmd::WriteReq, addr, size, event, data, delay); 276 } 277 278 void dmaRead(Addr addr, int size, Event *event, uint8_t *data, Tick delay = 0) 279 { 280 dmaPort->dmaAction(MemCmd::ReadReq, addr, size, event, data, delay); 281 } 282 283 bool dmaPending() { return dmaPort->dmaPending(); } 284 285 virtual unsigned int drain(Event *de); 286 287 int cacheBlockSize() { return dmaPort->cacheBlockSize(); } 288 289 virtual Port *getPort(const std::string &if_name, int idx = -1) 290 { 291 if (if_name == "pio") { 292 if (pioPort != NULL) 293 fatal("%s: pio port already connected to %s", 294 name(), pioPort->getPeer()->name()); 295 pioPort = new PioPort(this, sys); 296 return pioPort; 297 } else if (if_name == "dma") { 298 if (dmaPort != NULL) 299 fatal("%s: dma port already connected to %s", 300 name(), pioPort->getPeer()->name()); 301 dmaPort = new DmaPort(this, sys); 302 return dmaPort; 303 } else 304 return NULL; 305 } 306 307 friend class DmaPort; 308}; 309 310 311#endif // __DEV_IO_DEVICE_HH__ 312