request.hh revision 8833:2870638642bd
1/*
2 * Copyright (c) 2002-2005 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 * Authors: Ron Dreslinski
29 *          Steve Reinhardt
30 *          Ali Saidi
31 */
32
33/**
34 * @file
35 * Declaration of a request, the overall memory request consisting of
36 the parts of the request that are persistent throughout the transaction.
37 */
38
39#ifndef __MEM_REQUEST_HH__
40#define __MEM_REQUEST_HH__
41
42#include <cassert>
43#include <climits>
44
45#include "base/fast_alloc.hh"
46#include "base/flags.hh"
47#include "base/misc.hh"
48#include "base/types.hh"
49#include "sim/core.hh"
50
51class Request;
52
53typedef Request* RequestPtr;
54typedef uint16_t MasterID;
55
56class Request : public FastAlloc
57{
58  public:
59    typedef uint32_t FlagsType;
60    typedef ::Flags<FlagsType> Flags;
61
62    /** ASI information for this request if it exists. */
63    static const FlagsType ASI_BITS                    = 0x000000FF;
64    /** The request was an instruction fetch. */
65    static const FlagsType INST_FETCH                  = 0x00000100;
66    /** The virtual address is also the physical address. */
67    static const FlagsType PHYSICAL                    = 0x00000200;
68    /** The request is an ALPHA VPTE pal access (hw_ld). */
69    static const FlagsType VPTE                        = 0x00000400;
70    /** Use the alternate mode bits in ALPHA. */
71    static const FlagsType ALTMODE                     = 0x00000800;
72    /** The request is to an uncacheable address. */
73    static const FlagsType UNCACHEABLE                 = 0x00001000;
74    /** This request is to a memory mapped register. */
75    static const FlagsType MMAPPED_IPR                  = 0x00002000;
76    /** This request is a clear exclusive. */
77    static const FlagsType CLEAR_LL                    = 0x00004000;
78
79    /** The request should not cause a memory access. */
80    static const FlagsType NO_ACCESS                   = 0x00080000;
81    /** This request will lock or unlock the accessed memory. When used with
82     * a load, the access locks the particular chunk of memory. When used
83     * with a store, it unlocks. The rule is that locked accesses have to be
84     * made up of a locked load, some operation on the data, and then a locked
85     * store.
86     */
87    static const FlagsType LOCKED                      = 0x00100000;
88    /** The request is a Load locked/store conditional. */
89    static const FlagsType LLSC                        = 0x00200000;
90    /** This request is for a memory swap. */
91    static const FlagsType MEM_SWAP                    = 0x00400000;
92    static const FlagsType MEM_SWAP_COND               = 0x00800000;
93
94    /** The request is a prefetch. */
95    static const FlagsType PREFETCH                    = 0x01000000;
96    /** The request should be prefetched into the exclusive state. */
97    static const FlagsType PF_EXCLUSIVE                = 0x02000000;
98    /** The request should be marked as LRU. */
99    static const FlagsType EVICT_NEXT                  = 0x04000000;
100
101    /** These flags are *not* cleared when a Request object is reused
102       (assigned a new address). */
103    static const FlagsType STICKY_FLAGS = INST_FETCH;
104
105    /** Request Ids that are statically allocated
106     * @{*/
107    /** This request id is used for writeback requests by the caches */
108    static const MasterID wbMasterId = 0;
109    /** This request id is used for functional requests that don't come from a
110     * particular device
111     */
112    static const MasterID funcMasterId = 1;
113    /** This request id is used for message signaled interrupts */
114    static const MasterID intMasterId = 2;
115    /** Invalid request id for assertion checking only. It is invalid behavior
116     * to ever send this id as part of a request.
117     * @todo C++1x replace with numeric_limits when constexpr is added  */
118    static const MasterID invldMasterId = USHRT_MAX;
119    /** @} */
120
121  private:
122    typedef uint8_t PrivateFlagsType;
123    typedef ::Flags<PrivateFlagsType> PrivateFlags;
124
125    /** Whether or not the size is valid. */
126    static const PrivateFlagsType VALID_SIZE           = 0x00000001;
127    /** Whether or not paddr is valid (has been written yet). */
128    static const PrivateFlagsType VALID_PADDR          = 0x00000002;
129    /** Whether or not the vaddr & asid are valid. */
130    static const PrivateFlagsType VALID_VADDR          = 0x00000004;
131    /** Whether or not the pc is valid. */
132    static const PrivateFlagsType VALID_PC             = 0x00000010;
133    /** Whether or not the context ID is valid. */
134    static const PrivateFlagsType VALID_CONTEXT_ID     = 0x00000020;
135    static const PrivateFlagsType VALID_THREAD_ID      = 0x00000040;
136    /** Whether or not the sc result is valid. */
137    static const PrivateFlagsType VALID_EXTRA_DATA     = 0x00000080;
138
139    /** These flags are *not* cleared when a Request object is reused
140       (assigned a new address). */
141    static const PrivateFlagsType STICKY_PRIVATE_FLAGS =
142        VALID_CONTEXT_ID | VALID_THREAD_ID;
143
144  private:
145    /**
146     * The physical address of the request. Valid only if validPaddr
147     * is set.
148     */
149    Addr _paddr;
150
151    /**
152     * The size of the request. This field must be set when vaddr or
153     * paddr is written via setVirt() or setPhys(), so it is always
154     * valid as long as one of the address fields is valid.
155     */
156    int _size;
157
158    /** The requestor ID which is unique in the system for all ports
159     * that are capable of issuing a transaction
160     */
161    MasterID _masterId;
162
163    /** Flag structure for the request. */
164    Flags _flags;
165
166    /** Private flags for field validity checking. */
167    PrivateFlags privateFlags;
168
169    /**
170     * The time this request was started. Used to calculate
171     * latencies. This field is set to curTick() any time paddr or vaddr
172     * is written.
173     */
174    Tick _time;
175
176    /** The address space ID. */
177    int _asid;
178
179    /** The virtual address of the request. */
180    Addr _vaddr;
181
182    /**
183     * Extra data for the request, such as the return value of
184     * store conditional or the compare value for a CAS. */
185    uint64_t _extraData;
186
187    /** The context ID (for statistics, typically). */
188    int _contextId;
189    /** The thread ID (id within this CPU) */
190    int _threadId;
191
192    /** program counter of initiating access; for tracing/debugging */
193    Addr _pc;
194
195  public:
196    /** Minimal constructor.  No fields are initialized.
197     *  (Note that _flags and privateFlags are cleared by Flags
198     *  default constructor.)
199     */
200    Request()
201    {}
202
203    /**
204     * Constructor for physical (e.g. device) requests.  Initializes
205     * just physical address, size, flags, and timestamp (to curTick()).
206     * These fields are adequate to perform a request.
207     */
208    Request(Addr paddr, int size, Flags flags, MasterID mid)
209    {
210        setPhys(paddr, size, flags, mid);
211    }
212
213    Request(Addr paddr, int size, Flags flags, MasterID mid, Tick time)
214    {
215        setPhys(paddr, size, flags, mid, time);
216    }
217
218    Request(Addr paddr, int size, Flags flags, MasterID mid, Tick time, Addr pc)
219    {
220        setPhys(paddr, size, flags, mid, time);
221        privateFlags.set(VALID_PC);
222        _pc = pc;
223    }
224
225    Request(int asid, Addr vaddr, int size, Flags flags, MasterID mid, Addr pc,
226            int cid, ThreadID tid)
227    {
228        setVirt(asid, vaddr, size, flags, mid, pc);
229        setThreadContext(cid, tid);
230    }
231
232    ~Request() {}  // for FastAlloc
233
234    /**
235     * Set up CPU and thread numbers.
236     */
237    void
238    setThreadContext(int context_id, ThreadID tid)
239    {
240        _contextId = context_id;
241        _threadId = tid;
242        privateFlags.set(VALID_CONTEXT_ID|VALID_THREAD_ID);
243    }
244
245    /**
246     * Set up a physical (e.g. device) request in a previously
247     * allocated Request object.
248     */
249    void
250    setPhys(Addr paddr, int size, Flags flags, MasterID mid, Tick time)
251    {
252        assert(size >= 0);
253        _paddr = paddr;
254        _size = size;
255        _time = time;
256        _masterId = mid;
257        _flags.clear(~STICKY_FLAGS);
258        _flags.set(flags);
259        privateFlags.clear(~STICKY_PRIVATE_FLAGS);
260        privateFlags.set(VALID_PADDR|VALID_SIZE);
261    }
262
263    void
264    setPhys(Addr paddr, int size, Flags flags, MasterID mid)
265    {
266        setPhys(paddr, size, flags, mid, curTick());
267    }
268
269    /**
270     * Set up a virtual (e.g., CPU) request in a previously
271     * allocated Request object.
272     */
273    void
274    setVirt(int asid, Addr vaddr, int size, Flags flags, MasterID mid, Addr pc)
275    {
276        assert(size >= 0);
277        _asid = asid;
278        _vaddr = vaddr;
279        _size = size;
280        _masterId = mid;
281        _pc = pc;
282        _time = curTick();
283
284        _flags.clear(~STICKY_FLAGS);
285        _flags.set(flags);
286        privateFlags.clear(~STICKY_PRIVATE_FLAGS);
287        privateFlags.set(VALID_VADDR|VALID_SIZE|VALID_PC);
288    }
289
290    /**
291     * Set just the physical address.  This should only be used to
292     * record the result of a translation, and thus the vaddr must be
293     * valid before this method is called.  Otherwise, use setPhys()
294     * to guarantee that the size and flags are also set.
295     */
296    void
297    setPaddr(Addr paddr)
298    {
299        assert(privateFlags.isSet(VALID_VADDR));
300        _paddr = paddr;
301        privateFlags.set(VALID_PADDR);
302    }
303
304    /**
305     * Generate two requests as if this request had been split into two
306     * pieces. The original request can't have been translated already.
307     */
308    void splitOnVaddr(Addr split_addr, RequestPtr &req1, RequestPtr &req2)
309    {
310        assert(privateFlags.isSet(VALID_VADDR));
311        assert(privateFlags.noneSet(VALID_PADDR));
312        assert(split_addr > _vaddr && split_addr < _vaddr + _size);
313        req1 = new Request;
314        *req1 = *this;
315        req2 = new Request;
316        *req2 = *this;
317        req1->_size = split_addr - _vaddr;
318        req2->_vaddr = split_addr;
319        req2->_size = _size - req1->_size;
320    }
321
322    /**
323     * Accessor for paddr.
324     */
325    bool
326    hasPaddr()
327    {
328        return privateFlags.isSet(VALID_PADDR);
329    }
330
331    Addr
332    getPaddr()
333    {
334        assert(privateFlags.isSet(VALID_PADDR));
335        return _paddr;
336    }
337
338    /**
339     *  Accessor for size.
340     */
341    bool
342    hasSize()
343    {
344        return privateFlags.isSet(VALID_SIZE);
345    }
346
347    int
348    getSize()
349    {
350        assert(privateFlags.isSet(VALID_SIZE));
351        return _size;
352    }
353
354    /** Accessor for time. */
355    Tick
356    time() const
357    {
358        assert(privateFlags.isSet(VALID_PADDR|VALID_VADDR));
359        return _time;
360    }
361
362    void
363    time(Tick time)
364    {
365        assert(privateFlags.isSet(VALID_PADDR|VALID_VADDR));
366        _time = time;
367    }
368
369    /** Accessor for flags. */
370    Flags
371    getFlags()
372    {
373        assert(privateFlags.isSet(VALID_PADDR|VALID_VADDR));
374        return _flags;
375    }
376
377    /** Note that unlike other accessors, this function sets *specific
378       flags* (ORs them in); it does not assign its argument to the
379       _flags field.  Thus this method should rightly be called
380       setFlags() and not just flags(). */
381    void
382    setFlags(Flags flags)
383    {
384        assert(privateFlags.isSet(VALID_PADDR|VALID_VADDR));
385        _flags.set(flags);
386    }
387
388    /** Accessor function for vaddr.*/
389    Addr
390    getVaddr()
391    {
392        assert(privateFlags.isSet(VALID_VADDR));
393        return _vaddr;
394    }
395
396    /** Accesssor for the requestor id. */
397    MasterID
398    masterId()
399    {
400        return _masterId;
401    }
402
403    /** Accessor function for asid.*/
404    int
405    getAsid()
406    {
407        assert(privateFlags.isSet(VALID_VADDR));
408        return _asid;
409    }
410
411    /** Accessor function for asid.*/
412    void
413    setAsid(int asid)
414    {
415        _asid = asid;
416    }
417
418    /** Accessor function for asi.*/
419    uint8_t
420    getAsi()
421    {
422        assert(privateFlags.isSet(VALID_VADDR));
423        return _flags & ASI_BITS;
424    }
425
426    /** Accessor function to check if sc result is valid. */
427    bool
428    extraDataValid()
429    {
430        return privateFlags.isSet(VALID_EXTRA_DATA);
431    }
432
433    /** Accessor function for store conditional return value.*/
434    uint64_t
435    getExtraData() const
436    {
437        assert(privateFlags.isSet(VALID_EXTRA_DATA));
438        return _extraData;
439    }
440
441    /** Accessor function for store conditional return value.*/
442    void
443    setExtraData(uint64_t extraData)
444    {
445        _extraData = extraData;
446        privateFlags.set(VALID_EXTRA_DATA);
447    }
448
449    bool
450    hasContextId() const
451    {
452        return privateFlags.isSet(VALID_CONTEXT_ID);
453    }
454
455    /** Accessor function for context ID.*/
456    int
457    contextId() const
458    {
459        assert(privateFlags.isSet(VALID_CONTEXT_ID));
460        return _contextId;
461    }
462
463    /** Accessor function for thread ID. */
464    int
465    threadId() const
466    {
467        assert(privateFlags.isSet(VALID_THREAD_ID));
468        return _threadId;
469    }
470
471    bool
472    hasPC() const
473    {
474        return privateFlags.isSet(VALID_PC);
475    }
476
477    /** Accessor function for pc.*/
478    Addr
479    getPC() const
480    {
481        assert(privateFlags.isSet(VALID_PC));
482        return _pc;
483    }
484
485    /** Accessor functions for flags.  Note that these are for testing
486       only; setting flags should be done via setFlags(). */
487    bool isUncacheable() const { return _flags.isSet(UNCACHEABLE); }
488    bool isInstFetch() const { return _flags.isSet(INST_FETCH); }
489    bool isPrefetch() const { return _flags.isSet(PREFETCH); }
490    bool isLLSC() const { return _flags.isSet(LLSC); }
491    bool isLocked() const { return _flags.isSet(LOCKED); }
492    bool isSwap() const { return _flags.isSet(MEM_SWAP|MEM_SWAP_COND); }
493    bool isCondSwap() const { return _flags.isSet(MEM_SWAP_COND); }
494    bool isMmappedIpr() const { return _flags.isSet(MMAPPED_IPR); }
495    bool isClearLL() const { return _flags.isSet(CLEAR_LL); }
496};
497
498#endif // __MEM_REQUEST_HH__
499