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