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
, _ce
->params()->min_backoff_delay
,
82 _ce
->params()->max_backoff_delay
),
83 ce(_ce
), channelId(cid
), busy(false), underReset(false),
84 refreshNext(false), latBeforeBegin(ce
->params()->latBeforeBegin
),
85 latAfterCompletion(ce
->params()->latAfterCompletion
),
86 completionDataReg(0), nextState(Idle
), drainEvent(NULL
),
87 fetchCompleteEvent(this), addrCompleteEvent(this),
88 readCompleteEvent(this), writeCompleteEvent(this),
89 statusCompleteEvent(this)
92 cr
.status
.dma_transfer_status(3);
94 cr
.completionAddr
= 0;
96 curDmaDesc
= new DmaDesc
;
97 memset(curDmaDesc
, 0, sizeof(DmaDesc
));
98 copyBuffer
= new uint8_t[ce
->params()->XferCap
];
101 CopyEngine::~CopyEngine()
103 for (int x
= 0; x
< chan
.size(); x
++) {
108 CopyEngine::CopyEngineChannel::~CopyEngineChannel()
111 delete [] copyBuffer
;
115 CopyEngine::getMasterPort(const std::string
&if_name
, int idx
)
117 if (if_name
!= "dma") {
118 // pass it along to our super class
119 return PciDev::getMasterPort(if_name
, idx
);
121 if (idx
>= static_cast<int>(chan
.size())) {
122 panic("CopyEngine::getMasterPort: unknown index %d\n", idx
);
125 return chan
[idx
]->getMasterPort();
131 CopyEngine::CopyEngineChannel::getMasterPort()
137 CopyEngine::CopyEngineChannel::recvCommand()
139 if (cr
.command
.start_dma()) {
141 cr
.status
.dma_transfer_status(0);
142 nextState
= DescriptorFetch
;
143 fetchAddress
= cr
.descChainAddr
;
144 if (ce
->getState() == SimObject::Running
)
145 fetchDescriptor(cr
.descChainAddr
);
146 } else if (cr
.command
.append_dma()) {
148 nextState
= AddressFetch
;
149 if (ce
->getState() == SimObject::Running
)
150 fetchNextAddr(lastDescriptorAddr
);
153 } else if (cr
.command
.reset_dma()) {
157 cr
.status
.dma_transfer_status(3);
160 } else if (cr
.command
.resume_dma() || cr
.command
.abort_dma() ||
161 cr
.command
.suspend_dma())
162 panic("Resume, Abort, and Suspend are not supported\n");
167 CopyEngine::read(PacketPtr pkt
)
172 if (!getBAR(pkt
->getAddr(), bar
, daddr
))
173 panic("Invalid PCI memory access to unmapped memory.\n");
175 // Only Memory register BAR is allowed
178 int size
= pkt
->getSize();
179 if (size
!= sizeof(uint64_t) && size
!= sizeof(uint32_t) &&
180 size
!= sizeof(uint16_t) && size
!= sizeof(uint8_t)) {
181 panic("Unknown size for MMIO access: %d\n", pkt
->getSize());
184 DPRINTF(DMACopyEngine
, "Read device register %#X size: %d\n", daddr
, size
);
189 /// Handle read of register here
195 assert(size
== sizeof(regs
.chanCount
));
196 pkt
->set
<uint8_t>(regs
.chanCount
);
199 assert(size
== sizeof(regs
.xferCap
));
200 pkt
->set
<uint8_t>(regs
.xferCap
);
203 assert(size
== sizeof(uint8_t));
204 pkt
->set
<uint8_t>(regs
.intrctrl());
205 regs
.intrctrl
.master_int_enable(0);
208 assert(size
== sizeof(regs
.attnStatus
));
209 pkt
->set
<uint32_t>(regs
.attnStatus
);
213 panic("Read request to unknown register number: %#x\n", daddr
);
215 pkt
->makeAtomicResponse();
220 // Find which channel we're accessing
223 while (daddr
>= 0x80) {
228 if (chanid
>= regs
.chanCount
)
229 panic("Access to channel %d (device only configured for %d channels)",
230 chanid
, regs
.chanCount
);
233 /// Channel registers are handled here
235 chan
[chanid
]->channelRead(pkt
, daddr
, size
);
237 pkt
->makeAtomicResponse();
242 CopyEngine::CopyEngineChannel::channelRead(Packet
*pkt
, Addr daddr
, int size
)
246 assert(size
== sizeof(uint16_t));
247 pkt
->set
<uint16_t>(cr
.ctrl());
251 assert(size
== sizeof(uint64_t));
252 pkt
->set
<uint64_t>(cr
.status() | ~busy
);
255 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
256 if (size
== sizeof(uint64_t))
257 pkt
->set
<uint64_t>(cr
.descChainAddr
);
259 pkt
->set
<uint32_t>(bits(cr
.descChainAddr
,0,31));
261 case CHAN_CHAINADDR_HIGH
:
262 assert(size
== sizeof(uint32_t));
263 pkt
->set
<uint32_t>(bits(cr
.descChainAddr
,32,63));
266 assert(size
== sizeof(uint8_t));
267 pkt
->set
<uint32_t>(cr
.command());
270 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
271 if (size
== sizeof(uint64_t))
272 pkt
->set
<uint64_t>(cr
.completionAddr
);
274 pkt
->set
<uint32_t>(bits(cr
.completionAddr
,0,31));
276 case CHAN_CMPLNADDR_HIGH
:
277 assert(size
== sizeof(uint32_t));
278 pkt
->set
<uint32_t>(bits(cr
.completionAddr
,32,63));
281 assert(size
== sizeof(uint32_t));
282 pkt
->set
<uint32_t>(cr
.error());
285 panic("Read request to unknown channel register number: (%d)%#x\n",
292 CopyEngine::write(PacketPtr pkt
)
298 if (!getBAR(pkt
->getAddr(), bar
, daddr
))
299 panic("Invalid PCI memory access to unmapped memory.\n");
301 // Only Memory register BAR is allowed
304 int size
= pkt
->getSize();
307 /// Handle write of register here
310 if (size
== sizeof(uint64_t)) {
311 uint64_t val M5_VAR_USED
= pkt
->get
<uint64_t>();
312 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
313 } else if (size
== sizeof(uint32_t)) {
314 uint32_t val M5_VAR_USED
= pkt
->get
<uint32_t>();
315 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
316 } else if (size
== sizeof(uint16_t)) {
317 uint16_t val M5_VAR_USED
= pkt
->get
<uint16_t>();
318 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
319 } else if (size
== sizeof(uint8_t)) {
320 uint8_t val M5_VAR_USED
= pkt
->get
<uint8_t>();
321 DPRINTF(DMACopyEngine
, "Wrote device register %#X value %#X\n", daddr
, val
);
323 panic("Unknown size for MMIO access: %d\n", size
);
331 DPRINTF(DMACopyEngine
, "Warning, ignorning write to register %x\n",
335 regs
.intrctrl
.master_int_enable(bits(pkt
->get
<uint8_t>(),0,1));
338 panic("Read request to unknown register number: %#x\n", daddr
);
340 pkt
->makeAtomicResponse();
344 // Find which channel we're accessing
347 while (daddr
>= 0x80) {
352 if (chanid
>= regs
.chanCount
)
353 panic("Access to channel %d (device only configured for %d channels)",
354 chanid
, regs
.chanCount
);
357 /// Channel registers are handled here
359 chan
[chanid
]->channelWrite(pkt
, daddr
, size
);
361 pkt
->makeAtomicResponse();
366 CopyEngine::CopyEngineChannel::channelWrite(Packet
*pkt
, Addr daddr
, int size
)
370 assert(size
== sizeof(uint16_t));
372 old_int_disable
= cr
.ctrl
.interrupt_disable();
373 cr
.ctrl(pkt
->get
<uint16_t>());
374 if (cr
.ctrl
.interrupt_disable())
375 cr
.ctrl
.interrupt_disable(0);
377 cr
.ctrl
.interrupt_disable(old_int_disable
);
380 assert(size
== sizeof(uint64_t));
381 DPRINTF(DMACopyEngine
, "Warning, ignorning write to register %x\n",
385 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
386 if (size
== sizeof(uint64_t))
387 cr
.descChainAddr
= pkt
->get
<uint64_t>();
389 cr
.descChainAddr
= (uint64_t)pkt
->get
<uint32_t>() |
390 (cr
.descChainAddr
& ~mask(32));
391 DPRINTF(DMACopyEngine
, "Chain Address %x\n", cr
.descChainAddr
);
393 case CHAN_CHAINADDR_HIGH
:
394 assert(size
== sizeof(uint32_t));
395 cr
.descChainAddr
= ((uint64_t)pkt
->get
<uint32_t>() <<32) |
396 (cr
.descChainAddr
& mask(32));
397 DPRINTF(DMACopyEngine
, "Chain Address %x\n", cr
.descChainAddr
);
400 assert(size
== sizeof(uint8_t));
401 cr
.command(pkt
->get
<uint8_t>());
405 assert(size
== sizeof(uint64_t) || size
== sizeof(uint32_t));
406 if (size
== sizeof(uint64_t))
407 cr
.completionAddr
= pkt
->get
<uint64_t>();
409 cr
.completionAddr
= pkt
->get
<uint32_t>() |
410 (cr
.completionAddr
& ~mask(32));
412 case CHAN_CMPLNADDR_HIGH
:
413 assert(size
== sizeof(uint32_t));
414 cr
.completionAddr
= ((uint64_t)pkt
->get
<uint32_t>() <<32) |
415 (cr
.completionAddr
& mask(32));
418 assert(size
== sizeof(uint32_t));
419 cr
.error(~pkt
->get
<uint32_t>() & cr
.error());
422 panic("Read request to unknown channel register number: (%d)%#x\n",
428 CopyEngine::regStats()
430 using namespace Stats
;
432 .init(regs
.chanCount
)
433 .name(name() + ".bytes_copied")
434 .desc("Number of bytes copied by each engine")
438 .init(regs
.chanCount
)
439 .name(name() + ".copies_processed")
440 .desc("Number of copies processed by each engine")
446 CopyEngine::CopyEngineChannel::fetchDescriptor(Addr address
)
449 anBegin("FetchDescriptor");
450 DPRINTF(DMACopyEngine
, "Reading descriptor from at memory location %#x(%#x)\n",
451 address
, ce
->platform
->pciToDma(address
));
455 DPRINTF(DMACopyEngine
, "dmaAction: %#x, %d bytes, to addr %#x\n",
456 ce
->platform
->pciToDma(address
), sizeof(DmaDesc
), curDmaDesc
);
458 cePort
.dmaAction(MemCmd::ReadReq
, ce
->platform
->pciToDma(address
),
459 sizeof(DmaDesc
), &fetchCompleteEvent
,
460 (uint8_t*)curDmaDesc
, latBeforeBegin
);
461 lastDescriptorAddr
= address
;
465 CopyEngine::CopyEngineChannel::fetchDescComplete()
467 DPRINTF(DMACopyEngine
, "Read of descriptor complete\n");
469 if ((curDmaDesc
->command
& DESC_CTRL_NULL
)) {
470 DPRINTF(DMACopyEngine
, "Got NULL descriptor, skipping\n");
471 assert(!(curDmaDesc
->command
& DESC_CTRL_CP_STS
));
472 if (curDmaDesc
->command
& DESC_CTRL_CP_STS
) {
473 panic("Shouldn't be able to get here\n");
474 nextState
= CompletionWrite
;
475 if (inDrain()) return;
476 writeCompletionStatus();
487 if (curDmaDesc
->command
& ~DESC_CTRL_CP_STS
)
488 panic("Descriptor has flag other that completion status set\n");
491 if (inDrain()) return;
496 CopyEngine::CopyEngineChannel::readCopyBytes()
498 anBegin("ReadCopyBytes");
499 DPRINTF(DMACopyEngine
, "Reading %d bytes from buffer to memory location %#x(%#x)\n",
500 curDmaDesc
->len
, curDmaDesc
->dest
,
501 ce
->platform
->pciToDma(curDmaDesc
->src
));
502 cePort
.dmaAction(MemCmd::ReadReq
, ce
->platform
->pciToDma(curDmaDesc
->src
),
503 curDmaDesc
->len
, &readCompleteEvent
, copyBuffer
, 0);
507 CopyEngine::CopyEngineChannel::readCopyBytesComplete()
509 DPRINTF(DMACopyEngine
, "Read of bytes to copy complete\n");
511 nextState
= DMAWrite
;
512 if (inDrain()) return;
517 CopyEngine::CopyEngineChannel::writeCopyBytes()
519 anBegin("WriteCopyBytes");
520 DPRINTF(DMACopyEngine
, "Writing %d bytes from buffer to memory location %#x(%#x)\n",
521 curDmaDesc
->len
, curDmaDesc
->dest
,
522 ce
->platform
->pciToDma(curDmaDesc
->dest
));
524 cePort
.dmaAction(MemCmd::WriteReq
, ce
->platform
->pciToDma(curDmaDesc
->dest
),
525 curDmaDesc
->len
, &writeCompleteEvent
, copyBuffer
, 0);
527 ce
->bytesCopied
[channelId
] += curDmaDesc
->len
;
528 ce
->copiesProcessed
[channelId
]++;
532 CopyEngine::CopyEngineChannel::writeCopyBytesComplete()
534 DPRINTF(DMACopyEngine
, "Write of bytes to copy complete user1: %#x\n",
537 cr
.status
.compl_desc_addr(lastDescriptorAddr
>> 6);
538 completionDataReg
= cr
.status() | 1;
540 anQ("DMAUsedDescQ", channelId
, 1);
541 anQ("AppRecvQ", curDmaDesc
->user1
, curDmaDesc
->len
);
542 if (curDmaDesc
->command
& DESC_CTRL_CP_STS
) {
543 nextState
= CompletionWrite
;
544 if (inDrain()) return;
545 writeCompletionStatus();
549 continueProcessing();
553 CopyEngine::CopyEngineChannel::continueProcessing()
567 if (curDmaDesc
->next
) {
568 nextState
= DescriptorFetch
;
569 fetchAddress
= curDmaDesc
->next
;
570 if (inDrain()) return;
571 fetchDescriptor(curDmaDesc
->next
);
572 } else if (refreshNext
) {
573 nextState
= AddressFetch
;
575 if (inDrain()) return;
576 fetchNextAddr(lastDescriptorAddr
);
586 CopyEngine::CopyEngineChannel::writeCompletionStatus()
588 anBegin("WriteCompletionStatus");
589 DPRINTF(DMACopyEngine
, "Writing completion status %#x to address %#x(%#x)\n",
590 completionDataReg
, cr
.completionAddr
,
591 ce
->platform
->pciToDma(cr
.completionAddr
));
593 cePort
.dmaAction(MemCmd::WriteReq
,
594 ce
->platform
->pciToDma(cr
.completionAddr
),
595 sizeof(completionDataReg
), &statusCompleteEvent
,
596 (uint8_t*)&completionDataReg
, latAfterCompletion
);
600 CopyEngine::CopyEngineChannel::writeStatusComplete()
602 DPRINTF(DMACopyEngine
, "Writing completion status complete\n");
603 continueProcessing();
607 CopyEngine::CopyEngineChannel::fetchNextAddr(Addr address
)
609 anBegin("FetchNextAddr");
610 DPRINTF(DMACopyEngine
, "Fetching next address...\n");
612 cePort
.dmaAction(MemCmd::ReadReq
,
613 ce
->platform
->pciToDma(address
+ offsetof(DmaDesc
, next
)),
614 sizeof(Addr
), &addrCompleteEvent
,
615 (uint8_t*)curDmaDesc
+ offsetof(DmaDesc
, next
), 0);
619 CopyEngine::CopyEngineChannel::fetchAddrComplete()
621 DPRINTF(DMACopyEngine
, "Fetching next address complete: %#x\n",
623 if (!curDmaDesc
->next
) {
624 DPRINTF(DMACopyEngine
, "Got NULL descriptor, nothing more to do\n");
632 nextState
= DescriptorFetch
;
633 fetchAddress
= curDmaDesc
->next
;
634 if (inDrain()) return;
635 fetchDescriptor(curDmaDesc
->next
);
639 CopyEngine::CopyEngineChannel::inDrain()
641 if (ce
->getState() == SimObject::Draining
) {
642 DPRINTF(Drain
, "CopyEngine done draining, processing drain event\n");
644 drainEvent
->process();
648 return ce
->getState() != SimObject::Running
;
652 CopyEngine::CopyEngineChannel::drain(Event
*de
)
654 if (nextState
== Idle
|| ce
->getState() != SimObject::Running
)
656 unsigned int count
= 1;
657 count
+= cePort
.drain(de
);
659 DPRINTF(Drain
, "CopyEngineChannel not drained\n");
665 CopyEngine::drain(Event
*de
)
668 count
= pioPort
.drain(de
) + dmaPort
.drain(de
) + configPort
.drain(de
);
669 for (int x
= 0;x
< chan
.size(); x
++)
670 count
+= chan
[x
]->drain(de
);
673 changeState(Draining
);
675 changeState(Drained
);
677 DPRINTF(Drain
, "CopyEngine not drained\n");
682 CopyEngine::serialize(std::ostream
&os
)
684 PciDev::serialize(os
);
686 for (int x
=0; x
< chan
.size(); x
++) {
687 nameOut(os
, csprintf("%s.channel%d", name(), x
));
688 chan
[x
]->serialize(os
);
693 CopyEngine::unserialize(Checkpoint
*cp
, const std::string
§ion
)
695 PciDev::unserialize(cp
, section
);
696 regs
.unserialize(cp
, section
);
697 for (int x
= 0; x
< chan
.size(); x
++)
698 chan
[x
]->unserialize(cp
, csprintf("%s.channel%d", section
, x
));
702 CopyEngine::CopyEngineChannel::serialize(std::ostream
&os
)
704 SERIALIZE_SCALAR(channelId
);
705 SERIALIZE_SCALAR(busy
);
706 SERIALIZE_SCALAR(underReset
);
707 SERIALIZE_SCALAR(refreshNext
);
708 SERIALIZE_SCALAR(lastDescriptorAddr
);
709 SERIALIZE_SCALAR(completionDataReg
);
710 SERIALIZE_SCALAR(fetchAddress
);
711 int nextState
= this->nextState
;
712 SERIALIZE_SCALAR(nextState
);
713 arrayParamOut(os
, "curDmaDesc", (uint8_t*)curDmaDesc
, sizeof(DmaDesc
));
714 SERIALIZE_ARRAY(copyBuffer
, ce
->params()->XferCap
);
719 CopyEngine::CopyEngineChannel::unserialize(Checkpoint
*cp
, const std::string
§ion
)
721 UNSERIALIZE_SCALAR(channelId
);
722 UNSERIALIZE_SCALAR(busy
);
723 UNSERIALIZE_SCALAR(underReset
);
724 UNSERIALIZE_SCALAR(refreshNext
);
725 UNSERIALIZE_SCALAR(lastDescriptorAddr
);
726 UNSERIALIZE_SCALAR(completionDataReg
);
727 UNSERIALIZE_SCALAR(fetchAddress
);
729 UNSERIALIZE_SCALAR(nextState
);
730 this->nextState
= (ChannelState
)nextState
;
731 arrayParamIn(cp
, section
, "curDmaDesc", (uint8_t*)curDmaDesc
, sizeof(DmaDesc
));
732 UNSERIALIZE_ARRAY(copyBuffer
, ce
->params()->XferCap
);
733 cr
.unserialize(cp
, section
);
738 CopyEngine::CopyEngineChannel::restartStateMachine()
742 fetchNextAddr(lastDescriptorAddr
);
744 case DescriptorFetch
:
745 fetchDescriptor(fetchAddress
);
753 case CompletionWrite
:
754 writeCompletionStatus();
759 panic("Unknown state for CopyEngineChannel\n");
767 for (int x
= 0;x
< chan
.size(); x
++)
773 CopyEngine::CopyEngineChannel::resume()
775 DPRINTF(DMACopyEngine
, "Restarting state machine at state %d\n", nextState
);
776 restartStateMachine();
780 CopyEngineParams::create()
782 return new CopyEngine(this);