io_device.cc revision 4435
1/* 2 * Copyright (c) 2006 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#include "base/chunk_generator.hh" 33#include "base/trace.hh" 34#include "dev/io_device.hh" 35#include "sim/builder.hh" 36#include "sim/system.hh" 37 38 39PioPort::PioPort(PioDevice *dev, System *s, std::string pname) 40 : SimpleTimingPort(dev->name() + pname, dev), device(dev) 41{ } 42 43 44Tick 45PioPort::recvAtomic(PacketPtr pkt) 46{ 47 return pkt->isRead() ? device->read(pkt) : device->write(pkt); 48} 49 50void 51PioPort::getDeviceAddressRanges(AddrRangeList &resp, AddrRangeList &snoop) 52{ 53 snoop.clear(); 54 device->addressRanges(resp); 55} 56 57 58PioDevice::~PioDevice() 59{ 60 if (pioPort) 61 delete pioPort; 62} 63 64void 65PioDevice::init() 66{ 67 if (!pioPort) 68 panic("Pio port not connected to anything!"); 69 pioPort->sendStatusChange(Port::RangeChange); 70} 71 72 73unsigned int 74PioDevice::drain(Event *de) 75{ 76 unsigned int count; 77 count = pioPort->drain(de); 78 if (count) 79 changeState(Draining); 80 else 81 changeState(Drained); 82 return count; 83} 84 85void 86BasicPioDevice::addressRanges(AddrRangeList &range_list) 87{ 88 assert(pioSize != 0); 89 range_list.clear(); 90 range_list.push_back(RangeSize(pioAddr, pioSize)); 91} 92 93 94DmaPort::DmaPort(DmaDevice *dev, System *s) 95 : Port(dev->name() + "-dmaport", dev), device(dev), sys(s), 96 pendingCount(0), actionInProgress(0), drainEvent(NULL), 97 backoffTime(0), inRetry(false), backoffEvent(this) 98{ } 99 100bool 101DmaPort::recvTiming(PacketPtr pkt) 102{ 103 104 105 if (pkt->result == Packet::Nacked) { 106 DPRINTF(DMA, "Received nacked Pkt %#x with State: %#x Addr: %#x\n", 107 pkt, pkt->senderState, pkt->getAddr()); 108 109 if (backoffTime < device->minBackoffDelay) 110 backoffTime = device->minBackoffDelay; 111 else if (backoffTime < device->maxBackoffDelay) 112 backoffTime <<= 1; 113 114 if (backoffEvent.scheduled()) 115 backoffEvent.reschedule(curTick + backoffTime); 116 else 117 backoffEvent.schedule(curTick + backoffTime); 118 119 DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime); 120 121 pkt->reinitNacked(); 122 queueDma(pkt, true); 123 } else if (pkt->senderState) { 124 DmaReqState *state; 125 backoffTime >>= 2; 126 127 DPRINTF(DMA, "Received response Pkt %#x with State: %#x Addr: %#x size: %#x\n", 128 pkt, pkt->senderState, pkt->getAddr(), pkt->req->getSize()); 129 state = dynamic_cast<DmaReqState*>(pkt->senderState); 130 pendingCount--; 131 132 assert(pendingCount >= 0); 133 assert(state); 134 135 state->numBytes += pkt->req->getSize(); 136 assert(state->totBytes >= state->numBytes); 137 if (state->totBytes == state->numBytes) { 138 state->completionEvent->process(); 139 delete state; 140 } 141 delete pkt->req; 142 delete pkt; 143 144 if (pendingCount == 0 && drainEvent) { 145 drainEvent->process(); 146 drainEvent = NULL; 147 } 148 } else { 149 panic("Got packet without sender state... huh?\n"); 150 } 151 152 return true; 153} 154 155DmaDevice::DmaDevice(Params *p) 156 : PioDevice(p), dmaPort(NULL), minBackoffDelay(p->min_backoff_delay), 157 maxBackoffDelay(p->max_backoff_delay) 158{ } 159 160 161unsigned int 162DmaDevice::drain(Event *de) 163{ 164 unsigned int count; 165 count = pioPort->drain(de) + dmaPort->drain(de); 166 if (count) 167 changeState(Draining); 168 else 169 changeState(Drained); 170 return count; 171} 172 173unsigned int 174DmaPort::drain(Event *de) 175{ 176 if (pendingCount == 0) 177 return 0; 178 drainEvent = de; 179 return 1; 180} 181 182 183void 184DmaPort::recvRetry() 185{ 186 assert(transmitList.size()); 187 PacketPtr pkt = transmitList.front(); 188 bool result = true; 189 do { 190 DPRINTF(DMA, "Retry on Packet %#x with senderState: %#x\n", 191 pkt, pkt->senderState); 192 result = sendTiming(pkt); 193 if (result) { 194 DPRINTF(DMA, "-- Done\n"); 195 transmitList.pop_front(); 196 inRetry = false; 197 } else { 198 inRetry = true; 199 DPRINTF(DMA, "-- Failed, queued\n"); 200 } 201 } while (!backoffTime && result && transmitList.size()); 202 203 if (transmitList.size() && backoffTime && !inRetry) { 204 DPRINTF(DMA, "Scheduling backoff for %d\n", curTick+backoffTime); 205 if (!backoffEvent.scheduled()) 206 backoffEvent.schedule(backoffTime+curTick); 207 } 208 DPRINTF(DMA, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n", 209 transmitList.size(), backoffTime, inRetry, 210 backoffEvent.scheduled()); 211} 212 213 214void 215DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event, 216 uint8_t *data) 217{ 218 assert(event); 219 220 assert(device->getState() == SimObject::Running); 221 222 DmaReqState *reqState = new DmaReqState(event, this, size); 223 224 for (ChunkGenerator gen(addr, size, peerBlockSize()); 225 !gen.done(); gen.next()) { 226 Request *req = new Request(gen.addr(), gen.size(), 0); 227 PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast); 228 229 // Increment the data pointer on a write 230 if (data) 231 pkt->dataStatic(data + gen.complete()); 232 233 pkt->senderState = reqState; 234 235 assert(pendingCount >= 0); 236 pendingCount++; 237 queueDma(pkt); 238 } 239 240} 241 242void 243DmaPort::queueDma(PacketPtr pkt, bool front) 244{ 245 246 if (front) 247 transmitList.push_front(pkt); 248 else 249 transmitList.push_back(pkt); 250 sendDma(); 251} 252 253 254void 255DmaPort::sendDma() 256{ 257 // some kind of selction between access methods 258 // more work is going to have to be done to make 259 // switching actually work 260 assert(transmitList.size()); 261 PacketPtr pkt = transmitList.front(); 262 263 System::MemoryMode state = sys->getMemoryMode(); 264 if (state == System::Timing) { 265 if (backoffEvent.scheduled() || inRetry) { 266 DPRINTF(DMA, "Can't send immediately, waiting for retry or backoff timer\n"); 267 return; 268 } 269 270 DPRINTF(DMA, "Attempting to send Packet %#x with addr: %#x\n", 271 pkt, pkt->getAddr()); 272 273 bool result; 274 do { 275 result = sendTiming(pkt); 276 if (result) { 277 transmitList.pop_front(); 278 DPRINTF(DMA, "-- Done\n"); 279 } else { 280 inRetry = true; 281 DPRINTF(DMA, "-- Failed: queued\n"); 282 } 283 } while (result && !backoffTime && transmitList.size()); 284 285 if (transmitList.size() && backoffTime && !inRetry && 286 !backoffEvent.scheduled()) { 287 backoffEvent.schedule(backoffTime+curTick); 288 } 289 } else if (state == System::Atomic) { 290 transmitList.pop_front(); 291 292 Tick lat; 293 lat = sendAtomic(pkt); 294 assert(pkt->senderState); 295 DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState); 296 assert(state); 297 298 state->numBytes += pkt->req->getSize(); 299 if (state->totBytes == state->numBytes) { 300 state->completionEvent->schedule(curTick + lat); 301 delete state; 302 delete pkt->req; 303 } 304 pendingCount--; 305 assert(pendingCount >= 0); 306 delete pkt; 307 308 if (pendingCount == 0 && drainEvent) { 309 drainEvent->process(); 310 drainEvent = NULL; 311 } 312 313 } else 314 panic("Unknown memory command state."); 315} 316 317DmaDevice::~DmaDevice() 318{ 319 if (dmaPort) 320 delete dmaPort; 321} 322 323 324