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->state &= ~PROC_RUNNING;
74 video_daemon_pcb->flags |= PF_NEED_SCHED;
75 sched();
76 }
77
78 return 0;
79 }
80
81 /**
82 * @brief 唤醒video的守护进程
83 */
video_refresh_framebuffer(void * data)84 void video_refresh_framebuffer(void *data)
85 {
86 if (unlikely(video_daemon_pcb == NULL))
87 return;
88 if (clock() >= video_refresh_expire_jiffies)
89 {
90 video_daemon_pcb->virtual_runtime = 0;
91 process_wakeup(video_daemon_pcb);
92 }
93 }
94
95 /**
96 * @brief 初始化显示模块,需先低级初始化才能高级初始化
97 * @param level 初始化等级
98 * false -> 低级初始化:不使用double buffer
99 * true ->高级初始化:增加double buffer的支持
100 * @return int
101 */
video_reinitialize(bool level)102 int video_reinitialize(bool level) // 这个函数会在main.c调用, 保证 video_init() 先被调用
103 {
104 if (level == false)
105 init_frame_buffer();
106 else
107 {
108 // 计算开始时间
109 video_refresh_expire_jiffies = cal_next_n_ms_jiffies(10 * REFRESH_INTERVAL);
110
111 // 创建video守护进程
112 video_daemon_pcb = kthread_run(&video_refresh_daemon, NULL, "Video refresh daemon");
113 video_daemon_pcb->virtual_runtime = 0; // 特殊情况, 最高优先级, 以后再改
114
115 // 启用屏幕刷新软中断
116 register_softirq(VIDEO_REFRESH_SIRQ, &video_refresh_framebuffer, NULL);
117
118 raise_softirq(VIDEO_REFRESH_SIRQ);
119 }
120 return 0;
121 }
122
123 /**
124 * @brief 设置帧缓冲区刷新目标
125 *
126 * @param buf
127 * @return int
128 */
video_set_refresh_target(struct scm_buffer_info_t * buf)129 int video_set_refresh_target(struct scm_buffer_info_t *buf)
130 {
131
132 unregister_softirq(VIDEO_REFRESH_SIRQ);
133 // todo: 在completion实现后,在这里等待其他刷新任务完成,再进行下一步。
134
135 // int counter = 100;
136
137 // while ((get_softirq_pending() & (1 << VIDEO_REFRESH_SIRQ)) && counter > 0)
138 // {
139 // --counter;
140 // usleep(1000);
141 // }
142 // kdebug("buf = %#018lx", buf);
143 video_refresh_target = buf;
144 register_softirq(VIDEO_REFRESH_SIRQ, &video_refresh_framebuffer, NULL);
145 raise_softirq(VIDEO_REFRESH_SIRQ);
146 }
147
148 /**
149 * @brief 初始化显示驱动
150 *
151 * @return int
152 */
video_init()153 int video_init()
154 {
155
156 memset(&video_frame_buffer_info, 0, sizeof(struct scm_buffer_info_t));
157 memset(&__fb_info, 0, sizeof(struct multiboot_tag_framebuffer_info_t));
158 video_refresh_target = NULL;
159
160 io_mfence();
161 // 从multiboot2获取帧缓冲区信息
162 int reserved;
163 multiboot2_iter(multiboot2_get_Framebuffer_info, &__fb_info, &reserved);
164 io_mfence();
165
166 // 初始化帧缓冲区信息结构体
167 if (__fb_info.framebuffer_type == 2)
168 {
169 video_frame_buffer_info.bit_depth = 8; // type=2时,width和height是按照字符数来表示的,因此depth=8
170 video_frame_buffer_info.flags |= SCM_BF_TEXT;
171 }
172 else
173 {
174 video_frame_buffer_info.bit_depth = __fb_info.framebuffer_bpp;
175 video_frame_buffer_info.flags |= SCM_BF_PIXEL;
176 }
177
178 video_frame_buffer_info.flags |= SCM_BF_FB;
179 video_frame_buffer_info.width = __fb_info.framebuffer_width;
180 video_frame_buffer_info.height = __fb_info.framebuffer_height;
181 io_mfence();
182
183 video_frame_buffer_info.size =
184 video_frame_buffer_info.width * video_frame_buffer_info.height * ((video_frame_buffer_info.bit_depth + 7) / 8);
185 // 先临时映射到该地址,稍后再重新映射
186 video_frame_buffer_info.vaddr = 0xffff800003000000;
187 mm_map_phys_addr(video_frame_buffer_info.vaddr, __fb_info.framebuffer_addr, video_frame_buffer_info.size,
188 PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
189
190 io_mfence();
191 char init_text2[] = "Video driver initialized.\n";
192 for (int i = 0; i < sizeof(init_text2) - 1; ++i)
193 c_uart_send(COM1, init_text2[i]);
194
195 return 0;
196 }