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