coalesced_mmio.c 4.1 KB

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