From: "Jan Beulich" Subject: [PATCH 2/4] AMD IOMMU: cover all functions of a device even if ACPI only tells us of func 0 Date: Fri, 15 Feb 2013 16:20:31 +0000 Message-ID: <511E6E5F02000078000BED4E@nat28.tlf.novell.com> References: <511E6CAD02000078000BED19@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part18285A5F.1__=" Return-path: In-Reply-To: <511E6CAD02000078000BED19@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Boris Ostrovsky , Sherry Hurwitz List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part18285A5F.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This ought to work as all functions of a device have the same place in the bus topology, i.e. use the same IOMMU. Also fix the type of ivrs_bdf_entries (when it's 'unsigned short' and the last device found on a segment is ff:1f.x, it would otherwise end up being zero). And drop the bogus 'last_bdf' static variable, which conflicted anyway with various functions' parameters. Signed-off-by: Jan Beulich --- a/xen/drivers/passthrough/amd/iommu_acpi.c +++ b/xen/drivers/passthrough/amd/iommu_acpi.c @@ -54,8 +54,6 @@ union acpi_ivhd_device { struct acpi_ivrs_device8c special; }; =20 -static unsigned short __initdata last_bdf; - static void __init add_ivrs_mapping_entry( u16 bdf, u16 alias_id, u8 flags, struct amd_iommu *iommu) { @@ -991,6 +989,7 @@ static int __init get_last_bdf_ivhd( { const union acpi_ivhd_device *ivhd_device; u16 block_length, dev_length; + int last_bdf =3D 0; =20 if ( ivhd_block->header.length < sizeof(*ivhd_block) ) { @@ -1051,27 +1050,34 @@ static int __init get_last_bdf_ivhd( return -ENODEV; } =20 - return 0; + return last_bdf; } =20 static int __init get_last_bdf_acpi(struct acpi_table_header *table) { const struct acpi_ivrs_header *ivrs_block; unsigned long length =3D sizeof(struct acpi_table_ivrs); + int last_bdf =3D 0; =20 while ( table->length > (length + sizeof(*ivrs_block)) ) { ivrs_block =3D (struct acpi_ivrs_header *)((u8 *)table + length); if ( table->length < (length + ivrs_block->length) ) return -ENODEV; - if ( ivrs_block->type =3D=3D ACPI_IVRS_TYPE_HARDWARE && - get_last_bdf_ivhd( + if ( ivrs_block->type =3D=3D ACPI_IVRS_TYPE_HARDWARE ) + { + int ret =3D get_last_bdf_ivhd( container_of(ivrs_block, const struct acpi_ivrs_hardware,= - header)) !=3D 0 ) - return -ENODEV; + header)); + + if ( ret < 0 ) + return ret; + UPDATE_LAST_BDF(ret); + } length +=3D ivrs_block->length; } - return 0; + + return last_bdf; } =20 int __init amd_iommu_detect_acpi(void) @@ -1081,8 +1087,9 @@ int __init amd_iommu_detect_acpi(void) =20 int __init amd_iommu_get_ivrs_dev_entries(void) { - acpi_table_parse(ACPI_SIG_IVRS, get_last_bdf_acpi); - return last_bdf + 1; + int ret =3D acpi_table_parse(ACPI_SIG_IVRS, get_last_bdf_acpi); + + return ret < 0 ? ret : (ret | PCI_FUNC(~0)) + 1; } =20 int __init amd_iommu_update_ivrs_mapping_acpi(void) --- a/xen/drivers/passthrough/amd/iommu_init.c +++ b/xen/drivers/passthrough/amd/iommu_init.c @@ -35,7 +35,7 @@ static int __initdata nr_amd_iommus; =20 static struct tasklet amd_iommu_irq_tasklet; =20 -unsigned short ivrs_bdf_entries; +unsigned int __read_mostly ivrs_bdf_entries; static struct radix_tree_root ivrs_maps; struct list_head amd_iommu_head; struct table_struct device_table; --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -28,12 +28,38 @@ #include #include "../ats.h" =20 +static bool_t __read_mostly init_done; + struct amd_iommu *find_iommu_for_device(int seg, int bdf) { struct ivrs_mappings *ivrs_mappings =3D get_ivrs_mappings(seg); =20 - return ivrs_mappings && bdf < ivrs_bdf_entries ? ivrs_mappings[bdf].io= mmu - : NULL; + if ( !ivrs_mappings || bdf >=3D ivrs_bdf_entries ) + return NULL; + + if ( unlikely(!ivrs_mappings[bdf].iommu) && likely(init_done) ) + { + unsigned int bd0 =3D bdf & ~PCI_FUNC(~0); + + if ( ivrs_mappings[bd0].iommu ) + { + struct ivrs_mappings tmp =3D ivrs_mappings[bd0]; + + tmp.iommu =3D NULL; + if ( tmp.dte_requestor_id =3D=3D bd0 ) + tmp.dte_requestor_id =3D bdf; + ivrs_mappings[bdf] =3D tmp; + + printk(XENLOG_WARNING "%04x:%02x:%02x.%u not found in ACPI = tables;" + " using same IOMMU as function 0\n", + seg, PCI_BUS(bdf), PCI_SLOT(bdf), PCI_FUNC(bdf)); + + /* write iommu field last */ + ivrs_mappings[bdf].iommu =3D ivrs_mappings[bd0].iommu; + } + } + + return ivrs_mappings[bdf].iommu; } =20 /* @@ -179,6 +205,8 @@ int __init amd_iov_detect(void) return -ENODEV; } =20 + init_done =3D 1; + /* * AMD IOMMUs don't distinguish between vectors destined for * different cpus when doing interrupt remapping. This means --- a/xen/include/asm-x86/amd-iommu.h +++ b/xen/include/asm-x86/amd-iommu.h @@ -125,7 +125,7 @@ struct ivrs_mappings { u8 device_flags; }; =20 -extern unsigned short ivrs_bdf_entries; +extern unsigned int ivrs_bdf_entries; =20 struct ivrs_mappings *get_ivrs_mappings(u16 seg); int iterate_ivrs_mappings(int (*)(u16 seg, struct ivrs_mappings *)); --=__Part18285A5F.1__= Content-Type: text/plain; name="AMD-IOMMU-cover-all-functions.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="AMD-IOMMU-cover-all-functions.patch" AMD IOMMU: cover all functions of a device even if ACPI only tells us of = func 0=0A=0AThis ought to work as all functions of a device have the same = place in=0Athe bus topology, i.e. use the same IOMMU.=0A=0AAlso fix the = type of ivrs_bdf_entries (when it's 'unsigned short' and=0Athe last device = found on a segment is ff:1f.x, it would otherwise end=0Aup being = zero).=0A=0AAnd drop the bogus 'last_bdf' static variable, which conflicted= anyway=0Awith various functions' parameters.=0A=0ASigned-off-by: Jan = Beulich =0A=0A--- a/xen/drivers/passthrough/amd/iommu_ac= pi.c=0A+++ b/xen/drivers/passthrough/amd/iommu_acpi.c=0A@@ -54,8 +54,6 @@ = union acpi_ivhd_device {=0A struct acpi_ivrs_device8c special;=0A };=0A = =0A-static unsigned short __initdata last_bdf;=0A-=0A static void __init = add_ivrs_mapping_entry(=0A u16 bdf, u16 alias_id, u8 flags, struct = amd_iommu *iommu)=0A {=0A@@ -991,6 +989,7 @@ static int __init get_last_bdf= _ivhd(=0A {=0A const union acpi_ivhd_device *ivhd_device;=0A u16 = block_length, dev_length;=0A+ int last_bdf =3D 0;=0A =0A if ( = ivhd_block->header.length < sizeof(*ivhd_block) )=0A {=0A@@ -1051,27 = +1050,34 @@ static int __init get_last_bdf_ivhd(=0A return = -ENODEV;=0A }=0A =0A- return 0;=0A+ return last_bdf;=0A }=0A =0A = static int __init get_last_bdf_acpi(struct acpi_table_header *table)=0A = {=0A const struct acpi_ivrs_header *ivrs_block;=0A unsigned long = length =3D sizeof(struct acpi_table_ivrs);=0A+ int last_bdf =3D 0;=0A = =0A while ( table->length > (length + sizeof(*ivrs_block)) )=0A = {=0A ivrs_block =3D (struct acpi_ivrs_header *)((u8 *)table + = length);=0A if ( table->length < (length + ivrs_block->length) = )=0A return -ENODEV;=0A- if ( ivrs_block->type =3D=3D = ACPI_IVRS_TYPE_HARDWARE &&=0A- get_last_bdf_ivhd(=0A+ = if ( ivrs_block->type =3D=3D ACPI_IVRS_TYPE_HARDWARE )=0A+ {=0A+ = int ret =3D get_last_bdf_ivhd(=0A container_of(ivr= s_block, const struct acpi_ivrs_hardware,=0A- = header)) !=3D 0 )=0A- return -ENODEV;=0A+ = header));=0A+=0A+ if ( ret < 0 )=0A+ = return ret;=0A+ UPDATE_LAST_BDF(ret);=0A+ }=0A = length +=3D ivrs_block->length;=0A }=0A- return 0;=0A+=0A+ return = last_bdf;=0A }=0A =0A int __init amd_iommu_detect_acpi(void)=0A@@ -1081,8 = +1087,9 @@ int __init amd_iommu_detect_acpi(void)=0A =0A int __init = amd_iommu_get_ivrs_dev_entries(void)=0A {=0A- acpi_table_parse(ACPI_SIG_= IVRS, get_last_bdf_acpi);=0A- return last_bdf + 1;=0A+ int ret =3D = acpi_table_parse(ACPI_SIG_IVRS, get_last_bdf_acpi);=0A+=0A+ return ret = < 0 ? ret : (ret | PCI_FUNC(~0)) + 1;=0A }=0A =0A int __init amd_iommu_upda= te_ivrs_mapping_acpi(void)=0A--- a/xen/drivers/passthrough/amd/iommu_init.c= =0A+++ b/xen/drivers/passthrough/amd/iommu_init.c=0A@@ -35,7 +35,7 @@ = static int __initdata nr_amd_iommus;=0A =0A static struct tasklet = amd_iommu_irq_tasklet;=0A =0A-unsigned short ivrs_bdf_entries;=0A+unsigned = int __read_mostly ivrs_bdf_entries;=0A static struct radix_tree_root = ivrs_maps;=0A struct list_head amd_iommu_head;=0A struct table_struct = device_table;=0A--- a/xen/drivers/passthrough/amd/pci_amd_iommu.c=0A+++ = b/xen/drivers/passthrough/amd/pci_amd_iommu.c=0A@@ -28,12 +28,38 @@=0A = #include =0A #include "../ats.h"=0A = =0A+static bool_t __read_mostly init_done;=0A+=0A struct amd_iommu = *find_iommu_for_device(int seg, int bdf)=0A {=0A struct ivrs_mappings = *ivrs_mappings =3D get_ivrs_mappings(seg);=0A =0A- return ivrs_mappings = && bdf < ivrs_bdf_entries ? ivrs_mappings[bdf].iommu=0A- = : NULL;=0A+ if ( !ivrs_mappings || bdf = >=3D ivrs_bdf_entries )=0A+ return NULL;=0A+=0A+ if ( unlikely(!i= vrs_mappings[bdf].iommu) && likely(init_done) )=0A+ {=0A+ = unsigned int bd0 =3D bdf & ~PCI_FUNC(~0);=0A+=0A+ if ( ivrs_mappings= [bd0].iommu )=0A+ {=0A+ struct ivrs_mappings tmp =3D = ivrs_mappings[bd0];=0A+=0A+ tmp.iommu =3D NULL;=0A+ = if ( tmp.dte_requestor_id =3D=3D bd0 )=0A+ tmp.dte_requestor= _id =3D bdf;=0A+ ivrs_mappings[bdf] =3D tmp;=0A+=0A+ = printk(XENLOG_WARNING "%04x:%02x:%02x.%u not found in ACPI tables;"=0A+ = " using same IOMMU as function 0\n",=0A+ = seg, PCI_BUS(bdf), PCI_SLOT(bdf), PCI_FUNC(bdf));=0A+=0A+ /* = write iommu field last */=0A+ ivrs_mappings[bdf].iommu =3D = ivrs_mappings[bd0].iommu;=0A+ }=0A+ }=0A+=0A+ return = ivrs_mappings[bdf].iommu;=0A }=0A =0A /*=0A@@ -179,6 +205,8 @@ int __init = amd_iov_detect(void)=0A return -ENODEV;=0A }=0A =0A+ = init_done =3D 1;=0A+=0A /*=0A * AMD IOMMUs don't distinguish = between vectors destined for=0A * different cpus when doing interrupt = remapping. This means=0A--- a/xen/include/asm-x86/amd-iommu.h=0A+++ = b/xen/include/asm-x86/amd-iommu.h=0A@@ -125,7 +125,7 @@ struct ivrs_mapping= s {=0A u8 device_flags;=0A };=0A =0A-extern unsigned short ivrs_bdf_ent= ries;=0A+extern unsigned int ivrs_bdf_entries;=0A =0A struct ivrs_mappings = *get_ivrs_mappings(u16 seg);=0A int iterate_ivrs_mappings(int (*)(u16 seg, = struct ivrs_mappings *));=0A --=__Part18285A5F.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part18285A5F.1__=--