packet.cc revision 4626
12568SN/A/*
22568SN/A * Copyright (c) 2006 The Regents of The University of Michigan
32568SN/A * All rights reserved.
42568SN/A *
52568SN/A * Redistribution and use in source and binary forms, with or without
62568SN/A * modification, are permitted provided that the following conditions are
72568SN/A * met: redistributions of source code must retain the above copyright
82568SN/A * notice, this list of conditions and the following disclaimer;
92568SN/A * redistributions in binary form must reproduce the above copyright
102568SN/A * notice, this list of conditions and the following disclaimer in the
112568SN/A * documentation and/or other materials provided with the distribution;
122568SN/A * neither the name of the copyright holders nor the names of its
132568SN/A * contributors may be used to endorse or promote products derived from
142568SN/A * this software without specific prior written permission.
152568SN/A *
162568SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172568SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182568SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192568SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202568SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212568SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222568SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232568SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242568SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252568SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262568SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Ali Saidi
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302568SN/A */
312568SN/A
322568SN/A/**
332568SN/A * @file
342568SN/A * Definition of the Packet Class, a packet is a transaction occuring
352568SN/A * between a single level of the memory heirarchy (ie L1->L2).
362568SN/A */
373260Ssaidi@eecs.umich.edu
383260Ssaidi@eecs.umich.edu#include <iostream>
393918Ssaidi@eecs.umich.edu#include <cstring>
402590SN/A#include "base/misc.hh"
413348Sbinkertn@umich.edu#include "base/trace.hh"
422568SN/A#include "mem/packet.hh"
432568SN/A
444022Sstever@eecs.umich.edu// The one downside to bitsets is that static initializers can get ugly.
454022Sstever@eecs.umich.edu#define SET1(a1)                     (1 << (a1))
464022Sstever@eecs.umich.edu#define SET2(a1, a2)                 (SET1(a1) | SET1(a2))
474022Sstever@eecs.umich.edu#define SET3(a1, a2, a3)             (SET2(a1, a2) | SET1(a3))
484022Sstever@eecs.umich.edu#define SET4(a1, a2, a3, a4)         (SET3(a1, a2, a3) | SET1(a4))
494022Sstever@eecs.umich.edu#define SET5(a1, a2, a3, a4, a5)     (SET4(a1, a2, a3, a4) | SET1(a5))
504022Sstever@eecs.umich.edu#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
512641Sstever@eecs.umich.edu
524022Sstever@eecs.umich.educonst MemCmd::CommandInfo
534022Sstever@eecs.umich.eduMemCmd::commandInfo[] =
542641Sstever@eecs.umich.edu{
554022Sstever@eecs.umich.edu    /* InvalidCmd */
564022Sstever@eecs.umich.edu    { 0, InvalidCmd, "InvalidCmd" },
574022Sstever@eecs.umich.edu    /* ReadReq */
584022Sstever@eecs.umich.edu    { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
594473Sstever@eecs.umich.edu    /* ReadResp */
604473Sstever@eecs.umich.edu    { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
614022Sstever@eecs.umich.edu    /* WriteReq */
624626Sstever@eecs.umich.edu    { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
634022Sstever@eecs.umich.edu            WriteResp, "WriteReq" },
644022Sstever@eecs.umich.edu    /* WriteResp */
654626Sstever@eecs.umich.edu    { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
664022Sstever@eecs.umich.edu    /* Writeback */
674626Sstever@eecs.umich.edu    { SET5(IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
684473Sstever@eecs.umich.edu            WritebackAck, "Writeback" },
694473Sstever@eecs.umich.edu    /* WritebackAck */
704626Sstever@eecs.umich.edu    { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WritebackAck" },
714022Sstever@eecs.umich.edu    /* SoftPFReq */
724022Sstever@eecs.umich.edu    { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
734022Sstever@eecs.umich.edu            SoftPFResp, "SoftPFReq" },
744022Sstever@eecs.umich.edu    /* HardPFReq */
754022Sstever@eecs.umich.edu    { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
764022Sstever@eecs.umich.edu            HardPFResp, "HardPFReq" },
774022Sstever@eecs.umich.edu    /* SoftPFResp */
784022Sstever@eecs.umich.edu    { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
794022Sstever@eecs.umich.edu            InvalidCmd, "SoftPFResp" },
804022Sstever@eecs.umich.edu    /* HardPFResp */
814022Sstever@eecs.umich.edu    { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
824022Sstever@eecs.umich.edu            InvalidCmd, "HardPFResp" },
834022Sstever@eecs.umich.edu    /* WriteInvalidateReq */
844626Sstever@eecs.umich.edu    { SET6(IsWrite, NeedsExclusive, IsInvalidate,
854626Sstever@eecs.umich.edu           IsRequest, HasData, NeedsResponse),
864022Sstever@eecs.umich.edu            WriteInvalidateResp, "WriteInvalidateReq" },
874022Sstever@eecs.umich.edu    /* WriteInvalidateResp */
884626Sstever@eecs.umich.edu    { SET4(IsWrite, NeedsExclusive, IsInvalidate, IsResponse),
894022Sstever@eecs.umich.edu            InvalidCmd, "WriteInvalidateResp" },
904022Sstever@eecs.umich.edu    /* UpgradeReq */
914022Sstever@eecs.umich.edu    { SET3(IsInvalidate, IsRequest, IsUpgrade), InvalidCmd, "UpgradeReq" },
924022Sstever@eecs.umich.edu    /* ReadExReq */
934626Sstever@eecs.umich.edu    { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
944022Sstever@eecs.umich.edu            ReadExResp, "ReadExReq" },
954022Sstever@eecs.umich.edu    /* ReadExResp */
964626Sstever@eecs.umich.edu    { SET5(IsRead, NeedsExclusive, IsInvalidate, IsResponse, HasData),
974040Ssaidi@eecs.umich.edu            InvalidCmd, "ReadExResp" },
984626Sstever@eecs.umich.edu    /* LoadLockedReq */
994626Sstever@eecs.umich.edu    { SET4(IsRead, IsLocked, IsRequest, NeedsResponse),
1004626Sstever@eecs.umich.edu            ReadResp, "LoadLockedReq" },
1014626Sstever@eecs.umich.edu    /* LoadLockedResp */
1024626Sstever@eecs.umich.edu    { SET4(IsRead, IsLocked, IsResponse, HasData),
1034626Sstever@eecs.umich.edu            InvalidCmd, "LoadLockedResp" },
1044626Sstever@eecs.umich.edu    /* StoreCondReq */
1054626Sstever@eecs.umich.edu    { SET6(IsWrite, NeedsExclusive, IsLocked,
1064626Sstever@eecs.umich.edu           IsRequest, NeedsResponse, HasData),
1074626Sstever@eecs.umich.edu            StoreCondResp, "StoreCondReq" },
1084626Sstever@eecs.umich.edu    /* StoreCondResp */
1094626Sstever@eecs.umich.edu    { SET4(IsWrite, NeedsExclusive, IsLocked, IsResponse),
1104626Sstever@eecs.umich.edu            InvalidCmd, "StoreCondResp" },
1114040Ssaidi@eecs.umich.edu    /* SwapReq -- for Swap ldstub type operations */
1124626Sstever@eecs.umich.edu    { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
1134040Ssaidi@eecs.umich.edu        SwapResp, "SwapReq" },
1144040Ssaidi@eecs.umich.edu    /* SwapResp -- for Swap ldstub type operations */
1154626Sstever@eecs.umich.edu    { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
1164040Ssaidi@eecs.umich.edu        InvalidCmd, "SwapResp" }
1174022Sstever@eecs.umich.edu};
1182592SN/A
1192811Srdreslin@umich.edu
1202592SN/A/** delete the data pointed to in the data pointer. Ok to call to matter how
1212592SN/A * data was allocted. */
1222592SN/Avoid
1232641Sstever@eecs.umich.eduPacket::deleteData()
1242641Sstever@eecs.umich.edu{
1252592SN/A    assert(staticData || dynamicData);
1262592SN/A    if (staticData)
1272592SN/A        return;
1282592SN/A
1292592SN/A    if (arrayData)
1302592SN/A        delete [] data;
1312592SN/A    else
1322592SN/A        delete data;
1332592SN/A}
1342592SN/A
1352592SN/A/** If there isn't data in the packet, allocate some. */
1362592SN/Avoid
1372641Sstever@eecs.umich.eduPacket::allocate()
1382641Sstever@eecs.umich.edu{
1392592SN/A    if (data)
1402592SN/A        return;
1412592SN/A    assert(!staticData);
1422592SN/A    dynamicData = true;
1432592SN/A    arrayData = true;
1442641Sstever@eecs.umich.edu    data = new uint8_t[getSize()];
1452592SN/A}
1462592SN/A
1472592SN/A/** Do the packet modify the same addresses. */
1482592SN/Abool
1493349Sbinkertn@umich.eduPacket::intersect(PacketPtr p)
1502641Sstever@eecs.umich.edu{
1512641Sstever@eecs.umich.edu    Addr s1 = getAddr();
1523242Sgblack@eecs.umich.edu    Addr e1 = getAddr() + getSize() - 1;
1532641Sstever@eecs.umich.edu    Addr s2 = p->getAddr();
1543242Sgblack@eecs.umich.edu    Addr e2 = p->getAddr() + p->getSize() - 1;
1552592SN/A
1563242Sgblack@eecs.umich.edu    return !(s1 > e2 || e1 < s2);
1572592SN/A}
1582592SN/A
1592641Sstever@eecs.umich.edubool
1603607Srdreslin@umich.edufixDelayedResponsePacket(PacketPtr func, PacketPtr timing)
1613607Srdreslin@umich.edu{
1623607Srdreslin@umich.edu    bool result;
1633607Srdreslin@umich.edu
1643607Srdreslin@umich.edu    if (timing->isRead() || timing->isWrite()) {
1654022Sstever@eecs.umich.edu        // Ugly hack to deal with the fact that we queue the requests
1664022Sstever@eecs.umich.edu        // and don't convert them to responses until we issue them on
1674022Sstever@eecs.umich.edu        // the bus.  I tried to avoid this by converting packets to
1684022Sstever@eecs.umich.edu        // responses right away, but this breaks during snoops where a
1694022Sstever@eecs.umich.edu        // responder may do the conversion before other caches have
1704022Sstever@eecs.umich.edu        // done the snoop.  Would work if we copied the packet instead
1714022Sstever@eecs.umich.edu        // of just hanging on to a pointer.
1724022Sstever@eecs.umich.edu        MemCmd oldCmd = timing->cmd;
1734022Sstever@eecs.umich.edu        timing->cmd = timing->cmd.responseCommand();
1743607Srdreslin@umich.edu        result = fixPacket(func, timing);
1754022Sstever@eecs.umich.edu        timing->cmd = oldCmd;
1763607Srdreslin@umich.edu    }
1773607Srdreslin@umich.edu    else {
1783607Srdreslin@umich.edu        //Don't toggle if it isn't a read/write response
1793607Srdreslin@umich.edu        result = fixPacket(func, timing);
1803607Srdreslin@umich.edu    }
1813607Srdreslin@umich.edu
1823607Srdreslin@umich.edu    return result;
1833607Srdreslin@umich.edu}
1843607Srdreslin@umich.edu
1853607Srdreslin@umich.edubool
1864626Sstever@eecs.umich.eduPacket::checkFunctional(Addr addr, int size, uint8_t *data)
1872641Sstever@eecs.umich.edu{
1884626Sstever@eecs.umich.edu    Addr func_start = getAddr();
1894626Sstever@eecs.umich.edu    Addr func_end   = getAddr() + getSize() - 1;
1904626Sstever@eecs.umich.edu    Addr val_start  = addr;
1914626Sstever@eecs.umich.edu    Addr val_end    = val_start + size - 1;
1923260Ssaidi@eecs.umich.edu
1934626Sstever@eecs.umich.edu    if (func_start > val_end || val_start > func_end) {
1944626Sstever@eecs.umich.edu        // no intersection
1954626Sstever@eecs.umich.edu        return false;
1964626Sstever@eecs.umich.edu    }
1973260Ssaidi@eecs.umich.edu
1984626Sstever@eecs.umich.edu    // offset of functional request into supplied value (could be
1994626Sstever@eecs.umich.edu    // negative if partial overlap)
2004626Sstever@eecs.umich.edu    int offset = func_start - val_start;
2013260Ssaidi@eecs.umich.edu
2024626Sstever@eecs.umich.edu    if (isRead()) {
2034626Sstever@eecs.umich.edu        if (func_start >= val_start && func_end <= val_end) {
2044626Sstever@eecs.umich.edu            allocate();
2054626Sstever@eecs.umich.edu            std::memcpy(getPtr<uint8_t>(), data + offset, getSize());
2064626Sstever@eecs.umich.edu            result = Packet::Success;
2074626Sstever@eecs.umich.edu            return true;
2083260Ssaidi@eecs.umich.edu        } else {
2094489Sstever@eecs.umich.edu            // In this case the timing packet only partially satisfies
2104489Sstever@eecs.umich.edu            // the request, so we would need more information to make
2114489Sstever@eecs.umich.edu            // this work.  Like bytes valid in the packet or
2124489Sstever@eecs.umich.edu            // something, so the request could continue and get this
2134489Sstever@eecs.umich.edu            // bit of possibly newer data along with the older data
2144489Sstever@eecs.umich.edu            // not written to yet.
2154626Sstever@eecs.umich.edu            panic("Memory value only partially satisfies the functional "
2164626Sstever@eecs.umich.edu                  "request. Now what?");
2173260Ssaidi@eecs.umich.edu        }
2184626Sstever@eecs.umich.edu    } else if (isWrite()) {
2194626Sstever@eecs.umich.edu        if (offset >= 0) {
2204626Sstever@eecs.umich.edu            std::memcpy(data + offset, getPtr<uint8_t>(),
2214626Sstever@eecs.umich.edu                        (std::min(func_end, val_end) - func_start) + 1);
2224626Sstever@eecs.umich.edu        } else { // val_start > func_start
2234626Sstever@eecs.umich.edu            std::memcpy(data, getPtr<uint8_t>() - offset,
2244626Sstever@eecs.umich.edu                        (std::min(func_end, val_end) - val_start) + 1);
2253260Ssaidi@eecs.umich.edu        }
2263260Ssaidi@eecs.umich.edu        // we always want to keep going with a write
2274626Sstever@eecs.umich.edu        return false;
2283260Ssaidi@eecs.umich.edu    } else
2294626Sstever@eecs.umich.edu        panic("Don't know how to handle command %s\n", cmdString());
2302641Sstever@eecs.umich.edu}
2313260Ssaidi@eecs.umich.edu
2323260Ssaidi@eecs.umich.edu
2333260Ssaidi@eecs.umich.edustd::ostream &
2343260Ssaidi@eecs.umich.eduoperator<<(std::ostream &o, const Packet &p)
2353260Ssaidi@eecs.umich.edu{
2363260Ssaidi@eecs.umich.edu
2373260Ssaidi@eecs.umich.edu    o << "[0x";
2383260Ssaidi@eecs.umich.edu    o.setf(std::ios_base::hex, std::ios_base::showbase);
2393260Ssaidi@eecs.umich.edu    o <<  p.getAddr();
2403260Ssaidi@eecs.umich.edu    o.unsetf(std::ios_base::hex| std::ios_base::showbase);
2413260Ssaidi@eecs.umich.edu    o <<  ":";
2423260Ssaidi@eecs.umich.edu    o.setf(std::ios_base::hex, std::ios_base::showbase);
2433260Ssaidi@eecs.umich.edu    o <<  p.getAddr() + p.getSize() - 1 << "] ";
2443260Ssaidi@eecs.umich.edu    o.unsetf(std::ios_base::hex| std::ios_base::showbase);
2453260Ssaidi@eecs.umich.edu
2463260Ssaidi@eecs.umich.edu    if (p.result == Packet::Success)
2473260Ssaidi@eecs.umich.edu        o << "Successful ";
2483260Ssaidi@eecs.umich.edu    if (p.result == Packet::BadAddress)
2493260Ssaidi@eecs.umich.edu        o << "BadAddress ";
2503260Ssaidi@eecs.umich.edu    if (p.result == Packet::Nacked)
2513260Ssaidi@eecs.umich.edu        o << "Nacked ";
2523260Ssaidi@eecs.umich.edu    if (p.result == Packet::Unknown)
2533260Ssaidi@eecs.umich.edu        o << "Inflight ";
2543260Ssaidi@eecs.umich.edu
2553260Ssaidi@eecs.umich.edu    if (p.isRead())
2563260Ssaidi@eecs.umich.edu        o << "Read ";
2573260Ssaidi@eecs.umich.edu    if (p.isWrite())
2584040Ssaidi@eecs.umich.edu        o << "Write ";
2593260Ssaidi@eecs.umich.edu    if (p.isInvalidate())
2604040Ssaidi@eecs.umich.edu        o << "Invalidate ";
2613260Ssaidi@eecs.umich.edu    if (p.isRequest())
2623260Ssaidi@eecs.umich.edu        o << "Request ";
2633260Ssaidi@eecs.umich.edu    if (p.isResponse())
2643260Ssaidi@eecs.umich.edu        o << "Response ";
2653260Ssaidi@eecs.umich.edu    if (p.hasData())
2663260Ssaidi@eecs.umich.edu        o << "w/Data ";
2673260Ssaidi@eecs.umich.edu
2683260Ssaidi@eecs.umich.edu    o << std::endl;
2693260Ssaidi@eecs.umich.edu    return o;
2703260Ssaidi@eecs.umich.edu}
2713260Ssaidi@eecs.umich.edu
272