Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * KVM coalesced MMIO |
| 3 | * |
| 4 | * Copyright (c) 2008 Bull S.A.S. |
| 5 | * |
| 6 | * Author: Laurent Vivier <Laurent.Vivier@bull.net> |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include "iodev.h" |
| 11 | |
| 12 | #include <linux/kvm_host.h> |
| 13 | #include <linux/kvm.h> |
| 14 | |
| 15 | #include "coalesced_mmio.h" |
| 16 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 17 | static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev) |
| 18 | { |
| 19 | return container_of(dev, struct kvm_coalesced_mmio_dev, dev); |
| 20 | } |
| 21 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 22 | static int coalesced_mmio_in_range(struct kvm_io_device *this, |
| 23 | gpa_t addr, int len, int is_write) |
| 24 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 25 | struct kvm_coalesced_mmio_dev *dev = to_mmio(this); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 26 | struct kvm_coalesced_mmio_zone *zone; |
Avi Kivity | 105f8d4 | 2009-06-04 18:09:08 +0300 | [diff] [blame] | 27 | struct kvm_coalesced_mmio_ring *ring; |
| 28 | unsigned avail; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 29 | int i; |
| 30 | |
| 31 | if (!is_write) |
| 32 | return 0; |
| 33 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 34 | /* Are we able to batch it ? */ |
| 35 | |
| 36 | /* last is the first free entry |
| 37 | * check if we don't meet the first used entry |
| 38 | * there is always one unused entry in the buffer |
| 39 | */ |
Avi Kivity | 105f8d4 | 2009-06-04 18:09:08 +0300 | [diff] [blame] | 40 | ring = dev->kvm->coalesced_mmio_ring; |
| 41 | avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX; |
Marcelo Tosatti | 64a2268 | 2009-06-04 15:08:22 -0300 | [diff] [blame^] | 42 | if (avail < KVM_MAX_VCPUS) { |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 43 | /* full */ |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | /* is it in a batchable area ? */ |
| 48 | |
| 49 | for (i = 0; i < dev->nb_zones; i++) { |
| 50 | zone = &dev->zone[i]; |
| 51 | |
| 52 | /* (addr,len) is fully included in |
| 53 | * (zone->addr, zone->size) |
| 54 | */ |
| 55 | |
| 56 | if (zone->addr <= addr && |
| 57 | addr + len <= zone->addr + zone->size) |
| 58 | return 1; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static void coalesced_mmio_write(struct kvm_io_device *this, |
| 64 | gpa_t addr, int len, const void *val) |
| 65 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 66 | struct kvm_coalesced_mmio_dev *dev = to_mmio(this); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 67 | struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring; |
| 68 | |
Marcelo Tosatti | 64a2268 | 2009-06-04 15:08:22 -0300 | [diff] [blame^] | 69 | spin_lock(&dev->lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 70 | |
| 71 | /* copy data in first free entry of the ring */ |
| 72 | |
| 73 | ring->coalesced_mmio[ring->last].phys_addr = addr; |
| 74 | ring->coalesced_mmio[ring->last].len = len; |
| 75 | memcpy(ring->coalesced_mmio[ring->last].data, val, len); |
| 76 | smp_wmb(); |
| 77 | ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; |
Marcelo Tosatti | 64a2268 | 2009-06-04 15:08:22 -0300 | [diff] [blame^] | 78 | spin_unlock(&dev->lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void coalesced_mmio_destructor(struct kvm_io_device *this) |
| 82 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 83 | struct kvm_coalesced_mmio_dev *dev = to_mmio(this); |
Gregory Haskins | 787a660 | 2009-06-01 12:54:45 -0400 | [diff] [blame] | 84 | |
| 85 | kfree(dev); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 88 | static const struct kvm_io_device_ops coalesced_mmio_ops = { |
| 89 | .write = coalesced_mmio_write, |
| 90 | .in_range = coalesced_mmio_in_range, |
| 91 | .destructor = coalesced_mmio_destructor, |
| 92 | }; |
| 93 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 94 | int kvm_coalesced_mmio_init(struct kvm *kvm) |
| 95 | { |
| 96 | struct kvm_coalesced_mmio_dev *dev; |
| 97 | |
| 98 | dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); |
| 99 | if (!dev) |
| 100 | return -ENOMEM; |
Marcelo Tosatti | 64a2268 | 2009-06-04 15:08:22 -0300 | [diff] [blame^] | 101 | spin_lock_init(&dev->lock); |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 102 | kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 103 | dev->kvm = kvm; |
| 104 | kvm->coalesced_mmio_dev = dev; |
| 105 | kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm, |
| 111 | struct kvm_coalesced_mmio_zone *zone) |
| 112 | { |
| 113 | struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; |
| 114 | |
| 115 | if (dev == NULL) |
| 116 | return -EINVAL; |
| 117 | |
| 118 | mutex_lock(&kvm->lock); |
| 119 | if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) { |
| 120 | mutex_unlock(&kvm->lock); |
| 121 | return -ENOBUFS; |
| 122 | } |
| 123 | |
| 124 | dev->zone[dev->nb_zones] = *zone; |
| 125 | dev->nb_zones++; |
| 126 | |
| 127 | mutex_unlock(&kvm->lock); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, |
| 132 | struct kvm_coalesced_mmio_zone *zone) |
| 133 | { |
| 134 | int i; |
| 135 | struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; |
| 136 | struct kvm_coalesced_mmio_zone *z; |
| 137 | |
| 138 | if (dev == NULL) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | mutex_lock(&kvm->lock); |
| 142 | |
| 143 | i = dev->nb_zones; |
| 144 | while(i) { |
| 145 | z = &dev->zone[i - 1]; |
| 146 | |
| 147 | /* unregister all zones |
| 148 | * included in (zone->addr, zone->size) |
| 149 | */ |
| 150 | |
| 151 | if (zone->addr <= z->addr && |
| 152 | z->addr + z->size <= zone->addr + zone->size) { |
| 153 | dev->nb_zones--; |
| 154 | *z = dev->zone[dev->nb_zones]; |
| 155 | } |
| 156 | i--; |
| 157 | } |
| 158 | |
| 159 | mutex_unlock(&kvm->lock); |
| 160 | |
| 161 | return 0; |
| 162 | } |