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 初始化acpi模块
108 *
109 */
110 // todo: 修复bug:当物理机上提供了rsdpv2之后,rsdpv1是不提供的(物理地址为0),因此需要手动判断rsdp的版本信息,然后做对应的解析。
acpi_init()111 void acpi_init()
112 {
113 kinfo("Initializing ACPI...");
114
115 // 获取物理地址
116 int reserved;
117
118 multiboot2_iter(multiboot2_get_acpi_old_RSDP, &old_acpi, &reserved);
119 rsdpv1 = &(old_acpi.rsdp);
120
121 multiboot2_iter(multiboot2_get_acpi_new_RSDP, &new_acpi, &reserved);
122 rsdpv2 = &(new_acpi.rsdp);
123
124 uint64_t paddr = 0;
125 // An ACPI-compatible OS must use the XSDT if present
126 if (rsdpv2->XsdtAddress != 0x00UL)
127 {
128 // 不要删除这段注释(因为还不确定是代码的bug,还是真机的bug)
129 /*
130 acpi_use_xsdt = true;
131 ul xsdt_phys_base = rsdpv2->XsdtAddress & PAGE_2M_MASK;
132 acpi_XSDT_offset = rsdpv2->XsdtAddress - xsdt_phys_base;
133 mm_map_phys_addr(ACPI_XSDT_VIRT_ADDR_BASE, xsdt_phys_base, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
134 kdebug("XSDT mapped!");
135
136 xsdt = (struct acpi_XSDT_Structure_t *)(ACPI_XSDT_VIRT_ADDR_BASE + acpi_XSDT_offset);
137 // 计算RSDT Entry的数量
138 kdebug("offset=%d", sizeof(xsdt->header));
139 kdebug("xsdt sign=%s", xsdt->header.Signature);
140 acpi_XSDT_Entry_num = (xsdt->header.Length - sizeof(xsdt->header)) / 8;
141
142 printk_color(ORANGE, BLACK, "XSDT Length=%dbytes.\n", xsdt->header.Length);
143 printk_color(ORANGE, BLACK, "XSDT Entry num=%d\n", acpi_XSDT_Entry_num);
144
145 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);
146 // 映射所有的Entry的物理地址
147 ul *ent = &(xsdt->Entry);
148 for (int j = 0; j < acpi_XSDT_Entry_num; ++j)
149 {
150 kdebug("entry=%#018lx, virt=%#018lx", (*(ent + j)) & PAGE_2M_MASK, ACPI_XSDT_DESCRIPTION_HEDERS_BASE + PAGE_2M_SIZE * j);
151 // 映射RSDT ENTRY的物理地址
152 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);
153 }
154 */
155
156 // 由于解析XSDT出现问题。暂时只使用Rsdpv2的rsdt,但是这是不符合ACPI规范的!!!
157 ul rsdt_phys_base = rsdpv2->rsdp1.RsdtAddress & PAGE_2M_MASK;
158 acpi_RSDT_offset = rsdpv2->rsdp1.RsdtAddress - rsdt_phys_base;
159
160 //申请mmio空间
161 uint64_t size = 0;
162 mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_rsdt_virt_addr_base, &size);
163
164 //映射rsdt表
165 paddr = (uint64_t)rsdt_phys_base;
166 mm_map(&initial_mm, acpi_rsdt_virt_addr_base, PAGE_2M_SIZE, paddr);
167
168 // rsdt表虚拟地址
169 rsdt = (struct acpi_RSDT_Structure_t *)(acpi_rsdt_virt_addr_base + acpi_RSDT_offset);
170 kdebug("RSDT mapped!(v2)");
171
172 // 计算RSDT Entry的数量
173 kdebug("offset=%d", sizeof(rsdt->header));
174 acpi_RSDT_Entry_num = (rsdt->header.Length - 36) / 4;
175
176 printk_color(ORANGE, BLACK, "RSDT Length=%dbytes.\n", rsdt->header.Length);
177 printk_color(ORANGE, BLACK, "RSDT Entry num=%d\n", acpi_RSDT_Entry_num);
178
179 //申请mmio空间
180 mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_description_header_base, &size);
181
182 // 映射所有的Entry的物理地址
183 acpi_RSDT_entry_phys_base = ((ul)(rsdt->Entry)) & PAGE_2M_MASK;
184 // 由于地址只是32bit的,并且存在脏数据,这里需要手动清除高32bit,否则会触发#GP
185 acpi_RSDT_entry_phys_base = MASK_HIGH_32bit(acpi_RSDT_entry_phys_base);
186
187 paddr = (uint64_t)acpi_RSDT_entry_phys_base;
188 mm_map(&initial_mm, acpi_description_header_base, PAGE_2M_SIZE, paddr);
189 }
190 else if (rsdpv1->RsdtAddress != (uint)0x00UL)
191 {
192 // rsdt表物理地址
193 ul rsdt_phys_base = rsdpv1->RsdtAddress & PAGE_2M_MASK;
194 acpi_RSDT_offset = rsdpv1->RsdtAddress - rsdt_phys_base;
195
196 kdebug("rsdpv1->RsdtAddress=%#018lx", rsdpv1->RsdtAddress);
197 //申请mmio空间
198 uint64_t size = 0;
199 mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_rsdt_virt_addr_base, &size);
200
201 // kdebug("acpi_rsdt_virt_addr_base = %#018lx,size= %#010lx", acpi_rsdt_virt_addr_base, size);
202 //映射rsdt表
203 paddr = (uint64_t)rsdt_phys_base;
204 mm_map(&initial_mm, acpi_rsdt_virt_addr_base, PAGE_2M_SIZE, paddr);
205 // rsdt表虚拟地址
206 rsdt = (struct acpi_RSDT_Structure_t *)(acpi_rsdt_virt_addr_base + acpi_RSDT_offset);
207 kdebug("RSDT mapped!");
208
209 // kdebug("length = %d",rsdt->header.Length);
210 // 计算RSDT Entry的数量
211 // kdebug("offset=%d", sizeof(rsdt->header));
212
213 acpi_RSDT_Entry_num = (rsdt->header.Length - 36) / 4;
214
215 printk_color(ORANGE, BLACK, "RSDT Length=%dbytes.\n", rsdt->header.Length);
216 printk_color(ORANGE, BLACK, "RSDT Entry num=%d\n", acpi_RSDT_Entry_num);
217
218 //申请mmio空间
219 mmio_create(PAGE_2M_SIZE, VM_IO | VM_DONTCOPY, &acpi_description_header_base, &size);
220
221 // 映射所有的Entry的物理地址
222 acpi_RSDT_entry_phys_base = ((ul)(rsdt->Entry)) & PAGE_2M_MASK;
223 // 由于地址只是32bit的,并且存在脏数据,这里需要手动清除高32bit,否则会触发#GP
224 acpi_RSDT_entry_phys_base = MASK_HIGH_32bit(acpi_RSDT_entry_phys_base);
225
226 paddr = (uint64_t)acpi_RSDT_entry_phys_base;
227 mm_map(&initial_mm, acpi_description_header_base, PAGE_2M_SIZE, paddr);
228 // kinfo("entry mapped!");
229
230 }
231 else
232 {
233 // should not reach here!
234 kBUG("At acpi_init(): Cannot get right SDT!");
235 while (1)
236 ;
237 }
238
239 kinfo("ACPI module initialized!");
240 return;
241 }
242