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" 24*db1015e9SEduardo 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 #define SMMU_MAX_VA_BITS 48 31527773eeSEric Auger 32527773eeSEric Auger /* 33527773eeSEric Auger * Page table walk error types 34527773eeSEric Auger */ 35527773eeSEric Auger typedef enum { 36527773eeSEric Auger SMMU_PTW_ERR_NONE, 37527773eeSEric Auger SMMU_PTW_ERR_WALK_EABT, /* Translation walk external abort */ 38527773eeSEric Auger SMMU_PTW_ERR_TRANSLATION, /* Translation fault */ 39527773eeSEric Auger SMMU_PTW_ERR_ADDR_SIZE, /* Address Size fault */ 40527773eeSEric Auger SMMU_PTW_ERR_ACCESS, /* Access fault */ 41527773eeSEric Auger SMMU_PTW_ERR_PERMISSION, /* Permission fault */ 42527773eeSEric Auger } SMMUPTWEventType; 43527773eeSEric Auger 44527773eeSEric Auger typedef struct SMMUPTWEventInfo { 45527773eeSEric Auger SMMUPTWEventType type; 46527773eeSEric Auger dma_addr_t addr; /* fetched address that induced an abort, if any */ 47527773eeSEric Auger } SMMUPTWEventInfo; 48527773eeSEric Auger 49527773eeSEric Auger typedef struct SMMUTransTableInfo { 50527773eeSEric Auger bool disabled; /* is the translation table disabled? */ 51527773eeSEric Auger uint64_t ttb; /* TT base address */ 52527773eeSEric Auger uint8_t tsz; /* input range, ie. 2^(64 -tsz)*/ 53527773eeSEric Auger uint8_t granule_sz; /* granule page shift */ 54e7c3b9d9SEric Auger bool had; /* hierarchical attribute disable */ 55527773eeSEric Auger } SMMUTransTableInfo; 56527773eeSEric Auger 57a7550158SEric Auger typedef struct SMMUTLBEntry { 58a7550158SEric Auger IOMMUTLBEntry entry; 59a7550158SEric Auger uint8_t level; 60a7550158SEric Auger uint8_t granule; 61a7550158SEric Auger } SMMUTLBEntry; 62a7550158SEric Auger 63527773eeSEric Auger /* 64527773eeSEric Auger * Generic structure populated by derived SMMU devices 65527773eeSEric Auger * after decoding the configuration information and used as 66527773eeSEric Auger * input to the page table walk 67527773eeSEric Auger */ 68527773eeSEric Auger typedef struct SMMUTransCfg { 69527773eeSEric Auger int stage; /* translation stage */ 70527773eeSEric Auger bool aa64; /* arch64 or aarch32 translation table */ 71527773eeSEric Auger bool disabled; /* smmu is disabled */ 72527773eeSEric Auger bool bypassed; /* translation is bypassed */ 73527773eeSEric Auger bool aborted; /* translation is aborted */ 74527773eeSEric Auger uint64_t ttb; /* TT base address */ 75527773eeSEric Auger uint8_t oas; /* output address width */ 76527773eeSEric Auger uint8_t tbi; /* Top Byte Ignore */ 77527773eeSEric Auger uint16_t asid; 78527773eeSEric Auger SMMUTransTableInfo tt[2]; 79cc27ed81SEric Auger uint32_t iotlb_hits; /* counts IOTLB hits for this asid */ 80cc27ed81SEric Auger uint32_t iotlb_misses; /* counts IOTLB misses for this asid */ 81527773eeSEric Auger } SMMUTransCfg; 82527773eeSEric Auger 83527773eeSEric Auger typedef struct SMMUDevice { 84527773eeSEric Auger void *smmu; 85527773eeSEric Auger PCIBus *bus; 86527773eeSEric Auger int devfn; 87527773eeSEric Auger IOMMUMemoryRegion iommu; 88527773eeSEric Auger AddressSpace as; 8932cfd7f3SEric Auger uint32_t cfg_cache_hits; 9032cfd7f3SEric Auger uint32_t cfg_cache_misses; 91c6370441SEric Auger QLIST_ENTRY(SMMUDevice) next; 92527773eeSEric Auger } SMMUDevice; 93527773eeSEric Auger 94527773eeSEric Auger typedef struct SMMUPciBus { 95527773eeSEric Auger PCIBus *bus; 96f7795e40SPhilippe Mathieu-Daudé SMMUDevice *pbdev[]; /* Parent array is sparse, so dynamically alloc */ 97527773eeSEric Auger } SMMUPciBus; 98527773eeSEric Auger 99cc27ed81SEric Auger typedef struct SMMUIOTLBKey { 100cc27ed81SEric Auger uint64_t iova; 101cc27ed81SEric Auger uint16_t asid; 1029e54dee7SEric Auger uint8_t tg; 1039e54dee7SEric Auger uint8_t level; 104cc27ed81SEric Auger } SMMUIOTLBKey; 105cc27ed81SEric Auger 106*db1015e9SEduardo Habkost struct SMMUState { 107527773eeSEric Auger /* <private> */ 108527773eeSEric Auger SysBusDevice dev; 109527773eeSEric Auger const char *mrtypename; 110527773eeSEric Auger MemoryRegion iomem; 111527773eeSEric Auger 112527773eeSEric Auger GHashTable *smmu_pcibus_by_busptr; 113527773eeSEric Auger GHashTable *configs; /* cache for configuration data */ 114527773eeSEric Auger GHashTable *iotlb; 115527773eeSEric Auger SMMUPciBus *smmu_pcibus_by_bus_num[SMMU_PCI_BUS_MAX]; 116527773eeSEric Auger PCIBus *pci_bus; 117c6370441SEric Auger QLIST_HEAD(, SMMUDevice) devices_with_notifiers; 118527773eeSEric Auger uint8_t bus_num; 119527773eeSEric Auger PCIBus *primary_bus; 120*db1015e9SEduardo Habkost }; 121*db1015e9SEduardo Habkost typedef struct SMMUState SMMUState; 122527773eeSEric Auger 123*db1015e9SEduardo Habkost struct SMMUBaseClass { 124527773eeSEric Auger /* <private> */ 125527773eeSEric Auger SysBusDeviceClass parent_class; 126527773eeSEric Auger 127527773eeSEric Auger /*< public >*/ 128527773eeSEric Auger 129527773eeSEric Auger DeviceRealize parent_realize; 130527773eeSEric Auger 131*db1015e9SEduardo Habkost }; 132*db1015e9SEduardo Habkost typedef struct SMMUBaseClass SMMUBaseClass; 133527773eeSEric Auger 134527773eeSEric Auger #define TYPE_ARM_SMMU "arm-smmu" 135527773eeSEric Auger #define ARM_SMMU(obj) OBJECT_CHECK(SMMUState, (obj), TYPE_ARM_SMMU) 136527773eeSEric Auger #define ARM_SMMU_CLASS(klass) \ 137527773eeSEric Auger OBJECT_CLASS_CHECK(SMMUBaseClass, (klass), TYPE_ARM_SMMU) 138527773eeSEric Auger #define ARM_SMMU_GET_CLASS(obj) \ 139527773eeSEric Auger OBJECT_GET_CLASS(SMMUBaseClass, (obj), TYPE_ARM_SMMU) 140527773eeSEric Auger 141cac994efSEric Auger /* Return the SMMUPciBus handle associated to a PCI bus number */ 142cac994efSEric Auger SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num); 143cac994efSEric Auger 144cac994efSEric Auger /* Return the stream ID of an SMMU device */ 145cac994efSEric Auger static inline uint16_t smmu_get_sid(SMMUDevice *sdev) 146cac994efSEric Auger { 147cac994efSEric Auger return PCI_BUILD_BDF(pci_bus_num(sdev->bus), sdev->devfn); 148cac994efSEric Auger } 14993641948SEric Auger 15093641948SEric Auger /** 15193641948SEric Auger * smmu_ptw - Perform the page table walk for a given iova / access flags 15293641948SEric Auger * pair, according to @cfg translation config 15393641948SEric Auger */ 15493641948SEric Auger int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm, 155a7550158SEric Auger SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info); 15693641948SEric Auger 15793641948SEric Auger /** 15893641948SEric Auger * select_tt - compute which translation table shall be used according to 15993641948SEric Auger * the input iova and translation config and return the TT specific info 16093641948SEric Auger */ 16193641948SEric Auger SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova); 16293641948SEric Auger 16332cfd7f3SEric Auger /* Return the iommu mr associated to @sid, or NULL if none */ 16432cfd7f3SEric Auger IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid); 16532cfd7f3SEric Auger 166cc27ed81SEric Auger #define SMMU_IOTLB_MAX_SIZE 256 167cc27ed81SEric Auger 1689e54dee7SEric Auger SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg, 1699e54dee7SEric Auger SMMUTransTableInfo *tt, hwaddr iova); 170a7550158SEric Auger void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry); 1719e54dee7SEric Auger SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova, 1729e54dee7SEric Auger uint8_t tg, uint8_t level); 173cc27ed81SEric Auger void smmu_iotlb_inv_all(SMMUState *s); 174cc27ed81SEric Auger void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid); 175d5291561SEric Auger void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova, 176d5291561SEric Auger uint8_t tg, uint64_t num_pages, uint8_t ttl); 177cc27ed81SEric Auger 178832e4222SEric Auger /* Unmap the range of all the notifiers registered to any IOMMU mr */ 179832e4222SEric Auger void smmu_inv_notifiers_all(SMMUState *s); 180832e4222SEric Auger 181832e4222SEric Auger /* Unmap the range of all the notifiers registered to @mr */ 182832e4222SEric Auger void smmu_inv_notifiers_mr(IOMMUMemoryRegion *mr); 183832e4222SEric Auger 1846834c3f4SMarkus Armbruster #endif /* HW_ARM_SMMU_COMMON_H */ 185