dma_device.hh revision 4263
1/* 2 * Copyright (c) 2004-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Ali Saidi 29 * Nathan Binkert 30 */ 31 32#ifndef __DEV_IO_DEVICE_HH__ 33#define __DEV_IO_DEVICE_HH__ 34 35#include "mem/mem_object.hh" 36#include "mem/packet.hh" 37#include "mem/tport.hh" 38#include "sim/sim_object.hh" 39 40class Event; 41class Platform; 42class PioDevice; 43class DmaDevice; 44class System; 45 46/** 47 * The PioPort class is a programmed i/o port that all devices that are 48 * sensitive to an address range use. The port takes all the memory 49 * access types and roles them into one read() and write() call that the device 50 * must respond to. The device must also provide the addressRanges() function 51 * with which it returns the address ranges it is interested in. 52 */ 53class PioPort : public SimpleTimingPort 54{ 55 protected: 56 /** The device that this port serves. */ 57 PioDevice *device; 58 59 virtual Tick recvAtomic(PacketPtr pkt); 60 61 virtual void getDeviceAddressRanges(AddrRangeList &resp, 62 AddrRangeList &snoop); 63 64 public: 65 66 PioPort(PioDevice *dev, System *s, std::string pname = "-pioport"); 67}; 68 69 70class DmaPort : public Port 71{ 72 protected: 73 struct DmaReqState : public Packet::SenderState 74 { 75 /** Event to call on the device when this transaction (all packets) 76 * complete. */ 77 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 int cacheBlockSize() { return peerBlockSize(); } 136 unsigned int drain(Event *de); 137}; 138 139/** 140 * This device is the base class which all devices senstive to an address range 141 * inherit from. There are three pure virtual functions which all devices must 142 * implement addressRanges(), read(), and write(). The magic do choose which 143 * mode we are in, etc is handled by the PioPort so the device doesn't have to 144 * bother. 145 */ 146class PioDevice : public MemObject 147{ 148 protected: 149 150 /** The platform we are in. This is used to decide what type of memory 151 * transaction we should perform. */ 152 Platform *platform; 153 154 System *sys; 155 156 /** The pioPort that handles the requests for us and provides us requests 157 * that it sees. */ 158 PioPort *pioPort; 159 160 virtual void addressRanges(AddrRangeList &range_list) = 0; 161 162 /** Pure virtual function that the device must implement. Called 163 * when a read command is recieved by the port. 164 * @param pkt Packet describing this request 165 * @return number of ticks it took to complete 166 */ 167 virtual Tick read(PacketPtr pkt) = 0; 168 169 /** Pure virtual function that the device must implement. Called when a 170 * write command is recieved by the port. 171 * @param pkt Packet describing this request 172 * @return number of ticks it took to complete 173 */ 174 virtual Tick write(PacketPtr pkt) = 0; 175 176 public: 177 /** Params struct which is extended through each device based on 178 * the parameters it needs. Since we are re-writing everything, we 179 * might as well start from the bottom this time. */ 180 struct Params 181 { 182 std::string name; 183 Platform *platform; 184 System *system; 185 }; 186 187 protected: 188 Params *_params; 189 190 public: 191 const Params *params() const { return _params; } 192 193 PioDevice(Params *p) 194 : MemObject(p->name), platform(p->platform), sys(p->system), 195 pioPort(NULL), _params(p) 196 {} 197 198 virtual ~PioDevice(); 199 200 virtual void init(); 201 202 virtual unsigned int drain(Event *de); 203 204 virtual Port *getPort(const std::string &if_name, int idx = -1) 205 { 206 if (if_name == "pio") { 207 if (pioPort != NULL) 208 panic("pio port already connected to."); 209 pioPort = new PioPort(this, sys); 210 return pioPort; 211 } else 212 return NULL; 213 } 214 friend class PioPort; 215 216}; 217 218class BasicPioDevice : public PioDevice 219{ 220 public: 221 struct Params : public PioDevice::Params 222 { 223 Addr pio_addr; 224 Tick pio_delay; 225 }; 226 227 protected: 228 /** Address that the device listens to. */ 229 Addr pioAddr; 230 231 /** Size that the device's address range. */ 232 Addr pioSize; 233 234 /** Delay that the device experinces on an access. */ 235 Tick pioDelay; 236 237 public: 238 BasicPioDevice(Params *p) 239 : PioDevice(p), pioAddr(p->pio_addr), pioSize(0), 240 pioDelay(p->pio_delay) 241 {} 242 243 /** return the address ranges that this device responds to. 244 * @param range_list range list to populate with ranges 245 */ 246 void addressRanges(AddrRangeList &range_list); 247 248}; 249 250class DmaDevice : public PioDevice 251{ 252 protected: 253 DmaPort *dmaPort; 254 255 public: 256 DmaDevice(Params *p); 257 virtual ~DmaDevice(); 258 259 void dmaWrite(Addr addr, int size, Event *event, uint8_t *data) 260 { 261 dmaPort->dmaAction(MemCmd::WriteInvalidateReq, 262 addr, size, event, data); 263 } 264 265 void dmaRead(Addr addr, int size, Event *event, uint8_t *data) 266 { 267 dmaPort->dmaAction(MemCmd::ReadReq, addr, size, event, data); 268 } 269 270 bool dmaPending() { return dmaPort->dmaPending(); } 271 272 virtual unsigned int drain(Event *de); 273 274 int cacheBlockSize() { return dmaPort->cacheBlockSize(); } 275 276 virtual Port *getPort(const std::string &if_name, int idx = -1) 277 { 278 if (if_name == "pio") { 279 if (pioPort != NULL) 280 panic("pio port already connected to."); 281 pioPort = new PioPort(this, sys); 282 return pioPort; 283 } else if (if_name == "dma") { 284 if (dmaPort != NULL) 285 panic("dma port already connected to."); 286 dmaPort = new DmaPort(this, sys); 287 return dmaPort; 288 } else 289 return NULL; 290 } 291 292 friend class DmaPort; 293}; 294 295 296#endif // __DEV_IO_DEVICE_HH__ 297