packet.hh revision 2568
1/*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @file
31 * Declaration of the Packet Class, a packet is a transaction occuring
32 * between a single level of the memory heirarchy (ie L1->L2).
33 */
34
35#ifndef __MEM_PACKET_HH__
36#define __MEM_PACKET_HH__
37
38#include "mem/request.hh"
39#include "arch/isa_traits.hh"
40#include "sim/root.hh"
41
42struct Packet;
43typedef Packet* PacketPtr;
44typedef uint8_t* PacketDataPtr;
45
46/** List of all commands associated with a packet. */
47enum Command
48{
49    Read,
50    Write
51};
52
53/** The result of a particular pakets request. */
54enum PacketResult
55{
56    Success,
57    BadAddress,
58    Unknown
59};
60
61class SenderState{};
62class Coherence{};
63
64/**
65 * A Packet is the structure to handle requests between two levels
66 * of the memory system.  The Request is a global object that trancends
67 * all of the memory heirarchy, but at each levels interface a packet
68 * is created to transfer data/requests.  For example, a request would
69 * be used to initiate a request to go to memory/IOdevices, as the request
70 * passes through the memory system several packets will be created.  One
71 * will be created to go between the L1 and L2 caches and another to go to
72 * the next level and so forth.
73 *
74 * Packets are assumed to be returned in the case of a single response.  If
75 * the transaction has no response, then the consumer will delete the packet.
76 */
77struct Packet
78{
79  private:
80   /** A pointer to the data being transfered.  It can be differnt sizes
81        at each level of the heirarchy so it belongs in the packet,
82        not request. This may or may not be populated when a responder recieves
83        the packet. If not populated it memory should be allocated.
84    */
85    PacketDataPtr data;
86
87    /** Is the data pointer set to a value that shouldn't be freed when the
88     * packet is destroyed? */
89    bool staticData;
90    /** The data pointer points to a value that should be freed when the packet
91     * is destroyed. */
92    bool dynamicData;
93    /** the data pointer points to an array (thus delete [] ) needs to be called
94     * on it rather than simply delete.*/
95    bool arrayData;
96
97
98  public:
99    /** The address of the request, could be virtual or physical (depending on
100        cache configurations). */
101    Addr addr;
102
103    /** Flag structure to hold flags for this particular packet */
104    uint64_t flags;
105
106    /** A pointer to the overall request. */
107    RequestPtr req;
108
109    /** A virtual base opaque structure used to hold
110        coherence status messages. */
111    Coherence *coherence;  // virtual base opaque,
112                           // assert(dynamic_cast<Foo>) etc.
113
114    /** A virtual base opaque structure used to hold the senders state. */
115    void *senderState; // virtual base opaque,
116                           // assert(dynamic_cast<Foo>) etc.
117
118     /** Indicates the size of the request. */
119    int size;
120
121    /** A index of the source of the transaction. */
122    short src;
123
124    /** A index to the destination of the transaction. */
125    short dest;
126
127    /** The command of the transaction. */
128    Command cmd;
129
130    /** The time this request was responded to. Used to calculate latencies. */
131    Tick time;
132
133    /** The result of the packet transaction. */
134    PacketResult result;
135
136    /** Accessor function that returns the source index of the packet. */
137    short getSrc() const { return src; }
138
139    /** Accessor function that returns the destination index of
140        the packet. */
141    short getDest() const { return dest; }
142
143    Packet()
144        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
145           time(curTick), result(Unknown)
146        {}
147
148    ~Packet()
149    { deleteData(); }
150
151
152    /** Minimally reset a packet so something like simple cpu can reuse it. */
153    void reset() {
154        result = Unknown;
155        if (dynamicData) {
156           deleteData();
157           dynamicData = false;
158           arrayData = false;
159           time = curTick;
160        }
161    }
162
163    /** Set the data pointer to the following value that should not be freed. */
164    template <typename T>
165    void dataStatic(T *p) {
166        assert(!dynamicData);
167        data = (PacketDataPtr)p;
168        staticData = true;
169    }
170
171    /** Set the data pointer to a value that should have delete [] called on it.
172     */
173    template <typename T>
174    void dataDynamicArray(T *p) {
175        assert(!staticData && !dynamicData);
176        data = (PacketDataPtr)p;
177        dynamicData = true;
178        arrayData = true;
179    }
180
181    /** set the data pointer to a value that should have delete called on it. */
182    template <typename T>
183    void dataDynamic(T *p) {
184        assert(!staticData && !dynamicData);
185        data = (PacketDataPtr)p;
186        dynamicData = true;
187        arrayData = false;
188    }
189
190    /** return the value of what is pointed to in the packet. */
191    template <typename T>
192    T get() {
193        assert(staticData || dynamicData);
194        assert(sizeof(T) <= size);
195        return *(T*)data;
196    }
197
198    /** get a pointer to the data ptr. */
199    template <typename T>
200    T* getPtr() {
201        assert(staticData || dynamicData);
202        return (T*)data;
203    }
204
205
206    /** set the value in the data pointer to v. */
207    template <typename T>
208    void set(T v) {
209        assert(sizeof(T) <= size);
210        *(T*)data = v;
211    }
212
213    /** delete the data pointed to in the data pointer. Ok to call to matter how
214     * data was allocted. */
215    void deleteData() {
216        assert(staticData || dynamicData);
217        if (staticData)
218            return;
219
220        if (arrayData)
221            delete [] data;
222        else
223            delete data;
224    }
225
226    /** If there isn't data in the packet, allocate some. */
227    void allocate() {
228        if (data)
229            return;
230        assert(!staticData);
231        dynamicData = true;
232        arrayData = true;
233        data = new uint8_t[size];
234    }
235
236    /** Do the packet modify the same addresses. */
237    bool intersect(Packet *p) {
238        Addr s1 = addr;
239        Addr e1 = addr + size;
240        Addr s2 = p->addr;
241        Addr e2 = p->addr + p->size;
242
243        if (s1 >= s2 && s1 < e2)
244            return true;
245        if (e1 >= s2 && e1 < e2)
246            return true;
247        return false;
248    }
249};
250
251bool fixPacket(Packet &func, Packet &timing);
252#endif //__MEM_PACKET_HH
253