From nobody Tue Apr 30 00:11:19 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1490903034665463.55299513184434; Thu, 30 Mar 2017 12:43:54 -0700 (PDT) Received: from localhost ([::1]:37398 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctfz5-00042v-QJ for importer@patchew.org; Thu, 30 Mar 2017 15:43:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39927) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctfxs-0003Ay-AN for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ctfxr-00078v-Ho for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45894) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ctfxp-000770-GG; Thu, 30 Mar 2017 15:42:33 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5C489C05AA6F; Thu, 30 Mar 2017 19:42:32 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-27.ams2.redhat.com [10.36.117.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3222B77D53; Thu, 30 Mar 2017 19:42:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5C489C05AA6F Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eric.auger@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 5C489C05AA6F From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, peter.maydell@linaro.org, edgar.iglesias@gmail.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org, prem.mallappa@gmail.com Date: Thu, 30 Mar 2017 21:42:14 +0200 Message-Id: <1490902938-9009-2-git-send-email-eric.auger@redhat.com> In-Reply-To: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> References: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 30 Mar 2017 19:42:32 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC v3 1/5] log: Add new IOMMU type X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: drjones@redhat.com, Radha.Chintakuntla@cavium.com, christoffer.dall@linaro.org, Sunil.Goutham@cavium.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Prem Mallappa Add CPU_LOG_IOMMU to log traces related to the IOMMU. Signed-off-by: Prem Mallappa Signed-off-by: Eric Auger --- This may be replaced by dynamic traces later on --- include/qemu/log.h | 1 + util/log.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/include/qemu/log.h b/include/qemu/log.h index a50e994..906913b 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -43,6 +43,7 @@ static inline bool qemu_log_separate(void) #define CPU_LOG_PAGE (1 << 14) #define LOG_TRACE (1 << 15) #define CPU_LOG_TB_OP_IND (1 << 16) +#define CPU_LOG_IOMMU (1 << 17) =20 /* Returns true if a bit is set in the current loglevel mask */ diff --git a/util/log.c b/util/log.c index 96f30dd..76968d6 100644 --- a/util/log.c +++ b/util/log.c @@ -258,6 +258,8 @@ const QEMULogItem qemu_log_items[] =3D { "show CPU registers before entering a TB (lots of logs)" }, { CPU_LOG_MMU, "mmu", "log MMU-related activities" }, + { CPU_LOG_IOMMU, "iommu", + "log IOMMU-related activities" }, { CPU_LOG_PCALL, "pcall", "x86 only: show protected mode far calls/returns/exceptions" }, { CPU_LOG_RESET, "cpu_reset", --=20 2.5.5 From nobody Tue Apr 30 00:11:19 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1490903057399247.776351705941; Thu, 30 Mar 2017 12:44:17 -0700 (PDT) Received: from localhost ([::1]:37399 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctfzT-0004KM-2g for importer@patchew.org; Thu, 30 Mar 2017 15:44:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39984) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctfy1-0003Ic-32 for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ctfxz-0007Dl-5M for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58385) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ctfxt-00079q-Fv; Thu, 30 Mar 2017 15:42:37 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 421BE2E6065; Thu, 30 Mar 2017 19:42:36 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-27.ams2.redhat.com [10.36.117.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA0EF77D53; Thu, 30 Mar 2017 19:42:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 421BE2E6065 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eric.auger@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 421BE2E6065 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, peter.maydell@linaro.org, edgar.iglesias@gmail.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org, prem.mallappa@gmail.com Date: Thu, 30 Mar 2017 21:42:15 +0200 Message-Id: <1490902938-9009-3-git-send-email-eric.auger@redhat.com> In-Reply-To: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> References: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 30 Mar 2017 19:42:36 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC v3 2/5] hw/arm/smmu-common: smmu base class X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: drjones@redhat.com, Radha.Chintakuntla@cavium.com, christoffer.dall@linaro.org, Sunil.Goutham@cavium.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Introduces the base device and class for the ARM smmu. Also introduces common data types and helpers. Signed-off-by: Eric Auger Signed-off-by: Prem Mallappa --- v3: - moved the base code in a separate patch to ease the review. - clearer separation between base class and smmuv3 class - translate_* only implemented as class methods --- default-configs/aarch64-softmmu.mak | 1 + hw/arm/Makefile.objs | 1 + hw/arm/smmu-common.c | 193 ++++++++++++++++++++++++++++++++= ++++ include/hw/arm/smmu-common.h | 151 ++++++++++++++++++++++++++++ 4 files changed, 346 insertions(+) create mode 100644 hw/arm/smmu-common.c create mode 100644 include/hw/arm/smmu-common.h diff --git a/default-configs/aarch64-softmmu.mak b/default-configs/aarch64-= softmmu.mak index 2449483..83a2932 100644 --- a/default-configs/aarch64-softmmu.mak +++ b/default-configs/aarch64-softmmu.mak @@ -7,3 +7,4 @@ CONFIG_AUX=3Dy CONFIG_DDC=3Dy CONFIG_DPCD=3Dy CONFIG_XLNX_ZYNQMP=3Dy +CONFIG_ARM_SMMUV3=3Dy diff --git a/hw/arm/Makefile.objs b/hw/arm/Makefile.objs index 4c5c4ee..6c7d4af 100644 --- a/hw/arm/Makefile.objs +++ b/hw/arm/Makefile.objs @@ -18,3 +18,4 @@ obj-$(CONFIG_FSL_IMX25) +=3D fsl-imx25.o imx25_pdk.o obj-$(CONFIG_FSL_IMX31) +=3D fsl-imx31.o kzm.o obj-$(CONFIG_FSL_IMX6) +=3D fsl-imx6.o sabrelite.o obj-$(CONFIG_ASPEED_SOC) +=3D aspeed_soc.o aspeed.o +obj-$(CONFIG_ARM_SMMUV3) +=3D smmu-common.o diff --git a/hw/arm/smmu-common.c b/hw/arm/smmu-common.c new file mode 100644 index 0000000..7444c79 --- /dev/null +++ b/hw/arm/smmu-common.c @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2014-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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U= SA. + * + * Author: Prem Mallappa + * + */ + +#include "qemu/osdep.h" +#include "sysemu/sysemu.h" +#include "exec/address-spaces.h" + +#include "hw/arm/smmu-common.h" + +#ifdef ARM_SMMU_DEBUG +uint32_t dbg_bits =3D \ + DBG_BIT(PANIC) | DBG_BIT(CRIT) | DBG_BIT(WARN) | DBG_BIT(IRQ); +#else +const uint32_t dbg_bits; +#endif + +inline MemTxResult smmu_read_sysmem(hwaddr addr, void *buf, int len, + bool secure) +{ + MemTxAttrs attrs =3D {.unspecified =3D 1, .secure =3D secure}; + + switch (len) { + case 4: + *(uint32_t *)buf =3D ldl_le_phys(&address_space_memory, addr); + break; + case 8: + *(uint64_t *)buf =3D ldq_le_phys(&address_space_memory, addr); + break; + default: + return address_space_rw(&address_space_memory, addr, + attrs, buf, len, false); + } + return MEMTX_OK; +} + +inline void +smmu_write_sysmem(hwaddr addr, void *buf, int len, bool secure) +{ + MemTxAttrs attrs =3D {.unspecified =3D 1, .secure =3D secure}; + + switch (len) { + case 4: + stl_le_phys(&address_space_memory, addr, *(uint32_t *)buf); + break; + case 8: + stq_le_phys(&address_space_memory, addr, *(uint64_t *)buf); + break; + default: + address_space_rw(&address_space_memory, addr, + attrs, buf, len, true); + } +} + +static SMMUTransErr +smmu_translate_64(SMMUTransCfg *cfg, uint32_t *pagesize, + uint32_t *perm, bool is_write) +{ + int ret, level; + int stage =3D cfg->stage; + int granule_sz =3D cfg->granule_sz[stage]; + int va_size =3D cfg->va_size[stage]; + hwaddr va, addr, mask; + hwaddr *outaddr; + + + va =3D addr =3D cfg->va; /* or ipa in Stage2 */ + SMMU_DPRINTF(TT_1, "stage:%d\n", stage); + assert(va_size =3D=3D 64); /* We dont support 32-bit yet */ + /* same location, for clearity */ + outaddr =3D &cfg->pa; + + level =3D 4 - (va_size - cfg->tsz[stage] - 4) / granule_sz; + + mask =3D (1ULL << (granule_sz + 3)) - 1; + + addr =3D extract64(cfg->ttbr[stage], 0, 48); + addr &=3D ~((1ULL << (va_size - cfg->tsz[stage] - + (granule_sz * (4 - level)))) - 1); + + for (;;) { + uint64_t desc; +#ifdef ARM_SMMU_DEBUG + uint64_t ored =3D (va >> (granule_sz * (4 - level))) & mask; + SMMU_DPRINTF(TT_1, + "Level: %d va:%lx addr:%lx ored:%lx\n", + level, va, addr, ored); +#endif + addr |=3D (va >> (granule_sz * (4 - level))) & mask; + addr &=3D ~7ULL; + + if (smmu_read_sysmem(addr, &desc, sizeof(desc), false)) { + ret =3D SMMU_TRANS_ERR_WALK_EXT_ABRT; + SMMU_DPRINTF(CRIT, "Translation table read error lvl:%d\n", le= vel); + break; + } + + SMMU_DPRINTF(TT_1, + "Level: %d gran_sz:%d mask:%lx addr:%lx desc:%lx\n", + level, granule_sz, mask, addr, desc); + + if (!(desc & 1) || + (!(desc & 2) && (level =3D=3D 3))) { + ret =3D SMMU_TRANS_ERR_TRANS; + break; + } + + /* We call again to resolve address at this 'level' */ + if (cfg->s2_needed) { + uint32_t perm_s2, pagesize_s2; + SMMUTransCfg s2cfg =3D *cfg; + + s2cfg.stage++; + s2cfg.va =3D desc; + s2cfg.s2_needed =3D false; + + ret =3D smmu_translate_64(&s2cfg, &pagesize_s2, + &perm_s2, is_write); + if (ret) { + break; + } + + desc =3D (uint64_t)s2cfg.pa; + SMMU_DPRINTF(TT_2, "addr:%lx pagesize:%x\n", addr, *pagesize); + } + + addr =3D desc & 0xffffffff000ULL; + if ((desc & 2) && (level < 3)) { + level++; + continue; + } + *pagesize =3D (1ULL << ((granule_sz * (4 - level)) + 3)); + addr |=3D (va & (*pagesize - 1)); + SMMU_DPRINTF(TT_1, "addr:%lx pagesize:%x\n", addr, *pagesize); + break; + } + + if (ret =3D=3D 0) { + *outaddr =3D addr; + } + + return ret; +} + +static void smmu_base_instance_init(Object *obj) +{ + /* Nothing much to do here as of now */ +} + +static void smmu_base_class_init(ObjectClass *klass, void *data) +{ + SMMUBaseClass *sbc =3D SMMU_DEVICE_CLASS(klass); + + sbc->translate_64 =3D smmu_translate_64; + sbc->translate_32 =3D NULL; /* not yet implemented */ +} + +static const TypeInfo smmu_base_info =3D { + .name =3D TYPE_SMMU_DEV_BASE, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(SMMUState), + .instance_init =3D smmu_base_instance_init, + .class_data =3D NULL, + .class_size =3D sizeof(SMMUBaseClass), + .class_init =3D smmu_base_class_init, + .abstract =3D true, +}; + +static void smmu_base_register_types(void) +{ + type_register_static(&smmu_base_info); +} + +type_init(smmu_base_register_types) + diff --git a/include/hw/arm/smmu-common.h b/include/hw/arm/smmu-common.h new file mode 100644 index 0000000..b29648f --- /dev/null +++ b/include/hw/arm/smmu-common.h @@ -0,0 +1,151 @@ +/* + * 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 as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifndef HW_ARM_SMMU_COMMON_H +#define HW_ARM_SMMU_COMMON_H + +#include +#include +#include "hw/pci/pci.h" + +typedef enum { + SMMU_TRANS_ERR_WALK_EXT_ABRT =3D 0x1, /* Translation walk external ab= ort */ + SMMU_TRANS_ERR_TRANS =3D 0x10, /* Translation fault */ + SMMU_TRANS_ERR_ADDR_SZ, /* Address Size fault */ + SMMU_TRANS_ERR_ACCESS, /* Access fault */ + SMMU_TRANS_ERR_PERM, /* Permission fault */ + SMMU_TRANS_ERR_TLB_CONFLICT =3D 0x20, /* TLB Conflict */ +} SMMUTransErr; + +/* + * This needs to be populated by SMMUv2 and SMMUv3 + * each do it in their own way + * translate functions use it to call translations + */ +typedef struct SMMUTransCfg { + hwaddr va; /* Input to S1 */ + int stage; + uint32_t oas[3]; + uint32_t tsz[3]; + uint64_t ttbr[3]; + uint32_t granule[3]; + uint32_t va_size[3]; + uint32_t granule_sz[3]; + + hwaddr pa; /* Output from S1, Final PA */ + bool s2_needed; +} SMMUTransCfg; + +struct SMMUTransReq { + uint32_t stage; + SMMUTransCfg cfg[2]; +}; + +typedef struct SMMUDevice { + void *smmu; + PCIBus *bus; + int devfn; + MemoryRegion iommu; + AddressSpace as; +} SMMUDevice; + +typedef struct SMMUPciBus { + PCIBus *bus; + SMMUDevice *pbdev[0]; /* Parent array is sparse, so dynamically allo= c */ +} SMMUPciBus; + +typedef struct SMMUState { + /* */ + SysBusDevice dev; + + MemoryRegion iomem; +} SMMUState; + +typedef struct { + /* */ + SysBusDeviceClass parent_class; + + /* public */ + SMMUTransErr (*translate_32)(SMMUTransCfg *cfg, uint32_t *pagesize, + uint32_t *perm, bool is_write); + SMMUTransErr (*translate_64)(SMMUTransCfg *cfg, uint32_t *pagesize, + uint32_t *perm, bool is_write); +} SMMUBaseClass; + +#define TYPE_SMMU_DEV_BASE "smmu-base" +#define SMMU_SYS_DEV(obj) OBJECT_CHECK(SMMUState, (obj), TYPE_SMMU_DEV_BAS= E) +#define SMMU_DEVICE_GET_CLASS(obj) \ + OBJECT_GET_CLASS(SMMUBaseClass, (obj), TYPE_SMMU_DEV_BASE) +#define SMMU_DEVICE_CLASS(klass) \ + OBJECT_CLASS_CHECK(SMMUBaseClass, (klass), TYPE_SMMU_DEV_BASE) + +/* #define ARM_SMMU_DEBUG */ +#ifdef ARM_SMMU_DEBUG + +extern uint32_t dbg_bits; + +#define HERE() printf("%s:%d\n", __func__, __LINE__) + +enum { + SMMU_DBG_PANIC, SMMU_DBG_CRIT, SMMU_DBG_WARN, /* error level */ + SMMU_DBG_DBG1, SMMU_DBG_DBG2, SMMU_DBG_INFO, /* info level */ + SMMU_DBG_CMDQ, /* Just command queue */ + SMMU_DBG_STE, SMMU_DBG_CD, /* Specific parts STE/CD = */ + SMMU_DBG_TT_1, SMMU_DBG_TT_2, /* Translation Stage 1/2 = */ + SMMU_DBG_IRQ, /* IRQ */ +}; + +#define DBG_BIT(bit) (1 << SMMU_DBG_##bit) + +#define IS_DBG_ENABLED(bit) (dbg_bits & (1 << SMMU_DBG_##bit)) + +#define DBG_DEFAULT (DBG_BIT(PANIC) | DBG_BIT(CRIT) | DBG_BIT(IRQ)) +#define DBG_EXTRA (DBG_BIT(STE) | DBG_BIT(CD) | DBG_BIT(TT_1)) +#define DBG_VERBOSE1 DBG_BIT(DBG1) +#define DBG_VERBOSE2 (DBG_VERBOSE1 | DBG_BIT(DBG1)) +#define DBG_VERBOSE3 (DBG_VERBOSE2 | DBG_BIT(DBG2)) +#define DBG_VERBOSE4 (DBG_VERBOSE3 | DBG_BIT(INFO)) + +#define SMMU_DPRINTF(lvl, fmt, ...) \ + do { \ + if (dbg_bits & DBG_BIT(lvl)) { \ + qemu_log_mask(CPU_LOG_IOMMU, \ + "(smmu)%s: " fmt , \ + __func__, \ + ## __VA_ARGS__); \ + } \ + } while (0) + +#else +#define IS_DBG_ENABLED(bit) false +#define SMMU_DPRINTF(lvl, fmt, ...) + +#endif /* SMMU_DEBUG */ + +MemTxResult smmu_read_sysmem(hwaddr addr, void *buf, int len, bool secure); +void smmu_write_sysmem(hwaddr addr, void *buf, int len, bool secure); + +static inline uint16_t smmu_get_sid(SMMUDevice *sdev) +{ + return ((pci_bus_num(sdev->bus) & 0xff) << 8) | sdev->devfn; +} + +#endif /* HW_ARM_SMMU_COMMON */ --=20 2.5.5 From nobody Tue Apr 30 00:11:19 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1490903071263417.8575029153369; Thu, 30 Mar 2017 12:44:31 -0700 (PDT) Received: from localhost ([::1]:37400 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctfzh-0004WF-QQ for importer@patchew.org; Thu, 30 Mar 2017 15:44:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40084) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctfyE-0003Sx-67 for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:43:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ctfy9-0007Ih-2q for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:58 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39560) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ctfxx-0007Cc-Km; Thu, 30 Mar 2017 15:42:41 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 791F6C60E6; Thu, 30 Mar 2017 19:42:40 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-27.ams2.redhat.com [10.36.117.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id AEBED77D53; Thu, 30 Mar 2017 19:42:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 791F6C60E6 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eric.auger@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 791F6C60E6 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, peter.maydell@linaro.org, edgar.iglesias@gmail.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org, prem.mallappa@gmail.com Date: Thu, 30 Mar 2017 21:42:16 +0200 Message-Id: <1490902938-9009-4-git-send-email-eric.auger@redhat.com> In-Reply-To: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> References: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 30 Mar 2017 19:42:40 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC v3 3/5] hw/arm/smmuv3: smmuv3 emulation model X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: drjones@redhat.com, Radha.Chintakuntla@cavium.com, christoffer.dall@linaro.org, Sunil.Goutham@cavium.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Prem Mallappa Introduces the SMMUv3 derived model. This is based on System MMUv3 specification (v17). Signed-off-by: Prem Mallappa Signed-off-by: Eric Auger --- v2 -> v3: - move creation of include/hw/arm/smmuv3.h to this patch to fix compil issue - compilation allowed - fix sbus allocation in smmu_init_pci_iommu - restructure code into headers - misc cleanups --- hw/arm/Makefile.objs | 2 +- hw/arm/smmuv3-internal.h | 544 ++++++++++++++++++++++ hw/arm/smmuv3.c | 1131 ++++++++++++++++++++++++++++++++++++++++++= ++++ include/hw/arm/smmuv3.h | 88 ++++ 4 files changed, 1764 insertions(+), 1 deletion(-) create mode 100644 hw/arm/smmuv3-internal.h create mode 100644 hw/arm/smmuv3.c create mode 100644 include/hw/arm/smmuv3.h diff --git a/hw/arm/Makefile.objs b/hw/arm/Makefile.objs index 6c7d4af..02cd23f 100644 --- a/hw/arm/Makefile.objs +++ b/hw/arm/Makefile.objs @@ -18,4 +18,4 @@ obj-$(CONFIG_FSL_IMX25) +=3D fsl-imx25.o imx25_pdk.o obj-$(CONFIG_FSL_IMX31) +=3D fsl-imx31.o kzm.o obj-$(CONFIG_FSL_IMX6) +=3D fsl-imx6.o sabrelite.o obj-$(CONFIG_ASPEED_SOC) +=3D aspeed_soc.o aspeed.o -obj-$(CONFIG_ARM_SMMUV3) +=3D smmu-common.o +obj-$(CONFIG_ARM_SMMUV3) +=3D smmu-common.o smmuv3.o diff --git a/hw/arm/smmuv3-internal.h b/hw/arm/smmuv3-internal.h new file mode 100644 index 0000000..1675e03 --- /dev/null +++ b/hw/arm/smmuv3-internal.h @@ -0,0 +1,544 @@ +/* + * ARM SMMUv3 support - Internal API + * + * Copyright (C) 2014-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 as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifndef HW_ARM_SMMU_V3_INTERNAL_H +#define HW_ARM_SMMU_V3_INTERNAL_H + +/***************************** + * MMIO Register + *****************************/ +enum { + SMMU_REG_IDR0 =3D 0x0, + +#define SMMU_IDR0_S2P (1 << 0) +#define SMMU_IDR0_S1P (1 << 1) +#define SMMU_IDR0_TTF (0x3 << 2) +#define SMMU_IDR0_HTTU (0x3 << 6) +#define SMMU_IDR0_HYP (1 << 9) +#define SMMU_IDR0_ATS (1 << 10) +#define SMMU_IDR0_VMID16 (1 << 18) +#define SMMU_IDR0_CD2L (1 << 19) + + SMMU_REG_IDR1 =3D 0x4, + SMMU_REG_IDR2 =3D 0x8, + SMMU_REG_IDR3 =3D 0xc, + SMMU_REG_IDR4 =3D 0x10, + SMMU_REG_IDR5 =3D 0x14, + SMMU_REG_IIDR =3D 0x1c, + SMMU_REG_CR0 =3D 0x20, + +#define SMMU_CR0_SMMU_ENABLE (1 << 0) +#define SMMU_CR0_PRIQ_ENABLE (1 << 1) +#define SMMU_CR0_EVTQ_ENABLE (1 << 2) +#define SMMU_CR0_CMDQ_ENABLE (1 << 3) +#define SMMU_CR0_ATS_CHECK (1 << 4) + + SMMU_REG_CR0_ACK =3D 0x24, + SMMU_REG_CR1 =3D 0x28, + SMMU_REG_CR2 =3D 0x2c, + + SMMU_REG_STATUSR =3D 0x40, + + SMMU_REG_IRQ_CTRL =3D 0x50, + SMMU_REG_IRQ_CTRL_ACK =3D 0x54, + +#define SMMU_IRQ_CTRL_GERROR_EN (1 << 0) +#define SMMU_IRQ_CTRL_EVENT_EN (1 << 1) +#define SMMU_IRQ_CTRL_PRI_EN (1 << 2) + + SMMU_REG_GERROR =3D 0x60, + +#define SMMU_GERROR_CMDQ (1 << 0) +#define SMMU_GERROR_EVENTQ (1 << 2) +#define SMMU_GERROR_PRIQ (1 << 3) +#define SMMU_GERROR_MSI_CMDQ (1 << 4) +#define SMMU_GERROR_MSI_EVENTQ (1 << 5) +#define SMMU_GERROR_MSI_PRIQ (1 << 6) +#define SMMU_GERROR_MSI_GERROR (1 << 7) +#define SMMU_GERROR_SFM_ERR (1 << 8) + + SMMU_REG_GERRORN =3D 0x64, + SMMU_REG_GERROR_IRQ_CFG0 =3D 0x68, + SMMU_REG_GERROR_IRQ_CFG1 =3D 0x70, + SMMU_REG_GERROR_IRQ_CFG2 =3D 0x74, + + /* SMMU_BASE_RA Applies to STRTAB_BASE, CMDQ_BASE and EVTQ_BASE */ +#define SMMU_BASE_RA (1ULL << 62) + SMMU_REG_STRTAB_BASE =3D 0x80, + SMMU_REG_STRTAB_BASE_CFG =3D 0x88, + + SMMU_REG_CMDQ_BASE =3D 0x90, + SMMU_REG_CMDQ_PROD =3D 0x98, + SMMU_REG_CMDQ_CONS =3D 0x9c, + /* CMD Consumer (CONS) */ +#define SMMU_CMD_CONS_ERR_SHIFT 24 +#define SMMU_CMD_CONS_ERR_BITS 7 + + SMMU_REG_EVTQ_BASE =3D 0xa0, + SMMU_REG_EVTQ_PROD =3D 0xa8, + SMMU_REG_EVTQ_CONS =3D 0xac, + SMMU_REG_EVTQ_IRQ_CFG0 =3D 0xb0, + SMMU_REG_EVTQ_IRQ_CFG1 =3D 0xb8, + SMMU_REG_EVTQ_IRQ_CFG2 =3D 0xbc, + + SMMU_REG_PRIQ_BASE =3D 0xc0, + SMMU_REG_PRIQ_PROD =3D 0xc8, + SMMU_REG_PRIQ_CONS =3D 0xcc, + SMMU_REG_PRIQ_IRQ_CFG0 =3D 0xd0, + SMMU_REG_PRIQ_IRQ_CFG1 =3D 0xd8, + SMMU_REG_PRIQ_IRQ_CFG2 =3D 0xdc, + + SMMU_ID_REGS_OFFSET =3D 0xfd0, + + /* Secure registers are not used for now */ + SMMU_SECURE_OFFSET =3D 0x8000, +}; + +/********************** + * Data Structures + **********************/ + +struct __smmu_data2 { + uint32_t word[2]; +}; + +struct __smmu_data8 { + uint32_t word[8]; +}; + +struct __smmu_data16 { + uint32_t word[16]; +}; + +struct __smmu_data4 { + uint32_t word[4]; +}; + +typedef struct __smmu_data2 STEDesc; /* STE Level 1 Descriptor */ +typedef struct __smmu_data16 Ste; /* Stream Table Entry(STE) */ +typedef struct __smmu_data2 CDDesc; /* CD Level 1 Descriptor */ +typedef struct __smmu_data16 Cd; /* Context Descriptor(CD) */ + +typedef struct __smmu_data4 Cmd; /* Command Entry */ +typedef struct __smmu_data8 Evt; /* Event Entry */ +typedef struct __smmu_data4 Pri; /* PRI entry */ + +/***************************** + * STE fields + *****************************/ + +#define STE_VALID(x) extract32((x)->word[0], 0, 1) /* 0 */ +#define STE_CONFIG(x) (extract32((x)->word[0], 1, 3) & 0x7) +enum { + STE_CONFIG_NONE =3D 0, + STE_CONFIG_BYPASS =3D 4, /* S1 Bypass, S2 Bypass */ + STE_CONFIG_S1TR =3D 1, /* S1 Translate, S2 Bypass */ + STE_CONFIG_S2TR =3D 2, /* S1 Bypass, S2 Translate */ + STE_CONFIG_S1TR_S2TR =3D 3, /* S1 Translate, S2 Translate */ +}; +#define STE_S1FMT(x) extract32((x)->word[0], 4, 2) +#define STE_S1CDMAX(x) extract32((x)->word[1], 8, 2) +#define STE_EATS(x) extract32((x)->word[2], 28, 2) +#define STE_STRW(x) extract32((x)->word[2], 30, 2) +#define STE_S2VMID(x) extract32((x)->word[4], 0, 16) /* 4 */ +#define STE_S2T0SZ(x) extract32((x)->word[5], 0, 6) /* 5 */ +#define STE_S2TG(x) extract32((x)->word[5], 14, 2) +#define STE_S2PS(x) extract32((x)->word[5], 16, 3) +#define STE_S2AA64(x) extract32((x)->word[5], 19, 1) +#define STE_S2HD(x) extract32((x)->word[5], 24, 1) +#define STE_S2HA(x) extract32((x)->word[5], 25, 1) +#define STE_S2S(x) extract32((x)->word[5], 26, 1) +#define STE_CTXPTR(x) \ + ({ \ + unsigned long addr; \ + addr =3D (uint64_t)extract32((x)->word[1], 0, 16) << 32; \ + addr |=3D (uint64_t)((x)->word[0] & 0xffffffc0); \ + addr; \ + }) + +#define STE_S2TTB(x) \ + ({ \ + unsigned long addr; \ + addr =3D (uint64_t)extract32((x)->word[7], 0, 16) << 32; \ + addr |=3D (uint64_t)((x)->word[6] & 0xfffffff0); \ + addr; \ + }) + +static inline int is_ste_valid(SMMUV3State *s, Ste *ste) +{ + return STE_VALID(ste); +} + +static inline int is_ste_bypass(SMMUV3State *s, Ste *ste) +{ + return STE_CONFIG(ste) =3D=3D STE_CONFIG_BYPASS; +} + +/***************************** + * CD fields + *****************************/ +#define CD_VALID(x) extract32((x)->word[0], 30, 1) +#define CD_ASID(x) extract32((x)->word[1], 16, 16) +#define CD_TTB(x, sel) \ + ({ \ + uint64_t hi, lo; \ + hi =3D extract32((x)->word[(sel) * 2 + 3], 0, 16); \ + hi <<=3D 32; \ + lo =3D (x)->word[(sel) * 2 + 2] & ~0xf; \ + hi | lo; \ + }) + +#define CD_TSZ(x, sel) extract32((x)->word[0], (16 * (sel)) + 0, 6) +#define CD_TG(x, sel) extract32((x)->word[0], (16 * (sel)) + 6, 2) +#define CD_EPD(x, sel) extract32((x)->word[0], (16 * (sel)) + 14, 1) + +#define CD_T0SZ(x) CD_TSZ((x), 0) +#define CD_T1SZ(x) CD_TSZ((x), 1) +#define CD_TG0(x) CD_TG((x), 0) +#define CD_TG1(x) CD_TG((x), 1) +#define CD_EPD0(x) CD_EPD((x), 0) +#define CD_EPD1(x) CD_EPD((x), 1) +#define CD_IPS(x) extract32((x)->word[1], 0, 3) +#define CD_AARCH64(x) extract32((x)->word[1], 9, 1) +#define CD_TTB0(x) CD_TTB((x), 0) +#define CD_TTB1(x) CD_TTB((x), 1) + +#define CDM_VALID(x) ((x)->word[0] & 0x1) + +static inline int is_cd_valid(SMMUV3State *s, Ste *ste, Cd *cd) +{ + return CD_VALID(cd); +} + +/***************************** + * Commands + *****************************/ +enum { + SMMU_CMD_PREFETCH_CONFIG =3D 0x01, + SMMU_CMD_PREFETCH_ADDR, + SMMU_CMD_CFGI_STE, + SMMU_CMD_CFGI_STE_RANGE, + SMMU_CMD_CFGI_CD, + SMMU_CMD_CFGI_CD_ALL, + SMMU_CMD_TLBI_NH_ALL =3D 0x10, + SMMU_CMD_TLBI_NH_ASID, + SMMU_CMD_TLBI_NH_VA, + SMMU_CMD_TLBI_NH_VAA, + SMMU_CMD_TLBI_EL3_ALL =3D 0x18, + SMMU_CMD_TLBI_EL3_VA =3D 0x1a, + SMMU_CMD_TLBI_EL2_ALL =3D 0x20, + SMMU_CMD_TLBI_EL2_ASID, + SMMU_CMD_TLBI_EL2_VA, + SMMU_CMD_TLBI_EL2_VAA, /* 0x23 */ + SMMU_CMD_TLBI_S12_VMALL =3D 0x28, + SMMU_CMD_TLBI_S2_IPA =3D 0x2a, + SMMU_CMD_TLBI_NSNH_ALL =3D 0x30, + SMMU_CMD_ATC_INV =3D 0x40, + SMMU_CMD_PRI_RESP, + SMMU_CMD_RESUME =3D 0x44, + SMMU_CMD_STALL_TERM, + SMMU_CMD_SYNC, /* 0x46 */ +}; + +/***************************** + * Register Access Primitives + *****************************/ + +static void smmu_write64_reg(SMMUV3State *s, uint32_t addr, uint64_t val) +{ + addr >>=3D 2; + s->regs[addr] =3D val & 0xFFFFFFFFULL; + s->regs[addr + 1] =3D val & ~0xFFFFFFFFULL; +} + +static void smmu_write_reg(SMMUV3State *s, uint32_t addr, uint64_t val) +{ + s->regs[addr >> 2] =3D val; +} + +static inline uint32_t smmu_read_reg(SMMUV3State *s, uint32_t addr) +{ + return s->regs[addr >> 2]; +} + +static inline uint64_t smmu_read64_reg(SMMUV3State *s, uint32_t addr) +{ + addr >>=3D 2; + return s->regs[addr] | (s->regs[addr + 1] << 32); +} + +#define smmu_read32_reg smmu_read_reg +#define smmu_write32_reg smmu_write_reg + +/***************************** + * CMDQ fields + *****************************/ + +enum { /* Command Errors */ + SMMU_CMD_ERR_NONE =3D 0, + SMMU_CMD_ERR_ILLEGAL, + SMMU_CMD_ERR_ABORT +}; + +enum { /* Command completion notification */ + CMD_SYNC_SIG_NONE, + CMD_SYNC_SIG_IRQ, + CMD_SYNC_SIG_SEV, +}; + +#define CMD_TYPE(x) extract32((x)->word[0], 0, 8) +#define CMD_SEC(x) extract32((x)->word[0], 9, 1) +#define CMD_SEV(x) extract32((x)->word[0], 10, 1) +#define CMD_AC(x) extract32((x)->word[0], 12, 1) +#define CMD_AB(x) extract32((x)->word[0], 13, 1) +#define CMD_CS(x) extract32((x)->word[0], 12, 2) +#define CMD_SSID(x) extract32((x)->word[0], 16, 16) +#define CMD_SID(x) ((x)->word[1]) +#define CMD_VMID(x) extract32((x)->word[1], 0, 16) +#define CMD_ASID(x) extract32((x)->word[1], 16, 16) +#define CMD_STAG(x) extract32((x)->word[2], 0, 16) +#define CMD_RESP(x) extract32((x)->word[2], 11, 2) +#define CMD_GRPID(x) extract32((x)->word[3], 0, 8) +#define CMD_SIZE(x) extract32((x)->word[3], 0, 16) +#define CMD_LEAF(x) extract32((x)->word[3], 0, 1) +#define CMD_SPAN(x) extract32((x)->word[3], 0, 5) +#define CMD_ADDR(x) ({ \ + uint64_t addr =3D (uint64_t)(x)->word[3]; \ + addr <<=3D 32; \ + addr |=3D extract32((x)->word[3], 12, 20); \ + addr; \ + }) + +/*************************** + * Queue Handling + ***************************/ + +typedef enum { + CMD_Q_EMPTY, + CMD_Q_FULL, + CMD_Q_INUSE, +} SMMUQStatus; + +#define Q_ENTRY(q, idx) (q->base + q->ent_size * idx) +#define Q_WRAP(q, pc) ((pc) >> (q)->shift) +#define Q_IDX(q, pc) ((pc) & ((1 << (q)->shift) - 1)) + +static inline SMMUQStatus +__smmu_queue_status(SMMUV3State *s, SMMUQueue *q) +{ + uint32_t prod =3D Q_IDX(q, q->prod), cons =3D Q_IDX(q, q->cons); + if ((prod =3D=3D cons) && (q->wrap.prod !=3D q->wrap.cons)) { + return CMD_Q_FULL; + } else if ((prod =3D=3D cons) && (q->wrap.prod =3D=3D q->wrap.cons)) { + return CMD_Q_EMPTY; + } + return CMD_Q_INUSE; +} +#define smmu_is_q_full(s, q) (__smmu_queue_status(s, q) =3D=3D CMD_Q_FULL) +#define smmu_is_q_empty(s, q) (__smmu_queue_status(s, q) =3D=3D CMD_Q_EMPT= Y) + +static int __smmu_q_enabled(SMMUV3State *s, uint32_t q) +{ + return smmu_read32_reg(s, SMMU_REG_CR0) & q; +} +#define smmu_cmd_q_enabled(s) __smmu_q_enabled(s, SMMU_CR0_CMDQ_ENABLE) +#define smmu_evt_q_enabled(s) __smmu_q_enabled(s, SMMU_CR0_EVTQ_ENABLE) + +#define SMMU_CMDQ_ERR(s) ((smmu_read32_reg(s, SMMU_REG_GERROR) ^ \ + smmu_read32_reg(s, SMMU_REG_GERRORN)) & \ + SMMU_GERROR_CMDQ) + +/***************************** + * EVTQ fields + *****************************/ + +#define EVT_Q_OVERFLOW (1 << 31) + +#define EVT_SET_TYPE(x, t) deposit32((x)->word[0], 0, 8, t) +#define EVT_SET_SID(x, s) ((x)->word[1] =3D s) +#define EVT_SET_INPUT_ADDR(x, addr) ({ \ + (x)->word[5] =3D (uint32_t)(addr >> 32); \ + (x)->word[4] =3D (uint32_t)(addr & 0xffffffff); \ + addr; \ + }) + +/***************************** + * Events + *****************************/ + +enum evt_err { + SMMU_EVT_F_UUT =3D 0x1, + SMMU_EVT_C_BAD_SID, + SMMU_EVT_F_STE_FETCH, + SMMU_EVT_C_BAD_STE, + SMMU_EVT_F_BAD_ATS_REQ, + SMMU_EVT_F_STREAM_DISABLED, + SMMU_EVT_F_TRANS_FORBIDDEN, + SMMU_EVT_C_BAD_SSID, + SMMU_EVT_F_CD_FETCH, + SMMU_EVT_C_BAD_CD, + SMMU_EVT_F_WALK_EXT_ABRT, + SMMU_EVT_F_TRANS =3D 0x10, + SMMU_EVT_F_ADDR_SZ, + SMMU_EVT_F_ACCESS, + SMMU_EVT_F_PERM, + SMMU_EVT_F_TLB_CONFLICT =3D 0x20, + SMMU_EVT_F_CFG_CONFLICT =3D 0x21, + SMMU_EVT_E_PAGE_REQ =3D 0x24, +}; + +typedef enum evt_err SMMUEvtErr; + +/***************************** + * Interrupts + *****************************/ + +static inline int __smmu_irq_enabled(SMMUV3State *s, uint32_t q) +{ + return smmu_read64_reg(s, SMMU_REG_IRQ_CTRL) & q; +} +#define smmu_evt_irq_enabled(s) \ + __smmu_irq_enabled(s, SMMU_IRQ_CTRL_EVENT_EN) +#define smmu_gerror_irq_enabled(s) \ + __smmu_irq_enabled(s, SMMU_IRQ_CTRL_GERROR_EN) +#define smmu_pri_irq_enabled(s) \ + __smmu_irq_enabled(s, SMMU_IRQ_CTRL_PRI_EN) + +static inline bool +smmu_is_irq_pending(SMMUV3State *s, int irq) +{ + return smmu_read32_reg(s, SMMU_REG_GERROR) ^ + smmu_read32_reg(s, SMMU_REG_GERRORN); +} + +/***************************** + * Hash Table + *****************************/ + +static inline gboolean smmu_uint64_equal(gconstpointer v1, gconstpointer v= 2) +{ + return *((const uint64_t *)v1) =3D=3D *((const uint64_t *)v2); +} + +static inline guint smmu_uint64_hash(gconstpointer v) +{ + return (guint)*(const uint64_t *)v; +} + +/***************************** + * Misc + *****************************/ + +static int tg2granule(int bits, bool tg1) +{ + switch (bits) { + case 1: + return tg1 ? 14 : 16; + case 2: + return tg1 ? 14 : 12; + case 3: + return tg1 ? 16 : 12; + default: + return 12; + } +} + +static inline int oas2bits(int oas) +{ + switch (oas) { + case 2: + return 40; + case 3: + return 42; + case 4: + return 44; + case 5: + default: return 48; + } +} + +#define STM2U64(stm) ({ \ + uint64_t hi, lo; \ + hi =3D (stm)->word[1]; \ + lo =3D (stm)->word[0] & ~(uint64_t)0x1f; \ + hi << 32 | lo; \ + }) + +#define STMSPAN(stm) (1 << (extract32((stm)->word[0], 0, 4) - 1)) + +/***************************** + * Debug + *****************************/ + +#ifdef ARM_SMMU_DEBUG +static inline void dump_ste(Ste *ste) +{ + int i; + + for (i =3D 0; i < ARRAY_SIZE(ste->word); i +=3D 2) { + SMMU_DPRINTF(STE, "STE[%2d]: %#010x\t STE[%2d]: %#010x\n", + i, ste->word[i], i + 1, ste->word[i + 1]); + } +} + +static inline void dump_cd(Cd *cd) +{ + int i; + for (i =3D 0; i < ARRAY_SIZE(cd->word); i +=3D 2) { + SMMU_DPRINTF(CD, "CD[%2d]: %#010x\t CD[%2d]: %#010x\n", + i, cd->word[i], i + 1, cd->word[i + 1]); + } +} + +static inline void dump_evt(Evt *e) +{} + +static inline void dump_cmd(Cmd *cmd) +{ + int i; + for (i =3D 0; i < ARRAY_SIZE(cmd->word); i +=3D 2) { + SMMU_DPRINTF(CMDQ, "CMD[%2d]: %#010x\t CMD[%2d]: %#010x\n", + i, cmd->word[i], i + 1, cmd->word[i + 1]); + } +} + +static void dump_smmutranscfg(SMMUTransCfg *cfg) +{ + int i; + SMMU_DPRINTF(TT_1, "TransCFG stage:%d va:%lx pa:%lx s2_needed:%d\n", + cfg->stage, cfg->va, cfg->pa, cfg->s2_needed); + for (i =3D 1; i <=3D 2; i++) { + SMMU_DPRINTF(TT_1, "TransCFG i:%d oas:%x tsz:%x ttbr:%lx granule:%= x" + " va_size:%x gran_sz:%x\n", i, cfg->oas[i], cfg->tsz[= i], + cfg->ttbr[i], cfg->granule[i], cfg->va_size[i], + cfg->granule_sz[i]); + } +} + +#else +#define dump_ste(...) do {} while (0) +#define dump_cd(...) do {} while (0) +#define dump_evt(...) do {} while (0) +#define dump_cmd(...) do {} while (0) +static void dump_smmutranscfg(SMMUTransCfg *cfg) {} +#endif + +#endif diff --git a/hw/arm/smmuv3.c b/hw/arm/smmuv3.c new file mode 100644 index 0000000..e4c091a --- /dev/null +++ b/hw/arm/smmuv3.c @@ -0,0 +1,1131 @@ +/* + * Copyright (C) 2014-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 as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include "qemu/osdep.h" +#include "hw/boards.h" +#include "sysemu/sysemu.h" +#include "hw/sysbus.h" +#include "hw/pci/pci.h" +#include "exec/address-spaces.h" + +#include "hw/arm/smmuv3.h" +#include "smmuv3-internal.h" + +static inline int smmu_enabled(SMMUV3State *s) +{ + return smmu_read32_reg(s, SMMU_REG_CR0) & SMMU_CR0_SMMU_ENABLE; +} + +/** + * smmu_irq_update - update the GERROR register according to + * the IRQ and the enable state + * + * return > 0 when IRQ is supposed to be raised + * Spec req: + * Raise irq only when it not active already, + * blindly toggling bits may actually clear the error + */ +static int smmu_irq_update(SMMUV3State *s, int irq, uint64_t data) +{ + uint32_t error =3D 0; + + switch (irq) { + case SMMU_IRQ_EVTQ: + if (smmu_evt_irq_enabled(s)) { + error =3D SMMU_GERROR_EVENTQ; + } + break; + case SMMU_IRQ_CMD_SYNC: + if (smmu_gerror_irq_enabled(s)) { + uint32_t err_type =3D (uint32_t)data; + + if (err_type) { + uint32_t regval =3D smmu_read32_reg(s, SMMU_REG_CMDQ_CONS); + smmu_write32_reg(s, SMMU_REG_CMDQ_CONS, + regval | err_type << SMMU_CMD_CONS_ERR_SH= IFT); + } + error =3D SMMU_GERROR_CMDQ; + } + break; + case SMMU_IRQ_PRIQ: + if (smmu_pri_irq_enabled(s)) { + error =3D SMMU_GERROR_PRIQ; + } + break; + } + SMMU_DPRINTF(IRQ, "<< error:%x\n", error); + + if (error && smmu_gerror_irq_enabled(s)) { + uint32_t gerror =3D smmu_read32_reg(s, SMMU_REG_GERROR); + uint32_t gerrorn =3D smmu_read32_reg(s, SMMU_REG_GERRORN); + + SMMU_DPRINTF(IRQ, "<<<< error:%x gerror:%x gerrorn:%x\n", + error, gerror, gerrorn); + + if (!((gerror ^ gerrorn) & error)) { + smmu_write32_reg(s, SMMU_REG_GERROR, gerror ^ error); + } + } + + return error; +} + +static void smmu_irq_raise(SMMUV3State *s, int irq, uint64_t data) +{ + SMMU_DPRINTF(IRQ, "irq:%d\n", irq); + if (smmu_irq_update(s, irq, data)) { + qemu_irq_raise(s->irq[irq]); + } +} + +static MemTxResult smmu_q_read(SMMUV3State *s, SMMUQueue *q, void *data) +{ + uint64_t addr =3D Q_ENTRY(q, Q_IDX(q, q->cons)); + + q->cons++; + if (q->cons =3D=3D q->entries) { + q->cons =3D 0; + q->wrap.cons++; /* this will toggle */ + } + + return smmu_read_sysmem(addr, data, q->ent_size, false); +} + +static MemTxResult smmu_q_write(SMMUV3State *s, SMMUQueue *q, void *data) +{ + uint64_t addr =3D Q_ENTRY(q, Q_IDX(q, q->prod)); + + if (q->prod =3D=3D q->entries) { + q->prod =3D 0; + q->wrap.prod++; /* this will toggle */ + } + + q->prod++; + + smmu_write_sysmem(addr, data, q->ent_size, false); + + return MEMTX_OK; +} + +static MemTxResult smmu_read_cmdq(SMMUV3State *s, Cmd *cmd) +{ + SMMUQueue *q =3D &s->cmdq; + MemTxResult ret =3D smmu_q_read(s, q, cmd); + uint32_t val =3D 0; + + val |=3D (q->wrap.cons << q->shift) | q->cons; + + /* Update consumer pointer */ + smmu_write32_reg(s, SMMU_REG_CMDQ_CONS, val); + + return ret; +} + +static int smmu_cmdq_consume(SMMUV3State *s) +{ + uint32_t error =3D SMMU_CMD_ERR_NONE; + + SMMU_DPRINTF(CMDQ, "CMDQ_ERR: %d\n", SMMU_CMDQ_ERR(s)); + + if (!smmu_cmd_q_enabled(s)) { + goto out; + } + + while (!SMMU_CMDQ_ERR(s) && !smmu_is_q_empty(s, &s->cmdq)) { + Cmd cmd; +#ifdef ARM_SMMU_DEBUG + SMMUQueue *q =3D &s->cmdq; +#endif + if (smmu_read_cmdq(s, &cmd) !=3D MEMTX_OK) { + error =3D SMMU_CMD_ERR_ABORT; + goto out; + } + + SMMU_DPRINTF(DBG2, "CMDQ base: %lx cons:%d prod:%d val:%x wrap:%d\= n", + q->base, q->cons, q->prod, cmd.word[0], q->wrap.cons); + + switch (CMD_TYPE(&cmd)) { + case SMMU_CMD_CFGI_STE: + case SMMU_CMD_CFGI_STE_RANGE: + break; + case SMMU_CMD_TLBI_NSNH_ALL: /* TLB not implemented */ + case SMMU_CMD_TLBI_EL2_ALL: /* Fallthrough */ + case SMMU_CMD_TLBI_EL3_ALL: + case SMMU_CMD_TLBI_NH_ALL: + case SMMU_CMD_TLBI_S2_IPA: + break; + case SMMU_CMD_SYNC: /* Fallthrough */ + if (CMD_CS(&cmd) & CMD_SYNC_SIG_IRQ) { + smmu_irq_raise(s, SMMU_IRQ_CMD_SYNC, SMMU_CMD_ERR_NONE); + } + break; + case SMMU_CMD_PREFETCH_CONFIG: + break; + case SMMU_CMD_TLBI_NH_ASID: + case SMMU_CMD_TLBI_NH_VA: /* too many of this is sent */ + break; + + default: + error =3D SMMU_CMD_ERR_ILLEGAL; + SMMU_DPRINTF(CRIT, "Unknown Command type: %x, ignoring\n", + CMD_TYPE(&cmd)); + if (IS_DBG_ENABLED(CD)) { + dump_cmd(&cmd); + } + break; + } + + if (error !=3D SMMU_CMD_ERR_NONE) { + SMMU_DPRINTF(INFO, "CMD Error\n"); + goto out; + } + } + +out: + if (error) { + smmu_irq_raise(s, SMMU_IRQ_GERROR, error); + } + + SMMU_DPRINTF(CMDQ, "prod_wrap:%d, prod:%x cons_wrap:%d cons:%x\n", + s->cmdq.wrap.prod, s->cmdq.prod, + s->cmdq.wrap.cons, s->cmdq.cons); + + return 0; +} + +/* + * GERROR is updated when raising an interrupt, GERRORN will be updated + * by s/w and should match GERROR before normal operation resumes. + */ +static void smmu_irq_clear(SMMUV3State *s, uint64_t gerrorn) +{ + int irq =3D SMMU_IRQ_GERROR; + uint32_t toggled; + + toggled =3D smmu_read32_reg(s, SMMU_REG_GERRORN) ^ gerrorn; + + while (toggled) { + irq =3D ctz32(toggled); + + qemu_irq_lower(s->irq[irq]); + + toggled &=3D toggled - 1; + } +} + +static int smmu_evtq_update(SMMUV3State *s) +{ + if (!smmu_enabled(s)) { + return 0; + } + + if (!smmu_is_q_empty(s, &s->evtq)) { + if (smmu_evt_irq_enabled(s)) { + smmu_irq_raise(s, SMMU_IRQ_EVTQ, 0); + } + } + + if (smmu_is_q_empty(s, &s->evtq)) { + smmu_irq_clear(s, SMMU_GERROR_EVENTQ); + } + + return 1; +} + +static void smmu_create_event(SMMUV3State *s, hwaddr iova, + uint32_t sid, bool is_write, int error); + +static void smmu_update(SMMUV3State *s) +{ + int error =3D 0; + + /* SMMU starts processing commands even when not enabled */ + if (!smmu_enabled(s)) { + goto check_cmdq; + } + + /* EVENT Q updates takes more priority */ + if ((smmu_evt_q_enabled(s)) && !smmu_is_q_empty(s, &s->evtq)) { + SMMU_DPRINTF(CRIT, "q empty:%d prod:%d cons:%d p.wrap:%d p.cons:%d= \n", + smmu_is_q_empty(s, &s->evtq), s->evtq.prod, + s->evtq.cons, s->evtq.wrap.prod, s->evtq.wrap.cons); + error =3D smmu_evtq_update(s); + } + + if (error) { + /* TODO: May be in future we create proper event queue entry */ + /* an error condition is not a recoverable event, like other devic= es */ + SMMU_DPRINTF(CRIT, "An unfavourable condition\n"); + smmu_create_event(s, 0, 0, 0, error); + } + +check_cmdq: + if (smmu_cmd_q_enabled(s) && !SMMU_CMDQ_ERR(s)) { + smmu_cmdq_consume(s); + } else { + SMMU_DPRINTF(INFO, "cmdq not enabled or error :%x\n", SMMU_CMDQ_ER= R(s)); + } + +} + +static void smmu_update_irq(SMMUV3State *s, uint64_t addr, uint64_t val) +{ + smmu_irq_clear(s, val); + + smmu_write32_reg(s, SMMU_REG_GERRORN, val); + + SMMU_DPRINTF(IRQ, "irq pend: %d gerror:%x gerrorn:%x\n", + smmu_is_irq_pending(s, 0), + smmu_read32_reg(s, SMMU_REG_GERROR), + smmu_read32_reg(s, SMMU_REG_GERRORN)); + + /* Clear only when no more left */ + if (!smmu_is_irq_pending(s, 0)) { + qemu_irq_lower(s->irq[0]); + } +} + +#define SMMU_ID_REG_INIT(s, reg, d) do { \ + s->regs[reg >> 2] =3D d; \ + } while (0) + +static void smmuv3_id_reg_init(SMMUV3State *s) +{ + uint32_t data =3D + 1 << 27 | /* 2 Level stream id */ + 1 << 26 | /* Term Model */ + 1 << 24 | /* Stall model not supported */ + 1 << 18 | /* VMID 16 bits */ + 1 << 16 | /* PRI */ + 1 << 12 | /* ASID 16 bits */ + 1 << 10 | /* ATS */ + 1 << 9 | /* HYP */ + 2 << 6 | /* HTTU */ + 1 << 4 | /* COHACC */ + 2 << 2 | /* TTF=3DArch64 */ + 1 << 1 | /* Stage 1 */ + 1 << 0; /* Stage 2 */ + + SMMU_ID_REG_INIT(s, SMMU_REG_IDR0, data); + +#define SMMU_SID_SIZE 16 +#define SMMU_QUEUE_SIZE_LOG2 19 + data =3D + 1 << 27 | /* Attr Types override */ + SMMU_QUEUE_SIZE_LOG2 << 21 | /* Cmd Q size */ + SMMU_QUEUE_SIZE_LOG2 << 16 | /* Event Q size */ + SMMU_QUEUE_SIZE_LOG2 << 11 | /* PRI Q size */ + 0 << 6 | /* SSID not supported */ + SMMU_SID_SIZE << 0 ; /* SID size */ + + SMMU_ID_REG_INIT(s, SMMU_REG_IDR1, data); + + data =3D + 1 << 6 | /* Granule 64K */ + 1 << 4 | /* Granule 4K */ + 4 << 0; /* OAS =3D 44 bits */ + + SMMU_ID_REG_INIT(s, SMMU_REG_IDR5, data); + +} + +static void smmuv3_init(SMMUV3State *s) +{ + smmuv3_id_reg_init(s); /* Update ID regs alone */ + + s->sid_size =3D SMMU_SID_SIZE; + + s->cmdq.entries =3D (smmu_read32_reg(s, SMMU_REG_IDR1) >> 21) & 0x1f; + s->cmdq.ent_size =3D sizeof(Cmd); + s->evtq.entries =3D (smmu_read32_reg(s, SMMU_REG_IDR1) >> 16) & 0x1f; + s->evtq.ent_size =3D sizeof(Evt); +} + +/* + * All SMMU data structures are little endian, and are aligned to 8 bytes + * L1STE/STE/L1CD/CD, Queue entries in CMDQ/EVTQ/PRIQ + */ +static inline int smmu_get_ste(SMMUV3State *s, hwaddr addr, Ste *buf) +{ + return dma_memory_read(&address_space_memory, addr, buf, sizeof(*buf)); +} + +/* + * For now we only support CD with a single entry, 'ssid' is used to ident= ify + * otherwise + */ +static inline int smmu_get_cd(SMMUV3State *s, Ste *ste, uint32_t ssid, Cd = *buf) +{ + hwaddr addr =3D STE_CTXPTR(ste); + + if (STE_S1CDMAX(ste) !=3D 0) { + SMMU_DPRINTF(CRIT, "Multilevel Ctx Descriptor not supported yet\n"= ); + } + + return dma_memory_read(&address_space_memory, addr, buf, sizeof(*buf)); +} + +static int +is_ste_consistent(SMMUV3State *s, Ste *ste) +{ + uint32_t _config =3D STE_CONFIG(ste) & 0x7, + idr0 =3D smmu_read32_reg(s, SMMU_REG_IDR0), + idr5 =3D smmu_read32_reg(s, SMMU_REG_IDR5); + + uint32_t httu =3D extract32(idr0, 6, 2); + bool config[] =3D {_config & 0x1, + _config & 0x2, + _config & 0x3}; + bool granule_supported; + + bool s1p =3D idr0 & SMMU_IDR0_S1P, + s2p =3D idr0 & SMMU_IDR0_S2P, + hyp =3D idr0 & SMMU_IDR0_HYP, + cd2l =3D idr0 & SMMU_IDR0_CD2L, + idr0_vmid =3D idr0 & SMMU_IDR0_VMID16, + ats =3D idr0 & SMMU_IDR0_ATS, + ttf0 =3D (idr0 >> 2) & 0x1, + ttf1 =3D (idr0 >> 3) & 0x1; + + int ssidsz =3D (smmu_read32_reg(s, SMMU_REG_IDR1) >> 6) & 0x1f; + + uint32_t ste_vmid =3D STE_S2VMID(ste), + ste_eats =3D STE_EATS(ste), + ste_s2s =3D STE_S2S(ste), + ste_s1fmt =3D STE_S1FMT(ste), + aa64 =3D STE_S2AA64(ste), + ste_s1cdmax =3D STE_S1CDMAX(ste); + + uint8_t ste_strw =3D STE_STRW(ste); + uint64_t oas, max_pa; + bool strw_ign; + bool addr_out_of_range; + + if (!STE_VALID(ste)) { + SMMU_DPRINTF(STE, "STE NOT valid\n"); + return false; + } + + switch (STE_S2TG(ste)) { + case 1: + granule_supported =3D 0x4; break; + case 2: + granule_supported =3D 0x2; break; + case 0: + granule_supported =3D 0x1; break; + } + granule_supported &=3D (idr5 >> 4); + + if (!config[2]) { + if ((!s1p && config[0]) || + (!s2p && config[1]) || + (s2p && config[1])) { + SMMU_DPRINTF(STE, "STE inconsistant, S2P mismatch\n"); + return false; + } + if (!ssidsz && ste_s1cdmax && config[0] && !cd2l && + (ste_s1fmt =3D=3D 1 || ste_s1fmt =3D=3D 2)) { + SMMU_DPRINTF(STE, "STE inconsistant, CD mismatch\n"); + return false; + } + if (ats && ((_config & 0x3) =3D=3D 0) && + ((ste_eats =3D=3D 2 && (_config !=3D 0x7 || ste_s2s)) || + (ste_eats =3D=3D 1 && !ste_s2s))) { + SMMU_DPRINTF(STE, "STE inconsistant, EATS/S2S mismatch\n"); + return false; + } + if (config[0] && (ssidsz && (ste_s1cdmax > ssidsz))) { + SMMU_DPRINTF(STE, "STE inconsistant, SSID out of range\n"); + return false; + } + } + + oas =3D MIN(STE_S2PS(ste), idr5 & 0x7); + + if (oas =3D=3D 3) { + max_pa =3D deposit64(0, 0, 42, ~0UL); + } else { + max_pa =3D deposit64(0, 0, (32 + (oas * 4)), ~0UL); + } + + strw_ign =3D (!s1p || !hyp || (_config =3D=3D 4)); + + addr_out_of_range =3D (int64_t)(max_pa - STE_S2TTB(ste)) < 0; + + if (config[1] && ( + (aa64 && !granule_supported) || + (!aa64 && !ttf0) || + (aa64 && !ttf1) || + ((STE_S2HA(ste) || STE_S2HD(ste)) && !aa64) || + ((STE_S2HA(ste) || STE_S2HD(ste)) && !httu) || + (STE_S2HD(ste) && (httu =3D=3D 1)) || + addr_out_of_range)) { + SMMU_DPRINTF(STE, "STE inconsistant\n"); + SMMU_DPRINTF(STE, "config[1]:%d gran:%d addr:%d\n" + " aa64:%d ttf0:%d ttf1:%d s2ha:%d s2hd:%d httu:%d\n", + config[1], granule_supported, + addr_out_of_range, aa64, ttf0, ttf1, STE_S2HA(ste), + STE_S2HD(ste), httu); + SMMU_DPRINTF(STE, "maxpa:%lx s2ttb:%lx\n", max_pa, STE_S2TTB(ste)); + return false; + } + if (s2p && (config[0] =3D=3D 0 && config[1]) && + (strw_ign || !ste_strw) && !idr0_vmid && !(ste_vmid >> 8)) { + SMMU_DPRINTF(STE, "STE inconsistant, VMID out of range\n"); + return false; + } + + return true; +} + +static int smmu_find_ste(SMMUV3State *s, uint16_t sid, Ste *ste) +{ + hwaddr addr; + + SMMU_DPRINTF(STE, "SID:%x\n", sid); + /* Check SID range */ + if (sid > (1 << s->sid_size)) { + return SMMU_EVT_C_BAD_SID; + } + SMMU_DPRINTF(STE, "features:%x\n", s->features); + if (s->features & SMMU_FEATURE_2LVL_STE) { + int span; + hwaddr stm_addr; + STEDesc stm; + int l1_ste_offset, l2_ste_offset; + SMMU_DPRINTF(STE, "no. ste: %x\n", s->sid_split); + + l1_ste_offset =3D sid >> s->sid_split; + l2_ste_offset =3D sid & ((1 << s->sid_split) - 1); + SMMU_DPRINTF(STE, "l1_off:%x, l2_off:%x\n", l1_ste_offset, + l2_ste_offset); + stm_addr =3D (hwaddr)(s->strtab_base + l1_ste_offset * sizeof(stm)= ); + smmu_read_sysmem(stm_addr, &stm, sizeof(stm), false); + + SMMU_DPRINTF(STE, "strtab_base:%lx stm_addr:%lx\n" + "l1_ste_offset:%x l1(64):%#016lx\n", + s->strtab_base, stm_addr, l1_ste_offset, + STM2U64(&stm)); + + span =3D STMSPAN(&stm); + SMMU_DPRINTF(STE, "l2_ste_offset:%x ~ span:%d\n", l2_ste_offset, s= pan); + if (l2_ste_offset > span) { + SMMU_DPRINTF(CRIT, "l2_ste_offset > span\n"); + return SMMU_EVT_C_BAD_STE; + } + addr =3D STM2U64(&stm) + l2_ste_offset * sizeof(*ste); + } else { + addr =3D s->strtab_base + sid * sizeof(*ste); + } + SMMU_DPRINTF(STE, "ste:%lx\n", addr); + if (smmu_get_ste(s, addr, ste)) { + SMMU_DPRINTF(CRIT, "Unable to Fetch STE\n"); + return SMMU_EVT_F_UUT; + } + + return 0; +} + +static void smmu_cfg_populate_s2(SMMUTransCfg *cfg, Ste *ste) +{ /* stage 2 cfg */ + bool s2a64 =3D STE_S2AA64(ste); + const int stage =3D 2; + + cfg->granule[stage] =3D STE_S2TG(ste); + cfg->tsz[stage] =3D STE_S2T0SZ(ste); + cfg->ttbr[stage] =3D STE_S2TTB(ste); + cfg->oas[stage] =3D oas2bits(STE_S2PS(ste)); + + if (s2a64) { + cfg->tsz[stage] =3D MIN(cfg->tsz[stage], 39); + cfg->tsz[stage] =3D MAX(cfg->tsz[stage], 16); + } + cfg->va_size[stage] =3D STE_S2AA64(ste) ? 64 : 32; + cfg->granule_sz[stage] =3D tg2granule(cfg->granule[stage], 0) - 3; +} + +static void smmu_cfg_populate_s1(SMMUTransCfg *cfg, Cd *cd) +{ /* stage 1 cfg */ + bool s1a64 =3D CD_AARCH64(cd); + const int stage =3D 1; + + cfg->granule[stage] =3D (CD_EPD0(cd)) ? CD_TG1(cd) : CD_TG0(cd); + cfg->tsz[stage] =3D (CD_EPD0(cd)) ? CD_T1SZ(cd) : CD_T0SZ(cd); + cfg->ttbr[stage] =3D (CD_EPD0(cd)) ? CD_TTB1(cd) : CD_TTB0(cd); + cfg->oas[stage] =3D oas2bits(CD_IPS(cd)); + + if (s1a64) { + cfg->tsz[stage] =3D MIN(cfg->tsz[stage], 39); + cfg->tsz[stage] =3D MAX(cfg->tsz[stage], 16); + } + cfg->va_size[stage] =3D CD_AARCH64(cd) ? 64 : 32; + cfg->granule_sz[stage] =3D tg2granule(cfg->granule[stage], CD_EPD0(cd)= ) - 3; +} + +static SMMUEvtErr smmu_walk_pgtable(SMMUV3State *s, Ste *ste, Cd *cd, + IOMMUTLBEntry *tlbe, bool is_write) +{ + SMMUState *sys =3D SMMU_SYS_DEV(s); + SMMUBaseClass *sbc =3D SMMU_DEVICE_GET_CLASS(sys); + SMMUTransCfg _cfg =3D {}; + SMMUTransCfg *cfg =3D &_cfg; + SMMUEvtErr retval =3D 0; + uint32_t ste_cfg =3D STE_CONFIG(ste); + uint32_t page_size =3D 0, perm =3D 0; + hwaddr pa; /* Input address, output address */ + int stage =3D 0; + + SMMU_DPRINTF(DBG1, "ste_cfg :%x\n", ste_cfg); + /* Both Bypass, we dont need to do anything */ + if (is_ste_bypass(s, ste)) { + return 0; + } + + SMMU_DPRINTF(TT_1, "Input addr: %lx ste_config:%d\n", + tlbe->iova, ste_cfg); + + if (ste_cfg & STE_CONFIG_S1TR) { + stage =3D cfg->stage =3D 1; + smmu_cfg_populate_s1(cfg, cd); + + cfg->oas[stage] =3D MIN(oas2bits(smmu_read32_reg(s, SMMU_REG_IDR5)= & 0xf), + cfg->oas[stage]); + /* fix ttbr - make top bits zero*/ + cfg->ttbr[stage] =3D extract64(cfg->ttbr[stage], 0, cfg->oas[stage= ]); + cfg->s2_needed =3D (STE_CONFIG(ste) =3D=3D STE_CONFIG_S1TR_S2TR) ?= 1 : 0; + + SMMU_DPRINTF(DBG1, "S1 populated\n "); + } + + if (ste_cfg & STE_CONFIG_S2TR) { + stage =3D 2; + if (cfg->stage) { /* S1+S2 */ + cfg->s2_needed =3D true; + } else { /* Stage2 only */ + cfg->stage =3D stage; + } + + /* Stage2 only configuratoin */ + smmu_cfg_populate_s2(cfg, ste); + + cfg->oas[stage] =3D MIN(oas2bits(smmu_read32_reg(s, SMMU_REG_IDR5)= & 0xf), + cfg->oas[stage]); + /* fix ttbr - make top bits zero*/ + cfg->ttbr[stage] =3D extract64(cfg->ttbr[stage], 0, cfg->oas[stage= ]); + + SMMU_DPRINTF(DBG1, "S2 populated\n "); + } + + cfg->va =3D tlbe->iova; + + if ((cfg->stage =3D=3D 1 && CD_AARCH64(cd)) || + STE_S2AA64(ste)) { + SMMU_DPRINTF(DBG1, "Translate 64\n"); + retval =3D sbc->translate_64(cfg, &page_size, &perm, + is_write); + } else { + SMMU_DPRINTF(DBG1, "Translate 32\n"); + retval =3D sbc->translate_32(cfg, &page_size, &perm, is_write); + } + + if (retval !=3D 0) { + SMMU_DPRINTF(CRIT, "FAILED Stage1 translation\n"); + goto exit; + } + pa =3D cfg->pa; + + SMMU_DPRINTF(TT_1, "DONE: o/p addr:%lx mask:%x is_write:%d\n ", + pa, page_size - 1, is_write); + tlbe->translated_addr =3D pa; + tlbe->addr_mask =3D page_size - 1; + tlbe->perm =3D perm; + +exit: + dump_smmutranscfg(cfg); + return retval; +} + +static MemTxResult smmu_write_evtq(SMMUV3State *s, Evt *evt) +{ + SMMUQueue *q =3D &s->evtq; + int ret =3D smmu_q_write(s, q, evt); + uint32_t val =3D 0; + + val |=3D (q->wrap.prod << q->shift) | q->prod; + + smmu_write32_reg(s, SMMU_REG_EVTQ_PROD, val); + + return ret; +} + +/* + * Events created on the EventQ + */ +static void smmu_create_event(SMMUV3State *s, hwaddr iova, + uint32_t sid, bool is_write, int error) +{ + SMMUQueue *q =3D &s->evtq; + uint64_t head; + Evt evt; + + if (!smmu_evt_q_enabled(s)) { + return; + } + + EVT_SET_TYPE(&evt, error); + EVT_SET_SID(&evt, sid); + + switch (error) { + case SMMU_EVT_F_UUT: + case SMMU_EVT_C_BAD_STE: + break; + case SMMU_EVT_C_BAD_CD: + case SMMU_EVT_F_CD_FETCH: + break; + case SMMU_EVT_F_TRANS_FORBIDDEN: + case SMMU_EVT_F_WALK_EXT_ABRT: + EVT_SET_INPUT_ADDR(&evt, iova); + default: + break; + } + + smmu_write_evtq(s, &evt); + + head =3D Q_IDX(q, q->prod); + + if (smmu_is_q_full(s, &s->evtq)) { + head =3D q->prod ^ (1 << 31); /* Set overflow */ + } + + smmu_write32_reg(s, SMMU_REG_EVTQ_PROD, head); + + smmu_irq_raise(s, SMMU_IRQ_EVTQ, (uint64_t)&evt); +} + +/* + * TR - Translation Request + * TT - Translated Tansaction + * OT - Other Transaction + */ +static IOMMUTLBEntry +smmuv3_translate(MemoryRegion *mr, hwaddr addr, bool is_write) +{ + SMMUDevice *sdev =3D container_of(mr, SMMUDevice, iommu); + SMMUV3State *s =3D sdev->smmu; + uint16_t sid =3D 0, config; + Ste ste; + Cd cd; + SMMUEvtErr error =3D 0; + + IOMMUTLBEntry ret =3D { + .target_as =3D &address_space_memory, + .iova =3D addr, + .translated_addr =3D addr, + .addr_mask =3D ~(hwaddr)0, + .perm =3D IOMMU_NONE, + }; + + /* SMMU Bypass, We allow traffic through if SMMU is disabled */ + if (!smmu_enabled(s)) { + SMMU_DPRINTF(CRIT, "SMMU Not enabled.. bypassing addr:%lx\n", addr= ); + goto bypass; + } + + sid =3D smmu_get_sid(sdev); + SMMU_DPRINTF(TT_1, "SID:%x bus:%d ste_base:%lx\n", + sid, pci_bus_num(sdev->bus), s->strtab_base); + + /* Fetch & Check STE */ + error =3D smmu_find_ste(s, sid, &ste); + if (error) { + goto error_out; /* F_STE_FETCH or F_CFG_CONFLICT */ + } + + if (IS_DBG_ENABLED(STE)) { + dump_ste(&ste); + } + + if (is_ste_valid(s, &ste) && is_ste_bypass(s, &ste)) { + goto bypass; + } + + SMMU_DPRINTF(STE, "STE is not bypass\n"); + if (!is_ste_consistent(s, &ste)) { + error =3D SMMU_EVT_C_BAD_STE; + goto error_out; + } + SMMU_DPRINTF(INFO, "Valid STE Found\n"); + + /* Stream Bypass */ + config =3D STE_CONFIG(&ste) & 0x3; + + if (config & (STE_CONFIG_S1TR)) { + smmu_get_cd(s, &ste, 0, &cd); /* We dont have SSID yet, so 0 */ + SMMU_DPRINTF(INFO, "GET_CD CTXPTR:%p\n", (void *)STE_CTXPTR(&ste)); + if (IS_DBG_ENABLED(CD)) { + dump_cd(&cd); + } + + if (!is_cd_valid(s, &ste, &cd)) { + error =3D SMMU_EVT_C_BAD_CD; + goto error_out; + } + } + + /* Walk Stage1, if S2 is enabled, S2 walked for Every access on S1 */ + error =3D smmu_walk_pgtable(s, &ste, &cd, &ret, is_write); + + SMMU_DPRINTF(INFO, "DONE walking tables\n"); + +error_out: + if (error) { /* Post the Error using Event Q */ + SMMU_DPRINTF(CRIT, "Translation Error: %x\n", error); + smmu_create_event(s, ret.iova, sid, is_write, error); + goto out; + } + +bypass: + ret.perm =3D is_write ? IOMMU_RW : IOMMU_RO; + +out: + return ret; +} + +static const MemoryRegionIOMMUOps smmu_iommu_ops =3D { + .translate =3D smmuv3_translate, +}; + +static AddressSpace *smmu_init_pci_iommu(PCIBus *bus, void *opaque, int de= vfn) +{ + SMMUV3State *s =3D opaque; + SMMUState *sys =3D SMMU_SYS_DEV(s); + uintptr_t key =3D (uintptr_t)bus; + SMMUPciBus *sbus =3D g_hash_table_lookup(s->smmu_as_by_busptr, &key); + SMMUDevice *sdev; + + if (!sbus) { + sbus =3D g_malloc0(sizeof(SMMUPciBus) + + sizeof(SMMUDevice *) * SMMU_PCI_DEVFN_MAX); + sbus->bus =3D bus; + g_hash_table_insert(s->smmu_as_by_busptr, &key, sbus); + } + + sdev =3D sbus->pbdev[devfn]; + if (!sdev) { + sdev =3D sbus->pbdev[devfn] =3D g_malloc0(sizeof(SMMUDevice)); + + sdev->smmu =3D s; + sdev->bus =3D bus; + sdev->devfn =3D devfn; + + memory_region_init_iommu(&sdev->iommu, OBJECT(sys), + &smmu_iommu_ops, TYPE_SMMU_V3_DEV, UINT64= _MAX); + address_space_init(&sdev->as, &sdev->iommu, TYPE_SMMU_V3_DEV); + } + + return &sdev->as; +} + +static inline void smmu_update_base_reg(SMMUV3State *s, uint64_t *base, + uint64_t val) +{ + *base =3D val & ~(SMMU_BASE_RA | 0x3fULL); +} + +static void smmu_update_qreg(SMMUV3State *s, SMMUQueue *q, hwaddr reg, + uint32_t off, uint64_t val, unsigned size) +{ + if (size =3D=3D 8 && off =3D=3D 0) { + smmu_write64_reg(s, reg, val); + } else { + smmu_write_reg(s, reg, val); + } + + switch (off) { + case 0: /* BASE register */ + val =3D smmu_read64_reg(s, reg); + q->shift =3D val & 0x1f; + q->entries =3D 1 << (q->shift); + smmu_update_base_reg(s, &q->base, val); + break; + + case 4: /* CONS */ + q->cons =3D Q_IDX(q, val); + q->wrap.cons =3D val >> q->shift; + SMMU_DPRINTF(DBG2, "cons written : %d val:%lx\n", q->cons, val); + break; + + case 8: /* PROD */ + q->prod =3D Q_IDX(q, val); + q->wrap.prod =3D val >> q->shift; + break; + } + + switch (reg) { + case SMMU_REG_CMDQ_PROD: /* should be only for CMDQ_PROD */ + case SMMU_REG_CMDQ_CONS: /* but we do it anyway */ + smmu_update(s); + break; + } +} + +static void smmu_write_mmio_fixup(SMMUV3State *s, hwaddr *addr) +{ + switch (*addr) { + case 0x100a8: case 0x100ac: /* Aliasing =3D> page0 registers */ + case 0x100c8: case 0x100cc: + *addr ^=3D (hwaddr)0x10000; + } +} + +static void smmu_write_mmio(void *opaque, hwaddr addr, + uint64_t val, unsigned size) +{ + SMMUState *sys =3D opaque; + SMMUV3State *s =3D SMMU_V3_DEV(sys); + bool update =3D false; + + smmu_write_mmio_fixup(s, &addr); + + SMMU_DPRINTF(DBG2, "addr: %lx val:%lx\n", addr, val); + + switch (addr) { + case 0xFDC ... 0xFFC: + case SMMU_REG_IDR0 ... SMMU_REG_IDR5: + SMMU_DPRINTF(CRIT, "write to RO/Unimpl reg %lx val64:%lx\n", + addr, val); + return; + + case SMMU_REG_GERRORN: + smmu_update_irq(s, addr, val); + return; + + case SMMU_REG_CR0: + smmu_write32_reg(s, SMMU_REG_CR0_ACK, val); + update =3D true; + break; + + case SMMU_REG_IRQ_CTRL: + smmu_write32_reg(s, SMMU_REG_IRQ_CTRL_ACK, val); + update =3D true; + break; + + case SMMU_REG_STRTAB_BASE: + smmu_update_base_reg(s, &s->strtab_base, val); + return; + + case SMMU_REG_STRTAB_BASE_CFG: + if (((val >> 16) & 0x3) =3D=3D 0x1) { + s->sid_split =3D (val >> 6) & 0x1f; + s->features |=3D SMMU_FEATURE_2LVL_STE; + } + break; + + case SMMU_REG_CMDQ_PROD: + case SMMU_REG_CMDQ_CONS: + case SMMU_REG_CMDQ_BASE: + case SMMU_REG_CMDQ_BASE + 4: + smmu_update_qreg(s, &s->cmdq, addr, addr - SMMU_REG_CMDQ_BASE, + val, size); + return; + + case SMMU_REG_EVTQ_CONS: /* fallthrough */ + { + SMMUQueue *evtq =3D &s->evtq; + evtq->cons =3D Q_IDX(evtq, val); + evtq->wrap.cons =3D Q_WRAP(evtq, val); + + SMMU_DPRINTF(IRQ, "Before clearing interrupt " + "prod:%x cons:%x prod.w:%d cons.w:%d\n", + evtq->prod, evtq->cons, evtq->wrap.prod, evtq->wrap.c= ons); + if (smmu_is_q_empty(s, &s->evtq)) { + SMMU_DPRINTF(IRQ, "Clearing interrupt" + " prod:%x cons:%x prod.w:%d cons.w:%d\n", + evtq->prod, evtq->cons, evtq->wrap.prod, + evtq->wrap.cons); + qemu_irq_lower(s->irq[SMMU_IRQ_EVTQ]); + } + } + case SMMU_REG_EVTQ_BASE: + case SMMU_REG_EVTQ_BASE + 4: + case SMMU_REG_EVTQ_PROD: + smmu_update_qreg(s, &s->evtq, addr, addr - SMMU_REG_EVTQ_BASE, + val, size); + return; + + case SMMU_REG_PRIQ_CONS: + case SMMU_REG_PRIQ_BASE: + case SMMU_REG_PRIQ_BASE + 4: + case SMMU_REG_PRIQ_PROD: + smmu_update_qreg(s, &s->priq, addr, addr - SMMU_REG_PRIQ_BASE, + val, size); + return; + } + + if (size =3D=3D 8) { + smmu_write_reg(s, addr, val); + } else { + smmu_write32_reg(s, addr, (uint32_t)val); + } + + if (update) { + smmu_update(s); + } +} + +static uint64_t smmu_read_mmio(void *opaque, hwaddr addr, unsigned size) +{ + SMMUState *sys =3D opaque; + SMMUV3State *s =3D SMMU_V3_DEV(sys); + uint64_t val; + + smmu_write_mmio_fixup(s, &addr); + + /* Primecell/Corelink ID registers */ + switch (addr) { + case 0xFF0 ... 0xFFC: + case 0xFDC ... 0xFE4: + val =3D 0; + SMMU_DPRINTF(CRIT, "***************** addr: %lx val:%lx\n", addr, = val); + break; + + default: + val =3D (uint64_t)smmu_read32_reg(s, addr); + break; + + case SMMU_REG_STRTAB_BASE ... SMMU_REG_CMDQ_BASE: + case SMMU_REG_EVTQ_BASE: + case SMMU_REG_PRIQ_BASE ... SMMU_REG_PRIQ_IRQ_CFG1: + val =3D smmu_read64_reg(s, addr); + break; + } + + SMMU_DPRINTF(DBG2, "addr: %lx val:%lx\n", addr, val); + SMMU_DPRINTF(DBG2, "cmdq cons:%d\n", s->cmdq.cons); + return val; +} + +static const MemoryRegionOps smmu_mem_ops =3D { + .read =3D smmu_read_mmio, + .write =3D smmu_write_mmio, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 8, + }, +}; + +static void smmu_init_irq(SMMUV3State *s, SysBusDevice *dev) +{ + int i; + + for (i =3D 0; i < ARRAY_SIZE(s->irq); i++) { + sysbus_init_irq(dev, &s->irq[i]); + } +} + +static void smmu_init_iommu_as(SMMUV3State *sys) +{ + SMMUState *s =3D SMMU_SYS_DEV(sys); + PCIBus *pcibus =3D pci_find_primary_bus(); + + if (pcibus) { + SMMU_DPRINTF(CRIT, "Found PCI bus, setting up iommu\n"); + pci_setup_iommu(pcibus, smmu_init_pci_iommu, s); + } else { + SMMU_DPRINTF(CRIT, "No PCI bus, SMMU is not registered\n"); + } +} + +static void smmu_reset(DeviceState *dev) +{ + SMMUV3State *s =3D SMMU_V3_DEV(dev); + smmuv3_init(s); +} + +static int smmu_populate_internal_state(void *opaque, int version_id) +{ + SMMUV3State *s =3D opaque; + + smmu_update(s); + return 0; +} + +static void smmu_realize(DeviceState *d, Error **errp) +{ + SMMUState *sys =3D SMMU_SYS_DEV(d); + SMMUV3State *s =3D SMMU_V3_DEV(sys); + SysBusDevice *dev =3D SYS_BUS_DEVICE(d); + + /* Register Access */ + memory_region_init_io(&sys->iomem, OBJECT(s), + &smmu_mem_ops, sys, TYPE_SMMU_V3_DEV, 0x20000); + + s->smmu_as_by_busptr =3D g_hash_table_new_full(smmu_uint64_hash, + smmu_uint64_equal, + g_free, g_free); + sysbus_init_mmio(dev, &sys->iomem); + + smmu_init_irq(s, dev); + + smmu_init_iommu_as(s); +} + +static const VMStateDescription vmstate_smmuv3 =3D { + .name =3D "smmuv3", + .version_id =3D 1, + .minimum_version_id =3D 1, + .post_load =3D smmu_populate_internal_state, + .fields =3D (VMStateField[]) { + VMSTATE_UINT64_ARRAY(regs, SMMUV3State, SMMU_NREGS), + VMSTATE_END_OF_LIST(), + }, +}; + +static void smmuv3_instance_init(Object *obj) +{ + /* Nothing much to do here as of now */ +} + +static void smmuv3_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->reset =3D smmu_reset; + dc->vmsd =3D &vmstate_smmuv3; + dc->realize =3D smmu_realize; +} + +static const TypeInfo smmuv3_type_info =3D { + .name =3D TYPE_SMMU_V3_DEV, + .parent =3D TYPE_SMMU_DEV_BASE, + .instance_size =3D sizeof(SMMUV3State), + .instance_init =3D smmuv3_instance_init, + .class_data =3D NULL, + .class_size =3D sizeof(SMMUV3Class), + .class_init =3D smmuv3_class_init, +}; + +static void smmuv3_register_types(void) +{ + type_register(&smmuv3_type_info); +} + +type_init(smmuv3_register_types) + diff --git a/include/hw/arm/smmuv3.h b/include/hw/arm/smmuv3.h new file mode 100644 index 0000000..4e43f67 --- /dev/null +++ b/include/hw/arm/smmuv3.h @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2014-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 as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifndef HW_ARM_SMMUV3_H +#define HW_ARM_SMMUV3_H + +#include "hw/arm/smmu-common.h" + +#define SMMU_NREGS 0x200 +#define SMMU_PCI_DEVFN_MAX 256 + +typedef struct SMMUQueue { + hwaddr base; + uint32_t prod; + uint32_t cons; + union { + struct { + uint8_t prod:1; + uint8_t cons:1; + }; + uint8_t unused; + } wrap; + + uint16_t entries; /* Number of entries */ + uint8_t ent_size; /* Size of entry in bytes */ + uint8_t shift; /* Size in log2 */ +} SMMUQueue; + +typedef struct SMMUV3State { + SMMUState smmu_state; + +#define SMMU_FEATURE_2LVL_STE (1 << 0) + /* Local cache of most-frequently used register */ + uint32_t features; + uint16_t sid_size; + uint16_t sid_split; + uint64_t strtab_base; + + uint64_t regs[SMMU_NREGS]; + + qemu_irq irq[4]; + + SMMUQueue cmdq, evtq, priq; + + /* IOMMU Address space */ + MemoryRegion iommu; + AddressSpace iommu_as; + /* + * Bus number is not populated in the beginning, hence we need + * a mechanism to retrieve the corresponding address space for each + * pci device. + */ + GHashTable *smmu_as_by_busptr; +} SMMUV3State; + +typedef enum { + SMMU_IRQ_GERROR, + SMMU_IRQ_PRIQ, + SMMU_IRQ_EVTQ, + SMMU_IRQ_CMD_SYNC, +} SMMUIrq; + +typedef struct { + SMMUBaseClass smmu_base_class; +} SMMUV3Class; + +#define TYPE_SMMU_V3_DEV "smmuv3" +#define SMMU_V3_DEV(obj) OBJECT_CHECK(SMMUV3State, (obj), TYPE_SMMU_V3_DEV) +#define SMMU_V3_DEVICE_GET_CLASS(obj) \ + OBJECT_GET_CLASS(SMMUBaseClass, (obj), TYPE_SMMU_V3_DEV) + +#endif --=20 2.5.5 From nobody Tue Apr 30 00:11:19 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1490903246228829.5344170837471; Thu, 30 Mar 2017 12:47:26 -0700 (PDT) Received: from localhost ([::1]:37421 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctg2W-0006p7-Uz for importer@patchew.org; Thu, 30 Mar 2017 15:47:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40053) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctfy9-0003Ov-FQ for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ctfy8-0007IQ-7j for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46160) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ctfy3-0007G2-A2; Thu, 30 Mar 2017 15:42:47 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 363E3C0092C9; Thu, 30 Mar 2017 19:42:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-27.ams2.redhat.com [10.36.117.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id E6F5777D53; Thu, 30 Mar 2017 19:42:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 363E3C0092C9 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eric.auger@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 363E3C0092C9 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, peter.maydell@linaro.org, edgar.iglesias@gmail.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org, prem.mallappa@gmail.com Date: Thu, 30 Mar 2017 21:42:17 +0200 Message-Id: <1490902938-9009-5-git-send-email-eric.auger@redhat.com> In-Reply-To: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> References: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 30 Mar 2017 19:42:46 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC v3 4/5] hw/arm/virt: Add SMMUv3 to the virt board X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: drjones@redhat.com, Radha.Chintakuntla@cavium.com, christoffer.dall@linaro.org, Sunil.Goutham@cavium.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Prem Mallappa Add code to instantiate an smmu-v3 in mach-virt. A new boolean flag is introduced in VirtMachineState to allow this instantiation. It is currently false. Signed-off-by: Prem Mallappa Signed-off-by: Eric Auger --- v2 -> v3: - vbi was removed. Use vms instead - migrate to new smmu binding format (iommu-map) - don't use appendprop anymore - add vms->smmu and guard instantiation with this latter - interrupts type changed to edge --- hw/arm/virt.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ include/hw/arm/virt.h | 4 ++++ 2 files changed, 62 insertions(+) diff --git a/hw/arm/virt.c b/hw/arm/virt.c index 5f62a03..c00efb2 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -56,6 +56,7 @@ #include "hw/smbios/smbios.h" #include "qapi/visitor.h" #include "standard-headers/linux/input.h" +#include "hw/arm/smmuv3.h" =20 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \ static void virt_##major##_##minor##_class_init(ObjectClass *oc, \ @@ -139,6 +140,7 @@ static const MemMapEntry a15memmap[] =3D { [VIRT_FW_CFG] =3D { 0x09020000, 0x00000018 }, [VIRT_GPIO] =3D { 0x09030000, 0x00001000 }, [VIRT_SECURE_UART] =3D { 0x09040000, 0x00001000 }, + [VIRT_SMMU] =3D { 0x09050000, 0x00020000 }, /* 128K, nee= ded */ [VIRT_MMIO] =3D { 0x0a000000, 0x00000200 }, /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that siz= e */ [VIRT_PLATFORM_BUS] =3D { 0x0c000000, 0x02000000 }, @@ -159,6 +161,7 @@ static const int a15irqmap[] =3D { [VIRT_SECURE_UART] =3D 8, [VIRT_MMIO] =3D 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */ [VIRT_GIC_V2M] =3D 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */ + [VIRT_SMMU] =3D 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */ [VIRT_PLATFORM_BUS] =3D 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */ }; =20 @@ -969,6 +972,52 @@ static void create_pcie_irq_map(const VirtMachineState= *vms, 0x7 /* PCI irq */); } =20 +static void alloc_smmu_phandle(VirtMachineState *vms) +{ + if (vms->smmu && !vms->smmu_phandle) { + vms->smmu_phandle =3D qemu_fdt_alloc_phandle(vms->fdt); + } +} + +static void create_smmu(const VirtMachineState *vms, qemu_irq *pic) +{ + char *smmu; + const char compat[] =3D "arm,smmu-v3"; + int irq =3D vms->irqmap[VIRT_SMMU]; + hwaddr base =3D vms->memmap[VIRT_SMMU].base; + hwaddr size =3D vms->memmap[VIRT_SMMU].size; + const char irq_names[] =3D "eventq\0priq\0cmdq-sync\0gerror"; + + if (!vms->smmu) { + return; + } + + sysbus_create_varargs("smmuv3", base, pic[irq], pic[irq + 1], + pic[irq + 2], pic[irq + 3], NULL); + + smmu =3D g_strdup_printf("/smmuv3@%" PRIx64, base); + qemu_fdt_add_subnode(vms->fdt, smmu); + qemu_fdt_setprop(vms->fdt, smmu, "compatible", compat, sizeof(compat)); + qemu_fdt_setprop_sized_cells(vms->fdt, smmu, "reg", 2, base, 2, size); + + qemu_fdt_setprop_cells(vms->fdt, smmu, "interrupts", + GIC_FDT_IRQ_TYPE_SPI, irq , GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, + GIC_FDT_IRQ_TYPE_SPI, irq + 1, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, + GIC_FDT_IRQ_TYPE_SPI, irq + 2, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI, + GIC_FDT_IRQ_TYPE_SPI, irq + 3, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI); + + qemu_fdt_setprop(vms->fdt, smmu, "interrupt-names", irq_names, + sizeof(irq_names)); + + qemu_fdt_setprop_cell(vms->fdt, smmu, "clocks", vms->clock_phandle); + qemu_fdt_setprop_string(vms->fdt, smmu, "clock-names", "apb_pclk"); + + qemu_fdt_setprop_cell(vms->fdt, smmu, "#iommu-cells", 1); + + qemu_fdt_setprop_cell(vms->fdt, smmu, "phandle", vms->smmu_phandle); + g_free(smmu); +} + static void create_pcie(const VirtMachineState *vms, qemu_irq *pic) { hwaddr base_mmio =3D vms->memmap[VIRT_PCIE_MMIO].base; @@ -1081,6 +1130,11 @@ static void create_pcie(const VirtMachineState *vms,= qemu_irq *pic) qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 1); create_pcie_irq_map(vms, vms->gic_phandle, irq, nodename); =20 + if (vms->smmu) { + qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map", + 0x0, vms->smmu_phandle, 0x0, 0x10000); + } + g_free(nodename); } =20 @@ -1402,8 +1456,12 @@ static void machvirt_init(MachineState *machine) =20 create_rtc(vms, pic); =20 + alloc_smmu_phandle(vms); + create_pcie(vms, pic); =20 + create_smmu(vms, pic); + create_gpio(vms, pic); =20 /* Create mmio transports, so the user can create virtio backends diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h index 33b0ff3..164a531 100644 --- a/include/hw/arm/virt.h +++ b/include/hw/arm/virt.h @@ -38,6 +38,7 @@ =20 #define NUM_GICV2M_SPIS 64 #define NUM_VIRTIO_TRANSPORTS 32 +#define NUM_SMMU_IRQS 4 =20 #define ARCH_GICV3_MAINT_IRQ 9 =20 @@ -59,6 +60,7 @@ enum { VIRT_GIC_V2M, VIRT_GIC_ITS, VIRT_GIC_REDIST, + VIRT_SMMU, VIRT_UART, VIRT_MMIO, VIRT_RTC, @@ -95,6 +97,7 @@ typedef struct { bool highmem; bool its; bool virt; + bool smmu; int32_t gic_version; struct arm_boot_info bootinfo; const MemMapEntry *memmap; @@ -105,6 +108,7 @@ typedef struct { uint32_t clock_phandle; uint32_t gic_phandle; uint32_t msi_phandle; + uint32_t smmu_phandle; int psci_conduit; } VirtMachineState; =20 --=20 2.5.5 From nobody Tue Apr 30 00:11:19 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149090316867196.39367715723597; Thu, 30 Mar 2017 12:46:08 -0700 (PDT) Received: from localhost ([::1]:37414 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctg1H-0005tl-E4 for importer@patchew.org; Thu, 30 Mar 2017 15:46:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40065) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctfyB-0003Qj-30 for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ctfy9-0007J7-Vb for qemu-devel@nongnu.org; Thu, 30 Mar 2017 15:42:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34396) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ctfy7-0007Hv-0z; Thu, 30 Mar 2017 15:42:51 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D49E480493; Thu, 30 Mar 2017 19:42:49 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-27.ams2.redhat.com [10.36.117.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1DF577D53; Thu, 30 Mar 2017 19:42:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D49E480493 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eric.auger@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D49E480493 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, peter.maydell@linaro.org, edgar.iglesias@gmail.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org, prem.mallappa@gmail.com Date: Thu, 30 Mar 2017 21:42:18 +0200 Message-Id: <1490902938-9009-6-git-send-email-eric.auger@redhat.com> In-Reply-To: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> References: <1490902938-9009-1-git-send-email-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 30 Mar 2017 19:42:50 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC v3 5/5] hw/arm/virt: Add 2.10 machine type X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: drjones@redhat.com, Radha.Chintakuntla@cavium.com, christoffer.dall@linaro.org, Sunil.Goutham@cavium.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The new machine type allows smmuv3 instantiation. A new option is introduced to turn the feature on/off (off by default). Signed-off-by: Eric Auger --- Another alternative would be to use the -device option as done on x86. As the smmu is a sysbus device, we would need to use the platform bus framework. This would work fine for the dt generation. However the feasibility needs to be studied for ACPI table generation. a Veuillez saisir le message de validation pour vos modifications. Les lign= es --- hw/arm/virt.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++= ++-- include/hw/arm/virt.h | 1 + include/hw/compat.h | 3 +++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/hw/arm/virt.c b/hw/arm/virt.c index c00efb2..71ea707 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -1496,6 +1496,20 @@ static void machvirt_init(MachineState *machine) create_platform_bus(vms, pic); } =20 +static bool virt_get_smmu(Object *obj, Error **errp) +{ + VirtMachineState *vms =3D VIRT_MACHINE(obj); + + return vms->smmu; +} + +static void virt_set_smmu(Object *obj, bool value, Error **errp) +{ + VirtMachineState *vms =3D VIRT_MACHINE(obj); + + vms->smmu =3D value; +} + static bool virt_get_secure(Object *obj, Error **errp) { VirtMachineState *vms =3D VIRT_MACHINE(obj); @@ -1609,7 +1623,7 @@ static void machvirt_machine_init(void) } type_init(machvirt_machine_init); =20 -static void virt_2_9_instance_init(Object *obj) +static void virt_2_10_instance_init(Object *obj) { VirtMachineState *vms =3D VIRT_MACHINE(obj); VirtMachineClass *vmc =3D VIRT_MACHINE_GET_CLASS(vms); @@ -1665,14 +1679,47 @@ static void virt_2_9_instance_init(Object *obj) NULL); } =20 + if (vmc->no_smmu) { + vms->smmu =3D false; + } else { + /* Default disallows smmu instantiation */ + vms->smmu =3D false; + object_property_add_bool(obj, "smmu", virt_get_smmu, + virt_set_smmu, NULL); + object_property_set_description(obj, "smmu", + "Set on/off to enable/disable " + "smmu instantiation (default off)", + NULL); + } + vms->memmap =3D a15memmap; vms->irqmap =3D a15irqmap; } =20 +static void virt_machine_2_10_options(MachineClass *mc) +{ +} +DEFINE_VIRT_MACHINE_AS_LATEST(2, 10) + +#define VIRT_COMPAT_2_9 \ + HW_COMPAT_2_9 + +static void virt_2_9_instance_init(Object *obj) +{ + virt_2_10_instance_init(obj); +} + static void virt_machine_2_9_options(MachineClass *mc) { + VirtMachineClass *vmc =3D VIRT_MACHINE_CLASS(OBJECT_CLASS(mc)); + + virt_machine_2_10_options(mc); + SET_MACHINE_COMPAT(mc, VIRT_COMPAT_2_9); + + vmc->no_smmu =3D true; } -DEFINE_VIRT_MACHINE_AS_LATEST(2, 9) +DEFINE_VIRT_MACHINE(2, 9) + =20 #define VIRT_COMPAT_2_8 \ HW_COMPAT_2_8 diff --git a/include/hw/arm/virt.h b/include/hw/arm/virt.h index 164a531..cd2c82e 100644 --- a/include/hw/arm/virt.h +++ b/include/hw/arm/virt.h @@ -86,6 +86,7 @@ typedef struct { bool disallow_affinity_adjustment; bool no_its; bool no_pmu; + bool no_smmu; bool claim_edge_triggered_timers; } VirtMachineClass; =20 diff --git a/include/hw/compat.h b/include/hw/compat.h index 5d5be91..846b90e 100644 --- a/include/hw/compat.h +++ b/include/hw/compat.h @@ -1,6 +1,9 @@ #ifndef HW_COMPAT_H #define HW_COMPAT_H =20 +#define HW_COMPAT_2_9 \ + /* empty */ + #define HW_COMPAT_2_8 \ {\ .driver =3D "fw_cfg_mem",\ --=20 2.5.5