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