DisplayConnector.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/FileSystem/SysFS/Subsystems/DeviceIdentifiers/CharacterDevicesDirectory.h>
  7. #include <Kernel/FileSystem/SysFS/Subsystems/Devices/Graphics/DisplayConnector/DeviceDirectory.h>
  8. #include <Kernel/FileSystem/SysFS/Subsystems/Devices/Graphics/DisplayConnector/Directory.h>
  9. #include <Kernel/Graphics/DisplayConnector.h>
  10. #include <Kernel/Graphics/GraphicsManagement.h>
  11. #include <Kernel/Memory/MemoryManager.h>
  12. #include <LibC/sys/ioctl_numbers.h>
  13. namespace Kernel {
  14. DisplayConnector::DisplayConnector(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size, bool enable_write_combine_optimization)
  15. : CharacterDevice(226, GraphicsManagement::the().allocate_minor_device_number())
  16. , m_enable_write_combine_optimization(enable_write_combine_optimization)
  17. , m_framebuffer_at_arbitrary_physical_range(false)
  18. , m_framebuffer_address(framebuffer_address)
  19. , m_framebuffer_resource_size(framebuffer_resource_size)
  20. {
  21. }
  22. DisplayConnector::DisplayConnector(size_t framebuffer_resource_size, bool enable_write_combine_optimization)
  23. : CharacterDevice(226, GraphicsManagement::the().allocate_minor_device_number())
  24. , m_enable_write_combine_optimization(enable_write_combine_optimization)
  25. , m_framebuffer_at_arbitrary_physical_range(true)
  26. , m_framebuffer_address({})
  27. , m_framebuffer_resource_size(framebuffer_resource_size)
  28. {
  29. }
  30. ErrorOr<Memory::Region*> DisplayConnector::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
  31. {
  32. VERIFY(m_shared_framebuffer_vmobject);
  33. if (offset != 0)
  34. return Error::from_errno(ENOTSUP);
  35. return process.address_space().allocate_region_with_vmobject(
  36. range,
  37. *m_shared_framebuffer_vmobject,
  38. 0,
  39. "Mapped Framebuffer"sv,
  40. prot,
  41. shared);
  42. }
  43. ErrorOr<size_t> DisplayConnector::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t)
  44. {
  45. return Error::from_errno(ENOTIMPL);
  46. }
  47. ErrorOr<size_t> DisplayConnector::write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t)
  48. {
  49. return Error::from_errno(ENOTIMPL);
  50. }
  51. void DisplayConnector::will_be_destroyed()
  52. {
  53. GraphicsManagement::the().detach_display_connector({}, *this);
  54. VERIFY(m_symlink_sysfs_component);
  55. before_will_be_destroyed_remove_symlink_from_device_identifier_directory();
  56. m_symlink_sysfs_component.clear();
  57. SysFSDisplayConnectorsDirectory::the().unplug({}, *m_sysfs_device_directory);
  58. before_will_be_destroyed_remove_from_device_management();
  59. }
  60. void DisplayConnector::after_inserting()
  61. {
  62. after_inserting_add_to_device_management();
  63. auto sysfs_display_connector_device_directory = DisplayConnectorSysFSDirectory::create(SysFSDisplayConnectorsDirectory::the(), *this);
  64. m_sysfs_device_directory = sysfs_display_connector_device_directory;
  65. SysFSDisplayConnectorsDirectory::the().plug({}, *sysfs_display_connector_device_directory);
  66. VERIFY(!m_symlink_sysfs_component);
  67. auto sys_fs_component = MUST(SysFSSymbolicLinkDeviceComponent::try_create(SysFSDeviceIdentifiersDirectory::the(), *this, *m_sysfs_device_directory));
  68. m_symlink_sysfs_component = sys_fs_component;
  69. after_inserting_add_symlink_to_device_identifier_directory();
  70. auto rounded_size = MUST(Memory::page_round_up(m_framebuffer_resource_size));
  71. if (!m_framebuffer_at_arbitrary_physical_range) {
  72. VERIFY(m_framebuffer_address.value().page_base() == m_framebuffer_address.value());
  73. m_shared_framebuffer_vmobject = MUST(Memory::SharedFramebufferVMObject::try_create_for_physical_range(m_framebuffer_address.value(), rounded_size));
  74. m_framebuffer_region = MUST(MM.allocate_kernel_region(m_framebuffer_address.value().page_base(), rounded_size, "Framebuffer"sv, Memory::Region::Access::ReadWrite));
  75. } else {
  76. m_shared_framebuffer_vmobject = MUST(Memory::SharedFramebufferVMObject::try_create_at_arbitrary_physical_range(rounded_size));
  77. m_framebuffer_region = MUST(MM.allocate_kernel_region_with_vmobject(m_shared_framebuffer_vmobject->real_writes_framebuffer_vmobject(), rounded_size, "Framebuffer"sv, Memory::Region::Access::ReadWrite));
  78. }
  79. m_framebuffer_data = m_framebuffer_region->vaddr().as_ptr();
  80. m_fake_writes_framebuffer_region = MUST(MM.allocate_kernel_region_with_vmobject(m_shared_framebuffer_vmobject->fake_writes_framebuffer_vmobject(), rounded_size, "Fake Writes Framebuffer"sv, Memory::Region::Access::ReadWrite));
  81. GraphicsManagement::the().attach_new_display_connector({}, *this);
  82. if (m_enable_write_combine_optimization) {
  83. [[maybe_unused]] auto result = m_framebuffer_region->set_write_combine(true);
  84. }
  85. }
  86. bool DisplayConnector::console_mode() const
  87. {
  88. VERIFY(m_control_lock.is_locked());
  89. return m_console_mode;
  90. }
  91. void DisplayConnector::set_display_mode(Badge<GraphicsManagement>, DisplayMode mode)
  92. {
  93. SpinlockLocker locker(m_control_lock);
  94. {
  95. SpinlockLocker locker(m_modeset_lock);
  96. [[maybe_unused]] auto result = set_y_offset(0);
  97. }
  98. m_console_mode = mode == DisplayMode::Console ? true : false;
  99. if (m_console_mode) {
  100. VERIFY(m_framebuffer_region->size() == m_fake_writes_framebuffer_region->size());
  101. memcpy(m_fake_writes_framebuffer_region->vaddr().as_ptr(), m_framebuffer_region->vaddr().as_ptr(), m_framebuffer_region->size());
  102. m_shared_framebuffer_vmobject->switch_to_fake_sink_framebuffer_writes({});
  103. enable_console();
  104. } else {
  105. disable_console();
  106. m_shared_framebuffer_vmobject->switch_to_real_framebuffer_writes({});
  107. VERIFY(m_framebuffer_region->size() == m_fake_writes_framebuffer_region->size());
  108. memcpy(m_framebuffer_region->vaddr().as_ptr(), m_fake_writes_framebuffer_region->vaddr().as_ptr(), m_framebuffer_region->size());
  109. }
  110. }
  111. ErrorOr<void> DisplayConnector::initialize_edid_for_generic_monitor(Optional<Array<u8, 3>> possible_manufacturer_id_string)
  112. {
  113. u8 raw_manufacturer_id[2] = { 0x0, 0x0 };
  114. if (possible_manufacturer_id_string.has_value()) {
  115. Array<u8, 3> manufacturer_id_string = possible_manufacturer_id_string.release_value();
  116. u8 byte1 = (((static_cast<u8>(manufacturer_id_string[0]) - '@') & 0x1f) << 2) | (((static_cast<u8>(manufacturer_id_string[1]) - '@') >> 3) & 3);
  117. u8 byte2 = ((static_cast<u8>(manufacturer_id_string[2]) - '@') & 0x1f) | (((static_cast<u8>(manufacturer_id_string[1]) - '@') << 5) & 0xe0);
  118. Array<u8, 2> manufacturer_id_string_packed_bytes = { byte1, byte2 };
  119. raw_manufacturer_id[0] = manufacturer_id_string_packed_bytes[1];
  120. raw_manufacturer_id[1] = manufacturer_id_string_packed_bytes[0];
  121. }
  122. Array<u8, 128> virtual_monitor_edid = {
  123. 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
  124. raw_manufacturer_id[1], raw_manufacturer_id[0], /* manufacturer */
  125. 0x00, 0x00, /* product code */
  126. 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
  127. 0x01, /* week of manufacture */
  128. 0x00, /* year of manufacture */
  129. 0x01, 0x03, /* EDID version */
  130. 0x80, /* capabilities - digital */
  131. 0x00, /* horiz. res in cm, zero for projectors */
  132. 0x00, /* vert. res in cm */
  133. 0x78, /* display gamma (120 == 2.2). */
  134. 0xEE, /* features (standby, suspend, off, RGB, std */
  135. /* colour space, preferred timing mode) */
  136. 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
  137. /* chromaticity for standard colour space. */
  138. 0x00, 0x00, 0x00, /* no default timings */
  139. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  140. 0x01, 0x01,
  141. 0x01, 0x01, 0x01, 0x01, /* no standard timings */
  142. 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
  143. 0x02, 0x02,
  144. /* descriptor block 1 goes below */
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. /* descriptor block 2, monitor ranges */
  147. 0x00, 0x00, 0x00, 0xFD, 0x00,
  148. 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
  149. 0x20, 0x20,
  150. /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
  151. 0x20,
  152. /* descriptor block 3, monitor name */
  153. 0x00, 0x00, 0x00, 0xFC, 0x00,
  154. 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S', 'c', 'r', 'e', 'e', 'n',
  155. /* descriptor block 4: dummy data */
  156. 0x00, 0x00, 0x00, 0x10, 0x00,
  157. 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
  158. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  159. 0x20,
  160. 0x00, /* number of extensions */
  161. 0x00 /* checksum goes here */
  162. };
  163. // Note: Fix checksum to avoid warnings about checksum mismatch.
  164. size_t checksum = 0;
  165. // Note: Read all 127 bytes to add them to the checksum. Byte 128 is zeroed so
  166. // we could technically add it to the sum result, but it could lead to an error if it contained
  167. // a non-zero value, so we are not using it.
  168. for (size_t index = 0; index < sizeof(virtual_monitor_edid) - 1; index++)
  169. checksum += virtual_monitor_edid[index];
  170. virtual_monitor_edid[127] = 0x100 - checksum;
  171. set_edid_bytes(virtual_monitor_edid);
  172. return {};
  173. }
  174. void DisplayConnector::set_edid_bytes(Array<u8, 128> const& edid_bytes)
  175. {
  176. memcpy((u8*)m_edid_bytes, edid_bytes.data(), sizeof(m_edid_bytes));
  177. if (auto parsed_edid = EDID::Parser::from_bytes({ m_edid_bytes, sizeof(m_edid_bytes) }); !parsed_edid.is_error()) {
  178. m_edid_parser = parsed_edid.release_value();
  179. m_edid_valid = true;
  180. } else {
  181. dmesgln("DisplayConnector: Print offending EDID");
  182. for (size_t x = 0; x < 128; x = x + 16) {
  183. dmesgln("{:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x}",
  184. m_edid_bytes[x], m_edid_bytes[x + 1], m_edid_bytes[x + 2], m_edid_bytes[x + 3],
  185. m_edid_bytes[x + 4], m_edid_bytes[x + 5], m_edid_bytes[x + 6], m_edid_bytes[x + 7],
  186. m_edid_bytes[x + 8], m_edid_bytes[x + 9], m_edid_bytes[x + 10], m_edid_bytes[x + 11],
  187. m_edid_bytes[x + 12], m_edid_bytes[x + 13], m_edid_bytes[x + 14], m_edid_bytes[x + 15]);
  188. }
  189. dmesgln("DisplayConnector: Parsing EDID failed: {}", parsed_edid.error());
  190. }
  191. }
  192. ErrorOr<void> DisplayConnector::flush_rectangle(size_t, FBRect const&)
  193. {
  194. return Error::from_errno(ENOTSUP);
  195. }
  196. DisplayConnector::ModeSetting DisplayConnector::current_mode_setting() const
  197. {
  198. SpinlockLocker locker(m_modeset_lock);
  199. return m_current_mode_setting;
  200. }
  201. ErrorOr<ByteBuffer> DisplayConnector::get_edid() const
  202. {
  203. if (!m_edid_valid)
  204. return Error::from_errno(ENOTIMPL);
  205. return ByteBuffer::copy(m_edid_bytes, sizeof(m_edid_bytes));
  206. }
  207. ErrorOr<void> DisplayConnector::ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg)
  208. {
  209. TRY(Process::current().require_promise(Pledge::video));
  210. switch (request) {
  211. case GRAPHICS_IOCTL_GET_PROPERTIES: {
  212. auto user_properties = static_ptr_cast<GraphicsConnectorProperties*>(arg);
  213. GraphicsConnectorProperties properties {};
  214. properties.flushing_support = flush_support();
  215. properties.doublebuffer_support = double_framebuffering_capable();
  216. properties.partial_flushing_support = partial_flush_support();
  217. properties.refresh_rate_support = refresh_rate_support();
  218. return copy_to_user(user_properties, &properties);
  219. }
  220. case GRAPHICS_IOCTL_GET_HEAD_MODE_SETTING: {
  221. auto user_head_mode_setting = static_ptr_cast<GraphicsHeadModeSetting*>(arg);
  222. GraphicsHeadModeSetting head_mode_setting {};
  223. TRY(copy_from_user(&head_mode_setting, user_head_mode_setting));
  224. {
  225. SpinlockLocker control_locker(m_control_lock);
  226. head_mode_setting.horizontal_stride = m_current_mode_setting.horizontal_stride;
  227. head_mode_setting.pixel_clock_in_khz = m_current_mode_setting.pixel_clock_in_khz;
  228. head_mode_setting.horizontal_active = m_current_mode_setting.horizontal_active;
  229. head_mode_setting.horizontal_front_porch_pixels = m_current_mode_setting.horizontal_front_porch_pixels;
  230. head_mode_setting.horizontal_sync_time_pixels = m_current_mode_setting.horizontal_sync_time_pixels;
  231. head_mode_setting.horizontal_blank_pixels = m_current_mode_setting.horizontal_blank_pixels;
  232. head_mode_setting.vertical_active = m_current_mode_setting.vertical_active;
  233. head_mode_setting.vertical_front_porch_lines = m_current_mode_setting.vertical_front_porch_lines;
  234. head_mode_setting.vertical_sync_time_lines = m_current_mode_setting.vertical_sync_time_lines;
  235. head_mode_setting.vertical_blank_lines = m_current_mode_setting.vertical_blank_lines;
  236. head_mode_setting.horizontal_offset = m_current_mode_setting.horizontal_offset;
  237. head_mode_setting.vertical_offset = m_current_mode_setting.vertical_offset;
  238. }
  239. return copy_to_user(user_head_mode_setting, &head_mode_setting);
  240. }
  241. case GRAPHICS_IOCTL_SET_HEAD_MODE_SETTING: {
  242. auto user_mode_setting = static_ptr_cast<GraphicsHeadModeSetting const*>(arg);
  243. auto head_mode_setting = TRY(copy_typed_from_user(user_mode_setting));
  244. if (head_mode_setting.horizontal_stride < 0)
  245. return Error::from_errno(EINVAL);
  246. if (head_mode_setting.pixel_clock_in_khz < 0)
  247. return Error::from_errno(EINVAL);
  248. if (head_mode_setting.horizontal_active < 0)
  249. return Error::from_errno(EINVAL);
  250. if (head_mode_setting.horizontal_front_porch_pixels < 0)
  251. return Error::from_errno(EINVAL);
  252. if (head_mode_setting.horizontal_sync_time_pixels < 0)
  253. return Error::from_errno(EINVAL);
  254. if (head_mode_setting.horizontal_blank_pixels < 0)
  255. return Error::from_errno(EINVAL);
  256. if (head_mode_setting.vertical_active < 0)
  257. return Error::from_errno(EINVAL);
  258. if (head_mode_setting.vertical_front_porch_lines < 0)
  259. return Error::from_errno(EINVAL);
  260. if (head_mode_setting.vertical_sync_time_lines < 0)
  261. return Error::from_errno(EINVAL);
  262. if (head_mode_setting.vertical_blank_lines < 0)
  263. return Error::from_errno(EINVAL);
  264. if (head_mode_setting.horizontal_offset < 0)
  265. return Error::from_errno(EINVAL);
  266. if (head_mode_setting.vertical_offset < 0)
  267. return Error::from_errno(EINVAL);
  268. {
  269. SpinlockLocker control_locker(m_control_lock);
  270. ModeSetting requested_mode_setting;
  271. requested_mode_setting.horizontal_stride = 0;
  272. requested_mode_setting.pixel_clock_in_khz = head_mode_setting.pixel_clock_in_khz;
  273. requested_mode_setting.horizontal_active = head_mode_setting.horizontal_active;
  274. requested_mode_setting.horizontal_front_porch_pixels = head_mode_setting.horizontal_front_porch_pixels;
  275. requested_mode_setting.horizontal_sync_time_pixels = head_mode_setting.horizontal_sync_time_pixels;
  276. requested_mode_setting.horizontal_blank_pixels = head_mode_setting.horizontal_blank_pixels;
  277. requested_mode_setting.vertical_active = head_mode_setting.vertical_active;
  278. requested_mode_setting.vertical_front_porch_lines = head_mode_setting.vertical_front_porch_lines;
  279. requested_mode_setting.vertical_sync_time_lines = head_mode_setting.vertical_sync_time_lines;
  280. requested_mode_setting.vertical_blank_lines = head_mode_setting.vertical_blank_lines;
  281. requested_mode_setting.horizontal_offset = head_mode_setting.horizontal_offset;
  282. requested_mode_setting.vertical_offset = head_mode_setting.vertical_offset;
  283. TRY(set_mode_setting(requested_mode_setting));
  284. }
  285. return {};
  286. }
  287. case GRAPHICS_IOCTL_SET_SAFE_HEAD_MODE_SETTING: {
  288. SpinlockLocker control_locker(m_control_lock);
  289. TRY(set_safe_mode_setting());
  290. return {};
  291. }
  292. case GRAPHICS_IOCTL_SET_HEAD_VERTICAL_OFFSET_BUFFER: {
  293. // FIXME: We silently ignore the request if we are in console mode.
  294. // WindowServer is not ready yet to handle errors such as EBUSY currently.
  295. SpinlockLocker control_locker(m_control_lock);
  296. if (console_mode()) {
  297. return {};
  298. }
  299. auto user_head_vertical_buffer_offset = static_ptr_cast<GraphicsHeadVerticalOffset const*>(arg);
  300. auto head_vertical_buffer_offset = TRY(copy_typed_from_user(user_head_vertical_buffer_offset));
  301. SpinlockLocker locker(m_modeset_lock);
  302. if (head_vertical_buffer_offset.offsetted < 0 || head_vertical_buffer_offset.offsetted > 1)
  303. return Error::from_errno(EINVAL);
  304. TRY(set_y_offset(head_vertical_buffer_offset.offsetted == 0 ? 0 : m_current_mode_setting.vertical_active));
  305. if (head_vertical_buffer_offset.offsetted == 0)
  306. m_vertical_offsetted = false;
  307. else
  308. m_vertical_offsetted = true;
  309. return {};
  310. }
  311. case GRAPHICS_IOCTL_GET_HEAD_VERTICAL_OFFSET_BUFFER: {
  312. auto user_head_vertical_buffer_offset = static_ptr_cast<GraphicsHeadVerticalOffset*>(arg);
  313. GraphicsHeadVerticalOffset head_vertical_buffer_offset {};
  314. TRY(copy_from_user(&head_vertical_buffer_offset, user_head_vertical_buffer_offset));
  315. head_vertical_buffer_offset.offsetted = m_vertical_offsetted;
  316. return copy_to_user(user_head_vertical_buffer_offset, &head_vertical_buffer_offset);
  317. }
  318. case GRAPHICS_IOCTL_FLUSH_HEAD_BUFFERS: {
  319. if (console_mode())
  320. return {};
  321. if (!partial_flush_support())
  322. return Error::from_errno(ENOTSUP);
  323. MutexLocker locker(m_flushing_lock);
  324. auto user_flush_rects = static_ptr_cast<FBFlushRects const*>(arg);
  325. auto flush_rects = TRY(copy_typed_from_user(user_flush_rects));
  326. if (Checked<unsigned>::multiplication_would_overflow(flush_rects.count, sizeof(FBRect)))
  327. return Error::from_errno(EFAULT);
  328. if (flush_rects.count > 0) {
  329. for (unsigned i = 0; i < flush_rects.count; i++) {
  330. FBRect user_dirty_rect;
  331. TRY(copy_from_user(&user_dirty_rect, &flush_rects.rects[i]));
  332. {
  333. SpinlockLocker control_locker(m_control_lock);
  334. if (console_mode()) {
  335. return {};
  336. }
  337. TRY(flush_rectangle(flush_rects.buffer_index, user_dirty_rect));
  338. }
  339. }
  340. }
  341. return {};
  342. };
  343. case GRAPHICS_IOCTL_FLUSH_HEAD: {
  344. // FIXME: We silently ignore the request if we are in console mode.
  345. // WindowServer is not ready yet to handle errors such as EBUSY currently.
  346. MutexLocker locker(m_flushing_lock);
  347. SpinlockLocker control_locker(m_control_lock);
  348. if (console_mode()) {
  349. return {};
  350. }
  351. if (!flush_support())
  352. return Error::from_errno(ENOTSUP);
  353. TRY(flush_first_surface());
  354. return {};
  355. }
  356. }
  357. return EINVAL;
  358. }
  359. }