DMASequencer.cc revision 10231
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
298232Snate@binkert.org#include "debug/RubyDma.hh"
309104Shestness@cs.utexas.edu#include "debug/RubyStats.hh"
317039Snate@binkert.org#include "mem/protocol/SequencerMsg.hh"
327039Snate@binkert.org#include "mem/protocol/SequencerRequestType.hh"
337039Snate@binkert.org#include "mem/ruby/system/DMASequencer.hh"
346285Snate@binkert.org#include "mem/ruby/system/System.hh"
356285Snate@binkert.org
366876Ssteve.reinhardt@amd.comDMASequencer::DMASequencer(const Params *p)
377039Snate@binkert.org    : RubyPort(p)
386285Snate@binkert.org{
396285Snate@binkert.org}
406285Snate@binkert.org
417039Snate@binkert.orgvoid
427039Snate@binkert.orgDMASequencer::init()
436285Snate@binkert.org{
447039Snate@binkert.org    RubyPort::init();
457039Snate@binkert.org    m_is_busy = false;
467039Snate@binkert.org    m_data_block_mask = ~ (~0 << RubySystem::getBlockSizeBits());
476285Snate@binkert.org}
486285Snate@binkert.org
497039Snate@binkert.orgRequestStatus
508615Snilay@cs.wisc.eduDMASequencer::makeRequest(PacketPtr pkt)
516285Snate@binkert.org{
527544SBrad.Beckmann@amd.com    if (m_is_busy) {
537544SBrad.Beckmann@amd.com        return RequestStatus_BufferFull;
547544SBrad.Beckmann@amd.com    }
557544SBrad.Beckmann@amd.com
568615Snilay@cs.wisc.edu    uint64_t paddr = pkt->getAddr();
578615Snilay@cs.wisc.edu    uint8_t* data =  pkt->getPtr<uint8_t>(true);
588615Snilay@cs.wisc.edu    int len = pkt->getSize();
598615Snilay@cs.wisc.edu    bool write = pkt->isWrite();
606285Snate@binkert.org
617039Snate@binkert.org    assert(!m_is_busy);  // only support one outstanding DMA request
627039Snate@binkert.org    m_is_busy = true;
636285Snate@binkert.org
647039Snate@binkert.org    active_request.start_paddr = paddr;
657039Snate@binkert.org    active_request.write = write;
667039Snate@binkert.org    active_request.data = data;
677039Snate@binkert.org    active_request.len = len;
687039Snate@binkert.org    active_request.bytes_completed = 0;
697039Snate@binkert.org    active_request.bytes_issued = 0;
708615Snilay@cs.wisc.edu    active_request.pkt = pkt;
716285Snate@binkert.org
729508Snilay@cs.wisc.edu    SequencerMsg *msg = new SequencerMsg(clockEdge());
737453Snate@binkert.org    msg->getPhysicalAddress() = Address(paddr);
747453Snate@binkert.org    msg->getLineAddress() = line_address(msg->getPhysicalAddress());
757453Snate@binkert.org    msg->getType() = write ? SequencerRequestType_ST : SequencerRequestType_LD;
767039Snate@binkert.org    int offset = paddr & m_data_block_mask;
776888SBrad.Beckmann@amd.com
787453Snate@binkert.org    msg->getLen() = (offset + len) <= RubySystem::getBlockSizeBytes() ?
797039Snate@binkert.org        len : RubySystem::getBlockSizeBytes() - offset;
806888SBrad.Beckmann@amd.com
817915SBrad.Beckmann@amd.com    if (write && (data != NULL)) {
827915SBrad.Beckmann@amd.com        if (active_request.data != NULL) {
837915SBrad.Beckmann@amd.com            msg->getDataBlk().setData(data, offset, msg->getLen());
847915SBrad.Beckmann@amd.com        }
857039Snate@binkert.org    }
866888SBrad.Beckmann@amd.com
877039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
887039Snate@binkert.org    m_mandatory_q_ptr->enqueue(msg);
897453Snate@binkert.org    active_request.bytes_issued += msg->getLen();
906285Snate@binkert.org
917039Snate@binkert.org    return RequestStatus_Issued;
926285Snate@binkert.org}
936285Snate@binkert.org
947039Snate@binkert.orgvoid
957039Snate@binkert.orgDMASequencer::issueNext()
966285Snate@binkert.org{
9710231Ssteve.reinhardt@amd.com    assert(m_is_busy);
987039Snate@binkert.org    active_request.bytes_completed = active_request.bytes_issued;
997039Snate@binkert.org    if (active_request.len == active_request.bytes_completed) {
1008162SBrad.Beckmann@amd.com        //
1018162SBrad.Beckmann@amd.com        // Must unset the busy flag before calling back the dma port because
1028162SBrad.Beckmann@amd.com        // the callback may cause a previously nacked request to be reissued
1038162SBrad.Beckmann@amd.com        //
1048162SBrad.Beckmann@amd.com        DPRINTF(RubyDma, "DMA request completed\n");
1058162SBrad.Beckmann@amd.com        m_is_busy = false;
1067039Snate@binkert.org        ruby_hit_callback(active_request.pkt);
1077039Snate@binkert.org        return;
1087039Snate@binkert.org    }
1096285Snate@binkert.org
1109508Snilay@cs.wisc.edu    SequencerMsg *msg = new SequencerMsg(clockEdge());
1117453Snate@binkert.org    msg->getPhysicalAddress() = Address(active_request.start_paddr +
1127039Snate@binkert.org                                       active_request.bytes_completed);
1136888SBrad.Beckmann@amd.com
1147453Snate@binkert.org    assert((msg->getPhysicalAddress().getAddress() & m_data_block_mask) == 0);
1157453Snate@binkert.org    msg->getLineAddress() = line_address(msg->getPhysicalAddress());
1166888SBrad.Beckmann@amd.com
1177453Snate@binkert.org    msg->getType() = (active_request.write ? SequencerRequestType_ST :
1187039Snate@binkert.org                     SequencerRequestType_LD);
1196888SBrad.Beckmann@amd.com
1207453Snate@binkert.org    msg->getLen() =
1217039Snate@binkert.org        (active_request.len -
1227039Snate@binkert.org         active_request.bytes_completed < RubySystem::getBlockSizeBytes() ?
1237039Snate@binkert.org         active_request.len - active_request.bytes_completed :
1247039Snate@binkert.org         RubySystem::getBlockSizeBytes());
1256888SBrad.Beckmann@amd.com
1267039Snate@binkert.org    if (active_request.write) {
1277453Snate@binkert.org        msg->getDataBlk().
1287039Snate@binkert.org            setData(&active_request.data[active_request.bytes_completed],
1297453Snate@binkert.org                    0, msg->getLen());
1307453Snate@binkert.org        msg->getType() = SequencerRequestType_ST;
1317039Snate@binkert.org    } else {
1327453Snate@binkert.org        msg->getType() = SequencerRequestType_LD;
1337039Snate@binkert.org    }
1346888SBrad.Beckmann@amd.com
1357039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
1367039Snate@binkert.org    m_mandatory_q_ptr->enqueue(msg);
1377453Snate@binkert.org    active_request.bytes_issued += msg->getLen();
1388160SBrad.Beckmann@amd.com    DPRINTF(RubyDma,
1398160SBrad.Beckmann@amd.com            "DMA request bytes issued %d, bytes completed %d, total len %d\n",
1408160SBrad.Beckmann@amd.com            active_request.bytes_issued, active_request.bytes_completed,
1418160SBrad.Beckmann@amd.com            active_request.len);
1426285Snate@binkert.org}
1436285Snate@binkert.org
1447039Snate@binkert.orgvoid
1457039Snate@binkert.orgDMASequencer::dataCallback(const DataBlock & dblk)
1466285Snate@binkert.org{
14710231Ssteve.reinhardt@amd.com    assert(m_is_busy);
1487039Snate@binkert.org    int len = active_request.bytes_issued - active_request.bytes_completed;
1497039Snate@binkert.org    int offset = 0;
1507039Snate@binkert.org    if (active_request.bytes_completed == 0)
1517039Snate@binkert.org        offset = active_request.start_paddr & m_data_block_mask;
15210231Ssteve.reinhardt@amd.com    assert(!active_request.write);
1537915SBrad.Beckmann@amd.com    if (active_request.data != NULL) {
1547915SBrad.Beckmann@amd.com        memcpy(&active_request.data[active_request.bytes_completed],
1557915SBrad.Beckmann@amd.com               dblk.getData(offset, len), len);
1567915SBrad.Beckmann@amd.com    }
1577039Snate@binkert.org    issueNext();
1586285Snate@binkert.org}
1596285Snate@binkert.org
1607039Snate@binkert.orgvoid
1617039Snate@binkert.orgDMASequencer::ackCallback()
1626285Snate@binkert.org{
1637039Snate@binkert.org    issueNext();
1646285Snate@binkert.org}
1656285Snate@binkert.org
1667039Snate@binkert.orgvoid
1679104Shestness@cs.utexas.eduDMASequencer::recordRequestType(DMASequencerRequestType requestType) {
1689104Shestness@cs.utexas.edu    DPRINTF(RubyStats, "Recorded statistic: %s\n",
1699104Shestness@cs.utexas.edu            DMASequencerRequestType_to_string(requestType));
1709104Shestness@cs.utexas.edu}
1719104Shestness@cs.utexas.edu
1726876Ssteve.reinhardt@amd.comDMASequencer *
1736876Ssteve.reinhardt@amd.comDMASequencerParams::create()
1746876Ssteve.reinhardt@amd.com{
1756876Ssteve.reinhardt@amd.com    return new DMASequencer(this);
1766876Ssteve.reinhardt@amd.com}
177