video.c 3.6 KB

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