NativeGraphicsAdapter.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Graphics/Console/ContiguousFramebufferConsole.h>
  7. #include <Kernel/Graphics/Definitions.h>
  8. #include <Kernel/Graphics/GraphicsManagement.h>
  9. #include <Kernel/Graphics/Intel/NativeGraphicsAdapter.h>
  10. #include <Kernel/IO.h>
  11. #include <Kernel/PhysicalAddress.h>
  12. namespace Kernel {
  13. static constexpr IntelNativeGraphicsAdapter::PLLMaxSettings G35Limits {
  14. { 20'000'000, 400'000'000 }, // values in Hz, dot_clock
  15. { 1'400'000'000, 2'800'000'000 }, // values in Hz, VCO
  16. { 3, 8 }, // n
  17. { 70, 120 }, // m
  18. { 10, 20 }, // m1
  19. { 5, 9 }, // m2
  20. { 5, 80 }, // p
  21. { 1, 8 }, // p1
  22. { 5, 10 } // p2
  23. };
  24. static constexpr u16 supported_models[] {
  25. 0x29c2, // Intel G35 Adapter
  26. };
  27. static bool is_supported_model(u16 device_id)
  28. {
  29. for (auto& id : supported_models) {
  30. if (id == device_id)
  31. return true;
  32. }
  33. return false;
  34. }
  35. #define DDC2_I2C_ADDRESS 0x50
  36. RefPtr<IntelNativeGraphicsAdapter> IntelNativeGraphicsAdapter::initialize(PCI::Address address)
  37. {
  38. auto id = PCI::get_id(address);
  39. VERIFY(id.vendor_id == 0x8086);
  40. if (!is_supported_model(id.device_id))
  41. return {};
  42. return adopt_ref(*new IntelNativeGraphicsAdapter(address));
  43. }
  44. static size_t compute_dac_multiplier(size_t pixel_clock_in_khz)
  45. {
  46. dbgln_if(INTEL_GRAPHICS_DEBUG, "Intel native graphics: Pixel clock is {} KHz", pixel_clock_in_khz);
  47. VERIFY(pixel_clock_in_khz >= 25000);
  48. if (pixel_clock_in_khz >= 100000) {
  49. return 1;
  50. } else if (pixel_clock_in_khz >= 50000) {
  51. return 2;
  52. } else {
  53. return 4;
  54. }
  55. }
  56. static Graphics::Modesetting calculate_modesetting_from_edid(const Graphics::VideoInfoBlock& edid, size_t index)
  57. {
  58. Graphics::Modesetting mode;
  59. VERIFY(edid.details[0].pixel_clock);
  60. mode.pixel_clock_in_khz = edid.details[0].pixel_clock * 10;
  61. size_t horizontal_active = edid.details[index].horizontal_active | ((edid.details[index].horizontal_active_blank_msb >> 4) << 8);
  62. size_t horizontal_blank = edid.details[index].horizontal_blank | ((edid.details[index].horizontal_active_blank_msb & 0xF) << 8);
  63. size_t horizontal_sync_offset = edid.details[index].horizontal_sync_offset | ((edid.details[index].sync_msb >> 6) << 8);
  64. size_t horizontal_sync_pulse = edid.details[index].horizontal_sync_pulse | (((edid.details[index].sync_msb >> 4) & 0x3) << 8);
  65. mode.horizontal.active = horizontal_active;
  66. mode.horizontal.sync_start = horizontal_active + horizontal_sync_offset;
  67. mode.horizontal.sync_end = horizontal_active + horizontal_sync_offset + horizontal_sync_pulse;
  68. mode.horizontal.total = horizontal_active + horizontal_blank;
  69. size_t vertical_active = edid.details[index].vertical_active | ((edid.details[index].vertical_active_blank_msb >> 4) << 8);
  70. size_t vertical_blank = edid.details[index].vertical_blank | ((edid.details[index].vertical_active_blank_msb & 0xF) << 8);
  71. size_t vertical_sync_offset = (edid.details[index].vertical_sync >> 4) | (((edid.details[index].sync_msb >> 2) & 0x3) << 4);
  72. size_t vertical_sync_pulse = (edid.details[index].vertical_sync & 0xF) | ((edid.details[index].sync_msb & 0x3) << 4);
  73. mode.vertical.active = vertical_active;
  74. mode.vertical.sync_start = vertical_active + vertical_sync_offset;
  75. mode.vertical.sync_end = vertical_active + vertical_sync_offset + vertical_sync_pulse;
  76. mode.vertical.total = vertical_active + vertical_blank;
  77. return mode;
  78. }
  79. static bool check_pll_settings(const IntelNativeGraphicsAdapter::PLLSettings& settings, size_t reference_clock, const IntelNativeGraphicsAdapter::PLLMaxSettings& limits)
  80. {
  81. if (settings.n < limits.n.min || settings.n > limits.n.max) {
  82. dbgln_if(INTEL_GRAPHICS_DEBUG, "N is invalid {}", settings.n);
  83. return false;
  84. }
  85. if (settings.m1 < limits.m1.min || settings.m1 > limits.m1.max) {
  86. dbgln_if(INTEL_GRAPHICS_DEBUG, "m1 is invalid {}", settings.m1);
  87. return false;
  88. }
  89. if (settings.m2 < limits.m2.min || settings.m2 > limits.m2.max) {
  90. dbgln_if(INTEL_GRAPHICS_DEBUG, "m2 is invalid {}", settings.m2);
  91. return false;
  92. }
  93. if (settings.p1 < limits.p1.min || settings.p1 > limits.p1.max) {
  94. dbgln_if(INTEL_GRAPHICS_DEBUG, "p1 is invalid {}", settings.p1);
  95. return false;
  96. }
  97. if (settings.m1 <= settings.m2) {
  98. dbgln_if(INTEL_GRAPHICS_DEBUG, "m2 is invalid {} as it is bigger than m1 {}", settings.m2, settings.m1);
  99. return false;
  100. }
  101. auto m = settings.compute_m();
  102. auto p = settings.compute_p();
  103. if (m < limits.m.min || m > limits.m.max) {
  104. dbgln_if(INTEL_GRAPHICS_DEBUG, "m invalid {}", m);
  105. return false;
  106. }
  107. if (p < limits.p.min || p > limits.p.max) {
  108. dbgln_if(INTEL_GRAPHICS_DEBUG, "p invalid {}", p);
  109. return false;
  110. }
  111. auto dot = settings.compute_dot_clock(reference_clock);
  112. auto vco = settings.compute_vco(reference_clock);
  113. if (dot < limits.dot_clock.min || dot > limits.dot_clock.max) {
  114. dbgln_if(INTEL_GRAPHICS_DEBUG, "Dot clock invalid {}", dot);
  115. return false;
  116. }
  117. if (vco < limits.vco.min || vco > limits.vco.max) {
  118. dbgln_if(INTEL_GRAPHICS_DEBUG, "VCO clock invalid {}", vco);
  119. return false;
  120. }
  121. return true;
  122. }
  123. static size_t find_absolute_difference(u64 target_frequency, u64 checked_frequency)
  124. {
  125. if (target_frequency >= checked_frequency)
  126. return target_frequency - checked_frequency;
  127. return checked_frequency - target_frequency;
  128. }
  129. Optional<IntelNativeGraphicsAdapter::PLLSettings> IntelNativeGraphicsAdapter::create_pll_settings(u64 target_frequency, u64 reference_clock, const PLLMaxSettings& limits)
  130. {
  131. IntelNativeGraphicsAdapter::PLLSettings settings;
  132. IntelNativeGraphicsAdapter::PLLSettings best_settings;
  133. // FIXME: Is this correct for all Intel Native graphics cards?
  134. settings.p2 = 10;
  135. dbgln_if(INTEL_GRAPHICS_DEBUG, "Check PLL settings for ref clock of {} Hz, for target of {} Hz", reference_clock, target_frequency);
  136. u64 best_difference = 0xffffffff;
  137. for (settings.n = limits.n.min; settings.n <= limits.n.max; ++settings.n) {
  138. for (settings.m1 = limits.m1.max; settings.m1 >= limits.m1.min; --settings.m1) {
  139. for (settings.m2 = limits.m2.max; settings.m2 >= limits.m2.min; --settings.m2) {
  140. for (settings.p1 = limits.p1.max; settings.p1 >= limits.p1.min; --settings.p1) {
  141. dbgln_if(INTEL_GRAPHICS_DEBUG, "Check PLL settings for {} {} {} {} {}", settings.n, settings.m1, settings.m2, settings.p1, settings.p2);
  142. if (!check_pll_settings(settings, reference_clock, limits))
  143. continue;
  144. auto current_dot_clock = settings.compute_dot_clock(reference_clock);
  145. if (current_dot_clock == target_frequency)
  146. return settings;
  147. auto difference = find_absolute_difference(target_frequency, current_dot_clock);
  148. if (difference < best_difference && (current_dot_clock > target_frequency)) {
  149. best_settings = settings;
  150. best_difference = difference;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. if (best_settings.is_valid())
  157. return best_settings;
  158. return {};
  159. }
  160. IntelNativeGraphicsAdapter::IntelNativeGraphicsAdapter(PCI::Address address)
  161. : VGACompatibleAdapter(address)
  162. , m_registers(PCI::get_BAR0(address) & 0xfffffffc)
  163. , m_framebuffer_addr(PCI::get_BAR2(address) & 0xfffffffc)
  164. {
  165. dbgln_if(INTEL_GRAPHICS_DEBUG, "Intel Native Graphics Adapter @ {}", address);
  166. auto bar0_space_size = PCI::get_BAR_space_size(address, 0);
  167. VERIFY(bar0_space_size == 0x80000);
  168. dmesgln("Intel Native Graphics Adapter @ {}, MMIO @ {}, space size is {:x} bytes", address, PhysicalAddress(PCI::get_BAR0(address)), bar0_space_size);
  169. dmesgln("Intel Native Graphics Adapter @ {}, framebuffer @ {}", address, PhysicalAddress(PCI::get_BAR2(address)));
  170. m_registers_region = MM.allocate_kernel_region(PhysicalAddress(PCI::get_BAR0(address)).page_base(), bar0_space_size, "Intel Native Graphics Registers", Memory::Region::Access::ReadWrite);
  171. PCI::enable_bus_mastering(address);
  172. {
  173. SpinlockLocker control_lock(m_control_lock);
  174. set_gmbus_default_rate();
  175. set_gmbus_pin_pair(GMBusPinPair::DedicatedAnalog);
  176. }
  177. gmbus_read_edid();
  178. auto modesetting = calculate_modesetting_from_edid(m_crt_edid, 0);
  179. dmesgln("Intel Native Graphics Adapter @ {}, preferred resolution is {:d}x{:d}", pci_address(), modesetting.horizontal.active, modesetting.vertical.active);
  180. set_crt_resolution(modesetting.horizontal.active, modesetting.vertical.active);
  181. auto framebuffer_address = PhysicalAddress(PCI::get_BAR2(pci_address()) & 0xfffffff0);
  182. VERIFY(!framebuffer_address.is_null());
  183. VERIFY(m_framebuffer_pitch != 0);
  184. VERIFY(m_framebuffer_height != 0);
  185. VERIFY(m_framebuffer_width != 0);
  186. m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(framebuffer_address, m_framebuffer_width, m_framebuffer_height, m_framebuffer_pitch);
  187. // FIXME: This is a very wrong way to do this...
  188. GraphicsManagement::the().m_console = m_framebuffer_console;
  189. }
  190. void IntelNativeGraphicsAdapter::enable_vga_plane()
  191. {
  192. VERIFY(m_control_lock.is_locked());
  193. VERIFY(m_modeset_lock.is_locked());
  194. }
  195. [[maybe_unused]] static StringView convert_register_index_to_string(IntelGraphics::RegisterIndex index)
  196. {
  197. switch (index) {
  198. case IntelGraphics::RegisterIndex::PipeAConf:
  199. return "PipeAConf"sv;
  200. case IntelGraphics::RegisterIndex::PipeBConf:
  201. return "PipeBConf"sv;
  202. case IntelGraphics::RegisterIndex::GMBusData:
  203. return "GMBusData"sv;
  204. case IntelGraphics::RegisterIndex::GMBusStatus:
  205. return "GMBusStatus"sv;
  206. case IntelGraphics::RegisterIndex::GMBusCommand:
  207. return "GMBusCommand"sv;
  208. case IntelGraphics::RegisterIndex::GMBusClock:
  209. return "GMBusClock"sv;
  210. case IntelGraphics::RegisterIndex::DisplayPlaneAControl:
  211. return "DisplayPlaneAControl"sv;
  212. case IntelGraphics::RegisterIndex::DisplayPlaneALinearOffset:
  213. return "DisplayPlaneALinearOffset"sv;
  214. case IntelGraphics::RegisterIndex::DisplayPlaneAStride:
  215. return "DisplayPlaneAStride"sv;
  216. case IntelGraphics::RegisterIndex::DisplayPlaneASurface:
  217. return "DisplayPlaneASurface"sv;
  218. case IntelGraphics::RegisterIndex::DPLLDivisorA0:
  219. return "DPLLDivisorA0"sv;
  220. case IntelGraphics::RegisterIndex::DPLLDivisorA1:
  221. return "DPLLDivisorA1"sv;
  222. case IntelGraphics::RegisterIndex::DPLLControlA:
  223. return "DPLLControlA"sv;
  224. case IntelGraphics::RegisterIndex::DPLLControlB:
  225. return "DPLLControlB"sv;
  226. case IntelGraphics::RegisterIndex::DPLLMultiplierA:
  227. return "DPLLMultiplierA"sv;
  228. case IntelGraphics::RegisterIndex::HTotalA:
  229. return "HTotalA"sv;
  230. case IntelGraphics::RegisterIndex::HBlankA:
  231. return "HBlankA"sv;
  232. case IntelGraphics::RegisterIndex::HSyncA:
  233. return "HSyncA"sv;
  234. case IntelGraphics::RegisterIndex::VTotalA:
  235. return "VTotalA"sv;
  236. case IntelGraphics::RegisterIndex::VBlankA:
  237. return "VBlankA"sv;
  238. case IntelGraphics::RegisterIndex::VSyncA:
  239. return "VSyncA"sv;
  240. case IntelGraphics::RegisterIndex::PipeASource:
  241. return "PipeASource"sv;
  242. case IntelGraphics::RegisterIndex::AnalogDisplayPort:
  243. return "AnalogDisplayPort"sv;
  244. case IntelGraphics::RegisterIndex::VGADisplayPlaneControl:
  245. return "VGADisplayPlaneControl"sv;
  246. default:
  247. VERIFY_NOT_REACHED();
  248. }
  249. }
  250. void IntelNativeGraphicsAdapter::write_to_register(IntelGraphics::RegisterIndex index, u32 value) const
  251. {
  252. VERIFY(m_control_lock.is_locked());
  253. VERIFY(m_registers_region);
  254. SpinlockLocker lock(m_registers_lock);
  255. dbgln_if(INTEL_GRAPHICS_DEBUG, "Intel Graphics {}: Write to {} value of {:x}", pci_address(), convert_register_index_to_string(index), value);
  256. auto* reg = (volatile u32*)m_registers_region->vaddr().offset(index).as_ptr();
  257. *reg = value;
  258. }
  259. u32 IntelNativeGraphicsAdapter::read_from_register(IntelGraphics::RegisterIndex index) const
  260. {
  261. VERIFY(m_control_lock.is_locked());
  262. VERIFY(m_registers_region);
  263. SpinlockLocker lock(m_registers_lock);
  264. auto* reg = (volatile u32*)m_registers_region->vaddr().offset(index).as_ptr();
  265. u32 value = *reg;
  266. dbgln_if(INTEL_GRAPHICS_DEBUG, "Intel Graphics {}: Read from {} value of {:x}", pci_address(), convert_register_index_to_string(index), value);
  267. return value;
  268. }
  269. bool IntelNativeGraphicsAdapter::pipe_a_enabled() const
  270. {
  271. VERIFY(m_control_lock.is_locked());
  272. return read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 30);
  273. }
  274. bool IntelNativeGraphicsAdapter::pipe_b_enabled() const
  275. {
  276. VERIFY(m_control_lock.is_locked());
  277. return read_from_register(IntelGraphics::RegisterIndex::PipeBConf) & (1 << 30);
  278. }
  279. bool IntelNativeGraphicsAdapter::gmbus_wait_for(GMBusStatus desired_status, Optional<size_t> milliseconds_timeout)
  280. {
  281. VERIFY(m_control_lock.is_locked());
  282. size_t milliseconds_passed = 0;
  283. while (1) {
  284. if (milliseconds_timeout.has_value() && milliseconds_timeout.value() < milliseconds_passed)
  285. return false;
  286. full_memory_barrier();
  287. u32 status = read_from_register(IntelGraphics::RegisterIndex::GMBusStatus);
  288. full_memory_barrier();
  289. VERIFY(!(status & (1 << 10))); // error happened
  290. switch (desired_status) {
  291. case GMBusStatus::HardwareReady:
  292. if (status & (1 << 11))
  293. return true;
  294. break;
  295. case GMBusStatus::TransactionCompletion:
  296. if (status & (1 << 14))
  297. return true;
  298. break;
  299. default:
  300. VERIFY_NOT_REACHED();
  301. }
  302. IO::delay(1000);
  303. milliseconds_passed++;
  304. }
  305. }
  306. void IntelNativeGraphicsAdapter::gmbus_write(unsigned address, u32 byte)
  307. {
  308. VERIFY(m_control_lock.is_locked());
  309. VERIFY(address < 256);
  310. full_memory_barrier();
  311. write_to_register(IntelGraphics::RegisterIndex::GMBusData, byte);
  312. full_memory_barrier();
  313. write_to_register(IntelGraphics::RegisterIndex::GMBusCommand, ((address << 1) | (1 << 16) | (GMBusCycle::Wait << 25) | (1 << 30)));
  314. full_memory_barrier();
  315. gmbus_wait_for(GMBusStatus::TransactionCompletion, {});
  316. }
  317. void IntelNativeGraphicsAdapter::gmbus_read(unsigned address, u8* buf, size_t length)
  318. {
  319. VERIFY(address < 256);
  320. VERIFY(m_control_lock.is_locked());
  321. size_t nread = 0;
  322. auto read_set = [&] {
  323. full_memory_barrier();
  324. u32 data = read_from_register(IntelGraphics::RegisterIndex::GMBusData);
  325. full_memory_barrier();
  326. for (size_t index = 0; index < 4; index++) {
  327. if (nread == length)
  328. break;
  329. buf[nread] = (data >> (8 * index)) & 0xFF;
  330. nread++;
  331. }
  332. };
  333. full_memory_barrier();
  334. write_to_register(IntelGraphics::RegisterIndex::GMBusCommand, (1 | (address << 1) | (length << 16) | (GMBusCycle::Wait << 25) | (1 << 30)));
  335. full_memory_barrier();
  336. while (nread < length) {
  337. gmbus_wait_for(GMBusStatus::HardwareReady, {});
  338. read_set();
  339. }
  340. gmbus_wait_for(GMBusStatus::TransactionCompletion, {});
  341. }
  342. void IntelNativeGraphicsAdapter::gmbus_read_edid()
  343. {
  344. SpinlockLocker control_lock(m_control_lock);
  345. gmbus_write(DDC2_I2C_ADDRESS, 0);
  346. gmbus_read(DDC2_I2C_ADDRESS, (u8*)&m_crt_edid, sizeof(Graphics::VideoInfoBlock));
  347. }
  348. bool IntelNativeGraphicsAdapter::is_resolution_valid(size_t, size_t)
  349. {
  350. VERIFY(m_control_lock.is_locked());
  351. VERIFY(m_modeset_lock.is_locked());
  352. // FIXME: Check that we are able to modeset to the requested resolution!
  353. return true;
  354. }
  355. void IntelNativeGraphicsAdapter::disable_output()
  356. {
  357. VERIFY(m_control_lock.is_locked());
  358. disable_dac_output();
  359. disable_all_planes();
  360. disable_pipe_a();
  361. disable_pipe_b();
  362. disable_vga_emulation();
  363. disable_dpll();
  364. }
  365. void IntelNativeGraphicsAdapter::enable_output(PhysicalAddress fb_address, size_t width)
  366. {
  367. VERIFY(m_control_lock.is_locked());
  368. VERIFY(!pipe_a_enabled());
  369. enable_pipe_a();
  370. enable_primary_plane(fb_address, width);
  371. enable_dac_output();
  372. }
  373. bool IntelNativeGraphicsAdapter::set_crt_resolution(size_t width, size_t height)
  374. {
  375. SpinlockLocker control_lock(m_control_lock);
  376. SpinlockLocker modeset_lock(m_modeset_lock);
  377. if (!is_resolution_valid(width, height)) {
  378. return false;
  379. }
  380. // FIXME: Get the requested resolution from the EDID!!
  381. auto modesetting = calculate_modesetting_from_edid(m_crt_edid, 0);
  382. disable_output();
  383. auto dac_multiplier = compute_dac_multiplier(modesetting.pixel_clock_in_khz);
  384. auto pll_settings = create_pll_settings((1000 * modesetting.pixel_clock_in_khz * dac_multiplier), 96'000'000, G35Limits);
  385. if (!pll_settings.has_value())
  386. VERIFY_NOT_REACHED();
  387. auto settings = pll_settings.value();
  388. dbgln_if(INTEL_GRAPHICS_DEBUG, "PLL settings for {} {} {} {} {}", settings.n, settings.m1, settings.m2, settings.p1, settings.p2);
  389. enable_dpll_without_vga(pll_settings.value(), dac_multiplier);
  390. set_display_timings(modesetting);
  391. auto address = PhysicalAddress(PCI::get_BAR2(pci_address()) & 0xfffffff0);
  392. VERIFY(!address.is_null());
  393. enable_output(address, width);
  394. m_framebuffer_width = width;
  395. m_framebuffer_height = height;
  396. m_framebuffer_pitch = width * 4;
  397. return true;
  398. }
  399. void IntelNativeGraphicsAdapter::set_display_timings(const Graphics::Modesetting& modesetting)
  400. {
  401. VERIFY(m_control_lock.is_locked());
  402. VERIFY(m_modeset_lock.is_locked());
  403. VERIFY(!(read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 31)));
  404. VERIFY(!(read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 30)));
  405. dbgln_if(INTEL_GRAPHICS_DEBUG, "htotal - {}, {}", (modesetting.horizontal.active - 1), (modesetting.horizontal.total - 1));
  406. write_to_register(IntelGraphics::RegisterIndex::HTotalA, (modesetting.horizontal.active - 1) | (modesetting.horizontal.total - 1) << 16);
  407. dbgln_if(INTEL_GRAPHICS_DEBUG, "hblank - {}, {}", (modesetting.horizontal.blanking_start() - 1), (modesetting.horizontal.blanking_end() - 1));
  408. write_to_register(IntelGraphics::RegisterIndex::HBlankA, (modesetting.horizontal.blanking_start() - 1) | (modesetting.horizontal.blanking_end() - 1) << 16);
  409. dbgln_if(INTEL_GRAPHICS_DEBUG, "hsync - {}, {}", (modesetting.horizontal.sync_start - 1), (modesetting.horizontal.sync_end - 1));
  410. write_to_register(IntelGraphics::RegisterIndex::HSyncA, (modesetting.horizontal.sync_start - 1) | (modesetting.horizontal.sync_end - 1) << 16);
  411. dbgln_if(INTEL_GRAPHICS_DEBUG, "vtotal - {}, {}", (modesetting.vertical.active - 1), (modesetting.vertical.total - 1));
  412. write_to_register(IntelGraphics::RegisterIndex::VTotalA, (modesetting.vertical.active - 1) | (modesetting.vertical.total - 1) << 16);
  413. dbgln_if(INTEL_GRAPHICS_DEBUG, "vblank - {}, {}", (modesetting.vertical.blanking_start() - 1), (modesetting.vertical.blanking_end() - 1));
  414. write_to_register(IntelGraphics::RegisterIndex::VBlankA, (modesetting.vertical.blanking_start() - 1) | (modesetting.vertical.blanking_end() - 1) << 16);
  415. dbgln_if(INTEL_GRAPHICS_DEBUG, "vsync - {}, {}", (modesetting.vertical.sync_start - 1), (modesetting.vertical.sync_end - 1));
  416. write_to_register(IntelGraphics::RegisterIndex::VSyncA, (modesetting.vertical.sync_start - 1) | (modesetting.vertical.sync_end - 1) << 16);
  417. dbgln_if(INTEL_GRAPHICS_DEBUG, "sourceSize - {}, {}", (modesetting.vertical.active - 1), (modesetting.horizontal.active - 1));
  418. write_to_register(IntelGraphics::RegisterIndex::PipeASource, (modesetting.vertical.active - 1) | (modesetting.horizontal.active - 1) << 16);
  419. IO::delay(200);
  420. }
  421. bool IntelNativeGraphicsAdapter::wait_for_enabled_pipe_a(size_t milliseconds_timeout) const
  422. {
  423. size_t current_time = 0;
  424. while (current_time < milliseconds_timeout) {
  425. if (pipe_a_enabled())
  426. return true;
  427. IO::delay(1000);
  428. current_time++;
  429. }
  430. return false;
  431. }
  432. bool IntelNativeGraphicsAdapter::wait_for_disabled_pipe_a(size_t milliseconds_timeout) const
  433. {
  434. size_t current_time = 0;
  435. while (current_time < milliseconds_timeout) {
  436. if (!pipe_a_enabled())
  437. return true;
  438. IO::delay(1000);
  439. current_time++;
  440. }
  441. return false;
  442. }
  443. bool IntelNativeGraphicsAdapter::wait_for_disabled_pipe_b(size_t milliseconds_timeout) const
  444. {
  445. size_t current_time = 0;
  446. while (current_time < milliseconds_timeout) {
  447. if (!pipe_b_enabled())
  448. return true;
  449. IO::delay(1000);
  450. current_time++;
  451. }
  452. return false;
  453. }
  454. void IntelNativeGraphicsAdapter::disable_dpll()
  455. {
  456. VERIFY(m_control_lock.is_locked());
  457. VERIFY(m_modeset_lock.is_locked());
  458. write_to_register(IntelGraphics::RegisterIndex::DPLLControlA, read_from_register(IntelGraphics::RegisterIndex::DPLLControlA) & ~0x80000000);
  459. write_to_register(IntelGraphics::RegisterIndex::DPLLControlB, read_from_register(IntelGraphics::RegisterIndex::DPLLControlB) & ~0x80000000);
  460. }
  461. void IntelNativeGraphicsAdapter::disable_pipe_a()
  462. {
  463. VERIFY(m_control_lock.is_locked());
  464. VERIFY(m_modeset_lock.is_locked());
  465. write_to_register(IntelGraphics::RegisterIndex::PipeAConf, read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & ~0x80000000);
  466. dbgln_if(INTEL_GRAPHICS_DEBUG, "Disabling Pipe A");
  467. wait_for_disabled_pipe_a(100);
  468. dbgln_if(INTEL_GRAPHICS_DEBUG, "Disabling Pipe A - done.");
  469. }
  470. void IntelNativeGraphicsAdapter::disable_pipe_b()
  471. {
  472. VERIFY(m_control_lock.is_locked());
  473. VERIFY(m_modeset_lock.is_locked());
  474. write_to_register(IntelGraphics::RegisterIndex::PipeAConf, read_from_register(IntelGraphics::RegisterIndex::PipeBConf) & ~0x80000000);
  475. dbgln_if(INTEL_GRAPHICS_DEBUG, "Disabling Pipe B");
  476. wait_for_disabled_pipe_b(100);
  477. dbgln_if(INTEL_GRAPHICS_DEBUG, "Disabling Pipe B - done.");
  478. }
  479. void IntelNativeGraphicsAdapter::set_gmbus_default_rate()
  480. {
  481. // FIXME: Verify GMBUS Rate Select is set only when GMBUS is idle
  482. VERIFY(m_control_lock.is_locked());
  483. // Set the rate to 100KHz
  484. write_to_register(IntelGraphics::RegisterIndex::GMBusClock, read_from_register(IntelGraphics::RegisterIndex::GMBusClock) & ~(0b111 << 8));
  485. }
  486. void IntelNativeGraphicsAdapter::enable_pipe_a()
  487. {
  488. VERIFY(m_control_lock.is_locked());
  489. VERIFY(m_modeset_lock.is_locked());
  490. VERIFY(!(read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 31)));
  491. VERIFY(!(read_from_register(IntelGraphics::RegisterIndex::PipeAConf) & (1 << 30)));
  492. write_to_register(IntelGraphics::RegisterIndex::PipeAConf, read_from_register(IntelGraphics::RegisterIndex::PipeAConf) | 0x80000000);
  493. dbgln_if(INTEL_GRAPHICS_DEBUG, "enabling Pipe A");
  494. // FIXME: Seems like my video card is buggy and doesn't set the enabled bit (bit 30)!!
  495. wait_for_enabled_pipe_a(100);
  496. dbgln_if(INTEL_GRAPHICS_DEBUG, "enabling Pipe A - done.");
  497. }
  498. void IntelNativeGraphicsAdapter::enable_primary_plane(PhysicalAddress fb_address, size_t width)
  499. {
  500. VERIFY(m_control_lock.is_locked());
  501. VERIFY(m_modeset_lock.is_locked());
  502. VERIFY(((width * 4) % 64 == 0));
  503. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneAStride, width * 4);
  504. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneALinearOffset, 0);
  505. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneASurface, fb_address.get());
  506. // FIXME: Serenity uses BGR 32 bit pixel format, but maybe we should try to determine it somehow!
  507. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneAControl, (read_from_register(IntelGraphics::RegisterIndex::DisplayPlaneAControl) & (~(0b1111 << 26))) | (0b0110 << 26) | (1 << 31));
  508. }
  509. void IntelNativeGraphicsAdapter::set_dpll_registers(const PLLSettings& settings)
  510. {
  511. VERIFY(m_control_lock.is_locked());
  512. VERIFY(m_modeset_lock.is_locked());
  513. write_to_register(IntelGraphics::RegisterIndex::DPLLDivisorA0, (settings.m2 - 2) | ((settings.m1 - 2) << 8) | ((settings.n - 2) << 16));
  514. write_to_register(IntelGraphics::RegisterIndex::DPLLDivisorA1, (settings.m2 - 2) | ((settings.m1 - 2) << 8) | ((settings.n - 2) << 16));
  515. write_to_register(IntelGraphics::RegisterIndex::DPLLControlA, read_from_register(IntelGraphics::RegisterIndex::DPLLControlA) & ~0x80000000);
  516. }
  517. void IntelNativeGraphicsAdapter::enable_dpll_without_vga(const PLLSettings& settings, size_t dac_multiplier)
  518. {
  519. VERIFY(m_control_lock.is_locked());
  520. VERIFY(m_modeset_lock.is_locked());
  521. set_dpll_registers(settings);
  522. IO::delay(200);
  523. write_to_register(IntelGraphics::RegisterIndex::DPLLControlA, (6 << 9) | (settings.p1) << 16 | (1 << 26) | (1 << 28) | (1 << 31));
  524. write_to_register(IntelGraphics::RegisterIndex::DPLLMultiplierA, (dac_multiplier - 1) | ((dac_multiplier - 1) << 8));
  525. // The specification says we should wait (at least) about 150 microseconds
  526. // after enabling the DPLL to allow the clock to stabilize
  527. IO::delay(200);
  528. VERIFY(read_from_register(IntelGraphics::RegisterIndex::DPLLControlA) & (1 << 31));
  529. }
  530. void IntelNativeGraphicsAdapter::set_gmbus_pin_pair(GMBusPinPair pin_pair)
  531. {
  532. // FIXME: Verify GMBUS is idle
  533. VERIFY(m_control_lock.is_locked());
  534. write_to_register(IntelGraphics::RegisterIndex::GMBusClock, (read_from_register(IntelGraphics::RegisterIndex::GMBusClock) & (~0b111)) | (pin_pair & 0b111));
  535. }
  536. void IntelNativeGraphicsAdapter::disable_dac_output()
  537. {
  538. VERIFY(m_control_lock.is_locked());
  539. VERIFY(m_modeset_lock.is_locked());
  540. write_to_register(IntelGraphics::RegisterIndex::AnalogDisplayPort, 0b11 << 10);
  541. }
  542. void IntelNativeGraphicsAdapter::enable_dac_output()
  543. {
  544. VERIFY(m_control_lock.is_locked());
  545. VERIFY(m_modeset_lock.is_locked());
  546. write_to_register(IntelGraphics::RegisterIndex::AnalogDisplayPort, (read_from_register(IntelGraphics::RegisterIndex::AnalogDisplayPort) & (~(0b11 << 10))) | 0x80000000);
  547. }
  548. void IntelNativeGraphicsAdapter::disable_vga_emulation()
  549. {
  550. VERIFY(m_control_lock.is_locked());
  551. VERIFY(m_modeset_lock.is_locked());
  552. write_to_register(IntelGraphics::RegisterIndex::VGADisplayPlaneControl, (read_from_register(IntelGraphics::RegisterIndex::VGADisplayPlaneControl) & (~(1 << 30))) | 0x80000000);
  553. }
  554. void IntelNativeGraphicsAdapter::disable_all_planes()
  555. {
  556. VERIFY(m_control_lock.is_locked());
  557. VERIFY(m_modeset_lock.is_locked());
  558. write_to_register(IntelGraphics::RegisterIndex::DisplayPlaneAControl, read_from_register(IntelGraphics::RegisterIndex::DisplayPlaneAControl) & ~(1 << 31));
  559. }
  560. void IntelNativeGraphicsAdapter::initialize_framebuffer_devices()
  561. {
  562. auto address = PhysicalAddress(PCI::get_BAR2(pci_address()) & 0xfffffff0);
  563. VERIFY(!address.is_null());
  564. VERIFY(m_framebuffer_pitch != 0);
  565. VERIFY(m_framebuffer_height != 0);
  566. VERIFY(m_framebuffer_width != 0);
  567. m_framebuffer_device = FramebufferDevice::create(*this, 0, address, m_framebuffer_width, m_framebuffer_height, m_framebuffer_pitch);
  568. // FIXME: Would be nice to be able to return a KResult here.
  569. VERIFY(!m_framebuffer_device->initialize().is_error());
  570. }
  571. }