gic_v3_its.hh revision 13996:8a567118e670
110447Snilay@cs.wisc.edu/*
210447Snilay@cs.wisc.edu * Copyright (c) 2019 ARM Limited
310447Snilay@cs.wisc.edu * All rights reserved
410447Snilay@cs.wisc.edu *
510447Snilay@cs.wisc.edu * The license below extends only to copyright in the software and shall
610447Snilay@cs.wisc.edu * not be construed as granting a license to any other intellectual
710447Snilay@cs.wisc.edu * property including but not limited to intellectual property relating
810447Snilay@cs.wisc.edu * to a hardware implementation of the functionality of the software
910447Snilay@cs.wisc.edu * licensed hereunder.  You may use the software subject to the license
1010447Snilay@cs.wisc.edu * terms below provided that you ensure that this notice is replicated
1110447Snilay@cs.wisc.edu * unmodified and in its entirety in all distributions of the software,
1210447Snilay@cs.wisc.edu * modified or unmodified, in source code or in binary form.
1310447Snilay@cs.wisc.edu *
1410447Snilay@cs.wisc.edu * Redistribution and use in source and binary forms, with or without
1510447Snilay@cs.wisc.edu * modification, are permitted provided that the following conditions are
1610447Snilay@cs.wisc.edu * met: redistributions of source code must retain the above copyright
1710447Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer;
1810447Snilay@cs.wisc.edu * redistributions in binary form must reproduce the above copyright
1910447Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer in the
2010447Snilay@cs.wisc.edu * documentation and/or other materials provided with the distribution;
2110447Snilay@cs.wisc.edu * neither the name of the copyright holders nor the names of its
2210447Snilay@cs.wisc.edu * contributors may be used to endorse or promote products derived from
2310447Snilay@cs.wisc.edu * this software without specific prior written permission.
2410447Snilay@cs.wisc.edu *
2510447Snilay@cs.wisc.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2610447Snilay@cs.wisc.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2710447Snilay@cs.wisc.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2810447Snilay@cs.wisc.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2910447Snilay@cs.wisc.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3010447Snilay@cs.wisc.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3110447Snilay@cs.wisc.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3210447Snilay@cs.wisc.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3310447Snilay@cs.wisc.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3410447Snilay@cs.wisc.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3510447Snilay@cs.wisc.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3610447Snilay@cs.wisc.edu *
3710447Snilay@cs.wisc.edu * Authors: Giacomo Travaglini
3810447Snilay@cs.wisc.edu */
3910447Snilay@cs.wisc.edu
4010447Snilay@cs.wisc.edu#ifndef __DEV_ARM_GICV3_ITS_H__
4110447Snilay@cs.wisc.edu#define __DEV_ARM_GICV3_ITS_H__
4210447Snilay@cs.wisc.edu
4310447Snilay@cs.wisc.edu#include <queue>
4410447Snilay@cs.wisc.edu
4510447Snilay@cs.wisc.edu#include "base/coroutine.hh"
4610447Snilay@cs.wisc.edu#include "dev/dma_device.hh"
4710447Snilay@cs.wisc.edu#include "params/Gicv3Its.hh"
4810447Snilay@cs.wisc.edu
4910447Snilay@cs.wisc.educlass Gicv3;
5010447Snilay@cs.wisc.educlass Gicv3Redistributor;
5110447Snilay@cs.wisc.educlass ItsProcess;
5210447Snilay@cs.wisc.educlass ItsTranslation;
5310447Snilay@cs.wisc.educlass ItsCommand;
5410447Snilay@cs.wisc.edu
5510447Snilay@cs.wisc.eduenum class ItsActionType
5610447Snilay@cs.wisc.edu{
5710447Snilay@cs.wisc.edu    INITIAL_NOP,
5810447Snilay@cs.wisc.edu    SEND_REQ,
5910447Snilay@cs.wisc.edu    TERMINATE,
6010447Snilay@cs.wisc.edu};
6110447Snilay@cs.wisc.edu
6210447Snilay@cs.wisc.edustruct ItsAction
6310447Snilay@cs.wisc.edu{
6410447Snilay@cs.wisc.edu    ItsActionType type;
6510447Snilay@cs.wisc.edu    PacketPtr pkt;
6610447Snilay@cs.wisc.edu    Tick delay;
6710447Snilay@cs.wisc.edu};
6810447Snilay@cs.wisc.edu
6910447Snilay@cs.wisc.edu/**
7010447Snilay@cs.wisc.edu * GICv3 ITS module. This class is just modelling a pio device with its
7110447Snilay@cs.wisc.edu * memory mapped registers. Most of the ITS functionalities are
7210447Snilay@cs.wisc.edu * implemented as processes (ItsProcess) objects, like ItsTranslation or
7310447Snilay@cs.wisc.edu * ItsCommand.
7410447Snilay@cs.wisc.edu * Main job of Gicv3Its is to spawn those processes upon receival of packets.
7510447Snilay@cs.wisc.edu */
7610447Snilay@cs.wisc.educlass Gicv3Its : public BasicPioDevice
7710447Snilay@cs.wisc.edu{
7810447Snilay@cs.wisc.edu    friend class ::ItsProcess;
7910447Snilay@cs.wisc.edu    friend class ::ItsTranslation;
8010447Snilay@cs.wisc.edu    friend class ::ItsCommand;
8110447Snilay@cs.wisc.edu  public:
82    class DataPort : public MasterPort
83    {
84      protected:
85        Gicv3Its &its;
86
87      public:
88        DataPort(const std::string &_name, Gicv3Its &_its) :
89            MasterPort(_name, &_its),
90            its(_its)
91        {}
92
93        virtual ~DataPort() {}
94
95        bool recvTimingResp(PacketPtr pkt) { return its.recvTimingResp(pkt); }
96        void recvReqRetry() { return its.recvReqRetry(); }
97    };
98
99    DataPort dmaPort;
100
101    Port & getPort(const std::string &if_name, PortID idx) override;
102    bool recvTimingResp(PacketPtr pkt);
103    void recvReqRetry();
104
105    Gicv3Its(const Gicv3ItsParams *params);
106
107    void setGIC(Gicv3 *_gic);
108
109    static const uint32_t itsControl = 0x0;
110    static const uint32_t itsTranslate = 0x10000;
111
112    // Address range part of Control frame
113    static const AddrRange GITS_BASER;
114
115    static const uint32_t NUM_BASER_REGS = 8;
116
117    enum : Addr
118    {
119        // Control frame
120        GITS_CTLR    = itsControl + 0x0000,
121        GITS_IIDR    = itsControl + 0x0004,
122        GITS_TYPER   = itsControl + 0x0008,
123        GITS_CBASER  = itsControl + 0x0080,
124        GITS_CWRITER = itsControl + 0x0088,
125        GITS_CREADR  = itsControl + 0x0090,
126
127        // Translation frame
128        GITS_TRANSLATER = itsTranslate + 0x0040
129    };
130
131    AddrRangeList getAddrRanges() const override;
132
133    Tick read(PacketPtr pkt) override;
134    Tick write(PacketPtr pkt) override;
135
136    DrainState drain() override;
137    void serialize(CheckpointOut & cp) const override;
138    void unserialize(CheckpointIn & cp) override;
139
140    void translate(PacketPtr pkt);
141
142    BitUnion32(CTLR)
143        Bitfield<31> quiescent;
144        Bitfield<7, 4> itsNumber;
145        Bitfield<1> imDe;
146        Bitfield<0> enabled;
147    EndBitUnion(CTLR)
148
149    // Command read/write, (CREADR, CWRITER)
150    BitUnion64(CRDWR)
151        Bitfield<19, 5> offset;
152        Bitfield<0> retry;
153        Bitfield<0> stalled;
154    EndBitUnion(CRDWR)
155
156    BitUnion64(CBASER)
157        Bitfield<63> valid;
158        Bitfield<61, 59> innerCache;
159        Bitfield<55, 53> outerCache;
160        Bitfield<51, 12> physAddr;
161        Bitfield<11, 10> shareability;
162        Bitfield<7, 0> size;
163    EndBitUnion(CBASER)
164
165    BitUnion64(BASER)
166        Bitfield<63> valid;
167        Bitfield<62> indirect;
168        Bitfield<61, 59> innerCache;
169        Bitfield<58, 56> type;
170        Bitfield<55, 53> outerCache;
171        Bitfield<52, 48> entrySize;
172        Bitfield<47, 12> physAddr;
173        Bitfield<11, 10> shareability;
174        Bitfield<9, 8> pageSize;
175        Bitfield<7, 0> size;
176    EndBitUnion(BASER)
177
178    BitUnion64(TYPER)
179        Bitfield<37> vmovp;
180        Bitfield<36> cil;
181        Bitfield<35, 32> cidBits;
182        Bitfield<31, 24> hcc;
183        Bitfield<19> pta;
184        Bitfield<18> seis;
185        Bitfield<17, 13> devBits;
186        Bitfield<12, 8> idBits;
187        Bitfield<7, 4> ittEntrySize;
188        Bitfield<2> cct;
189        Bitfield<1> _virtual;
190        Bitfield<0> physical;
191    EndBitUnion(TYPER)
192
193    CTLR     gitsControl;
194    TYPER    gitsTyper;
195    CBASER   gitsCbaser;
196    CRDWR    gitsCreadr;
197    CRDWR    gitsCwriter;
198    uint32_t gitsIidr;
199    uint32_t gitsTranslater;
200
201    std::vector<BASER> tableBases;
202
203    /**
204     * Returns TRUE if the eventID supplied has bits above the implemented
205     * size or above the itt_range
206     */
207    bool idOutOfRange(uint32_t event_id, uint8_t itt_range) const;
208
209    /**
210     * Returns TRUE if the value supplied has bits above the implemented range
211     * or if the value supplied exceeds the maximum configured size in the
212     * appropriate GITS_BASER<n>
213     */
214    bool deviceOutOfRange(uint32_t device_id) const;
215
216    /**
217     * Returns TRUE if the value (size) supplied exceeds the maximum
218     * allowed by GITS_TYPER.ID_bits. Size is the parameter which is
219     * passed to the ITS via the MAPD command and is stored in the
220     * DTE.ittRange field.
221     */
222    bool sizeOutOfRange(uint32_t size) const;
223
224    /**
225     * Returns TRUE if the value supplied has bits above the implemented range
226     * or if the value exceeds the total number of collections supported in
227     * hardware and external memory
228     */
229    bool collectionOutOfRange(uint32_t collection_id) const;
230
231    /**
232     * Returns TRUE if the value supplied is larger than that permitted by
233     * GICD_TYPER.IDbits or not in the LPI range and is not 1023
234     */
235    bool lpiOutOfRange(uint32_t intid) const;
236
237  private: // Command
238    void checkCommandQueue();
239    void incrementReadPointer();
240
241  public: // TableWalk
242    BitUnion64(DTE)
243        Bitfield<57, 53> ittRange;
244        Bitfield<52, 1> ittAddress;
245        Bitfield<0> valid;
246    EndBitUnion(DTE)
247
248    BitUnion64(ITTE)
249        Bitfield<59, 46> vpeid;
250        Bitfield<45, 30> icid;
251        Bitfield<29, 16> intNumHyp;
252        Bitfield<15, 2> intNum;
253        Bitfield<1> intType;
254        Bitfield<0> valid;
255    EndBitUnion(ITTE)
256
257    BitUnion64(CTE)
258        Bitfield<40, 1> rdBase;
259        Bitfield<0> valid;
260    EndBitUnion(CTE)
261
262    enum InterruptType
263    {
264        VIRTUAL_INTERRUPT = 0,
265        PHYSICAL_INTERRUPT = 1
266    };
267
268  private:
269    Gicv3Redistributor* getRedistributor(uint64_t rd_base);
270    Gicv3Redistributor* getRedistributor(CTE cte)
271    {
272        return getRedistributor(cte.rdBase);
273    }
274
275    ItsAction runProcess(ItsProcess *proc, PacketPtr pkt);
276    ItsAction runProcessTiming(ItsProcess *proc, PacketPtr pkt);
277    ItsAction runProcessAtomic(ItsProcess *proc, PacketPtr pkt);
278
279    enum ItsTables
280    {
281        DEVICE_TABLE = 1,
282        VPE_TABLE = 2,
283        TRANSLATION_TABLE = 3,
284        COLLECTION_TABLE = 4
285    };
286
287    enum PageSize
288    {
289        SIZE_4K,
290        SIZE_16K,
291        SIZE_64K
292    };
293
294    Addr pageAddress(enum ItsTables table);
295
296    void moveAllPendingState(
297        Gicv3Redistributor *rd1, Gicv3Redistributor *rd2);
298
299  private:
300    std::queue<ItsAction> packetsToRetry;
301    uint32_t masterId;
302    Gicv3 *gic;
303    EventFunctionWrapper commandEvent;
304
305    bool pendingCommands;
306    uint32_t pendingTranslations;
307};
308
309/**
310 * ItsProcess is a base coroutine wrapper which is spawned by
311 * the Gicv3Its module when the latter needs to perform different
312 * actions, like translating a peripheral's MSI into an LPI
313 * (See derived ItsTranslation) or processing a Command from the
314 * ITS queue (ItsCommand).
315 * The action to take is implemented by the method:
316 *
317 * virtual void main(Yield &yield) = 0;
318 * It's inheriting from Packet::SenderState since the generic process
319 * will be stopped (we are using coroutines) and sent with the packet
320 * to memory when doing table walks.
321 * When Gicv3Its receives a response, it will resume the coroutine from
322 * the point it stopped when yielding.
323 */
324class ItsProcess : public Packet::SenderState
325{
326  public:
327    using DTE = Gicv3Its::DTE;
328    using ITTE = Gicv3Its::ITTE;
329    using CTE = Gicv3Its::CTE;
330    using Coroutine = m5::Coroutine<PacketPtr, ItsAction>;
331    using Yield = Coroutine::CallerType;
332
333    ItsProcess(Gicv3Its &_its);
334    virtual ~ItsProcess();
335
336    /** Returns the Gicv3Its name. Mainly used for DPRINTS */
337    const std::string name() const;
338
339    ItsAction run(PacketPtr pkt);
340
341  protected:
342    void reinit();
343    virtual void main(Yield &yield) = 0;
344
345    void writeDeviceTable(Yield &yield, uint32_t device_id, DTE dte);
346
347    void writeIrqTranslationTable(
348        Yield &yield, const Addr itt_base, uint32_t event_id, ITTE itte);
349
350    void writeIrqCollectionTable(
351        Yield &yield, uint32_t collection_id, CTE cte);
352
353    uint64_t readDeviceTable(
354        Yield &yield, uint32_t device_id);
355
356    uint64_t readIrqTranslationTable(
357        Yield &yield, const Addr itt_base, uint32_t event_id);
358
359    uint64_t readIrqCollectionTable(Yield &yield, uint32_t collection_id);
360
361    void doRead(Yield &yield, Addr addr, void *ptr, size_t size);
362    void doWrite(Yield &yield, Addr addr, void *ptr, size_t size);
363    void terminate(Yield &yield);
364
365  protected:
366    Gicv3Its &its;
367
368  private:
369    std::unique_ptr<Coroutine> coroutine;
370};
371
372/**
373 * An ItsTranslation is created whenever a peripheral writes a message in
374 * GITS_TRANSLATER (MSI). In this case main will simply do the table walks
375 * until it gets a redistributor and an INTID. It will then raise the
376 * LPI interrupt to the target redistributor.
377 */
378class ItsTranslation : public ItsProcess
379{
380  public:
381    ItsTranslation(Gicv3Its &_its);
382    ~ItsTranslation();
383
384  protected:
385    void main(Yield &yield) override;
386
387    std::pair<uint32_t, Gicv3Redistributor *>
388    translateLPI(Yield &yield, uint32_t device_id, uint32_t event_id);
389};
390
391/**
392 * An ItsCommand is created whenever there is a new command in the command
393 * queue. Only one command can be executed per time.
394 * main will firstly read the command from memory and then it will process
395 * it.
396 */
397class ItsCommand : public ItsProcess
398{
399  public:
400    union CommandEntry
401    {
402        struct
403        {
404            uint32_t type;
405            uint32_t deviceId;
406            uint32_t eventId;
407            uint32_t pintId;
408
409            uint32_t data[4];
410        };
411        uint64_t raw[4];
412    };
413
414    enum CommandType : uint32_t
415    {
416        CLEAR = 0x04,
417        DISCARD = 0x0F,
418        INT = 0x03,
419        INV = 0x0C,
420        INVALL = 0x0D,
421        MAPC = 0x09,
422        MAPD = 0x08,
423        MAPI = 0x0B,
424        MAPTI = 0x0A,
425        MOVALL = 0x0E,
426        MOVI = 0x01,
427        SYNC = 0x05,
428        VINVALL = 0x2D,
429        VMAPI = 0x2B,
430        VMAPP = 0x29,
431        VMAPTI = 0x2A,
432        VMOVI = 0x21,
433        VMOVP = 0x22,
434        VSYNC = 0x25
435    };
436
437    ItsCommand(Gicv3Its &_its);
438    ~ItsCommand();
439
440  protected:
441    /**
442     * Dispatch entry is a metadata struct which contains information about
443     * the command (like the name) and the function object implementing
444     * the command.
445     */
446    struct DispatchEntry
447    {
448        using ExecFn = std::function<void(ItsCommand*, Yield&, CommandEntry&)>;
449
450        DispatchEntry(std::string _name, ExecFn _exec)
451          : name(_name), exec(_exec)
452        {}
453
454        std::string name;
455        ExecFn exec;
456    };
457
458    using DispatchTable = std::unordered_map<
459        std::underlying_type<enum CommandType>::type, DispatchEntry>;
460
461    static DispatchTable cmdDispatcher;
462
463    static std::string commandName(uint32_t cmd);
464
465    void main(Yield &yield) override;
466
467    void readCommand(Yield &yield, CommandEntry &command);
468    void processCommand(Yield &yield, CommandEntry &command);
469
470    // Commands
471    void clear(Yield &yield, CommandEntry &command);
472    void discard(Yield &yield, CommandEntry &command);
473    void mapc(Yield &yield, CommandEntry &command);
474    void mapd(Yield &yield, CommandEntry &command);
475    void mapi(Yield &yield, CommandEntry &command);
476    void mapti(Yield &yield, CommandEntry &command);
477    void movall(Yield &yield, CommandEntry &command);
478    void movi(Yield &yield, CommandEntry &command);
479    void sync(Yield &yield, CommandEntry &command);
480    void doInt(Yield &yield, CommandEntry &command);
481    void inv(Yield &yield, CommandEntry &command);
482    void invall(Yield &yield, CommandEntry &command);
483    void vinvall(Yield &yield, CommandEntry &command);
484    void vmapi(Yield &yield, CommandEntry &command);
485    void vmapp(Yield &yield, CommandEntry &command);
486    void vmapti(Yield &yield, CommandEntry &command);
487    void vmovi(Yield &yield, CommandEntry &command);
488    void vmovp(Yield &yield, CommandEntry &command);
489    void vsync(Yield &yield, CommandEntry &command);
490
491  protected: // Helpers
492    bool idOutOfRange(CommandEntry &command, DTE dte) const
493    {
494        return its.idOutOfRange(command.eventId, dte.ittRange);
495    }
496
497    bool deviceOutOfRange(CommandEntry &command) const
498    {
499        return its.deviceOutOfRange(command.deviceId);
500    }
501
502    bool sizeOutOfRange(CommandEntry &command) const
503    {
504        const auto size = bits(command.raw[1], 4, 0);
505        const auto valid = bits(command.raw[2], 63);
506        if (valid)
507            return its.sizeOutOfRange(size);
508        else
509            return false;
510    }
511
512    bool collectionOutOfRange(CommandEntry &command) const
513    {
514        return its.collectionOutOfRange(bits(command.raw[2], 15, 0));
515    }
516};
517
518#endif
519