Commit 6865e52b authored by Jan Beulich's avatar Jan Beulich

PCI multi-seg: adjust domctl interface

Again, a couple of directly related functions at once get adjusted to
account for the segment number.
Signed-off-by: default avatarJan Beulich <jbeulich@suse.com>
parent 1b0061ac
......@@ -1132,13 +1132,13 @@ int xc_domain_setdebugging(xc_interface *xch,
int xc_assign_device(
xc_interface *xch,
uint32_t domid,
uint32_t machine_bdf)
uint32_t machine_sbdf)
{
DECLARE_DOMCTL;
domctl.cmd = XEN_DOMCTL_assign_device;
domctl.domain = domid;
domctl.u.assign_device.machine_bdf = machine_bdf;
domctl.u.assign_device.machine_sbdf = machine_sbdf;
return do_domctl(xch, &domctl);
}
......@@ -1146,7 +1146,7 @@ int xc_assign_device(
int xc_get_device_group(
xc_interface *xch,
uint32_t domid,
uint32_t machine_bdf,
uint32_t machine_sbdf,
uint32_t max_sdevs,
uint32_t *num_sdevs,
uint32_t *sdev_array)
......@@ -1164,7 +1164,7 @@ int xc_get_device_group(
domctl.cmd = XEN_DOMCTL_get_device_group;
domctl.domain = (domid_t)domid;
domctl.u.get_device_group.machine_bdf = machine_bdf;
domctl.u.get_device_group.machine_sbdf = machine_sbdf;
domctl.u.get_device_group.max_sdevs = max_sdevs;
set_xen_guest_handle(domctl.u.get_device_group.sdev_array, sdev_array);
......@@ -1181,13 +1181,13 @@ int xc_get_device_group(
int xc_test_assign_device(
xc_interface *xch,
uint32_t domid,
uint32_t machine_bdf)
uint32_t machine_sbdf)
{
DECLARE_DOMCTL;
domctl.cmd = XEN_DOMCTL_test_assign_device;
domctl.domain = domid;
domctl.u.assign_device.machine_bdf = machine_bdf;
domctl.u.assign_device.machine_sbdf = machine_sbdf;
return do_domctl(xch, &domctl);
}
......@@ -1195,13 +1195,13 @@ int xc_test_assign_device(
int xc_deassign_device(
xc_interface *xch,
uint32_t domid,
uint32_t machine_bdf)
uint32_t machine_sbdf)
{
DECLARE_DOMCTL;
domctl.cmd = XEN_DOMCTL_deassign_device;
domctl.domain = domid;
domctl.u.assign_device.machine_bdf = machine_bdf;
domctl.u.assign_device.machine_sbdf = machine_sbdf;
return do_domctl(xch, &domctl);
}
......
......@@ -45,10 +45,10 @@ static unsigned int pcidev_encode_bdf(libxl_device_pci *pcidev)
{
unsigned int value;
value = 0;
value |= (pcidev->bus & 0xff) << 16;
value |= (pcidev->dev & 0x1f) << (8+3);
value |= (pcidev->func & 0x7) << (8+0);
value = pcidev->domain << 16;
value |= (pcidev->bus & 0xff) << 8;
value |= (pcidev->dev & 0x1f) << 3;
value |= (pcidev->func & 0x7);
return value;
}
......
......@@ -609,7 +609,7 @@ static PyObject *pyxc_test_assign_device(XcObject *self,
{
uint32_t dom;
char *pci_str;
int32_t bdf = 0;
int32_t sbdf = 0;
int seg, bus, dev, func;
static char *kwd_list[] = { "domid", "pci", NULL };
......@@ -619,20 +619,21 @@ static PyObject *pyxc_test_assign_device(XcObject *self,
while ( next_bdf(&pci_str, &seg, &bus, &dev, &func) )
{
bdf |= (bus & 0xff) << 16;
bdf |= (dev & 0x1f) << 11;
bdf |= (func & 0x7) << 8;
sbdf = seg << 16;
sbdf |= (bus & 0xff) << 8;
sbdf |= (dev & 0x1f) << 3;
sbdf |= (func & 0x7);
if ( xc_test_assign_device(self->xc_handle, dom, bdf) != 0 )
if ( xc_test_assign_device(self->xc_handle, dom, sbdf) != 0 )
{
if (errno == ENOSYS)
bdf = -1;
sbdf = -1;
break;
}
bdf = 0;
sbdf = 0;
}
return Py_BuildValue("i", bdf);
return Py_BuildValue("i", sbdf);
}
static PyObject *pyxc_assign_device(XcObject *self,
......@@ -641,7 +642,7 @@ static PyObject *pyxc_assign_device(XcObject *self,
{
uint32_t dom;
char *pci_str;
int32_t bdf = 0;
int32_t sbdf = 0;
int seg, bus, dev, func;
static char *kwd_list[] = { "domid", "pci", NULL };
......@@ -651,20 +652,21 @@ static PyObject *pyxc_assign_device(XcObject *self,
while ( next_bdf(&pci_str, &seg, &bus, &dev, &func) )
{
bdf |= (bus & 0xff) << 16;
bdf |= (dev & 0x1f) << 11;
bdf |= (func & 0x7) << 8;
sbdf = seg << 16;
sbdf |= (bus & 0xff) << 8;
sbdf |= (dev & 0x1f) << 3;
sbdf |= (func & 0x7);
if ( xc_assign_device(self->xc_handle, dom, bdf) != 0 )
if ( xc_assign_device(self->xc_handle, dom, sbdf) != 0 )
{
if (errno == ENOSYS)
bdf = -1;
sbdf = -1;
break;
}
bdf = 0;
sbdf = 0;
}
return Py_BuildValue("i", bdf);
return Py_BuildValue("i", sbdf);
}
static PyObject *pyxc_deassign_device(XcObject *self,
......@@ -673,7 +675,7 @@ static PyObject *pyxc_deassign_device(XcObject *self,
{
uint32_t dom;
char *pci_str;
int32_t bdf = 0;
int32_t sbdf = 0;
int seg, bus, dev, func;
static char *kwd_list[] = { "domid", "pci", NULL };
......@@ -683,26 +685,27 @@ static PyObject *pyxc_deassign_device(XcObject *self,
while ( next_bdf(&pci_str, &seg, &bus, &dev, &func) )
{
bdf |= (bus & 0xff) << 16;
bdf |= (dev & 0x1f) << 11;
bdf |= (func & 0x7) << 8;
sbdf = seg << 16;
sbdf |= (bus & 0xff) << 8;
sbdf |= (dev & 0x1f) << 3;
sbdf |= (func & 0x7);
if ( xc_deassign_device(self->xc_handle, dom, bdf) != 0 )
if ( xc_deassign_device(self->xc_handle, dom, sbdf) != 0 )
{
if (errno == ENOSYS)
bdf = -1;
sbdf = -1;
break;
}
bdf = 0;
sbdf = 0;
}
return Py_BuildValue("i", bdf);
return Py_BuildValue("i", sbdf);
}
static PyObject *pyxc_get_device_group(XcObject *self,
PyObject *args)
{
uint32_t bdf = 0;
uint32_t sbdf;
uint32_t max_sdevs, num_sdevs;
int domid, seg, bus, dev, func, rc, i;
PyObject *Pystr;
......@@ -720,12 +723,13 @@ static PyObject *pyxc_get_device_group(XcObject *self,
if (sdev_array == NULL)
return PyErr_NoMemory();
bdf |= (bus & 0xff) << 16;
bdf |= (dev & 0x1f) << 11;
bdf |= (func & 0x7) << 8;
sbdf = seg << 16;
sbdf |= (bus & 0xff) << 8;
sbdf |= (dev & 0x1f) << 3;
sbdf |= (func & 0x7);
rc = xc_get_device_group(self->xc_handle,
domid, bdf, max_sdevs, &num_sdevs, sdev_array);
domid, sbdf, max_sdevs, &num_sdevs, sdev_array);
if ( rc < 0 )
{
......
......@@ -258,138 +258,6 @@ long arch_do_domctl(xen_domctl_t *op, XEN_GUEST_HANDLE(xen_domctl_t) u_domctl)
}
break;
case XEN_DOMCTL_get_device_group:
{
struct domain *d;
u32 max_sdevs;
u8 bus, devfn;
XEN_GUEST_HANDLE_64(uint32) sdevs;
int num_sdevs;
ret = -ENOSYS;
if ( !iommu_enabled )
break;
ret = -EINVAL;
if ( (d = rcu_lock_domain_by_id(op->domain)) == NULL )
break;
bus = (op->u.get_device_group.machine_bdf >> 16) & 0xff;
devfn = (op->u.get_device_group.machine_bdf >> 8) & 0xff;
max_sdevs = op->u.get_device_group.max_sdevs;
sdevs = op->u.get_device_group.sdev_array;
num_sdevs = iommu_get_device_group(d, bus, devfn, sdevs, max_sdevs);
if ( num_sdevs < 0 )
{
dprintk(XENLOG_ERR, "iommu_get_device_group() failed!\n");
ret = -EFAULT;
op->u.get_device_group.num_sdevs = 0;
}
else
{
ret = 0;
op->u.get_device_group.num_sdevs = num_sdevs;
}
if ( copy_to_guest(u_domctl, op, 1) )
ret = -EFAULT;
rcu_unlock_domain(d);
}
break;
case XEN_DOMCTL_test_assign_device:
{
u8 bus, devfn;
ret = -ENOSYS;
if ( !iommu_enabled )
break;
ret = -EINVAL;
bus = (op->u.assign_device.machine_bdf >> 16) & 0xff;
devfn = (op->u.assign_device.machine_bdf >> 8) & 0xff;
if ( device_assigned(bus, devfn) )
{
printk( "XEN_DOMCTL_test_assign_device: "
"%x:%x.%x already assigned, or non-existent\n",
bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
break;
}
ret = 0;
}
break;
case XEN_DOMCTL_assign_device:
{
struct domain *d;
u8 bus, devfn;
ret = -ENOSYS;
if ( !iommu_enabled )
break;
ret = -EINVAL;
if ( unlikely((d = get_domain_by_id(op->domain)) == NULL) )
{
gdprintk(XENLOG_ERR,
"XEN_DOMCTL_assign_device: get_domain_by_id() failed\n");
break;
}
bus = (op->u.assign_device.machine_bdf >> 16) & 0xff;
devfn = (op->u.assign_device.machine_bdf >> 8) & 0xff;
if ( device_assigned(bus, devfn) )
{
gdprintk(XENLOG_ERR, "XEN_DOMCTL_assign_device: "
"%x:%x.%x already assigned, or non-existent\n",
bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
break;
}
ret = assign_device(d, bus, devfn);
if ( ret )
gdprintk(XENLOG_ERR, "XEN_DOMCTL_assign_device: "
"assign device (%x:%x.%x) failed\n",
bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
put_domain(d);
}
break;
case XEN_DOMCTL_deassign_device:
{
struct domain *d;
u8 bus, devfn;
ret = -ENOSYS;
if ( !iommu_enabled )
break;
ret = -EINVAL;
if ( unlikely((d = get_domain_by_id(op->domain)) == NULL) )
{
gdprintk(XENLOG_ERR,
"XEN_DOMCTL_deassign_device: get_domain_by_id() failed\n");
break;
}
bus = (op->u.assign_device.machine_bdf >> 16) & 0xff;
devfn = (op->u.assign_device.machine_bdf >> 8) & 0xff;
if ( !device_assigned(bus, devfn) )
break;
spin_lock(&pcidevs_lock);
ret = deassign_device(d, bus, devfn);
spin_unlock(&pcidevs_lock);
if ( ret )
gdprintk(XENLOG_ERR, "XEN_DOMCTL_deassign_device: "
"deassign device (%x:%x.%x) failed\n",
bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
put_domain(d);
}
break;
case XEN_DOMCTL_bind_pt_irq:
{
struct domain * d;
......@@ -707,8 +575,8 @@ long arch_do_domctl(xen_domctl_t *op, XEN_GUEST_HANDLE(xen_domctl_t) u_domctl)
break;
default:
printk("arch_do_domctl: unrecognized domctl: %d!!!\n",op->cmd);
ret = -ENOSYS;
ret = iommu_do_domctl(op, u_domctl);
break;
}
......
......@@ -742,144 +742,6 @@ long arch_do_domctl(
}
break;
case XEN_DOMCTL_get_device_group:
{
struct domain *d;
u32 max_sdevs;
u8 bus, devfn;
XEN_GUEST_HANDLE_64(uint32) sdevs;
int num_sdevs;
ret = -ENOSYS;
if ( !iommu_enabled )
break;
ret = -EINVAL;
if ( (d = rcu_lock_domain_by_id(domctl->domain)) == NULL )
break;
bus = (domctl->u.get_device_group.machine_bdf >> 16) & 0xff;
devfn = (domctl->u.get_device_group.machine_bdf >> 8) & 0xff;
max_sdevs = domctl->u.get_device_group.max_sdevs;
sdevs = domctl->u.get_device_group.sdev_array;
num_sdevs = iommu_get_device_group(d, bus, devfn, sdevs, max_sdevs);
if ( num_sdevs < 0 )
{
dprintk(XENLOG_ERR, "iommu_get_device_group() failed!\n");
ret = -EFAULT;
domctl->u.get_device_group.num_sdevs = 0;
}
else
{
ret = 0;
domctl->u.get_device_group.num_sdevs = num_sdevs;
}
if ( copy_to_guest(u_domctl, domctl, 1) )
ret = -EFAULT;
rcu_unlock_domain(d);
}
break;
case XEN_DOMCTL_test_assign_device:
{
u8 bus, devfn;
ret = -ENOSYS;
if ( !iommu_enabled )
break;
ret = xsm_test_assign_device(domctl->u.assign_device.machine_bdf);
if ( ret )
break;
ret = -EINVAL;
bus = (domctl->u.assign_device.machine_bdf >> 16) & 0xff;
devfn = (domctl->u.assign_device.machine_bdf >> 8) & 0xff;
if ( device_assigned(bus, devfn) )
{
gdprintk(XENLOG_ERR, "XEN_DOMCTL_test_assign_device: "
"%x:%x.%x already assigned, or non-existent\n",
bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
break;
}
ret = 0;
}
break;
case XEN_DOMCTL_assign_device:
{
struct domain *d;
u8 bus, devfn;
ret = -ENOSYS;
if ( !iommu_enabled )
break;
ret = -EINVAL;
if ( unlikely((d = get_domain_by_id(domctl->domain)) == NULL) )
{
gdprintk(XENLOG_ERR,
"XEN_DOMCTL_assign_device: get_domain_by_id() failed\n");
break;
}
ret = xsm_assign_device(d, domctl->u.assign_device.machine_bdf);
if ( ret )
goto assign_device_out;
bus = (domctl->u.assign_device.machine_bdf >> 16) & 0xff;
devfn = (domctl->u.assign_device.machine_bdf >> 8) & 0xff;
ret = assign_device(d, bus, devfn);
if ( ret )
gdprintk(XENLOG_ERR, "XEN_DOMCTL_assign_device: "
"assign device (%x:%x.%x) failed\n",
bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
assign_device_out:
put_domain(d);
}
break;
case XEN_DOMCTL_deassign_device:
{
struct domain *d;
u8 bus, devfn;
ret = -ENOSYS;
if ( !iommu_enabled )
break;
ret = -EINVAL;
if ( unlikely((d = get_domain_by_id(domctl->domain)) == NULL) )
{
gdprintk(XENLOG_ERR,
"XEN_DOMCTL_deassign_device: get_domain_by_id() failed\n");
break;
}
ret = xsm_assign_device(d, domctl->u.assign_device.machine_bdf);
if ( ret )
goto deassign_device_out;
bus = (domctl->u.assign_device.machine_bdf >> 16) & 0xff;
devfn = (domctl->u.assign_device.machine_bdf >> 8) & 0xff;
spin_lock(&pcidevs_lock);
ret = deassign_device(d, bus, devfn);
spin_unlock(&pcidevs_lock);
if ( ret )
gdprintk(XENLOG_ERR, "XEN_DOMCTL_deassign_device: "
"deassign device (%x:%x.%x) failed\n",
bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
deassign_device_out:
put_domain(d);
}
break;
case XEN_DOMCTL_bind_pt_irq:
{
struct domain * d;
......@@ -1601,7 +1463,7 @@ long arch_do_domctl(
break;
default:
ret = -ENOSYS;
ret = iommu_do_domctl(domctl, u_domctl);
break;
}
......
......@@ -305,7 +305,7 @@ static void amd_iommu_disable_domain_device(
}
static int reassign_device( struct domain *source, struct domain *target,
u8 bus, u8 devfn)
u16 seg, u8 bus, u8 devfn)
{
struct pci_dev *pdev;
struct amd_iommu *iommu;
......@@ -313,7 +313,7 @@ static int reassign_device( struct domain *source, struct domain *target,
struct hvm_iommu *t = domain_hvm_iommu(target);
ASSERT(spin_is_locked(&pcidevs_lock));
pdev = pci_get_pdev_by_domain(source, 0, bus, devfn);
pdev = pci_get_pdev_by_domain(source, seg, bus, devfn);
if ( !pdev )
return -ENODEV;
......@@ -346,7 +346,7 @@ static int reassign_device( struct domain *source, struct domain *target,
return 0;
}
static int amd_iommu_assign_device(struct domain *d, u8 bus, u8 devfn)
static int amd_iommu_assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn)
{
int bdf = (bus << 8) | devfn;
int req_id = get_dma_requestor_id(bdf);
......@@ -361,7 +361,7 @@ static int amd_iommu_assign_device(struct domain *d, u8 bus, u8 devfn)
ivrs_mappings[req_id].read_permission);
}
return reassign_device(dom0, d, bus, devfn);
return reassign_device(dom0, d, seg, bus, devfn);
}
static void deallocate_next_page_table(struct page_info* pg, int level)
......@@ -426,9 +426,9 @@ static void amd_iommu_domain_destroy(struct domain *d)
}
static int amd_iommu_return_device(
struct domain *s, struct domain *t, u8 bus, u8 devfn)
struct domain *s, struct domain *t, u16 seg, u8 bus, u8 devfn)
{
return reassign_device(s, t, bus, devfn);
return reassign_device(s, t, seg, bus, devfn);
}
static int amd_iommu_add_device(struct pci_dev *pdev)
......@@ -475,7 +475,7 @@ static int amd_iommu_remove_device(struct pci_dev *pdev)
return 0;
}
static int amd_iommu_group_id(u8 bus, u8 devfn)
static int amd_iommu_group_id(u16 seg, u8 bus, u8 devfn)
{
int rt;
int bdf = (bus << 8) | devfn;
......
......@@ -19,6 +19,7 @@
#include <xen/paging.h>
#include <xen/guest_access.h>
#include <xen/softirq.h>
#include <xsm/xsm.h>
static void parse_iommu_param(char *s);
static int iommu_populate_page_table(struct domain *d);
......@@ -165,7 +166,22 @@ int iommu_remove_device(struct pci_dev *pdev)
return hd->platform_ops->remove_device(pdev);
}
int assign_device(struct domain *d, u8 bus, u8 devfn)
/*
* If the device isn't owned by dom0, it means it already
* has been assigned to other domain, or it doesn't exist.
*/
static int device_assigned(u16 seg, u8 bus, u8 devfn)
{
struct pci_dev *pdev;
spin_lock(&pcidevs_lock);
pdev = pci_get_pdev_by_domain(dom0, seg, bus, devfn);
spin_unlock(&pcidevs_lock);
return pdev ? 0 : -1;
}
static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn)
{
struct hvm_iommu *hd = domain_hvm_iommu(d);
int rc = 0;
......@@ -174,7 +190,7 @@ int assign_device(struct domain *d, u8 bus, u8 devfn)
return 0;
spin_lock(&pcidevs_lock);
if ( (rc = hd->platform_ops->assign_device(d, bus, devfn)) )
if ( (rc = hd->platform_ops->assign_device(d, seg, bus, devfn)) )
goto done;
if ( has_arch_pdevs(d) && !need_iommu(d) )
......@@ -272,7 +288,7 @@ int iommu_unmap_page(struct domain *d, unsigned long gfn)
}
/* caller should hold the pcidevs_lock */
int deassign_device(struct domain *d, u8 bus, u8 devfn)
int deassign_device(struct domain *d, u16 seg, u8 bus, u8 devfn)
{
struct hvm_iommu *hd = domain_hvm_iommu(d);
struct pci_dev *pdev = NULL;
......@@ -282,7 +298,7 @@ int deassign_device(struct domain *d, u8 bus, u8 devfn)
return -EINVAL;
ASSERT(spin_is_locked(&pcidevs_lock));
pdev = pci_get_pdev(0, bus, devfn);
pdev = pci_get_pdev(seg, bus, devfn);
if ( !pdev )
return -ENODEV;
......@@ -293,12 +309,12 @@ int deassign_device(struct domain *d, u8 bus, u8 devfn)
return -EINVAL;
}
ret = hd->platform_ops->reassign_device(d, dom0, bus, devfn);
ret = hd->platform_ops->reassign_device(d, dom0, seg, bus, devfn);
if ( ret )
{
dprintk(XENLOG_ERR VTDPREFIX,
"d%d: Deassign device (%x:%x.%x) failed!\n",
d->domain_id, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
"d%d: Deassign device (%04x:%02x:%02x.%u) failed!\n",
d->domain_id, seg, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
return ret;
}
......@@ -347,7 +363,8 @@ int __init iommu_setup(void)
return rc;
}
int iommu_get_device_group(struct domain *d, u8 bus, u8 devfn,
static int iommu_get_device_group(
struct domain *d, u16 seg, u8 bus, u8 devfn,
XEN_GUEST_HANDLE_64(uint32) buf, int max_sdevs)
{
struct hvm_iommu *hd = domain_hvm_iommu(d);
......@@ -360,15 +377,16 @@ int iommu_get_device_group(struct domain *d, u8 bus, u8 devfn,
if ( !iommu_enabled || !ops || !ops->get_device_group_id )
return 0;
group_id = ops->get_device_group_id(bus, devfn);
group_id = ops->get_device_group_id(seg, bus, devfn);
spin_lock(&pcidevs_lock);
for_each_pdev( d, pdev )
{
if ( (pdev->bus == bus) && (pdev->devfn == devfn) )
if ( (pdev->seg != seg) ||
((pdev->bus == bus) && (pdev->devfn == devfn)) )
continue;
sdev_id = ops->get_device_group_id(pdev->bus, pdev->devfn);
sdev_id = ops->get_device_group_id(seg, pdev->bus, pdev->devfn);
if ( (sdev_id == group_id) && (i < max_sdevs) )
{
bdf = 0;
......@@ -443,6 +461,154 @@ void iommu_crash_shutdown(void)
iommu_enabled = 0;
}
int iommu_do_domctl(
struct xen_domctl *domctl,
XEN_GUEST_HANDLE(xen_domctl_t) u_domctl)
{
struct domain *d;
u16 seg;
u8 bus, devfn;
int ret = 0;
if ( !iommu_enabled )
return -ENOSYS;
switch ( domctl->cmd )
{
case XEN_DOMCTL_get_device_group:
{
u32 max_sdevs;
XEN_GUEST_HANDLE_64(uint32) sdevs;
ret = -EINVAL;
if ( (d = rcu_lock_domain_by_id(domctl->domain)) == NULL )
break;
seg = domctl->u.get_device_group.machine_sbdf >> 16;
bus = (domctl->u.get_device_group.machine_sbdf >> 8) & 0xff;
devfn = domctl->u.get_device_group.machine_sbdf & 0xff;
max_sdevs = domctl->u.get_device_group.max_sdevs;
<