VirtIOQueue.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Atomic.h>
  7. #include <Kernel/Bus/VirtIO/VirtIOQueue.h>
  8. namespace Kernel {
  9. VirtIOQueue::VirtIOQueue(u16 queue_size, u16 notify_offset)
  10. : m_queue_size(queue_size)
  11. , m_notify_offset(notify_offset)
  12. , m_free_buffers(queue_size)
  13. {
  14. size_t size_of_descriptors = sizeof(VirtIOQueueDescriptor) * queue_size;
  15. size_t size_of_driver = sizeof(VirtIOQueueDriver) + queue_size * sizeof(u16);
  16. size_t size_of_device = sizeof(VirtIOQueueDevice) + queue_size * sizeof(VirtIOQueueDeviceItem);
  17. auto queue_region_size = Memory::page_round_up(size_of_descriptors + size_of_driver + size_of_device);
  18. if (queue_region_size <= PAGE_SIZE)
  19. m_queue_region = MM.allocate_kernel_region(queue_region_size, "VirtIO Queue", Memory::Region::Access::ReadWrite);
  20. else
  21. m_queue_region = MM.allocate_contiguous_kernel_region(queue_region_size, "VirtIO Queue", Memory::Region::Access::ReadWrite);
  22. VERIFY(m_queue_region);
  23. // TODO: ensure alignment!!!
  24. u8* ptr = m_queue_region->vaddr().as_ptr();
  25. memset(ptr, 0, m_queue_region->size());
  26. m_descriptors = adopt_own_if_nonnull(reinterpret_cast<VirtIOQueueDescriptor*>(ptr));
  27. m_driver = adopt_own_if_nonnull(reinterpret_cast<VirtIOQueueDriver*>(ptr + size_of_descriptors));
  28. m_device = adopt_own_if_nonnull(reinterpret_cast<VirtIOQueueDevice*>(ptr + size_of_descriptors + size_of_driver));
  29. for (auto i = 0; i + 1 < queue_size; i++) {
  30. m_descriptors[i].next = i + 1; // link all of the descriptors in a line
  31. }
  32. enable_interrupts();
  33. }
  34. VirtIOQueue::~VirtIOQueue()
  35. {
  36. }
  37. void VirtIOQueue::enable_interrupts()
  38. {
  39. SpinlockLocker lock(m_lock);
  40. m_driver->flags = 0;
  41. }
  42. void VirtIOQueue::disable_interrupts()
  43. {
  44. SpinlockLocker lock(m_lock);
  45. m_driver->flags = 1;
  46. }
  47. bool VirtIOQueue::new_data_available() const
  48. {
  49. const auto index = AK::atomic_load(&m_device->index, AK::MemoryOrder::memory_order_relaxed);
  50. const auto used_tail = AK::atomic_load(&m_used_tail, AK::MemoryOrder::memory_order_relaxed);
  51. return index != used_tail;
  52. }
  53. VirtIOQueueChain VirtIOQueue::pop_used_buffer_chain(size_t& used)
  54. {
  55. VERIFY(m_lock.is_locked());
  56. if (!new_data_available()) {
  57. used = 0;
  58. return VirtIOQueueChain(*this);
  59. }
  60. full_memory_barrier();
  61. // Determine used length
  62. used = m_device->rings[m_used_tail % m_queue_size].length;
  63. // Determine start, end and number of nodes in chain
  64. auto descriptor_index = m_device->rings[m_used_tail % m_queue_size].index;
  65. size_t length_of_chain = 1;
  66. auto last_index = descriptor_index;
  67. while (m_descriptors[last_index].flags & VIRTQ_DESC_F_NEXT) {
  68. ++length_of_chain;
  69. last_index = m_descriptors[last_index].next;
  70. }
  71. // We are now done with this buffer chain
  72. m_used_tail++;
  73. return VirtIOQueueChain(*this, descriptor_index, last_index, length_of_chain);
  74. }
  75. void VirtIOQueue::discard_used_buffers()
  76. {
  77. VERIFY(m_lock.is_locked());
  78. size_t used;
  79. for (auto buffer = pop_used_buffer_chain(used); !buffer.is_empty(); buffer = pop_used_buffer_chain(used)) {
  80. buffer.release_buffer_slots_to_queue();
  81. }
  82. }
  83. void VirtIOQueue::reclaim_buffer_chain(u16 chain_start_index, u16 chain_end_index, size_t length_of_chain)
  84. {
  85. VERIFY(m_lock.is_locked());
  86. m_descriptors[chain_end_index].next = m_free_head;
  87. m_free_head = chain_start_index;
  88. m_free_buffers += length_of_chain;
  89. }
  90. bool VirtIOQueue::has_free_slots() const
  91. {
  92. const auto free_buffers = AK::atomic_load(&m_free_buffers, AK::MemoryOrder::memory_order_relaxed);
  93. return free_buffers > 0;
  94. }
  95. Optional<u16> VirtIOQueue::take_free_slot()
  96. {
  97. VERIFY(m_lock.is_locked());
  98. if (has_free_slots()) {
  99. auto descriptor_index = m_free_head;
  100. m_free_head = m_descriptors[descriptor_index].next;
  101. --m_free_buffers;
  102. return descriptor_index;
  103. } else {
  104. return {};
  105. }
  106. }
  107. bool VirtIOQueue::should_notify() const
  108. {
  109. VERIFY(m_lock.is_locked());
  110. auto device_flags = m_device->flags;
  111. return !(device_flags & VIRTQ_USED_F_NO_NOTIFY);
  112. }
  113. bool VirtIOQueueChain::add_buffer_to_chain(PhysicalAddress buffer_start, size_t buffer_length, BufferType buffer_type)
  114. {
  115. VERIFY(m_queue.lock().is_locked());
  116. // Ensure that no readable pages will be inserted after a writable one, as required by the VirtIO spec
  117. VERIFY(buffer_type == BufferType::DeviceWritable || !m_chain_has_writable_pages);
  118. m_chain_has_writable_pages |= (buffer_type == BufferType::DeviceWritable);
  119. // Take a free slot from the queue
  120. auto descriptor_index = m_queue.take_free_slot();
  121. if (!descriptor_index.has_value())
  122. return false;
  123. if (!m_start_of_chain_index.has_value()) {
  124. // Set start of chain if it hasn't been set
  125. m_start_of_chain_index = descriptor_index.value();
  126. } else {
  127. // Link from previous element in VirtIOQueueChain
  128. m_queue.m_descriptors[m_end_of_chain_index.value()].flags |= VIRTQ_DESC_F_NEXT;
  129. m_queue.m_descriptors[m_end_of_chain_index.value()].next = descriptor_index.value();
  130. }
  131. // Update end of chain
  132. m_end_of_chain_index = descriptor_index.value();
  133. ++m_chain_length;
  134. // Populate buffer info
  135. VERIFY(buffer_length <= NumericLimits<size_t>::max());
  136. m_queue.m_descriptors[descriptor_index.value()].address = static_cast<u64>(buffer_start.get());
  137. m_queue.m_descriptors[descriptor_index.value()].flags = static_cast<u16>(buffer_type);
  138. m_queue.m_descriptors[descriptor_index.value()].length = static_cast<u32>(buffer_length);
  139. return true;
  140. }
  141. void VirtIOQueueChain::submit_to_queue()
  142. {
  143. VERIFY(m_queue.lock().is_locked());
  144. VERIFY(m_start_of_chain_index.has_value());
  145. auto next_index = m_queue.m_driver_index_shadow % m_queue.m_queue_size;
  146. m_queue.m_driver->rings[next_index] = m_start_of_chain_index.value();
  147. m_queue.m_driver_index_shadow++;
  148. full_memory_barrier();
  149. m_queue.m_driver->index = m_queue.m_driver_index_shadow;
  150. // Reset internal chain state
  151. m_start_of_chain_index = m_end_of_chain_index = {};
  152. m_chain_has_writable_pages = false;
  153. m_chain_length = 0;
  154. }
  155. void VirtIOQueueChain::release_buffer_slots_to_queue()
  156. {
  157. VERIFY(m_queue.lock().is_locked());
  158. if (m_start_of_chain_index.has_value()) {
  159. // Add the currently stored chain back to the queue's free pool
  160. m_queue.reclaim_buffer_chain(m_start_of_chain_index.value(), m_end_of_chain_index.value(), m_chain_length);
  161. // Reset internal chain state
  162. m_start_of_chain_index = m_end_of_chain_index = {};
  163. m_chain_has_writable_pages = false;
  164. m_chain_length = 0;
  165. }
  166. }
  167. }