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