2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2008 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * Device model for Intel's I/O AT DMA copy engine.
49 #include "base/cp_annotate.hh"
50 #include "base/trace.hh"
51 #include "debug/DMACopyEngine.hh"
52 #include "debug/Drain.hh"
53 #include "dev/copy_engine.hh"
54 #include "mem/packet.hh"
55 #include "mem/packet_access.hh"
56 #include "params/CopyEngine.hh"
57 #include "sim/stats.hh"
58 #include "sim/system.hh"
60 using namespace CopyEngineReg
;
62 CopyEngine::CopyEngine(const Params
*p
)
65 // All Reg regs are initialized to 0 by default
66 regs
.chanCount
= p
->ChanCnt
;
67 regs
.xferCap
= findMsbSet(p
->XferCap
);
70 if (regs
.chanCount
> 64)
71 fatal("CopyEngine interface doesn't support more than 64 DMA engines\n");
73 for (int x
= 0; x
< regs
.chanCount
; x
++) {
74 CopyEngineChannel
*ch
= new CopyEngineChannel(this, x
);
80 CopyEngine::CopyEngineChannel::CopyEngineChannel(CopyEngine
*_ce
, int cid
)
81 : cePort(_ce
, _ce
->sys
),
82 ce(_ce
), channelId(cid
), busy(false), underReset(false),
83 refreshNext(false), latBeforeBegin(ce
->params()->latBeforeBegin
),
84 latAfterCompletion(ce
->params()->latAfterCompletion
),
85 completionDataReg(0), nextState(Idle
), drainManager(NULL
),
86 fetchCompleteEvent(this), addrCompleteEvent(this),
87 readCompleteEvent(this), writeCompleteEvent(this),
88 statusCompleteEvent(this)
91 cr
.status
.dma_transfer_status(3);
93 cr
.completionAddr
= 0;
95 curDmaDesc
= new DmaDesc
;
96 memset(curDmaDesc
, 0, sizeof(DmaDesc
));
97 copyBuffer
= new uint8_t[ce
->params()->XferCap
];
100 CopyEngine::~CopyEngine()
102 for (int x
= 0; x
< chan
.size(); x
++) {
107 CopyEngine::CopyEngineChannel::~CopyEngineChannel()
110 delete [] copyBuffer
;
114 CopyEngine::getMasterPort(const std::string
&if_name
, PortID idx
)
116 if (if_name
!= "dma") {
117 // pass it along to our super class
118 return PciDevice::getMasterPort(if_name
, idx
);
120 if (idx
>= static_cast<int>(chan
.size())) {
121 panic("CopyEngine::getMasterPort: unknown index %d\n", idx
);
124 return chan
[idx
]->getMasterPort();
130 CopyEngine::CopyEngineChannel::getMasterPort()
136 CopyEngine::CopyEngineChannel::recvCommand()
138 if (cr
.command
.start_dma()) {
140 cr
.status
.dma_transfer_status(0);
141 nextState
= DescriptorFetch
;
142 fetchAddress
= cr
.descChainAddr
;
143 if (ce
->getDrainState() == Drainable::Running
)
144 fetchDescriptor(cr
.descChainAddr
);
145 } else if (cr
.command
.append_dma()) {
147 nextState
= AddressFetch
;
148 if (ce
->getDrainState() == Drainable::Running
)
149 fetchNextAddr(lastDescriptorAddr
);
152 } else if (cr
.command
.reset_dma()) {
156 cr
.status
.dma_transfer_status(3);
159 } else if (cr
.command
.resume_dma() || cr
.command
.abort_dma() ||
160 cr
.command
.suspend_dma())
161 panic("Resume, Abort, and Suspend are not supported\n");
166 CopyEngine::read(PacketPtr pkt
)
171 if (!getBAR(pkt
->getAddr(), bar
, daddr
))
172 panic("Invalid PCI memory access to unmapped memory.\n");
174 // Only Memory register BAR is allowed
177 int size
= pkt
->getSize();
178 if (size
!= sizeof(uint64_t) && size
!= sizeof(uint32_t) &&
179 size
!= sizeof(uint16_t) && size
!= sizeof(uint8_t)) {
180 panic("Unknown size for MMIO access: %d\n", pkt
->getSize());
183 DPRINTF(DMACopyEngine
, "Read device register %#X size: %d\n", daddr
, size
);
186 /// Handle read of register here
192 assert(size
== sizeof(regs
.chanCount
));
193 pkt
->set
<uint8_t>(regs
.chanCount
);
196 assert(size
== sizeof(regs
.xferCap
));
197 pkt
->set
<uint8_t>(regs
.xferCap
);
200 assert(size
== sizeof(uint8_t));
201 pkt
->set
<uint8_t>(regs
.intrctrl());
202 regs
.intrctrl
.master_int_enable(0);
205 assert(size
== sizeof(regs
.attnStatus
));
206 pkt
->set
<uint32_t>(regs
.attnStatus
);
210 panic("Read request to unknown register number: %#x\n", daddr
);
212 pkt
->makeAtomicResponse();
217 // Find which channel we're accessing
220 while (daddr
>= 0x80) {
225 if (chanid
>= regs
.chanCount
)
226 panic("Access to channel %d (device only configured for %d channels)",
227 chanid
, regs
.chanCount
);
230 /// Channel registers are handled here
232 chan
[chanid
]->channelRead(pkt
, daddr
, size
);
234 pkt
->makeAtomicResponse();
239 CopyEngine::CopyEngineChannel::channelRead(Packet
*pkt
, Addr daddr
, int size
)
243 assert(size
== sizeof(uint16_t));
244 pkt
->set
<uint16_t>(cr
.ctrl());
248 assert(size
== sizeof(uint64_t));
249 pkt
->set
<uint64_t>(cr
.status() | ~busy
);
252 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
253 if (size
== sizeof(uint64_t))
254 pkt
->set
<uint64_t>(cr
.descChainAddr
);
256 pkt
->set
<uint32_t>(bits(cr
.descChainAddr
,0,31));
258 case CHAN_CHAINADDR_HIGH
:
259 assert(size
== sizeof(uint32_t));
260 pkt
->set
<uint32_t>(bits(cr
.descChainAddr
,32,63));
263 assert(size
== sizeof(uint8_t));
264 pkt
->set
<uint32_t>(cr
.command());
267 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
268 if (size
== sizeof(uint64_t))
269 pkt
->set
<uint64_t>(cr
.completionAddr
);
271 pkt
->set
<uint32_t>(bits(cr
.completionAddr
,0,31));
273 case CHAN_CMPLNADDR_HIGH
:
274 assert(size
== sizeof(uint32_t));
275 pkt
->set
<uint32_t>(bits(cr
.completionAddr
,32,63));
278 assert(size
== sizeof(uint32_t));
279 pkt
->set
<uint32_t>(cr
.error());
282 panic("Read request to unknown channel register number: (%d)%#x\n",
289 CopyEngine::write(PacketPtr pkt
)
295 if (!getBAR(pkt
->getAddr(), bar
, daddr
))
296 panic("Invalid PCI memory access to unmapped memory.\n");
298 // Only Memory register BAR is allowed
301 int size
= pkt
->getSize();
304 /// Handle write of register here
307 if (size
== sizeof(uint64_t)) {
308 uint64_t val M5_VAR_USED
= pkt
->get
<uint64_t>();
309 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
310 } else if (size
== sizeof(uint32_t)) {
311 uint32_t val M5_VAR_USED
= pkt
->get
<uint32_t>();
312 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
313 } else if (size
== sizeof(uint16_t)) {
314 uint16_t val M5_VAR_USED
= pkt
->get
<uint16_t>();
315 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
316 } else if (size
== sizeof(uint8_t)) {
317 uint8_t val M5_VAR_USED
= pkt
->get
<uint8_t>();
318 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
320 panic("Unknown size for MMIO access: %d\n", size
);
328 DPRINTF(DMACopyEngine
, "Warning, ignorning write to register %x\n",
332 regs
.intrctrl
.master_int_enable(bits(pkt
->get
<uint8_t>(),0,1));
335 panic("Read request to unknown register number: %#x\n", daddr
);
337 pkt
->makeAtomicResponse();
341 // Find which channel we're accessing
344 while (daddr
>= 0x80) {
349 if (chanid
>= regs
.chanCount
)
350 panic("Access to channel %d (device only configured for %d channels)",
351 chanid
, regs
.chanCount
);
354 /// Channel registers are handled here
356 chan
[chanid
]->channelWrite(pkt
, daddr
, size
);
358 pkt
->makeAtomicResponse();
363 CopyEngine::CopyEngineChannel::channelWrite(Packet
*pkt
, Addr daddr
, int size
)
367 assert(size
== sizeof(uint16_t));
369 old_int_disable
= cr
.ctrl
.interrupt_disable();
370 cr
.ctrl(pkt
->get
<uint16_t>());
371 if (cr
.ctrl
.interrupt_disable())
372 cr
.ctrl
.interrupt_disable(0);
374 cr
.ctrl
.interrupt_disable(old_int_disable
);
377 assert(size
== sizeof(uint64_t));
378 DPRINTF(DMACopyEngine
, "Warning, ignorning write to register %x\n",
382 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
383 if (size
== sizeof(uint64_t))
384 cr
.descChainAddr
= pkt
->get
<uint64_t>();
386 cr
.descChainAddr
= (uint64_t)pkt
->get
<uint32_t>() |
387 (cr
.descChainAddr
& ~mask(32));
388 DPRINTF(DMACopyEngine
, "Chain Address %x\n", cr
.descChainAddr
);
390 case CHAN_CHAINADDR_HIGH
:
391 assert(size
== sizeof(uint32_t));
392 cr
.descChainAddr
= ((uint64_t)pkt
->get
<uint32_t>() <<32) |
393 (cr
.descChainAddr
& mask(32));
394 DPRINTF(DMACopyEngine
, "Chain Address %x\n", cr
.descChainAddr
);
397 assert(size
== sizeof(uint8_t));
398 cr
.command(pkt
->get
<uint8_t>());
402 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
403 if (size
== sizeof(uint64_t))
404 cr
.completionAddr
= pkt
->get
<uint64_t>();
406 cr
.completionAddr
= pkt
->get
<uint32_t>() |
407 (cr
.completionAddr
& ~mask(32));
409 case CHAN_CMPLNADDR_HIGH
:
410 assert(size
== sizeof(uint32_t));
411 cr
.completionAddr
= ((uint64_t)pkt
->get
<uint32_t>() <<32) |
412 (cr
.completionAddr
& mask(32));
415 assert(size
== sizeof(uint32_t));
416 cr
.error(~pkt
->get
<uint32_t>() & cr
.error());
419 panic("Read request to unknown channel register number: (%d)%#x\n",
425 CopyEngine::regStats()
427 using namespace Stats
;
429 .init(regs
.chanCount
)
430 .name(name() + ".bytes_copied")
431 .desc("Number of bytes copied by each engine")
435 .init(regs
.chanCount
)
436 .name(name() + ".copies_processed")
437 .desc("Number of copies processed by each engine")
443 CopyEngine::CopyEngineChannel::fetchDescriptor(Addr address
)
446 anBegin("FetchDescriptor");
447 DPRINTF(DMACopyEngine
, "Reading descriptor from at memory location %#x(%#x)\n",
448 address
, ce
->platform
->pciToDma(address
));
452 DPRINTF(DMACopyEngine
, "dmaAction: %#x, %d bytes, to addr %#x\n",
453 ce
->platform
->pciToDma(address
), sizeof(DmaDesc
), curDmaDesc
);
455 cePort
.dmaAction(MemCmd::ReadReq
, ce
->platform
->pciToDma(address
),
456 sizeof(DmaDesc
), &fetchCompleteEvent
,
457 (uint8_t*)curDmaDesc
, latBeforeBegin
);
458 lastDescriptorAddr
= address
;
462 CopyEngine::CopyEngineChannel::fetchDescComplete()
464 DPRINTF(DMACopyEngine
, "Read of descriptor complete\n");
466 if ((curDmaDesc
->command
& DESC_CTRL_NULL
)) {
467 DPRINTF(DMACopyEngine
, "Got NULL descriptor, skipping\n");
468 assert(!(curDmaDesc
->command
& DESC_CTRL_CP_STS
));
469 if (curDmaDesc
->command
& DESC_CTRL_CP_STS
) {
470 panic("Shouldn't be able to get here\n");
471 nextState
= CompletionWrite
;
472 if (inDrain()) return;
473 writeCompletionStatus();
484 if (curDmaDesc
->command
& ~DESC_CTRL_CP_STS
)
485 panic("Descriptor has flag other that completion status set\n");
488 if (inDrain()) return;
493 CopyEngine::CopyEngineChannel::readCopyBytes()
495 anBegin("ReadCopyBytes");
496 DPRINTF(DMACopyEngine
, "Reading %d bytes from buffer to memory location %#x(%#x)\n",
497 curDmaDesc
->len
, curDmaDesc
->dest
,
498 ce
->platform
->pciToDma(curDmaDesc
->src
));
499 cePort
.dmaAction(MemCmd::ReadReq
, ce
->platform
->pciToDma(curDmaDesc
->src
),
500 curDmaDesc
->len
, &readCompleteEvent
, copyBuffer
, 0);
504 CopyEngine::CopyEngineChannel::readCopyBytesComplete()
506 DPRINTF(DMACopyEngine
, "Read of bytes to copy complete\n");
508 nextState
= DMAWrite
;
509 if (inDrain()) return;
514 CopyEngine::CopyEngineChannel::writeCopyBytes()
516 anBegin("WriteCopyBytes");
517 DPRINTF(DMACopyEngine
, "Writing %d bytes from buffer to memory location %#x(%#x)\n",
518 curDmaDesc
->len
, curDmaDesc
->dest
,
519 ce
->platform
->pciToDma(curDmaDesc
->dest
));
521 cePort
.dmaAction(MemCmd::WriteReq
, ce
->platform
->pciToDma(curDmaDesc
->dest
),
522 curDmaDesc
->len
, &writeCompleteEvent
, copyBuffer
, 0);
524 ce
->bytesCopied
[channelId
] += curDmaDesc
->len
;
525 ce
->copiesProcessed
[channelId
]++;
529 CopyEngine::CopyEngineChannel::writeCopyBytesComplete()
531 DPRINTF(DMACopyEngine
, "Write of bytes to copy complete user1: %#x\n",
534 cr
.status
.compl_desc_addr(lastDescriptorAddr
>> 6);
535 completionDataReg
= cr
.status() | 1;
537 anQ("DMAUsedDescQ", channelId
, 1);
538 anQ("AppRecvQ", curDmaDesc
->user1
, curDmaDesc
->len
);
539 if (curDmaDesc
->command
& DESC_CTRL_CP_STS
) {
540 nextState
= CompletionWrite
;
541 if (inDrain()) return;
542 writeCompletionStatus();
546 continueProcessing();
550 CopyEngine::CopyEngineChannel::continueProcessing()
564 if (curDmaDesc
->next
) {
565 nextState
= DescriptorFetch
;
566 fetchAddress
= curDmaDesc
->next
;
567 if (inDrain()) return;
568 fetchDescriptor(curDmaDesc
->next
);
569 } else if (refreshNext
) {
570 nextState
= AddressFetch
;
572 if (inDrain()) return;
573 fetchNextAddr(lastDescriptorAddr
);
583 CopyEngine::CopyEngineChannel::writeCompletionStatus()
585 anBegin("WriteCompletionStatus");
586 DPRINTF(DMACopyEngine
, "Writing completion status %#x to address %#x(%#x)\n",
587 completionDataReg
, cr
.completionAddr
,
588 ce
->platform
->pciToDma(cr
.completionAddr
));
590 cePort
.dmaAction(MemCmd::WriteReq
,
591 ce
->platform
->pciToDma(cr
.completionAddr
),
592 sizeof(completionDataReg
), &statusCompleteEvent
,
593 (uint8_t*)&completionDataReg
, latAfterCompletion
);
597 CopyEngine::CopyEngineChannel::writeStatusComplete()
599 DPRINTF(DMACopyEngine
, "Writing completion status complete\n");
600 continueProcessing();
604 CopyEngine::CopyEngineChannel::fetchNextAddr(Addr address
)
606 anBegin("FetchNextAddr");
607 DPRINTF(DMACopyEngine
, "Fetching next address...\n");
609 cePort
.dmaAction(MemCmd::ReadReq
,
610 ce
->platform
->pciToDma(address
+ offsetof(DmaDesc
, next
)),
611 sizeof(Addr
), &addrCompleteEvent
,
612 (uint8_t*)curDmaDesc
+ offsetof(DmaDesc
, next
), 0);
616 CopyEngine::CopyEngineChannel::fetchAddrComplete()
618 DPRINTF(DMACopyEngine
, "Fetching next address complete: %#x\n",
620 if (!curDmaDesc
->next
) {
621 DPRINTF(DMACopyEngine
, "Got NULL descriptor, nothing more to do\n");
629 nextState
= DescriptorFetch
;
630 fetchAddress
= curDmaDesc
->next
;
631 if (inDrain()) return;
632 fetchDescriptor(curDmaDesc
->next
);
636 CopyEngine::CopyEngineChannel::inDrain()
638 if (ce
->getDrainState() == Drainable::Draining
) {
639 DPRINTF(Drain
, "CopyEngine done draining, processing drain event\n");
640 assert(drainManager
);
641 drainManager
->signalDrainDone();
645 return ce
->getDrainState() != Drainable::Running
;
649 CopyEngine::CopyEngineChannel::drain(DrainManager
*dm
)
651 if (nextState
== Idle
|| ce
->getDrainState() != Drainable::Running
)
653 unsigned int count
= 1;
654 count
+= cePort
.drain(dm
);
656 DPRINTF(Drain
, "CopyEngineChannel not drained\n");
657 this->drainManager
= dm
;
662 CopyEngine::drain(DrainManager
*dm
)
665 count
= pioPort
.drain(dm
) + dmaPort
.drain(dm
) + configPort
.drain(dm
);
666 for (int x
= 0;x
< chan
.size(); x
++)
667 count
+= chan
[x
]->drain(dm
);
670 setDrainState(Draining
);
672 setDrainState(Drained
);
674 DPRINTF(Drain
, "CopyEngine not drained\n");
679 CopyEngine::serialize(std::ostream
&os
)
681 PciDevice::serialize(os
);
683 for (int x
=0; x
< chan
.size(); x
++) {
684 nameOut(os
, csprintf("%s.channel%d", name(), x
));
685 chan
[x
]->serialize(os
);
690 CopyEngine::unserialize(Checkpoint
*cp
, const std::string
§ion
)
692 PciDevice::unserialize(cp
, section
);
693 regs
.unserialize(cp
, section
);
694 for (int x
= 0; x
< chan
.size(); x
++)
695 chan
[x
]->unserialize(cp
, csprintf("%s.channel%d", section
, x
));
699 CopyEngine::CopyEngineChannel::serialize(std::ostream
&os
)
701 SERIALIZE_SCALAR(channelId
);
702 SERIALIZE_SCALAR(busy
);
703 SERIALIZE_SCALAR(underReset
);
704 SERIALIZE_SCALAR(refreshNext
);
705 SERIALIZE_SCALAR(lastDescriptorAddr
);
706 SERIALIZE_SCALAR(completionDataReg
);
707 SERIALIZE_SCALAR(fetchAddress
);
708 int nextState
= this->nextState
;
709 SERIALIZE_SCALAR(nextState
);
710 arrayParamOut(os
, "curDmaDesc", (uint8_t*)curDmaDesc
, sizeof(DmaDesc
));
711 SERIALIZE_ARRAY(copyBuffer
, ce
->params()->XferCap
);
716 CopyEngine::CopyEngineChannel::unserialize(Checkpoint
*cp
, const std::string
§ion
)
718 UNSERIALIZE_SCALAR(channelId
);
719 UNSERIALIZE_SCALAR(busy
);
720 UNSERIALIZE_SCALAR(underReset
);
721 UNSERIALIZE_SCALAR(refreshNext
);
722 UNSERIALIZE_SCALAR(lastDescriptorAddr
);
723 UNSERIALIZE_SCALAR(completionDataReg
);
724 UNSERIALIZE_SCALAR(fetchAddress
);
726 UNSERIALIZE_SCALAR(nextState
);
727 this->nextState
= (ChannelState
)nextState
;
728 arrayParamIn(cp
, section
, "curDmaDesc", (uint8_t*)curDmaDesc
, sizeof(DmaDesc
));
729 UNSERIALIZE_ARRAY(copyBuffer
, ce
->params()->XferCap
);
730 cr
.unserialize(cp
, section
);
735 CopyEngine::CopyEngineChannel::restartStateMachine()
739 fetchNextAddr(lastDescriptorAddr
);
741 case DescriptorFetch
:
742 fetchDescriptor(fetchAddress
);
750 case CompletionWrite
:
751 writeCompletionStatus();
756 panic("Unknown state for CopyEngineChannel\n");
761 CopyEngine::drainResume()
763 Drainable::drainResume();
764 for (int x
= 0;x
< chan
.size(); x
++)
765 chan
[x
]->drainResume();
770 CopyEngine::CopyEngineChannel::drainResume()
772 DPRINTF(DMACopyEngine
, "Restarting state machine at state %d\n", nextState
);
773 restartStateMachine();
777 CopyEngineParams::create()
779 return new CopyEngine(this);