Console.cpp 8.7 KB

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