qemu/include/hw/arm/smmu-common.h
<<
>>
Prefs
   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
  19#ifndef HW_ARM_SMMU_COMMON_H
  20#define HW_ARM_SMMU_COMMON_H
  21
  22#include "hw/sysbus.h"
  23#include "hw/pci/pci.h"
  24#include "qom/object.h"
  25
  26#define SMMU_PCI_BUS_MAX      256
  27#define SMMU_PCI_DEVFN_MAX    256
  28#define SMMU_PCI_DEVFN(sid)   (sid & 0xFF)
  29
  30#define SMMU_MAX_VA_BITS      48
  31
  32#define SMMU_MAX_TBU          16
  33
  34/*
  35 * Page table walk error types
  36 */
  37typedef enum {
  38    SMMU_PTW_ERR_NONE,
  39    SMMU_PTW_ERR_WALK_EABT,   /* Translation walk external abort */
  40    SMMU_PTW_ERR_TRANSLATION, /* Translation fault */
  41    SMMU_PTW_ERR_ADDR_SIZE,   /* Address Size fault */
  42    SMMU_PTW_ERR_ACCESS,      /* Access fault */
  43    SMMU_PTW_ERR_PERMISSION,  /* Permission fault */
  44} SMMUPTWEventType;
  45
  46typedef struct SMMUPTWEventInfo {
  47    SMMUPTWEventType type;
  48    dma_addr_t addr; /* fetched address that induced an abort, if any */
  49} SMMUPTWEventInfo;
  50
  51typedef struct SMMUTransTableInfo {
  52    bool disabled;             /* is the translation table disabled? */
  53    uint64_t ttb;              /* TT base address */
  54    uint8_t tsz;               /* input range, ie. 2^(64 -tsz)*/
  55    uint8_t granule_sz;        /* granule page shift */
  56    bool had;                  /* hierarchical attribute disable */
  57} SMMUTransTableInfo;
  58
  59typedef struct SMMUTLBEntry {
  60    IOMMUTLBEntry entry;
  61    uint8_t level;
  62    uint8_t granule;
  63} SMMUTLBEntry;
  64
  65/*
  66 * Generic structure populated by derived SMMU devices
  67 * after decoding the configuration information and used as
  68 * input to the page table walk
  69 */
  70typedef struct SMMUTransCfg {
  71    int stage;                 /* translation stage */
  72    bool aa64;                 /* arch64 or aarch32 translation table */
  73    bool disabled;             /* smmu is disabled */
  74    bool bypassed;             /* translation is bypassed */
  75    bool aborted;              /* translation is aborted */
  76    bool affd;                 /* Access Flag Fault Disabled */
  77    uint64_t ttb;              /* TT base address */
  78    uint8_t s2_sl0;            /* S2 Start level */
  79    uint8_t oas;               /* output address width */
  80    uint8_t tbi;               /* Top Byte Ignore */
  81    uint32_t asid;
  82    SMMUTransTableInfo tt[2];
  83    uint32_t iotlb_hits;       /* counts IOTLB hits for this asid */
  84    uint32_t iotlb_misses;     /* counts IOTLB misses for this asid */
  85} SMMUTransCfg;
  86
  87typedef struct SMMUDevice {
  88    void               *smmu;
  89    PCIBus             *bus;
  90    int                devfn;
  91    IOMMUMemoryRegion  iommu;
  92    AddressSpace       as;
  93    AddressSpace       as_downstream;
  94    uint32_t           cfg_cache_hits;
  95    uint32_t           cfg_cache_misses;
  96    QLIST_ENTRY(SMMUDevice) next;
  97} SMMUDevice;
  98
  99typedef struct SMMUPciBus {
 100    PCIBus       *bus;
 101    SMMUDevice   *pbdev[]; /* Parent array is sparse, so dynamically alloc */
 102} SMMUPciBus;
 103
 104typedef struct SMMUIOTLBKey {
 105    uint64_t iova;
 106    uint16_t asid;
 107    uint8_t tg;
 108    uint8_t level;
 109} SMMUIOTLBKey;
 110
 111struct SMMUState {
 112    /* <private> */
 113    SysBusDevice  dev;
 114    const char *mrtypename;
 115    MemoryRegion iomem;
 116
 117    GHashTable *smmu_pcibus_by_busptr;
 118    GHashTable *configs; /* cache for configuration data */
 119    GHashTable *iotlb;
 120    SMMUPciBus *smmu_pcibus_by_bus_num[SMMU_PCI_BUS_MAX];
 121    PCIBus *pci_bus;
 122    QLIST_HEAD(, SMMUDevice) devices_with_notifiers;
 123    uint8_t bus_num;
 124    PCIBus *primary_bus;
 125
 126    struct {
 127        MemoryRegion *mr;
 128        SMMUDevice *sdev;
 129    } tbu[SMMU_MAX_TBU];
 130};
 131
 132struct SMMUBaseClass {
 133    /* <private> */
 134    SysBusDeviceClass parent_class;
 135
 136    /*< public >*/
 137
 138    DeviceRealize parent_realize;
 139
 140};
 141
 142#define TYPE_ARM_SMMU "arm-smmu"
 143OBJECT_DECLARE_TYPE(SMMUState, SMMUBaseClass, ARM_SMMU)
 144
 145/* Return the SMMUPciBus handle associated to a PCI bus number */
 146SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num);
 147
 148/* Return the stream ID of an SMMU device */
 149static inline uint16_t smmu_get_sid(SMMUDevice *sdev)
 150{
 151    return PCI_BUILD_BDF(pci_bus_num(sdev->bus), sdev->devfn);
 152}
 153
 154/**
 155 * smmu_ptw - Perform the page table walk for a given iova / access flags
 156 * pair, according to @cfg translation config
 157 */
 158int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm,
 159             SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info);
 160
 161/**
 162 * select_tt - compute which translation table shall be used according to
 163 * the input iova and translation config and return the TT specific info
 164 */
 165SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova);
 166
 167/* Return the iommu mr associated to @sid, or NULL if none */
 168IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid);
 169
 170#define SMMU_IOTLB_MAX_SIZE 256
 171
 172SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
 173                                SMMUTransTableInfo *tt, hwaddr iova);
 174void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry);
 175SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint64_t iova,
 176                                uint8_t tg, uint8_t level);
 177void smmu_iotlb_inv_all(SMMUState *s);
 178void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid);
 179void smmu_iotlb_inv_iova(SMMUState *s, int asid, dma_addr_t iova,
 180                         uint8_t tg, uint64_t num_pages, uint8_t ttl);
 181
 182/* Unmap the range of all the notifiers registered to any IOMMU mr */
 183void smmu_inv_notifiers_all(SMMUState *s);
 184
 185/* Unmap the range of all the notifiers registered to @mr */
 186void smmu_inv_notifiers_mr(IOMMUMemoryRegion *mr);
 187
 188#endif /* HW_ARM_SMMU_COMMON_H */
 189