coalesced_mmio.c 4.0 KB

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