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