mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-08 18:23:57 -06:00
hw: move target-independent files to subdirectories
This patch tackles all files that are compiled once, moving them to subdirectories of hw/. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
ce3b494cb5
commit
49ab747f66
227 changed files with 205 additions and 208 deletions
|
@ -0,0 +1,4 @@
|
|||
common-obj-$(CONFIG_VIRTIO) += virtio-rng.o
|
||||
common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
|
||||
common-obj-$(CONFIG_VIRTIO) += virtio-bus.o
|
||||
|
164
hw/virtio/virtio-bus.c
Normal file
164
hw/virtio/virtio-bus.c
Normal file
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "hw/hw.h"
|
||||
#include "qemu/error-report.h"
|
||||
#include "hw/qdev.h"
|
||||
#include "hw/virtio/virtio-bus.h"
|
||||
#include "hw/virtio/virtio.h"
|
||||
|
||||
/* #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
|
||||
|
||||
/* Plug the VirtIODevice */
|
||||
int virtio_bus_plug_device(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);
|
||||
DPRINTF("%s: plug device.\n", qbus->name);
|
||||
|
||||
bus->vdev = vdev;
|
||||
|
||||
/*
|
||||
* The lines below will disappear when we drop VirtIOBindings, at the end
|
||||
* of the series.
|
||||
*/
|
||||
bus->bindings.notify = klass->notify;
|
||||
bus->bindings.save_config = klass->save_config;
|
||||
bus->bindings.save_queue = klass->save_queue;
|
||||
bus->bindings.load_config = klass->load_config;
|
||||
bus->bindings.load_queue = klass->load_queue;
|
||||
bus->bindings.load_done = klass->load_done;
|
||||
bus->bindings.get_features = klass->get_features;
|
||||
bus->bindings.query_guest_notifiers = klass->query_guest_notifiers;
|
||||
bus->bindings.set_guest_notifiers = klass->set_guest_notifiers;
|
||||
bus->bindings.set_host_notifier = klass->set_host_notifier;
|
||||
bus->bindings.vmstate_change = klass->vmstate_change;
|
||||
virtio_bind_device(bus->vdev, &bus->bindings, qbus->parent);
|
||||
|
||||
if (klass->device_plugged != NULL) {
|
||||
klass->device_plugged(qbus->parent);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Reset the virtio_bus */
|
||||
void virtio_bus_reset(VirtioBusState *bus)
|
||||
{
|
||||
DPRINTF("%s: reset device.\n", qbus->name);
|
||||
if (bus->vdev != NULL) {
|
||||
virtio_reset(bus->vdev);
|
||||
}
|
||||
}
|
||||
|
||||
/* Destroy the VirtIODevice */
|
||||
void virtio_bus_destroy_device(VirtioBusState *bus)
|
||||
{
|
||||
DeviceState *qdev;
|
||||
BusState *qbus = BUS(bus);
|
||||
VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
|
||||
DPRINTF("%s: remove device.\n", qbus->name);
|
||||
|
||||
if (bus->vdev != NULL) {
|
||||
if (klass->device_unplug != NULL) {
|
||||
klass->device_unplug(qbus->parent);
|
||||
}
|
||||
qdev = DEVICE(bus->vdev);
|
||||
qdev_free(qdev);
|
||||
bus->vdev = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the device id of the plugged device. */
|
||||
uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
|
||||
{
|
||||
assert(bus->vdev != NULL);
|
||||
return bus->vdev->device_id;
|
||||
}
|
||||
|
||||
/* Get the config_len field of the plugged device. */
|
||||
size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
|
||||
{
|
||||
assert(bus->vdev != NULL);
|
||||
return bus->vdev->config_len;
|
||||
}
|
||||
|
||||
/* Get the features of the plugged device. */
|
||||
uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus,
|
||||
uint32_t requested_features)
|
||||
{
|
||||
VirtioDeviceClass *k;
|
||||
assert(bus->vdev != NULL);
|
||||
k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
|
||||
assert(k->get_features != NULL);
|
||||
return k->get_features(bus->vdev, requested_features);
|
||||
}
|
||||
|
||||
/* Get bad features of the plugged device. */
|
||||
uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
|
||||
{
|
||||
VirtioDeviceClass *k;
|
||||
assert(bus->vdev != NULL);
|
||||
k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
|
||||
if (k->bad_features != NULL) {
|
||||
return k->bad_features(bus->vdev);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get config of the plugged device. */
|
||||
void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
|
||||
{
|
||||
VirtioDeviceClass *k;
|
||||
assert(bus->vdev != NULL);
|
||||
k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
|
||||
if (k->get_config != NULL) {
|
||||
k->get_config(bus->vdev, config);
|
||||
}
|
||||
}
|
||||
|
||||
static const TypeInfo virtio_bus_info = {
|
||||
.name = TYPE_VIRTIO_BUS,
|
||||
.parent = TYPE_BUS,
|
||||
.instance_size = sizeof(VirtioBusState),
|
||||
.abstract = true,
|
||||
.class_size = sizeof(VirtioBusClass),
|
||||
};
|
||||
|
||||
static void virtio_register_types(void)
|
||||
{
|
||||
type_register_static(&virtio_bus_info);
|
||||
}
|
||||
|
||||
type_init(virtio_register_types)
|
1514
hw/virtio/virtio-pci.c
Normal file
1514
hw/virtio/virtio-pci.c
Normal file
File diff suppressed because it is too large
Load diff
187
hw/virtio/virtio-rng.c
Normal file
187
hw/virtio/virtio-rng.c
Normal file
|
@ -0,0 +1,187 @@
|
|||
/*
|
||||
* A virtio device implementing a hardware random number generator.
|
||||
*
|
||||
* Copyright 2012 Red Hat, Inc.
|
||||
* Copyright 2012 Amit Shah <amit.shah@redhat.com>
|
||||
*
|
||||
* This work is licensed under the terms of the GNU GPL, version 2 or
|
||||
* (at your option) any later version. See the COPYING file in the
|
||||
* top-level directory.
|
||||
*/
|
||||
|
||||
#include "qemu/iov.h"
|
||||
#include "hw/qdev.h"
|
||||
#include "qapi/qmp/qerror.h"
|
||||
#include "hw/virtio/virtio.h"
|
||||
#include "hw/virtio/virtio-rng.h"
|
||||
#include "qemu/rng.h"
|
||||
|
||||
static bool is_guest_ready(VirtIORNG *vrng)
|
||||
{
|
||||
if (virtio_queue_ready(vrng->vq)
|
||||
&& (vrng->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static size_t get_request_size(VirtQueue *vq, unsigned quota)
|
||||
{
|
||||
unsigned int in, out;
|
||||
|
||||
virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
|
||||
return in;
|
||||
}
|
||||
|
||||
static void virtio_rng_process(VirtIORNG *vrng);
|
||||
|
||||
/* Send data from a char device over to the guest */
|
||||
static void chr_read(void *opaque, const void *buf, size_t size)
|
||||
{
|
||||
VirtIORNG *vrng = opaque;
|
||||
VirtQueueElement elem;
|
||||
size_t len;
|
||||
int offset;
|
||||
|
||||
if (!is_guest_ready(vrng)) {
|
||||
return;
|
||||
}
|
||||
|
||||
vrng->quota_remaining -= size;
|
||||
|
||||
offset = 0;
|
||||
while (offset < size) {
|
||||
if (!virtqueue_pop(vrng->vq, &elem)) {
|
||||
break;
|
||||
}
|
||||
len = iov_from_buf(elem.in_sg, elem.in_num,
|
||||
0, buf + offset, size - offset);
|
||||
offset += len;
|
||||
|
||||
virtqueue_push(vrng->vq, &elem, len);
|
||||
}
|
||||
virtio_notify(&vrng->vdev, vrng->vq);
|
||||
}
|
||||
|
||||
static void virtio_rng_process(VirtIORNG *vrng)
|
||||
{
|
||||
size_t size;
|
||||
unsigned quota;
|
||||
|
||||
if (!is_guest_ready(vrng)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (vrng->quota_remaining < 0) {
|
||||
quota = 0;
|
||||
} else {
|
||||
quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
|
||||
}
|
||||
size = get_request_size(vrng->vq, quota);
|
||||
size = MIN(vrng->quota_remaining, size);
|
||||
if (size) {
|
||||
rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
|
||||
{
|
||||
VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
|
||||
virtio_rng_process(vrng);
|
||||
}
|
||||
|
||||
static uint32_t get_features(VirtIODevice *vdev, uint32_t f)
|
||||
{
|
||||
return f;
|
||||
}
|
||||
|
||||
static void virtio_rng_save(QEMUFile *f, void *opaque)
|
||||
{
|
||||
VirtIORNG *vrng = opaque;
|
||||
|
||||
virtio_save(&vrng->vdev, f);
|
||||
}
|
||||
|
||||
static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id)
|
||||
{
|
||||
VirtIORNG *vrng = opaque;
|
||||
|
||||
if (version_id != 1) {
|
||||
return -EINVAL;
|
||||
}
|
||||
virtio_load(&vrng->vdev, f);
|
||||
|
||||
/* We may have an element ready but couldn't process it due to a quota
|
||||
* limit. Make sure to try again after live migration when the quota may
|
||||
* have been reset.
|
||||
*/
|
||||
virtio_rng_process(vrng);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void check_rate_limit(void *opaque)
|
||||
{
|
||||
VirtIORNG *s = opaque;
|
||||
|
||||
s->quota_remaining = s->conf->max_bytes;
|
||||
virtio_rng_process(s);
|
||||
qemu_mod_timer(s->rate_limit_timer,
|
||||
qemu_get_clock_ms(vm_clock) + s->conf->period_ms);
|
||||
}
|
||||
|
||||
|
||||
VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf)
|
||||
{
|
||||
VirtIORNG *vrng;
|
||||
VirtIODevice *vdev;
|
||||
Error *local_err = NULL;
|
||||
|
||||
vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0,
|
||||
sizeof(VirtIORNG));
|
||||
|
||||
vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
|
||||
|
||||
vrng->rng = conf->rng;
|
||||
if (vrng->rng == NULL) {
|
||||
qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rng_backend_open(vrng->rng, &local_err);
|
||||
if (local_err) {
|
||||
qerror_report_err(local_err);
|
||||
error_free(local_err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
vrng->vq = virtio_add_queue(vdev, 8, handle_input);
|
||||
vrng->vdev.get_features = get_features;
|
||||
|
||||
vrng->qdev = dev;
|
||||
vrng->conf = conf;
|
||||
|
||||
assert(vrng->conf->max_bytes <= INT64_MAX);
|
||||
vrng->quota_remaining = vrng->conf->max_bytes;
|
||||
|
||||
vrng->rate_limit_timer = qemu_new_timer_ms(vm_clock,
|
||||
check_rate_limit, vrng);
|
||||
|
||||
qemu_mod_timer(vrng->rate_limit_timer,
|
||||
qemu_get_clock_ms(vm_clock) + vrng->conf->period_ms);
|
||||
|
||||
register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
|
||||
virtio_rng_load, vrng);
|
||||
|
||||
return vdev;
|
||||
}
|
||||
|
||||
void virtio_rng_exit(VirtIODevice *vdev)
|
||||
{
|
||||
VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
|
||||
|
||||
qemu_del_timer(vrng->rate_limit_timer);
|
||||
qemu_free_timer(vrng->rate_limit_timer);
|
||||
unregister_savevm(vrng->qdev, "virtio-rng", vrng);
|
||||
virtio_cleanup(vdev);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue