xref: /qemu/include/hw/arm/smmu-common.h (revision ffbc5e661fc3b73debaec2354bf46273186bf882)
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 
30bcc919e7SMostafa Saleh /* VMSAv8-64 Translation constants and functions */
31bcc919e7SMostafa Saleh #define VMSA_LEVELS                         4
3221eb5b5cSMostafa Saleh #define VMSA_MAX_S2_CONCAT                  16
33bcc919e7SMostafa Saleh 
34bcc919e7SMostafa Saleh #define VMSA_STRIDE(gran)                   ((gran) - VMSA_LEVELS + 1)
35bcc919e7SMostafa Saleh #define VMSA_BIT_LVL(isz, strd, lvl)        ((isz) - (strd) * \
36bcc919e7SMostafa Saleh                                              (VMSA_LEVELS - (lvl)))
37bcc919e7SMostafa Saleh #define VMSA_IDXMSK(isz, strd, lvl)         ((1ULL << \
38bcc919e7SMostafa Saleh                                              VMSA_BIT_LVL(isz, strd, lvl)) - 1)
39bcc919e7SMostafa Saleh 
40ec31ef91SMostafa Saleh #define CACHED_ENTRY_TO_ADDR(ent, addr)      ((ent)->entry.translated_addr + \
41ec31ef91SMostafa Saleh                                              ((addr) & (ent)->entry.addr_mask))
42ec31ef91SMostafa Saleh 
43527773eeSEric Auger /*
44527773eeSEric Auger  * Page table walk error types
45527773eeSEric Auger  */
46527773eeSEric Auger typedef enum {
47527773eeSEric Auger     SMMU_PTW_ERR_NONE,
48527773eeSEric Auger     SMMU_PTW_ERR_WALK_EABT,   /* Translation walk external abort */
49527773eeSEric Auger     SMMU_PTW_ERR_TRANSLATION, /* Translation fault */
50527773eeSEric Auger     SMMU_PTW_ERR_ADDR_SIZE,   /* Address Size fault */
51527773eeSEric Auger     SMMU_PTW_ERR_ACCESS,      /* Access fault */
52527773eeSEric Auger     SMMU_PTW_ERR_PERMISSION,  /* Permission fault */
53527773eeSEric Auger } SMMUPTWEventType;
54527773eeSEric Auger 
55f6cc1980SMostafa Saleh /* SMMU Stage */
56f6cc1980SMostafa Saleh typedef enum {
57f6cc1980SMostafa Saleh     SMMU_STAGE_1 = 1,
58f6cc1980SMostafa Saleh     SMMU_STAGE_2,
59f6cc1980SMostafa Saleh     SMMU_NESTED,
60f6cc1980SMostafa Saleh } SMMUStage;
61f6cc1980SMostafa Saleh 
62527773eeSEric Auger typedef struct SMMUPTWEventInfo {
63f6cc1980SMostafa Saleh     SMMUStage stage;
64527773eeSEric Auger     SMMUPTWEventType type;
65527773eeSEric Auger     dma_addr_t addr; /* fetched address that induced an abort, if any */
66f42a0a57SMostafa Saleh     bool is_ipa_descriptor; /* src for fault in nested translation. */
67527773eeSEric Auger } SMMUPTWEventInfo;
68527773eeSEric Auger 
69527773eeSEric Auger typedef struct SMMUTransTableInfo {
70527773eeSEric Auger     bool disabled;             /* is the translation table disabled? */
71527773eeSEric Auger     uint64_t ttb;              /* TT base address */
72527773eeSEric Auger     uint8_t tsz;               /* input range, ie. 2^(64 -tsz)*/
73527773eeSEric Auger     uint8_t granule_sz;        /* granule page shift */
74e7c3b9d9SEric Auger     bool had;                  /* hierarchical attribute disable */
75527773eeSEric Auger } SMMUTransTableInfo;
76527773eeSEric Auger 
77a7550158SEric Auger typedef struct SMMUTLBEntry {
78a7550158SEric Auger     IOMMUTLBEntry entry;
79a7550158SEric Auger     uint8_t level;
80a7550158SEric Auger     uint8_t granule;
81d7cdf89cSMostafa Saleh     IOMMUAccessFlags parent_perm;
82a7550158SEric Auger } SMMUTLBEntry;
83a7550158SEric Auger 
843b736c61SMostafa Saleh /* Stage-2 configuration. */
853b736c61SMostafa Saleh typedef struct SMMUS2Cfg {
863b736c61SMostafa Saleh     uint8_t tsz;            /* Size of IPA input region (S2T0SZ) */
873b736c61SMostafa Saleh     uint8_t sl0;            /* Start level of translation (S2SL0) */
883b736c61SMostafa Saleh     bool affd;              /* AF Fault Disable (S2AFFD) */
893b736c61SMostafa Saleh     bool record_faults;     /* Record fault events (S2R) */
903b736c61SMostafa Saleh     uint8_t granule_sz;     /* Granule page shift (based on S2TG) */
913b736c61SMostafa Saleh     uint8_t eff_ps;         /* Effective PA output range (based on S2PS) */
92d8838226SMostafa Saleh     int vmid;               /* Virtual Machine ID (S2VMID) */
933b736c61SMostafa Saleh     uint64_t vttb;          /* Address of translation table base (S2TTB) */
943b736c61SMostafa Saleh } SMMUS2Cfg;
953b736c61SMostafa Saleh 
96527773eeSEric Auger /*
97527773eeSEric Auger  * Generic structure populated by derived SMMU devices
98527773eeSEric Auger  * after decoding the configuration information and used as
99527773eeSEric Auger  * input to the page table walk
100527773eeSEric Auger  */
101527773eeSEric Auger typedef struct SMMUTransCfg {
1023b736c61SMostafa Saleh     /* Shared fields between stage-1 and stage-2. */
103f6cc1980SMostafa Saleh     SMMUStage stage;           /* translation stage */
104527773eeSEric Auger     bool disabled;             /* smmu is disabled */
105527773eeSEric Auger     bool bypassed;             /* translation is bypassed */
106527773eeSEric Auger     bool aborted;              /* translation is aborted */
10715f6c16eSLuc Michel     bool affd;                 /* AF fault disable */
1083b736c61SMostafa Saleh     uint32_t iotlb_hits;       /* counts IOTLB hits */
1093b736c61SMostafa Saleh     uint32_t iotlb_misses;     /* counts IOTLB misses*/
1103b736c61SMostafa Saleh     /* Used by stage-1 only. */
1113b736c61SMostafa Saleh     bool aa64;                 /* arch64 or aarch32 translation table */
112ced71694SJean-Philippe Brucker     bool record_faults;        /* record fault events */
113527773eeSEric Auger     uint8_t oas;               /* output address width */
114527773eeSEric Auger     uint8_t tbi;               /* Top Byte Ignore */
115d8838226SMostafa Saleh     int asid;
116527773eeSEric Auger     SMMUTransTableInfo tt[2];
1173b736c61SMostafa Saleh     /* Used by stage-2 only. */
1183b736c61SMostafa Saleh     struct SMMUS2Cfg s2cfg;
119527773eeSEric Auger } SMMUTransCfg;
120527773eeSEric Auger 
121527773eeSEric Auger typedef struct SMMUDevice {
122527773eeSEric Auger     void               *smmu;
123527773eeSEric Auger     PCIBus             *bus;
124527773eeSEric Auger     int                devfn;
125527773eeSEric Auger     IOMMUMemoryRegion  iommu;
126527773eeSEric Auger     AddressSpace       as;
12732cfd7f3SEric Auger     uint32_t           cfg_cache_hits;
12832cfd7f3SEric Auger     uint32_t           cfg_cache_misses;
129c6370441SEric Auger     QLIST_ENTRY(SMMUDevice) next;
130527773eeSEric Auger } SMMUDevice;
131527773eeSEric Auger 
132527773eeSEric Auger typedef struct SMMUPciBus {
133527773eeSEric Auger     PCIBus       *bus;
134f7795e40SPhilippe Mathieu-Daudé     SMMUDevice   *pbdev[]; /* Parent array is sparse, so dynamically alloc */
135527773eeSEric Auger } SMMUPciBus;
136527773eeSEric Auger 
137cc27ed81SEric Auger typedef struct SMMUIOTLBKey {
138cc27ed81SEric Auger     uint64_t iova;
139d8838226SMostafa Saleh     int asid;
140d8838226SMostafa Saleh     int vmid;
1419e54dee7SEric Auger     uint8_t tg;
1429e54dee7SEric Auger     uint8_t level;
143cc27ed81SEric Auger } SMMUIOTLBKey;
144cc27ed81SEric Auger 
145*8881b691SJianChunfu typedef struct SMMUSIDRange {
146*8881b691SJianChunfu     uint32_t start;
147*8881b691SJianChunfu     uint32_t end;
148*8881b691SJianChunfu } SMMUSIDRange;
149*8881b691SJianChunfu 
150db1015e9SEduardo Habkost struct SMMUState {
151527773eeSEric Auger     /* <private> */
152527773eeSEric Auger     SysBusDevice  dev;
153527773eeSEric Auger     const char *mrtypename;
154527773eeSEric Auger     MemoryRegion iomem;
155527773eeSEric Auger 
156527773eeSEric Auger     GHashTable *smmu_pcibus_by_busptr;
157527773eeSEric Auger     GHashTable *configs; /* cache for configuration data */
158527773eeSEric Auger     GHashTable *iotlb;
159527773eeSEric Auger     SMMUPciBus *smmu_pcibus_by_bus_num[SMMU_PCI_BUS_MAX];
160527773eeSEric Auger     PCIBus *pci_bus;
161c6370441SEric Auger     QLIST_HEAD(, SMMUDevice) devices_with_notifiers;
162527773eeSEric Auger     uint8_t bus_num;
163527773eeSEric Auger     PCIBus *primary_bus;
164db1015e9SEduardo Habkost };
165527773eeSEric Auger 
166db1015e9SEduardo Habkost struct SMMUBaseClass {
167527773eeSEric Auger     /* <private> */
168527773eeSEric Auger     SysBusDeviceClass parent_class;
169527773eeSEric Auger 
170527773eeSEric Auger     /*< public >*/
171527773eeSEric Auger 
172527773eeSEric Auger     DeviceRealize parent_realize;
173527773eeSEric Auger 
174db1015e9SEduardo Habkost };
175527773eeSEric Auger 
176527773eeSEric Auger #define TYPE_ARM_SMMU "arm-smmu"
177a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(SMMUState, SMMUBaseClass, ARM_SMMU)
178527773eeSEric Auger 
179cac994efSEric Auger /* Return the SMMUPciBus handle associated to a PCI bus number */
180cac994efSEric Auger SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num);
181cac994efSEric Auger 
182cac994efSEric Auger /* Return the stream ID of an SMMU device */
smmu_get_sid(SMMUDevice * sdev)183cac994efSEric Auger static inline uint16_t smmu_get_sid(SMMUDevice *sdev)
184cac994efSEric Auger {
185cac994efSEric Auger     return PCI_BUILD_BDF(pci_bus_num(sdev->bus), sdev->devfn);
186cac994efSEric Auger }
18793641948SEric Auger 
18893641948SEric Auger /**
18993641948SEric Auger  * smmu_ptw - Perform the page table walk for a given iova / access flags
19093641948SEric Auger  * pair, according to @cfg translation config
19193641948SEric Auger  */
192f42a0a57SMostafa Saleh int smmu_ptw(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t iova,
193f42a0a57SMostafa Saleh              IOMMUAccessFlags perm, SMMUTLBEntry *tlbe,
194f42a0a57SMostafa Saleh              SMMUPTWEventInfo *info);
195a9e3f4c1SMostafa Saleh 
196a9e3f4c1SMostafa Saleh /*
197a9e3f4c1SMostafa Saleh  * smmu_translate - Look for a translation in TLB, if not, do a PTW.
198a9e3f4c1SMostafa Saleh  * Returns NULL on PTW error or incase of TLB permission errors.
199a9e3f4c1SMostafa Saleh  */
200a9e3f4c1SMostafa Saleh SMMUTLBEntry *smmu_translate(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t addr,
201a9e3f4c1SMostafa Saleh                              IOMMUAccessFlags flag, SMMUPTWEventInfo *info);
202a9e3f4c1SMostafa Saleh 
20393641948SEric Auger /**
20493641948SEric Auger  * select_tt - compute which translation table shall be used according to
20593641948SEric Auger  * the input iova and translation config and return the TT specific info
20693641948SEric Auger  */
20793641948SEric Auger SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova);
20893641948SEric Auger 
20969970205SNicolin Chen /* Return the SMMUDevice associated to @sid, or NULL if none */
21069970205SNicolin Chen SMMUDevice *smmu_find_sdev(SMMUState *s, uint32_t sid);
21132cfd7f3SEric Auger 
212cc27ed81SEric Auger #define SMMU_IOTLB_MAX_SIZE 256
213cc27ed81SEric Auger 
2149e54dee7SEric Auger SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
2159e54dee7SEric Auger                                 SMMUTransTableInfo *tt, hwaddr iova);
216a7550158SEric Auger void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry);
217d8838226SMostafa Saleh SMMUIOTLBKey smmu_get_iotlb_key(int asid, int vmid, uint64_t iova,
2189e54dee7SEric Auger                                 uint8_t tg, uint8_t level);
219cc27ed81SEric Auger void smmu_iotlb_inv_all(SMMUState *s);
220eb41313cSMostafa Saleh void smmu_iotlb_inv_asid_vmid(SMMUState *s, int asid, int vmid);
221d8838226SMostafa Saleh void smmu_iotlb_inv_vmid(SMMUState *s, int vmid);
222b8fa4c23SMostafa Saleh void smmu_iotlb_inv_vmid_s1(SMMUState *s, int vmid);
2232eaeb7d5SMostafa Saleh void smmu_iotlb_inv_iova(SMMUState *s, int asid, int vmid, dma_addr_t iova,
224d5291561SEric Auger                          uint8_t tg, uint64_t num_pages, uint8_t ttl);
2251ea8a6f5SMostafa Saleh void smmu_iotlb_inv_ipa(SMMUState *s, int vmid, dma_addr_t ipa, uint8_t tg,
2261ea8a6f5SMostafa Saleh                         uint64_t num_pages, uint8_t ttl);
227*8881b691SJianChunfu void smmu_configs_inv_sid_range(SMMUState *s, SMMUSIDRange sid_range);
228832e4222SEric Auger /* Unmap the range of all the notifiers registered to any IOMMU mr */
229832e4222SEric Auger void smmu_inv_notifiers_all(SMMUState *s);
230832e4222SEric Auger 
2316834c3f4SMarkus Armbruster #endif /* HW_ARM_SMMU_COMMON_H */
232