DMASequencer.cc revision 11443
17008Snate@binkert.org/*
27008Snate@binkert.org * Copyright (c) 2008 Mark D. Hill and David A. Wood
37008Snate@binkert.org * All rights reserved.
47008Snate@binkert.org *
57008Snate@binkert.org * Redistribution and use in source and binary forms, with or without
67008Snate@binkert.org * modification, are permitted provided that the following conditions are
77008Snate@binkert.org * met: redistributions of source code must retain the above copyright
87008Snate@binkert.org * notice, this list of conditions and the following disclaimer;
97008Snate@binkert.org * redistributions in binary form must reproduce the above copyright
107008Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
117008Snate@binkert.org * documentation and/or other materials provided with the distribution;
127008Snate@binkert.org * neither the name of the copyright holders nor the names of its
137008Snate@binkert.org * contributors may be used to endorse or promote products derived from
147008Snate@binkert.org * this software without specific prior written permission.
157008Snate@binkert.org *
167008Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177008Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187008Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197008Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207008Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217008Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227008Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237008Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247008Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257008Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267008Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277008Snate@binkert.org */
286285Snate@binkert.org
2910472Sandreas.hansson@arm.com#include <memory>
3010472Sandreas.hansson@arm.com
318232Snate@binkert.org#include "debug/RubyDma.hh"
329104Shestness@cs.utexas.edu#include "debug/RubyStats.hh"
337039Snate@binkert.org#include "mem/protocol/SequencerMsg.hh"
3411339SMichael.Lebeane@amd.com#include "mem/protocol/SequencerRequestType.hh"
357039Snate@binkert.org#include "mem/ruby/system/DMASequencer.hh"
3611108Sdavid.hashe@amd.com#include "mem/ruby/system/RubySystem.hh"
376285Snate@binkert.org
386876Ssteve.reinhardt@amd.comDMASequencer::DMASequencer(const Params *p)
3911339SMichael.Lebeane@amd.com    : RubyPort(p)
406285Snate@binkert.org{
416285Snate@binkert.org}
426285Snate@binkert.org
437039Snate@binkert.orgvoid
447039Snate@binkert.orgDMASequencer::init()
456285Snate@binkert.org{
4611339SMichael.Lebeane@amd.com    RubyPort::init();
477039Snate@binkert.org    m_is_busy = false;
4811443Sandreas.hansson@arm.com    m_data_block_mask = mask(RubySystem::getBlockSizeBits());
4911346Santhony.gutierrez@amd.com
5011346Santhony.gutierrez@amd.com    for (const auto &s_port : slave_ports)
5111346Santhony.gutierrez@amd.com        s_port->sendRangeChange();
5210518Snilay@cs.wisc.edu}
5310518Snilay@cs.wisc.edu
547039Snate@binkert.orgRequestStatus
558615Snilay@cs.wisc.eduDMASequencer::makeRequest(PacketPtr pkt)
566285Snate@binkert.org{
577544SBrad.Beckmann@amd.com    if (m_is_busy) {
587544SBrad.Beckmann@amd.com        return RequestStatus_BufferFull;
597544SBrad.Beckmann@amd.com    }
607544SBrad.Beckmann@amd.com
6111025Snilay@cs.wisc.edu    Addr paddr = pkt->getAddr();
6210562Sandreas.hansson@arm.com    uint8_t* data =  pkt->getPtr<uint8_t>();
638615Snilay@cs.wisc.edu    int len = pkt->getSize();
648615Snilay@cs.wisc.edu    bool write = pkt->isWrite();
656285Snate@binkert.org
667039Snate@binkert.org    assert(!m_is_busy);  // only support one outstanding DMA request
677039Snate@binkert.org    m_is_busy = true;
686285Snate@binkert.org
697039Snate@binkert.org    active_request.start_paddr = paddr;
707039Snate@binkert.org    active_request.write = write;
717039Snate@binkert.org    active_request.data = data;
727039Snate@binkert.org    active_request.len = len;
737039Snate@binkert.org    active_request.bytes_completed = 0;
747039Snate@binkert.org    active_request.bytes_issued = 0;
758615Snilay@cs.wisc.edu    active_request.pkt = pkt;
766285Snate@binkert.org
7710472Sandreas.hansson@arm.com    std::shared_ptr<SequencerMsg> msg =
7810472Sandreas.hansson@arm.com        std::make_shared<SequencerMsg>(clockEdge());
7911025Snilay@cs.wisc.edu    msg->getPhysicalAddress() = paddr;
8011025Snilay@cs.wisc.edu    msg->getLineAddress() = makeLineAddress(msg->getPhysicalAddress());
817453Snate@binkert.org    msg->getType() = write ? SequencerRequestType_ST : SequencerRequestType_LD;
827039Snate@binkert.org    int offset = paddr & m_data_block_mask;
836888SBrad.Beckmann@amd.com
847453Snate@binkert.org    msg->getLen() = (offset + len) <= RubySystem::getBlockSizeBytes() ?
857039Snate@binkert.org        len : RubySystem::getBlockSizeBytes() - offset;
866888SBrad.Beckmann@amd.com
877915SBrad.Beckmann@amd.com    if (write && (data != NULL)) {
887915SBrad.Beckmann@amd.com        if (active_request.data != NULL) {
897915SBrad.Beckmann@amd.com            msg->getDataBlk().setData(data, offset, msg->getLen());
907915SBrad.Beckmann@amd.com        }
917039Snate@binkert.org    }
926888SBrad.Beckmann@amd.com
937039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
9411111Snilay@cs.wisc.edu    m_mandatory_q_ptr->enqueue(msg, clockEdge(), cyclesToTicks(Cycles(1)));
957453Snate@binkert.org    active_request.bytes_issued += msg->getLen();
966285Snate@binkert.org
977039Snate@binkert.org    return RequestStatus_Issued;
986285Snate@binkert.org}
996285Snate@binkert.org
1007039Snate@binkert.orgvoid
1017039Snate@binkert.orgDMASequencer::issueNext()
1026285Snate@binkert.org{
10310231Ssteve.reinhardt@amd.com    assert(m_is_busy);
1047039Snate@binkert.org    active_request.bytes_completed = active_request.bytes_issued;
1057039Snate@binkert.org    if (active_request.len == active_request.bytes_completed) {
1068162SBrad.Beckmann@amd.com        //
1078162SBrad.Beckmann@amd.com        // Must unset the busy flag before calling back the dma port because
1088162SBrad.Beckmann@amd.com        // the callback may cause a previously nacked request to be reissued
1098162SBrad.Beckmann@amd.com        //
1108162SBrad.Beckmann@amd.com        DPRINTF(RubyDma, "DMA request completed\n");
1118162SBrad.Beckmann@amd.com        m_is_busy = false;
1127039Snate@binkert.org        ruby_hit_callback(active_request.pkt);
1137039Snate@binkert.org        return;
1147039Snate@binkert.org    }
1156285Snate@binkert.org
11610472Sandreas.hansson@arm.com    std::shared_ptr<SequencerMsg> msg =
11710472Sandreas.hansson@arm.com        std::make_shared<SequencerMsg>(clockEdge());
11811025Snilay@cs.wisc.edu    msg->getPhysicalAddress() = active_request.start_paddr +
11911025Snilay@cs.wisc.edu                                active_request.bytes_completed;
1206888SBrad.Beckmann@amd.com
12111025Snilay@cs.wisc.edu    assert((msg->getPhysicalAddress() & m_data_block_mask) == 0);
12211025Snilay@cs.wisc.edu    msg->getLineAddress() = makeLineAddress(msg->getPhysicalAddress());
1236888SBrad.Beckmann@amd.com
1247453Snate@binkert.org    msg->getType() = (active_request.write ? SequencerRequestType_ST :
1257039Snate@binkert.org                     SequencerRequestType_LD);
1266888SBrad.Beckmann@amd.com
1277453Snate@binkert.org    msg->getLen() =
1287039Snate@binkert.org        (active_request.len -
1297039Snate@binkert.org         active_request.bytes_completed < RubySystem::getBlockSizeBytes() ?
1307039Snate@binkert.org         active_request.len - active_request.bytes_completed :
1317039Snate@binkert.org         RubySystem::getBlockSizeBytes());
1326888SBrad.Beckmann@amd.com
1337039Snate@binkert.org    if (active_request.write) {
1347453Snate@binkert.org        msg->getDataBlk().
1357039Snate@binkert.org            setData(&active_request.data[active_request.bytes_completed],
1367453Snate@binkert.org                    0, msg->getLen());
1377039Snate@binkert.org    }
1386888SBrad.Beckmann@amd.com
1397039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
14011111Snilay@cs.wisc.edu    m_mandatory_q_ptr->enqueue(msg, clockEdge(), cyclesToTicks(Cycles(1)));
1417453Snate@binkert.org    active_request.bytes_issued += msg->getLen();
14210917Sbrandon.potter@amd.com    DPRINTF(RubyDma,
1438160SBrad.Beckmann@amd.com            "DMA request bytes issued %d, bytes completed %d, total len %d\n",
1448160SBrad.Beckmann@amd.com            active_request.bytes_issued, active_request.bytes_completed,
1458160SBrad.Beckmann@amd.com            active_request.len);
1466285Snate@binkert.org}
1476285Snate@binkert.org
1487039Snate@binkert.orgvoid
1497039Snate@binkert.orgDMASequencer::dataCallback(const DataBlock & dblk)
1506285Snate@binkert.org{
15110231Ssteve.reinhardt@amd.com    assert(m_is_busy);
1527039Snate@binkert.org    int len = active_request.bytes_issued - active_request.bytes_completed;
1537039Snate@binkert.org    int offset = 0;
1547039Snate@binkert.org    if (active_request.bytes_completed == 0)
1557039Snate@binkert.org        offset = active_request.start_paddr & m_data_block_mask;
15610231Ssteve.reinhardt@amd.com    assert(!active_request.write);
1577915SBrad.Beckmann@amd.com    if (active_request.data != NULL) {
1587915SBrad.Beckmann@amd.com        memcpy(&active_request.data[active_request.bytes_completed],
1597915SBrad.Beckmann@amd.com               dblk.getData(offset, len), len);
1607915SBrad.Beckmann@amd.com    }
1617039Snate@binkert.org    issueNext();
1626285Snate@binkert.org}
1636285Snate@binkert.org
1647039Snate@binkert.orgvoid
1657039Snate@binkert.orgDMASequencer::ackCallback()
1666285Snate@binkert.org{
1677039Snate@binkert.org    issueNext();
1686285Snate@binkert.org}
1696285Snate@binkert.org
1707039Snate@binkert.orgvoid
17110518Snilay@cs.wisc.eduDMASequencer::recordRequestType(DMASequencerRequestType requestType)
17210518Snilay@cs.wisc.edu{
1739104Shestness@cs.utexas.edu    DPRINTF(RubyStats, "Recorded statistic: %s\n",
1749104Shestness@cs.utexas.edu            DMASequencerRequestType_to_string(requestType));
1759104Shestness@cs.utexas.edu}
1769104Shestness@cs.utexas.edu
1776876Ssteve.reinhardt@amd.comDMASequencer *
1786876Ssteve.reinhardt@amd.comDMASequencerParams::create()
1796876Ssteve.reinhardt@amd.com{
1806876Ssteve.reinhardt@amd.com    return new DMASequencer(this);
1816876Ssteve.reinhardt@amd.com}
182