1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25f94c174SLaurent Vivier /*
35f94c174SLaurent Vivier * KVM coalesced MMIO
45f94c174SLaurent Vivier *
55f94c174SLaurent Vivier * Copyright (c) 2008 Bull S.A.S.
6221d059dSAvi Kivity * Copyright 2009 Red Hat, Inc. and/or its affiliates.
75f94c174SLaurent Vivier *
85f94c174SLaurent Vivier * Author: Laurent Vivier <Laurent.Vivier@bull.net>
95f94c174SLaurent Vivier *
105f94c174SLaurent Vivier */
115f94c174SLaurent Vivier
12af669ac6SAndre Przywara #include <kvm/iodev.h>
135f94c174SLaurent Vivier
145f94c174SLaurent Vivier #include <linux/kvm_host.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
165f94c174SLaurent Vivier #include <linux/kvm.h>
175f94c174SLaurent Vivier
185f94c174SLaurent Vivier #include "coalesced_mmio.h"
195f94c174SLaurent Vivier
to_mmio(struct kvm_io_device * dev)20d76685c4SGregory Haskins static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
21d76685c4SGregory Haskins {
22d76685c4SGregory Haskins return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
23d76685c4SGregory Haskins }
24d76685c4SGregory Haskins
coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev * dev,gpa_t addr,int len)25bda9020eSMichael S. Tsirkin static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
26bda9020eSMichael S. Tsirkin gpa_t addr, int len)
275f94c174SLaurent Vivier {
282b3c246aSSasha Levin /* is it in a batchable area ?
292b3c246aSSasha Levin * (addr,len) is fully included in
305f94c174SLaurent Vivier * (zone->addr, zone->size)
315f94c174SLaurent Vivier */
321a214246SDan Carpenter if (len < 0)
331a214246SDan Carpenter return 0;
341a214246SDan Carpenter if (addr + len < addr)
351a214246SDan Carpenter return 0;
361a214246SDan Carpenter if (addr < dev->zone.addr)
371a214246SDan Carpenter return 0;
381a214246SDan Carpenter if (addr + len > dev->zone.addr + dev->zone.size)
391a214246SDan Carpenter return 0;
401a214246SDan Carpenter return 1;
415f94c174SLaurent Vivier }
425f94c174SLaurent Vivier
coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev * dev,u32 last)43b60fe990SMatt Delco static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev, u32 last)
44c298125fSSasha Levin {
45c298125fSSasha Levin struct kvm_coalesced_mmio_ring *ring;
46c298125fSSasha Levin unsigned avail;
47c298125fSSasha Levin
48c298125fSSasha Levin /* Are we able to batch it ? */
49c298125fSSasha Levin
50c298125fSSasha Levin /* last is the first free entry
51c298125fSSasha Levin * check if we don't meet the first used entry
52c298125fSSasha Levin * there is always one unused entry in the buffer
53c298125fSSasha Levin */
54c298125fSSasha Levin ring = dev->kvm->coalesced_mmio_ring;
55b60fe990SMatt Delco avail = (ring->first - last - 1) % KVM_COALESCED_MMIO_MAX;
56c298125fSSasha Levin if (avail == 0) {
57c298125fSSasha Levin /* full */
58c298125fSSasha Levin return 0;
59c298125fSSasha Levin }
60c298125fSSasha Levin
61c298125fSSasha Levin return 1;
62c298125fSSasha Levin }
63c298125fSSasha Levin
coalesced_mmio_write(struct kvm_vcpu * vcpu,struct kvm_io_device * this,gpa_t addr,int len,const void * val)64e32edf4fSNikolay Nikolaev static int coalesced_mmio_write(struct kvm_vcpu *vcpu,
65e32edf4fSNikolay Nikolaev struct kvm_io_device *this, gpa_t addr,
66e32edf4fSNikolay Nikolaev int len, const void *val)
675f94c174SLaurent Vivier {
68d76685c4SGregory Haskins struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
695f94c174SLaurent Vivier struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
70b60fe990SMatt Delco __u32 insert;
71c298125fSSasha Levin
72bda9020eSMichael S. Tsirkin if (!coalesced_mmio_in_range(dev, addr, len))
73bda9020eSMichael S. Tsirkin return -EOPNOTSUPP;
745f94c174SLaurent Vivier
752b3c246aSSasha Levin spin_lock(&dev->kvm->ring_lock);
765f94c174SLaurent Vivier
77b60fe990SMatt Delco insert = READ_ONCE(ring->last);
78b60fe990SMatt Delco if (!coalesced_mmio_has_room(dev, insert) ||
79b60fe990SMatt Delco insert >= KVM_COALESCED_MMIO_MAX) {
802b3c246aSSasha Levin spin_unlock(&dev->kvm->ring_lock);
81c298125fSSasha Levin return -EOPNOTSUPP;
82c298125fSSasha Levin }
83c298125fSSasha Levin
845f94c174SLaurent Vivier /* copy data in first free entry of the ring */
855f94c174SLaurent Vivier
86b60fe990SMatt Delco ring->coalesced_mmio[insert].phys_addr = addr;
87b60fe990SMatt Delco ring->coalesced_mmio[insert].len = len;
88b60fe990SMatt Delco memcpy(ring->coalesced_mmio[insert].data, val, len);
89b60fe990SMatt Delco ring->coalesced_mmio[insert].pio = dev->zone.pio;
905f94c174SLaurent Vivier smp_wmb();
91b60fe990SMatt Delco ring->last = (insert + 1) % KVM_COALESCED_MMIO_MAX;
922b3c246aSSasha Levin spin_unlock(&dev->kvm->ring_lock);
93bda9020eSMichael S. Tsirkin return 0;
945f94c174SLaurent Vivier }
955f94c174SLaurent Vivier
coalesced_mmio_destructor(struct kvm_io_device * this)965f94c174SLaurent Vivier static void coalesced_mmio_destructor(struct kvm_io_device *this)
975f94c174SLaurent Vivier {
98d76685c4SGregory Haskins struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
99787a660aSGregory Haskins
1002b3c246aSSasha Levin list_del(&dev->list);
1012b3c246aSSasha Levin
102787a660aSGregory Haskins kfree(dev);
1035f94c174SLaurent Vivier }
1045f94c174SLaurent Vivier
105d76685c4SGregory Haskins static const struct kvm_io_device_ops coalesced_mmio_ops = {
106d76685c4SGregory Haskins .write = coalesced_mmio_write,
107d76685c4SGregory Haskins .destructor = coalesced_mmio_destructor,
108d76685c4SGregory Haskins };
109d76685c4SGregory Haskins
kvm_coalesced_mmio_init(struct kvm * kvm)1105f94c174SLaurent Vivier int kvm_coalesced_mmio_init(struct kvm *kvm)
1115f94c174SLaurent Vivier {
112980da6ceSAvi Kivity struct page *page;
1135f94c174SLaurent Vivier
11493bb59caSShakeel Butt page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
115980da6ceSAvi Kivity if (!page)
116b139b5a2SMiaohe Lin return -ENOMEM;
1172b3c246aSSasha Levin
118980da6ceSAvi Kivity kvm->coalesced_mmio_ring = page_address(page);
119980da6ceSAvi Kivity
1202b3c246aSSasha Levin /*
1212b3c246aSSasha Levin * We're using this spinlock to sync access to the coalesced ring.
122656012c7SFuad Tabba * The list doesn't need its own lock since device registration and
1232b3c246aSSasha Levin * unregistration should only happen when kvm->slots_lock is held.
1242b3c246aSSasha Levin */
1252b3c246aSSasha Levin spin_lock_init(&kvm->ring_lock);
1262b3c246aSSasha Levin INIT_LIST_HEAD(&kvm->coalesced_zones);
1275f94c174SLaurent Vivier
128b139b5a2SMiaohe Lin return 0;
129980da6ceSAvi Kivity }
130980da6ceSAvi Kivity
kvm_coalesced_mmio_free(struct kvm * kvm)131980da6ceSAvi Kivity void kvm_coalesced_mmio_free(struct kvm *kvm)
132980da6ceSAvi Kivity {
133980da6ceSAvi Kivity if (kvm->coalesced_mmio_ring)
134980da6ceSAvi Kivity free_page((unsigned long)kvm->coalesced_mmio_ring);
1355f94c174SLaurent Vivier }
1365f94c174SLaurent Vivier
kvm_vm_ioctl_register_coalesced_mmio(struct kvm * kvm,struct kvm_coalesced_mmio_zone * zone)1375f94c174SLaurent Vivier int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
1385f94c174SLaurent Vivier struct kvm_coalesced_mmio_zone *zone)
1395f94c174SLaurent Vivier {
1402b3c246aSSasha Levin int ret;
1412b3c246aSSasha Levin struct kvm_coalesced_mmio_dev *dev;
1422b3c246aSSasha Levin
1430804c849SPeng Hao if (zone->pio != 1 && zone->pio != 0)
1440804c849SPeng Hao return -EINVAL;
1450804c849SPeng Hao
146b12ce36aSBen Gardon dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev),
147b12ce36aSBen Gardon GFP_KERNEL_ACCOUNT);
1482b3c246aSSasha Levin if (!dev)
1492b3c246aSSasha Levin return -ENOMEM;
1502b3c246aSSasha Levin
1512b3c246aSSasha Levin kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
1522b3c246aSSasha Levin dev->kvm = kvm;
1532b3c246aSSasha Levin dev->zone = *zone;
1542b3c246aSSasha Levin
1552b3c246aSSasha Levin mutex_lock(&kvm->slots_lock);
1560804c849SPeng Hao ret = kvm_io_bus_register_dev(kvm,
1570804c849SPeng Hao zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS,
1580804c849SPeng Hao zone->addr, zone->size, &dev->dev);
1592b3c246aSSasha Levin if (ret < 0)
1602b3c246aSSasha Levin goto out_free_dev;
1612b3c246aSSasha Levin list_add_tail(&dev->list, &kvm->coalesced_zones);
1622b3c246aSSasha Levin mutex_unlock(&kvm->slots_lock);
1632b3c246aSSasha Levin
164aac5c422SDan Carpenter return 0;
1652b3c246aSSasha Levin
1662b3c246aSSasha Levin out_free_dev:
1672b3c246aSSasha Levin mutex_unlock(&kvm->slots_lock);
1682b3c246aSSasha Levin kfree(dev);
1695f94c174SLaurent Vivier
170aac5c422SDan Carpenter return ret;
1715f94c174SLaurent Vivier }
1725f94c174SLaurent Vivier
kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm * kvm,struct kvm_coalesced_mmio_zone * zone)1735f94c174SLaurent Vivier int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
1745f94c174SLaurent Vivier struct kvm_coalesced_mmio_zone *zone)
1755f94c174SLaurent Vivier {
1762b3c246aSSasha Levin struct kvm_coalesced_mmio_dev *dev, *tmp;
1775d3c4c79SSean Christopherson int r;
1785f94c174SLaurent Vivier
179987d1149SEric Biggers if (zone->pio != 1 && zone->pio != 0)
180987d1149SEric Biggers return -EINVAL;
181987d1149SEric Biggers
18279fac95eSMarcelo Tosatti mutex_lock(&kvm->slots_lock);
1835f94c174SLaurent Vivier
1845d3c4c79SSean Christopherson list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list) {
185987d1149SEric Biggers if (zone->pio == dev->zone.pio &&
186987d1149SEric Biggers coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
1875d3c4c79SSean Christopherson r = kvm_io_bus_unregister_dev(kvm,
1880804c849SPeng Hao zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev);
1895d3c4c79SSean Christopherson /*
1905d3c4c79SSean Christopherson * On failure, unregister destroys all devices on the
191*5ea5ca3cSWei Wang * bus, including the target device. There's no need
192*5ea5ca3cSWei Wang * to restart the walk as there aren't any zones left.
1935d3c4c79SSean Christopherson */
1945d3c4c79SSean Christopherson if (r)
1955d3c4c79SSean Christopherson break;
1965d3c4c79SSean Christopherson }
1975f94c174SLaurent Vivier }
1985f94c174SLaurent Vivier
19979fac95eSMarcelo Tosatti mutex_unlock(&kvm->slots_lock);
2005f94c174SLaurent Vivier
2015d3c4c79SSean Christopherson /*
2025d3c4c79SSean Christopherson * Ignore the result of kvm_io_bus_unregister_dev(), from userspace's
2035d3c4c79SSean Christopherson * perspective, the coalesced MMIO is most definitely unregistered.
2045d3c4c79SSean Christopherson */
2055f94c174SLaurent Vivier return 0;
2065f94c174SLaurent Vivier }
207