coalesced_mmio.c 4.2 KB

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