video.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "video.h"
  2. #include <mm/mm.h>
  3. #include <common/printk.h>
  4. #include <driver/multiboot2/multiboot2.h>
  5. #include <driver/timers/timer.h>
  6. #include <common/kprint.h>
  7. #include <mm/mm.h>
  8. #include <mm/slab.h>
  9. #define REFRESH_INTERVAL 15 // 启动刷新帧缓冲区任务的时间间隔
  10. ul VBE_FB_phys_addr; // 由bootloader传来的帧缓存区的物理地址
  11. struct screen_info_t
  12. {
  13. int width, height;
  14. uint64_t length;
  15. uint64_t fb_vaddr, fb_paddr;
  16. uint64_t double_fb_vaddr;
  17. } sc_info;
  18. /**
  19. * @brief VBE帧缓存区的地址重新映射
  20. * 将帧缓存区映射到地址0xffff800003000000处
  21. */
  22. void init_frame_buffer(bool level)
  23. {
  24. kinfo("Re-mapping VBE frame buffer...");
  25. uint64_t global_CR3 = (uint64_t)get_CR3();
  26. if (level == false)
  27. {
  28. struct multiboot_tag_framebuffer_info_t info;
  29. int reserved;
  30. multiboot2_iter(multiboot2_get_Framebuffer_info, &info, &reserved);
  31. sc_info.fb_vaddr = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + FRAME_BUFFER_MAPPING_OFFSET;
  32. sc_info.fb_paddr = info.framebuffer_addr;
  33. sc_info.width = info.framebuffer_width;
  34. sc_info.height = info.framebuffer_height;
  35. sc_info.length = 1UL * sc_info.width * sc_info.height;
  36. mm_map_proc_page_table(global_CR3, true, sc_info.fb_vaddr, sc_info.fb_paddr, get_VBE_FB_length() << 2, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
  37. set_pos_VBE_FB_addr((uint *)sc_info.fb_vaddr);
  38. }
  39. else // 高级初始化,增加双缓冲区的支持
  40. {
  41. // 申请双重缓冲区
  42. struct Page *p = alloc_pages(ZONE_NORMAL, PAGE_2M_ALIGN(sc_info.length << 2) / PAGE_2M_SIZE, 0);
  43. sc_info.double_fb_vaddr = (uint64_t)phys_2_virt(p->addr_phys);
  44. mm_map_proc_page_table(global_CR3, true, sc_info.double_fb_vaddr, p->addr_phys, PAGE_2M_ALIGN(sc_info.length << 2), PAGE_KERNEL_PAGE, false);
  45. // 将原有的数据拷贝到double buffer里面
  46. memcpy((void *)sc_info.double_fb_vaddr, (void *)sc_info.fb_vaddr, sc_info.length << 2);
  47. set_pos_VBE_FB_addr((uint *)sc_info.double_fb_vaddr);
  48. }
  49. flush_tlb();
  50. kinfo("VBE frame buffer successfully Re-mapped!");
  51. }
  52. /**
  53. * @brief 刷新帧缓冲区
  54. *
  55. */
  56. static void video_refresh_framebuffer()
  57. {
  58. memcpy((void *)sc_info.fb_vaddr, (void *)sc_info.double_fb_vaddr, (sc_info.length << 2));
  59. // 新增下一个刷新定时任务
  60. struct timer_func_list_t *tmp = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
  61. timer_func_init(tmp, &video_refresh_framebuffer, NULL, REFRESH_INTERVAL);
  62. timer_func_add(tmp);
  63. }
  64. /**
  65. * @brief 初始化显示模块,需先低级初始化才能高级初始化
  66. * @param level 初始化等级
  67. * false -> 低级初始化:不使用double buffer
  68. * true ->高级初始化:增加double buffer的支持
  69. * @return int
  70. */
  71. int video_init(bool level)
  72. {
  73. init_frame_buffer(level);
  74. if (level)
  75. {
  76. // 启用双缓冲后,使能printk滚动动画
  77. // printk_enable_animation();
  78. // 初始化第一个屏幕刷新任务
  79. struct timer_func_list_t *tmp = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
  80. timer_func_init(tmp, &video_refresh_framebuffer, NULL, REFRESH_INTERVAL);
  81. timer_func_add(tmp);
  82. }
  83. }