coalesced_mmio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /* is it in a batchable area ?
  23. * (addr,len) is fully included in
  24. * (zone->addr, zone->size)
  25. */
  26. return (dev->zone.addr <= addr &&
  27. addr + len <= dev->zone.addr + dev->zone.size);
  28. }
  29. static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev)
  30. {
  31. struct kvm_coalesced_mmio_ring *ring;
  32. unsigned avail;
  33. /* Are we able to batch it ? */
  34. /* last is the first free entry
  35. * check if we don't meet the first used entry
  36. * there is always one unused entry in the buffer
  37. */
  38. ring = dev->kvm->coalesced_mmio_ring;
  39. avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
  40. if (avail == 0) {
  41. /* full */
  42. return 0;
  43. }
  44. return 1;
  45. }
  46. static int coalesced_mmio_write(struct kvm_io_device *this,
  47. gpa_t addr, int len, const void *val)
  48. {
  49. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  50. struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
  51. if (!coalesced_mmio_in_range(dev, addr, len))
  52. return -EOPNOTSUPP;
  53. spin_lock(&dev->kvm->ring_lock);
  54. if (!coalesced_mmio_has_room(dev)) {
  55. spin_unlock(&dev->kvm->ring_lock);
  56. return -EOPNOTSUPP;
  57. }
  58. /* copy data in first free entry of the ring */
  59. ring->coalesced_mmio[ring->last].phys_addr = addr;
  60. ring->coalesced_mmio[ring->last].len = len;
  61. memcpy(ring->coalesced_mmio[ring->last].data, val, len);
  62. smp_wmb();
  63. ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
  64. spin_unlock(&dev->kvm->ring_lock);
  65. return 0;
  66. }
  67. static void coalesced_mmio_destructor(struct kvm_io_device *this)
  68. {
  69. struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
  70. list_del(&dev->list);
  71. kfree(dev);
  72. }
  73. static const struct kvm_io_device_ops coalesced_mmio_ops = {
  74. .write = coalesced_mmio_write,
  75. .destructor = coalesced_mmio_destructor,
  76. };
  77. int kvm_coalesced_mmio_init(struct kvm *kvm)
  78. {
  79. struct page *page;
  80. int ret;
  81. ret = -ENOMEM;
  82. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  83. if (!page)
  84. goto out_err;
  85. ret = 0;
  86. kvm->coalesced_mmio_ring = page_address(page);
  87. /*
  88. * We're using this spinlock to sync access to the coalesced ring.
  89. * The list doesn't need it's own lock since device registration and
  90. * unregistration should only happen when kvm->slots_lock is held.
  91. */
  92. spin_lock_init(&kvm->ring_lock);
  93. INIT_LIST_HEAD(&kvm->coalesced_zones);
  94. out_err:
  95. return ret;
  96. }
  97. void kvm_coalesced_mmio_free(struct kvm *kvm)
  98. {
  99. if (kvm->coalesced_mmio_ring)
  100. free_page((unsigned long)kvm->coalesced_mmio_ring);
  101. }
  102. int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
  103. struct kvm_coalesced_mmio_zone *zone)
  104. {
  105. int ret;
  106. struct kvm_coalesced_mmio_dev *dev;
  107. dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
  108. if (!dev)
  109. return -ENOMEM;
  110. kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
  111. dev->kvm = kvm;
  112. dev->zone = *zone;
  113. mutex_lock(&kvm->slots_lock);
  114. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, zone->addr,
  115. zone->size, &dev->dev);
  116. if (ret < 0)
  117. goto out_free_dev;
  118. list_add_tail(&dev->list, &kvm->coalesced_zones);
  119. mutex_unlock(&kvm->slots_lock);
  120. return ret;
  121. out_free_dev:
  122. mutex_unlock(&kvm->slots_lock);
  123. kfree(dev);
  124. if (dev == NULL)
  125. return -ENXIO;
  126. return 0;
  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. mutex_lock(&kvm->slots_lock);
  133. list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list)
  134. if (coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
  135. kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &dev->dev);
  136. kvm_iodevice_destructor(&dev->dev);
  137. }
  138. mutex_unlock(&kvm->slots_lock);
  139. return 0;
  140. }