IntelNativeGraphicsAdapter.cpp 27 KB

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