smmu-common.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * ARM SMMU Support
  3. *
  4. * Copyright (C) 2015-2016 Broadcom Corporation
  5. * Copyright (c) 2017 Red Hat, Inc.
  6. * Written by Prem Mallappa, Eric Auger
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #ifndef HW_ARM_SMMU_COMMON_H
  19. #define HW_ARM_SMMU_COMMON_H
  20. #include "hw/sysbus.h"
  21. #include "hw/pci/pci.h"
  22. #include "qom/object.h"
  23. #define SMMU_PCI_BUS_MAX 256
  24. #define SMMU_PCI_DEVFN_MAX 256
  25. #define SMMU_PCI_DEVFN(sid) (sid & 0xFF)
  26. /* VMSAv8-64 Translation constants and functions */
  27. #define VMSA_LEVELS 4
  28. #define VMSA_MAX_S2_CONCAT 16
  29. #define VMSA_STRIDE(gran) ((gran) - VMSA_LEVELS + 1)
  30. #define VMSA_BIT_LVL(isz, strd, lvl) ((isz) - (strd) * \
  31. (VMSA_LEVELS - (lvl)))
  32. #define VMSA_IDXMSK(isz, strd, lvl) ((1ULL << \
  33. VMSA_BIT_LVL(isz, strd, lvl)) - 1)
  34. #define CACHED_ENTRY_TO_ADDR(ent, addr) ((ent)->entry.translated_addr + \
  35. ((addr) & (ent)->entry.addr_mask))
  36. /*
  37. * Page table walk error types
  38. */
  39. typedef enum {
  40. SMMU_PTW_ERR_NONE,
  41. SMMU_PTW_ERR_WALK_EABT, /* Translation walk external abort */
  42. SMMU_PTW_ERR_TRANSLATION, /* Translation fault */
  43. SMMU_PTW_ERR_ADDR_SIZE, /* Address Size fault */
  44. SMMU_PTW_ERR_ACCESS, /* Access fault */
  45. SMMU_PTW_ERR_PERMISSION, /* Permission fault */
  46. } SMMUPTWEventType;
  47. /* SMMU Stage */
  48. typedef enum {
  49. SMMU_STAGE_1 = 1,
  50. SMMU_STAGE_2,
  51. SMMU_NESTED,
  52. } SMMUStage;
  53. typedef struct SMMUPTWEventInfo {
  54. SMMUStage stage;
  55. SMMUPTWEventType type;
  56. dma_addr_t addr; /* fetched address that induced an abort, if any */
  57. bool is_ipa_descriptor; /* src for fault in nested translation. */
  58. } SMMUPTWEventInfo;
  59. typedef struct SMMUTransTableInfo {
  60. bool disabled; /* is the translation table disabled? */
  61. uint64_t ttb; /* TT base address */
  62. uint8_t tsz; /* input range, ie. 2^(64 -tsz)*/
  63. uint8_t granule_sz; /* granule page shift */
  64. bool had; /* hierarchical attribute disable */
  65. } SMMUTransTableInfo;
  66. typedef struct SMMUTLBEntry {
  67. IOMMUTLBEntry entry;
  68. uint8_t level;
  69. uint8_t granule;
  70. IOMMUAccessFlags parent_perm;
  71. } SMMUTLBEntry;
  72. /* Stage-2 configuration. */
  73. typedef struct SMMUS2Cfg {
  74. uint8_t tsz; /* Size of IPA input region (S2T0SZ) */
  75. uint8_t sl0; /* Start level of translation (S2SL0) */
  76. bool affd; /* AF Fault Disable (S2AFFD) */
  77. bool record_faults; /* Record fault events (S2R) */
  78. uint8_t granule_sz; /* Granule page shift (based on S2TG) */
  79. uint8_t eff_ps; /* Effective PA output range (based on S2PS) */
  80. int vmid; /* Virtual Machine ID (S2VMID) */
  81. uint64_t vttb; /* Address of translation table base (S2TTB) */
  82. } SMMUS2Cfg;
  83. /*
  84. * Generic structure populated by derived SMMU devices
  85. * after decoding the configuration information and used as
  86. * input to the page table walk
  87. */
  88. typedef struct SMMUTransCfg {
  89. /* Shared fields between stage-1 and stage-2. */
  90. SMMUStage stage; /* translation stage */
  91. bool disabled; /* smmu is disabled */
  92. bool bypassed; /* translation is bypassed */
  93. bool aborted; /* translation is aborted */
  94. bool affd; /* AF fault disable */
  95. uint32_t iotlb_hits; /* counts IOTLB hits */
  96. uint32_t iotlb_misses; /* counts IOTLB misses*/
  97. /* Used by stage-1 only. */
  98. bool aa64; /* arch64 or aarch32 translation table */
  99. bool record_faults; /* record fault events */
  100. uint8_t oas; /* output address width */
  101. uint8_t tbi; /* Top Byte Ignore */
  102. int asid;
  103. SMMUTransTableInfo tt[2];
  104. /* Used by stage-2 only. */
  105. struct SMMUS2Cfg s2cfg;
  106. } SMMUTransCfg;
  107. typedef struct SMMUDevice {
  108. void *smmu;
  109. PCIBus *bus;
  110. int devfn;
  111. IOMMUMemoryRegion iommu;
  112. AddressSpace as;
  113. uint32_t cfg_cache_hits;
  114. uint32_t cfg_cache_misses;
  115. QLIST_ENTRY(SMMUDevice) next;
  116. } SMMUDevice;
  117. typedef struct SMMUPciBus {
  118. PCIBus *bus;
  119. SMMUDevice *pbdev[]; /* Parent array is sparse, so dynamically alloc */
  120. } SMMUPciBus;
  121. typedef struct SMMUIOTLBKey {
  122. uint64_t iova;
  123. int asid;
  124. int vmid;
  125. uint8_t tg;
  126. uint8_t level;
  127. } SMMUIOTLBKey;
  128. typedef struct SMMUSIDRange {
  129. uint32_t start;
  130. uint32_t end;
  131. } SMMUSIDRange;
  132. struct SMMUState {
  133. /* <private> */
  134. SysBusDevice dev;
  135. const char *mrtypename;
  136. MemoryRegion iomem;
  137. GHashTable *smmu_pcibus_by_busptr;
  138. GHashTable *configs; /* cache for configuration data */
  139. GHashTable *iotlb;
  140. SMMUPciBus *smmu_pcibus_by_bus_num[SMMU_PCI_BUS_MAX];
  141. PCIBus *pci_bus;
  142. QLIST_HEAD(, SMMUDevice) devices_with_notifiers;
  143. uint8_t bus_num;
  144. PCIBus *primary_bus;
  145. };
  146. struct SMMUBaseClass {
  147. /* <private> */
  148. SysBusDeviceClass parent_class;
  149. /*< public >*/
  150. DeviceRealize parent_realize;
  151. };
  152. #define TYPE_ARM_SMMU "arm-smmu"
  153. OBJECT_DECLARE_TYPE(SMMUState, SMMUBaseClass, ARM_SMMU)
  154. /* Return the SMMUPciBus handle associated to a PCI bus number */
  155. SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num);
  156. /* Return the stream ID of an SMMU device */
  157. static inline uint16_t smmu_get_sid(SMMUDevice *sdev)
  158. {
  159. return PCI_BUILD_BDF(pci_bus_num(sdev->bus), sdev->devfn);
  160. }
  161. /**
  162. * smmu_ptw - Perform the page table walk for a given iova / access flags
  163. * pair, according to @cfg translation config
  164. */
  165. int smmu_ptw(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t iova,
  166. IOMMUAccessFlags perm, SMMUTLBEntry *tlbe,
  167. SMMUPTWEventInfo *info);
  168. /*
  169. * smmu_translate - Look for a translation in TLB, if not, do a PTW.
  170. * Returns NULL on PTW error or incase of TLB permission errors.
  171. */
  172. SMMUTLBEntry *smmu_translate(SMMUState *bs, SMMUTransCfg *cfg, dma_addr_t addr,
  173. IOMMUAccessFlags flag, SMMUPTWEventInfo *info);
  174. /**
  175. * select_tt - compute which translation table shall be used according to
  176. * the input iova and translation config and return the TT specific info
  177. */
  178. SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova);
  179. /* Return the SMMUDevice associated to @sid, or NULL if none */
  180. SMMUDevice *smmu_find_sdev(SMMUState *s, uint32_t sid);
  181. #define SMMU_IOTLB_MAX_SIZE 256
  182. SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
  183. SMMUTransTableInfo *tt, hwaddr iova);
  184. void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry);
  185. SMMUIOTLBKey smmu_get_iotlb_key(int asid, int vmid, uint64_t iova,
  186. uint8_t tg, uint8_t level);
  187. void smmu_iotlb_inv_all(SMMUState *s);
  188. void smmu_iotlb_inv_asid_vmid(SMMUState *s, int asid, int vmid);
  189. void smmu_iotlb_inv_vmid(SMMUState *s, int vmid);
  190. void smmu_iotlb_inv_vmid_s1(SMMUState *s, int vmid);
  191. void smmu_iotlb_inv_iova(SMMUState *s, int asid, int vmid, dma_addr_t iova,
  192. uint8_t tg, uint64_t num_pages, uint8_t ttl);
  193. void smmu_iotlb_inv_ipa(SMMUState *s, int vmid, dma_addr_t ipa, uint8_t tg,
  194. uint64_t num_pages, uint8_t ttl);
  195. void smmu_configs_inv_sid_range(SMMUState *s, SMMUSIDRange sid_range);
  196. /* Unmap the range of all the notifiers registered to any IOMMU mr */
  197. void smmu_inv_notifiers_all(SMMUState *s);
  198. #endif /* HW_ARM_SMMU_COMMON_H */