io_device.cc revision 4870
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, bool &snoop) 52{ 53 snoop = false; 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 if (pkt->wasNacked()) { 104 DPRINTF(DMA, "Received nacked Pkt %#x with State: %#x Addr: %#x\n", 105 pkt, pkt->senderState, pkt->getAddr()); 106 107 if (backoffTime < device->minBackoffDelay) 108 backoffTime = device->minBackoffDelay; 109 else if (backoffTime < device->maxBackoffDelay) 110 backoffTime <<= 1; 111 112 backoffEvent.reschedule(curTick + backoffTime, true); 113 114 DPRINTF(DMA, "Backoff time set to %d ticks\n", backoffTime); 115 116 pkt->reinitNacked(); 117 queueDma(pkt, true); 118 } else if (pkt->senderState) { 119 DmaReqState *state; 120 backoffTime >>= 2; 121 122 DPRINTF(DMA, "Received response Pkt %#x with State: %#x Addr: %#x size: %#x\n", 123 pkt, pkt->senderState, pkt->getAddr(), pkt->req->getSize()); 124 state = dynamic_cast<DmaReqState*>(pkt->senderState); 125 pendingCount--; 126 127 assert(pendingCount >= 0); 128 assert(state); 129 130 state->numBytes += pkt->req->getSize(); 131 assert(state->totBytes >= state->numBytes); 132 if (state->totBytes == state->numBytes) { 133 state->completionEvent->process(); 134 delete state; 135 } 136 delete pkt->req; 137 delete pkt; 138 139 if (pendingCount == 0 && drainEvent) { 140 drainEvent->process(); 141 drainEvent = NULL; 142 } 143 } else { 144 panic("Got packet without sender state... huh?\n"); 145 } 146 147 return true; 148} 149 150DmaDevice::DmaDevice(Params *p) 151 : PioDevice(p), dmaPort(NULL), minBackoffDelay(p->min_backoff_delay), 152 maxBackoffDelay(p->max_backoff_delay) 153{ } 154 155 156unsigned int 157DmaDevice::drain(Event *de) 158{ 159 unsigned int count; 160 count = pioPort->drain(de) + dmaPort->drain(de); 161 if (count) 162 changeState(Draining); 163 else 164 changeState(Drained); 165 return count; 166} 167 168unsigned int 169DmaPort::drain(Event *de) 170{ 171 if (pendingCount == 0) 172 return 0; 173 drainEvent = de; 174 return 1; 175} 176 177 178void 179DmaPort::recvRetry() 180{ 181 assert(transmitList.size()); 182 PacketPtr pkt = transmitList.front(); 183 bool result = true; 184 do { 185 DPRINTF(DMA, "Retry on Packet %#x with senderState: %#x\n", 186 pkt, pkt->senderState); 187 result = sendTiming(pkt); 188 if (result) { 189 DPRINTF(DMA, "-- Done\n"); 190 transmitList.pop_front(); 191 inRetry = false; 192 } else { 193 inRetry = true; 194 DPRINTF(DMA, "-- Failed, queued\n"); 195 } 196 } while (!backoffTime && result && transmitList.size()); 197 198 if (transmitList.size() && backoffTime && !inRetry) { 199 DPRINTF(DMA, "Scheduling backoff for %d\n", curTick+backoffTime); 200 if (!backoffEvent.scheduled()) 201 backoffEvent.schedule(backoffTime+curTick); 202 } 203 DPRINTF(DMA, "TransmitList: %d, backoffTime: %d inRetry: %d es: %d\n", 204 transmitList.size(), backoffTime, inRetry, 205 backoffEvent.scheduled()); 206} 207 208 209void 210DmaPort::dmaAction(Packet::Command cmd, Addr addr, int size, Event *event, 211 uint8_t *data) 212{ 213 assert(event); 214 215 assert(device->getState() == SimObject::Running); 216 217 DmaReqState *reqState = new DmaReqState(event, this, size); 218 219 220 DPRINTF(DMA, "Starting DMA for addr: %#x size: %d sched: %d\n", addr, size, 221 event->scheduled()); 222 for (ChunkGenerator gen(addr, size, peerBlockSize()); 223 !gen.done(); gen.next()) { 224 Request *req = new Request(gen.addr(), gen.size(), 0); 225 PacketPtr pkt = new Packet(req, cmd, Packet::Broadcast); 226 227 // Increment the data pointer on a write 228 if (data) 229 pkt->dataStatic(data + gen.complete()); 230 231 pkt->senderState = reqState; 232 233 assert(pendingCount >= 0); 234 pendingCount++; 235 DPRINTF(DMA, "--Queuing DMA for addr: %#x size: %d\n", gen.addr(), 236 gen.size()); 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 DPRINTF(DMA, "-- Scheduling backoff timer for %d\n", 288 backoffTime+curTick); 289 backoffEvent.schedule(backoffTime+curTick); 290 } 291 } else if (state == System::Atomic) { 292 transmitList.pop_front(); 293 294 Tick lat; 295 DPRINTF(DMA, "--Sending DMA for addr: %#x size: %d\n", 296 pkt->req->getPaddr(), pkt->req->getSize()); 297 lat = sendAtomic(pkt); 298 assert(pkt->senderState); 299 DmaReqState *state = dynamic_cast<DmaReqState*>(pkt->senderState); 300 assert(state); 301 state->numBytes += pkt->req->getSize(); 302 303 DPRINTF(DMA, "--Received response for DMA for addr: %#x size: %d nb: %d, tot: %d sched %d\n", 304 pkt->req->getPaddr(), pkt->req->getSize(), state->numBytes, 305 state->totBytes, state->completionEvent->scheduled()); 306 307 if (state->totBytes == state->numBytes) { 308 assert(!state->completionEvent->scheduled()); 309 state->completionEvent->schedule(curTick + lat); 310 delete state; 311 delete pkt->req; 312 } 313 pendingCount--; 314 assert(pendingCount >= 0); 315 delete pkt; 316 317 if (pendingCount == 0 && drainEvent) { 318 drainEvent->process(); 319 drainEvent = NULL; 320 } 321 322 } else 323 panic("Unknown memory command state."); 324} 325 326DmaDevice::~DmaDevice() 327{ 328 if (dmaPort) 329 delete dmaPort; 330} 331 332 333