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