From patchwork Tue Aug 2 08:39:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Kiarie X-Patchwork-Id: 9255323 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 52A1A60754 for ; Tue, 2 Aug 2016 08:40:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 428C028511 for ; Tue, 2 Aug 2016 08:40:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 36EE028520; Tue, 2 Aug 2016 08:40:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2A47B28511 for ; Tue, 2 Aug 2016 08:40:33 +0000 (UTC) Received: from localhost ([::1]:54685 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUVFY-0002tm-4t for patchwork-qemu-devel@patchwork.kernel.org; Tue, 02 Aug 2016 04:40:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34757) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUVEu-0002pV-8f for qemu-devel@nongnu.org; Tue, 02 Aug 2016 04:39:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bUVEr-0003cv-9c for qemu-devel@nongnu.org; Tue, 02 Aug 2016 04:39:51 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:33972) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUVEq-0003cq-WF for qemu-devel@nongnu.org; Tue, 02 Aug 2016 04:39:49 -0400 Received: by mail-wm0-x243.google.com with SMTP id q128so29540400wma.1 for ; Tue, 02 Aug 2016 01:39:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AzUnAa9DLJOo9PuBK1I+SFTvjKlqahTBl4te6AKKiyU=; b=FhMTwkvF4BkOOK/OONbgokaYbQLpERC5qRlpubYPJ2pbho/A1OKhhdhXHQmq74MJf7 oeJX8ZU5J/XgGdCJ0tqWskljvuqtcg1a/rV67fij3zG78lz4k/kjDJTPaWB9IX4PjJQu tctVPmGGkH4gUkXiRBVPjNoi5ka8Vg2Y9sxfUsAQt3jj1BvNkBAQW84ZbOBl+nY5bWQY GPgyBTu5Z7rzIs621zgJgZNH47BQnHUbOOb98TdnTUlQAvgwd9KVZeholgHQacwc0Lo2 lrT8R2///X3UKTrex2G5nnJnU3InQOIC+D/9aVLjvzUKQt1lKoM79bVHGl+uZwJlHExR 10ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AzUnAa9DLJOo9PuBK1I+SFTvjKlqahTBl4te6AKKiyU=; b=EL3ldIq9Dk/nwNGZ1CfvR7HzmjGA6Ejbebd+rBRtfGcrMs+sdhn66gxEdAG2cy9Fyw cGK2Vx8GnJBUuTI9APvr6Zqf0YsrZUb9dNNgkhkEauLf64EB4NxDNPye9XKBSacOaP58 SR+n7Nh2hbzV03vjsfqZ2+DiwOxgniKCKJF6tPOe/7ZLParoExnDIRZctdDIDJUjUlQC u5jdPuLvVPP+pzC2W2krej4r2WY2DgkLQ18D5fye/1frkfeikWFWCcCmRxpJWAxK0o10 /XPmYYkSpkr5nQoy7tFxLlS1OJ0Bm5W5aTdhqVSpdUkJFOhIRgAr2XRgBV8ZIkM9x2F4 3ufA== X-Gm-Message-State: AEkooutEJX+gEja7nV7Nam+aN15j7zXG5CuHegtUaawy2Ll8qvScKBU7St4gYGtArvwLGA== X-Received: by 10.194.142.198 with SMTP id ry6mr54296577wjb.62.1470127188042; Tue, 02 Aug 2016 01:39:48 -0700 (PDT) Received: from debian.flybox.orange ([154.122.72.250]) by smtp.googlemail.com with ESMTPSA id q139sm20878961wmb.18.2016.08.02.01.39.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 02 Aug 2016 01:39:47 -0700 (PDT) From: David Kiarie To: qemu-devel@nongnu.org Date: Tue, 2 Aug 2016 11:39:07 +0300 Message-Id: <1470127147-12442-5-git-send-email-davidkiarie4@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1470127147-12442-1-git-send-email-davidkiarie4@gmail.com> References: <1470127147-12442-1-git-send-email-davidkiarie4@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [V15 4/4] hw/i386: AMD IOMMU IVRS table 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: ehabkost@redhat.com, mst@redhat.com, rkrcmar@redhat.com, peterx@redhat.com, valentine.sinitsyn@gmail.com, jan.kiszka@web.de, David Kiarie Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add IVRS table for AMD IOMMU. Generate IVRS or DMAR depending on emulated IOMMU. Signed-off-by: David Kiarie --- hw/acpi/aml-build.c | 2 +- hw/i386/acpi-build.c | 76 ++++++++++++++++++++++++++++++++++++++++----- hw/i386/x86-iommu.c | 19 ++++++++++++ include/hw/acpi/aml-build.h | 1 + include/hw/i386/x86-iommu.h | 11 +++++++ 5 files changed, 101 insertions(+), 8 deletions(-) diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index db3e914..b2a1e40 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -226,7 +226,7 @@ static void build_extop_package(GArray *package, uint8_t op) build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ } -static void build_append_int_noprefix(GArray *table, uint64_t value, int size) +void build_append_int_noprefix(GArray *table, uint64_t value, int size) { int i; diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index a26a4bb..efed318 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -59,7 +59,8 @@ #include "qapi/qmp/qint.h" #include "qom/qom-qobject.h" -#include "hw/i386/x86-iommu.h" +#include "hw/i386/amd_iommu.h" +#include "hw/i386/intel_iommu.h" #include "hw/acpi/ipmi.h" @@ -2562,6 +2563,68 @@ build_dmar_q35(GArray *table_data, BIOSLinker *linker) build_header(linker, table_data, (void *)(table_data->data + dmar_start), "DMAR", table_data->len - dmar_start, 1, NULL, NULL); } +/* + * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2 + * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf + */ +static void +build_amd_iommu(GArray *table_data, BIOSLinker *linker) +{ + int iommu_start = table_data->len; + AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default()); + assert(s); + + /* IVRS header */ + acpi_data_push(table_data, sizeof(AcpiTableHeader)); + /* IVinfo - IO virtualization information common to all IOMMU + * units in a system + */ + build_append_int_noprefix(table_data, 40UL << 8/* PASize */, 4); + /* reserved */ + build_append_int_noprefix(table_data, 0, 8); + + /* IVHD definition - type 10h */ + build_append_int_noprefix(table_data, 0x10, 1); + /* virtualization flags */ + build_append_int_noprefix(table_data, + (1UL << 0) | /* HtTunEn */ + (1UL << 4) | /* iotblSup */ + (1UL << 6) | /* PrefSup */ + (1UL << 7), /* PPRSup */ + 1); + /* IVHD length */ + build_append_int_noprefix(table_data, 0x28, 2); + /* DeviceID */ + build_append_int_noprefix(table_data, s->devid, 2); + /* Capability offset */ + build_append_int_noprefix(table_data, s->capab_offset, 2); + /* IOMMU base address */ + build_append_int_noprefix(table_data, s->mmio.addr, 8); + /* PCI Segment Group */ + build_append_int_noprefix(table_data, 0, 2); + /* IOMMU info */ + build_append_int_noprefix(table_data, 0, 2); + /* IOMMU Feature Reporting */ + build_append_int_noprefix(table_data, + (48UL << 30) | /* HATS */ + (48UL << 28) | /* GATS */ + (1UL << 2), /* GTSup */ + 4); + /* Add device flags here + * These are 4-byte device entries currently reporting the range of + * devices 00h - ffffh; all devices + * Device setting affecting all devices should be made here + * + * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte) + */ + /* start of device range, 4-byte entries */ + build_append_int_noprefix(table_data, 0x00000003, 4); + /* end of device range */ + build_append_int_noprefix(table_data, 0x00ffff04, 4); + + build_header(linker, table_data, (void *)(table_data->data + iommu_start), + "IVRS", table_data->len - iommu_start, 1, NULL, NULL); +} static GArray * build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned rsdt_tbl_offset) @@ -2622,11 +2685,6 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg) return true; } -static bool acpi_has_iommu(void) -{ - return !!x86_iommu_get_default(); -} - static void acpi_build(AcpiBuildTables *tables, MachineState *machine) { @@ -2639,6 +2697,7 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine) AcpiMcfgInfo mcfg; Range pci_hole, pci_hole64; uint8_t *u; + IommuType IOMMUType = x86_iommu_get_type(); size_t aml_len = 0; GArray *tables_blob = tables->table_data; AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL }; @@ -2706,7 +2765,10 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine) acpi_add_table(table_offsets, tables_blob); build_mcfg_q35(tables_blob, tables->linker, &mcfg); } - if (acpi_has_iommu()) { + if (IOMMUType == TYPE_AMD) { + acpi_add_table(table_offsets, tables_blob); + build_amd_iommu(tables_blob, tables->linker); + } else if (IOMMUType == TYPE_INTEL) { acpi_add_table(table_offsets, tables_blob); build_dmar_q35(tables_blob, tables->linker); } diff --git a/hw/i386/x86-iommu.c b/hw/i386/x86-iommu.c index ce26b2a..893d54d 100644 --- a/hw/i386/x86-iommu.c +++ b/hw/i386/x86-iommu.c @@ -20,7 +20,10 @@ #include "qemu/osdep.h" #include "hw/sysbus.h" #include "hw/boards.h" +#include "hw/i386/intel_iommu.h" +#include "hw/i386/amd_iommu.h" #include "hw/i386/x86-iommu.h" +#include "sysemu/kvm.h" #include "qemu/error-report.h" #include "trace.h" @@ -71,6 +74,21 @@ X86IOMMUState *x86_iommu_get_default(void) return x86_iommu_default; } +IommuType x86_iommu_get_type(void) +{ + bool ambiguous; + + if (object_resolve_path_type("", TYPE_AMD_IOMMU_DEVICE, &ambiguous) + && !ambiguous) { + return TYPE_AMD; + } else if (object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE, &ambiguous) + && !ambiguous) { + return TYPE_INTEL; + } else { + return TYPE_NONE; + } +} + static void x86_iommu_realize(DeviceState *dev, Error **errp) { X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev); @@ -79,6 +97,7 @@ static void x86_iommu_realize(DeviceState *dev, Error **errp) if (x86_class->realize) { x86_class->realize(dev, errp); } + x86_iommu_set_default(X86_IOMMU_DEVICE(dev)); } diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index e5f0878..559326c 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -367,6 +367,7 @@ Aml *aml_sizeof(Aml *arg); Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target); Aml *aml_object_type(Aml *object); +void build_append_int_noprefix(GArray *table, uint64_t value, int size); void build_header(BIOSLinker *linker, GArray *table_data, AcpiTableHeader *h, const char *sig, int len, uint8_t rev, diff --git a/include/hw/i386/x86-iommu.h b/include/hw/i386/x86-iommu.h index c48e8dd..2acc04a 100644 --- a/include/hw/i386/x86-iommu.h +++ b/include/hw/i386/x86-iommu.h @@ -37,6 +37,12 @@ typedef struct X86IOMMUState X86IOMMUState; typedef struct X86IOMMUClass X86IOMMUClass; +typedef enum IommuType { + TYPE_INTEL, + TYPE_AMD, + TYPE_NONE +} IommuType; + struct X86IOMMUClass { SysBusDeviceClass parent; /* Intel/AMD specific realize() hook */ @@ -76,6 +82,11 @@ struct X86IOMMUState { */ X86IOMMUState *x86_iommu_get_default(void); +/* + * x86_iommu_get_type - get IOMMU type + */ +IommuType x86_iommu_get_type(void); + /** * x86_iommu_iec_register_notifier - register IEC (Interrupt Entry * Cache) notifiers