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