2018-05-04 20:05:51 +03:00
|
|
|
/*
|
|
|
|
* ARM SMMU Support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015-2016 Broadcom Corporation
|
|
|
|
* Copyright (c) 2017 Red Hat, Inc.
|
|
|
|
* Written by Prem Mallappa, Eric Auger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HW_ARM_SMMU_COMMON_H
|
|
|
|
#define HW_ARM_SMMU_COMMON_H
|
|
|
|
|
|
|
|
#include "hw/sysbus.h"
|
|
|
|
#include "hw/pci/pci.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2018-05-04 20:05:51 +03:00
|
|
|
|
2023-05-25 12:37:49 +03:00
|
|
|
#define SMMU_PCI_BUS_MAX 256
|
|
|
|
#define SMMU_PCI_DEVFN_MAX 256
|
|
|
|
#define SMMU_PCI_DEVFN(sid) (sid & 0xFF)
|
|
|
|
|
|
|
|
/* VMSAv8-64 Translation constants and functions */
|
|
|
|
#define VMSA_LEVELS 4
|
2023-05-25 12:37:50 +03:00
|
|
|
#define VMSA_MAX_S2_CONCAT 16
|
2023-05-25 12:37:49 +03:00
|
|
|
|
|
|
|
#define VMSA_STRIDE(gran) ((gran) - VMSA_LEVELS + 1)
|
|
|
|
#define VMSA_BIT_LVL(isz, strd, lvl) ((isz) - (strd) * \
|
|
|
|
(VMSA_LEVELS - (lvl)))
|
|
|
|
#define VMSA_IDXMSK(isz, strd, lvl) ((1ULL << \
|
|
|
|
VMSA_BIT_LVL(isz, strd, lvl)) - 1)
|
2018-05-04 20:05:51 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Page table walk error types
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
SMMU_PTW_ERR_NONE,
|
|
|
|
SMMU_PTW_ERR_WALK_EABT, /* Translation walk external abort */
|
|
|
|
SMMU_PTW_ERR_TRANSLATION, /* Translation fault */
|
|
|
|
SMMU_PTW_ERR_ADDR_SIZE, /* Address Size fault */
|
|
|
|
SMMU_PTW_ERR_ACCESS, /* Access fault */
|
|
|
|
SMMU_PTW_ERR_PERMISSION, /* Permission fault */
|
|
|
|
} SMMUPTWEventType;
|
|
|
|
|
|
|
|
typedef struct SMMUPTWEventInfo {
|
2023-05-25 12:37:49 +03:00
|
|
|
int stage;
|
2018-05-04 20:05:51 +03:00
|
|
|
SMMUPTWEventType type;
|
|
|
|
dma_addr_t addr; /* fetched address that induced an abort, if any */
|
|
|
|
} SMMUPTWEventInfo;
|
|
|
|
|
|
|
|
typedef struct SMMUTransTableInfo {
|
|
|
|
bool disabled; /* is the translation table disabled? */
|
|
|
|
uint64_t ttb; /* TT base address */
|
|
|
|
uint8_t tsz; /* input range, ie. 2^(64 -tsz)*/
|
|
|
|
uint8_t granule_sz; /* granule page shift */
|
2020-07-28 18:08:14 +03:00
|
|
|
bool had; /* hierarchical attribute disable */
|
2018-05-04 20:05:51 +03:00
|
|
|
} SMMUTransTableInfo;
|
|
|
|
|
2020-07-28 18:08:08 +03:00
|
|
|
typedef struct SMMUTLBEntry {
|
|
|
|
IOMMUTLBEntry entry;
|
|
|
|
uint8_t level;
|
|
|
|
uint8_t granule;
|
|
|
|
} SMMUTLBEntry;
|
|
|
|
|
hw/arm/smmuv3: Update translation config to hold stage-2
In preparation for adding stage-2 support, add a S2 config
struct(SMMUS2Cfg), composed of the following fields and embedded in
the main SMMUTransCfg:
-tsz: Size of IPA input region (S2T0SZ)
-sl0: Start level of translation (S2SL0)
-affd: AF Fault Disable (S2AFFD)
-record_faults: Record fault events (S2R)
-granule_sz: Granule page shift (based on S2TG)
-vmid: Virtual Machine ID (S2VMID)
-vttb: Address of translation table base (S2TTB)
-eff_ps: Effective PA output range (based on S2PS)
They will be used in the next patches in stage-2 address translation.
The fields in SMMUS2Cfg, are reordered to make the shared and stage-1
fields next to each other, this reordering didn't change the struct
size (104 bytes before and after).
Stage-1 only fields: aa64, asid, tt, ttb, tbi, record_faults, oas.
oas is stage-1 output address size. However, it is used to check
input address in case stage-1 is unimplemented or bypassed according
to SMMUv3 manual IHI0070.E "3.4. Address sizes"
Shared fields: stage, disabled, bypassed, aborted, iotlb_*.
No functional change intended.
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Message-id: 20230516203327.2051088-3-smostafa@google.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
2023-05-25 12:37:49 +03:00
|
|
|
/* Stage-2 configuration. */
|
|
|
|
typedef struct SMMUS2Cfg {
|
|
|
|
uint8_t tsz; /* Size of IPA input region (S2T0SZ) */
|
|
|
|
uint8_t sl0; /* Start level of translation (S2SL0) */
|
|
|
|
bool affd; /* AF Fault Disable (S2AFFD) */
|
|
|
|
bool record_faults; /* Record fault events (S2R) */
|
|
|
|
uint8_t granule_sz; /* Granule page shift (based on S2TG) */
|
|
|
|
uint8_t eff_ps; /* Effective PA output range (based on S2PS) */
|
|
|
|
uint16_t vmid; /* Virtual Machine ID (S2VMID) */
|
|
|
|
uint64_t vttb; /* Address of translation table base (S2TTB) */
|
|
|
|
} SMMUS2Cfg;
|
|
|
|
|
2018-05-04 20:05:51 +03:00
|
|
|
/*
|
|
|
|
* Generic structure populated by derived SMMU devices
|
|
|
|
* after decoding the configuration information and used as
|
|
|
|
* input to the page table walk
|
|
|
|
*/
|
|
|
|
typedef struct SMMUTransCfg {
|
hw/arm/smmuv3: Update translation config to hold stage-2
In preparation for adding stage-2 support, add a S2 config
struct(SMMUS2Cfg), composed of the following fields and embedded in
the main SMMUTransCfg:
-tsz: Size of IPA input region (S2T0SZ)
-sl0: Start level of translation (S2SL0)
-affd: AF Fault Disable (S2AFFD)
-record_faults: Record fault events (S2R)
-granule_sz: Granule page shift (based on S2TG)
-vmid: Virtual Machine ID (S2VMID)
-vttb: Address of translation table base (S2TTB)
-eff_ps: Effective PA output range (based on S2PS)
They will be used in the next patches in stage-2 address translation.
The fields in SMMUS2Cfg, are reordered to make the shared and stage-1
fields next to each other, this reordering didn't change the struct
size (104 bytes before and after).
Stage-1 only fields: aa64, asid, tt, ttb, tbi, record_faults, oas.
oas is stage-1 output address size. However, it is used to check
input address in case stage-1 is unimplemented or bypassed according
to SMMUv3 manual IHI0070.E "3.4. Address sizes"
Shared fields: stage, disabled, bypassed, aborted, iotlb_*.
No functional change intended.
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Message-id: 20230516203327.2051088-3-smostafa@google.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
2023-05-25 12:37:49 +03:00
|
|
|
/* Shared fields between stage-1 and stage-2. */
|
2018-05-04 20:05:51 +03:00
|
|
|
int stage; /* translation stage */
|
|
|
|
bool disabled; /* smmu is disabled */
|
|
|
|
bool bypassed; /* translation is bypassed */
|
|
|
|
bool aborted; /* translation is aborted */
|
hw/arm/smmuv3: Update translation config to hold stage-2
In preparation for adding stage-2 support, add a S2 config
struct(SMMUS2Cfg), composed of the following fields and embedded in
the main SMMUTransCfg:
-tsz: Size of IPA input region (S2T0SZ)
-sl0: Start level of translation (S2SL0)
-affd: AF Fault Disable (S2AFFD)
-record_faults: Record fault events (S2R)
-granule_sz: Granule page shift (based on S2TG)
-vmid: Virtual Machine ID (S2VMID)
-vttb: Address of translation table base (S2TTB)
-eff_ps: Effective PA output range (based on S2PS)
They will be used in the next patches in stage-2 address translation.
The fields in SMMUS2Cfg, are reordered to make the shared and stage-1
fields next to each other, this reordering didn't change the struct
size (104 bytes before and after).
Stage-1 only fields: aa64, asid, tt, ttb, tbi, record_faults, oas.
oas is stage-1 output address size. However, it is used to check
input address in case stage-1 is unimplemented or bypassed according
to SMMUv3 manual IHI0070.E "3.4. Address sizes"
Shared fields: stage, disabled, bypassed, aborted, iotlb_*.
No functional change intended.
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Message-id: 20230516203327.2051088-3-smostafa@google.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
2023-05-25 12:37:49 +03:00
|
|
|
uint32_t iotlb_hits; /* counts IOTLB hits */
|
|
|
|
uint32_t iotlb_misses; /* counts IOTLB misses*/
|
|
|
|
/* Used by stage-1 only. */
|
|
|
|
bool aa64; /* arch64 or aarch32 translation table */
|
2022-04-27 14:15:43 +03:00
|
|
|
bool record_faults; /* record fault events */
|
2018-05-04 20:05:51 +03:00
|
|
|
uint64_t ttb; /* TT base address */
|
|
|
|
uint8_t oas; /* output address width */
|
|
|
|
uint8_t tbi; /* Top Byte Ignore */
|
|
|
|
uint16_t asid;
|
|
|
|
SMMUTransTableInfo tt[2];
|
hw/arm/smmuv3: Update translation config to hold stage-2
In preparation for adding stage-2 support, add a S2 config
struct(SMMUS2Cfg), composed of the following fields and embedded in
the main SMMUTransCfg:
-tsz: Size of IPA input region (S2T0SZ)
-sl0: Start level of translation (S2SL0)
-affd: AF Fault Disable (S2AFFD)
-record_faults: Record fault events (S2R)
-granule_sz: Granule page shift (based on S2TG)
-vmid: Virtual Machine ID (S2VMID)
-vttb: Address of translation table base (S2TTB)
-eff_ps: Effective PA output range (based on S2PS)
They will be used in the next patches in stage-2 address translation.
The fields in SMMUS2Cfg, are reordered to make the shared and stage-1
fields next to each other, this reordering didn't change the struct
size (104 bytes before and after).
Stage-1 only fields: aa64, asid, tt, ttb, tbi, record_faults, oas.
oas is stage-1 output address size. However, it is used to check
input address in case stage-1 is unimplemented or bypassed according
to SMMUv3 manual IHI0070.E "3.4. Address sizes"
Shared fields: stage, disabled, bypassed, aborted, iotlb_*.
No functional change intended.
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Tested-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Message-id: 20230516203327.2051088-3-smostafa@google.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
2023-05-25 12:37:49 +03:00
|
|
|
/* Used by stage-2 only. */
|
|
|
|
struct SMMUS2Cfg s2cfg;
|
2018-05-04 20:05:51 +03:00
|
|
|
} SMMUTransCfg;
|
|
|
|
|
|
|
|
typedef struct SMMUDevice {
|
|
|
|
void *smmu;
|
|
|
|
PCIBus *bus;
|
|
|
|
int devfn;
|
|
|
|
IOMMUMemoryRegion iommu;
|
|
|
|
AddressSpace as;
|
2018-06-26 19:50:42 +03:00
|
|
|
uint32_t cfg_cache_hits;
|
|
|
|
uint32_t cfg_cache_misses;
|
2019-04-29 19:35:57 +03:00
|
|
|
QLIST_ENTRY(SMMUDevice) next;
|
2018-05-04 20:05:51 +03:00
|
|
|
} SMMUDevice;
|
|
|
|
|
|
|
|
typedef struct SMMUPciBus {
|
|
|
|
PCIBus *bus;
|
misc: Replace zero-length arrays with flexible array member (automatic)
Description copied from Linux kernel commit from Gustavo A. R. Silva
(see [3]):
--v-- description start --v--
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to
declare variable-length types such as these ones is a flexible
array member [1], introduced in C99:
struct foo {
int stuff;
struct boo array[];
};
By making use of the mechanism above, we will get a compiler
warning in case the flexible array does not occur last in the
structure, which will help us prevent some kind of undefined
behavior bugs from being unadvertenly introduced [2] to the
Linux codebase from now on.
--^-- description end --^--
Do the similar housekeeping in the QEMU codebase (which uses
C99 since commit 7be41675f7cb).
All these instances of code were found with the help of the
following Coccinelle script:
@@
identifier s, m, a;
type t, T;
@@
struct s {
...
t m;
- T a[0];
+ T a[];
};
@@
identifier s, m, a;
type t, T;
@@
struct s {
...
t m;
- T a[0];
+ T a[];
} QEMU_PACKED;
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=76497732932f
[3] https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/commit/?id=17642a2fbd2c1
Inspired-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-03-04 18:38:15 +03:00
|
|
|
SMMUDevice *pbdev[]; /* Parent array is sparse, so dynamically alloc */
|
2018-05-04 20:05:51 +03:00
|
|
|
} SMMUPciBus;
|
|
|
|
|
2018-06-26 19:50:42 +03:00
|
|
|
typedef struct SMMUIOTLBKey {
|
|
|
|
uint64_t iova;
|
|
|
|
uint16_t asid;
|
2023-05-25 12:37:50 +03:00
|
|
|
uint16_t vmid;
|
2020-07-28 18:08:09 +03:00
|
|
|
uint8_t tg;
|
|
|
|
uint8_t level;
|
2018-06-26 19:50:42 +03:00
|
|
|
} SMMUIOTLBKey;
|
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct SMMUState {
|
2018-05-04 20:05:51 +03:00
|
|
|
/* <private> */
|
|
|
|
SysBusDevice dev;
|
|
|
|
const char *mrtypename;
|
|
|
|
MemoryRegion iomem;
|
|
|
|
|
|
|
|
GHashTable *smmu_pcibus_by_busptr;
|
|
|
|
GHashTable *configs; /* cache for configuration data */
|
|
|
|
GHashTable *iotlb;
|
|
|
|
SMMUPciBus *smmu_pcibus_by_bus_num[SMMU_PCI_BUS_MAX];
|
|
|
|
PCIBus *pci_bus;
|
2019-04-29 19:35:57 +03:00
|
|
|
QLIST_HEAD(, SMMUDevice) devices_with_notifiers;
|
2018-05-04 20:05:51 +03:00
|
|
|
uint8_t bus_num;
|
|
|
|
PCIBus *primary_bus;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2018-05-04 20:05:51 +03:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct SMMUBaseClass {
|
2018-05-04 20:05:51 +03:00
|
|
|
/* <private> */
|
|
|
|
SysBusDeviceClass parent_class;
|
|
|
|
|
|
|
|
/*< public >*/
|
|
|
|
|
|
|
|
DeviceRealize parent_realize;
|
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2018-05-04 20:05:51 +03:00
|
|
|
|
|
|
|
#define TYPE_ARM_SMMU "arm-smmu"
|
2020-09-16 21:25:18 +03:00
|
|
|
OBJECT_DECLARE_TYPE(SMMUState, SMMUBaseClass, ARM_SMMU)
|
2018-05-04 20:05:51 +03:00
|
|
|
|
2018-05-04 20:05:51 +03:00
|
|
|
/* Return the SMMUPciBus handle associated to a PCI bus number */
|
|
|
|
SMMUPciBus *smmu_find_smmu_pcibus(SMMUState *s, uint8_t bus_num);
|
|
|
|
|
|
|
|
/* Return the stream ID of an SMMU device */
|
|
|
|
static inline uint16_t smmu_get_sid(SMMUDevice *sdev)
|
|
|
|
{
|
|
|
|
return PCI_BUILD_BDF(pci_bus_num(sdev->bus), sdev->devfn);
|
|
|
|
}
|
2018-05-04 20:05:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* smmu_ptw - Perform the page table walk for a given iova / access flags
|
|
|
|
* pair, according to @cfg translation config
|
|
|
|
*/
|
|
|
|
int smmu_ptw(SMMUTransCfg *cfg, dma_addr_t iova, IOMMUAccessFlags perm,
|
2020-07-28 18:08:08 +03:00
|
|
|
SMMUTLBEntry *tlbe, SMMUPTWEventInfo *info);
|
2018-05-04 20:05:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* select_tt - compute which translation table shall be used according to
|
|
|
|
* the input iova and translation config and return the TT specific info
|
|
|
|
*/
|
|
|
|
SMMUTransTableInfo *select_tt(SMMUTransCfg *cfg, dma_addr_t iova);
|
|
|
|
|
2018-06-26 19:50:42 +03:00
|
|
|
/* Return the iommu mr associated to @sid, or NULL if none */
|
|
|
|
IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid);
|
|
|
|
|
2018-06-26 19:50:42 +03:00
|
|
|
#define SMMU_IOTLB_MAX_SIZE 256
|
|
|
|
|
2020-07-28 18:08:09 +03:00
|
|
|
SMMUTLBEntry *smmu_iotlb_lookup(SMMUState *bs, SMMUTransCfg *cfg,
|
|
|
|
SMMUTransTableInfo *tt, hwaddr iova);
|
2020-07-28 18:08:08 +03:00
|
|
|
void smmu_iotlb_insert(SMMUState *bs, SMMUTransCfg *cfg, SMMUTLBEntry *entry);
|
2023-05-25 12:37:50 +03:00
|
|
|
SMMUIOTLBKey smmu_get_iotlb_key(uint16_t asid, uint16_t vmid, uint64_t iova,
|
2020-07-28 18:08:09 +03:00
|
|
|
uint8_t tg, uint8_t level);
|
2018-06-26 19:50:42 +03:00
|
|
|
void smmu_iotlb_inv_all(SMMUState *s);
|
|
|
|
void smmu_iotlb_inv_asid(SMMUState *s, uint16_t asid);
|
2023-05-25 12:37:51 +03:00
|
|
|
void smmu_iotlb_inv_vmid(SMMUState *s, uint16_t vmid);
|
2023-05-25 12:37:50 +03:00
|
|
|
void smmu_iotlb_inv_iova(SMMUState *s, int asid, int vmid, dma_addr_t iova,
|
2020-07-28 18:08:11 +03:00
|
|
|
uint8_t tg, uint64_t num_pages, uint8_t ttl);
|
2018-06-26 19:50:42 +03:00
|
|
|
|
2018-06-26 19:50:42 +03:00
|
|
|
/* Unmap the range of all the notifiers registered to any IOMMU mr */
|
|
|
|
void smmu_inv_notifiers_all(SMMUState *s);
|
|
|
|
|
2019-03-15 17:51:23 +03:00
|
|
|
#endif /* HW_ARM_SMMU_COMMON_H */
|