2013-01-15 00:08:01 +01:00
|
|
|
/*
|
|
|
|
* VirtioBus
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 : GreenSocs Ltd
|
|
|
|
* http://www.greensocs.com/ , email: info@greensocs.com
|
|
|
|
*
|
|
|
|
* Developed by :
|
|
|
|
* Frederic Konrad <fred.konrad@greensocs.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:07 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-01-15 00:08:01 +01:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2017-03-17 13:32:42 +01:00
|
|
|
#include "qapi/error.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/virtio/virtio-bus.h"
|
|
|
|
#include "hw/virtio/virtio.h"
|
2016-12-30 11:09:10 +01:00
|
|
|
#include "exec/address-spaces.h"
|
2013-01-15 00:08:01 +01:00
|
|
|
|
|
|
|
/* #define DEBUG_VIRTIO_BUS */
|
|
|
|
|
|
|
|
#ifdef DEBUG_VIRTIO_BUS
|
|
|
|
#define DPRINTF(fmt, ...) \
|
|
|
|
do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
|
|
|
|
#else
|
|
|
|
#define DPRINTF(fmt, ...) do { } while (0)
|
|
|
|
#endif
|
|
|
|
|
2013-09-20 13:59:08 +02:00
|
|
|
/* A VirtIODevice is being plugged */
|
2015-05-29 08:15:25 +02:00
|
|
|
void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
|
2013-01-15 00:08:01 +01:00
|
|
|
{
|
|
|
|
DeviceState *qdev = DEVICE(vdev);
|
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(qdev));
|
|
|
|
VirtioBusState *bus = VIRTIO_BUS(qbus);
|
|
|
|
VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
|
2015-05-26 16:34:47 +02:00
|
|
|
VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
|
2017-01-17 05:01:00 +01:00
|
|
|
bool has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
|
virtio: fix the condition for iommu_platform not supported
The commit 04ceb61a40 ("virtio: Fail if iommu_platform is requested, but
unsupported") claims to fail the device hotplug when iommu_platform
is requested, but not supported by the (vhost) device. On the first
glance the condition for detecting that situation looks perfect, but
because a certain peculiarity of virtio_platform it ain't.
In fact the aforementioned commit introduces a regression. It breaks
virtio-fs support for Secure Execution, and most likely also for AMD SEV
or any other confidential guest scenario that relies encrypted guest
memory. The same also applies to any other vhost device that does not
support _F_ACCESS_PLATFORM.
The peculiarity is that iommu_platform and _F_ACCESS_PLATFORM collates
"device can not access all of the guest RAM" and "iova != gpa, thus
device needs to translate iova".
Confidential guest technologies currently rely on the device/hypervisor
offering _F_ACCESS_PLATFORM, so that, after the feature has been
negotiated, the guest grants access to the portions of memory the
device needs to see. So in for confidential guests, generally,
_F_ACCESS_PLATFORM is about the restricted access to memory, but not
about the addresses used being something else than guest physical
addresses.
This is the very reason for which commit f7ef7e6e3b ("vhost: correctly
turn on VIRTIO_F_IOMMU_PLATFORM") fences _F_ACCESS_PLATFORM from the
vhost device that does not need it, because on the vhost interface it
only means "I/O address translation is needed".
This patch takes inspiration from f7ef7e6e3b ("vhost: correctly turn on
VIRTIO_F_IOMMU_PLATFORM"), and uses the same condition for detecting the
situation when _F_ACCESS_PLATFORM is requested, but no I/O translation
by the device, and thus no device capability is needed. In this
situation claiming that the device does not support iommu_plattform=on
is counter-productive. So let us stop doing that!
Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
Reported-by: Jakob Naucke <Jakob.Naucke@ibm.com>
Fixes: 04ceb61a40 ("virtio: Fail if iommu_platform is requested, but
unsupported")
Acked-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-stable@nongnu.org
Message-Id: <20220207112857.607829-1-pasic@linux.ibm.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
2022-02-07 12:28:57 +01:00
|
|
|
bool vdev_has_iommu;
|
2017-03-17 13:32:42 +01:00
|
|
|
Error *local_err = NULL;
|
2015-05-26 16:34:47 +02:00
|
|
|
|
2013-01-15 00:08:01 +01:00
|
|
|
DPRINTF("%s: plug device.\n", qbus->name);
|
|
|
|
|
2016-09-13 15:30:30 +02:00
|
|
|
if (klass->pre_plugged != NULL) {
|
2017-03-17 13:32:42 +01:00
|
|
|
klass->pre_plugged(qbus->parent, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-15 00:08:01 +01:00
|
|
|
}
|
|
|
|
|
2015-05-26 16:34:47 +02:00
|
|
|
/* Get the features of the plugged device. */
|
|
|
|
assert(vdc->get_features != NULL);
|
2015-07-27 11:49:19 +02:00
|
|
|
vdev->host_features = vdc->get_features(vdev, vdev->host_features,
|
2017-03-17 13:32:42 +01:00
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-13 15:30:30 +02:00
|
|
|
|
|
|
|
if (klass->device_plugged != NULL) {
|
2017-03-17 13:32:42 +01:00
|
|
|
klass->device_plugged(qbus->parent, &local_err);
|
|
|
|
}
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
2015-12-02 18:31:57 +01:00
|
|
|
}
|
2016-12-30 11:09:10 +01:00
|
|
|
|
virtio: fix feature negotiation for ACCESS_PLATFORM
Unlike most virtio features ACCESS_PLATFORM is considered mandatory by
QEMU, i.e. the driver must accept it if offered by the device. The
virtio specification says that the driver SHOULD accept the
ACCESS_PLATFORM feature if offered, and that the device MAY fail to
operate if ACCESS_PLATFORM was offered but not negotiated.
While a SHOULD ain't exactly a MUST, we are certainly allowed to fail
the device when the driver fences ACCESS_PLATFORM. With commit
2943b53f68 ("virtio: force VIRTIO_F_IOMMU_PLATFORM") we already made the
decision to do so whenever the get_dma_as() callback is implemented (by
the bus), which in practice means for the entirety of virtio-pci.
That means, if the device needs to translate I/O addresses, then
ACCESS_PLATFORM is mandatory. The aforementioned commit tells us in the
commit message that this is for security reasons. More precisely if we
were to allow a less then trusted driver (e.g. an user-space driver, or
a nested guest) to make the device bypass the IOMMU by not negotiating
ACCESS_PLATFORM, then the guest kernel would have no ability to
control/police (by programming the IOMMU) what pieces of guest memory
the driver may manipulate using the device. Which would break security
assumptions within the guest.
If ACCESS_PLATFORM is offered not because we want the device to utilize
an IOMMU and do address translation, but because the device does not
have access to the entire guest RAM, and needs the driver to grant
access to the bits it needs access to (e.g. confidential guest support),
we still require the guest to have the corresponding logic and to accept
ACCESS_PLATFORM. If the driver does not accept ACCESS_PLATFORM, then
things are bound to go wrong, and we may see failures much less graceful
than failing the device because the driver didn't negotiate
ACCESS_PLATFORM.
So let us make ACCESS_PLATFORM mandatory for the driver regardless
of whether the get_dma_as() callback is implemented or not.
Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
Fixes: 2943b53f68 ("virtio: force VIRTIO_F_IOMMU_PLATFORM")
Message-Id: <20220307112939.2780117-1-pasic@linux.ibm.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
2022-03-07 12:29:39 +01:00
|
|
|
vdev->dma_as = &address_space_memory;
|
|
|
|
if (has_iommu) {
|
|
|
|
vdev_has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
|
|
|
|
/*
|
|
|
|
* Present IOMMU_PLATFORM to the driver iff iommu_plattform=on and
|
|
|
|
* device operational. If the driver does not accept IOMMU_PLATFORM
|
|
|
|
* we fail the device.
|
|
|
|
*/
|
2017-01-17 05:01:00 +01:00
|
|
|
virtio_add_feature(&vdev->host_features, VIRTIO_F_IOMMU_PLATFORM);
|
virtio: fix feature negotiation for ACCESS_PLATFORM
Unlike most virtio features ACCESS_PLATFORM is considered mandatory by
QEMU, i.e. the driver must accept it if offered by the device. The
virtio specification says that the driver SHOULD accept the
ACCESS_PLATFORM feature if offered, and that the device MAY fail to
operate if ACCESS_PLATFORM was offered but not negotiated.
While a SHOULD ain't exactly a MUST, we are certainly allowed to fail
the device when the driver fences ACCESS_PLATFORM. With commit
2943b53f68 ("virtio: force VIRTIO_F_IOMMU_PLATFORM") we already made the
decision to do so whenever the get_dma_as() callback is implemented (by
the bus), which in practice means for the entirety of virtio-pci.
That means, if the device needs to translate I/O addresses, then
ACCESS_PLATFORM is mandatory. The aforementioned commit tells us in the
commit message that this is for security reasons. More precisely if we
were to allow a less then trusted driver (e.g. an user-space driver, or
a nested guest) to make the device bypass the IOMMU by not negotiating
ACCESS_PLATFORM, then the guest kernel would have no ability to
control/police (by programming the IOMMU) what pieces of guest memory
the driver may manipulate using the device. Which would break security
assumptions within the guest.
If ACCESS_PLATFORM is offered not because we want the device to utilize
an IOMMU and do address translation, but because the device does not
have access to the entire guest RAM, and needs the driver to grant
access to the bits it needs access to (e.g. confidential guest support),
we still require the guest to have the corresponding logic and to accept
ACCESS_PLATFORM. If the driver does not accept ACCESS_PLATFORM, then
things are bound to go wrong, and we may see failures much less graceful
than failing the device because the driver didn't negotiate
ACCESS_PLATFORM.
So let us make ACCESS_PLATFORM mandatory for the driver regardless
of whether the get_dma_as() callback is implemented or not.
Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
Fixes: 2943b53f68 ("virtio: force VIRTIO_F_IOMMU_PLATFORM")
Message-Id: <20220307112939.2780117-1-pasic@linux.ibm.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
2022-03-07 12:29:39 +01:00
|
|
|
if (klass->get_dma_as) {
|
|
|
|
vdev->dma_as = klass->get_dma_as(qbus->parent);
|
|
|
|
if (!vdev_has_iommu && vdev->dma_as != &address_space_memory) {
|
|
|
|
error_setg(errp,
|
virtio: fix the condition for iommu_platform not supported
The commit 04ceb61a40 ("virtio: Fail if iommu_platform is requested, but
unsupported") claims to fail the device hotplug when iommu_platform
is requested, but not supported by the (vhost) device. On the first
glance the condition for detecting that situation looks perfect, but
because a certain peculiarity of virtio_platform it ain't.
In fact the aforementioned commit introduces a regression. It breaks
virtio-fs support for Secure Execution, and most likely also for AMD SEV
or any other confidential guest scenario that relies encrypted guest
memory. The same also applies to any other vhost device that does not
support _F_ACCESS_PLATFORM.
The peculiarity is that iommu_platform and _F_ACCESS_PLATFORM collates
"device can not access all of the guest RAM" and "iova != gpa, thus
device needs to translate iova".
Confidential guest technologies currently rely on the device/hypervisor
offering _F_ACCESS_PLATFORM, so that, after the feature has been
negotiated, the guest grants access to the portions of memory the
device needs to see. So in for confidential guests, generally,
_F_ACCESS_PLATFORM is about the restricted access to memory, but not
about the addresses used being something else than guest physical
addresses.
This is the very reason for which commit f7ef7e6e3b ("vhost: correctly
turn on VIRTIO_F_IOMMU_PLATFORM") fences _F_ACCESS_PLATFORM from the
vhost device that does not need it, because on the vhost interface it
only means "I/O address translation is needed".
This patch takes inspiration from f7ef7e6e3b ("vhost: correctly turn on
VIRTIO_F_IOMMU_PLATFORM"), and uses the same condition for detecting the
situation when _F_ACCESS_PLATFORM is requested, but no I/O translation
by the device, and thus no device capability is needed. In this
situation claiming that the device does not support iommu_plattform=on
is counter-productive. So let us stop doing that!
Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
Reported-by: Jakob Naucke <Jakob.Naucke@ibm.com>
Fixes: 04ceb61a40 ("virtio: Fail if iommu_platform is requested, but
unsupported")
Acked-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-stable@nongnu.org
Message-Id: <20220207112857.607829-1-pasic@linux.ibm.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
2022-02-07 12:28:57 +01:00
|
|
|
"iommu_platform=true is not supported by the device");
|
virtio: fix feature negotiation for ACCESS_PLATFORM
Unlike most virtio features ACCESS_PLATFORM is considered mandatory by
QEMU, i.e. the driver must accept it if offered by the device. The
virtio specification says that the driver SHOULD accept the
ACCESS_PLATFORM feature if offered, and that the device MAY fail to
operate if ACCESS_PLATFORM was offered but not negotiated.
While a SHOULD ain't exactly a MUST, we are certainly allowed to fail
the device when the driver fences ACCESS_PLATFORM. With commit
2943b53f68 ("virtio: force VIRTIO_F_IOMMU_PLATFORM") we already made the
decision to do so whenever the get_dma_as() callback is implemented (by
the bus), which in practice means for the entirety of virtio-pci.
That means, if the device needs to translate I/O addresses, then
ACCESS_PLATFORM is mandatory. The aforementioned commit tells us in the
commit message that this is for security reasons. More precisely if we
were to allow a less then trusted driver (e.g. an user-space driver, or
a nested guest) to make the device bypass the IOMMU by not negotiating
ACCESS_PLATFORM, then the guest kernel would have no ability to
control/police (by programming the IOMMU) what pieces of guest memory
the driver may manipulate using the device. Which would break security
assumptions within the guest.
If ACCESS_PLATFORM is offered not because we want the device to utilize
an IOMMU and do address translation, but because the device does not
have access to the entire guest RAM, and needs the driver to grant
access to the bits it needs access to (e.g. confidential guest support),
we still require the guest to have the corresponding logic and to accept
ACCESS_PLATFORM. If the driver does not accept ACCESS_PLATFORM, then
things are bound to go wrong, and we may see failures much less graceful
than failing the device because the driver didn't negotiate
ACCESS_PLATFORM.
So let us make ACCESS_PLATFORM mandatory for the driver regardless
of whether the get_dma_as() callback is implemented or not.
Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
Fixes: 2943b53f68 ("virtio: force VIRTIO_F_IOMMU_PLATFORM")
Message-Id: <20220307112939.2780117-1-pasic@linux.ibm.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
2022-03-07 12:29:39 +01:00
|
|
|
return;
|
|
|
|
}
|
virtio: fix the condition for iommu_platform not supported
The commit 04ceb61a40 ("virtio: Fail if iommu_platform is requested, but
unsupported") claims to fail the device hotplug when iommu_platform
is requested, but not supported by the (vhost) device. On the first
glance the condition for detecting that situation looks perfect, but
because a certain peculiarity of virtio_platform it ain't.
In fact the aforementioned commit introduces a regression. It breaks
virtio-fs support for Secure Execution, and most likely also for AMD SEV
or any other confidential guest scenario that relies encrypted guest
memory. The same also applies to any other vhost device that does not
support _F_ACCESS_PLATFORM.
The peculiarity is that iommu_platform and _F_ACCESS_PLATFORM collates
"device can not access all of the guest RAM" and "iova != gpa, thus
device needs to translate iova".
Confidential guest technologies currently rely on the device/hypervisor
offering _F_ACCESS_PLATFORM, so that, after the feature has been
negotiated, the guest grants access to the portions of memory the
device needs to see. So in for confidential guests, generally,
_F_ACCESS_PLATFORM is about the restricted access to memory, but not
about the addresses used being something else than guest physical
addresses.
This is the very reason for which commit f7ef7e6e3b ("vhost: correctly
turn on VIRTIO_F_IOMMU_PLATFORM") fences _F_ACCESS_PLATFORM from the
vhost device that does not need it, because on the vhost interface it
only means "I/O address translation is needed".
This patch takes inspiration from f7ef7e6e3b ("vhost: correctly turn on
VIRTIO_F_IOMMU_PLATFORM"), and uses the same condition for detecting the
situation when _F_ACCESS_PLATFORM is requested, but no I/O translation
by the device, and thus no device capability is needed. In this
situation claiming that the device does not support iommu_plattform=on
is counter-productive. So let us stop doing that!
Signed-off-by: Halil Pasic <pasic@linux.ibm.com>
Reported-by: Jakob Naucke <Jakob.Naucke@ibm.com>
Fixes: 04ceb61a40 ("virtio: Fail if iommu_platform is requested, but
unsupported")
Acked-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-stable@nongnu.org
Message-Id: <20220207112857.607829-1-pasic@linux.ibm.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
2022-02-07 12:28:57 +01:00
|
|
|
}
|
2016-12-30 11:09:10 +01:00
|
|
|
}
|
2013-01-15 00:08:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the virtio_bus */
|
|
|
|
void virtio_bus_reset(VirtioBusState *bus)
|
|
|
|
{
|
2013-09-20 13:31:39 +02:00
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
|
|
|
|
2014-11-13 03:39:32 +01:00
|
|
|
DPRINTF("%s: reset device.\n", BUS(bus)->name);
|
2013-09-20 13:31:39 +02:00
|
|
|
if (vdev != NULL) {
|
|
|
|
virtio_reset(vdev);
|
2013-01-15 00:08:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:59:08 +02:00
|
|
|
/* A VirtIODevice is being unplugged */
|
|
|
|
void virtio_bus_device_unplugged(VirtIODevice *vdev)
|
2013-01-15 00:08:01 +01:00
|
|
|
{
|
2013-09-20 13:59:08 +02:00
|
|
|
DeviceState *qdev = DEVICE(vdev);
|
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(qdev));
|
|
|
|
VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
|
2013-09-20 13:31:39 +02:00
|
|
|
|
2013-01-15 00:08:01 +01:00
|
|
|
DPRINTF("%s: remove device.\n", qbus->name);
|
|
|
|
|
2013-09-20 13:31:39 +02:00
|
|
|
if (vdev != NULL) {
|
2013-09-20 13:59:08 +02:00
|
|
|
if (klass->device_unplugged != NULL) {
|
|
|
|
klass->device_unplugged(qbus->parent);
|
2013-01-15 00:08:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the device id of the plugged device. */
|
|
|
|
uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
|
|
|
|
{
|
2013-09-20 13:31:39 +02:00
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
|
|
|
assert(vdev != NULL);
|
|
|
|
return vdev->device_id;
|
2013-01-15 00:08:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the config_len field of the plugged device. */
|
|
|
|
size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
|
|
|
|
{
|
2013-09-20 13:31:39 +02:00
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
|
|
|
assert(vdev != NULL);
|
|
|
|
return vdev->config_len;
|
2013-01-15 00:08:01 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 00:08:02 +01:00
|
|
|
/* Get bad features of the plugged device. */
|
|
|
|
uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
|
|
|
|
{
|
2013-09-20 13:31:39 +02:00
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
2013-01-15 00:08:02 +01:00
|
|
|
VirtioDeviceClass *k;
|
2013-09-20 13:31:39 +02:00
|
|
|
|
|
|
|
assert(vdev != NULL);
|
|
|
|
k = VIRTIO_DEVICE_GET_CLASS(vdev);
|
2013-01-15 00:08:02 +01:00
|
|
|
if (k->bad_features != NULL) {
|
2013-09-20 13:31:39 +02:00
|
|
|
return k->bad_features(vdev);
|
2013-01-15 00:08:02 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get config of the plugged device. */
|
|
|
|
void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
|
|
|
|
{
|
2013-09-20 13:31:39 +02:00
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
2013-01-15 00:08:02 +01:00
|
|
|
VirtioDeviceClass *k;
|
2013-09-20 13:31:39 +02:00
|
|
|
|
|
|
|
assert(vdev != NULL);
|
|
|
|
k = VIRTIO_DEVICE_GET_CLASS(vdev);
|
2013-01-15 00:08:02 +01:00
|
|
|
if (k->get_config != NULL) {
|
2013-09-20 13:31:39 +02:00
|
|
|
k->get_config(vdev, config);
|
2013-01-15 00:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 10:21:17 +02:00
|
|
|
/* Set config of the plugged device. */
|
|
|
|
void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
|
|
|
|
{
|
2013-09-20 13:31:39 +02:00
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
2013-04-24 10:21:17 +02:00
|
|
|
VirtioDeviceClass *k;
|
2013-09-20 13:31:39 +02:00
|
|
|
|
|
|
|
assert(vdev != NULL);
|
|
|
|
k = VIRTIO_DEVICE_GET_CLASS(vdev);
|
2013-04-24 10:21:17 +02:00
|
|
|
if (k->set_config != NULL) {
|
2013-09-20 13:31:39 +02:00
|
|
|
k->set_config(vdev, config);
|
2013-04-24 10:21:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:07:00 +01:00
|
|
|
/* On success, ioeventfd ownership belongs to the caller. */
|
|
|
|
int virtio_bus_grab_ioeventfd(VirtioBusState *bus)
|
|
|
|
{
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
|
|
|
|
|
|
|
|
/* vhost can be used even if ioeventfd=off in the proxy device,
|
|
|
|
* so do not check k->ioeventfd_enabled.
|
|
|
|
*/
|
|
|
|
if (!k->ioeventfd_assign) {
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
|
|
|
|
virtio_bus_stop_ioeventfd(bus);
|
|
|
|
/* Remember that we need to restart ioeventfd
|
|
|
|
* when ioeventfd_grabbed becomes zero.
|
|
|
|
*/
|
|
|
|
bus->ioeventfd_started = true;
|
|
|
|
}
|
|
|
|
bus->ioeventfd_grabbed++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void virtio_bus_release_ioeventfd(VirtioBusState *bus)
|
|
|
|
{
|
|
|
|
assert(bus->ioeventfd_grabbed != 0);
|
|
|
|
if (--bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
|
|
|
|
/* Force virtio_bus_start_ioeventfd to act. */
|
|
|
|
bus->ioeventfd_started = false;
|
|
|
|
virtio_bus_start_ioeventfd(bus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 22:48:07 +02:00
|
|
|
int virtio_bus_start_ioeventfd(VirtioBusState *bus)
|
2016-06-10 11:04:09 +02:00
|
|
|
{
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
|
|
|
|
DeviceState *proxy = DEVICE(BUS(bus)->parent);
|
2016-10-21 22:48:07 +02:00
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
|
|
|
VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
|
|
|
|
int r;
|
2016-06-10 11:04:09 +02:00
|
|
|
|
2016-10-21 22:48:08 +02:00
|
|
|
if (!k->ioeventfd_assign || !k->ioeventfd_enabled(proxy)) {
|
2016-10-21 22:48:07 +02:00
|
|
|
return -ENOSYS;
|
2016-06-10 11:04:09 +02:00
|
|
|
}
|
2016-10-21 22:48:13 +02:00
|
|
|
if (bus->ioeventfd_started) {
|
2016-10-21 22:48:07 +02:00
|
|
|
return 0;
|
2016-06-10 11:04:09 +02:00
|
|
|
}
|
2016-11-18 16:07:00 +01:00
|
|
|
|
|
|
|
/* Only set our notifier if we have ownership. */
|
|
|
|
if (!bus->ioeventfd_grabbed) {
|
|
|
|
r = vdc->start_ioeventfd(vdev);
|
|
|
|
if (r < 0) {
|
|
|
|
error_report("%s: failed. Fallback to userspace (slower).", __func__);
|
|
|
|
return r;
|
|
|
|
}
|
2016-06-10 11:04:09 +02:00
|
|
|
}
|
2016-10-21 22:48:06 +02:00
|
|
|
bus->ioeventfd_started = true;
|
2016-10-21 22:48:07 +02:00
|
|
|
return 0;
|
2016-06-10 11:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void virtio_bus_stop_ioeventfd(VirtioBusState *bus)
|
|
|
|
{
|
|
|
|
VirtIODevice *vdev;
|
2016-10-21 22:48:07 +02:00
|
|
|
VirtioDeviceClass *vdc;
|
2016-06-10 11:04:09 +02:00
|
|
|
|
2016-10-21 22:48:06 +02:00
|
|
|
if (!bus->ioeventfd_started) {
|
2016-06-10 11:04:09 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-10-21 22:48:07 +02:00
|
|
|
|
2016-11-18 16:07:00 +01:00
|
|
|
/* Only remove our notifier if we have ownership. */
|
|
|
|
if (!bus->ioeventfd_grabbed) {
|
|
|
|
vdev = virtio_bus_get_device(bus);
|
|
|
|
vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
|
|
|
|
vdc->stop_ioeventfd(vdev);
|
|
|
|
}
|
2016-10-21 22:48:06 +02:00
|
|
|
bus->ioeventfd_started = false;
|
2016-06-10 11:04:09 +02:00
|
|
|
}
|
|
|
|
|
2016-10-21 22:48:08 +02:00
|
|
|
bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus)
|
|
|
|
{
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
|
|
|
|
DeviceState *proxy = DEVICE(BUS(bus)->parent);
|
|
|
|
|
|
|
|
return k->ioeventfd_assign && k->ioeventfd_enabled(proxy);
|
|
|
|
}
|
|
|
|
|
2016-06-10 11:04:09 +02:00
|
|
|
/*
|
2016-10-21 22:48:13 +02:00
|
|
|
* This function switches ioeventfd on/off in the device.
|
|
|
|
* The caller must set or clear the handlers for the EventNotifier.
|
2016-06-10 11:04:09 +02:00
|
|
|
*/
|
|
|
|
int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign)
|
|
|
|
{
|
2016-10-21 22:48:16 +02:00
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
2016-06-10 11:04:09 +02:00
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
|
|
|
|
DeviceState *proxy = DEVICE(BUS(bus)->parent);
|
2016-10-21 22:48:16 +02:00
|
|
|
VirtQueue *vq = virtio_get_queue(vdev, n);
|
|
|
|
EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
|
|
|
|
int r = 0;
|
2016-06-10 11:04:09 +02:00
|
|
|
|
2016-10-21 22:48:06 +02:00
|
|
|
if (!k->ioeventfd_assign) {
|
2016-06-10 11:04:09 +02:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2016-10-21 22:48:16 +02:00
|
|
|
|
2016-06-10 11:04:09 +02:00
|
|
|
if (assign) {
|
2016-10-21 22:48:16 +02:00
|
|
|
r = event_notifier_init(notifier, 1);
|
|
|
|
if (r < 0) {
|
|
|
|
error_report("%s: unable to init event notifier: %s (%d)",
|
|
|
|
__func__, strerror(-r), r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
r = k->ioeventfd_assign(proxy, notifier, n, true);
|
|
|
|
if (r < 0) {
|
|
|
|
error_report("%s: unable to assign ioeventfd: %d", __func__, r);
|
2018-01-29 15:20:56 +01:00
|
|
|
virtio_bus_cleanup_host_notifier(bus, n);
|
2016-10-21 22:48:16 +02:00
|
|
|
}
|
2016-10-21 22:48:13 +02:00
|
|
|
} else {
|
2016-10-21 22:48:16 +02:00
|
|
|
k->ioeventfd_assign(proxy, notifier, n, false);
|
2016-06-10 11:04:09 +02:00
|
|
|
}
|
2016-10-21 22:48:16 +02:00
|
|
|
|
2019-11-05 15:09:46 +01:00
|
|
|
if (r == 0) {
|
|
|
|
virtio_queue_set_host_notifier_enabled(vq, assign);
|
|
|
|
}
|
|
|
|
|
2018-01-29 15:20:56 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void virtio_bus_cleanup_host_notifier(VirtioBusState *bus, int n)
|
|
|
|
{
|
|
|
|
VirtIODevice *vdev = virtio_bus_get_device(bus);
|
|
|
|
VirtQueue *vq = virtio_get_queue(vdev, n);
|
|
|
|
EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
|
|
|
|
|
2018-01-24 18:09:13 +01:00
|
|
|
/* Test and clear notifier after disabling event,
|
|
|
|
* in case poll callback didn't have time to run.
|
|
|
|
*/
|
|
|
|
virtio_queue_host_notifier_read(notifier);
|
|
|
|
event_notifier_cleanup(notifier);
|
2016-06-10 11:04:09 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 19:06:07 +02:00
|
|
|
static char *virtio_bus_get_dev_path(DeviceState *dev)
|
|
|
|
{
|
|
|
|
BusState *bus = qdev_get_parent_bus(dev);
|
|
|
|
DeviceState *proxy = DEVICE(bus->parent);
|
|
|
|
return qdev_get_dev_path(proxy);
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:56:42 +02:00
|
|
|
static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:48:01 +02:00
|
|
|
bool virtio_bus_device_iommu_enabled(VirtIODevice *vdev)
|
|
|
|
{
|
|
|
|
DeviceState *qdev = DEVICE(vdev);
|
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(qdev));
|
|
|
|
VirtioBusState *bus = VIRTIO_BUS(qbus);
|
|
|
|
VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
|
|
|
|
|
|
|
|
if (!klass->iommu_enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return klass->iommu_enabled(qbus->parent);
|
|
|
|
}
|
|
|
|
|
2013-05-16 19:06:07 +02:00
|
|
|
static void virtio_bus_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
BusClass *bus_class = BUS_CLASS(klass);
|
|
|
|
bus_class->get_dev_path = virtio_bus_get_dev_path;
|
2013-05-29 09:56:42 +02:00
|
|
|
bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
|
2013-05-16 19:06:07 +02:00
|
|
|
}
|
|
|
|
|
2013-01-15 00:08:01 +01:00
|
|
|
static const TypeInfo virtio_bus_info = {
|
|
|
|
.name = TYPE_VIRTIO_BUS,
|
|
|
|
.parent = TYPE_BUS,
|
|
|
|
.instance_size = sizeof(VirtioBusState),
|
|
|
|
.abstract = true,
|
|
|
|
.class_size = sizeof(VirtioBusClass),
|
2013-05-16 19:06:07 +02:00
|
|
|
.class_init = virtio_bus_class_init
|
2013-01-15 00:08:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&virtio_bus_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(virtio_register_types)
|