coalesced_mmio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_io_device *this,
  18. gpa_t addr, int len, int is_write)
  19. {
  20. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  21. struct kvm_coalesced_mmio_zone *zone;
  22. struct kvm_coalesced_mmio_ring *ring;
  23. unsigned avail;
  24. int i;
  25. if (!is_write)
  26. return 0;
  27. /* kvm->lock is taken by the caller and must be not released before
  28. * dev.read/write
  29. */
  30. /* Are we able to batch it ? */
  31. /* last is the first free entry
  32. * check if we don't meet the first used entry
  33. * there is always one unused entry in the buffer
  34. */
  35. ring = dev->kvm->coalesced_mmio_ring;
  36. avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
  37. if (avail < 1) {
  38. /* full */
  39. return 0;
  40. }
  41. /* is it in a batchable area ? */
  42. for (i = 0; i < dev->nb_zones; i++) {
  43. zone = &dev->zone[i];
  44. /* (addr,len) is fully included in
  45. * (zone->addr, zone->size)
  46. */
  47. if (zone->addr <= addr &&
  48. addr + len <= zone->addr + zone->size)
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. static void coalesced_mmio_write(struct kvm_io_device *this,
  54. gpa_t addr, int len, const void *val)
  55. {
  56. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  57. struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
  58. /* kvm->lock must be taken by caller before call to in_range()*/
  59. /* copy data in first free entry of the ring */
  60. ring->coalesced_mmio[ring->last].phys_addr = addr;
  61. ring->coalesced_mmio[ring->last].len = len;
  62. memcpy(ring->coalesced_mmio[ring->last].data, val, len);
  63. smp_wmb();
  64. ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
  65. }
  66. static void coalesced_mmio_destructor(struct kvm_io_device *this)
  67. {
  68. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  69. kfree(dev);
  70. }
  71. static const struct kvm_io_device_ops coalesced_mmio_ops = {
  72. .write = coalesced_mmio_write,
  73. .in_range = coalesced_mmio_in_range,
  74. .destructor = coalesced_mmio_destructor,
  75. };
  76. int kvm_coalesced_mmio_init(struct kvm *kvm)
  77. {
  78. struct kvm_coalesced_mmio_dev *dev;
  79. dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
  80. if (!dev)
  81. return -ENOMEM;
  82. kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
  83. dev->kvm = kvm;
  84. kvm->coalesced_mmio_dev = dev;
  85. kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev);
  86. return 0;
  87. }
  88. int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
  89. struct kvm_coalesced_mmio_zone *zone)
  90. {
  91. struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
  92. if (dev == NULL)
  93. return -EINVAL;
  94. mutex_lock(&kvm->lock);
  95. if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
  96. mutex_unlock(&kvm->lock);
  97. return -ENOBUFS;
  98. }
  99. dev->zone[dev->nb_zones] = *zone;
  100. dev->nb_zones++;
  101. mutex_unlock(&kvm->lock);
  102. return 0;
  103. }
  104. int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
  105. struct kvm_coalesced_mmio_zone *zone)
  106. {
  107. int i;
  108. struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
  109. struct kvm_coalesced_mmio_zone *z;
  110. if (dev == NULL)
  111. return -EINVAL;
  112. mutex_lock(&kvm->lock);
  113. i = dev->nb_zones;
  114. while(i) {
  115. z = &dev->zone[i - 1];
  116. /* unregister all zones
  117. * included in (zone->addr, zone->size)
  118. */
  119. if (zone->addr <= z->addr &&
  120. z->addr + z->size <= zone->addr + zone->size) {
  121. dev->nb_zones--;
  122. *z = dev->zone[dev->nb_zones];
  123. }
  124. i--;
  125. }
  126. mutex_unlock(&kvm->lock);
  127. return 0;
  128. }