1 #include "acpi.h"
2 #include <common/printk.h>
3 #include <common/kprint.h>
4 #include <driver/multiboot2/multiboot2.h>
5 #include <mm/mm.h>
6 #include <mm/mmio.h>
7
8 #define acpi_get_RSDT_entry_vaddr(phys_addr) (acpi_description_header_base + (phys_addr)-acpi_RSDT_entry_phys_base) // 获取RSDT entry的虚拟地址
9 // #define acpi_get_XSDT_entry_vaddr(phys_addr) (ACPI_DESCRIPTION_HEDERS_BASE + (phys_addr)-acpi_XSDT_entry_phys_base) // 获取XSDT entry的虚拟地址
10
11 static struct acpi_RSDP_t *rsdpv1;
12 static struct acpi_RSDP_2_t *rsdpv2;
13 static struct acpi_RSDT_Structure_t *rsdt;
14 static struct acpi_XSDT_Structure_t *xsdt;
15
16 static struct multiboot_tag_old_acpi_t old_acpi;
17 static struct multiboot_tag_new_acpi_t new_acpi;
18
19 static ul acpi_RSDT_offset = 0;
20 static ul acpi_XSDT_offset = 0;
21 static uint acpi_RSDT_Entry_num = 0;
22 static uint acpi_XSDT_Entry_num = 0;
23
24 static ul acpi_RSDT_entry_phys_base = 0; // RSDT中的第一个entry所在物理页的基地址
25
26 static uint64_t acpi_madt_vaddr = 0; // MADT的虚拟地址
27 static uint64_t acpi_rsdt_virt_addr_base = 0; // RSDT的虚拟地址
28 static uint64_t acpi_description_header_base = 0; // RSDT中的第一个entry所在虚拟地址
29
30 // static ul acpi_XSDT_entry_phys_base = 0; // XSDT中的第一个entry所在物理页的基地址
31
32 /**
33 * @brief 迭代器,用于迭代描述符头(位于ACPI标准文件的Table 5-29)
34 * @param _fun 迭代操作调用的函数
35 * @param _data 数据
36 */
acpi_iter_SDT(bool (* _fun)(const struct acpi_system_description_table_header_t *,void *),void * _data)37 void acpi_iter_SDT(bool (*_fun)(const struct acpi_system_description_table_header_t *, void *),
38 void *_data)
39 {
40
41 struct acpi_system_description_table_header_t *sdt_header;
42 if (acpi_use_xsdt)
43 {
44 ul *ent = &(xsdt->Entry);
45 for (int i = 0; i < acpi_XSDT_Entry_num; ++i)
46 {
47 mm_map_phys_addr(acpi_description_header_base + PAGE_2M_SIZE * i, (*(ent + i)) & PAGE_2M_MASK, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
48 sdt_header = (struct acpi_system_description_table_header_t *)((ul)(acpi_description_header_base + PAGE_2M_SIZE * i));
49
50 if (_fun(sdt_header, _data) == true)
51 return;
52 }
53 }
54 else
55 {
56 uint *ent = &(rsdt->Entry);
57 for (int i = 0; i < acpi_RSDT_Entry_num; ++i)
58 {
59
60 sdt_header = (struct acpi_system_description_table_header_t *)(acpi_get_RSDT_entry_vaddr((ul)(*(ent + i))));
61
62 if (_fun(sdt_header, _data) == true)
63 return;
64 }
65 }
66
67 return;
68 }
69
70 /**
71 * @brief 获取MADT信息 Multiple APIC Description Table
72 *
73 * @param _iter_data 要被迭代的信息的结构体
74 * @param _data 返回的MADT的虚拟地址
75 * @param count 返回数组的长度
76 * @return true
77 * @return false
78 */
acpi_get_MADT(const struct acpi_system_description_table_header_t * _iter_data,void * _data)79 bool acpi_get_MADT(const struct acpi_system_description_table_header_t *_iter_data, void *_data)
80 {
81 if (!(_iter_data->Signature[0] == 'A' && _iter_data->Signature[1] == 'P' && _iter_data->Signature[2] == 'I' && _iter_data->Signature[3] == 'C'))
82 return false;
83 //*(struct acpi_Multiple_APIC_Description_Table_t *)_data = *(struct acpi_Multiple_APIC_Description_Table_t *)_iter_data;
84 // 返回MADT的虚拟地址
85 *(ul *)_data = (ul)_iter_data;
86 acpi_madt_vaddr = (ul)_iter_data;
87 return true;
88 }
89
90 /**
91 * @brief 获取HPET HPET_description_table
92 *
93 * @param _iter_data 要被迭代的信息的结构体
94 * @param _data 返回的HPET表的虚拟地址
95 * @return true
96 * @return false
97 */
acpi_get_HPET(const struct acpi_system_description_table_header_t * _iter_data,void * _data)98 bool acpi_get_HPET(const struct acpi_system_description_table_header_t *_iter_data, void *_data)
99 {
100 if (!(_iter_data->Signature[0] == 'H' && _iter_data->Signature[1] == 'P' && _iter_data->Signature[2] == 'E' && _iter_data->Signature[3] == 'T'))
101 return false;
102 *(ul *)_data = (ul)_iter_data;
103 return true;
104 }
105
106 /**
107 * @brief 获取MCFG MCFG_description_table
108 *
109 * @param _iter_data 要被迭代的信息的结构体
110 * @param _data 返回的MCFG表的虚拟地址
111 * @return true
112 * @return false
113 */
acpi_get_MCFG(const struct acpi_system_description_table_header_t * _iter_data,void * _data)114 bool acpi_get_MCFG(const struct acpi_system_description_table_header_t *_iter_data, void *_data)
115 {
116 if (!(_iter_data->Signature[0] == 'M' && _iter_data->Signature[1] == 'C' && _iter_data->Signature[2] == 'F' && _iter_data->Signature[3] == 'G'))
117 return false;
118 *(ul *)_data = (ul)_iter_data;
119 return true;
120 }
121
122 /**
123 * @brief 初始化acpi模块
124 *
125 */
126 // todo: 修复bug:当物理机上提供了rsdpv2之后,rsdpv1是不提供的(物理地址为0),因此需要手动判断rsdp的版本信息,然后做对应的解析。
acpi_init()127 void acpi_init()
128 {
129 kinfo("Initializing ACPI...");
130
131 // 获取物理地址
132 int reserved;
133
134 multiboot2_iter(multiboot2_get_acpi_old_RSDP, &old_acpi, &reserved);
135 rsdpv1 = &(old_acpi.rsdp);
136
137 multiboot2_iter(multiboot2_get_acpi_new_RSDP, &new_acpi, &reserved);
138 rsdpv2 = &(new_acpi.rsdp);
139
140 uint64_t paddr = 0;
141 // An ACPI-compatible OS must use the XSDT if present
142 if (rsdpv2->XsdtAddress != 0x00UL)
143 {
144 // 不要删除这段注释(因为还不确定是代码的bug,还是真机的bug)
145 /*
146 acpi_use_xsdt = true;
147 ul xsdt_phys_base = rsdpv2->XsdtAddress & PAGE_2M_MASK;
148 acpi_XSDT_offset = rsdpv2->XsdtAddress - xsdt_phys_base;
149 mm_map_phys_addr(ACPI_XSDT_VIRT_ADDR_BASE, xsdt_phys_base, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
150 kdebug("XSDT mapped!");
151
152 xsdt = (struct acpi_XSDT_Structure_t *)(ACPI_XSDT_VIRT_ADDR_BASE + acpi_XSDT_offset);
153 // 计算RSDT Entry的数量
154 kdebug("offset=%d", sizeof(xsdt->header));
155 kdebug("xsdt sign=%s", xsdt->header.Signature);
156 acpi_XSDT_Entry_num = (xsdt->header.Length - sizeof(xsdt->header)) / 8;
157
158 printk_color(ORANGE, BLACK, "XSDT Length=%dbytes.\n", xsdt->header.Length);
159 printk_color(ORANGE, BLACK, "XSDT Entry num=%d\n", acpi_XSDT_Entry_num);
160
161 mm_map_phys_addr(ACPI_XSDT_VIRT_ADDR_BASE, xsdt_phys_base, xsdt->header.Length + PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
162 // 映射所有的Entry的物理地址
163 ul *ent = &(xsdt->Entry);
164 for (int j = 0; j < acpi_XSDT_Entry_num; ++j)
165 {
166 kdebug("entry=%#018lx, virt=%#018lx", (*(ent + j)) & PAGE_2M_MASK, ACPI_XSDT_DESCRIPTION_HEDERS_BASE + PAGE_2M_SIZE * j);
167 // 映射RSDT ENTRY的物理地址
168 mm_map_phys_addr(ACPI_XSDT_DESCRIPTION_HEDERS_BASE + PAGE_2M_SIZE * j, (*(ent + j)) & PAGE_2M_MASK, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
169 }
170 */
171
172 // 由于解析XSDT出现问题。暂时只使用Rsdpv2的rsdt,但是这是不符合ACPI规范的!!!
173 ul rsdt_phys_base = rsdpv2->rsdp1.RsdtAddress & PAGE_2M_MASK;
174 acpi_RSDT_offset = rsdpv2->rsdp1.RsdtAddress - rsdt_phys_base;
175
176 //申请mmio空间
177 uint64_t size = 0;
178 mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_rsdt_virt_addr_base, &size);
179
180 //映射rsdt表
181 paddr = (uint64_t)rsdt_phys_base;
182 mm_map(&initial_mm, acpi_rsdt_virt_addr_base, PAGE_2M_SIZE, paddr);
183
184 // rsdt表虚拟地址
185 rsdt = (struct acpi_RSDT_Structure_t *)(acpi_rsdt_virt_addr_base + acpi_RSDT_offset);
186 kdebug("RSDT mapped!(v2)");
187
188 // 计算RSDT Entry的数量
189 kdebug("offset=%d", sizeof(rsdt->header));
190 acpi_RSDT_Entry_num = (rsdt->header.Length - 36) / 4;
191
192 printk_color(ORANGE, BLACK, "RSDT Length=%dbytes.\n", rsdt->header.Length);
193 printk_color(ORANGE, BLACK, "RSDT Entry num=%d\n", acpi_RSDT_Entry_num);
194
195 //申请mmio空间
196 mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_description_header_base, &size);
197
198 // 映射所有的Entry的物理地址
199 acpi_RSDT_entry_phys_base = ((ul)(rsdt->Entry)) & PAGE_2M_MASK;
200 // 由于地址只是32bit的,并且存在脏数据,这里需要手动清除高32bit,否则会触发#GP
201 acpi_RSDT_entry_phys_base = MASK_HIGH_32bit(acpi_RSDT_entry_phys_base);
202
203 paddr = (uint64_t)acpi_RSDT_entry_phys_base;
204 mm_map(&initial_mm, acpi_description_header_base, PAGE_2M_SIZE, paddr);
205 }
206 else if (rsdpv1->RsdtAddress != (uint)0x00UL)
207 {
208 // rsdt表物理地址
209 ul rsdt_phys_base = rsdpv1->RsdtAddress & PAGE_2M_MASK;
210 acpi_RSDT_offset = rsdpv1->RsdtAddress - rsdt_phys_base;
211
212 kdebug("rsdpv1->RsdtAddress=%#018lx", rsdpv1->RsdtAddress);
213 //申请mmio空间
214 uint64_t size = 0;
215 mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_rsdt_virt_addr_base, &size);
216
217 // kdebug("acpi_rsdt_virt_addr_base = %#018lx,size= %#010lx", acpi_rsdt_virt_addr_base, size);
218 //映射rsdt表
219 paddr = (uint64_t)rsdt_phys_base;
220 mm_map(&initial_mm, acpi_rsdt_virt_addr_base, PAGE_2M_SIZE, paddr);
221 // rsdt表虚拟地址
222 rsdt = (struct acpi_RSDT_Structure_t *)(acpi_rsdt_virt_addr_base + acpi_RSDT_offset);
223 kdebug("RSDT mapped!");
224
225 // kdebug("length = %d",rsdt->header.Length);
226 // 计算RSDT Entry的数量
227 // kdebug("offset=%d", sizeof(rsdt->header));
228
229 acpi_RSDT_Entry_num = (rsdt->header.Length - 36) / 4;
230
231 printk_color(ORANGE, BLACK, "RSDT Length=%dbytes.\n", rsdt->header.Length);
232 printk_color(ORANGE, BLACK, "RSDT Entry num=%d\n", acpi_RSDT_Entry_num);
233
234 //申请mmio空间
235 mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_description_header_base, &size);
236
237 // 映射所有的Entry的物理地址
238 acpi_RSDT_entry_phys_base = ((ul)(rsdt->Entry)) & PAGE_2M_MASK;
239 // 由于地址只是32bit的,并且存在脏数据,这里需要手动清除高32bit,否则会触发#GP
240 acpi_RSDT_entry_phys_base = MASK_HIGH_32bit(acpi_RSDT_entry_phys_base);
241
242 paddr = (uint64_t)acpi_RSDT_entry_phys_base;
243 mm_map(&initial_mm, acpi_description_header_base, PAGE_2M_SIZE, paddr);
244 // kinfo("entry mapped!");
245
246 }
247 else
248 {
249 // should not reach here!
250 kBUG("At acpi_init(): Cannot get right SDT!");
251 while (1)
252 ;
253 }
254
255 kinfo("ACPI module initialized!");
256 return;
257 }
258