video.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. video_refresh_expire_jiffies = cal_next_n_ms_jiffies(REFRESH_INTERVAL << 1);
  53. if (likely(video_refresh_target != NULL))
  54. {
  55. spin_lock(&daemon_refresh_lock);
  56. memcpy((void *)video_frame_buffer_info.vaddr, (void *)video_refresh_target->vaddr,
  57. video_refresh_target->size);
  58. spin_unlock(&daemon_refresh_lock);
  59. }
  60. }
  61. video_daemon_pcb->flags &= ~PROC_RUNNING;
  62. sched();
  63. }
  64. return 0;
  65. }
  66. /**
  67. * @brief 唤醒video的守护进程
  68. */
  69. void video_refresh_framebuffer(void *data)
  70. {
  71. if (unlikely(video_daemon_pcb == NULL))
  72. return;
  73. process_wakeup(video_daemon_pcb);
  74. }
  75. /**
  76. * @brief 初始化显示模块,需先低级初始化才能高级初始化
  77. * @param level 初始化等级
  78. * false -> 低级初始化:不使用double buffer
  79. * true ->高级初始化:增加double buffer的支持
  80. * @return int
  81. */
  82. int video_reinitialize(bool level) // 这个函数会在main.c调用, 保证 video_init() 先被调用
  83. {
  84. if (level == false)
  85. init_frame_buffer();
  86. else
  87. {
  88. // 计算开始时间
  89. video_refresh_expire_jiffies = cal_next_n_ms_jiffies(10 * REFRESH_INTERVAL);
  90. // 创建video守护进程
  91. video_daemon_pcb = kthread_run(&video_refresh_daemon, NULL, CLONE_FS | CLONE_SIGNAL);
  92. video_daemon_pcb->virtual_runtime = 0; // 特殊情况, 最高优先级, 以后再改
  93. // 启用屏幕刷新软中断
  94. register_softirq(VIDEO_REFRESH_SIRQ, &video_refresh_framebuffer, NULL);
  95. raise_softirq(VIDEO_REFRESH_SIRQ);
  96. }
  97. return 0;
  98. }
  99. /**
  100. * @brief 设置帧缓冲区刷新目标
  101. *
  102. * @param buf
  103. * @return int
  104. */
  105. int video_set_refresh_target(struct scm_buffer_info_t *buf)
  106. {
  107. unregister_softirq(VIDEO_REFRESH_SIRQ);
  108. // todo: 在completion实现后,在这里等待其他刷新任务完成,再进行下一步。
  109. // int counter = 100;
  110. // while ((get_softirq_pending() & (1 << VIDEO_REFRESH_SIRQ)) && counter > 0)
  111. // {
  112. // --counter;
  113. // usleep(1000);
  114. // }
  115. // kdebug("buf = %#018lx", buf);
  116. video_refresh_target = buf;
  117. register_softirq(VIDEO_REFRESH_SIRQ, &video_refresh_framebuffer, NULL);
  118. raise_softirq(VIDEO_REFRESH_SIRQ);
  119. }
  120. /**
  121. * @brief 初始化显示驱动
  122. *
  123. * @return int
  124. */
  125. int video_init()
  126. {
  127. memset(&video_frame_buffer_info, 0, sizeof(struct scm_buffer_info_t));
  128. memset(&__fb_info, 0, sizeof(struct multiboot_tag_framebuffer_info_t));
  129. video_refresh_target = NULL;
  130. io_mfence();
  131. // 从multiboot2获取帧缓冲区信息
  132. int reserved;
  133. multiboot2_iter(multiboot2_get_Framebuffer_info, &__fb_info, &reserved);
  134. io_mfence();
  135. // 初始化帧缓冲区信息结构体
  136. if (__fb_info.framebuffer_type == 2)
  137. {
  138. video_frame_buffer_info.bit_depth = 8; // type=2时,width和height是按照字符数来表示的,因此depth=8
  139. video_frame_buffer_info.flags |= SCM_BF_TEXT;
  140. }
  141. else
  142. {
  143. video_frame_buffer_info.bit_depth = __fb_info.framebuffer_bpp;
  144. video_frame_buffer_info.flags |= SCM_BF_PIXEL;
  145. }
  146. video_frame_buffer_info.flags |= SCM_BF_FB;
  147. video_frame_buffer_info.width = __fb_info.framebuffer_width;
  148. video_frame_buffer_info.height = __fb_info.framebuffer_height;
  149. io_mfence();
  150. video_frame_buffer_info.size =
  151. video_frame_buffer_info.width * video_frame_buffer_info.height * ((video_frame_buffer_info.bit_depth + 7) / 8);
  152. // 先临时映射到该地址,稍后再重新映射
  153. video_frame_buffer_info.vaddr = 0xffff800003000000;
  154. mm_map_phys_addr(video_frame_buffer_info.vaddr, __fb_info.framebuffer_addr, video_frame_buffer_info.size,
  155. PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
  156. io_mfence();
  157. char init_text2[] = "Video driver initialized.";
  158. for (int i = 0; i < sizeof(init_text2) - 1; ++i)
  159. uart_send(COM1, init_text2[i]);
  160. return 0;
  161. }