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