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