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