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