diff options
author | Gregory Haskins <ghaskins@novell.com> | 2009-06-01 20:54:50 +0400 |
---|---|---|
committer | Avi Kivity <avi@redhat.com> | 2009-09-10 09:32:45 +0400 |
commit | d76685c4a074041ed168e0b04dd604c3df5dcaa5 (patch) | |
tree | 828fb3a57b7829530904318a0ad9d006e21408ad /virt/kvm | |
parent | 787a660a4f03325a0e00493ac39017e53fd345fa (diff) | |
download | linux-d76685c4a074041ed168e0b04dd604c3df5dcaa5.tar.xz |
KVM: cleanup io_device code
We modernize the io_device code so that we use container_of() instead of
dev->private, and move the vtable to a separate ops structure
(theoretically allows better caching for multiple instances of the same
ops structure)
Signed-off-by: Gregory Haskins <ghaskins@novell.com>
Acked-by: Chris Wright <chrisw@sous-sol.org>
Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'virt/kvm')
-rw-r--r-- | virt/kvm/coalesced_mmio.c | 25 | ||||
-rw-r--r-- | virt/kvm/ioapic.c | 22 | ||||
-rw-r--r-- | virt/kvm/iodev.h | 29 | ||||
-rw-r--r-- | virt/kvm/kvm_main.c | 2 |
4 files changed, 51 insertions, 27 deletions
diff --git a/virt/kvm/coalesced_mmio.c b/virt/kvm/coalesced_mmio.c index 03ea2807b1f2..c4c7ec2f9d30 100644 --- a/virt/kvm/coalesced_mmio.c +++ b/virt/kvm/coalesced_mmio.c @@ -14,11 +14,15 @@ #include "coalesced_mmio.h" +static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev) +{ + return container_of(dev, struct kvm_coalesced_mmio_dev, dev); +} + static int coalesced_mmio_in_range(struct kvm_io_device *this, gpa_t addr, int len, int is_write) { - struct kvm_coalesced_mmio_dev *dev = - (struct kvm_coalesced_mmio_dev*)this->private; + struct kvm_coalesced_mmio_dev *dev = to_mmio(this); struct kvm_coalesced_mmio_zone *zone; int next; int i; @@ -63,8 +67,7 @@ static int coalesced_mmio_in_range(struct kvm_io_device *this, static void coalesced_mmio_write(struct kvm_io_device *this, gpa_t addr, int len, const void *val) { - struct kvm_coalesced_mmio_dev *dev = - (struct kvm_coalesced_mmio_dev*)this->private; + struct kvm_coalesced_mmio_dev *dev = to_mmio(this); struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring; /* kvm->lock must be taken by caller before call to in_range()*/ @@ -80,12 +83,17 @@ static void coalesced_mmio_write(struct kvm_io_device *this, static void coalesced_mmio_destructor(struct kvm_io_device *this) { - struct kvm_coalesced_mmio_dev *dev = - (struct kvm_coalesced_mmio_dev *)this->private; + struct kvm_coalesced_mmio_dev *dev = to_mmio(this); kfree(dev); } +static const struct kvm_io_device_ops coalesced_mmio_ops = { + .write = coalesced_mmio_write, + .in_range = coalesced_mmio_in_range, + .destructor = coalesced_mmio_destructor, +}; + int kvm_coalesced_mmio_init(struct kvm *kvm) { struct kvm_coalesced_mmio_dev *dev; @@ -93,10 +101,7 @@ int kvm_coalesced_mmio_init(struct kvm *kvm) dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); if (!dev) return -ENOMEM; - dev->dev.write = coalesced_mmio_write; - dev->dev.in_range = coalesced_mmio_in_range; - dev->dev.destructor = coalesced_mmio_destructor; - dev->dev.private = dev; + kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops); dev->kvm = kvm; kvm->coalesced_mmio_dev = dev; kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev); diff --git a/virt/kvm/ioapic.c b/virt/kvm/ioapic.c index 1150c6d5c7b8..469b9faae665 100644 --- a/virt/kvm/ioapic.c +++ b/virt/kvm/ioapic.c @@ -220,10 +220,15 @@ void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode) __kvm_ioapic_update_eoi(ioapic, i, trigger_mode); } +static inline struct kvm_ioapic *to_ioapic(struct kvm_io_device *dev) +{ + return container_of(dev, struct kvm_ioapic, dev); +} + static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr, int len, int is_write) { - struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private; + struct kvm_ioapic *ioapic = to_ioapic(this); return ((addr >= ioapic->base_address && (addr < ioapic->base_address + IOAPIC_MEM_LENGTH))); @@ -232,7 +237,7 @@ static int ioapic_in_range(struct kvm_io_device *this, gpa_t addr, static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len, void *val) { - struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private; + struct kvm_ioapic *ioapic = to_ioapic(this); u32 result; ioapic_debug("addr %lx\n", (unsigned long)addr); @@ -269,7 +274,7 @@ static void ioapic_mmio_read(struct kvm_io_device *this, gpa_t addr, int len, static void ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len, const void *val) { - struct kvm_ioapic *ioapic = (struct kvm_ioapic *)this->private; + struct kvm_ioapic *ioapic = to_ioapic(this); u32 data; ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n", @@ -314,6 +319,12 @@ void kvm_ioapic_reset(struct kvm_ioapic *ioapic) ioapic->id = 0; } +static const struct kvm_io_device_ops ioapic_mmio_ops = { + .read = ioapic_mmio_read, + .write = ioapic_mmio_write, + .in_range = ioapic_in_range, +}; + int kvm_ioapic_init(struct kvm *kvm) { struct kvm_ioapic *ioapic; @@ -323,10 +334,7 @@ int kvm_ioapic_init(struct kvm *kvm) return -ENOMEM; kvm->arch.vioapic = ioapic; kvm_ioapic_reset(ioapic); - ioapic->dev.read = ioapic_mmio_read; - ioapic->dev.write = ioapic_mmio_write; - ioapic->dev.in_range = ioapic_in_range; - ioapic->dev.private = ioapic; + kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops); ioapic->kvm = kvm; kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev); return 0; diff --git a/virt/kvm/iodev.h b/virt/kvm/iodev.h index 55e8846ac3a6..2c67f5acd6db 100644 --- a/virt/kvm/iodev.h +++ b/virt/kvm/iodev.h @@ -18,7 +18,9 @@ #include <linux/kvm_types.h> -struct kvm_io_device { +struct kvm_io_device; + +struct kvm_io_device_ops { void (*read)(struct kvm_io_device *this, gpa_t addr, int len, @@ -30,16 +32,25 @@ struct kvm_io_device { int (*in_range)(struct kvm_io_device *this, gpa_t addr, int len, int is_write); void (*destructor)(struct kvm_io_device *this); +}; + - void *private; +struct kvm_io_device { + const struct kvm_io_device_ops *ops; }; +static inline void kvm_iodevice_init(struct kvm_io_device *dev, + const struct kvm_io_device_ops *ops) +{ + dev->ops = ops; +} + static inline void kvm_iodevice_read(struct kvm_io_device *dev, gpa_t addr, int len, void *val) { - dev->read(dev, addr, len, val); + dev->ops->read(dev, addr, len, val); } static inline void kvm_iodevice_write(struct kvm_io_device *dev, @@ -47,19 +58,19 @@ static inline void kvm_iodevice_write(struct kvm_io_device *dev, int len, const void *val) { - dev->write(dev, addr, len, val); + dev->ops->write(dev, addr, len, val); } -static inline int kvm_iodevice_inrange(struct kvm_io_device *dev, - gpa_t addr, int len, int is_write) +static inline int kvm_iodevice_in_range(struct kvm_io_device *dev, + gpa_t addr, int len, int is_write) { - return dev->in_range(dev, addr, len, is_write); + return dev->ops->in_range(dev, addr, len, is_write); } static inline void kvm_iodevice_destructor(struct kvm_io_device *dev) { - if (dev->destructor) - dev->destructor(dev); + if (dev->ops->destructor) + dev->ops->destructor(dev); } #endif /* __KVM_IODEV_H__ */ diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 2d9bc439d9a9..d1f9c87daa87 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2463,7 +2463,7 @@ struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, for (i = 0; i < bus->dev_count; i++) { struct kvm_io_device *pos = bus->devs[i]; - if (pos->in_range(pos, addr, len, is_write)) + if (kvm_iodevice_in_range(pos, addr, len, is_write)) return pos; } |