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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 14 | #include <linux/kvm.h> |
| 15 | |
| 16 | #include "coalesced_mmio.h" |
| 17 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 18 | static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev) |
| 19 | { |
| 20 | return container_of(dev, struct kvm_coalesced_mmio_dev, dev); |
| 21 | } |
| 22 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 23 | static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev, |
| 24 | gpa_t addr, int len) |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 25 | { |
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 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 31 | /* Are we able to batch it ? */ |
| 32 | |
| 33 | /* last is the first free entry |
| 34 | * check if we don't meet the first used entry |
| 35 | * there is always one unused entry in the buffer |
| 36 | */ |
Avi Kivity | 105f8d4 | 2009-06-04 18:09:08 +0300 | [diff] [blame] | 37 | ring = dev->kvm->coalesced_mmio_ring; |
| 38 | avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX; |
Marcelo Tosatti | 64a2268 | 2009-06-04 15:08:22 -0300 | [diff] [blame] | 39 | if (avail < KVM_MAX_VCPUS) { |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 40 | /* full */ |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | /* is it in a batchable area ? */ |
| 45 | |
| 46 | for (i = 0; i < dev->nb_zones; i++) { |
| 47 | zone = &dev->zone[i]; |
| 48 | |
| 49 | /* (addr,len) is fully included in |
| 50 | * (zone->addr, zone->size) |
| 51 | */ |
| 52 | |
| 53 | if (zone->addr <= addr && |
| 54 | addr + len <= zone->addr + zone->size) |
| 55 | return 1; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 60 | static int coalesced_mmio_write(struct kvm_io_device *this, |
| 61 | gpa_t addr, int len, const void *val) |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 62 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 63 | struct kvm_coalesced_mmio_dev *dev = to_mmio(this); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 64 | struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 65 | if (!coalesced_mmio_in_range(dev, addr, len)) |
| 66 | return -EOPNOTSUPP; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 67 | |
Marcelo Tosatti | 64a2268 | 2009-06-04 15:08:22 -0300 | [diff] [blame] | 68 | spin_lock(&dev->lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 69 | |
| 70 | /* copy data in first free entry of the ring */ |
| 71 | |
| 72 | ring->coalesced_mmio[ring->last].phys_addr = addr; |
| 73 | ring->coalesced_mmio[ring->last].len = len; |
| 74 | memcpy(ring->coalesced_mmio[ring->last].data, val, len); |
| 75 | smp_wmb(); |
| 76 | ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; |
Marcelo Tosatti | 64a2268 | 2009-06-04 15:08:22 -0300 | [diff] [blame] | 77 | spin_unlock(&dev->lock); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 78 | return 0; |
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, |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 90 | .destructor = coalesced_mmio_destructor, |
| 91 | }; |
| 92 | |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 93 | int kvm_coalesced_mmio_init(struct kvm *kvm) |
| 94 | { |
| 95 | struct kvm_coalesced_mmio_dev *dev; |
Avi Kivity | 980da6c | 2009-12-20 15:13:43 +0200 | [diff] [blame] | 96 | struct page *page; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 97 | int ret; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 98 | |
Avi Kivity | 980da6c | 2009-12-20 15:13:43 +0200 | [diff] [blame] | 99 | ret = -ENOMEM; |
| 100 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 101 | if (!page) |
| 102 | goto out_err; |
| 103 | kvm->coalesced_mmio_ring = page_address(page); |
| 104 | |
| 105 | ret = -ENOMEM; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 106 | dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); |
| 107 | if (!dev) |
Avi Kivity | 980da6c | 2009-12-20 15:13:43 +0200 | [diff] [blame] | 108 | goto out_free_page; |
Marcelo Tosatti | 64a2268 | 2009-06-04 15:08:22 -0300 | [diff] [blame] | 109 | spin_lock_init(&dev->lock); |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 110 | kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 111 | dev->kvm = kvm; |
| 112 | kvm->coalesced_mmio_dev = dev; |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 113 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 114 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 115 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 116 | mutex_unlock(&kvm->slots_lock); |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 117 | if (ret < 0) |
Avi Kivity | 980da6c | 2009-12-20 15:13:43 +0200 | [diff] [blame] | 118 | goto out_free_dev; |
Gregory Haskins | 090b7af | 2009-07-07 17:08:44 -0400 | [diff] [blame] | 119 | |
| 120 | return ret; |
Avi Kivity | 980da6c | 2009-12-20 15:13:43 +0200 | [diff] [blame] | 121 | |
| 122 | out_free_dev: |
| 123 | kfree(dev); |
| 124 | out_free_page: |
| 125 | __free_page(page); |
| 126 | out_err: |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | void kvm_coalesced_mmio_free(struct kvm *kvm) |
| 131 | { |
| 132 | if (kvm->coalesced_mmio_ring) |
| 133 | free_page((unsigned long)kvm->coalesced_mmio_ring); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm, |
Jochen Maes | 43db669 | 2010-02-08 11:29:33 +0100 | [diff] [blame] | 137 | struct kvm_coalesced_mmio_zone *zone) |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 138 | { |
| 139 | struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; |
| 140 | |
| 141 | if (dev == NULL) |
| 142 | return -EINVAL; |
| 143 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 144 | mutex_lock(&kvm->slots_lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 145 | if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) { |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 146 | mutex_unlock(&kvm->slots_lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 147 | return -ENOBUFS; |
| 148 | } |
| 149 | |
| 150 | dev->zone[dev->nb_zones] = *zone; |
| 151 | dev->nb_zones++; |
| 152 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 153 | mutex_unlock(&kvm->slots_lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, |
| 158 | struct kvm_coalesced_mmio_zone *zone) |
| 159 | { |
| 160 | int i; |
| 161 | struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; |
| 162 | struct kvm_coalesced_mmio_zone *z; |
| 163 | |
| 164 | if (dev == NULL) |
| 165 | return -EINVAL; |
| 166 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 167 | mutex_lock(&kvm->slots_lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 168 | |
| 169 | i = dev->nb_zones; |
Jochen Maes | 43db669 | 2010-02-08 11:29:33 +0100 | [diff] [blame] | 170 | while (i) { |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 171 | z = &dev->zone[i - 1]; |
| 172 | |
| 173 | /* unregister all zones |
| 174 | * included in (zone->addr, zone->size) |
| 175 | */ |
| 176 | |
| 177 | if (zone->addr <= z->addr && |
| 178 | z->addr + z->size <= zone->addr + zone->size) { |
| 179 | dev->nb_zones--; |
| 180 | *z = dev->zone[dev->nb_zones]; |
| 181 | } |
| 182 | i--; |
| 183 | } |
| 184 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 185 | mutex_unlock(&kvm->slots_lock); |
Laurent Vivier | 5f94c17 | 2008-05-30 16:05:54 +0200 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |