video.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "video.h"
  2. #include <mm/mm.h>
  3. #include <common/printk.h>
  4. #include <driver/multiboot2/multiboot2.h>
  5. #include <time/timer.h>
  6. #include <common/kprint.h>
  7. #include <mm/mm.h>
  8. #include <mm/slab.h>
  9. #include <common/spinlock.h>
  10. #include <exception/softirq.h>
  11. #include <driver/uart/uart.h>
  12. #include <common/time.h>
  13. uint64_t video_refresh_expire_jiffies = 0;
  14. uint64_t video_last_refresh_pid = -1;
  15. struct scm_buffer_info_t video_frame_buffer_info = {0};
  16. static struct multiboot_tag_framebuffer_info_t __fb_info;
  17. static struct scm_buffer_info_t *video_refresh_target = NULL;
  18. #define REFRESH_INTERVAL 15UL // 启动刷新帧缓冲区任务的时间间隔
  19. /**
  20. * @brief VBE帧缓存区的地址重新映射
  21. * 将帧缓存区映射到地址SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE处
  22. */
  23. void init_frame_buffer()
  24. {
  25. kinfo("Re-mapping VBE frame buffer...");
  26. uint64_t global_CR3 = (uint64_t)get_CR3();
  27. struct multiboot_tag_framebuffer_info_t info;
  28. int reserved;
  29. video_frame_buffer_info.vaddr = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + FRAME_BUFFER_MAPPING_OFFSET;
  30. mm_map_proc_page_table(global_CR3, true, video_frame_buffer_info.vaddr, __fb_info.framebuffer_addr, video_frame_buffer_info.size, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false, true, false);
  31. flush_tlb();
  32. kinfo("VBE frame buffer successfully Re-mapped!");
  33. }
  34. /**
  35. * @brief 刷新帧缓冲区
  36. *
  37. */
  38. void video_refresh_framebuffer(void *data)
  39. {
  40. video_refresh_expire_jiffies = cal_next_n_ms_jiffies(REFRESH_INTERVAL << 1);
  41. if (unlikely(video_refresh_target == NULL))
  42. return;
  43. memcpy((void *)video_frame_buffer_info.vaddr, (void *)video_refresh_target->vaddr, video_refresh_target->size);
  44. }
  45. /**
  46. * @brief 初始化显示模块,需先低级初始化才能高级初始化
  47. * @param level 初始化等级
  48. * false -> 低级初始化:不使用double buffer
  49. * true ->高级初始化:增加double buffer的支持
  50. * @return int
  51. */
  52. int video_reinitialize(bool level)
  53. {
  54. if (level == false)
  55. init_frame_buffer();
  56. else
  57. {
  58. // 启用屏幕刷新软中断
  59. register_softirq(VIDEO_REFRESH_SIRQ, &video_refresh_framebuffer, NULL);
  60. video_refresh_expire_jiffies = cal_next_n_ms_jiffies(10 * REFRESH_INTERVAL);
  61. raise_softirq(VIDEO_REFRESH_SIRQ);
  62. }
  63. return 0;
  64. }
  65. /**
  66. * @brief 设置帧缓冲区刷新目标
  67. *
  68. * @param buf
  69. * @return int
  70. */
  71. int video_set_refresh_target(struct scm_buffer_info_t *buf)
  72. {
  73. unregister_softirq(VIDEO_REFRESH_SIRQ);
  74. int counter = 100;
  75. while ((get_softirq_pending() & (1 << VIDEO_REFRESH_SIRQ)) && counter > 0)
  76. {
  77. --counter;
  78. usleep(1000);
  79. }
  80. video_refresh_target = buf;
  81. register_softirq(VIDEO_REFRESH_SIRQ, &video_refresh_framebuffer, NULL);
  82. raise_softirq(VIDEO_REFRESH_SIRQ);
  83. }
  84. /**
  85. * @brief 初始化显示驱动
  86. *
  87. * @return int
  88. */
  89. int video_init()
  90. {
  91. memset(&video_frame_buffer_info, 0, sizeof(struct scm_buffer_info_t));
  92. memset(&__fb_info, 0, sizeof(struct multiboot_tag_framebuffer_info_t));
  93. video_refresh_target = NULL;
  94. io_mfence();
  95. // 从multiboot2获取帧缓冲区信息
  96. int reserved;
  97. multiboot2_iter(multiboot2_get_Framebuffer_info, &__fb_info, &reserved);
  98. io_mfence();
  99. // 初始化帧缓冲区信息结构体
  100. if (__fb_info.framebuffer_type == 2)
  101. {
  102. video_frame_buffer_info.bit_depth = 8; // type=2时,width和height是按照字符数来表示的,因此depth=8
  103. video_frame_buffer_info.flags |= SCM_BF_TEXT;
  104. }
  105. else
  106. {
  107. video_frame_buffer_info.bit_depth = __fb_info.framebuffer_bpp;
  108. video_frame_buffer_info.flags |= SCM_BF_PIXEL;
  109. }
  110. video_frame_buffer_info.flags |= SCM_BF_FB;
  111. video_frame_buffer_info.width = __fb_info.framebuffer_width;
  112. video_frame_buffer_info.height = __fb_info.framebuffer_height;
  113. io_mfence();
  114. video_frame_buffer_info.size = video_frame_buffer_info.width * video_frame_buffer_info.height * ((video_frame_buffer_info.bit_depth + 7) / 8);
  115. // 先临时映射到该地址,稍后再重新映射
  116. video_frame_buffer_info.vaddr = 0xffff800003000000;
  117. mm_map_phys_addr(video_frame_buffer_info.vaddr, __fb_info.framebuffer_addr, video_frame_buffer_info.size, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
  118. io_mfence();
  119. char init_text2[] = "Video driver initialized.";
  120. for (int i = 0; i < sizeof(init_text2) - 1; ++i)
  121. uart_send(COM1, init_text2[i]);
  122. return 0;
  123. }