coalesced_mmio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * KVM coalesced MMIO
  3. *
  4. * Copyright (c) 2008 Bull S.A.S.
  5. *
  6. * Author: Laurent Vivier <Laurent.Vivier@bull.net>
  7. *
  8. */
  9. #include "iodev.h"
  10. #include <linux/kvm_host.h>
  11. #include <linux/slab.h>
  12. #include <linux/kvm.h>
  13. #include "coalesced_mmio.h"
  14. static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
  15. {
  16. return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
  17. }
  18. static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
  19. gpa_t addr, int len)
  20. {
  21. struct kvm_coalesced_mmio_zone *zone;
  22. struct kvm_coalesced_mmio_ring *ring;
  23. unsigned avail;
  24. int i;
  25. /* Are we able to batch it ? */
  26. /* last is the first free entry
  27. * check if we don't meet the first used entry
  28. * there is always one unused entry in the buffer
  29. */
  30. ring = dev->kvm->coalesced_mmio_ring;
  31. avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
  32. if (avail < KVM_MAX_VCPUS) {
  33. /* full */
  34. return 0;
  35. }
  36. /* is it in a batchable area ? */
  37. for (i = 0; i < dev->nb_zones; i++) {
  38. zone = &dev->zone[i];
  39. /* (addr,len) is fully included in
  40. * (zone->addr, zone->size)
  41. */
  42. if (zone->addr <= addr &&
  43. addr + len <= zone->addr + zone->size)
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. static int coalesced_mmio_write(struct kvm_io_device *this,
  49. gpa_t addr, int len, const void *val)
  50. {
  51. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  52. struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
  53. if (!coalesced_mmio_in_range(dev, addr, len))
  54. return -EOPNOTSUPP;
  55. spin_lock(&dev->lock);
  56. /* copy data in first free entry of the ring */
  57. ring->coalesced_mmio[ring->last].phys_addr = addr;
  58. ring->coalesced_mmio[ring->last].len = len;
  59. memcpy(ring->coalesced_mmio[ring->last].data, val, len);
  60. smp_wmb();
  61. ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
  62. spin_unlock(&dev->lock);
  63. return 0;
  64. }
  65. static void coalesced_mmio_destructor(struct kvm_io_device *this)
  66. {
  67. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  68. kfree(dev);
  69. }
  70. static const struct kvm_io_device_ops coalesced_mmio_ops = {
  71. .write = coalesced_mmio_write,
  72. .destructor = coalesced_mmio_destructor,
  73. };
  74. int kvm_coalesced_mmio_init(struct kvm *kvm)
  75. {
  76. struct kvm_coalesced_mmio_dev *dev;
  77. struct page *page;
  78. int ret;
  79. ret = -ENOMEM;
  80. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  81. if (!page)
  82. goto out_err;
  83. kvm->coalesced_mmio_ring = page_address(page);
  84. ret = -ENOMEM;
  85. dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
  86. if (!dev)
  87. goto out_free_page;
  88. spin_lock_init(&dev->lock);
  89. kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
  90. dev->kvm = kvm;
  91. kvm->coalesced_mmio_dev = dev;
  92. mutex_lock(&kvm->slots_lock);
  93. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
  94. mutex_unlock(&kvm->slots_lock);
  95. if (ret < 0)
  96. goto out_free_dev;
  97. return ret;
  98. out_free_dev:
  99. kfree(dev);
  100. out_free_page:
  101. __free_page(page);
  102. out_err:
  103. return ret;
  104. }
  105. void kvm_coalesced_mmio_free(struct kvm *kvm)
  106. {
  107. if (kvm->coalesced_mmio_ring)
  108. free_page((unsigned long)kvm->coalesced_mmio_ring);
  109. }
  110. int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
  111. struct kvm_coalesced_mmio_zone *zone)
  112. {
  113. struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
  114. if (dev == NULL)
  115. return -EINVAL;
  116. mutex_lock(&kvm->slots_lock);
  117. if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
  118. mutex_unlock(&kvm->slots_lock);
  119. return -ENOBUFS;
  120. }
  121. dev->zone[dev->nb_zones] = *zone;
  122. dev->nb_zones++;
  123. mutex_unlock(&kvm->slots_lock);
  124. return 0;
  125. }
  126. int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
  127. struct kvm_coalesced_mmio_zone *zone)
  128. {
  129. int i;
  130. struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
  131. struct kvm_coalesced_mmio_zone *z;
  132. if (dev == NULL)
  133. return -EINVAL;
  134. mutex_lock(&kvm->slots_lock);
  135. i = dev->nb_zones;
  136. while (i) {
  137. z = &dev->zone[i - 1];
  138. /* unregister all zones
  139. * included in (zone->addr, zone->size)
  140. */
  141. if (zone->addr <= z->addr &&
  142. z->addr + z->size <= zone->addr + zone->size) {
  143. dev->nb_zones--;
  144. *z = dev->zone[dev->nb_zones];
  145. }
  146. i--;
  147. }
  148. mutex_unlock(&kvm->slots_lock);
  149. return 0;
  150. }