apic.c 20 KB

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