xref: /qemu/include/hw/arm/smmu-common.h (revision 3b736c61849ebc16776143a947a90a731b0aea55)
1527773eeSEric Auger /*
2527773eeSEric Auger  * ARM SMMU Support
3527773eeSEric Auger  *
4527773eeSEric Auger  * Copyright (C) 2015-2016 Broadcom Corporation
5527773eeSEric Auger  * Copyright (c) 2017 Red Hat, Inc.
6527773eeSEric Auger  * Written by Prem Mallappa, Eric Auger
7527773eeSEric Auger  *
8527773eeSEric Auger  * This program is free software; you can redistribute it and/or modify
9527773eeSEric Auger  * it under the terms of the GNU General Public License version 2 as
10527773eeSEric Auger  * published by the Free Software Foundation.
11527773eeSEric Auger  *
12527773eeSEric Auger  * This program is distributed in the hope that it will be useful,
13527773eeSEric Auger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14527773eeSEric Auger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15527773eeSEric Auger  * GNU General Public License for more details.
16527773eeSEric Auger  *
17527773eeSEric Auger  */
18527773eeSEric Auger 
19527773eeSEric Auger #ifndef HW_ARM_SMMU_COMMON_H
20527773eeSEric Auger #define HW_ARM_SMMU_COMMON_H
21527773eeSEric Auger 
22527773eeSEric Auger #include "hw/sysbus.h"
23527773eeSEric Auger #include "hw/pci/pci.h"
24db1015e9SEduardo Habkost #include "qom/object.h"
25527773eeSEric Auger 
26527773eeSEric Auger #define SMMU_PCI_BUS_MAX      256
27527773eeSEric Auger #define SMMU_PCI_DEVFN_MAX    256
28b78aae9bSEric Auger #define SMMU_PCI_DEVFN(sid)   (sid & 0xFF)
29527773eeSEric Auger 
30527773eeSEric Auger /*
31527773eeSEric Auger  * Page table walk error types
32527773eeSEric Auger  */
33527773eeSEric Auger typedef enum {
34527773eeSEric Auger     SMMU_PTW_ERR_NONE,
35527773eeSEric Auger     SMMU_PTW_ERR_WALK_EABT,   /* Translation walk external abort */
36527773eeSEric Auger     SMMU_PTW_ERR_TRANSLATION, /* Translation fault */
37527773eeSEric Auger     SMMU_PTW_ERR_ADDR_SIZE,   /* Address Size fault */
38527773eeSEric Auger     SMMU_PTW_ERR_ACCESS,      /* Access fault */
39527773eeSEric Auger     SMMU_PTW_ERR_PERMISSION,  /* Permission fault */
40527773eeSEric Auger } SMMUPTWEventType;
41527773eeSEric Auger 
42527773eeSEric Auger typedef struct SMMUPTWEventInfo {
43527773eeSEric Auger     SMMUPTWEventType type;
44527773eeSEric Auger     dma_addr_t addr; /* fetched address that induced an abort, if any */
45527773eeSEric Auger } SMMUPTWEventInfo;
46527773eeSEric Auger 
47527773eeSEric Auger typedef struct SMMUTransTableInfo {
48527773eeSEric Auger     bool disabled;             /* is the translation table disabled? */
49527773eeSEric Auger     uint64_t ttb;              /* TT base address */
50527773eeSEric Auger     uint8_t tsz;               /* input range, ie. 2^(64 -tsz)*/
51527773eeSEric Auger     uint8_t granule_sz;        /* granule page shift */
52e7c3b9d9SEric Auger     bool had;                  /* hierarchical attribute disable */
53527773eeSEric Auger } SMMUTransTableInfo;
54527773eeSEric Auger 
55a7550158SEric Auger typedef struct SMMUTLBEntry {
56a7550158SEric Auger     IOMMUTLBEntry entry;
57a7550158SEric Auger     uint8_t level;
58a7550158SEric Auger     uint8_t granule;
59a7550158SEric Auger } SMMUTLBEntry;
60a7550158SEric Auger 
61*3b736c61SMostafa Saleh /* Stage-2 configuration. */
62*3b736c61SMostafa Saleh typedef struct SMMUS2Cfg {
63*3b736c61SMostafa Saleh     uint8_t tsz;            /* Size of IPA input region (S2T0SZ) */
64*3b736c61SMostafa Saleh     uint8_t sl0;            /* Start level of translation (S2SL0) */
65*3b736c61SMostafa Saleh     bool affd;              /* AF Fault Disable (S2AFFD) */
66*3b736c61SMostafa Saleh     bool record_faults;     /* Record fault events (S2R) */
67*3b736c61SMostafa Saleh     uint8_t granule_sz;     /* Granule page shift (based on S2TG) */
68*3b736c61SMostafa Saleh     uint8_t eff_ps;         /* Effective PA output range (based on S2PS) */
69*3b736c61SMostafa Saleh     uint16_t vmid;          /* Virtual Machine ID (S2VMID) */
70*3b736c61SMostafa Saleh     uint64_t vttb;          /* Address of translation table base (S2TTB) */
71*3b736c61SMostafa Saleh } SMMUS2Cfg;
72*3b736c61SMostafa Saleh 
73527773eeSEric Auger /*
74527773eeSEric Auger  * Generic structure populated by derived SMMU devices
75527773eeSEric Auger  * after decoding the configuration information and used as
76527773eeSEric Auger  * input to the page table walk
77527773eeSEric Auger  */
78527773eeSEric Auger typedef struct SMMUTransCfg {
79*3b736c61SMostafa Saleh     /* Shared fields between stage-1 and stage-2. */
80527773eeSEric Auger     int stage;                 /* translation stage */
81527773eeSEric Auger     bool disabled;             /* smmu is disabled */
82527773eeSEric Auger     bool bypassed;             /* translation is bypassed */
83527773eeSEric Auger     bool aborted;              /* translation is aborted */
84*3b736c61SMostafa Saleh     uint32_t iotlb_hits;       /* counts IOTLB hits */
85*3b736c61SMostafa Saleh     uint32_t iotlb_misses;     /* counts IOTLB misses*/
86*3b736c61SMostafa Saleh     /* Used by stage-1 only. */
87*3b736c61SMostafa Saleh     bool aa64;                 /* arch64 or aarch32 translation table */
88ced71694SJean-Philippe Brucker     bool record_faults;        /* record fault events */
89527773eeSEric Auger     uint64_t ttb;              /* TT base address */
90527773eeSEric Auger     uint8_t oas;               /* output address width */
91527773eeSEric Auger     uint8_t tbi;               /* Top Byte Ignore */
92527773eeSEric Auger     uint16_t asid;
93527773eeSEric Auger     SMMUTransTableInfo tt[2];
94*3b736c61SMostafa Saleh     /* Used by stage-2 only. */
95*3b736c61SMostafa Saleh     struct SMMUS2Cfg s2cfg;
96527773eeSEric Auger } SMMUTransCfg;
97527773eeSEric Auger 
98527773eeSEric Auger typedef struct SMMUDevice {
99527773eeSEric Auger     void               *smmu;
100527773eeSEric Auger     PCIBus             *bus;
101527773eeSEric Auger     int                devfn;
102527773eeSEric Auger     IOMMUMemoryRegion  iommu;
103527773eeSEric Auger     AddressSpace       as;
10432cfd7f3SEric Auger     uint32_t           cfg_cache_hits;
10532cfd7f3SEric Auger     uint32_t           cfg_cache_misses;
106c6370441SEric Auger     QLIST_ENTRY(SMMUDevice) next;
107527773eeSEric Auger } SMMUDevice;
108527773eeSEric Auger 
109527773eeSEric Auger typedef struct SMMUPciBus {
110527773eeSEric Auger     PCIBus       *bus;
111f7795e40SPhilippe Mathieu-Daudé     SMMUDevice   *pbdev[]; /* Parent array is sparse, so dynamically alloc */
112527773eeSEric Auger } SMMUPciBus;
113527773eeSEric Auger 
114cc27ed81SEric Auger typedef struct SMMUIOTLBKey {
115cc27ed81SEric Auger     uint64_t iova;
116cc27ed81SEric Auger     uint16_t asid;
1179e54dee7SEric Auger     uint8_t tg;
1189e54dee7SEric Auger     uint8_t level;
119cc27ed81SEric Auger } SMMUIOTLBKey;
120cc27ed81SEric Auger 
121db1015e9SEduardo Habkost struct SMMUState {
122527773eeSEric Auger     /* <private> */
123527773eeSEric Auger     SysBusDevice  dev;
124527773eeSEric Auger     const char *mrtypename;
125527773eeSEric Auger     MemoryRegion iomem;
126527773eeSEric Auger 
127527773eeSEric Auger     GHashTable *smmu_pcibus_by_busptr;
128527773eeSEric Auger     GHashTable *configs; /* cache for configuration data */
129527773eeSEric Auger     GHashTable *iotlb;
130527773eeSEric Auger     SMMUPciBus *smmu_pcibus_by_bus_num[SMMU_PCI_BUS_MAX];
131527773eeSEric Auger     PCIBus *pci_bus;
132c6370441SEric Auger     QLIST_HEAD(, SMMUDevice) devices_with_notifiers;
133527773eeSEric Auger     uint8_t bus_num;
134527773eeSEric Auger     PCIBus *primary_bus;
135db1015e9SEduardo Habkost };
136527773eeSEric Auger 
137db1015e9SEduardo Habkost struct SMMUBaseClass {
138527773eeSEric Auger     /* <private> */
139527773eeSEric Auger     SysBusDeviceClass parent_class;
140527773eeSEric Auger 
141527773eeSEric Auger     /*< public >*/
142527773eeSEric Auger 
143527773eeSEric Auger     DeviceRealize parent_realize;
144527773eeSEric Auger 
145db1015e9SEduardo Habkost };
146527773eeSEric Auger 
147527773eeSEric Auger #define TYPE_ARM_SMMU "arm-smmu"
148a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(SMMUState, SMMUBaseClass, ARM_SMMU)
149527773eeSEric Auger 
150cac994efSEric Auger /* Return the SMMUPciBus handle associated to a PCI bus number */
151cac994efSEric Auger SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num);
152cac994efSEric Auger 
153cac994efSEric Auger /* Return the stream ID of an SMMU device */
154cac994efSEric Auger static inline uint16_t smmu_get_sid(SMMUDevice *sdev)
155cac994efSEric Auger {
156cac994efSEric Auger     return PCI_BUILD_BDF(pci_bus_num(sdev->bus), sdev->devfn);
157cac994efSEric Auger }
15893641948SEric Auger 
15993641948SEric Auger /**
16093641948SEric Auger  * smmu_ptw - Perform the page table walk for a given iova / access flags
16193641948SEric Auger  * pair, according to @cfg translation config
16293641948SEric Auger  */
16393641948SEric Auger int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm,
164a7550158SEric Auger              SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info);
16593641948SEric Auger 
16693641948SEric Auger /**
16793641948SEric Auger  * select_tt - compute which translation table shall be used according to
16893641948SEric Auger  * the input iova and translation config and return the TT specific info
16993641948SEric Auger  */
17093641948SEric Auger SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova);
17193641948SEric Auger 
17232cfd7f3SEric Auger /* Return the iommu mr associated to @sid, or NULL if none */
17332cfd7f3SEric Auger IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid);
17432cfd7f3SEric Auger 
175cc27ed81SEric Auger #define SMMU_IOTLB_MAX_SIZE 256
176cc27ed81SEric Auger 
1779e54dee7SEric Auger SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
1789e54dee7SEric Auger                                 SMMUTransTableInfo *tt, hwaddr iova);
179a7550158SEric Auger void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry);
1809e54dee7SEric Auger SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova,
1819e54dee7SEric Auger                                 uint8_t tg, uint8_t level);
182cc27ed81SEric Auger void smmu_iotlb_inv_all(SMMUState *s);
183cc27ed81SEric Auger void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid);
184d5291561SEric Auger void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova,
185d5291561SEric Auger                          uint8_t tg, uint64_t num_pages, uint8_t ttl);
186cc27ed81SEric Auger 
187832e4222SEric Auger /* Unmap the range of all the notifiers registered to any IOMMU mr */
188832e4222SEric Auger void smmu_inv_notifiers_all(SMMUState *s);
189832e4222SEric Auger 
1906834c3f4SMarkus Armbruster #endif /* HW_ARM_SMMU_COMMON_H */
191