Console.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <Kernel/Bus/VirtIO/Console.h>
  8. #include <Kernel/Sections.h>
  9. #include <Kernel/WorkQueue.h>
  10. namespace Kernel::VirtIO {
  11. unsigned Console::next_device_id = 0;
  12. UNMAP_AFTER_INIT NonnullRefPtr<Console> Console::must_create(PCI::Address address)
  13. {
  14. return adopt_ref_if_nonnull(new Console(address)).release_nonnull();
  15. }
  16. UNMAP_AFTER_INIT void Console::initialize()
  17. {
  18. Device::initialize();
  19. if (auto cfg = get_config(ConfigurationType::Device)) {
  20. bool success = negotiate_features([&](u64 supported_features) {
  21. u64 negotiated = 0;
  22. if (is_feature_set(supported_features, VIRTIO_CONSOLE_F_SIZE))
  23. dbgln("VirtIO::Console: Console size is not yet supported!");
  24. if (is_feature_set(supported_features, VIRTIO_CONSOLE_F_MULTIPORT))
  25. negotiated |= VIRTIO_CONSOLE_F_MULTIPORT;
  26. return negotiated;
  27. });
  28. if (success) {
  29. u32 max_nr_ports = 0;
  30. u16 cols = 0, rows = 0;
  31. read_config_atomic([&]() {
  32. if (is_feature_accepted(VIRTIO_CONSOLE_F_SIZE)) {
  33. cols = config_read16(*cfg, 0x0);
  34. rows = config_read16(*cfg, 0x2);
  35. }
  36. if (is_feature_accepted(VIRTIO_CONSOLE_F_MULTIPORT)) {
  37. max_nr_ports = config_read32(*cfg, 0x4);
  38. m_ports.resize(max_nr_ports);
  39. }
  40. });
  41. dbgln("VirtIO::Console: cols: {}, rows: {}, max nr ports {}", cols, rows, max_nr_ports);
  42. // Base receiveq/transmitq for port0 + optional control queues and 2 per every additional port
  43. success = setup_queues(2 + max_nr_ports > 0 ? 2 + 2 * max_nr_ports : 0);
  44. }
  45. if (success) {
  46. finish_init();
  47. if (is_feature_accepted(VIRTIO_CONSOLE_F_MULTIPORT))
  48. setup_multiport();
  49. else {
  50. auto port = make_ref_counted<VirtIO::ConsolePort>(0u, *this);
  51. port->init_receive_buffer({});
  52. m_ports.append(port);
  53. }
  54. }
  55. }
  56. }
  57. UNMAP_AFTER_INIT Console::Console(PCI::Address address)
  58. : VirtIO::Device(address)
  59. , m_device_id(next_device_id++)
  60. {
  61. }
  62. bool Console::handle_device_config_change()
  63. {
  64. dbgln("VirtIO::Console: Handle device config change");
  65. return true;
  66. }
  67. void Console::handle_queue_update(u16 queue_index)
  68. {
  69. dbgln_if(VIRTIO_DEBUG, "VirtIO::Console: Handle queue update {}", queue_index);
  70. if (queue_index == CONTROL_RECEIVEQ) {
  71. SpinlockLocker ringbuffer_lock(m_control_receive_buffer->lock());
  72. auto& queue = get_queue(CONTROL_RECEIVEQ);
  73. SpinlockLocker queue_lock(queue.lock());
  74. size_t used;
  75. QueueChain popped_chain = queue.pop_used_buffer_chain(used);
  76. while (!popped_chain.is_empty()) {
  77. popped_chain.for_each([&](auto addr, auto) {
  78. auto offset = addr.as_ptr() - m_control_receive_buffer->start_of_region().as_ptr();
  79. auto* message = reinterpret_cast<ControlMessage*>(m_control_receive_buffer->vaddr().offset(offset).as_ptr());
  80. process_control_message(*message);
  81. });
  82. supply_chain_and_notify(CONTROL_RECEIVEQ, popped_chain);
  83. popped_chain = queue.pop_used_buffer_chain(used);
  84. }
  85. } else if (queue_index == CONTROL_TRANSMITQ) {
  86. SpinlockLocker ringbuffer_lock(m_control_transmit_buffer->lock());
  87. auto& queue = get_queue(CONTROL_TRANSMITQ);
  88. SpinlockLocker queue_lock(queue.lock());
  89. size_t used;
  90. QueueChain popped_chain = queue.pop_used_buffer_chain(used);
  91. auto number_of_messages = 0;
  92. do {
  93. popped_chain.for_each([this](PhysicalAddress address, size_t length) {
  94. m_control_transmit_buffer->reclaim_space(address, length);
  95. });
  96. popped_chain.release_buffer_slots_to_queue();
  97. popped_chain = queue.pop_used_buffer_chain(used);
  98. number_of_messages++;
  99. } while (!popped_chain.is_empty());
  100. m_control_wait_queue.wake_n(number_of_messages);
  101. } else {
  102. u32 port_index = queue_index < 2 ? 0 : (queue_index - 2) / 2;
  103. if (port_index >= m_ports.size() || !m_ports.at(port_index)) {
  104. dbgln("Invalid queue_index {}", queue_index);
  105. return;
  106. }
  107. m_ports.at(port_index)->handle_queue_update({}, queue_index);
  108. }
  109. }
  110. void Console::setup_multiport()
  111. {
  112. m_control_receive_buffer = make<Memory::RingBuffer>("VirtIOConsole control receive queue", CONTROL_BUFFER_SIZE);
  113. m_control_transmit_buffer = make<Memory::RingBuffer>("VirtIOConsole control transmit queue", CONTROL_BUFFER_SIZE);
  114. auto& queue = get_queue(CONTROL_RECEIVEQ);
  115. SpinlockLocker queue_lock(queue.lock());
  116. QueueChain chain(queue);
  117. auto offset = 0ul;
  118. while (offset < CONTROL_BUFFER_SIZE) {
  119. auto buffer_start = m_control_receive_buffer->start_of_region().offset(offset);
  120. auto did_add_buffer = chain.add_buffer_to_chain(buffer_start, CONTROL_MESSAGE_SIZE, BufferType::DeviceWritable);
  121. VERIFY(did_add_buffer);
  122. offset += CONTROL_MESSAGE_SIZE;
  123. supply_chain_and_notify(CONTROL_RECEIVEQ, chain);
  124. }
  125. ControlMessage ready_event {
  126. .id = 0, // Unused
  127. .event = (u16)ControlEvent::DeviceReady,
  128. .value = (u16)ControlMessage::Status::Success
  129. };
  130. write_control_message(ready_event);
  131. }
  132. void Console::process_control_message(ControlMessage message)
  133. {
  134. switch (message.event) {
  135. case (u16)ControlEvent::DeviceAdd: {
  136. g_io_work->queue([message, this]() -> void {
  137. u32 id = message.id;
  138. if (id >= m_ports.size()) {
  139. dbgln("Device provided an invalid port number {}. max_nr_ports: {}", id, m_ports.size());
  140. return;
  141. } else if (!m_ports.at(id).is_null()) {
  142. dbgln("Device tried to add port {} which was already added!", id);
  143. return;
  144. }
  145. m_ports.at(id) = make_ref_counted<VirtIO::ConsolePort>(id, *this);
  146. ControlMessage ready_event {
  147. .id = static_cast<u32>(id),
  148. .event = (u16)ControlEvent::PortReady,
  149. .value = (u16)ControlMessage::Status::Success
  150. };
  151. write_control_message(ready_event);
  152. });
  153. break;
  154. }
  155. case (u16)ControlEvent::ConsolePort:
  156. case (u16)ControlEvent::PortOpen: {
  157. if (message.id >= m_ports.size()) {
  158. dbgln("Device provided an invalid port number {}. max_nr_ports: {}", message.id, m_ports.size());
  159. return;
  160. } else if (m_ports.at(message.id).is_null()) {
  161. dbgln("Device tried to open port {} which was not added!", message.id);
  162. return;
  163. }
  164. if (message.value == (u16)ControlMessage::PortStatus::Open) {
  165. auto is_open = m_ports.at(message.id)->is_open();
  166. if (!is_open) {
  167. m_ports.at(message.id)->set_open({}, true);
  168. send_open_control_message(message.id, true);
  169. }
  170. } else if (message.value == (u16)ControlMessage::PortStatus::Close) {
  171. m_ports.at(message.id)->set_open({}, false);
  172. } else {
  173. dbgln("Device specified invalid value {}. Must be 0 or 1.", message.value);
  174. }
  175. break;
  176. }
  177. default:
  178. dbgln("Unhandled message event {}!", message.event);
  179. }
  180. }
  181. void Console::write_control_message(ControlMessage message)
  182. {
  183. SpinlockLocker ringbuffer_lock(m_control_transmit_buffer->lock());
  184. PhysicalAddress start_of_chunk;
  185. size_t length_of_chunk;
  186. auto data = UserOrKernelBuffer::for_kernel_buffer((u8*)&message);
  187. while (!m_control_transmit_buffer->copy_data_in(data, 0, sizeof(message), start_of_chunk, length_of_chunk)) {
  188. ringbuffer_lock.unlock();
  189. m_control_wait_queue.wait_forever();
  190. ringbuffer_lock.lock();
  191. }
  192. auto& queue = get_queue(CONTROL_TRANSMITQ);
  193. SpinlockLocker queue_lock(queue.lock());
  194. QueueChain chain(queue);
  195. bool did_add_buffer = chain.add_buffer_to_chain(start_of_chunk, length_of_chunk, BufferType::DeviceReadable);
  196. VERIFY(did_add_buffer);
  197. supply_chain_and_notify(CONTROL_TRANSMITQ, chain);
  198. }
  199. void Console::send_open_control_message(unsigned port_number, bool open)
  200. {
  201. ControlMessage port_open {
  202. .id = static_cast<u32>(port_number),
  203. .event = (u16)ControlEvent::PortOpen,
  204. .value = open
  205. };
  206. write_control_message(port_open);
  207. }
  208. }