DMASequencer.cc revision 8232
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"
307039Snate@binkert.org#include "mem/protocol/SequencerMsg.hh"
317039Snate@binkert.org#include "mem/protocol/SequencerRequestType.hh"
326285Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
336285Snate@binkert.org#include "mem/ruby/slicc_interface/AbstractController.hh"
347039Snate@binkert.org#include "mem/ruby/system/DMASequencer.hh"
356285Snate@binkert.org#include "mem/ruby/system/System.hh"
366285Snate@binkert.org
376876Ssteve.reinhardt@amd.comDMASequencer::DMASequencer(const Params *p)
387039Snate@binkert.org    : RubyPort(p)
396285Snate@binkert.org{
406285Snate@binkert.org}
416285Snate@binkert.org
427039Snate@binkert.orgvoid
437039Snate@binkert.orgDMASequencer::init()
446285Snate@binkert.org{
457039Snate@binkert.org    RubyPort::init();
467039Snate@binkert.org    m_is_busy = false;
477039Snate@binkert.org    m_data_block_mask = ~ (~0 << RubySystem::getBlockSizeBits());
486285Snate@binkert.org}
496285Snate@binkert.org
507039Snate@binkert.orgRequestStatus
517039Snate@binkert.orgDMASequencer::makeRequest(const RubyRequest &request)
526285Snate@binkert.org{
537544SBrad.Beckmann@amd.com    if (m_is_busy) {
547544SBrad.Beckmann@amd.com        return RequestStatus_BufferFull;
557544SBrad.Beckmann@amd.com    }
567544SBrad.Beckmann@amd.com
578174Snilay@cs.wisc.edu    uint64_t paddr = request.m_PhysicalAddress.getAddress();
587039Snate@binkert.org    uint8_t* data = request.data;
598174Snilay@cs.wisc.edu    int len = request.m_Size;
607039Snate@binkert.org    bool write = false;
618174Snilay@cs.wisc.edu    switch(request.m_Type) {
627039Snate@binkert.org      case RubyRequestType_LD:
637039Snate@binkert.org        write = false;
647039Snate@binkert.org        break;
657039Snate@binkert.org      case RubyRequestType_ST:
667039Snate@binkert.org        write = true;
677039Snate@binkert.org        break;
688165Snilay@cs.wisc.edu      default:
697039Snate@binkert.org        panic("DMASequencer::makeRequest does not support RubyRequestType");
707039Snate@binkert.org        return RequestStatus_NULL;
717039Snate@binkert.org    }
726285Snate@binkert.org
737039Snate@binkert.org    assert(!m_is_busy);  // only support one outstanding DMA request
747039Snate@binkert.org    m_is_busy = true;
756285Snate@binkert.org
767039Snate@binkert.org    active_request.start_paddr = paddr;
777039Snate@binkert.org    active_request.write = write;
787039Snate@binkert.org    active_request.data = data;
797039Snate@binkert.org    active_request.len = len;
807039Snate@binkert.org    active_request.bytes_completed = 0;
817039Snate@binkert.org    active_request.bytes_issued = 0;
827039Snate@binkert.org    active_request.pkt = request.pkt;
836285Snate@binkert.org
847453Snate@binkert.org    SequencerMsg *msg = new SequencerMsg;
857453Snate@binkert.org    msg->getPhysicalAddress() = Address(paddr);
867453Snate@binkert.org    msg->getLineAddress() = line_address(msg->getPhysicalAddress());
877453Snate@binkert.org    msg->getType() = write ? SequencerRequestType_ST : SequencerRequestType_LD;
887039Snate@binkert.org    int offset = paddr & m_data_block_mask;
896888SBrad.Beckmann@amd.com
907453Snate@binkert.org    msg->getLen() = (offset + len) <= RubySystem::getBlockSizeBytes() ?
917039Snate@binkert.org        len : RubySystem::getBlockSizeBytes() - offset;
926888SBrad.Beckmann@amd.com
937915SBrad.Beckmann@amd.com    if (write && (data != NULL)) {
947915SBrad.Beckmann@amd.com        if (active_request.data != NULL) {
957915SBrad.Beckmann@amd.com            msg->getDataBlk().setData(data, offset, msg->getLen());
967915SBrad.Beckmann@amd.com        }
977039Snate@binkert.org    }
986888SBrad.Beckmann@amd.com
997039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
1007039Snate@binkert.org    m_mandatory_q_ptr->enqueue(msg);
1017453Snate@binkert.org    active_request.bytes_issued += msg->getLen();
1026285Snate@binkert.org
1037039Snate@binkert.org    return RequestStatus_Issued;
1046285Snate@binkert.org}
1056285Snate@binkert.org
1067039Snate@binkert.orgvoid
1077039Snate@binkert.orgDMASequencer::issueNext()
1086285Snate@binkert.org{
1097039Snate@binkert.org    assert(m_is_busy == true);
1107039Snate@binkert.org    active_request.bytes_completed = active_request.bytes_issued;
1117039Snate@binkert.org    if (active_request.len == active_request.bytes_completed) {
1128162SBrad.Beckmann@amd.com        //
1138162SBrad.Beckmann@amd.com        // Must unset the busy flag before calling back the dma port because
1148162SBrad.Beckmann@amd.com        // the callback may cause a previously nacked request to be reissued
1158162SBrad.Beckmann@amd.com        //
1168162SBrad.Beckmann@amd.com        DPRINTF(RubyDma, "DMA request completed\n");
1178162SBrad.Beckmann@amd.com        m_is_busy = false;
1187039Snate@binkert.org        ruby_hit_callback(active_request.pkt);
1197039Snate@binkert.org        return;
1207039Snate@binkert.org    }
1216285Snate@binkert.org
1227453Snate@binkert.org    SequencerMsg *msg = new SequencerMsg;
1237453Snate@binkert.org    msg->getPhysicalAddress() = Address(active_request.start_paddr +
1247039Snate@binkert.org                                       active_request.bytes_completed);
1256888SBrad.Beckmann@amd.com
1267453Snate@binkert.org    assert((msg->getPhysicalAddress().getAddress() & m_data_block_mask) == 0);
1277453Snate@binkert.org    msg->getLineAddress() = line_address(msg->getPhysicalAddress());
1286888SBrad.Beckmann@amd.com
1297453Snate@binkert.org    msg->getType() = (active_request.write ? SequencerRequestType_ST :
1307039Snate@binkert.org                     SequencerRequestType_LD);
1316888SBrad.Beckmann@amd.com
1327453Snate@binkert.org    msg->getLen() =
1337039Snate@binkert.org        (active_request.len -
1347039Snate@binkert.org         active_request.bytes_completed < RubySystem::getBlockSizeBytes() ?
1357039Snate@binkert.org         active_request.len - active_request.bytes_completed :
1367039Snate@binkert.org         RubySystem::getBlockSizeBytes());
1376888SBrad.Beckmann@amd.com
1387039Snate@binkert.org    if (active_request.write) {
1397453Snate@binkert.org        msg->getDataBlk().
1407039Snate@binkert.org            setData(&active_request.data[active_request.bytes_completed],
1417453Snate@binkert.org                    0, msg->getLen());
1427453Snate@binkert.org        msg->getType() = SequencerRequestType_ST;
1437039Snate@binkert.org    } else {
1447453Snate@binkert.org        msg->getType() = SequencerRequestType_LD;
1457039Snate@binkert.org    }
1466888SBrad.Beckmann@amd.com
1477039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
1487039Snate@binkert.org    m_mandatory_q_ptr->enqueue(msg);
1497453Snate@binkert.org    active_request.bytes_issued += msg->getLen();
1508160SBrad.Beckmann@amd.com    DPRINTF(RubyDma,
1518160SBrad.Beckmann@amd.com            "DMA request bytes issued %d, bytes completed %d, total len %d\n",
1528160SBrad.Beckmann@amd.com            active_request.bytes_issued, active_request.bytes_completed,
1538160SBrad.Beckmann@amd.com            active_request.len);
1546285Snate@binkert.org}
1556285Snate@binkert.org
1567039Snate@binkert.orgvoid
1577039Snate@binkert.orgDMASequencer::dataCallback(const DataBlock & dblk)
1586285Snate@binkert.org{
1597039Snate@binkert.org    assert(m_is_busy == true);
1607039Snate@binkert.org    int len = active_request.bytes_issued - active_request.bytes_completed;
1617039Snate@binkert.org    int offset = 0;
1627039Snate@binkert.org    if (active_request.bytes_completed == 0)
1637039Snate@binkert.org        offset = active_request.start_paddr & m_data_block_mask;
1647039Snate@binkert.org    assert(active_request.write == false);
1657915SBrad.Beckmann@amd.com    if (active_request.data != NULL) {
1667915SBrad.Beckmann@amd.com        memcpy(&active_request.data[active_request.bytes_completed],
1677915SBrad.Beckmann@amd.com               dblk.getData(offset, len), len);
1687915SBrad.Beckmann@amd.com    }
1697039Snate@binkert.org    issueNext();
1706285Snate@binkert.org}
1716285Snate@binkert.org
1727039Snate@binkert.orgvoid
1737039Snate@binkert.orgDMASequencer::ackCallback()
1746285Snate@binkert.org{
1757039Snate@binkert.org    issueNext();
1766285Snate@binkert.org}
1776285Snate@binkert.org
1787039Snate@binkert.orgvoid
1797055Snate@binkert.orgDMASequencer::printConfig(std::ostream & out)
1806285Snate@binkert.org{
1816285Snate@binkert.org}
1826876Ssteve.reinhardt@amd.com
1836876Ssteve.reinhardt@amd.comDMASequencer *
1846876Ssteve.reinhardt@amd.comDMASequencerParams::create()
1856876Ssteve.reinhardt@amd.com{
1866876Ssteve.reinhardt@amd.com    return new DMASequencer(this);
1876876Ssteve.reinhardt@amd.com}
188