From patchwork Sun Aug 15 19:57:27 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard - Gabriel Munteanu X-Patchwork-Id: 119639 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o7FK13xj027333 for ; Sun, 15 Aug 2010 20:01:03 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751434Ab0HOUBA (ORCPT ); Sun, 15 Aug 2010 16:01:00 -0400 Received: from mail-fx0-f46.google.com ([209.85.161.46]:64241 "EHLO mail-fx0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751217Ab0HOUAv (ORCPT ); Sun, 15 Aug 2010 16:00:51 -0400 Received: by mail-fx0-f46.google.com with SMTP id 13so2635014fxm.19 for ; Sun, 15 Aug 2010 13:00:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:from:to:cc:subject :date:message-id:x-mailer:in-reply-to:references; bh=iHR5YHS230OpQLPm/vWYC3un8DicZdn++Ew+n7hcvfw=; b=Sle+3u634Uzcw4tqMUBj3+FXkFmFzYmypx/xUdCacehbrFVMSLGH3qMkCmfXZPe/HU LdiE4koH7JqLuxyyEcvWK00UZBwsVlbuybSDb3hlWnd93dGlYBYfPtwEzcQABp+l6f5F Cgzi9/qFdGRC1llSqjBmagbhG1TyKEHQkap+I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; b=GJkmIw8eMGlw3cwj4WDTDdYDvChiOAY0GNs0CVx9fvLcGleWaW4gIpQIYdZHmUV/MN 7agRU0EUlxUKsAdB+lGIwMsLsBXoXC7W3ChHXVfgmrT+aZjzdTT431AfuS/W8Y9J6kjN sGxn7ST5g2A0jLC6XQ6rKbW61LlgqQA6cQE8o= Received: by 10.223.103.72 with SMTP id j8mr4403232fao.4.1281902450762; Sun, 15 Aug 2010 13:00:50 -0700 (PDT) Received: from localhost.localdomain ([178.138.32.54]) by mx.google.com with ESMTPS id e17sm536426faa.15.2010.08.15.13.00.48 (version=SSLv3 cipher=RC4-MD5); Sun, 15 Aug 2010 13:00:50 -0700 (PDT) From: Eduard - Gabriel Munteanu To: joro@8bytes.org Cc: paul@codesourcery.com, blauwirbel@gmail.com, anthony@codemonkey.ws, avi@redhat.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, seabios@seabios.org, Eduard - Gabriel Munteanu Subject: [PATCH 2/2] AMD IOMMU support Date: Sun, 15 Aug 2010 22:57:27 +0300 Message-Id: <1281902247-5151-2-git-send-email-eduard.munteanu@linux360.ro> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1281902247-5151-1-git-send-email-eduard.munteanu@linux360.ro> References: <1281902247-5151-1-git-send-email-eduard.munteanu@linux360.ro> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Sun, 15 Aug 2010 20:01:03 +0000 (UTC) diff --git a/Makefile b/Makefile index fe0c1ce..98f253d 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ OUT=out/ SRCBOTH=misc.c pmm.c stacks.c output.c util.c block.c floppy.c ata.c mouse.c \ kbd.c pci.c serial.c clock.c pic.c cdrom.c ps2port.c smp.c resume.c \ pnpbios.c pirtable.c vgahooks.c ramdisk.c pcibios.c blockcmd.c \ - usb.c usb-uhci.c usb-ohci.c usb-ehci.c usb-hid.c usb-msc.c + usb.c usb-uhci.c usb-ohci.c usb-ehci.c usb-hid.c usb-msc.c iommu.c SRC16=$(SRCBOTH) system.c disk.c apm.c font.c SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \ acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \ diff --git a/src/acpi.c b/src/acpi.c index 0559443..7ea9c55 100644 --- a/src/acpi.c +++ b/src/acpi.c @@ -6,6 +6,7 @@ // This file may be distributed under the terms of the GNU LGPLv3 license. #include "acpi.h" // struct rsdp_descriptor +#include "iommu.h" #include "util.h" // memcpy #include "pci.h" // pci_find_device #include "biosvar.h" // GET_EBDA @@ -268,6 +269,36 @@ struct srat_memory_affinity u32 reserved3[2]; } PACKED; +/* + * IVRS (I/O Virtualization Reporting Structure) table. + * + * Describes the AMD IOMMU, as per: + * "AMD I/O Virtualization Technology (IOMMU) Specification", rev 1.26 + */ + +struct ivrs_ivhd +{ + u8 type; + u8 flags; + u16 length; + u16 devid; + u16 capab_off; + u32 iommu_base_low; + u32 iommu_base_high; + u16 pci_seg_group; + u16 iommu_info; + u32 reserved; + u8 entry[0]; +} PACKED; + +struct ivrs_table +{ + ACPI_TABLE_HEADER_DEF /* ACPI common table header. */ + u32 iv_info; + u32 reserved[2]; + struct ivrs_ivhd ivhd; +} PACKED; + #include "acpi-dsdt.hex" static inline u16 cpu_to_le16(u16 x) @@ -599,6 +630,53 @@ build_srat(void) return srat; } +#define IVRS_SIGNATURE 0x53525649 // IVRS +#define IVRS_MAX_DEVS 32 +static void * +build_ivrs(void) +{ + int iommu_bdf, bdf, max, i; + struct ivrs_table *ivrs; + struct ivrs_ivhd *ivhd; + + iommu_bdf = pci_find_class(PCI_CLASS_SYSTEM_IOMMU); + if (iommu_bdf < 0) + return NULL; + + ivrs = malloc_high(sizeof(struct ivrs_table) + 4 * IVRS_MAX_DEVS); + ivrs->iv_info = iommu_get_misc() & ~0x000F; + + ivhd = &ivrs->ivhd; + ivhd->type = 0x10; + ivhd->flags = 0; + ivhd->length = sizeof(struct ivrs_ivhd); + ivhd->devid = iommu_get_bdf(); + ivhd->capab_off = iommu_get_cap_offset(); + ivhd->iommu_base_low = iommu_get_base(); + ivhd->iommu_base_high = 0; + ivhd->pci_seg_group = 0; + ivhd->iommu_info = 0; + ivhd->reserved = 0; + + i = 0; + foreachpci(bdf, max) { + if (bdf == ivhd->devid) + continue; + ivhd->entry[4 * i + 0] = 2; + ivhd->entry[4 * i + 1] = bdf & 0xFF; + ivhd->entry[4 * i + 2] = (bdf >> 8) & 0xFF; + ivhd->entry[4 * i + 3] = ~(1 << 3); + ivhd->length += 4; + if (++i >= IVRS_MAX_DEVS) + break; + } + + build_header((void *) ivrs, IVRS_SIGNATURE, + sizeof(struct ivrs_table) + 4 * i, 1); + + return ivrs; +} + struct rsdp_descriptor *RsdpAddr; #define MAX_ACPI_TABLES 20 @@ -639,6 +717,7 @@ acpi_bios_init(void) ACPI_INIT_TABLE(build_madt()); ACPI_INIT_TABLE(build_hpet()); ACPI_INIT_TABLE(build_srat()); + ACPI_INIT_TABLE(build_ivrs()); u16 i, external_tables = qemu_cfg_acpi_additional_tables(); diff --git a/src/iommu.c b/src/iommu.c new file mode 100644 index 0000000..97af24a --- /dev/null +++ b/src/iommu.c @@ -0,0 +1,64 @@ +// AMD IOMMU initialization code. +// +// Copyright (C) 2010 Eduard - Gabriel Munteanu +// +// This file may be distributed under the terms of the GNU LGPLv3 license. + +#include "iommu.h" +#include "pci.h" +#include "types.h" + +#define IOMMU_CAP_BAR_LOW 0x04 +#define IOMMU_CAP_BAR_HIGH 0x08 +#define IOMMU_CAP_RANGE 0x0C +#define IOMMU_CAP_MISC 0x10 + +static int iommu_bdf = -1; +static u8 iommu_cap_offset; +static u32 iommu_base; + +void iommu_init(int bdf, u32 base) +{ + u8 ptr, cap, type; + + /* Only one IOMMU is supported. */ + if (iommu_bdf >= 0) + return; + + foreachcap(bdf, ptr, cap) { + type = pci_config_readb(bdf, cap); + if (type == PCI_CAP_ID_SEC) + break; + } + if (!cap) + return; + + pci_config_writel(bdf, cap + IOMMU_CAP_RANGE, 0); + pci_config_writel(bdf, cap + IOMMU_CAP_BAR_HIGH, 0); + pci_config_writel(bdf, cap + IOMMU_CAP_BAR_LOW, base | 1); + + iommu_bdf = bdf; + iommu_cap_offset = cap; + iommu_base = base; +} + +int iommu_get_bdf(void) +{ + return iommu_bdf; +} + +u8 iommu_get_cap_offset(void) +{ + return iommu_cap_offset; +} + +u32 iommu_get_misc(void) +{ + return pci_config_readw(iommu_bdf, iommu_cap_offset + IOMMU_CAP_MISC + 2); +} + +u32 iommu_get_base(void) +{ + return iommu_base; +} + diff --git a/src/iommu.h b/src/iommu.h new file mode 100644 index 0000000..105af25 --- /dev/null +++ b/src/iommu.h @@ -0,0 +1,12 @@ +#ifndef __IOMMU_H +#define __IOMMU_H + +#include "types.h" + +void iommu_init(int bdf, u32 base); +int iommu_get_bdf(void); +u8 iommu_get_cap_offset(void); +u32 iommu_get_misc(void); + +#endif + diff --git a/src/pci.h b/src/pci.h index eea5b09..3f38a0e 100644 --- a/src/pci.h +++ b/src/pci.h @@ -39,6 +39,10 @@ int pci_next(int bdf, int *pmax); for (MAX=0x0100, BDF=pci_next(0, &MAX) \ ; BDF >= 0 \ ; BDF=pci_next(BDF+1, &MAX)) +#define foreachcap(BDF, PTR, CAP) \ + for (PTR = PCI_CAPABILITY_LIST, CAP = pci_config_readb(BDF, PTR); \ + CAP; \ + PTR = CAP + PCI_CAP_LIST_NEXT, CAP = pci_config_readb(BDF, PTR)) // pirtable.c void create_pirtable(void); diff --git a/src/pci_ids.h b/src/pci_ids.h index 1800f1d..3c695b2 100644 --- a/src/pci_ids.h +++ b/src/pci_ids.h @@ -72,6 +72,7 @@ #define PCI_CLASS_SYSTEM_RTC 0x0803 #define PCI_CLASS_SYSTEM_PCI_HOTPLUG 0x0804 #define PCI_CLASS_SYSTEM_SDHCI 0x0805 +#define PCI_CLASS_SYSTEM_IOMMU 0x0806 #define PCI_CLASS_SYSTEM_OTHER 0x0880 #define PCI_BASE_CLASS_INPUT 0x09 diff --git a/src/pci_regs.h b/src/pci_regs.h index e5effd4..600df4d 100644 --- a/src/pci_regs.h +++ b/src/pci_regs.h @@ -208,6 +208,7 @@ #define PCI_CAP_ID_SHPC 0x0C /* PCI Standard Hot-Plug Controller */ #define PCI_CAP_ID_SSVID 0x0D /* Bridge subsystem vendor/device ID */ #define PCI_CAP_ID_AGP3 0x0E /* AGP Target PCI-PCI bridge */ +#define PCI_CAP_ID_SEC 0x0F /* Secure Device (AMD IOMMU) */ #define PCI_CAP_ID_EXP 0x10 /* PCI Express */ #define PCI_CAP_ID_MSIX 0x11 /* MSI-X */ #define PCI_CAP_LIST_NEXT 1 /* Next capability in the list */ diff --git a/src/pciinit.c b/src/pciinit.c index bfc669f..cad9114 100644 --- a/src/pciinit.c +++ b/src/pciinit.c @@ -85,6 +85,14 @@ static inline u32 pci_bios_alloc(u32 *region, u32 size) return ret; } +static void pci_bios_init_iommu(u16 bdf) +{ + u32 base; + + base = pci_bios_alloc(&pci_bios_mem_addr, 0x4000); + iommu_init(bdf, base); +} + static void pci_bios_init_device(u16 bdf) { int class; @@ -130,6 +138,9 @@ static void pci_bios_init_device(u16 bdf) pci_set_io_region_addr(bdf, 0, 0x80800000); } break; + case PCI_CLASS_SYSTEM_IOMMU: + pci_bios_init_iommu(bdf); + break; default: default_map: /* default memory mappings */