From: "Jan Beulich" Subject: [PATCH 4/6] AMD IOMMU: enable for multi-vector MSI Date: Fri, 19 Apr 2013 11:59:10 +0100 Message-ID: <51713F9E02000078000CEF23@nat28.tlf.novell.com> References: <51713D8F02000078000CEEE6@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part0535286E.2__=" Return-path: In-Reply-To: <51713D8F02000078000CEEE6@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: Jacob Shin , xiantao.zhang@intel.com, suravee.suthikulpanit@amd.com, Konrad Rzeszutek Wilk 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. --=__Part0535286E.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The main change being to make alloc_intremap_entry() capable of allocating a block of entries. Signed-off-by: Jan Beulich --- a/xen/drivers/passthrough/amd/iommu_intr.c +++ b/xen/drivers/passthrough/amd/iommu_intr.c @@ -47,13 +47,33 @@ static int get_intremap_requestor_id(int return get_ivrs_mappings(seg)[bdf].dte_requestor_id; } =20 -static unsigned int alloc_intremap_entry(int seg, int bdf) +static unsigned int alloc_intremap_entry(int seg, int bdf, unsigned int = nr) { unsigned long *inuse =3D get_ivrs_mappings(seg)[bdf].intremap_inuse; unsigned int slot =3D find_first_zero_bit(inuse, INTREMAP_ENTRIES); =20 - if ( slot < INTREMAP_ENTRIES ) - __set_bit(slot, inuse); + for ( ; ; ) + { + unsigned int end; + + if ( slot >=3D INTREMAP_ENTRIES ) + break; + end =3D find_next_bit(inuse, INTREMAP_ENTRIES, slot + 1); + if ( end > INTREMAP_ENTRIES ) + end =3D INTREMAP_ENTRIES; + slot =3D (slot + nr - 1) & ~(nr - 1); + if ( slot + nr <=3D end ) + { + while ( nr-- ) + __set_bit(slot + nr, inuse); + break; + } + slot =3D (end + nr) & ~(nr - 1); + if ( slot >=3D INTREMAP_ENTRIES ) + break; + slot =3D find_next_zero_bit(inuse, INTREMAP_ENTRIES, slot); + } + return slot; } =20 @@ -132,7 +152,7 @@ static int update_intremap_entry_from_io offset =3D *index; if ( offset >=3D INTREMAP_ENTRIES ) { - offset =3D alloc_intremap_entry(iommu->seg, req_id); + offset =3D alloc_intremap_entry(iommu->seg, req_id, 1); if ( offset >=3D INTREMAP_ENTRIES ) { spin_unlock_irqrestore(lock, flags); @@ -200,7 +220,7 @@ int __init amd_iommu_setup_ioapic_remapp dest =3D rte.dest.logical.logical_dest; =20 spin_lock_irqsave(lock, flags); - offset =3D alloc_intremap_entry(seg, req_id); + offset =3D alloc_intremap_entry(seg, req_id, 1); BUG_ON(offset >=3D INTREMAP_ENTRIES); entry =3D get_intremap_entry(iommu->seg, req_id, offset); update_intremap_entry(entry, vector, @@ -334,7 +354,7 @@ unsigned int amd_iommu_read_ioapic_from_ } =20 static int update_intremap_entry_from_msi_msg( - struct amd_iommu *iommu, u16 bdf, + struct amd_iommu *iommu, u16 bdf, unsigned int nr, int *remap_index, const struct msi_msg *msg, u32 *data) { unsigned long flags; @@ -342,7 +362,7 @@ static int update_intremap_entry_from_ms u16 req_id, alias_id; u8 delivery_mode, dest, vector, dest_mode; spinlock_t *lock; - unsigned int offset; + unsigned int offset, i; =20 req_id =3D get_dma_requestor_id(iommu->seg, bdf); alias_id =3D get_intremap_requestor_id(iommu->seg, bdf); @@ -351,7 +371,8 @@ static int update_intremap_entry_from_ms { lock =3D get_intremap_lock(iommu->seg, req_id); spin_lock_irqsave(lock, flags); - free_intremap_entry(iommu->seg, req_id, *remap_index); + for ( i =3D 0; i < nr; ++i ) + free_intremap_entry(iommu->seg, req_id, *remap_index + i); spin_unlock_irqrestore(lock, flags); goto done; } @@ -366,7 +387,8 @@ static int update_intremap_entry_from_ms offset =3D *remap_index; if ( offset >=3D INTREMAP_ENTRIES ) { - offset =3D alloc_intremap_entry(iommu->seg, bdf); + ASSERT(nr); + offset =3D alloc_intremap_entry(iommu->seg, bdf, nr); if ( offset >=3D INTREMAP_ENTRIES ) { spin_unlock_irqrestore(lock, flags); @@ -432,6 +454,7 @@ int amd_iommu_msi_msg_update_ire( struct pci_dev *pdev =3D msi_desc->dev; int bdf, seg, rc; struct amd_iommu *iommu; + unsigned int i, nr =3D 1; u32 data; =20 bdf =3D pdev ? PCI_BDF2(pdev->bus, pdev->devfn) : hpet_sbdf.bdf; @@ -441,10 +464,13 @@ int amd_iommu_msi_msg_update_ire( if ( IS_ERR_OR_NULL(iommu) ) return PTR_ERR(iommu); =20 + if ( msi_desc->msi_attrib.type =3D=3D PCI_CAP_ID_MSI ) + nr =3D msi_desc->msi.nvec; + if ( msi_desc->remap_index >=3D 0 && !msg ) { do { - update_intremap_entry_from_msi_msg(iommu, bdf, + update_intremap_entry_from_msi_msg(iommu, bdf, nr, &msi_desc->remap_index, NULL, NULL); if ( !pdev || !pdev->phantom_stride ) @@ -452,7 +478,8 @@ int amd_iommu_msi_msg_update_ire( bdf +=3D pdev->phantom_stride; } while ( PCI_SLOT(bdf) =3D=3D PCI_SLOT(pdev->devfn) ); =20 - msi_desc->remap_index =3D -1; + for ( i =3D 0; i < nr; ++i ) + msi_desc[i].remap_index =3D -1; if ( pdev ) bdf =3D PCI_BDF2(pdev->bus, pdev->devfn); } @@ -461,7 +488,7 @@ int amd_iommu_msi_msg_update_ire( return 0; =20 do { - rc =3D update_intremap_entry_from_msi_msg(iommu, bdf, + rc =3D update_intremap_entry_from_msi_msg(iommu, bdf, nr, &msi_desc->remap_index, msg, &data); if ( rc || !pdev || !pdev->phantom_stride ) @@ -469,6 +496,10 @@ int amd_iommu_msi_msg_update_ire( bdf +=3D pdev->phantom_stride; } while ( PCI_SLOT(bdf) =3D=3D PCI_SLOT(pdev->devfn) ); =20 + if ( !rc ) + for ( i =3D 1; i < nr; ++i ) + msi_desc[i].remap_index =3D msi_desc->remap_index + i; + msg->data =3D data; return rc; } @@ -487,6 +518,14 @@ void amd_iommu_read_msi_from_ire( =20 entry =3D get_intremap_entry(seg, get_dma_requestor_id(seg, bdf), = offset); =20 + if ( msi_desc->msi_attrib.type =3D=3D PCI_CAP_ID_MSI ) + { + int nr =3D msi_desc->msi_attrib.entry_nr; + + ASSERT(!(offset & (msi_desc[-nr].msi.nvec - 1))); + offset |=3D nr; + } + msg->data &=3D ~(INTREMAP_ENTRIES - 1); msg->data |=3D get_field_from_reg_u32(*entry, INT_REMAP_ENTRY_INTTYPE_MASK, --=__Part0535286E.2__= Content-Type: text/plain; name="AMD-IOMMU-multi-vector-MSI.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="AMD-IOMMU-multi-vector-MSI.patch" AMD IOMMU: enable for multi-vector MSI=0A=0AThe main change being to make = alloc_intremap_entry() capable of=0Aallocating a block of entries.=0A=0ASig= ned-off-by: Jan Beulich =0A=0A--- a/xen/drivers/passthro= ugh/amd/iommu_intr.c=0A+++ b/xen/drivers/passthrough/amd/iommu_intr.c=0A@@ = -47,13 +47,33 @@ static int get_intremap_requestor_id(int=0A return = get_ivrs_mappings(seg)[bdf].dte_requestor_id;=0A }=0A =0A-static unsigned = int alloc_intremap_entry(int seg, int bdf)=0A+static unsigned int = alloc_intremap_entry(int seg, int bdf, unsigned int nr)=0A {=0A = unsigned long *inuse =3D get_ivrs_mappings(seg)[bdf].intremap_inuse;=0A = unsigned int slot =3D find_first_zero_bit(inuse, INTREMAP_ENTRIES);=0A = =0A- if ( slot < INTREMAP_ENTRIES )=0A- __set_bit(slot, = inuse);=0A+ for ( ; ; )=0A+ {=0A+ unsigned int end;=0A+=0A+ = if ( slot >=3D INTREMAP_ENTRIES )=0A+ break;=0A+ = end =3D find_next_bit(inuse, INTREMAP_ENTRIES, slot + 1);=0A+ if ( = end > INTREMAP_ENTRIES )=0A+ end =3D INTREMAP_ENTRIES;=0A+ = slot =3D (slot + nr - 1) & ~(nr - 1);=0A+ if ( slot + nr <=3D end = )=0A+ {=0A+ while ( nr-- )=0A+ __set_bit(s= lot + nr, inuse);=0A+ break;=0A+ }=0A+ slot =3D = (end + nr) & ~(nr - 1);=0A+ if ( slot >=3D INTREMAP_ENTRIES )=0A+ = break;=0A+ slot =3D find_next_zero_bit(inuse, INTREMAP_ENTR= IES, slot);=0A+ }=0A+=0A return slot;=0A }=0A =0A@@ -132,7 +152,7 = @@ static int update_intremap_entry_from_io=0A offset =3D *index;=0A = if ( offset >=3D INTREMAP_ENTRIES )=0A {=0A- offset =3D = alloc_intremap_entry(iommu->seg, req_id);=0A+ offset =3D alloc_intre= map_entry(iommu->seg, req_id, 1);=0A if ( offset >=3D INTREMAP_ENTR= IES )=0A {=0A spin_unlock_irqrestore(lock, flags);=0A@@= -200,7 +220,7 @@ int __init amd_iommu_setup_ioapic_remapp=0A = dest =3D rte.dest.logical.logical_dest;=0A =0A spin_lock_irqsav= e(lock, flags);=0A- offset =3D alloc_intremap_entry(seg, = req_id);=0A+ offset =3D alloc_intremap_entry(seg, req_id, = 1);=0A BUG_ON(offset >=3D INTREMAP_ENTRIES);=0A = entry =3D get_intremap_entry(iommu->seg, req_id, offset);=0A = update_intremap_entry(entry, vector,=0A@@ -334,7 +354,7 @@ unsigned int = amd_iommu_read_ioapic_from_=0A }=0A =0A static int update_intremap_entry_fr= om_msi_msg(=0A- struct amd_iommu *iommu, u16 bdf,=0A+ struct = amd_iommu *iommu, u16 bdf, unsigned int nr,=0A int *remap_index, const = struct msi_msg *msg, u32 *data)=0A {=0A unsigned long flags;=0A@@ = -342,7 +362,7 @@ static int update_intremap_entry_from_ms=0A u16 = req_id, alias_id;=0A u8 delivery_mode, dest, vector, dest_mode;=0A = spinlock_t *lock;=0A- unsigned int offset;=0A+ unsigned int offset, = i;=0A =0A req_id =3D get_dma_requestor_id(iommu->seg, bdf);=0A = alias_id =3D get_intremap_requestor_id(iommu->seg, bdf);=0A@@ -351,7 = +371,8 @@ static int update_intremap_entry_from_ms=0A {=0A = lock =3D get_intremap_lock(iommu->seg, req_id);=0A spin_lock_irqsav= e(lock, flags);=0A- free_intremap_entry(iommu->seg, req_id, = *remap_index);=0A+ for ( i =3D 0; i < nr; ++i )=0A+ = free_intremap_entry(iommu->seg, req_id, *remap_index + i);=0A = spin_unlock_irqrestore(lock, flags);=0A goto done;=0A }=0A@@ = -366,7 +387,8 @@ static int update_intremap_entry_from_ms=0A offset = =3D *remap_index;=0A if ( offset >=3D INTREMAP_ENTRIES )=0A {=0A- = offset =3D alloc_intremap_entry(iommu->seg, bdf);=0A+ = ASSERT(nr);=0A+ offset =3D alloc_intremap_entry(iommu->seg, bdf, = nr);=0A if ( offset >=3D INTREMAP_ENTRIES )=0A {=0A = spin_unlock_irqrestore(lock, flags);=0A@@ -432,6 +454,7 @@ int = amd_iommu_msi_msg_update_ire(=0A struct pci_dev *pdev =3D msi_desc->dev= ;=0A int bdf, seg, rc;=0A struct amd_iommu *iommu;=0A+ unsigned = int i, nr =3D 1;=0A u32 data;=0A =0A bdf =3D pdev ? PCI_BDF2(pdev->= bus, pdev->devfn) : hpet_sbdf.bdf;=0A@@ -441,10 +464,13 @@ int amd_iommu_ms= i_msg_update_ire(=0A if ( IS_ERR_OR_NULL(iommu) )=0A return = PTR_ERR(iommu);=0A =0A+ if ( msi_desc->msi_attrib.type =3D=3D PCI_CAP_ID= _MSI )=0A+ nr =3D msi_desc->msi.nvec;=0A+=0A if ( msi_desc->rema= p_index >=3D 0 && !msg )=0A {=0A do {=0A- update_int= remap_entry_from_msi_msg(iommu, bdf,=0A+ update_intremap_entry_f= rom_msi_msg(iommu, bdf, nr,=0A = &msi_desc->remap_index,=0A = NULL, NULL);=0A if ( !pdev || !pdev->phantom_stride )=0A@@ = -452,7 +478,8 @@ int amd_iommu_msi_msg_update_ire(=0A bdf +=3D = pdev->phantom_stride;=0A } while ( PCI_SLOT(bdf) =3D=3D PCI_SLOT(pd= ev->devfn) );=0A =0A- msi_desc->remap_index =3D -1;=0A+ for = ( i =3D 0; i < nr; ++i )=0A+ msi_desc[i].remap_index =3D -1;=0A = if ( pdev )=0A bdf =3D PCI_BDF2(pdev->bus, pdev->devfn)= ;=0A }=0A@@ -461,7 +488,7 @@ int amd_iommu_msi_msg_update_ire(=0A = return 0;=0A =0A do {=0A- rc =3D update_intremap_entry_from_m= si_msg(iommu, bdf,=0A+ rc =3D update_intremap_entry_from_msi_msg(iom= mu, bdf, nr,=0A &msi_desc->= remap_index,=0A msg, = &data);=0A if ( rc || !pdev || !pdev->phantom_stride )=0A@@ -469,6 = +496,10 @@ int amd_iommu_msi_msg_update_ire(=0A bdf +=3D pdev->phan= tom_stride;=0A } while ( PCI_SLOT(bdf) =3D=3D PCI_SLOT(pdev->devfn) = );=0A =0A+ if ( !rc )=0A+ for ( i =3D 1; i < nr; ++i )=0A+ = msi_desc[i].remap_index =3D msi_desc->remap_index + i;=0A+=0A = msg->data =3D data;=0A return rc;=0A }=0A@@ -487,6 +518,14 @@ void = amd_iommu_read_msi_from_ire(=0A =0A entry =3D get_intremap_entry(seg, = get_dma_requestor_id(seg, bdf), offset);=0A =0A+ if ( msi_desc->msi_attr= ib.type =3D=3D PCI_CAP_ID_MSI )=0A+ {=0A+ int nr =3D msi_desc->ms= i_attrib.entry_nr;=0A+=0A+ ASSERT(!(offset & (msi_desc[-nr].msi.nvec= - 1)));=0A+ offset |=3D nr;=0A+ }=0A+=0A msg->data &=3D = ~(INTREMAP_ENTRIES - 1);=0A msg->data |=3D get_field_from_reg_u32(*entr= y,=0A INT_REMAP_ENTRY_INTTYPE_MASK,= =0A --=__Part0535286E.2__= 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 --=__Part0535286E.2__=--