apic.c 17 KB

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