xref: /openbmc/linux/virt/kvm/coalesced_mmio.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
15f94c174SLaurent Vivier /*
25f94c174SLaurent Vivier  * KVM coalesced MMIO
35f94c174SLaurent Vivier  *
45f94c174SLaurent Vivier  * Copyright (c) 2008 Bull S.A.S.
55f94c174SLaurent Vivier  *
65f94c174SLaurent Vivier  *  Author: Laurent Vivier <Laurent.Vivier@bull.net>
75f94c174SLaurent Vivier  *
85f94c174SLaurent Vivier  */
95f94c174SLaurent Vivier 
105f94c174SLaurent Vivier #include "iodev.h"
115f94c174SLaurent Vivier 
125f94c174SLaurent Vivier #include <linux/kvm_host.h>
13*5a0e3ad6STejun Heo #include <linux/slab.h>
145f94c174SLaurent Vivier #include <linux/kvm.h>
155f94c174SLaurent Vivier 
165f94c174SLaurent Vivier #include "coalesced_mmio.h"
175f94c174SLaurent Vivier 
18d76685c4SGregory Haskins static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
19d76685c4SGregory Haskins {
20d76685c4SGregory Haskins 	return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
21d76685c4SGregory Haskins }
22d76685c4SGregory Haskins 
23bda9020eSMichael S. Tsirkin static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
24bda9020eSMichael S. Tsirkin 				   gpa_t addr, int len)
255f94c174SLaurent Vivier {
265f94c174SLaurent Vivier 	struct kvm_coalesced_mmio_zone *zone;
27105f8d40SAvi Kivity 	struct kvm_coalesced_mmio_ring *ring;
28105f8d40SAvi Kivity 	unsigned avail;
295f94c174SLaurent Vivier 	int i;
305f94c174SLaurent Vivier 
315f94c174SLaurent Vivier 	/* Are we able to batch it ? */
325f94c174SLaurent Vivier 
335f94c174SLaurent Vivier 	/* last is the first free entry
345f94c174SLaurent Vivier 	 * check if we don't meet the first used entry
355f94c174SLaurent Vivier 	 * there is always one unused entry in the buffer
365f94c174SLaurent Vivier 	 */
37105f8d40SAvi Kivity 	ring = dev->kvm->coalesced_mmio_ring;
38105f8d40SAvi Kivity 	avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
3964a2268dSMarcelo Tosatti 	if (avail < KVM_MAX_VCPUS) {
405f94c174SLaurent Vivier 		/* full */
415f94c174SLaurent Vivier 		return 0;
425f94c174SLaurent Vivier 	}
435f94c174SLaurent Vivier 
445f94c174SLaurent Vivier 	/* is it in a batchable area ? */
455f94c174SLaurent Vivier 
465f94c174SLaurent Vivier 	for (i = 0; i < dev->nb_zones; i++) {
475f94c174SLaurent Vivier 		zone = &dev->zone[i];
485f94c174SLaurent Vivier 
495f94c174SLaurent Vivier 		/* (addr,len) is fully included in
505f94c174SLaurent Vivier 		 * (zone->addr, zone->size)
515f94c174SLaurent Vivier 		 */
525f94c174SLaurent Vivier 
535f94c174SLaurent Vivier 		if (zone->addr <= addr &&
545f94c174SLaurent Vivier 		    addr + len <= zone->addr + zone->size)
555f94c174SLaurent Vivier 			return 1;
565f94c174SLaurent Vivier 	}
575f94c174SLaurent Vivier 	return 0;
585f94c174SLaurent Vivier }
595f94c174SLaurent Vivier 
60bda9020eSMichael S. Tsirkin static int coalesced_mmio_write(struct kvm_io_device *this,
615f94c174SLaurent Vivier 				gpa_t addr, int len, const void *val)
625f94c174SLaurent Vivier {
63d76685c4SGregory Haskins 	struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
645f94c174SLaurent Vivier 	struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
65bda9020eSMichael S. Tsirkin 	if (!coalesced_mmio_in_range(dev, addr, len))
66bda9020eSMichael S. Tsirkin 		return -EOPNOTSUPP;
675f94c174SLaurent Vivier 
6864a2268dSMarcelo Tosatti 	spin_lock(&dev->lock);
695f94c174SLaurent Vivier 
705f94c174SLaurent Vivier 	/* copy data in first free entry of the ring */
715f94c174SLaurent Vivier 
725f94c174SLaurent Vivier 	ring->coalesced_mmio[ring->last].phys_addr = addr;
735f94c174SLaurent Vivier 	ring->coalesced_mmio[ring->last].len = len;
745f94c174SLaurent Vivier 	memcpy(ring->coalesced_mmio[ring->last].data, val, len);
755f94c174SLaurent Vivier 	smp_wmb();
765f94c174SLaurent Vivier 	ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
7764a2268dSMarcelo Tosatti 	spin_unlock(&dev->lock);
78bda9020eSMichael S. Tsirkin 	return 0;
795f94c174SLaurent Vivier }
805f94c174SLaurent Vivier 
815f94c174SLaurent Vivier static void coalesced_mmio_destructor(struct kvm_io_device *this)
825f94c174SLaurent Vivier {
83d76685c4SGregory Haskins 	struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
84787a660aSGregory Haskins 
85787a660aSGregory Haskins 	kfree(dev);
865f94c174SLaurent Vivier }
875f94c174SLaurent Vivier 
88d76685c4SGregory Haskins static const struct kvm_io_device_ops coalesced_mmio_ops = {
89d76685c4SGregory Haskins 	.write      = coalesced_mmio_write,
90d76685c4SGregory Haskins 	.destructor = coalesced_mmio_destructor,
91d76685c4SGregory Haskins };
92d76685c4SGregory Haskins 
935f94c174SLaurent Vivier int kvm_coalesced_mmio_init(struct kvm *kvm)
945f94c174SLaurent Vivier {
955f94c174SLaurent Vivier 	struct kvm_coalesced_mmio_dev *dev;
96980da6ceSAvi Kivity 	struct page *page;
97090b7affSGregory Haskins 	int ret;
985f94c174SLaurent Vivier 
99980da6ceSAvi Kivity 	ret = -ENOMEM;
100980da6ceSAvi Kivity 	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
101980da6ceSAvi Kivity 	if (!page)
102980da6ceSAvi Kivity 		goto out_err;
103980da6ceSAvi Kivity 	kvm->coalesced_mmio_ring = page_address(page);
104980da6ceSAvi Kivity 
105980da6ceSAvi Kivity 	ret = -ENOMEM;
1065f94c174SLaurent Vivier 	dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
1075f94c174SLaurent Vivier 	if (!dev)
108980da6ceSAvi Kivity 		goto out_free_page;
10964a2268dSMarcelo Tosatti 	spin_lock_init(&dev->lock);
110d76685c4SGregory Haskins 	kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
1115f94c174SLaurent Vivier 	dev->kvm = kvm;
1125f94c174SLaurent Vivier 	kvm->coalesced_mmio_dev = dev;
1135f94c174SLaurent Vivier 
11479fac95eSMarcelo Tosatti 	mutex_lock(&kvm->slots_lock);
115e93f8a0fSMarcelo Tosatti 	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
11679fac95eSMarcelo Tosatti 	mutex_unlock(&kvm->slots_lock);
117090b7affSGregory Haskins 	if (ret < 0)
118980da6ceSAvi Kivity 		goto out_free_dev;
119090b7affSGregory Haskins 
120090b7affSGregory Haskins 	return ret;
121980da6ceSAvi Kivity 
122980da6ceSAvi Kivity out_free_dev:
123980da6ceSAvi Kivity 	kfree(dev);
124980da6ceSAvi Kivity out_free_page:
125980da6ceSAvi Kivity 	__free_page(page);
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 {
1395f94c174SLaurent Vivier 	struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
1405f94c174SLaurent Vivier 
1415f94c174SLaurent Vivier 	if (dev == NULL)
1425f94c174SLaurent Vivier 		return -EINVAL;
1435f94c174SLaurent Vivier 
14479fac95eSMarcelo Tosatti 	mutex_lock(&kvm->slots_lock);
1455f94c174SLaurent Vivier 	if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
14679fac95eSMarcelo Tosatti 		mutex_unlock(&kvm->slots_lock);
1475f94c174SLaurent Vivier 		return -ENOBUFS;
1485f94c174SLaurent Vivier 	}
1495f94c174SLaurent Vivier 
1505f94c174SLaurent Vivier 	dev->zone[dev->nb_zones] = *zone;
1515f94c174SLaurent Vivier 	dev->nb_zones++;
1525f94c174SLaurent Vivier 
15379fac95eSMarcelo Tosatti 	mutex_unlock(&kvm->slots_lock);
1545f94c174SLaurent Vivier 	return 0;
1555f94c174SLaurent Vivier }
1565f94c174SLaurent Vivier 
1575f94c174SLaurent Vivier int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
1585f94c174SLaurent Vivier 					   struct kvm_coalesced_mmio_zone *zone)
1595f94c174SLaurent Vivier {
1605f94c174SLaurent Vivier 	int i;
1615f94c174SLaurent Vivier 	struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
1625f94c174SLaurent Vivier 	struct kvm_coalesced_mmio_zone *z;
1635f94c174SLaurent Vivier 
1645f94c174SLaurent Vivier 	if (dev == NULL)
1655f94c174SLaurent Vivier 		return -EINVAL;
1665f94c174SLaurent Vivier 
16779fac95eSMarcelo Tosatti 	mutex_lock(&kvm->slots_lock);
1685f94c174SLaurent Vivier 
1695f94c174SLaurent Vivier 	i = dev->nb_zones;
1705f94c174SLaurent Vivier 	while (i) {
1715f94c174SLaurent Vivier 		z = &dev->zone[i - 1];
1725f94c174SLaurent Vivier 
1735f94c174SLaurent Vivier 		/* unregister all zones
1745f94c174SLaurent Vivier 		 * included in (zone->addr, zone->size)
1755f94c174SLaurent Vivier 		 */
1765f94c174SLaurent Vivier 
1775f94c174SLaurent Vivier 		if (zone->addr <= z->addr &&
1785f94c174SLaurent Vivier 		    z->addr + z->size <= zone->addr + zone->size) {
1795f94c174SLaurent Vivier 			dev->nb_zones--;
1805f94c174SLaurent Vivier 			*z = dev->zone[dev->nb_zones];
1815f94c174SLaurent Vivier 		}
1825f94c174SLaurent Vivier 		i--;
1835f94c174SLaurent Vivier 	}
1845f94c174SLaurent Vivier 
18579fac95eSMarcelo Tosatti 	mutex_unlock(&kvm->slots_lock);
1865f94c174SLaurent Vivier 
1875f94c174SLaurent Vivier 	return 0;
1885f94c174SLaurent Vivier }
189