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 30*bcc919e7SMostafa Saleh /* VMSAv8-64 Translation constants and functions */ 31*bcc919e7SMostafa Saleh #define VMSA_LEVELS 4 32*bcc919e7SMostafa Saleh 33*bcc919e7SMostafa Saleh #define VMSA_STRIDE(gran) ((gran) - VMSA_LEVELS + 1) 34*bcc919e7SMostafa Saleh #define VMSA_BIT_LVL(isz, strd, lvl) ((isz) - (strd) * \ 35*bcc919e7SMostafa Saleh (VMSA_LEVELS - (lvl))) 36*bcc919e7SMostafa Saleh #define VMSA_IDXMSK(isz, strd, lvl) ((1ULL << \ 37*bcc919e7SMostafa Saleh VMSA_BIT_LVL(isz, strd, lvl)) - 1) 38*bcc919e7SMostafa Saleh 39527773eeSEric Auger /* 40527773eeSEric Auger * Page table walk error types 41527773eeSEric Auger */ 42527773eeSEric Auger typedef enum { 43527773eeSEric Auger SMMU_PTW_ERR_NONE, 44527773eeSEric Auger SMMU_PTW_ERR_WALK_EABT, /* Translation walk external abort */ 45527773eeSEric Auger SMMU_PTW_ERR_TRANSLATION, /* Translation fault */ 46527773eeSEric Auger SMMU_PTW_ERR_ADDR_SIZE, /* Address Size fault */ 47527773eeSEric Auger SMMU_PTW_ERR_ACCESS, /* Access fault */ 48527773eeSEric Auger SMMU_PTW_ERR_PERMISSION, /* Permission fault */ 49527773eeSEric Auger } SMMUPTWEventType; 50527773eeSEric Auger 51527773eeSEric Auger typedef struct SMMUPTWEventInfo { 52*bcc919e7SMostafa Saleh int stage; 53527773eeSEric Auger SMMUPTWEventType type; 54527773eeSEric Auger dma_addr_t addr; /* fetched address that induced an abort, if any */ 55527773eeSEric Auger } SMMUPTWEventInfo; 56527773eeSEric Auger 57527773eeSEric Auger typedef struct SMMUTransTableInfo { 58527773eeSEric Auger bool disabled; /* is the translation table disabled? */ 59527773eeSEric Auger uint64_t ttb; /* TT base address */ 60527773eeSEric Auger uint8_t tsz; /* input range, ie. 2^(64 -tsz)*/ 61527773eeSEric Auger uint8_t granule_sz; /* granule page shift */ 62e7c3b9d9SEric Auger bool had; /* hierarchical attribute disable */ 63527773eeSEric Auger } SMMUTransTableInfo; 64527773eeSEric Auger 65a7550158SEric Auger typedef struct SMMUTLBEntry { 66a7550158SEric Auger IOMMUTLBEntry entry; 67a7550158SEric Auger uint8_t level; 68a7550158SEric Auger uint8_t granule; 69a7550158SEric Auger } SMMUTLBEntry; 70a7550158SEric Auger 713b736c61SMostafa Saleh /* Stage-2 configuration. */ 723b736c61SMostafa Saleh typedef struct SMMUS2Cfg { 733b736c61SMostafa Saleh uint8_t tsz; /* Size of IPA input region (S2T0SZ) */ 743b736c61SMostafa Saleh uint8_t sl0; /* Start level of translation (S2SL0) */ 753b736c61SMostafa Saleh bool affd; /* AF Fault Disable (S2AFFD) */ 763b736c61SMostafa Saleh bool record_faults; /* Record fault events (S2R) */ 773b736c61SMostafa Saleh uint8_t granule_sz; /* Granule page shift (based on S2TG) */ 783b736c61SMostafa Saleh uint8_t eff_ps; /* Effective PA output range (based on S2PS) */ 793b736c61SMostafa Saleh uint16_t vmid; /* Virtual Machine ID (S2VMID) */ 803b736c61SMostafa Saleh uint64_t vttb; /* Address of translation table base (S2TTB) */ 813b736c61SMostafa Saleh } SMMUS2Cfg; 823b736c61SMostafa Saleh 83527773eeSEric Auger /* 84527773eeSEric Auger * Generic structure populated by derived SMMU devices 85527773eeSEric Auger * after decoding the configuration information and used as 86527773eeSEric Auger * input to the page table walk 87527773eeSEric Auger */ 88527773eeSEric Auger typedef struct SMMUTransCfg { 893b736c61SMostafa Saleh /* Shared fields between stage-1 and stage-2. */ 90527773eeSEric Auger int stage; /* translation stage */ 91527773eeSEric Auger bool disabled; /* smmu is disabled */ 92527773eeSEric Auger bool bypassed; /* translation is bypassed */ 93527773eeSEric Auger bool aborted; /* translation is aborted */ 943b736c61SMostafa Saleh uint32_t iotlb_hits; /* counts IOTLB hits */ 953b736c61SMostafa Saleh uint32_t iotlb_misses; /* counts IOTLB misses*/ 963b736c61SMostafa Saleh /* Used by stage-1 only. */ 973b736c61SMostafa Saleh bool aa64; /* arch64 or aarch32 translation table */ 98ced71694SJean-Philippe Brucker bool record_faults; /* record fault events */ 99527773eeSEric Auger uint64_t ttb; /* TT base address */ 100527773eeSEric Auger uint8_t oas; /* output address width */ 101527773eeSEric Auger uint8_t tbi; /* Top Byte Ignore */ 102527773eeSEric Auger uint16_t asid; 103527773eeSEric Auger SMMUTransTableInfo tt[2]; 1043b736c61SMostafa Saleh /* Used by stage-2 only. */ 1053b736c61SMostafa Saleh struct SMMUS2Cfg s2cfg; 106527773eeSEric Auger } SMMUTransCfg; 107527773eeSEric Auger 108527773eeSEric Auger typedef struct SMMUDevice { 109527773eeSEric Auger void *smmu; 110527773eeSEric Auger PCIBus *bus; 111527773eeSEric Auger int devfn; 112527773eeSEric Auger IOMMUMemoryRegion iommu; 113527773eeSEric Auger AddressSpace as; 11432cfd7f3SEric Auger uint32_t cfg_cache_hits; 11532cfd7f3SEric Auger uint32_t cfg_cache_misses; 116c6370441SEric Auger QLIST_ENTRY(SMMUDevice) next; 117527773eeSEric Auger } SMMUDevice; 118527773eeSEric Auger 119527773eeSEric Auger typedef struct SMMUPciBus { 120527773eeSEric Auger PCIBus *bus; 121f7795e40SPhilippe Mathieu-Daudé SMMUDevice *pbdev[]; /* Parent array is sparse, so dynamically alloc */ 122527773eeSEric Auger } SMMUPciBus; 123527773eeSEric Auger 124cc27ed81SEric Auger typedef struct SMMUIOTLBKey { 125cc27ed81SEric Auger uint64_t iova; 126cc27ed81SEric Auger uint16_t asid; 1279e54dee7SEric Auger uint8_t tg; 1289e54dee7SEric Auger uint8_t level; 129cc27ed81SEric Auger } SMMUIOTLBKey; 130cc27ed81SEric Auger 131db1015e9SEduardo Habkost struct SMMUState { 132527773eeSEric Auger /* <private> */ 133527773eeSEric Auger SysBusDevice dev; 134527773eeSEric Auger const char *mrtypename; 135527773eeSEric Auger MemoryRegion iomem; 136527773eeSEric Auger 137527773eeSEric Auger GHashTable *smmu_pcibus_by_busptr; 138527773eeSEric Auger GHashTable *configs; /* cache for configuration data */ 139527773eeSEric Auger GHashTable *iotlb; 140527773eeSEric Auger SMMUPciBus *smmu_pcibus_by_bus_num[SMMU_PCI_BUS_MAX]; 141527773eeSEric Auger PCIBus *pci_bus; 142c6370441SEric Auger QLIST_HEAD(, SMMUDevice) devices_with_notifiers; 143527773eeSEric Auger uint8_t bus_num; 144527773eeSEric Auger PCIBus *primary_bus; 145db1015e9SEduardo Habkost }; 146527773eeSEric Auger 147db1015e9SEduardo Habkost struct SMMUBaseClass { 148527773eeSEric Auger /* <private> */ 149527773eeSEric Auger SysBusDeviceClass parent_class; 150527773eeSEric Auger 151527773eeSEric Auger /*< public >*/ 152527773eeSEric Auger 153527773eeSEric Auger DeviceRealize parent_realize; 154527773eeSEric Auger 155db1015e9SEduardo Habkost }; 156527773eeSEric Auger 157527773eeSEric Auger #define TYPE_ARM_SMMU "arm-smmu" 158a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(SMMUState, SMMUBaseClass, ARM_SMMU) 159527773eeSEric Auger 160cac994efSEric Auger /* Return the SMMUPciBus handle associated to a PCI bus number */ 161cac994efSEric Auger SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num); 162cac994efSEric Auger 163cac994efSEric Auger /* Return the stream ID of an SMMU device */ 164cac994efSEric Auger static inline uint16_t smmu_get_sid(SMMUDevice *sdev) 165cac994efSEric Auger { 166cac994efSEric Auger return PCI_BUILD_BDF(pci_bus_num(sdev->bus), sdev->devfn); 167cac994efSEric Auger } 16893641948SEric Auger 16993641948SEric Auger /** 17093641948SEric Auger * smmu_ptw - Perform the page table walk for a given iova / access flags 17193641948SEric Auger * pair, according to @cfg translation config 17293641948SEric Auger */ 17393641948SEric Auger int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm, 174a7550158SEric Auger SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info); 17593641948SEric Auger 17693641948SEric Auger /** 17793641948SEric Auger * select_tt - compute which translation table shall be used according to 17893641948SEric Auger * the input iova and translation config and return the TT specific info 17993641948SEric Auger */ 18093641948SEric Auger SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova); 18193641948SEric Auger 18232cfd7f3SEric Auger /* Return the iommu mr associated to @sid, or NULL if none */ 18332cfd7f3SEric Auger IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid); 18432cfd7f3SEric Auger 185cc27ed81SEric Auger #define SMMU_IOTLB_MAX_SIZE 256 186cc27ed81SEric Auger 1879e54dee7SEric Auger SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, 1889e54dee7SEric Auger SMMUTransTableInfo *tt, hwaddr iova); 189a7550158SEric Auger void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry); 1909e54dee7SEric Auger SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova, 1919e54dee7SEric Auger uint8_t tg, uint8_t level); 192cc27ed81SEric Auger void smmu_iotlb_inv_all(SMMUState *s); 193cc27ed81SEric Auger void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid); 194d5291561SEric Auger void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova, 195d5291561SEric Auger uint8_t tg, uint64_t num_pages, uint8_t ttl); 196cc27ed81SEric Auger 197832e4222SEric Auger /* Unmap the range of all the notifiers registered to any IOMMU mr */ 198832e4222SEric Auger void smmu_inv_notifiers_all(SMMUState *s); 199832e4222SEric Auger 2006834c3f4SMarkus Armbruster #endif /* HW_ARM_SMMU_COMMON_H */ 201