NativeDisplayConnector.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Arch/Delay.h>
  7. #include <Kernel/Bus/PCI/API.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/Devices/DeviceManagement.h>
  10. #include <Kernel/Graphics/Console/ContiguousFramebufferConsole.h>
  11. #include <Kernel/Graphics/GraphicsManagement.h>
  12. #include <Kernel/Graphics/Intel/NativeDisplayConnector.h>
  13. #include <Kernel/Memory/Region.h>
  14. namespace Kernel {
  15. namespace IntelGraphics {
  16. #define DDC2_I2C_ADDRESS 0x50
  17. struct PLLSettings {
  18. bool is_valid() const { return (n != 0 && m1 != 0 && m2 != 0 && p1 != 0 && p2 != 0); }
  19. u64 compute_dot_clock(u64 refclock) const
  20. {
  21. return (refclock * (5 * m1 + m2) / n) / (p1 * p2);
  22. }
  23. u64 compute_vco(u64 refclock) const
  24. {
  25. return refclock * (5 * m1 + m2) / n;
  26. }
  27. u64 compute_m() const
  28. {
  29. return 5 * m1 + m2;
  30. }
  31. u64 compute_p() const
  32. {
  33. return p1 * p2;
  34. }
  35. u64 n { 0 };
  36. u64 m1 { 0 };
  37. u64 m2 { 0 };
  38. u64 p1 { 0 };
  39. u64 p2 { 0 };
  40. };
  41. static constexpr PLLMaxSettings G35Limits {
  42. { 20'000'000, 400'000'000 }, // values in Hz, dot_clock
  43. { 1'400'000'000, 2'800'000'000 }, // values in Hz, VCO
  44. { 3, 8 }, // n
  45. { 70, 120 }, // m
  46. { 10, 20 }, // m1
  47. { 5, 9 }, // m2
  48. { 5, 80 }, // p
  49. { 1, 8 }, // p1
  50. { 5, 10 } // p2
  51. };
  52. }
  53. static Graphics::Modesetting calculate_modesetting_from_edid(EDID::Parser& edid, size_t index)
  54. {
  55. auto details = edid.detailed_timing(index).release_value();
  56. Graphics::Modesetting mode;
  57. VERIFY(details.pixel_clock_khz());
  58. mode.pixel_clock_in_khz = details.pixel_clock_khz();
  59. size_t horizontal_active = details.horizontal_addressable_pixels();
  60. size_t horizontal_sync_offset = details.horizontal_front_porch_pixels();
  61. mode.horizontal.active = horizontal_active;
  62. mode.horizontal.sync_start = horizontal_active + horizontal_sync_offset;
  63. mode.horizontal.sync_end = horizontal_active + horizontal_sync_offset + details.horizontal_sync_pulse_width_pixels();
  64. mode.horizontal.total = horizontal_active + details.horizontal_blanking_pixels();
  65. size_t vertical_active = details.vertical_addressable_lines();
  66. size_t vertical_sync_offset = details.vertical_front_porch_lines();
  67. mode.vertical.active = vertical_active;
  68. mode.vertical.sync_start = vertical_active + vertical_sync_offset;
  69. mode.vertical.sync_end = vertical_active + vertical_sync_offset + details.vertical_sync_pulse_width_lines();
  70. mode.vertical.total = vertical_active + details.vertical_blanking_lines();
  71. return mode;
  72. }
  73. static bool check_pll_settings(IntelGraphics::PLLSettings const& settings, size_t reference_clock, IntelGraphics::PLLMaxSettings const& limits)
  74. {
  75. if (settings.n < limits.n.min || settings.n > limits.n.max) {
  76. dbgln_if(INTEL_GRAPHICS_DEBUG, "N is invalid {}", settings.n);
  77. return false;
  78. }
  79. if (settings.m1 < limits.m1.min || settings.m1 > limits.m1.max) {
  80. dbgln_if(INTEL_GRAPHICS_DEBUG, "m1 is invalid {}", settings.m1);
  81. return false;
  82. }
  83. if (settings.m2 < limits.m2.min || settings.m2 > limits.m2.max) {
  84. dbgln_if(INTEL_GRAPHICS_DEBUG, "m2 is invalid {}", settings.m2);
  85. return false;
  86. }
  87. if (settings.p1 < limits.p1.min || settings.p1 > limits.p1.max) {
  88. dbgln_if(INTEL_GRAPHICS_DEBUG, "p1 is invalid {}", settings.p1);
  89. return false;
  90. }
  91. if (settings.m1 <= settings.m2) {
  92. dbgln_if(INTEL_GRAPHICS_DEBUG, "m2 is invalid {} as it is bigger than m1 {}", settings.m2, settings.m1);
  93. return false;
  94. }
  95. auto m = settings.compute_m();
  96. auto p = settings.compute_p();
  97. if (m < limits.m.min || m > limits.m.max) {
  98. dbgln_if(INTEL_GRAPHICS_DEBUG, "m invalid {}", m);
  99. return false;
  100. }
  101. if (p < limits.p.min || p > limits.p.max) {
  102. dbgln_if(INTEL_GRAPHICS_DEBUG, "p invalid {}", p);
  103. return false;
  104. }
  105. auto dot = settings.compute_dot_clock(reference_clock);
  106. auto vco = settings.compute_vco(reference_clock);
  107. if (dot < limits.dot_clock.min || dot > limits.dot_clock.max) {
  108. dbgln_if(INTEL_GRAPHICS_DEBUG, "Dot clock invalid {}", dot);
  109. return false;
  110. }
  111. if (vco < limits.vco.min || vco > limits.vco.max) {
  112. dbgln_if(INTEL_GRAPHICS_DEBUG, "VCO clock invalid {}", vco);
  113. return false;
  114. }
  115. return true;
  116. }
  117. static size_t find_absolute_difference(u64 target_frequency, u64 checked_frequency)
  118. {
  119. if (target_frequency >= checked_frequency)
  120. return target_frequency - checked_frequency;
  121. return checked_frequency - target_frequency;
  122. }
  123. Optional<IntelGraphics::PLLSettings> IntelNativeDisplayConnector::create_pll_settings(u64 target_frequency, u64 reference_clock, IntelGraphics::PLLMaxSettings const& limits)
  124. {
  125. IntelGraphics::PLLSettings settings;
  126. IntelGraphics::PLLSettings best_settings;
  127. // FIXME: Is this correct for all Intel Native graphics cards?
  128. settings.p2 = 10;
  129. dbgln_if(INTEL_GRAPHICS_DEBUG, "Check PLL settings for ref clock of {} Hz, for target of {} Hz", reference_clock, target_frequency);
  130. u64 best_difference = 0xffffffff;
  131. for (settings.n = limits.n.min; settings.n <= limits.n.max; ++settings.n) {
  132. for (settings.m1 = limits.m1.max; settings.m1 >= limits.m1.min; --settings.m1) {
  133. for (settings.m2 = limits.m2.max; settings.m2 >= limits.m2.min; --settings.m2) {
  134. for (settings.p1 = limits.p1.max; settings.p1 >= limits.p1.min; --settings.p1) {
  135. dbgln_if(INTEL_GRAPHICS_DEBUG, "Check PLL settings for {} {} {} {} {}", settings.n, settings.m1, settings.m2, settings.p1, settings.p2);
  136. if (!check_pll_settings(settings, reference_clock, limits))
  137. continue;
  138. auto current_dot_clock = settings.compute_dot_clock(reference_clock);
  139. if (current_dot_clock == target_frequency)
  140. return settings;
  141. auto difference = find_absolute_difference(target_frequency, current_dot_clock);
  142. if (difference < best_difference && (current_dot_clock > target_frequency)) {
  143. best_settings = settings;
  144. best_difference = difference;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. if (best_settings.is_valid())
  151. return best_settings;
  152. return {};
  153. }
  154. ErrorOr<NonnullLockRefPtr<IntelNativeDisplayConnector>> IntelNativeDisplayConnector::try_create(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size, PhysicalAddress registers_region_address, size_t registers_region_length)
  155. {
  156. auto registers_region = TRY(MM.allocate_kernel_region(PhysicalAddress(registers_region_address), registers_region_length, "Intel Native Graphics Registers"sv, Memory::Region::Access::ReadWrite));
  157. // FIXME: Try to put the address as parameter to this function to allow creating this DisplayConnector for many generations...
  158. auto gmbus_connector = TRY(GMBusConnector::create_with_physical_address(registers_region_address.offset(to_underlying(IntelGraphics::RegisterIndex::GMBusClock))));
  159. auto connector = TRY(DeviceManagement::try_create_device<IntelNativeDisplayConnector>(framebuffer_address, framebuffer_resource_size, move(gmbus_connector), move(registers_region)));
  160. TRY(connector->initialize_gmbus_settings_and_read_edid());
  161. // Note: This is very important to set the resolution to something safe so we
  162. // can create a framebuffer console with valid resolution.
  163. {
  164. SpinlockLocker control_lock(connector->m_control_lock);
  165. TRY(connector->set_safe_mode_setting());
  166. }
  167. TRY(connector->create_attached_framebuffer_console());
  168. return connector;
  169. }
  170. ErrorOr<void> IntelNativeDisplayConnector::initialize_gmbus_settings_and_read_edid()
  171. {
  172. gmbus_read_edid();
  173. return {};
  174. }
  175. ErrorOr<void> IntelNativeDisplayConnector::set_y_offset(size_t)
  176. {
  177. return Error::from_errno(ENOTIMPL);
  178. }
  179. ErrorOr<void> IntelNativeDisplayConnector::unblank()
  180. {
  181. return Error::from_errno(ENOTIMPL);
  182. }
  183. void IntelNativeDisplayConnector::enable_console()
  184. {
  185. VERIFY(m_control_lock.is_locked());
  186. VERIFY(m_framebuffer_console);
  187. m_framebuffer_console->enable();
  188. }
  189. void IntelNativeDisplayConnector::disable_console()
  190. {
  191. VERIFY(m_control_lock.is_locked());
  192. VERIFY(m_framebuffer_console);
  193. m_framebuffer_console->disable();
  194. }
  195. ErrorOr<void> IntelNativeDisplayConnector::flush_first_surface()
  196. {
  197. return Error::from_errno(ENOTSUP);
  198. }
  199. ErrorOr<void> IntelNativeDisplayConnector::create_attached_framebuffer_console()
  200. {
  201. m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(m_framebuffer_address.value(), m_current_mode_setting.horizontal_active, m_current_mode_setting.vertical_active, m_current_mode_setting.horizontal_stride);
  202. GraphicsManagement::the().set_console(*m_framebuffer_console);
  203. return {};
  204. }
  205. IntelNativeDisplayConnector::IntelNativeDisplayConnector(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size, NonnullOwnPtr<GMBusConnector> gmbus_connector, NonnullOwnPtr<Memory::Region> registers_region)
  206. : DisplayConnector(framebuffer_address, framebuffer_resource_size, true)
  207. , m_registers_region(move(registers_region))
  208. , m_gmbus_connector(move(gmbus_connector))
  209. {
  210. }
  211. ErrorOr<void> IntelNativeDisplayConnector::set_mode_setting(DisplayConnector::ModeSetting const&)
  212. {
  213. return Error::from_errno(ENOTIMPL);
  214. }
  215. ErrorOr<void> IntelNativeDisplayConnector::set_safe_mode_setting()
  216. {
  217. set_safe_crt_resolution();
  218. return {};
  219. }
  220. void IntelNativeDisplayConnector::enable_vga_plane()
  221. {
  222. VERIFY(m_control_lock.is_locked());
  223. VERIFY(m_modeset_lock.is_locked());
  224. }
  225. [[maybe_unused]] static StringView convert_register_index_to_string(IntelGraphics::RegisterIndex index)
  226. {
  227. switch (index) {
  228. case IntelGraphics::RegisterIndex::PipeAConf:
  229. return "PipeAConf"sv;
  230. case IntelGraphics::RegisterIndex::PipeBConf:
  231. return "PipeBConf"sv;
  232. case IntelGraphics::RegisterIndex::GMBusData:
  233. return "GMBusData"sv;
  234. case IntelGraphics::RegisterIndex::GMBusStatus:
  235. return "GMBusStatus"sv;
  236. case IntelGraphics::RegisterIndex::GMBusCommand:
  237. return "GMBusCommand"sv;
  238. case IntelGraphics::RegisterIndex::GMBusClock:
  239. return "GMBusClock"sv;
  240. case IntelGraphics::RegisterIndex::DisplayPlaneAControl:
  241. return "DisplayPlaneAControl"sv;
  242. case IntelGraphics::RegisterIndex::DisplayPlaneALinearOffset:
  243. return "DisplayPlaneALinearOffset"sv;
  244. case IntelGraphics::RegisterIndex::DisplayPlaneAStride:
  245. return "DisplayPlaneAStride"sv;
  246. case IntelGraphics::RegisterIndex::DisplayPlaneASurface:
  247. return "DisplayPlaneASurface"sv;
  248. case IntelGraphics::RegisterIndex::DPLLDivisorA0:
  249. return "DPLLDivisorA0"sv;
  250. case IntelGraphics::RegisterIndex::DPLLDivisorA1:
  251. return "DPLLDivisorA1"sv;
  252. case IntelGraphics::RegisterIndex::DPLLControlA:
  253. return "DPLLControlA"sv;
  254. case IntelGraphics::RegisterIndex::DPLLControlB:
  255. return "DPLLControlB"sv;
  256. case IntelGraphics::RegisterIndex::DPLLMultiplierA:
  257. return "DPLLMultiplierA"sv;
  258. case IntelGraphics::RegisterIndex::HTotalA:
  259. return "HTotalA"sv;
  260. case IntelGraphics::RegisterIndex::HBlankA:
  261. return "HBlankA"sv;
  262. case IntelGraphics::RegisterIndex::HSyncA:
  263. return "HSyncA"sv;
  264. case IntelGraphics::RegisterIndex::VTotalA:
  265. return "VTotalA"sv;
  266. case IntelGraphics::RegisterIndex::VBlankA:
  267. return "VBlankA"sv;
  268. case IntelGraphics::RegisterIndex::VSyncA:
  269. return "VSyncA"sv;
  270. case IntelGraphics::RegisterIndex::PipeASource:
  271. return "PipeASource"sv;
  272. case IntelGraphics::RegisterIndex::AnalogDisplayPort:
  273. return "AnalogDisplayPort"sv;
  274. case IntelGraphics::RegisterIndex::VGADisplayPlaneControl:
  275. return "VGADisplayPlaneControl"sv;
  276. default:
  277. VERIFY_NOT_REACHED();
  278. }
  279. }
  280. void IntelNativeDisplayConnector::write_to_register(IntelGraphics::RegisterIndex index, u32 value) const
  281. {
  282. VERIFY(m_control_lock.is_locked());
  283. SpinlockLocker lock(m_registers_lock);
  284. dbgln_if(INTEL_GRAPHICS_DEBUG, "Intel Graphics Display Connector:: Write to {} value of {:x}", convert_register_index_to_string(index), value);
  285. auto* reg = (u32 volatile*)m_registers_region->vaddr().offset(to_underlying(index)).as_ptr();
  286. *reg = value;
  287. }
  288. u32 IntelNativeDisplayConnector::read_from_register(IntelGraphics::RegisterIndex index) const
  289. {
  290. VERIFY(m_control_lock.is_locked());
  291. SpinlockLocker lock(m_registers_lock);
  292. auto* reg = (u32 volatile*)m_registers_region->vaddr().offset(to_underlying(index)).as_ptr();
  293. u32 value = *reg;
  294. dbgln_if(INTEL_GRAPHICS_DEBUG, "Intel Graphics Display Connector: Read from {} value of {:x}", convert_register_index_to_string(index), value);
  295. return value;
  296. }
  297. bool IntelNativeDisplayConnector::pipe_a_enabled() const
  298. {
  299. VERIFY(m_control_lock.is_locked());
  300. return read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 30);
  301. }
  302. bool IntelNativeDisplayConnector::pipe_b_enabled() const
  303. {
  304. VERIFY(m_control_lock.is_locked());
  305. return read_from_register(IntelGraphics::RegisterIndex::PipeBConf) & (1 << 30);
  306. }
  307. void IntelNativeDisplayConnector::gmbus_read_edid()
  308. {
  309. Array<u8, 128> crt_edid_bytes {};
  310. {
  311. SpinlockLocker control_lock(m_control_lock);
  312. MUST(m_gmbus_connector->write(Graphics::ddc2_i2c_address, 0));
  313. MUST(m_gmbus_connector->read(Graphics::ddc2_i2c_address, crt_edid_bytes.data(), crt_edid_bytes.size()));
  314. }
  315. set_edid_bytes(crt_edid_bytes);
  316. }
  317. bool IntelNativeDisplayConnector::is_resolution_valid(size_t, size_t)
  318. {
  319. VERIFY(m_control_lock.is_locked());
  320. VERIFY(m_modeset_lock.is_locked());
  321. // FIXME: Check that we are able to modeset to the requested resolution!
  322. return true;
  323. }
  324. void IntelNativeDisplayConnector::disable_output()
  325. {
  326. VERIFY(m_control_lock.is_locked());
  327. disable_dac_output();
  328. disable_all_planes();
  329. disable_pipe_a();
  330. disable_pipe_b();
  331. disable_dpll();
  332. disable_vga_emulation();
  333. }
  334. void IntelNativeDisplayConnector::enable_output(PhysicalAddress fb_address, size_t width)
  335. {
  336. VERIFY(m_control_lock.is_locked());
  337. VERIFY(!pipe_a_enabled());
  338. enable_pipe_a();
  339. enable_primary_plane(fb_address, width);
  340. enable_dac_output();
  341. }
  342. static size_t compute_dac_multiplier(size_t pixel_clock_in_khz)
  343. {
  344. dbgln_if(INTEL_GRAPHICS_DEBUG, "Intel native graphics: Pixel clock is {} KHz", pixel_clock_in_khz);
  345. VERIFY(pixel_clock_in_khz >= 25000);
  346. if (pixel_clock_in_khz >= 100000) {
  347. return 1;
  348. } else if (pixel_clock_in_khz >= 50000) {
  349. return 2;
  350. } else {
  351. return 4;
  352. }
  353. }
  354. bool IntelNativeDisplayConnector::set_safe_crt_resolution()
  355. {
  356. VERIFY(m_control_lock.is_locked());
  357. SpinlockLocker modeset_lock(m_modeset_lock);
  358. // Note: Just in case we still allow access to VGA IO ports, disable it now.
  359. GraphicsManagement::the().disable_vga_emulation_access_permanently();
  360. // FIXME: Get the requested resolution from the EDID!!
  361. auto modesetting = calculate_modesetting_from_edid(m_edid_parser.value(), 0);
  362. disable_output();
  363. auto dac_multiplier = compute_dac_multiplier(modesetting.pixel_clock_in_khz);
  364. auto pll_settings = create_pll_settings((1000 * modesetting.pixel_clock_in_khz * dac_multiplier), 96'000'000, IntelGraphics::G35Limits);
  365. if (!pll_settings.has_value())
  366. VERIFY_NOT_REACHED();
  367. auto settings = pll_settings.value();
  368. dbgln_if(INTEL_GRAPHICS_DEBUG, "PLL settings for {} {} {} {} {}", settings.n, settings.m1, settings.m2, settings.p1, settings.p2);
  369. enable_dpll_without_vga(pll_settings.value(), dac_multiplier);
  370. set_display_timings(modesetting);
  371. enable_output(m_framebuffer_address.value(), modesetting.horizontal.blanking_start());
  372. DisplayConnector::ModeSetting mode_set {
  373. .horizontal_stride = modesetting.horizontal.blanking_start() * sizeof(u32),
  374. .pixel_clock_in_khz = 0,
  375. .horizontal_active = modesetting.horizontal.blanking_start(),
  376. .horizontal_front_porch_pixels = 0,
  377. .horizontal_sync_time_pixels = 0,
  378. .horizontal_blank_pixels = 0,
  379. .vertical_active = modesetting.vertical.blanking_start(),
  380. .vertical_front_porch_lines = 0,
  381. .vertical_sync_time_lines = 0,
  382. .vertical_blank_lines = 0,
  383. .horizontal_offset = 0,
  384. .vertical_offset = 0,
  385. };
  386. m_current_mode_setting = mode_set;
  387. if (m_framebuffer_console)
  388. m_framebuffer_console->set_resolution(m_current_mode_setting.horizontal_active, m_current_mode_setting.vertical_active, m_current_mode_setting.horizontal_stride);
  389. return true;
  390. }
  391. void IntelNativeDisplayConnector::set_display_timings(Graphics::Modesetting const& modesetting)
  392. {
  393. VERIFY(m_control_lock.is_locked());
  394. VERIFY(m_modeset_lock.is_locked());
  395. VERIFY(!(read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 31)));
  396. VERIFY(!(read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 30)));
  397. dbgln_if(INTEL_GRAPHICS_DEBUG, "htotal - {}, {}", (modesetting.horizontal.active - 1), (modesetting.horizontal.total - 1));
  398. write_to_register(IntelGraphics::RegisterIndex::HTotalA, (modesetting.horizontal.active - 1) | (modesetting.horizontal.total - 1) << 16);
  399. dbgln_if(INTEL_GRAPHICS_DEBUG, "hblank - {}, {}", (modesetting.horizontal.blanking_start() - 1), (modesetting.horizontal.blanking_end() - 1));
  400. write_to_register(IntelGraphics::RegisterIndex::HBlankA, (modesetting.horizontal.blanking_start() - 1) | (modesetting.horizontal.blanking_end() - 1) << 16);
  401. dbgln_if(INTEL_GRAPHICS_DEBUG, "hsync - {}, {}", (modesetting.horizontal.sync_start - 1), (modesetting.horizontal.sync_end - 1));
  402. write_to_register(IntelGraphics::RegisterIndex::HSyncA, (modesetting.horizontal.sync_start - 1) | (modesetting.horizontal.sync_end - 1) << 16);
  403. dbgln_if(INTEL_GRAPHICS_DEBUG, "vtotal - {}, {}", (modesetting.vertical.active - 1), (modesetting.vertical.total - 1));
  404. write_to_register(IntelGraphics::RegisterIndex::VTotalA, (modesetting.vertical.active - 1) | (modesetting.vertical.total - 1) << 16);
  405. dbgln_if(INTEL_GRAPHICS_DEBUG, "vblank - {}, {}", (modesetting.vertical.blanking_start() - 1), (modesetting.vertical.blanking_end() - 1));
  406. write_to_register(IntelGraphics::RegisterIndex::VBlankA, (modesetting.vertical.blanking_start() - 1) | (modesetting.vertical.blanking_end() - 1) << 16);
  407. dbgln_if(INTEL_GRAPHICS_DEBUG, "vsync - {}, {}", (modesetting.vertical.sync_start - 1), (modesetting.vertical.sync_end - 1));
  408. write_to_register(IntelGraphics::RegisterIndex::VSyncA, (modesetting.vertical.sync_start - 1) | (modesetting.vertical.sync_end - 1) << 16);
  409. dbgln_if(INTEL_GRAPHICS_DEBUG, "sourceSize - {}, {}", (modesetting.vertical.active - 1), (modesetting.horizontal.active - 1));
  410. write_to_register(IntelGraphics::RegisterIndex::PipeASource, (modesetting.vertical.active - 1) | (modesetting.horizontal.active - 1) << 16);
  411. microseconds_delay(200);
  412. }
  413. bool IntelNativeDisplayConnector::wait_for_enabled_pipe_a(size_t milliseconds_timeout) const
  414. {
  415. size_t current_time = 0;
  416. while (current_time < milliseconds_timeout) {
  417. if (pipe_a_enabled())
  418. return true;
  419. microseconds_delay(1000);
  420. current_time++;
  421. }
  422. return false;
  423. }
  424. bool IntelNativeDisplayConnector::wait_for_disabled_pipe_a(size_t milliseconds_timeout) const
  425. {
  426. size_t current_time = 0;
  427. while (current_time < milliseconds_timeout) {
  428. if (!pipe_a_enabled())
  429. return true;
  430. microseconds_delay(1000);
  431. current_time++;
  432. }
  433. return false;
  434. }
  435. bool IntelNativeDisplayConnector::wait_for_disabled_pipe_b(size_t milliseconds_timeout) const
  436. {
  437. size_t current_time = 0;
  438. while (current_time < milliseconds_timeout) {
  439. if (!pipe_b_enabled())
  440. return true;
  441. microseconds_delay(1000);
  442. current_time++;
  443. }
  444. return false;
  445. }
  446. void IntelNativeDisplayConnector::disable_dpll()
  447. {
  448. VERIFY(m_control_lock.is_locked());
  449. VERIFY(m_modeset_lock.is_locked());
  450. write_to_register(IntelGraphics::RegisterIndex::DPLLControlA, 0);
  451. write_to_register(IntelGraphics::RegisterIndex::DPLLControlB, 0);
  452. }
  453. void IntelNativeDisplayConnector::disable_pipe_a()
  454. {
  455. VERIFY(m_control_lock.is_locked());
  456. VERIFY(m_modeset_lock.is_locked());
  457. write_to_register(IntelGraphics::RegisterIndex::PipeAConf, 0);
  458. dbgln_if(INTEL_GRAPHICS_DEBUG, "Disabling Pipe A");
  459. wait_for_disabled_pipe_a(100);
  460. dbgln_if(INTEL_GRAPHICS_DEBUG, "Disabling Pipe A - done.");
  461. }
  462. void IntelNativeDisplayConnector::disable_pipe_b()
  463. {
  464. VERIFY(m_control_lock.is_locked());
  465. VERIFY(m_modeset_lock.is_locked());
  466. write_to_register(IntelGraphics::RegisterIndex::PipeAConf, 0);
  467. dbgln_if(INTEL_GRAPHICS_DEBUG, "Disabling Pipe B");
  468. wait_for_disabled_pipe_b(100);
  469. dbgln_if(INTEL_GRAPHICS_DEBUG, "Disabling Pipe B - done.");
  470. }
  471. void IntelNativeDisplayConnector::enable_pipe_a()
  472. {
  473. VERIFY(m_control_lock.is_locked());
  474. VERIFY(m_modeset_lock.is_locked());
  475. VERIFY(!(read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 31)));
  476. VERIFY(!(read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 30)));
  477. write_to_register(IntelGraphics::RegisterIndex::PipeAConf, (1 << 31) | (1 << 24));
  478. dbgln_if(INTEL_GRAPHICS_DEBUG, "enabling Pipe A");
  479. // FIXME: Seems like my video card is buggy and doesn't set the enabled bit (bit 30)!!
  480. wait_for_enabled_pipe_a(100);
  481. dbgln_if(INTEL_GRAPHICS_DEBUG, "enabling Pipe A - done.");
  482. }
  483. void IntelNativeDisplayConnector::enable_primary_plane(PhysicalAddress fb_address, size_t width)
  484. {
  485. VERIFY(m_control_lock.is_locked());
  486. VERIFY(m_modeset_lock.is_locked());
  487. VERIFY(((width * 4) % 64 == 0));
  488. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneAStride, width * 4);
  489. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneALinearOffset, 0);
  490. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneASurface, fb_address.get());
  491. // FIXME: Serenity uses BGR 32 bit pixel format, but maybe we should try to determine it somehow!
  492. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneAControl, (0b0110 << 26) | (1 << 31));
  493. }
  494. void IntelNativeDisplayConnector::set_dpll_registers(IntelGraphics::PLLSettings const& settings)
  495. {
  496. VERIFY(m_control_lock.is_locked());
  497. VERIFY(m_modeset_lock.is_locked());
  498. write_to_register(IntelGraphics::RegisterIndex::DPLLDivisorA0, (settings.m2 - 2) | ((settings.m1 - 2) << 8) | ((settings.n - 2) << 16));
  499. write_to_register(IntelGraphics::RegisterIndex::DPLLDivisorA1, (settings.m2 - 2) | ((settings.m1 - 2) << 8) | ((settings.n - 2) << 16));
  500. write_to_register(IntelGraphics::RegisterIndex::DPLLControlA, 0);
  501. }
  502. void IntelNativeDisplayConnector::enable_dpll_without_vga(IntelGraphics::PLLSettings const& settings, size_t dac_multiplier)
  503. {
  504. VERIFY(m_control_lock.is_locked());
  505. VERIFY(m_modeset_lock.is_locked());
  506. set_dpll_registers(settings);
  507. microseconds_delay(200);
  508. write_to_register(IntelGraphics::RegisterIndex::DPLLControlA, (6 << 9) | (settings.p1) << 16 | (1 << 26) | (1 << 28) | (1 << 31));
  509. write_to_register(IntelGraphics::RegisterIndex::DPLLMultiplierA, (dac_multiplier - 1) | ((dac_multiplier - 1) << 8));
  510. // The specification says we should wait (at least) about 150 microseconds
  511. // after enabling the DPLL to allow the clock to stabilize
  512. microseconds_delay(200);
  513. VERIFY(read_from_register(IntelGraphics::RegisterIndex::DPLLControlA) & (1 << 31));
  514. }
  515. void IntelNativeDisplayConnector::disable_dac_output()
  516. {
  517. VERIFY(m_control_lock.is_locked());
  518. VERIFY(m_modeset_lock.is_locked());
  519. write_to_register(IntelGraphics::RegisterIndex::AnalogDisplayPort, 0b11 << 10);
  520. }
  521. void IntelNativeDisplayConnector::enable_dac_output()
  522. {
  523. VERIFY(m_control_lock.is_locked());
  524. VERIFY(m_modeset_lock.is_locked());
  525. write_to_register(IntelGraphics::RegisterIndex::AnalogDisplayPort, (1 << 31));
  526. }
  527. void IntelNativeDisplayConnector::disable_vga_emulation()
  528. {
  529. VERIFY(m_control_lock.is_locked());
  530. VERIFY(m_modeset_lock.is_locked());
  531. write_to_register(IntelGraphics::RegisterIndex::VGADisplayPlaneControl, (1 << 31));
  532. read_from_register(IntelGraphics::RegisterIndex::VGADisplayPlaneControl);
  533. }
  534. void IntelNativeDisplayConnector::disable_all_planes()
  535. {
  536. VERIFY(m_control_lock.is_locked());
  537. VERIFY(m_modeset_lock.is_locked());
  538. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneAControl, 0);
  539. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneBControl, 0);
  540. }
  541. }