apic.c 20 KB

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