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
2911793Sbrandon.potter@amd.com#include "mem/ruby/system/DMASequencer.hh"
3011793Sbrandon.potter@amd.com
3110472Sandreas.hansson@arm.com#include <memory>
3210472Sandreas.hansson@arm.com
338232Snate@binkert.org#include "debug/RubyDma.hh"
349104Shestness@cs.utexas.edu#include "debug/RubyStats.hh"
3514184Sgabeblack@google.com#include "mem/ruby/protocol/SequencerMsg.hh"
3614184Sgabeblack@google.com#include "mem/ruby/protocol/SequencerRequestType.hh"
3711108Sdavid.hashe@amd.com#include "mem/ruby/system/RubySystem.hh"
386285Snate@binkert.org
3911702Smichael.lebeane@amd.comDMARequest::DMARequest(uint64_t start_paddr, int len, bool write,
4011702Smichael.lebeane@amd.com                       int bytes_completed, int bytes_issued, uint8_t *data,
4111702Smichael.lebeane@amd.com                       PacketPtr pkt)
4211702Smichael.lebeane@amd.com    : start_paddr(start_paddr), len(len), write(write),
4311702Smichael.lebeane@amd.com      bytes_completed(bytes_completed), bytes_issued(bytes_issued), data(data),
4411702Smichael.lebeane@amd.com      pkt(pkt)
4511702Smichael.lebeane@amd.com{
4611702Smichael.lebeane@amd.com}
4711702Smichael.lebeane@amd.com
486876Ssteve.reinhardt@amd.comDMASequencer::DMASequencer(const Params *p)
4911702Smichael.lebeane@amd.com    : RubyPort(p), m_outstanding_count(0),
5011702Smichael.lebeane@amd.com      m_max_outstanding_requests(p->max_outstanding_requests)
516285Snate@binkert.org{
526285Snate@binkert.org}
536285Snate@binkert.org
547039Snate@binkert.orgvoid
557039Snate@binkert.orgDMASequencer::init()
566285Snate@binkert.org{
5711339SMichael.Lebeane@amd.com    RubyPort::init();
5811443Sandreas.hansson@arm.com    m_data_block_mask = mask(RubySystem::getBlockSizeBits());
5911346Santhony.gutierrez@amd.com
6011346Santhony.gutierrez@amd.com    for (const auto &s_port : slave_ports)
6111346Santhony.gutierrez@amd.com        s_port->sendRangeChange();
6210518Snilay@cs.wisc.edu}
6310518Snilay@cs.wisc.edu
647039Snate@binkert.orgRequestStatus
658615Snilay@cs.wisc.eduDMASequencer::makeRequest(PacketPtr pkt)
666285Snate@binkert.org{
6711702Smichael.lebeane@amd.com    if (m_outstanding_count == m_max_outstanding_requests) {
687544SBrad.Beckmann@amd.com        return RequestStatus_BufferFull;
697544SBrad.Beckmann@amd.com    }
707544SBrad.Beckmann@amd.com
7111025Snilay@cs.wisc.edu    Addr paddr = pkt->getAddr();
7210562Sandreas.hansson@arm.com    uint8_t* data =  pkt->getPtr<uint8_t>();
738615Snilay@cs.wisc.edu    int len = pkt->getSize();
748615Snilay@cs.wisc.edu    bool write = pkt->isWrite();
756285Snate@binkert.org
7611702Smichael.lebeane@amd.com    assert(m_outstanding_count < m_max_outstanding_requests);
7711702Smichael.lebeane@amd.com    Addr line_addr = makeLineAddress(paddr);
7811702Smichael.lebeane@amd.com    auto emplace_pair =
7911702Smichael.lebeane@amd.com        m_RequestTable.emplace(std::piecewise_construct,
8011702Smichael.lebeane@amd.com                               std::forward_as_tuple(line_addr),
8111702Smichael.lebeane@amd.com                               std::forward_as_tuple(paddr, len, write, 0,
8211702Smichael.lebeane@amd.com                                                     0, data, pkt));
8311702Smichael.lebeane@amd.com    DMARequest& active_request = emplace_pair.first->second;
846285Snate@binkert.org
8511702Smichael.lebeane@amd.com    // This is pretty conservative.  A regular Sequencer with a  more beefy
8611702Smichael.lebeane@amd.com    // request table that can track multiple requests for a cache line should
8711702Smichael.lebeane@amd.com    // be used if a more aggressive policy is needed.
8811702Smichael.lebeane@amd.com    if (!emplace_pair.second) {
8911702Smichael.lebeane@amd.com            DPRINTF(RubyDma, "DMA aliased: addr %p, len %d\n", line_addr, len);
9011702Smichael.lebeane@amd.com            return RequestStatus_Aliased;
9111702Smichael.lebeane@amd.com    }
9211702Smichael.lebeane@amd.com
9311702Smichael.lebeane@amd.com    DPRINTF(RubyDma, "DMA req created: addr %p, len %d\n", line_addr, len);
946285Snate@binkert.org
9510472Sandreas.hansson@arm.com    std::shared_ptr<SequencerMsg> msg =
9610472Sandreas.hansson@arm.com        std::make_shared<SequencerMsg>(clockEdge());
9711025Snilay@cs.wisc.edu    msg->getPhysicalAddress() = paddr;
9811702Smichael.lebeane@amd.com    msg->getLineAddress() = line_addr;
997453Snate@binkert.org    msg->getType() = write ? SequencerRequestType_ST : SequencerRequestType_LD;
1007039Snate@binkert.org    int offset = paddr & m_data_block_mask;
1016888SBrad.Beckmann@amd.com
1027453Snate@binkert.org    msg->getLen() = (offset + len) <= RubySystem::getBlockSizeBytes() ?
1037039Snate@binkert.org        len : RubySystem::getBlockSizeBytes() - offset;
1046888SBrad.Beckmann@amd.com
1057915SBrad.Beckmann@amd.com    if (write && (data != NULL)) {
1067915SBrad.Beckmann@amd.com        if (active_request.data != NULL) {
1077915SBrad.Beckmann@amd.com            msg->getDataBlk().setData(data, offset, msg->getLen());
1087915SBrad.Beckmann@amd.com        }
1097039Snate@binkert.org    }
1106888SBrad.Beckmann@amd.com
11111702Smichael.lebeane@amd.com    m_outstanding_count++;
11211702Smichael.lebeane@amd.com
1137039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
11411111Snilay@cs.wisc.edu    m_mandatory_q_ptr->enqueue(msg, clockEdge(), cyclesToTicks(Cycles(1)));
1157453Snate@binkert.org    active_request.bytes_issued += msg->getLen();
1166285Snate@binkert.org
1177039Snate@binkert.org    return RequestStatus_Issued;
1186285Snate@binkert.org}
1196285Snate@binkert.org
1207039Snate@binkert.orgvoid
12111702Smichael.lebeane@amd.comDMASequencer::issueNext(const Addr& address)
1226285Snate@binkert.org{
12311702Smichael.lebeane@amd.com    RequestTable::iterator i = m_RequestTable.find(address);
12411702Smichael.lebeane@amd.com    assert(i != m_RequestTable.end());
12511702Smichael.lebeane@amd.com
12611702Smichael.lebeane@amd.com    DMARequest &active_request = i->second;
12711702Smichael.lebeane@amd.com
12811702Smichael.lebeane@amd.com    assert(m_outstanding_count <= m_max_outstanding_requests);
1297039Snate@binkert.org    active_request.bytes_completed = active_request.bytes_issued;
1307039Snate@binkert.org    if (active_request.len == active_request.bytes_completed) {
13111702Smichael.lebeane@amd.com        DPRINTF(RubyDma, "DMA request completed: addr %p, size %d\n",
13211702Smichael.lebeane@amd.com                address, active_request.len);
13311702Smichael.lebeane@amd.com        m_outstanding_count--;
13411702Smichael.lebeane@amd.com        PacketPtr pkt = active_request.pkt;
13511702Smichael.lebeane@amd.com        m_RequestTable.erase(i);
13611702Smichael.lebeane@amd.com        ruby_hit_callback(pkt);
1377039Snate@binkert.org        return;
1387039Snate@binkert.org    }
1396285Snate@binkert.org
14010472Sandreas.hansson@arm.com    std::shared_ptr<SequencerMsg> msg =
14110472Sandreas.hansson@arm.com        std::make_shared<SequencerMsg>(clockEdge());
14211025Snilay@cs.wisc.edu    msg->getPhysicalAddress() = active_request.start_paddr +
14311025Snilay@cs.wisc.edu                                active_request.bytes_completed;
1446888SBrad.Beckmann@amd.com
14511025Snilay@cs.wisc.edu    assert((msg->getPhysicalAddress() & m_data_block_mask) == 0);
14611025Snilay@cs.wisc.edu    msg->getLineAddress() = makeLineAddress(msg->getPhysicalAddress());
1476888SBrad.Beckmann@amd.com
1487453Snate@binkert.org    msg->getType() = (active_request.write ? SequencerRequestType_ST :
1497039Snate@binkert.org                     SequencerRequestType_LD);
1506888SBrad.Beckmann@amd.com
1517453Snate@binkert.org    msg->getLen() =
1527039Snate@binkert.org        (active_request.len -
1537039Snate@binkert.org         active_request.bytes_completed < RubySystem::getBlockSizeBytes() ?
1547039Snate@binkert.org         active_request.len - active_request.bytes_completed :
1557039Snate@binkert.org         RubySystem::getBlockSizeBytes());
1566888SBrad.Beckmann@amd.com
1577039Snate@binkert.org    if (active_request.write) {
1587453Snate@binkert.org        msg->getDataBlk().
1597039Snate@binkert.org            setData(&active_request.data[active_request.bytes_completed],
1607453Snate@binkert.org                    0, msg->getLen());
1617039Snate@binkert.org    }
1626888SBrad.Beckmann@amd.com
1637039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
16411111Snilay@cs.wisc.edu    m_mandatory_q_ptr->enqueue(msg, clockEdge(), cyclesToTicks(Cycles(1)));
1657453Snate@binkert.org    active_request.bytes_issued += msg->getLen();
16610917Sbrandon.potter@amd.com    DPRINTF(RubyDma,
1678160SBrad.Beckmann@amd.com            "DMA request bytes issued %d, bytes completed %d, total len %d\n",
1688160SBrad.Beckmann@amd.com            active_request.bytes_issued, active_request.bytes_completed,
1698160SBrad.Beckmann@amd.com            active_request.len);
1706285Snate@binkert.org}
1716285Snate@binkert.org
1727039Snate@binkert.orgvoid
17311702Smichael.lebeane@amd.comDMASequencer::dataCallback(const DataBlock & dblk, const Addr& address)
1746285Snate@binkert.org{
17511702Smichael.lebeane@amd.com
17611702Smichael.lebeane@amd.com    RequestTable::iterator i = m_RequestTable.find(address);
17711702Smichael.lebeane@amd.com    assert(i != m_RequestTable.end());
17811702Smichael.lebeane@amd.com
17911702Smichael.lebeane@amd.com    DMARequest &active_request = i->second;
1807039Snate@binkert.org    int len = active_request.bytes_issued - active_request.bytes_completed;
1817039Snate@binkert.org    int offset = 0;
1827039Snate@binkert.org    if (active_request.bytes_completed == 0)
1837039Snate@binkert.org        offset = active_request.start_paddr & m_data_block_mask;
18410231Ssteve.reinhardt@amd.com    assert(!active_request.write);
1857915SBrad.Beckmann@amd.com    if (active_request.data != NULL) {
1867915SBrad.Beckmann@amd.com        memcpy(&active_request.data[active_request.bytes_completed],
1877915SBrad.Beckmann@amd.com               dblk.getData(offset, len), len);
1887915SBrad.Beckmann@amd.com    }
18911702Smichael.lebeane@amd.com    issueNext(address);
1906285Snate@binkert.org}
1916285Snate@binkert.org
1927039Snate@binkert.orgvoid
19311702Smichael.lebeane@amd.comDMASequencer::ackCallback(const Addr& address)
1946285Snate@binkert.org{
19511778Santhony.gutierrez@amd.com    assert(m_RequestTable.find(address) != m_RequestTable.end());
19611702Smichael.lebeane@amd.com    issueNext(address);
1976285Snate@binkert.org}
1986285Snate@binkert.org
1997039Snate@binkert.orgvoid
20010518Snilay@cs.wisc.eduDMASequencer::recordRequestType(DMASequencerRequestType requestType)
20110518Snilay@cs.wisc.edu{
2029104Shestness@cs.utexas.edu    DPRINTF(RubyStats, "Recorded statistic: %s\n",
2039104Shestness@cs.utexas.edu            DMASequencerRequestType_to_string(requestType));
2049104Shestness@cs.utexas.edu}
2059104Shestness@cs.utexas.edu
2066876Ssteve.reinhardt@amd.comDMASequencer *
2076876Ssteve.reinhardt@amd.comDMASequencerParams::create()
2086876Ssteve.reinhardt@amd.com{
2096876Ssteve.reinhardt@amd.com    return new DMASequencer(this);
2106876Ssteve.reinhardt@amd.com}
211