2 * Copyright (c) 2008 The Regents of The University of Michigan
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.
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.
32 * Device model for Intel's I/O AT DMA copy engine.
37 #include "base/cp_annotate.hh"
38 #include "base/trace.hh"
39 #include "dev/copy_engine.hh"
40 #include "mem/packet.hh"
41 #include "mem/packet_access.hh"
42 #include "params/CopyEngine.hh"
43 #include "sim/stats.hh"
44 #include "sim/system.hh"
46 using namespace CopyEngineReg
;
49 CopyEngine::CopyEngine(const Params
*p
)
52 // All Reg regs are initialized to 0 by default
53 regs
.chanCount
= p
->ChanCnt
;
54 regs
.xferCap
= findMsbSet(p
->XferCap
);
57 if (regs
.chanCount
> 64)
58 fatal("CopyEngine interface doesn't support more than 64 DMA engines\n");
60 for (int x
= 0; x
< regs
.chanCount
; x
++) {
61 CopyEngineChannel
*ch
= new CopyEngineChannel(this, x
);
67 CopyEngine::CopyEngineChannel::CopyEngineChannel(CopyEngine
*_ce
, int cid
)
68 : ce(_ce
), channelId(cid
), busy(false), underReset(false),
69 refreshNext(false), latBeforeBegin(ce
->params()->latBeforeBegin
),
70 latAfterCompletion(ce
->params()->latAfterCompletion
),
71 completionDataReg(0), nextState(Idle
), drainEvent(NULL
),
72 fetchCompleteEvent(this), addrCompleteEvent(this),
73 readCompleteEvent(this), writeCompleteEvent(this),
74 statusCompleteEvent(this)
77 cr
.status
.dma_transfer_status(3);
79 cr
.completionAddr
= 0;
81 curDmaDesc
= new DmaDesc
;
82 memset(curDmaDesc
, 0, sizeof(DmaDesc
));
83 copyBuffer
= new uint8_t[ce
->params()->XferCap
];
86 CopyEngine::~CopyEngine()
88 for (int x
= 0; x
< chan
.size(); x
++) {
93 CopyEngine::CopyEngineChannel::~CopyEngineChannel()
104 for (int x
= 0; x
< chan
.size(); x
++)
109 CopyEngine::CopyEngineChannel::init()
113 cePort
= new DmaPort(ce
, ce
->sys
, ce
->params()->min_backoff_delay
,
114 ce
->params()->max_backoff_delay
);
115 peer
= ce
->dmaPort
->getPeer()->getOwner()->getPort("");
116 peer
->setPeer(cePort
);
117 cePort
->setPeer(peer
);
121 CopyEngine::CopyEngineChannel::recvCommand()
123 if (cr
.command
.start_dma()) {
125 cr
.status
.dma_transfer_status(0);
126 nextState
= DescriptorFetch
;
127 fetchAddress
= cr
.descChainAddr
;
128 if (ce
->getState() == SimObject::Running
)
129 fetchDescriptor(cr
.descChainAddr
);
130 } else if (cr
.command
.append_dma()) {
132 nextState
= AddressFetch
;
133 if (ce
->getState() == SimObject::Running
)
134 fetchNextAddr(lastDescriptorAddr
);
137 } else if (cr
.command
.reset_dma()) {
141 cr
.status
.dma_transfer_status(3);
144 } else if (cr
.command
.resume_dma() || cr
.command
.abort_dma() ||
145 cr
.command
.suspend_dma())
146 panic("Resume, Abort, and Suspend are not supported\n");
151 CopyEngine::read(PacketPtr pkt
)
156 if (!getBAR(pkt
->getAddr(), bar
, daddr
))
157 panic("Invalid PCI memory access to unmapped memory.\n");
159 // Only Memory register BAR is allowed
162 int size
= pkt
->getSize();
163 if (size
!= sizeof(uint64_t) && size
!= sizeof(uint32_t) &&
164 size
!= sizeof(uint16_t) && size
!= sizeof(uint8_t)) {
165 panic("Unknown size for MMIO access: %d\n", pkt
->getSize());
168 DPRINTF(DMACopyEngine
, "Read device register %#X size: %d\n", daddr
, size
);
173 /// Handle read of register here
179 assert(size
== sizeof(regs
.chanCount
));
180 pkt
->set
<uint8_t>(regs
.chanCount
);
183 assert(size
== sizeof(regs
.xferCap
));
184 pkt
->set
<uint8_t>(regs
.xferCap
);
187 assert(size
== sizeof(uint8_t));
188 pkt
->set
<uint8_t>(regs
.intrctrl());
189 regs
.intrctrl
.master_int_enable(0);
192 assert(size
== sizeof(regs
.attnStatus
));
193 pkt
->set
<uint32_t>(regs
.attnStatus
);
197 panic("Read request to unknown register number: %#x\n", daddr
);
199 pkt
->makeAtomicResponse();
204 // Find which channel we're accessing
207 while (daddr
>= 0x80) {
212 if (chanid
>= regs
.chanCount
)
213 panic("Access to channel %d (device only configured for %d channels)",
214 chanid
, regs
.chanCount
);
217 /// Channel registers are handled here
219 chan
[chanid
]->channelRead(pkt
, daddr
, size
);
221 pkt
->makeAtomicResponse();
226 CopyEngine::CopyEngineChannel::channelRead(Packet
*pkt
, Addr daddr
, int size
)
230 assert(size
== sizeof(uint16_t));
231 pkt
->set
<uint16_t>(cr
.ctrl());
235 assert(size
== sizeof(uint64_t));
236 pkt
->set
<uint64_t>(cr
.status() | ~busy
);
239 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
240 if (size
== sizeof(uint64_t))
241 pkt
->set
<uint64_t>(cr
.descChainAddr
);
243 pkt
->set
<uint32_t>(bits(cr
.descChainAddr
,0,31));
245 case CHAN_CHAINADDR_HIGH
:
246 assert(size
== sizeof(uint32_t));
247 pkt
->set
<uint32_t>(bits(cr
.descChainAddr
,32,63));
250 assert(size
== sizeof(uint8_t));
251 pkt
->set
<uint32_t>(cr
.command());
254 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
255 if (size
== sizeof(uint64_t))
256 pkt
->set
<uint64_t>(cr
.completionAddr
);
258 pkt
->set
<uint32_t>(bits(cr
.completionAddr
,0,31));
260 case CHAN_CMPLNADDR_HIGH
:
261 assert(size
== sizeof(uint32_t));
262 pkt
->set
<uint32_t>(bits(cr
.completionAddr
,32,63));
265 assert(size
== sizeof(uint32_t));
266 pkt
->set
<uint32_t>(cr
.error());
269 panic("Read request to unknown channel register number: (%d)%#x\n",
276 CopyEngine::write(PacketPtr pkt
)
282 if (!getBAR(pkt
->getAddr(), bar
, daddr
))
283 panic("Invalid PCI memory access to unmapped memory.\n");
285 // Only Memory register BAR is allowed
288 int size
= pkt
->getSize();
291 /// Handle write of register here
294 if (size
== sizeof(uint64_t)) {
295 uint64_t val M5_VAR_USED
= pkt
->get
<uint64_t>();
296 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
297 } else if (size
== sizeof(uint32_t)) {
298 uint32_t val M5_VAR_USED
= pkt
->get
<uint32_t>();
299 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
300 } else if (size
== sizeof(uint16_t)) {
301 uint16_t val M5_VAR_USED
= pkt
->get
<uint16_t>();
302 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
303 } else if (size
== sizeof(uint8_t)) {
304 uint8_t val M5_VAR_USED
= pkt
->get
<uint8_t>();
305 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
307 panic("Unknown size for MMIO access: %d\n", size
);
315 DPRINTF(DMACopyEngine
, "Warning, ignorning write to register %x\n",
319 regs
.intrctrl
.master_int_enable(bits(pkt
->get
<uint8_t>(),0,1));
322 panic("Read request to unknown register number: %#x\n", daddr
);
324 pkt
->makeAtomicResponse();
328 // Find which channel we're accessing
331 while (daddr
>= 0x80) {
336 if (chanid
>= regs
.chanCount
)
337 panic("Access to channel %d (device only configured for %d channels)",
338 chanid
, regs
.chanCount
);
341 /// Channel registers are handled here
343 chan
[chanid
]->channelWrite(pkt
, daddr
, size
);
345 pkt
->makeAtomicResponse();
350 CopyEngine::CopyEngineChannel::channelWrite(Packet
*pkt
, Addr daddr
, int size
)
354 assert(size
== sizeof(uint16_t));
356 old_int_disable
= cr
.ctrl
.interrupt_disable();
357 cr
.ctrl(pkt
->get
<uint16_t>());
358 if (cr
.ctrl
.interrupt_disable())
359 cr
.ctrl
.interrupt_disable(0);
361 cr
.ctrl
.interrupt_disable(old_int_disable
);
364 assert(size
== sizeof(uint64_t));
365 DPRINTF(DMACopyEngine
, "Warning, ignorning write to register %x\n",
369 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
370 if (size
== sizeof(uint64_t))
371 cr
.descChainAddr
= pkt
->get
<uint64_t>();
373 cr
.descChainAddr
= (uint64_t)pkt
->get
<uint32_t>() |
374 (cr
.descChainAddr
& ~mask(32));
375 DPRINTF(DMACopyEngine
, "Chain Address %x\n", cr
.descChainAddr
);
377 case CHAN_CHAINADDR_HIGH
:
378 assert(size
== sizeof(uint32_t));
379 cr
.descChainAddr
= ((uint64_t)pkt
->get
<uint32_t>() <<32) |
380 (cr
.descChainAddr
& mask(32));
381 DPRINTF(DMACopyEngine
, "Chain Address %x\n", cr
.descChainAddr
);
384 assert(size
== sizeof(uint8_t));
385 cr
.command(pkt
->get
<uint8_t>());
389 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
390 if (size
== sizeof(uint64_t))
391 cr
.completionAddr
= pkt
->get
<uint64_t>();
393 cr
.completionAddr
= pkt
->get
<uint32_t>() |
394 (cr
.completionAddr
& ~mask(32));
396 case CHAN_CMPLNADDR_HIGH
:
397 assert(size
== sizeof(uint32_t));
398 cr
.completionAddr
= ((uint64_t)pkt
->get
<uint32_t>() <<32) |
399 (cr
.completionAddr
& mask(32));
402 assert(size
== sizeof(uint32_t));
403 cr
.error(~pkt
->get
<uint32_t>() & cr
.error());
406 panic("Read request to unknown channel register number: (%d)%#x\n",
412 CopyEngine::regStats()
414 using namespace Stats
;
416 .init(regs
.chanCount
)
417 .name(name() + ".bytes_copied")
418 .desc("Number of bytes copied by each engine")
422 .init(regs
.chanCount
)
423 .name(name() + ".copies_processed")
424 .desc("Number of copies processed by each engine")
430 CopyEngine::CopyEngineChannel::fetchDescriptor(Addr address
)
433 anBegin("FetchDescriptor");
434 DPRINTF(DMACopyEngine
, "Reading descriptor from at memory location %#x(%#x)\n",
435 address
, ce
->platform
->pciToDma(address
));
439 DPRINTF(DMACopyEngine
, "dmaAction: %#x, %d bytes, to addr %#x\n",
440 ce
->platform
->pciToDma(address
), sizeof(DmaDesc
), curDmaDesc
);
442 cePort
->dmaAction(MemCmd::ReadReq
, ce
->platform
->pciToDma(address
),
443 sizeof(DmaDesc
), &fetchCompleteEvent
, (uint8_t*)curDmaDesc
,
445 lastDescriptorAddr
= address
;
449 CopyEngine::CopyEngineChannel::fetchDescComplete()
451 DPRINTF(DMACopyEngine
, "Read of descriptor complete\n");
453 if ((curDmaDesc
->command
& DESC_CTRL_NULL
)) {
454 DPRINTF(DMACopyEngine
, "Got NULL descriptor, skipping\n");
455 assert(!(curDmaDesc
->command
& DESC_CTRL_CP_STS
));
456 if (curDmaDesc
->command
& DESC_CTRL_CP_STS
) {
457 panic("Shouldn't be able to get here\n");
458 nextState
= CompletionWrite
;
459 if (inDrain()) return;
460 writeCompletionStatus();
471 if (curDmaDesc
->command
& ~DESC_CTRL_CP_STS
)
472 panic("Descriptor has flag other that completion status set\n");
475 if (inDrain()) return;
480 CopyEngine::CopyEngineChannel::readCopyBytes()
482 anBegin("ReadCopyBytes");
483 DPRINTF(DMACopyEngine
, "Reading %d bytes from buffer to memory location %#x(%#x)\n",
484 curDmaDesc
->len
, curDmaDesc
->dest
,
485 ce
->platform
->pciToDma(curDmaDesc
->src
));
486 cePort
->dmaAction(MemCmd::ReadReq
, ce
->platform
->pciToDma(curDmaDesc
->src
),
487 curDmaDesc
->len
, &readCompleteEvent
, copyBuffer
, 0);
491 CopyEngine::CopyEngineChannel::readCopyBytesComplete()
493 DPRINTF(DMACopyEngine
, "Read of bytes to copy complete\n");
495 nextState
= DMAWrite
;
496 if (inDrain()) return;
501 CopyEngine::CopyEngineChannel::writeCopyBytes()
503 anBegin("WriteCopyBytes");
504 DPRINTF(DMACopyEngine
, "Writing %d bytes from buffer to memory location %#x(%#x)\n",
505 curDmaDesc
->len
, curDmaDesc
->dest
,
506 ce
->platform
->pciToDma(curDmaDesc
->dest
));
508 cePort
->dmaAction(MemCmd::WriteReq
, ce
->platform
->pciToDma(curDmaDesc
->dest
),
509 curDmaDesc
->len
, &writeCompleteEvent
, copyBuffer
, 0);
511 ce
->bytesCopied
[channelId
] += curDmaDesc
->len
;
512 ce
->copiesProcessed
[channelId
]++;
516 CopyEngine::CopyEngineChannel::writeCopyBytesComplete()
518 DPRINTF(DMACopyEngine
, "Write of bytes to copy complete user1: %#x\n",
521 cr
.status
.compl_desc_addr(lastDescriptorAddr
>> 6);
522 completionDataReg
= cr
.status() | 1;
524 anQ("DMAUsedDescQ", channelId
, 1);
525 anQ("AppRecvQ", curDmaDesc
->user1
, curDmaDesc
->len
);
526 if (curDmaDesc
->command
& DESC_CTRL_CP_STS
) {
527 nextState
= CompletionWrite
;
528 if (inDrain()) return;
529 writeCompletionStatus();
533 continueProcessing();
537 CopyEngine::CopyEngineChannel::continueProcessing()
551 if (curDmaDesc
->next
) {
552 nextState
= DescriptorFetch
;
553 fetchAddress
= curDmaDesc
->next
;
554 if (inDrain()) return;
555 fetchDescriptor(curDmaDesc
->next
);
556 } else if (refreshNext
) {
557 nextState
= AddressFetch
;
559 if (inDrain()) return;
560 fetchNextAddr(lastDescriptorAddr
);
570 CopyEngine::CopyEngineChannel::writeCompletionStatus()
572 anBegin("WriteCompletionStatus");
573 DPRINTF(DMACopyEngine
, "Writing completion status %#x to address %#x(%#x)\n",
574 completionDataReg
, cr
.completionAddr
,
575 ce
->platform
->pciToDma(cr
.completionAddr
));
577 cePort
->dmaAction(MemCmd::WriteReq
, ce
->platform
->pciToDma(cr
.completionAddr
),
578 sizeof(completionDataReg
), &statusCompleteEvent
,
579 (uint8_t*)&completionDataReg
, latAfterCompletion
);
583 CopyEngine::CopyEngineChannel::writeStatusComplete()
585 DPRINTF(DMACopyEngine
, "Writing completion status complete\n");
586 continueProcessing();
590 CopyEngine::CopyEngineChannel::fetchNextAddr(Addr address
)
592 anBegin("FetchNextAddr");
593 DPRINTF(DMACopyEngine
, "Fetching next address...\n");
595 cePort
->dmaAction(MemCmd::ReadReq
, ce
->platform
->pciToDma(address
+
596 offsetof(DmaDesc
, next
)), sizeof(Addr
), &addrCompleteEvent
,
597 (uint8_t*)curDmaDesc
+ offsetof(DmaDesc
, next
), 0);
601 CopyEngine::CopyEngineChannel::fetchAddrComplete()
603 DPRINTF(DMACopyEngine
, "Fetching next address complete: %#x\n",
605 if (!curDmaDesc
->next
) {
606 DPRINTF(DMACopyEngine
, "Got NULL descriptor, nothing more to do\n");
614 nextState
= DescriptorFetch
;
615 fetchAddress
= curDmaDesc
->next
;
616 if (inDrain()) return;
617 fetchDescriptor(curDmaDesc
->next
);
621 CopyEngine::CopyEngineChannel::inDrain()
623 if (ce
->getState() == SimObject::Draining
) {
624 DPRINTF(DMACopyEngine
, "processing drain\n");
626 drainEvent
->process();
630 return ce
->getState() != SimObject::Running
;
634 CopyEngine::CopyEngineChannel::drain(Event
*de
)
636 if (nextState
== Idle
|| ce
->getState() != SimObject::Running
)
638 unsigned int count
= 1;
639 count
+= cePort
->drain(de
);
641 DPRINTF(DMACopyEngine
, "unable to drain, returning %d\n", count
);
647 CopyEngine::drain(Event
*de
)
650 count
= pioPort
->drain(de
) + dmaPort
->drain(de
) + configPort
->drain(de
);
651 for (int x
= 0;x
< chan
.size(); x
++)
652 count
+= chan
[x
]->drain(de
);
655 changeState(Draining
);
657 changeState(Drained
);
659 DPRINTF(DMACopyEngine
, "call to CopyEngine::drain() returning %d\n", count
);
664 CopyEngine::serialize(std::ostream
&os
)
666 PciDev::serialize(os
);
668 for (int x
=0; x
< chan
.size(); x
++) {
669 nameOut(os
, csprintf("%s.channel%d", name(), x
));
670 chan
[x
]->serialize(os
);
675 CopyEngine::unserialize(Checkpoint
*cp
, const std::string
§ion
)
677 PciDev::unserialize(cp
, section
);
678 regs
.unserialize(cp
, section
);
679 for (int x
= 0; x
< chan
.size(); x
++)
680 chan
[x
]->unserialize(cp
, csprintf("%s.channel%d", section
, x
));
684 CopyEngine::CopyEngineChannel::serialize(std::ostream
&os
)
686 SERIALIZE_SCALAR(channelId
);
687 SERIALIZE_SCALAR(busy
);
688 SERIALIZE_SCALAR(underReset
);
689 SERIALIZE_SCALAR(refreshNext
);
690 SERIALIZE_SCALAR(lastDescriptorAddr
);
691 SERIALIZE_SCALAR(completionDataReg
);
692 SERIALIZE_SCALAR(fetchAddress
);
693 int nextState
= this->nextState
;
694 SERIALIZE_SCALAR(nextState
);
695 arrayParamOut(os
, "curDmaDesc", (uint8_t*)curDmaDesc
, sizeof(DmaDesc
));
696 SERIALIZE_ARRAY(copyBuffer
, ce
->params()->XferCap
);
701 CopyEngine::CopyEngineChannel::unserialize(Checkpoint
*cp
, const std::string
§ion
)
703 UNSERIALIZE_SCALAR(channelId
);
704 UNSERIALIZE_SCALAR(busy
);
705 UNSERIALIZE_SCALAR(underReset
);
706 UNSERIALIZE_SCALAR(refreshNext
);
707 UNSERIALIZE_SCALAR(lastDescriptorAddr
);
708 UNSERIALIZE_SCALAR(completionDataReg
);
709 UNSERIALIZE_SCALAR(fetchAddress
);
711 UNSERIALIZE_SCALAR(nextState
);
712 this->nextState
= (ChannelState
)nextState
;
713 arrayParamIn(cp
, section
, "curDmaDesc", (uint8_t*)curDmaDesc
, sizeof(DmaDesc
));
714 UNSERIALIZE_ARRAY(copyBuffer
, ce
->params()->XferCap
);
715 cr
.unserialize(cp
, section
);
720 CopyEngine::CopyEngineChannel::restartStateMachine()
724 fetchNextAddr(lastDescriptorAddr
);
726 case DescriptorFetch
:
727 fetchDescriptor(fetchAddress
);
735 case CompletionWrite
:
736 writeCompletionStatus();
741 panic("Unknown state for CopyEngineChannel\n");
749 for (int x
= 0;x
< chan
.size(); x
++)
755 CopyEngine::CopyEngineChannel::resume()
757 DPRINTF(DMACopyEngine
, "Restarting state machine at state %d\n", nextState
);
758 restartStateMachine();
762 CopyEngineParams::create()
764 return new CopyEngine(this);