video.c 3.6 KB

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