coalesced_mmio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KVM coalesced MMIO
  4. *
  5. * Copyright (c) 2008 Bull S.A.S.
  6. * Copyright 2009 Red Hat, Inc. and/or its affiliates.
  7. *
  8. * Author: Laurent Vivier <Laurent.Vivier@bull.net>
  9. *
  10. */
  11. #include <kvm/iodev.h>
  12. #include <linux/kvm_host.h>
  13. #include <linux/slab.h>
  14. #include <linux/kvm.h>
  15. #include "coalesced_mmio.h"
  16. static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
  17. {
  18. return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
  19. }
  20. static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
  21. gpa_t addr, int len)
  22. {
  23. /* is it in a batchable area ?
  24. * (addr,len) is fully included in
  25. * (zone->addr, zone->size)
  26. */
  27. if (len < 0)
  28. return 0;
  29. if (addr + len < addr)
  30. return 0;
  31. if (addr < dev->zone.addr)
  32. return 0;
  33. if (addr + len > dev->zone.addr + dev->zone.size)
  34. return 0;
  35. return 1;
  36. }
  37. static int coalesced_mmio_write(struct kvm_vcpu *vcpu,
  38. struct kvm_io_device *this, gpa_t addr,
  39. int len, const void *val)
  40. {
  41. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  42. struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
  43. __u32 insert;
  44. if (!coalesced_mmio_in_range(dev, addr, len))
  45. return -EOPNOTSUPP;
  46. spin_lock(&dev->kvm->ring_lock);
  47. /*
  48. * last is the index of the entry to fill. Verify userspace hasn't
  49. * set last to be out of range, and that there is room in the ring.
  50. * Leave one entry free in the ring so that userspace can differentiate
  51. * between an empty ring and a full ring.
  52. */
  53. insert = READ_ONCE(ring->last);
  54. if (insert >= KVM_COALESCED_MMIO_MAX ||
  55. (insert + 1) % KVM_COALESCED_MMIO_MAX == READ_ONCE(ring->first)) {
  56. spin_unlock(&dev->kvm->ring_lock);
  57. return -EOPNOTSUPP;
  58. }
  59. /* copy data in first free entry of the ring */
  60. ring->coalesced_mmio[insert].phys_addr = addr;
  61. ring->coalesced_mmio[insert].len = len;
  62. memcpy(ring->coalesced_mmio[insert].data, val, len);
  63. ring->coalesced_mmio[insert].pio = dev->zone.pio;
  64. smp_wmb();
  65. ring->last = (insert + 1) % KVM_COALESCED_MMIO_MAX;
  66. spin_unlock(&dev->kvm->ring_lock);
  67. return 0;
  68. }
  69. static void coalesced_mmio_destructor(struct kvm_io_device *this)
  70. {
  71. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  72. list_del(&dev->list);
  73. kfree(dev);
  74. }
  75. static const struct kvm_io_device_ops coalesced_mmio_ops = {
  76. .write = coalesced_mmio_write,
  77. .destructor = coalesced_mmio_destructor,
  78. };
  79. int kvm_coalesced_mmio_init(struct kvm *kvm)
  80. {
  81. struct page *page;
  82. page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  83. if (!page)
  84. return -ENOMEM;
  85. kvm->coalesced_mmio_ring = page_address(page);
  86. /*
  87. * We're using this spinlock to sync access to the coalesced ring.
  88. * The list doesn't need its own lock since device registration and
  89. * unregistration should only happen when kvm->slots_lock is held.
  90. */
  91. spin_lock_init(&kvm->ring_lock);
  92. INIT_LIST_HEAD(&kvm->coalesced_zones);
  93. return 0;
  94. }
  95. void kvm_coalesced_mmio_free(struct kvm *kvm)
  96. {
  97. if (kvm->coalesced_mmio_ring)
  98. free_page((unsigned long)kvm->coalesced_mmio_ring);
  99. }
  100. int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
  101. struct kvm_coalesced_mmio_zone *zone)
  102. {
  103. int ret;
  104. struct kvm_coalesced_mmio_dev *dev;
  105. if (zone->pio != 1 && zone->pio != 0)
  106. return -EINVAL;
  107. dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev),
  108. GFP_KERNEL_ACCOUNT);
  109. if (!dev)
  110. return -ENOMEM;
  111. kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
  112. dev->kvm = kvm;
  113. dev->zone = *zone;
  114. mutex_lock(&kvm->slots_lock);
  115. ret = kvm_io_bus_register_dev(kvm,
  116. zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS,
  117. zone->addr, zone->size, &dev->dev);
  118. if (ret < 0)
  119. goto out_free_dev;
  120. list_add_tail(&dev->list, &kvm->coalesced_zones);
  121. mutex_unlock(&kvm->slots_lock);
  122. return 0;
  123. out_free_dev:
  124. mutex_unlock(&kvm->slots_lock);
  125. kfree(dev);
  126. return ret;
  127. }
  128. int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
  129. struct kvm_coalesced_mmio_zone *zone)
  130. {
  131. struct kvm_coalesced_mmio_dev *dev, *tmp;
  132. int r;
  133. if (zone->pio != 1 && zone->pio != 0)
  134. return -EINVAL;
  135. mutex_lock(&kvm->slots_lock);
  136. list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list) {
  137. if (zone->pio == dev->zone.pio &&
  138. coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
  139. r = kvm_io_bus_unregister_dev(kvm,
  140. zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev);
  141. /*
  142. * On failure, unregister destroys all devices on the
  143. * bus, including the target device. There's no need
  144. * to restart the walk as there aren't any zones left.
  145. */
  146. if (r)
  147. break;
  148. }
  149. }
  150. mutex_unlock(&kvm->slots_lock);
  151. /*
  152. * Ignore the result of kvm_io_bus_unregister_dev(), from userspace's
  153. * perspective, the coalesced MMIO is most definitely unregistered.
  154. */
  155. return 0;
  156. }