apic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. #include "apic.h"
  2. #include "../../../common/kprint.h"
  3. #include "../../../common/printk.h"
  4. #include "../../../common/cpu.h"
  5. #include "../../../common/glib.h"
  6. #include "../../../exception/gate.h"
  7. #include "../../acpi/acpi.h"
  8. #include <exception/softirq.h>
  9. // 导出定义在irq.c中的中段门表
  10. extern void (*interrupt_table[24])(void);
  11. bool flag_support_apic = false;
  12. bool flag_support_x2apic = false;
  13. uint local_apic_version;
  14. uint local_apic_max_LVT_entries;
  15. static struct acpi_Multiple_APIC_Description_Table_t *madt;
  16. static struct acpi_IO_APIC_Structure_t *io_apic_ICS;
  17. /**
  18. * @brief 初始化io_apic
  19. *
  20. */
  21. void apic_io_apic_init()
  22. {
  23. ul madt_addr;
  24. acpi_iter_SDT(acpi_get_MADT, &madt_addr);
  25. madt = (struct acpi_Multiple_APIC_Description_Table_t *)madt_addr;
  26. // kdebug("MADT->local intr controller addr=%#018lx", madt->Local_Interrupt_Controller_Address);
  27. // kdebug("MADT->length= %d bytes", madt->header.Length);
  28. // 寻找io apic的ICS
  29. void *ent = (void *)(madt_addr) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
  30. struct apic_Interrupt_Controller_Structure_header_t *header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
  31. while (header->length > 2)
  32. {
  33. header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
  34. if (header->type == 1)
  35. {
  36. struct acpi_IO_APIC_Structure_t *t = (struct acpi_IO_APIC_Structure_t *)ent;
  37. // kdebug("IO apic addr = %#018lx", t->IO_APIC_Address);
  38. io_apic_ICS = t;
  39. break;
  40. }
  41. ent += header->length;
  42. }
  43. // kdebug("Global_System_Interrupt_Base=%d", io_apic_ICS->Global_System_Interrupt_Base);
  44. apic_ioapic_map.addr_phys = io_apic_ICS->IO_APIC_Address;
  45. apic_ioapic_map.virtual_index_addr = (unsigned char *)APIC_IO_APIC_VIRT_BASE_ADDR;
  46. apic_ioapic_map.virtual_data_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x10);
  47. apic_ioapic_map.virtual_EOI_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x40);
  48. // kdebug("(ul)apic_ioapic_map.virtual_index_addr=%#018lx", (ul)apic_ioapic_map.virtual_index_addr);
  49. // 填写页表,完成地址映射
  50. mm_map_phys_addr((ul)apic_ioapic_map.virtual_index_addr, apic_ioapic_map.addr_phys, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD);
  51. // 设置IO APIC ID 为0x0f000000
  52. *apic_ioapic_map.virtual_index_addr = 0x00;
  53. io_mfence();
  54. *apic_ioapic_map.virtual_data_addr = 0x0f000000;
  55. io_mfence();
  56. // kdebug("I/O APIC ID:%#010x", ((*apic_ioapic_map.virtual_data_addr) >> 24) & 0xff);
  57. io_mfence();
  58. // 获取IO APIC Version
  59. *apic_ioapic_map.virtual_index_addr = 0x01;
  60. io_mfence();
  61. kdebug("IO APIC Version=%d, Max Redirection Entries=%d", *apic_ioapic_map.virtual_data_addr & 0xff, (((*apic_ioapic_map.virtual_data_addr) >> 16) & 0xff) + 1);
  62. // 初始化RTE表项,将所有RTE表项屏蔽
  63. for (int i = 0x10; i < 0x40; i += 2)
  64. {
  65. // 以0x20为起始中断向量号,初始化RTE
  66. apic_ioapic_write_rte(i, 0x10020 + ((i - 0x10) >> 1));
  67. }
  68. // 不需要手动启动IO APIC,只要初始化了RTE寄存器之后,io apic就会自动启用了。
  69. // 而且不是每台电脑都有RCBA寄存器,因此不需要手动启用IO APIC
  70. /*
  71. // get RCBA address
  72. io_out32(0xcf8, 0x8000f8f0);
  73. uint x = io_in32(0xcfc);
  74. uint *p;
  75. printk_color(RED, BLACK, "Get RCBA Address:%#010x\n", x);
  76. x = x & 0xffffc000UL;
  77. printk_color(RED, BLACK, "Get RCBA Address:%#010x\n", x);
  78. // get OIC address
  79. if (x > 0xfec00000 && x < 0xfee00000)
  80. {
  81. p = (unsigned int *)(x + 0x31feUL-apic_ioapic_map.addr_phys+apic_ioapic_map.virtual_index_addr);
  82. }
  83. // enable IOAPIC
  84. x = (*p & 0xffffff00) | 0x100;
  85. io_mfence();
  86. *p = x;
  87. io_mfence();
  88. */
  89. }
  90. /**
  91. * @brief 初始化AP处理器的Local apic
  92. *
  93. */
  94. void apic_init_ap_core_local_apic()
  95. {
  96. kinfo("Initializing AP-core's local apic...");
  97. uint eax, edx;
  98. // 启用xAPIC 和x2APIC
  99. __asm__ __volatile__("movq $0x1b, %%rcx \n\t" // 读取IA32_APIC_BASE寄存器
  100. "rdmsr \n\t"
  101. "bts $10, %%rax \n\t"
  102. "bts $11, %%rax \n\t"
  103. "wrmsr \n\t"
  104. "movq $0x1b, %%rcx \n\t"
  105. "rdmsr \n\t"
  106. : "=a"(eax), "=d"(edx)::"memory");
  107. // kdebug("After enable xAPIC and x2APIC: edx=%#010x, eax=%#010x", edx, eax);
  108. // 检测是否成功启用xAPIC和x2APIC
  109. if (eax & 0xc00)
  110. kinfo("xAPIC & x2APIC enabled!");
  111. // 设置SVR寄存器,开启local APIC、禁止EOI广播
  112. // enable SVR[8]
  113. __asm__ __volatile__("movq $0x80f, %%rcx \n\t"
  114. "rdmsr \n\t"
  115. "bts $8, %%rax \n\t"
  116. // "bts $12, %%rax\n\t"
  117. "wrmsr \n\t"
  118. "movq $0x80f, %%rcx \n\t"
  119. "rdmsr \n\t"
  120. : "=a"(eax), "=d"(edx)
  121. :
  122. : "memory");
  123. if (eax & 0x100)
  124. printk_color(RED, YELLOW, "SVR[8] enabled\n");
  125. if (edx & 0x1000)
  126. printk_color(RED, YELLOW, "SVR[12] enabled\n");
  127. // get local APIC ID
  128. __asm__ __volatile__("movq $0x802, %%rcx \n\t"
  129. "rdmsr \n\t"
  130. : "=a"(eax), "=d"(edx)
  131. :
  132. : "memory");
  133. printk_color(RED, YELLOW, "x2APIC ID:%#010x\n", eax);
  134. // 由于尚未配置LVT对应的处理程序,因此先屏蔽所有的LVT
  135. // mask all LVT
  136. __asm__ __volatile__( //"movq $0x82f, %%rcx \n\t" //CMCI
  137. //"wrmsr \n\t"
  138. "movq $0x832, %%rcx \n\t" // Timer
  139. "wrmsr \n\t"
  140. "movq $0x833, %%rcx \n\t" // Thermal Monitor
  141. "wrmsr \n\t"
  142. "movq $0x834, %%rcx \n\t" // Performance Counter
  143. "wrmsr \n\t"
  144. "movq $0x835, %%rcx \n\t" // LINT0
  145. "wrmsr \n\t"
  146. "movq $0x836, %%rcx \n\t" // LINT1
  147. "wrmsr \n\t"
  148. "movq $0x837, %%rcx \n\t" // Error
  149. "wrmsr \n\t"
  150. :
  151. : "a"(0x10000), "d"(0x00)
  152. : "memory");
  153. /*
  154. io_mfence();
  155. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_CMCI) = 0x1000000;
  156. io_mfence();
  157. kdebug("cmci = %#018lx", *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_CMCI));
  158. */
  159. //*(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER) = 0x10000;
  160. // io_mfence();
  161. /*
  162. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_THERMAL) = 0x1000000;
  163. io_mfence();
  164. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_PERFORMANCE_MONITOR) = 0x1000000;
  165. io_mfence();
  166. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT0) = 0x1000000;
  167. io_mfence();
  168. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT1) = 0x1000000;
  169. io_mfence();
  170. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_ERROR) = 0x1000000;
  171. io_mfence();
  172. */
  173. kdebug("All LVT Masked");
  174. }
  175. /**
  176. * @brief 初始化local apic
  177. *
  178. */
  179. void apic_local_apic_init()
  180. {
  181. // 映射Local APIC 寄存器地址
  182. mm_map_phys_addr(APIC_LOCAL_APIC_VIRT_BASE_ADDR, 0xfee00000, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD);
  183. uint a, b, c, d;
  184. cpu_cpuid(1, 0, &a, &b, &c, &d);
  185. // kdebug("CPUID 0x01, eax:%#010lx, ebx:%#010lx, ecx:%#010lx, edx:%#010lx", a, b, c, d);
  186. // 判断是否支持APIC和xAPIC
  187. if ((1 << 9) & d)
  188. {
  189. flag_support_apic = true;
  190. kdebug("This computer support APIC&xAPIC");
  191. }
  192. else
  193. {
  194. flag_support_apic = false;
  195. kerror("This computer does not support APIC&xAPIC");
  196. while (1)
  197. ;
  198. }
  199. // 判断是否支持x2APIC
  200. if ((1 << 21) & c)
  201. {
  202. flag_support_x2apic = true;
  203. kdebug("This computer support x2APIC");
  204. }
  205. else
  206. {
  207. kerror("This computer does not support x2APIC");
  208. }
  209. uint eax, edx;
  210. // 启用xAPIC 和x2APIC
  211. __asm__ __volatile__("movq $0x1b, %%rcx \n\t" // 读取IA32_APIC_BASE寄存器
  212. "rdmsr \n\t"
  213. "bts $10, %%rax \n\t"
  214. "bts $11, %%rax \n\t"
  215. "wrmsr \n\t"
  216. "movq $0x1b, %%rcx \n\t"
  217. "rdmsr \n\t"
  218. : "=a"(eax), "=d"(edx)::"memory");
  219. // kdebug("After enable xAPIC and x2APIC: edx=%#010x, eax=%#010x", edx, eax);
  220. // 检测是否成功启用xAPIC和x2APIC
  221. if (eax & 0xc00)
  222. kinfo("xAPIC & x2APIC enabled!");
  223. /*
  224. io_mfence();
  225. uint *svr = (uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_SVR);
  226. uint tmp_svr = *svr;
  227. tmp_svr &= (~(1 << 12));
  228. tmp_svr |= (1 << 8);
  229. kdebug("tmp_svr = %#018lx", tmp_svr);
  230. io_mfence();
  231. *svr = tmp_svr;
  232. io_mfence();
  233. kdebug("svr = %#018lx", *svr);
  234. */
  235. // 设置SVR寄存器,开启local APIC、禁止EOI广播
  236. __asm__ __volatile__("movq $0x80f, %%rcx \n\t"
  237. "rdmsr \n\t"
  238. "bts $8, %%rax \n\t"
  239. // "bts $12, %%rax \n\t"
  240. "movq $0x80f, %%rcx \n\t"
  241. "wrmsr \n\t"
  242. "movq $0x80f , %%rcx \n\t"
  243. "rdmsr \n\t"
  244. : "=a"(eax), "=d"(edx)::"memory");
  245. /*
  246. //enable SVR[8]
  247. __asm__ __volatile__( "movq $0x80f, %%rcx \n\t"
  248. "rdmsr \n\t"
  249. "bts $8, %%rax \n\t"
  250. "bts $12,%%rax\n\t"
  251. "wrmsr \n\t"
  252. "movq $0x80f, %%rcx \n\t"
  253. "rdmsr \n\t"
  254. :"=a"(eax),"=d"(edx)
  255. :
  256. :"memory");
  257. */
  258. // kdebug("After setting SVR: edx=%#010x, eax=%#010x", edx, eax);
  259. if (eax & 0x100)
  260. kinfo("APIC Software Enabled.");
  261. if (eax & 0x1000)
  262. kinfo("EOI-Broadcast Suppression Enabled.");
  263. // 获取Local APIC的基础信息 (参见英特尔开发手册Vol3A 10-39)
  264. // Table 10-6. Local APIC Register Address Map Supported by x2APIC
  265. // 获取 Local APIC ID
  266. // 0x802处是x2APIC ID 位宽32bits 的 Local APIC ID register
  267. /*
  268. __asm__ __volatile__("movq $0x802, %%rcx \n\t"
  269. "rdmsr \n\t"
  270. : "=a"(eax), "=d"(edx)::"memory");
  271. */
  272. // kdebug("get Local APIC ID: edx=%#010x, eax=%#010x", edx, eax);
  273. // kdebug("local_apic_id=%#018lx", *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_ID));
  274. // 获取Local APIC Version
  275. // 0x803处是 Local APIC Version register
  276. __asm__ __volatile__("movq $0x803, %%rcx \n\t"
  277. "rdmsr \n\t"
  278. : "=a"(eax), "=d"(edx)::"memory");
  279. local_apic_max_LVT_entries = ((eax >> 16) & 0xff) + 1;
  280. local_apic_version = eax & 0xff;
  281. kdebug("local APIC Version:%#010x,Max LVT Entry:%#010x,SVR(Suppress EOI Broadcast):%#04x\t", local_apic_version, local_apic_max_LVT_entries, (eax >> 24) & 0x1);
  282. if ((eax & 0xff) < 0x10)
  283. {
  284. kdebug("82489DX discrete APIC");
  285. }
  286. else if (((eax & 0xff) >= 0x10) && ((eax & 0xff) <= 0x15))
  287. kdebug("Integrated APIC.");
  288. // 由于尚未配置LVT对应的处理程序,因此先屏蔽所有的LVT
  289. // mask all LVT
  290. __asm__ __volatile__( //"movq $0x82f, %%rcx \n\t" //CMCI
  291. //"wrmsr \n\t"
  292. "movq $0x832, %%rcx \n\t" // Timer
  293. "wrmsr \n\t"
  294. "movq $0x833, %%rcx \n\t" // Thermal Monitor
  295. "wrmsr \n\t"
  296. "movq $0x834, %%rcx \n\t" // Performance Counter
  297. "wrmsr \n\t"
  298. "movq $0x835, %%rcx \n\t" // LINT0
  299. "wrmsr \n\t"
  300. "movq $0x836, %%rcx \n\t" // LINT1
  301. "wrmsr \n\t"
  302. "movq $0x837, %%rcx \n\t" // Error
  303. "wrmsr \n\t"
  304. :
  305. : "a"(0x10000), "d"(0x00)
  306. : "memory");
  307. /*
  308. io_mfence();
  309. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_CMCI) = 0x1000000;
  310. io_mfence();
  311. kdebug("cmci = %#018lx", *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_CMCI));
  312. */
  313. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER) = 0x10000;
  314. io_mfence();
  315. /*
  316. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_THERMAL) = 0x1000000;
  317. io_mfence();
  318. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_PERFORMANCE_MONITOR) = 0x1000000;
  319. io_mfence();
  320. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT0) = 0x1000000;
  321. io_mfence();
  322. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT1) = 0x1000000;
  323. io_mfence();
  324. *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_ERROR) = 0x1000000;
  325. io_mfence();
  326. */
  327. kdebug("All LVT Masked");
  328. /*
  329. // 获取TPR寄存器的值
  330. __asm__ __volatile__("movq $0x808, %%rcx \n\t"
  331. "rdmsr \n\t"
  332. : "=a"(eax), "=d"(edx)::"memory");
  333. kdebug("LVT_TPR=%#010x", eax);
  334. // 获取PPR寄存器的值
  335. __asm__ __volatile__("movq $0x80a, %%rcx \n\t"
  336. "rdmsr \n\t"
  337. : "=a"(eax), "=d"(edx)::"memory");
  338. kdebug("LVT_PPR=%#010x", eax);
  339. */
  340. }
  341. /**
  342. * @brief 初始化apic控制器
  343. *
  344. */
  345. void apic_init()
  346. {
  347. // 初始化中断门, 中断使用rsp0防止在软中断时发生嵌套,然后处理器重新加载导致数据被抹掉
  348. for (int i = 32; i <= 55; ++i)
  349. set_intr_gate(i, 0, interrupt_table[i - 32]);
  350. // 初始化主芯片
  351. io_out8(0x20, 0x11); // 初始化主芯片的icw1
  352. io_out8(0x21, 0x20); // 设置主芯片的中断向量号为0x20(0x20-0x27)
  353. io_out8(0x21, 0x04); // 设置int2端口级联从芯片
  354. io_out8(0x21, 0x01); // 设置为AEOI模式、FNM、无缓冲
  355. // 初始化从芯片
  356. io_out8(0xa0, 0x11);
  357. io_out8(0xa1, 0x28); // 设置从芯片的中断向量号为0x28(0x28-0x2f)
  358. io_out8(0xa1, 0x02); // 设置从芯片连接到主芯片的int2
  359. io_out8(0xa1, 0x01);
  360. // 屏蔽类8259A芯片
  361. io_mfence();
  362. io_out8(0x21, 0xff);
  363. io_mfence();
  364. io_out8(0xa1, 0xff);
  365. io_mfence();
  366. kdebug("8259A Masked.");
  367. // enable IMCR
  368. io_out8(0x22, 0x70);
  369. io_out8(0x23, 0x01);
  370. apic_local_apic_init();
  371. apic_io_apic_init();
  372. sti();
  373. }
  374. /**
  375. * @brief 中断服务程序
  376. *
  377. * @param rsp 中断栈指针
  378. * @param number 中断向量号
  379. */
  380. void do_IRQ(struct pt_regs *rsp, ul number)
  381. {
  382. switch (number & 0x80) // 以0x80为界限,低于0x80的是外部中断控制器,高于0x80的是Local APIC
  383. {
  384. case 0x00: // 外部中断控制器
  385. /* code */
  386. {
  387. irq_desc_t *irq = &interrupt_desc[number - 32];
  388. // 执行中断上半部处理程序
  389. if (irq->handler != NULL)
  390. irq->handler(number, irq->parameter, rsp);
  391. else
  392. kwarn("Intr vector [%d] does not have a handler!");
  393. // 向中断控制器发送应答消息
  394. if (irq->controller != NULL && irq->controller->ack != NULL)
  395. irq->controller->ack(number);
  396. else
  397. {
  398. // 向EOI寄存器写入0x00表示结束中断
  399. __asm__ __volatile__("movq $0x00, %%rdx \n\t"
  400. "movq $0x00, %%rax \n\t"
  401. "movq $0x80b, %%rcx \n\t"
  402. "wrmsr \n\t" ::
  403. : "memory");
  404. }
  405. }
  406. break;
  407. case 0x80:
  408. printk_color(RED, BLACK, "SMP IPI [ %d ]\n", number);
  409. apic_local_apic_edge_ack(number);
  410. default:
  411. kwarn("do IRQ receive: %d", number);
  412. break;
  413. }
  414. // 检测是否有未处理的软中断
  415. if (softirq_status != 0)
  416. do_softirq();
  417. }
  418. /**
  419. * @brief 读取RTE寄存器
  420. * 由于RTE位宽为64位而IO window寄存器只有32位,因此需要两次读取
  421. * @param index 索引值
  422. * @return ul
  423. */
  424. ul apic_ioapic_read_rte(unsigned char index)
  425. {
  426. // 由于处理器的乱序执行的问题,需要加入内存屏障以保证结果的正确性。
  427. ul ret;
  428. // 先读取高32bit
  429. *apic_ioapic_map.virtual_index_addr = index + 1;
  430. io_mfence();
  431. ret = *apic_ioapic_map.virtual_data_addr;
  432. ret <<= 32;
  433. io_mfence();
  434. // 读取低32bit
  435. *apic_ioapic_map.virtual_index_addr = index;
  436. io_mfence();
  437. ret |= *apic_ioapic_map.virtual_data_addr;
  438. io_mfence();
  439. return ret;
  440. }
  441. /**
  442. * @brief 写入RTE寄存器
  443. *
  444. * @param index 索引值
  445. * @param value 要写入的值
  446. */
  447. void apic_ioapic_write_rte(unsigned char index, ul value)
  448. {
  449. // 先写入低32bit
  450. *apic_ioapic_map.virtual_index_addr = index;
  451. io_mfence();
  452. *apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
  453. io_mfence();
  454. // 再写入高32bit
  455. value >>= 32;
  456. io_mfence();
  457. *apic_ioapic_map.virtual_index_addr = index + 1;
  458. io_mfence();
  459. *apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
  460. io_mfence();
  461. }
  462. // =========== 中断控制操作接口 ============
  463. void apic_ioapic_enable(ul irq_num)
  464. {
  465. ul index = 0x10 + ((irq_num - 32) << 1);
  466. ul value = apic_ioapic_read_rte(index);
  467. value &= (~0x10000UL);
  468. apic_ioapic_write_rte(index, value);
  469. }
  470. void apic_ioapic_disable(ul irq_num)
  471. {
  472. ul index = 0x10 + ((irq_num - 32) << 1);
  473. ul value = apic_ioapic_read_rte(index);
  474. value |= (0x10000UL);
  475. apic_ioapic_write_rte(index, value);
  476. }
  477. ul apic_ioapic_install(ul irq_num, void *arg)
  478. {
  479. struct apic_IO_APIC_RTE_entry *entry = (struct apic_IO_APIC_RTE_entry *)arg;
  480. // RTE表项值写入对应的RTE寄存器
  481. apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), *(ul *)entry);
  482. return 0;
  483. }
  484. void apic_ioapic_uninstall(ul irq_num)
  485. {
  486. // 将对应的RTE表项设置为屏蔽状态
  487. apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), 0x10000UL);
  488. }
  489. void apic_ioapic_level_ack(ul irq_num) // 电平触发
  490. {
  491. // 向EOI寄存器写入0x00表示结束中断
  492. /*io_mfence();
  493. uint *eoi = (uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI);
  494. *eoi = 0x00;
  495. io_mfence(); */
  496. __asm__ __volatile__("movq $0x00, %%rdx \n\t"
  497. "movq $0x00, %%rax \n\t"
  498. "movq $0x80b, %%rcx \n\t"
  499. "wrmsr \n\t" ::
  500. : "memory");
  501. *apic_ioapic_map.virtual_EOI_addr = irq_num;
  502. }
  503. void apic_ioapic_edge_ack(ul irq_num) // 边沿触发
  504. {
  505. // 向EOI寄存器写入0x00表示结束中断
  506. /*
  507. uint *eoi = (uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI);
  508. *eoi = 0x00;
  509. */
  510. __asm__ __volatile__("movq $0x00, %%rdx \n\t"
  511. "movq $0x00, %%rax \n\t"
  512. "movq $0x80b, %%rcx \n\t"
  513. "wrmsr \n\t" ::
  514. : "memory");
  515. }
  516. /**
  517. * @brief local apic 边沿触发应答
  518. *
  519. * @param irq_num
  520. */
  521. void apic_local_apic_edge_ack(ul irq_num)
  522. {
  523. // 向EOI寄存器写入0x00表示结束中断
  524. __asm__ __volatile__("movq $0x00, %%rdx \n\t"
  525. "movq $0x00, %%rax \n\t"
  526. "movq $0x80b, %%rcx \n\t"
  527. "wrmsr \n\t" ::
  528. : "memory");
  529. }
  530. /**
  531. * @brief 读取指定类型的 Interrupt Control Structure
  532. *
  533. * @param type ics的类型
  534. * @param ret_vaddr 对应的ICS的虚拟地址数组
  535. * @param total 返回数组的元素总个数
  536. * @return uint
  537. */
  538. uint apic_get_ics(const uint type, ul ret_vaddr[], uint *total)
  539. {
  540. void *ent = (void *)(madt) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
  541. struct apic_Interrupt_Controller_Structure_header_t *header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
  542. bool flag = false;
  543. uint cnt = 0;
  544. while (header->length > 2)
  545. {
  546. header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
  547. if (header->type == type)
  548. {
  549. ret_vaddr[cnt++] = (ul)ent;
  550. flag = true;
  551. }
  552. ent += header->length;
  553. }
  554. *total = cnt;
  555. if (!flag)
  556. return APIC_E_NOTFOUND;
  557. else
  558. return APIC_SUCCESS;
  559. }
  560. /**
  561. * @brief 构造RTE Entry结构体
  562. *
  563. * @param entry 返回的结构体
  564. * @param vector 中断向量
  565. * @param deliver_mode 投递模式
  566. * @param dest_mode 目标模式
  567. * @param deliver_status 投递状态
  568. * @param polarity 电平触发极性
  569. * @param irr 远程IRR标志位(只读)
  570. * @param trigger 触发模式
  571. * @param mask 屏蔽标志位,(0为未屏蔽, 1为已屏蔽)
  572. * @param dest_apicID 目标apicID
  573. */
  574. void apic_make_rte_entry(struct apic_IO_APIC_RTE_entry *entry, uint8_t vector, uint8_t deliver_mode, uint8_t dest_mode,
  575. uint8_t deliver_status, uint8_t polarity, uint8_t irr, uint8_t trigger, uint8_t mask, uint8_t dest_apicID)
  576. {
  577. entry->vector = vector;
  578. entry->deliver_mode = deliver_mode;
  579. entry->dest_mode = dest_mode;
  580. entry->deliver_status = deliver_status;
  581. entry->polarity = polarity;
  582. entry->remote_IRR = irr;
  583. entry->trigger_mode = trigger;
  584. entry->mask = mask;
  585. entry->reserved = 0;
  586. if (dest_mode == DEST_PHYSICAL)
  587. {
  588. entry->destination.physical.phy_dest = dest_apicID;
  589. entry->destination.physical.reserved1 = 0;
  590. entry->destination.physical.reserved2 = 0;
  591. }
  592. else
  593. {
  594. entry->destination.logical.logical_dest = dest_apicID;
  595. entry->destination.logical.reserved1 = 0;
  596. }
  597. }