addr_mapper.hh revision 9259
114039Sstacze01@arm.com/*
214039Sstacze01@arm.com * Copyright (c) 2012 ARM Limited
314039Sstacze01@arm.com * All rights reserved
414039Sstacze01@arm.com *
514039Sstacze01@arm.com * The license below extends only to copyright in the software and shall
614039Sstacze01@arm.com * not be construed as granting a license to any other intellectual
714039Sstacze01@arm.com * property including but not limited to intellectual property relating
814039Sstacze01@arm.com * to a hardware implementation of the functionality of the software
914039Sstacze01@arm.com * licensed hereunder.  You may use the software subject to the license
1014039Sstacze01@arm.com * terms below provided that you ensure that this notice is replicated
1114039Sstacze01@arm.com * unmodified and in its entirety in all distributions of the software,
1214039Sstacze01@arm.com * modified or unmodified, in source code or in binary form.
1314039Sstacze01@arm.com *
1414039Sstacze01@arm.com * Redistribution and use in source and binary forms, with or without
1514039Sstacze01@arm.com * modification, are permitted provided that the following conditions are
1614039Sstacze01@arm.com * met: redistributions of source code must retain the above copyright
1714039Sstacze01@arm.com * notice, this list of conditions and the following disclaimer;
1814039Sstacze01@arm.com * redistributions in binary form must reproduce the above copyright
1914039Sstacze01@arm.com * notice, this list of conditions and the following disclaimer in the
2014039Sstacze01@arm.com * documentation and/or other materials provided with the distribution;
2114039Sstacze01@arm.com * neither the name of the copyright holders nor the names of its
2214039Sstacze01@arm.com * contributors may be used to endorse or promote products derived from
2314039Sstacze01@arm.com * this software without specific prior written permission.
2414039Sstacze01@arm.com *
2514039Sstacze01@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2614039Sstacze01@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2714039Sstacze01@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2814039Sstacze01@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2914039Sstacze01@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3014039Sstacze01@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3114039Sstacze01@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3214039Sstacze01@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3314039Sstacze01@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3414039Sstacze01@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3514039Sstacze01@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3614039Sstacze01@arm.com *
3714039Sstacze01@arm.com * Authors: Andreas Hansson
3814039Sstacze01@arm.com */
3914039Sstacze01@arm.com
4014039Sstacze01@arm.com#ifndef __MEM_ADDR_MAPPER_HH__
4114039Sstacze01@arm.com#define __MEM_ADDR_MAPPER_HH__
4214039Sstacze01@arm.com
4314039Sstacze01@arm.com#include "mem/mem_object.hh"
4414039Sstacze01@arm.com#include "params/AddrMapper.hh"
4514039Sstacze01@arm.com#include "params/RangeAddrMapper.hh"
4614039Sstacze01@arm.com
4714039Sstacze01@arm.com/**
4814039Sstacze01@arm.com * An address mapper changes the packet addresses in going from the
4914039Sstacze01@arm.com * slave port side of the mapper to the master port side. When the
5014039Sstacze01@arm.com * slave port is queried for the address ranges, it also performs the
5114252Sgabeblack@google.com * necessary range updates. Note that snoop requests that travel from
5214039Sstacze01@arm.com * the master port (i.e. the memory side) to the slave port are
5314039Sstacze01@arm.com * currently not modified.
5414039Sstacze01@arm.com */
5514039Sstacze01@arm.com
5614039Sstacze01@arm.comclass AddrMapper : public MemObject
5714252Sgabeblack@google.com{
5814039Sstacze01@arm.com
5914064Sadrian.herrera@arm.com  public:
6014064Sadrian.herrera@arm.com
6114064Sadrian.herrera@arm.com    AddrMapper(const AddrMapperParams* params);
6214039Sstacze01@arm.com
6314039Sstacze01@arm.com    virtual ~AddrMapper() { }
6414039Sstacze01@arm.com
6514039Sstacze01@arm.com    virtual MasterPort& getMasterPort(const std::string& if_name,
6614039Sstacze01@arm.com                                      int idx = -1);
6714039Sstacze01@arm.com
6814039Sstacze01@arm.com    virtual SlavePort& getSlavePort(const std::string& if_name,
6914039Sstacze01@arm.com                                    int idx = -1);
7014039Sstacze01@arm.com
7114039Sstacze01@arm.com    virtual void init();
7214039Sstacze01@arm.com
7314039Sstacze01@arm.com  protected:
7414039Sstacze01@arm.com
7514039Sstacze01@arm.com    /**
7614039Sstacze01@arm.com     * This function does the actual remapping of one address to another.
7714039Sstacze01@arm.com     * It is pure virtual in this case to to allow any implementation
7814039Sstacze01@arm.com     * required.
7914039Sstacze01@arm.com     * @param addr the address to remap
8014039Sstacze01@arm.com     * @return the new address (can be unchanged)
8114039Sstacze01@arm.com     */
8214039Sstacze01@arm.com    virtual Addr remapAddr(Addr addr) const = 0;
8314039Sstacze01@arm.com
8414039Sstacze01@arm.com    class AddrMapperSenderState : public Packet::SenderState
8514039Sstacze01@arm.com    {
8614223Sgiacomo.travaglini@arm.com
8714039Sstacze01@arm.com      public:
8814039Sstacze01@arm.com
8914039Sstacze01@arm.com        /**
9014039Sstacze01@arm.com         * Construct a new sender state and remember the original one
9114039Sstacze01@arm.com         * so that we can implement a stack.
9214039Sstacze01@arm.com         *
9314039Sstacze01@arm.com         * @param _origSenderState Sender state to remember
9414039Sstacze01@arm.com         * @param _origAddr Address before remapping
9514039Sstacze01@arm.com         */
9614039Sstacze01@arm.com        AddrMapperSenderState(SenderState* _origSenderState,
9714039Sstacze01@arm.com                              Addr _origAddr)
9814039Sstacze01@arm.com            : origSenderState(_origSenderState), origAddr(_origAddr)
9914039Sstacze01@arm.com        { }
10014039Sstacze01@arm.com
10114039Sstacze01@arm.com        /** Destructor */
10214039Sstacze01@arm.com        ~AddrMapperSenderState() { }
10314039Sstacze01@arm.com
10414039Sstacze01@arm.com        /** Pointer to old sender state of packet */
10514039Sstacze01@arm.com        SenderState* origSenderState;
10614039Sstacze01@arm.com
10714039Sstacze01@arm.com        /** The original address the packet was destined for */
10814039Sstacze01@arm.com        Addr origAddr;
10914039Sstacze01@arm.com
11014039Sstacze01@arm.com    };
11114039Sstacze01@arm.com
11214039Sstacze01@arm.com    class MapperMasterPort : public MasterPort
11314039Sstacze01@arm.com    {
11414039Sstacze01@arm.com
11514039Sstacze01@arm.com      public:
11614039Sstacze01@arm.com
11714039Sstacze01@arm.com        MapperMasterPort(const std::string& _name, AddrMapper& _mapper)
11814039Sstacze01@arm.com            : MasterPort(_name, &_mapper), mapper(_mapper)
11914039Sstacze01@arm.com        { }
12014092Smatteo.andreozzi@arm.com
12114039Sstacze01@arm.com      protected:
12214039Sstacze01@arm.com
12314039Sstacze01@arm.com        void recvFunctionalSnoop(PacketPtr pkt)
12414039Sstacze01@arm.com        {
12514039Sstacze01@arm.com            mapper.recvFunctionalSnoop(pkt);
12614039Sstacze01@arm.com        }
12714039Sstacze01@arm.com
12814039Sstacze01@arm.com        Tick recvAtomicSnoop(PacketPtr pkt)
12914039Sstacze01@arm.com        {
13014039Sstacze01@arm.com            return mapper.recvAtomicSnoop(pkt);
13114064Sadrian.herrera@arm.com        }
13214064Sadrian.herrera@arm.com
13314064Sadrian.herrera@arm.com        bool recvTimingResp(PacketPtr pkt)
13414064Sadrian.herrera@arm.com        {
13514064Sadrian.herrera@arm.com            return mapper.recvTimingResp(pkt);
13614064Sadrian.herrera@arm.com        }
13714064Sadrian.herrera@arm.com
13814064Sadrian.herrera@arm.com        void recvTimingSnoopReq(PacketPtr pkt)
13914039Sstacze01@arm.com        {
14014039Sstacze01@arm.com            mapper.recvTimingSnoopReq(pkt);
14114039Sstacze01@arm.com        }
14214039Sstacze01@arm.com
14314039Sstacze01@arm.com        void recvRangeChange()
144        {
145            mapper.recvRangeChange();
146        }
147
148        bool isSnooping() const
149        {
150            return mapper.isSnooping();
151        }
152
153        unsigned deviceBlockSize() const
154        {
155            return mapper.deviceBlockSizeMaster();
156        }
157
158        void recvRetry()
159        {
160            mapper.recvRetryMaster();
161        }
162
163      private:
164
165        AddrMapper& mapper;
166
167    };
168
169    /** Instance of master port, facing the memory side */
170    MapperMasterPort masterPort;
171
172    class MapperSlavePort : public SlavePort
173    {
174
175      public:
176
177        MapperSlavePort(const std::string& _name, AddrMapper& _mapper)
178            : SlavePort(_name, &_mapper), mapper(_mapper)
179        { }
180
181      protected:
182
183        void recvFunctional(PacketPtr pkt)
184        {
185            mapper.recvFunctional(pkt);
186        }
187
188        Tick recvAtomic(PacketPtr pkt)
189        {
190            return mapper.recvAtomic(pkt);
191        }
192
193        bool recvTimingReq(PacketPtr pkt)
194        {
195            return mapper.recvTimingReq(pkt);
196        }
197
198        bool recvTimingSnoopResp(PacketPtr pkt)
199        {
200            return mapper.recvTimingSnoopResp(pkt);
201        }
202
203        unsigned deviceBlockSize() const
204        {
205            return mapper.deviceBlockSizeSlave();
206        }
207
208        AddrRangeList getAddrRanges() const
209        {
210            return mapper.getAddrRanges();
211        }
212
213        void recvRetry()
214        {
215            mapper.recvRetrySlave();
216        }
217
218      private:
219
220        AddrMapper& mapper;
221
222    };
223
224    /** Instance of slave port, i.e. on the CPU side */
225    MapperSlavePort slavePort;
226
227    void recvFunctional(PacketPtr pkt);
228
229    void recvFunctionalSnoop(PacketPtr pkt);
230
231    Tick recvAtomic(PacketPtr pkt);
232
233    Tick recvAtomicSnoop(PacketPtr pkt);
234
235    bool recvTimingReq(PacketPtr pkt);
236
237    bool recvTimingResp(PacketPtr pkt);
238
239    void recvTimingSnoopReq(PacketPtr pkt);
240
241    bool recvTimingSnoopResp(PacketPtr pkt);
242
243    unsigned deviceBlockSizeMaster();
244
245    unsigned deviceBlockSizeSlave();
246
247    virtual AddrRangeList getAddrRanges() const = 0;
248
249    bool isSnooping() const;
250
251    void recvRetryMaster();
252
253    void recvRetrySlave();
254
255    void recvRangeChange();
256};
257
258/**
259 * Range address mapper that maps a set of original ranges to a set of
260 * remapped ranges, where a specific range is of the same size
261 * (original and remapped), only with an offset. It's useful for cases
262 * where memory is mapped to two different locations
263 */
264class RangeAddrMapper : public AddrMapper
265{
266
267  public:
268
269    RangeAddrMapper(const RangeAddrMapperParams* p);
270
271    ~RangeAddrMapper() { }
272
273    AddrRangeList getAddrRanges() const;
274
275  protected:
276
277    /**
278     * This contains a list of ranges the should be remapped. It must
279     * be the exact same length as remappedRanges which describes what
280     * manipulation should be done to each range.
281     */
282    std::vector<AddrRange> originalRanges;
283
284    /**
285     * This contains a list of ranges that addresses should be
286     * remapped to. See the description for originalRanges above
287     */
288    std::vector<AddrRange> remappedRanges;
289
290    Addr remapAddr(Addr addr) const;
291
292};
293
294#endif //__MEM_ADDR_MAPPER_HH__
295