15f94c174SLaurent Vivier /* 25f94c174SLaurent Vivier * KVM coalesced MMIO 35f94c174SLaurent Vivier * 45f94c174SLaurent Vivier * Copyright (c) 2008 Bull S.A.S. 5221d059dSAvi Kivity * Copyright 2009 Red Hat, Inc. and/or its affiliates. 65f94c174SLaurent Vivier * 75f94c174SLaurent Vivier * Author: Laurent Vivier <Laurent.Vivier@bull.net> 85f94c174SLaurent Vivier * 95f94c174SLaurent Vivier */ 105f94c174SLaurent Vivier 115f94c174SLaurent Vivier #include "iodev.h" 125f94c174SLaurent Vivier 135f94c174SLaurent Vivier #include <linux/kvm_host.h> 145a0e3ad6STejun Heo #include <linux/slab.h> 155f94c174SLaurent Vivier #include <linux/kvm.h> 165f94c174SLaurent Vivier 175f94c174SLaurent Vivier #include "coalesced_mmio.h" 185f94c174SLaurent Vivier 19d76685c4SGregory Haskins static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev) 20d76685c4SGregory Haskins { 21d76685c4SGregory Haskins return container_of(dev, struct kvm_coalesced_mmio_dev, dev); 22d76685c4SGregory Haskins } 23d76685c4SGregory Haskins 24bda9020eSMichael S. Tsirkin static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev, 25bda9020eSMichael S. Tsirkin gpa_t addr, int len) 265f94c174SLaurent Vivier { 272b3c246aSSasha Levin /* is it in a batchable area ? 282b3c246aSSasha Levin * (addr,len) is fully included in 295f94c174SLaurent Vivier * (zone->addr, zone->size) 305f94c174SLaurent Vivier */ 311a214246SDan Carpenter if (len < 0) 321a214246SDan Carpenter return 0; 331a214246SDan Carpenter if (addr + len < addr) 341a214246SDan Carpenter return 0; 351a214246SDan Carpenter if (addr < dev->zone.addr) 361a214246SDan Carpenter return 0; 371a214246SDan Carpenter if (addr + len > dev->zone.addr + dev->zone.size) 381a214246SDan Carpenter return 0; 391a214246SDan Carpenter return 1; 405f94c174SLaurent Vivier } 415f94c174SLaurent Vivier 42c298125fSSasha Levin static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev) 43c298125fSSasha Levin { 44c298125fSSasha Levin struct kvm_coalesced_mmio_ring *ring; 45c298125fSSasha Levin unsigned avail; 46c298125fSSasha Levin 47c298125fSSasha Levin /* Are we able to batch it ? */ 48c298125fSSasha Levin 49c298125fSSasha Levin /* last is the first free entry 50c298125fSSasha Levin * check if we don't meet the first used entry 51c298125fSSasha Levin * there is always one unused entry in the buffer 52c298125fSSasha Levin */ 53c298125fSSasha Levin ring = dev->kvm->coalesced_mmio_ring; 54c298125fSSasha Levin avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX; 55c298125fSSasha Levin if (avail == 0) { 56c298125fSSasha Levin /* full */ 57c298125fSSasha Levin return 0; 58c298125fSSasha Levin } 59c298125fSSasha Levin 60c298125fSSasha Levin return 1; 61c298125fSSasha Levin } 62c298125fSSasha Levin 63*e32edf4fSNikolay Nikolaev static int coalesced_mmio_write(struct kvm_vcpu *vcpu, 64*e32edf4fSNikolay Nikolaev struct kvm_io_device *this, gpa_t addr, 65*e32edf4fSNikolay Nikolaev int len, const void *val) 665f94c174SLaurent Vivier { 67d76685c4SGregory Haskins struct kvm_coalesced_mmio_dev *dev = to_mmio(this); 685f94c174SLaurent Vivier struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring; 69c298125fSSasha Levin 70bda9020eSMichael S. Tsirkin if (!coalesced_mmio_in_range(dev, addr, len)) 71bda9020eSMichael S. Tsirkin return -EOPNOTSUPP; 725f94c174SLaurent Vivier 732b3c246aSSasha Levin spin_lock(&dev->kvm->ring_lock); 745f94c174SLaurent Vivier 75c298125fSSasha Levin if (!coalesced_mmio_has_room(dev)) { 762b3c246aSSasha Levin spin_unlock(&dev->kvm->ring_lock); 77c298125fSSasha Levin return -EOPNOTSUPP; 78c298125fSSasha Levin } 79c298125fSSasha Levin 805f94c174SLaurent Vivier /* copy data in first free entry of the ring */ 815f94c174SLaurent Vivier 825f94c174SLaurent Vivier ring->coalesced_mmio[ring->last].phys_addr = addr; 835f94c174SLaurent Vivier ring->coalesced_mmio[ring->last].len = len; 845f94c174SLaurent Vivier memcpy(ring->coalesced_mmio[ring->last].data, val, len); 855f94c174SLaurent Vivier smp_wmb(); 865f94c174SLaurent Vivier ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; 872b3c246aSSasha Levin spin_unlock(&dev->kvm->ring_lock); 88bda9020eSMichael S. Tsirkin return 0; 895f94c174SLaurent Vivier } 905f94c174SLaurent Vivier 915f94c174SLaurent Vivier static void coalesced_mmio_destructor(struct kvm_io_device *this) 925f94c174SLaurent Vivier { 93d76685c4SGregory Haskins struct kvm_coalesced_mmio_dev *dev = to_mmio(this); 94787a660aSGregory Haskins 952b3c246aSSasha Levin list_del(&dev->list); 962b3c246aSSasha Levin 97787a660aSGregory Haskins kfree(dev); 985f94c174SLaurent Vivier } 995f94c174SLaurent Vivier 100d76685c4SGregory Haskins static const struct kvm_io_device_ops coalesced_mmio_ops = { 101d76685c4SGregory Haskins .write = coalesced_mmio_write, 102d76685c4SGregory Haskins .destructor = coalesced_mmio_destructor, 103d76685c4SGregory Haskins }; 104d76685c4SGregory Haskins 1055f94c174SLaurent Vivier int kvm_coalesced_mmio_init(struct kvm *kvm) 1065f94c174SLaurent Vivier { 107980da6ceSAvi Kivity struct page *page; 108090b7affSGregory Haskins int ret; 1095f94c174SLaurent Vivier 110980da6ceSAvi Kivity ret = -ENOMEM; 111980da6ceSAvi Kivity page = alloc_page(GFP_KERNEL | __GFP_ZERO); 112980da6ceSAvi Kivity if (!page) 113980da6ceSAvi Kivity goto out_err; 1142b3c246aSSasha Levin 1152b3c246aSSasha Levin ret = 0; 116980da6ceSAvi Kivity kvm->coalesced_mmio_ring = page_address(page); 117980da6ceSAvi Kivity 1182b3c246aSSasha Levin /* 1192b3c246aSSasha Levin * We're using this spinlock to sync access to the coalesced ring. 1202b3c246aSSasha Levin * The list doesn't need it's own lock since device registration and 1212b3c246aSSasha Levin * unregistration should only happen when kvm->slots_lock is held. 1222b3c246aSSasha Levin */ 1232b3c246aSSasha Levin spin_lock_init(&kvm->ring_lock); 1242b3c246aSSasha Levin INIT_LIST_HEAD(&kvm->coalesced_zones); 1255f94c174SLaurent Vivier 126980da6ceSAvi Kivity out_err: 127980da6ceSAvi Kivity return ret; 128980da6ceSAvi Kivity } 129980da6ceSAvi Kivity 130980da6ceSAvi Kivity void kvm_coalesced_mmio_free(struct kvm *kvm) 131980da6ceSAvi Kivity { 132980da6ceSAvi Kivity if (kvm->coalesced_mmio_ring) 133980da6ceSAvi Kivity free_page((unsigned long)kvm->coalesced_mmio_ring); 1345f94c174SLaurent Vivier } 1355f94c174SLaurent Vivier 1365f94c174SLaurent Vivier int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm, 1375f94c174SLaurent Vivier struct kvm_coalesced_mmio_zone *zone) 1385f94c174SLaurent Vivier { 1392b3c246aSSasha Levin int ret; 1402b3c246aSSasha Levin struct kvm_coalesced_mmio_dev *dev; 1412b3c246aSSasha Levin 1422b3c246aSSasha Levin dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); 1432b3c246aSSasha Levin if (!dev) 1442b3c246aSSasha Levin return -ENOMEM; 1452b3c246aSSasha Levin 1462b3c246aSSasha Levin kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops); 1472b3c246aSSasha Levin dev->kvm = kvm; 1482b3c246aSSasha Levin dev->zone = *zone; 1492b3c246aSSasha Levin 1502b3c246aSSasha Levin mutex_lock(&kvm->slots_lock); 151743eeb0bSSasha Levin ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, zone->addr, 152743eeb0bSSasha Levin zone->size, &dev->dev); 1532b3c246aSSasha Levin if (ret < 0) 1542b3c246aSSasha Levin goto out_free_dev; 1552b3c246aSSasha Levin list_add_tail(&dev->list, &kvm->coalesced_zones); 1562b3c246aSSasha Levin mutex_unlock(&kvm->slots_lock); 1572b3c246aSSasha Levin 158aac5c422SDan Carpenter return 0; 1592b3c246aSSasha Levin 1602b3c246aSSasha Levin out_free_dev: 1612b3c246aSSasha Levin mutex_unlock(&kvm->slots_lock); 1622b3c246aSSasha Levin kfree(dev); 1635f94c174SLaurent Vivier 164aac5c422SDan Carpenter return ret; 1655f94c174SLaurent Vivier } 1665f94c174SLaurent Vivier 1675f94c174SLaurent Vivier int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, 1685f94c174SLaurent Vivier struct kvm_coalesced_mmio_zone *zone) 1695f94c174SLaurent Vivier { 1702b3c246aSSasha Levin struct kvm_coalesced_mmio_dev *dev, *tmp; 1715f94c174SLaurent Vivier 17279fac95eSMarcelo Tosatti mutex_lock(&kvm->slots_lock); 1735f94c174SLaurent Vivier 1742b3c246aSSasha Levin list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list) 1752b3c246aSSasha Levin if (coalesced_mmio_in_range(dev, zone->addr, zone->size)) { 1762b3c246aSSasha Levin kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &dev->dev); 1772b3c246aSSasha Levin kvm_iodevice_destructor(&dev->dev); 1785f94c174SLaurent Vivier } 1795f94c174SLaurent Vivier 18079fac95eSMarcelo Tosatti mutex_unlock(&kvm->slots_lock); 1815f94c174SLaurent Vivier 1825f94c174SLaurent Vivier return 0; 1835f94c174SLaurent Vivier } 184