video.c 4.4 KB

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