video.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "video.h"
  2. #include <common/kprint.h>
  3. #include <common/kthread.h>
  4. #include <common/printk.h>
  5. #include <common/spinlock.h>
  6. #include <common/time.h>
  7. #include <driver/multiboot2/multiboot2.h>
  8. #include <driver/uart/uart.h>
  9. #include <exception/softirq.h>
  10. #include <mm/mm.h>
  11. #include <mm/slab.h>
  12. #include <process/process.h>
  13. #include <sched/sched.h>
  14. #include <time/timer.h>
  15. uint64_t video_refresh_expire_jiffies = 0;
  16. uint64_t video_last_refresh_pid = -1;
  17. struct scm_buffer_info_t video_frame_buffer_info = {0};
  18. static struct multiboot_tag_framebuffer_info_t __fb_info;
  19. static struct scm_buffer_info_t *video_refresh_target = NULL;
  20. static struct process_control_block *video_daemon_pcb = NULL;
  21. static spinlock_t daemon_refresh_lock;
  22. #define REFRESH_INTERVAL 15UL // 启动刷新帧缓冲区任务的时间间隔
  23. /**
  24. * @brief VBE帧缓存区的地址重新映射
  25. * 将帧缓存区映射到地址SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE处
  26. */
  27. void init_frame_buffer()
  28. {
  29. kinfo("Re-mapping VBE frame buffer...");
  30. uint64_t global_CR3 = (uint64_t)get_CR3();
  31. struct multiboot_tag_framebuffer_info_t info;
  32. int reserved;
  33. video_frame_buffer_info.vaddr = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + FRAME_BUFFER_MAPPING_OFFSET;
  34. mm_map_proc_page_table(global_CR3, true, video_frame_buffer_info.vaddr, __fb_info.framebuffer_addr,
  35. video_frame_buffer_info.size, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false, true, false);
  36. flush_tlb();
  37. kinfo("VBE frame buffer successfully Re-mapped!");
  38. }
  39. /**
  40. * @brief video守护进程, 按时刷新帧缓冲区
  41. * @param unused
  42. * @return int
  43. */
  44. int video_refresh_daemon(void *unused)
  45. {
  46. // 初始化锁, 这个锁只会在daemon中使用
  47. spin_init(&daemon_refresh_lock);
  48. for (;;)
  49. {
  50. if (clock() >= video_refresh_expire_jiffies)
  51. {
  52. if (likely(video_refresh_target != NULL))
  53. {
  54. spin_lock(&daemon_refresh_lock);
  55. memcpy((void *)video_frame_buffer_info.vaddr, (void *)video_refresh_target->vaddr,
  56. video_refresh_target->size);
  57. spin_unlock(&daemon_refresh_lock);
  58. video_daemon_pcb->virtual_runtime = 0xfffff0000000; // 临时解决由于显示刷新进程的虚拟运行时间过大/过小,导致其不运行,或者一直运行的问题。将来应使用实时调度解决它
  59. }
  60. video_refresh_expire_jiffies = cal_next_n_ms_jiffies(REFRESH_INTERVAL << 1);
  61. }
  62. video_daemon_pcb->state &= ~PROC_RUNNING;
  63. video_daemon_pcb->flags |= PF_NEED_SCHED;
  64. sched();
  65. }
  66. return 0;
  67. }
  68. /**
  69. * @brief 唤醒video的守护进程
  70. */
  71. void video_refresh_framebuffer(void *data)
  72. {
  73. if (unlikely(video_daemon_pcb == NULL))
  74. return;
  75. if (clock() >= video_refresh_expire_jiffies)
  76. {
  77. video_daemon_pcb->virtual_runtime = 0;
  78. process_wakeup(video_daemon_pcb);
  79. }
  80. }
  81. /**
  82. * @brief 初始化显示模块,需先低级初始化才能高级初始化
  83. * @param level 初始化等级
  84. * false -> 低级初始化:不使用double buffer
  85. * true ->高级初始化:增加double buffer的支持
  86. * @return int
  87. */
  88. int video_reinitialize(bool level) // 这个函数会在main.c调用, 保证 video_init() 先被调用
  89. {
  90. if (level == false)
  91. init_frame_buffer();
  92. else
  93. {
  94. unregister_softirq(VIDEO_REFRESH_SIRQ);
  95. // 计算开始时间
  96. video_refresh_expire_jiffies = cal_next_n_ms_jiffies(10 * REFRESH_INTERVAL);
  97. // 创建video守护进程
  98. video_daemon_pcb = kthread_run(&video_refresh_daemon, NULL, "Video refresh daemon");
  99. video_daemon_pcb->virtual_runtime = 0; // 特殊情况, 最高优先级, 以后再改
  100. // 启用屏幕刷新软中断
  101. register_softirq(VIDEO_REFRESH_SIRQ, &video_refresh_framebuffer, NULL);
  102. raise_softirq(VIDEO_REFRESH_SIRQ);
  103. }
  104. return 0;
  105. }
  106. /**
  107. * @brief 设置帧缓冲区刷新目标
  108. *
  109. * @param buf
  110. * @return int
  111. */
  112. int video_set_refresh_target(struct scm_buffer_info_t *buf)
  113. {
  114. unregister_softirq(VIDEO_REFRESH_SIRQ);
  115. // todo: 在completion实现后,在这里等待其他刷新任务完成,再进行下一步。
  116. // int counter = 100;
  117. // while ((get_softirq_pending() & (1 << VIDEO_REFRESH_SIRQ)) && counter > 0)
  118. // {
  119. // --counter;
  120. // usleep(1000);
  121. // }
  122. // kdebug("buf = %#018lx", buf);
  123. video_refresh_target = buf;
  124. register_softirq(VIDEO_REFRESH_SIRQ, &video_refresh_framebuffer, NULL);
  125. raise_softirq(VIDEO_REFRESH_SIRQ);
  126. }
  127. /**
  128. * @brief 初始化显示驱动
  129. *
  130. * @return int
  131. */
  132. int video_init()
  133. {
  134. memset(&video_frame_buffer_info, 0, sizeof(struct scm_buffer_info_t));
  135. memset(&__fb_info, 0, sizeof(struct multiboot_tag_framebuffer_info_t));
  136. video_refresh_target = NULL;
  137. io_mfence();
  138. // 从multiboot2获取帧缓冲区信息
  139. int reserved;
  140. multiboot2_iter(multiboot2_get_Framebuffer_info, &__fb_info, &reserved);
  141. io_mfence();
  142. // 初始化帧缓冲区信息结构体
  143. if (__fb_info.framebuffer_type == 2)
  144. {
  145. video_frame_buffer_info.bit_depth = 8; // type=2时,width和height是按照字符数来表示的,因此depth=8
  146. video_frame_buffer_info.flags |= SCM_BF_TEXT;
  147. }
  148. else
  149. {
  150. video_frame_buffer_info.bit_depth = __fb_info.framebuffer_bpp;
  151. video_frame_buffer_info.flags |= SCM_BF_PIXEL;
  152. }
  153. video_frame_buffer_info.flags |= SCM_BF_FB;
  154. video_frame_buffer_info.width = __fb_info.framebuffer_width;
  155. video_frame_buffer_info.height = __fb_info.framebuffer_height;
  156. io_mfence();
  157. video_frame_buffer_info.size =
  158. video_frame_buffer_info.width * video_frame_buffer_info.height * ((video_frame_buffer_info.bit_depth + 7) / 8);
  159. // 先临时映射到该地址,稍后再重新映射
  160. video_frame_buffer_info.vaddr = 0xffff800003000000;
  161. mm_map_phys_addr(video_frame_buffer_info.vaddr, __fb_info.framebuffer_addr, video_frame_buffer_info.size,
  162. PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
  163. io_mfence();
  164. char init_text2[] = "Video driver initialized.\n";
  165. for (int i = 0; i < sizeof(init_text2) - 1; ++i)
  166. c_uart_send(COM1, init_text2[i]);
  167. return 0;
  168. }