xref: /qemu/include/hw/arm/smmu-common.h (revision cac994ef43b128c80c56b4cd4dd9d8af0f95da3f)
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"
24527773eeSEric Auger 
25527773eeSEric Auger #define SMMU_PCI_BUS_MAX      256
26527773eeSEric Auger #define SMMU_PCI_DEVFN_MAX    256
27527773eeSEric Auger 
28527773eeSEric Auger #define SMMU_MAX_VA_BITS      48
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 */
52527773eeSEric Auger } SMMUTransTableInfo;
53527773eeSEric Auger 
54527773eeSEric Auger /*
55527773eeSEric Auger  * Generic structure populated by derived SMMU devices
56527773eeSEric Auger  * after decoding the configuration information and used as
57527773eeSEric Auger  * input to the page table walk
58527773eeSEric Auger  */
59527773eeSEric Auger typedef struct SMMUTransCfg {
60527773eeSEric Auger     int stage;                 /* translation stage */
61527773eeSEric Auger     bool aa64;                 /* arch64 or aarch32 translation table */
62527773eeSEric Auger     bool disabled;             /* smmu is disabled */
63527773eeSEric Auger     bool bypassed;             /* translation is bypassed */
64527773eeSEric Auger     bool aborted;              /* translation is aborted */
65527773eeSEric Auger     uint64_t ttb;              /* TT base address */
66527773eeSEric Auger     uint8_t oas;               /* output address width */
67527773eeSEric Auger     uint8_t tbi;               /* Top Byte Ignore */
68527773eeSEric Auger     uint16_t asid;
69527773eeSEric Auger     SMMUTransTableInfo tt[2];
70527773eeSEric Auger } SMMUTransCfg;
71527773eeSEric Auger 
72527773eeSEric Auger typedef struct SMMUDevice {
73527773eeSEric Auger     void               *smmu;
74527773eeSEric Auger     PCIBus             *bus;
75527773eeSEric Auger     int                devfn;
76527773eeSEric Auger     IOMMUMemoryRegion  iommu;
77527773eeSEric Auger     AddressSpace       as;
78527773eeSEric Auger } SMMUDevice;
79527773eeSEric Auger 
80527773eeSEric Auger typedef struct SMMUNotifierNode {
81527773eeSEric Auger     SMMUDevice *sdev;
82527773eeSEric Auger     QLIST_ENTRY(SMMUNotifierNode) next;
83527773eeSEric Auger } SMMUNotifierNode;
84527773eeSEric Auger 
85527773eeSEric Auger typedef struct SMMUPciBus {
86527773eeSEric Auger     PCIBus       *bus;
87527773eeSEric Auger     SMMUDevice   *pbdev[0]; /* Parent array is sparse, so dynamically alloc */
88527773eeSEric Auger } SMMUPciBus;
89527773eeSEric Auger 
90527773eeSEric Auger typedef struct SMMUState {
91527773eeSEric Auger     /* <private> */
92527773eeSEric Auger     SysBusDevice  dev;
93527773eeSEric Auger     const char *mrtypename;
94527773eeSEric Auger     MemoryRegion iomem;
95527773eeSEric Auger 
96527773eeSEric Auger     GHashTable *smmu_pcibus_by_busptr;
97527773eeSEric Auger     GHashTable *configs; /* cache for configuration data */
98527773eeSEric Auger     GHashTable *iotlb;
99527773eeSEric Auger     SMMUPciBus *smmu_pcibus_by_bus_num[SMMU_PCI_BUS_MAX];
100527773eeSEric Auger     PCIBus *pci_bus;
101527773eeSEric Auger     QLIST_HEAD(, SMMUNotifierNode) notifiers_list;
102527773eeSEric Auger     uint8_t bus_num;
103527773eeSEric Auger     PCIBus *primary_bus;
104527773eeSEric Auger } SMMUState;
105527773eeSEric Auger 
106527773eeSEric Auger typedef struct {
107527773eeSEric Auger     /* <private> */
108527773eeSEric Auger     SysBusDeviceClass parent_class;
109527773eeSEric Auger 
110527773eeSEric Auger     /*< public >*/
111527773eeSEric Auger 
112527773eeSEric Auger     DeviceRealize parent_realize;
113527773eeSEric Auger 
114527773eeSEric Auger } SMMUBaseClass;
115527773eeSEric Auger 
116527773eeSEric Auger #define TYPE_ARM_SMMU "arm-smmu"
117527773eeSEric Auger #define ARM_SMMU(obj) OBJECT_CHECK(SMMUState, (obj), TYPE_ARM_SMMU)
118527773eeSEric Auger #define ARM_SMMU_CLASS(klass)                                    \
119527773eeSEric Auger     OBJECT_CLASS_CHECK(SMMUBaseClass, (klass), TYPE_ARM_SMMU)
120527773eeSEric Auger #define ARM_SMMU_GET_CLASS(obj)                              \
121527773eeSEric Auger     OBJECT_GET_CLASS(SMMUBaseClass, (obj), TYPE_ARM_SMMU)
122527773eeSEric Auger 
123*cac994efSEric Auger /* Return the SMMUPciBus handle associated to a PCI bus number */
124*cac994efSEric Auger SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num);
125*cac994efSEric Auger 
126*cac994efSEric Auger /* Return the stream ID of an SMMU device */
127*cac994efSEric Auger static inline uint16_t smmu_get_sid(SMMUDevice *sdev)
128*cac994efSEric Auger {
129*cac994efSEric Auger     return PCI_BUILD_BDF(pci_bus_num(sdev->bus), sdev->devfn);
130*cac994efSEric Auger }
131527773eeSEric Auger #endif  /* HW_ARM_SMMU_COMMON */
132